From 0dea8806653adf888498843af9aad7f3e00ba4f9 Mon Sep 17 00:00:00 2001 From: PepperBell <44146161+PepperCode1@users.noreply.github.com> Date: Tue, 27 Jul 2021 13:25:16 -0700 Subject: [PATCH] Stress config value refactor - Add StressConfigValues to allow registering stress value providers and getting values by namespace - Use correct namespace inside StressConfigDefaults builder transformers - Expose StressConfigDefaults maps - Add methods to manually register default stress values - Only add stress configs for defaults that have the create namespace - Clean up ItemDescription#getKineticStats --- log.txt | 2 + src/main/java/com/simibubi/create/Create.java | 7 +- .../contraptions/base/KineticTileEntity.java | 15 ++-- .../engine/FurnaceEngineTileEntity.java | 4 +- .../create/foundation/config/AllConfigs.java | 7 +- .../create/foundation/config/CStress.java | 64 +++++++++++--- .../config/StressConfigDefaults.java | 19 ++-- .../foundation/config/StressConfigValues.java | 86 +++++++++++++++++++ .../foundation/item/ItemDescription.java | 56 ++++++------ 9 files changed, 197 insertions(+), 63 deletions(-) create mode 100644 src/main/java/com/simibubi/create/foundation/config/StressConfigValues.java diff --git a/log.txt b/log.txt index ce8856c74..d65b8d57b 100644 --- a/log.txt +++ b/log.txt @@ -1,5 +1,6 @@ - Bump minimum required Forge version to 36.2.0 +- Added the Creative Blaze Cake - Fixed backtank not rendering on all players - Added config options that allow some more customization of the goggle overlay - Fixed crash when simultaneously converting and destroying a Peculiar Bell @@ -17,6 +18,7 @@ - Added support for custom namespaces in ponder - Make ponder files use Minecraft's resource system - Allow easily registering and generating ponder lang for any namespace +- Allow registering stress value providers and getting values by namespace - Create now partially uses the Official Mappings provided by Mojang - Fixed upright items rendered on depots - Fixed invalid textures on Potato cannon model diff --git a/src/main/java/com/simibubi/create/Create.java b/src/main/java/com/simibubi/create/Create.java index 272da5d28..5935dfb7e 100644 --- a/src/main/java/com/simibubi/create/Create.java +++ b/src/main/java/com/simibubi/create/Create.java @@ -47,6 +47,7 @@ import net.minecraftforge.event.world.BiomeLoadingEvent; import net.minecraftforge.eventbus.api.EventPriority; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.fml.DistExecutor; +import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.event.lifecycle.GatherDataEvent; @@ -89,8 +90,10 @@ public class Create { AllTileEntities.register(); AllMovementBehaviours.register(); AllWorldFeatures.register(); - AllConfigs.register(); AllEnchantments.register(); + AllConfigs.register(ModLoadingContext.get()); + + ForgeMod.enableMilkFluid(); IEventBus modEventBus = FMLJavaModLoadingContext.get() .getModEventBus(); @@ -109,8 +112,6 @@ public class Create { DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> CreateClient.addClientListeners(forgeEventBus, modEventBus)); - - ForgeMod.enableMilkFluid(); } public static void init(final FMLCommonSetupEvent event) { diff --git a/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntity.java index ec172249f..e04468b45 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/base/KineticTileEntity.java @@ -19,6 +19,7 @@ import com.simibubi.create.content.contraptions.goggles.IHaveHoveringInformation import com.simibubi.create.content.contraptions.relays.elementary.ICogWheel; import com.simibubi.create.content.contraptions.relays.gearbox.GearboxBlock; import com.simibubi.create.foundation.config.AllConfigs; +import com.simibubi.create.foundation.config.StressConfigValues; import com.simibubi.create.foundation.item.TooltipHelper; import com.simibubi.create.foundation.sound.SoundScapes; import com.simibubi.create.foundation.sound.SoundScapes.AmbienceGroup; @@ -160,22 +161,22 @@ public abstract class KineticTileEntity extends SmartTileEntity } } - public float calculateAddedStressCapacity() { - float capacity = (float) AllConfigs.SERVER.kinetics.stressValues.getCapacityOf(getStressConfigKey()); - this.lastCapacityProvided = capacity; - return capacity; - } - protected Block getStressConfigKey() { return getBlockState().getBlock(); } public float calculateStressApplied() { - float impact = (float) AllConfigs.SERVER.kinetics.stressValues.getImpactOf(getStressConfigKey()); + float impact = (float) StressConfigValues.getImpact(getStressConfigKey()); this.lastStressApplied = impact; return impact; } + public float calculateAddedStressCapacity() { + float capacity = (float) StressConfigValues.getCapacity(getStressConfigKey()); + this.lastCapacityProvided = capacity; + return capacity; + } + public void onSpeedChanged(float previousSpeed) { boolean fromOrToZero = (previousSpeed == 0) != (getSpeed() == 0); boolean directionSwap = !fromOrToZero && Math.signum(previousSpeed) != Math.signum(getSpeed()); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/FurnaceEngineTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/FurnaceEngineTileEntity.java index 8d886b036..3cd4cd0b8 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/FurnaceEngineTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/flywheel/engine/FurnaceEngineTileEntity.java @@ -1,7 +1,7 @@ package com.simibubi.create.content.contraptions.components.flywheel.engine; import com.simibubi.create.AllBlocks; -import com.simibubi.create.foundation.config.AllConfigs; +import com.simibubi.create.foundation.config.StressConfigValues; import net.minecraft.block.AbstractFurnaceBlock; import net.minecraft.block.BlockState; @@ -29,7 +29,7 @@ public class FurnaceEngineTileEntity extends EngineTileEntity { boolean active = state.hasProperty(AbstractFurnaceBlock.LIT) && state.getValue(AbstractFurnaceBlock.LIT); float speed = active ? 16 * modifier : 0; float capacity = - (float) (active ? AllConfigs.SERVER.kinetics.stressValues.getCapacityOf(AllBlocks.FURNACE_ENGINE.get()) + (float) (active ? StressConfigValues.getCapacity(AllBlocks.FURNACE_ENGINE.get()) : 0); appliedCapacity = capacity; diff --git a/src/main/java/com/simibubi/create/foundation/config/AllConfigs.java b/src/main/java/com/simibubi/create/foundation/config/AllConfigs.java index 996d9242b..9354d53f9 100644 --- a/src/main/java/com/simibubi/create/foundation/config/AllConfigs.java +++ b/src/main/java/com/simibubi/create/foundation/config/AllConfigs.java @@ -33,14 +33,15 @@ public class AllConfigs { return config; } - public static void register() { + public static void register(ModLoadingContext context) { CLIENT = register(CClient::new, ModConfig.Type.CLIENT); COMMON = register(CCommon::new, ModConfig.Type.COMMON); SERVER = register(CServer::new, ModConfig.Type.SERVER); for (Entry pair : configs.entrySet()) - ModLoadingContext.get() - .registerConfig(pair.getValue(), pair.getKey().specification); + context.registerConfig(pair.getValue(), pair.getKey().specification); + + StressConfigValues.registerProvider(context.getActiveNamespace(), SERVER.kinetics.stressValues); } public static void onLoad(ModConfig.Loading event) { diff --git a/src/main/java/com/simibubi/create/foundation/config/CStress.java b/src/main/java/com/simibubi/create/foundation/config/CStress.java index 8f1450fa9..3ad7306a0 100644 --- a/src/main/java/com/simibubi/create/foundation/config/CStress.java +++ b/src/main/java/com/simibubi/create/foundation/config/CStress.java @@ -3,12 +3,17 @@ package com.simibubi.create.foundation.config; import java.util.HashMap; import java.util.Map; +import com.simibubi.create.AllBlocks; +import com.simibubi.create.Create; +import com.simibubi.create.content.contraptions.components.crank.ValveHandleBlock; +import com.simibubi.create.foundation.config.StressConfigValues.IStressValueProvider; + import net.minecraft.block.Block; import net.minecraft.util.ResourceLocation; import net.minecraftforge.common.ForgeConfigSpec.Builder; import net.minecraftforge.common.ForgeConfigSpec.ConfigValue; -public class CStress extends ConfigBase { +public class CStress extends ConfigBase implements IStressValueProvider { private Map> capacities = new HashMap<>(); private Map> impacts = new HashMap<>(); @@ -17,32 +22,67 @@ public class CStress extends ConfigBase { protected void registerAll(Builder builder) { builder.comment("", Comments.su, Comments.impact) .push("impact"); - StressConfigDefaults.registeredDefaultImpacts - .forEach((r, i) -> getImpacts().put(r, builder.define(r.getPath(), i))); + StressConfigDefaults.DEFAULT_IMPACTS + .forEach((r, i) -> { + if (r.getNamespace().equals(Create.ID)) + getImpacts().put(r, builder.define(r.getPath(), i)); + }); builder.pop(); builder.comment("", Comments.su, Comments.capacity) .push("capacity"); - StressConfigDefaults.registeredDefaultCapacities - .forEach((r, i) -> getCapacities().put(r, builder.define(r.getPath(), i))); + StressConfigDefaults.DEFAULT_CAPACITIES + .forEach((r, i) -> { + if (r.getNamespace().equals(Create.ID)) + getCapacities().put(r, builder.define(r.getPath(), i)); + }); builder.pop(); } - public double getImpactOf(Block block) { + @Override + public double getImpact(Block block) { + block = redirectValues(block); ResourceLocation key = block.getRegistryName(); - return getImpacts().containsKey(key) ? getImpacts().get(key) - .get() : 0; + ConfigValue value = getImpacts().get(key); + if (value != null) { + return value.get(); + } + return 0; } - public double getCapacityOf(Block block) { + @Override + public double getCapacity(Block block) { + block = redirectValues(block); ResourceLocation key = block.getRegistryName(); - return getCapacities().containsKey(key) ? getCapacities().get(key) - .get() : 0; + ConfigValue value = getCapacities().get(key); + if (value != null) { + return value.get(); + } + return 0; + } + + public boolean hasImpact(Block block) { + block = redirectValues(block); + ResourceLocation key = block.getRegistryName(); + return getImpacts().containsKey(key); + } + + public boolean hasCapacity(Block block) { + block = redirectValues(block); + ResourceLocation key = block.getRegistryName(); + return getCapacities().containsKey(key); + } + + protected Block redirectValues(Block block) { + if (block instanceof ValveHandleBlock) { + return AllBlocks.HAND_CRANK.get(); + } + return block; } @Override public String getName() { - return "stressValues.v" + StressConfigDefaults.forcedUpdateVersion; + return "stressValues.v" + StressConfigDefaults.FORCED_UPDATE_VERSION; } public Map> getImpacts() { diff --git a/src/main/java/com/simibubi/create/foundation/config/StressConfigDefaults.java b/src/main/java/com/simibubi/create/foundation/config/StressConfigDefaults.java index c93103f82..d17b4f964 100644 --- a/src/main/java/com/simibubi/create/foundation/config/StressConfigDefaults.java +++ b/src/main/java/com/simibubi/create/foundation/config/StressConfigDefaults.java @@ -3,7 +3,6 @@ package com.simibubi.create.foundation.config; import java.util.HashMap; import java.util.Map; -import com.simibubi.create.Create; import com.tterrag.registrate.builders.BlockBuilder; import com.tterrag.registrate.util.nullness.NonNullUnaryOperator; @@ -17,10 +16,18 @@ public class StressConfigDefaults { * Worlds from the previous version will overwrite potentially changed values * with the new defaults. */ - public static final int forcedUpdateVersion = 2; + public static final int FORCED_UPDATE_VERSION = 2; - static Map registeredDefaultImpacts = new HashMap<>(); - static Map registeredDefaultCapacities = new HashMap<>(); + public static final Map DEFAULT_IMPACTS = new HashMap<>(); + public static final Map DEFAULT_CAPACITIES = new HashMap<>(); + + public static void setDefaultImpact(ResourceLocation blockId, double impact) { + DEFAULT_IMPACTS.put(blockId, impact); + } + + public static void setDefaultCapacity(ResourceLocation blockId, double capacity) { + DEFAULT_CAPACITIES.put(blockId, capacity); + } public static NonNullUnaryOperator> setNoImpact() { return setImpact(0); @@ -28,14 +35,14 @@ public class StressConfigDefaults { public static NonNullUnaryOperator> setImpact(double impact) { return b -> { - registeredDefaultImpacts.put(Create.asResource(b.getName()), impact); + setDefaultImpact(new ResourceLocation(b.getOwner().getModid(), b.getName()), impact); return b; }; } public static NonNullUnaryOperator> setCapacity(double capacity) { return b -> { - registeredDefaultCapacities.put(Create.asResource(b.getName()), capacity); + setDefaultCapacity(new ResourceLocation(b.getOwner().getModid(), b.getName()), capacity); return b; }; } diff --git a/src/main/java/com/simibubi/create/foundation/config/StressConfigValues.java b/src/main/java/com/simibubi/create/foundation/config/StressConfigValues.java new file mode 100644 index 000000000..837122185 --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/config/StressConfigValues.java @@ -0,0 +1,86 @@ +package com.simibubi.create.foundation.config; + +import java.util.HashMap; +import java.util.Map; + +import javax.annotation.Nullable; + +import net.minecraft.block.Block; +import net.minecraft.util.ResourceLocation; + +public class StressConfigValues { + + private static final Map PROVIDERS = new HashMap<>(); + + public static void registerProvider(String namespace, IStressValueProvider provider) { + PROVIDERS.put(namespace, provider); + } + + @Nullable + public static IStressValueProvider getProvider(String namespace) { + return PROVIDERS.get(namespace); + } + + @Nullable + public static IStressValueProvider getProvider(Block block) { + ResourceLocation key = block.getRegistryName(); + String namespace = key.getNamespace(); + IStressValueProvider provider = getProvider(namespace); + return provider; + } + + public static double getImpact(Block block) { + IStressValueProvider provider = getProvider(block); + if (provider != null) { + return provider.getImpact(block); + } + return 0; + } + + public static double getCapacity(Block block) { + IStressValueProvider provider = getProvider(block); + if (provider != null) { + return provider.getCapacity(block); + } + return 0; + } + + public static boolean hasImpact(Block block) { + IStressValueProvider provider = getProvider(block); + if (provider != null) { + return provider.hasImpact(block); + } + return false; + } + + public static boolean hasCapacity(Block block) { + IStressValueProvider provider = getProvider(block); + if (provider != null) { + return provider.hasCapacity(block); + } + return false; + } + + public interface IStressValueProvider { + /** + * Gets the impact of a block. + * + * @param block The block. + * @return the impact value of the block, or 0 if it does not have one. + */ + double getImpact(Block block); + + /** + * Gets the capacity of a block. + * + * @param block The block. + * @return the capacity value of the block, or 0 if it does not have one. + */ + double getCapacity(Block block); + + boolean hasImpact(Block block); + + boolean hasCapacity(Block block); + } + +} diff --git a/src/main/java/com/simibubi/create/foundation/item/ItemDescription.java b/src/main/java/com/simibubi/create/foundation/item/ItemDescription.java index f564d0456..e38d64c6e 100644 --- a/src/main/java/com/simibubi/create/foundation/item/ItemDescription.java +++ b/src/main/java/com/simibubi/create/foundation/item/ItemDescription.java @@ -20,32 +20,28 @@ import static net.minecraft.util.text.TextFormatting.YELLOW; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Map; import com.simibubi.create.AllBlocks; import com.simibubi.create.AllItems; import com.simibubi.create.content.contraptions.base.IRotate; import com.simibubi.create.content.contraptions.base.IRotate.SpeedLevel; import com.simibubi.create.content.contraptions.base.IRotate.StressImpact; -import com.simibubi.create.content.contraptions.components.crank.ValveHandleBlock; import com.simibubi.create.content.contraptions.components.fan.EncasedFanBlock; -import com.simibubi.create.content.contraptions.components.flywheel.engine.EngineBlock; import com.simibubi.create.content.contraptions.components.flywheel.engine.FurnaceEngineBlock; import com.simibubi.create.content.contraptions.components.waterwheel.WaterWheelBlock; import com.simibubi.create.foundation.config.AllConfigs; import com.simibubi.create.foundation.config.CKinetics; +import com.simibubi.create.foundation.config.StressConfigValues; import com.simibubi.create.foundation.utility.Lang; import net.minecraft.block.Block; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screen.Screen; import net.minecraft.inventory.EquipmentSlotType; -import net.minecraft.util.ResourceLocation; import net.minecraft.util.text.IFormattableTextComponent; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.StringTextComponent; import net.minecraft.util.text.TextFormatting; -import net.minecraftforge.common.ForgeConfigSpec.ConfigValue; public class ItemDescription { @@ -93,23 +89,26 @@ public class ItemDescription { public static List getKineticStats(Block block) { List list = new ArrayList<>(); - boolean isEngine = block instanceof EngineBlock; - boolean isHandle = block instanceof ValveHandleBlock; CKinetics config = AllConfigs.SERVER.kinetics; - SpeedLevel minimumRequiredSpeedLevel = - isEngine ? SpeedLevel.NONE : ((IRotate) block).getMinimumRequiredSpeedLevel(); - boolean hasSpeedRequirement = minimumRequiredSpeedLevel != SpeedLevel.NONE; - ResourceLocation id = block.getRegistryName(); - Map> impacts = config.stressValues.getImpacts(); - Map> capacities = config.stressValues.getCapacities(); - boolean hasStressImpact = impacts.containsKey(id) && impacts.get(id) - .get() > 0 && StressImpact.isEnabled(); - boolean hasStressCapacity = (isHandle || capacities.containsKey(id)) && StressImpact.isEnabled(); - boolean hasGlasses = - AllItems.GOGGLES.get() == Minecraft.getInstance().player.getItemBySlot(EquipmentSlotType.HEAD) - .getItem(); - ITextComponent rpmUnit = Lang.translate("generic.unit.rpm"); + + boolean hasGoggles = + AllItems.GOGGLES.isIn(Minecraft.getInstance().player.getItemBySlot(EquipmentSlotType.HEAD)); + + SpeedLevel minimumRequiredSpeedLevel; + boolean showStressImpact; + if (!(block instanceof IRotate)) { + minimumRequiredSpeedLevel = SpeedLevel.NONE; + showStressImpact = true; + } else { + minimumRequiredSpeedLevel = ((IRotate) block).getMinimumRequiredSpeedLevel(); + showStressImpact = !((IRotate) block).hideStressImpact(); + } + + boolean hasSpeedRequirement = minimumRequiredSpeedLevel != SpeedLevel.NONE; + boolean hasStressImpact = StressImpact.isEnabled() && showStressImpact && StressConfigValues.getImpact(block) > 0; + boolean hasStressCapacity = StressImpact.isEnabled() && StressConfigValues.hasCapacity(block); + if (hasSpeedRequirement) { List speedLevels = Lang.translatedOptions("tooltip.speedRequirement", "none", "medium", "high"); @@ -117,7 +116,7 @@ public class ItemDescription { IFormattableTextComponent level = new StringTextComponent(makeProgressBar(3, index)).withStyle(minimumRequiredSpeedLevel.getTextColor()); - if (hasGlasses) + if (hasGoggles) level.append(String.valueOf(minimumRequiredSpeedLevel.getSpeedValue())) .append(rpmUnit) .append("+"); @@ -129,19 +128,17 @@ public class ItemDescription { list.add(level); } - if (hasStressImpact && !(!isEngine && ((IRotate) block).hideStressImpact())) { + if (hasStressImpact) { List stressLevels = Lang.translatedOptions("tooltip.stressImpact", "low", "medium", "high"); - double impact = impacts.get(id) - .get(); + double impact = StressConfigValues.getImpact(block); StressImpact impactId = impact >= config.highStressImpact.get() ? StressImpact.HIGH : (impact >= config.mediumStressImpact.get() ? StressImpact.MEDIUM : StressImpact.LOW); int index = impactId.ordinal(); IFormattableTextComponent level = new StringTextComponent(makeProgressBar(3, index)).withStyle(impactId.getAbsoluteColor()); - if (hasGlasses) - level.append(impacts.get(id) - .get() + "x ") + if (hasGoggles) + level.append(impact + "x ") .append(rpmUnit); else level.append(stressLevels.get(index)); @@ -154,15 +151,14 @@ public class ItemDescription { if (hasStressCapacity) { List stressCapacityLevels = Lang.translatedOptions("tooltip.capacityProvided", "low", "medium", "high"); - double capacity = capacities.get(isHandle ? AllBlocks.HAND_CRANK.getId() : id) - .get(); + double capacity = StressConfigValues.getCapacity(block); StressImpact impactId = capacity >= config.highCapacity.get() ? StressImpact.LOW : (capacity >= config.mediumCapacity.get() ? StressImpact.MEDIUM : StressImpact.HIGH); int index = StressImpact.values().length - 2 - impactId.ordinal(); IFormattableTextComponent level = new StringTextComponent(makeProgressBar(3, index)).withStyle(impactId.getAbsoluteColor()); - if (hasGlasses) + if (hasGoggles) level.append(capacity + "x ") .append(rpmUnit); else