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
This commit is contained in:
PepperBell 2021-07-27 13:25:16 -07:00
parent 85be4be395
commit 0dea880665
9 changed files with 197 additions and 63 deletions

View file

@ -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

View file

@ -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) {

View file

@ -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());

View file

@ -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;

View file

@ -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<ConfigBase, Type> 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) {

View file

@ -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<ResourceLocation, ConfigValue<Double>> capacities = new HashMap<>();
private Map<ResourceLocation, ConfigValue<Double>> 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<Double> 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<Double> 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<ResourceLocation, ConfigValue<Double>> getImpacts() {

View file

@ -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<ResourceLocation, Double> registeredDefaultImpacts = new HashMap<>();
static Map<ResourceLocation, Double> registeredDefaultCapacities = new HashMap<>();
public static final Map<ResourceLocation, Double> DEFAULT_IMPACTS = new HashMap<>();
public static final Map<ResourceLocation, Double> 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 <B extends Block, P> NonNullUnaryOperator<BlockBuilder<B, P>> setNoImpact() {
return setImpact(0);
@ -28,14 +35,14 @@ public class StressConfigDefaults {
public static <B extends Block, P> NonNullUnaryOperator<BlockBuilder<B, P>> 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 <B extends Block, P> NonNullUnaryOperator<BlockBuilder<B, P>> setCapacity(double capacity) {
return b -> {
registeredDefaultCapacities.put(Create.asResource(b.getName()), capacity);
setDefaultCapacity(new ResourceLocation(b.getOwner().getModid(), b.getName()), capacity);
return b;
};
}

View file

@ -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<String, IStressValueProvider> 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);
}
}

View file

@ -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<ITextComponent> getKineticStats(Block block) {
List<ITextComponent> 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<ResourceLocation, ConfigValue<Double>> impacts = config.stressValues.getImpacts();
Map<ResourceLocation, ConfigValue<Double>> 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<ITextComponent> 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<ITextComponent> 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<ITextComponent> 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