Skip to content

Commit

Permalink
Migrate to Mantle versions of some utilities and update Mantle
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed Feb 13, 2024
1 parent 91b8f1c commit 3e04b5d
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 166 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ forge_range=[40.1.88,)
parchment_version=2022.03.13

# Build Dependencies
mantle_version=1.9.47
mantle_range=[1.9.47,)
mantle_version=1.9.49
mantle_range=[1.9.49,)

# Optional compat
jei_version=9.7.1.255
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import net.minecraft.util.GsonHelper;
import slimeknights.mantle.data.GenericLoaderRegistry;
import slimeknights.mantle.data.GenericLoaderRegistry.IHaveLoader;
import slimeknights.mantle.util.LogicHelper;
import slimeknights.tconstruct.library.json.math.ModifierFormula;
import slimeknights.tconstruct.library.json.math.ModifierFormula.FallbackFormula;
import slimeknights.tconstruct.library.json.math.PostFixFormula;
import slimeknights.tconstruct.library.json.math.StackOperation;
import slimeknights.tconstruct.library.utils.Util;

import java.util.Arrays;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -86,7 +86,7 @@ static <C extends VariableFormula<T>, T extends IHaveLoader<T>> C deserialize(Ge
String[] newNames = Arrays.copyOf(defaultNames, index + variableObj.size());
for (Entry<String,JsonElement> entry : variableObj.entrySet()) {
String key = entry.getKey();
if (Util.isInList(defaultNames, key)) {
if (LogicHelper.isInList(defaultNames, key)) {
throw new JsonSyntaxException("Variable " + key + " is already defined for this module");
}
newNames[index] = key;
Expand Down Expand Up @@ -143,7 +143,7 @@ public Builder(String[] variables) {
@SuppressWarnings("unchecked")
public T customVariable(String key, V variable) {
// disallow a variable that is already there
if (Util.isInList(variableNames, key)) {
if (LogicHelper.isInList(variableNames, key)) {
throw new IllegalArgumentException("Variable " + key + " already exists in the module's variables");
}
// disallow adding the same name multiple times
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import slimeknights.mantle.data.GenericLoaderRegistry.IGenericLoader;
import slimeknights.mantle.util.JsonHelper;
import slimeknights.tconstruct.library.json.LevelingValue;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.modules.ModifierModule;
import slimeknights.tconstruct.library.modifiers.modules.ModifierModuleCondition;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.utils.JsonUtils;
import slimeknights.tconstruct.tools.TinkerModifiers;

/**
Expand Down Expand Up @@ -55,7 +55,7 @@ public IGenericLoader<? extends ModifierModule> getLoader() {
@Override
public CoverGroundWalkerModule deserialize(JsonObject json) {
return new CoverGroundWalkerModule(
JsonUtils.convertToBlockState(json),
JsonHelper.convertToBlockState(json),
LevelingValue.deserialize(GsonHelper.getAsJsonObject(json, "radius")),
ModifierModuleCondition.deserializeFrom(json)
);
Expand All @@ -64,7 +64,7 @@ public CoverGroundWalkerModule deserialize(JsonObject json) {
@Override
public void serialize(CoverGroundWalkerModule object, JsonObject json) {
object.condition.serializeInto(json);
JsonUtils.serializeBlockState(object.state, json);
JsonHelper.serializeBlockState(object.state, json);
json.add("radius", object.radius.serialize(new JsonObject()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import slimeknights.mantle.data.predicate.damage.DamageSourcePredicate;
import slimeknights.mantle.data.predicate.entity.LivingEntityPredicate;
import slimeknights.mantle.util.JsonHelper;
import slimeknights.mantle.util.LogicHelper;
import slimeknights.tconstruct.library.json.LevelingValue;
import slimeknights.tconstruct.library.modifiers.Modifier;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
Expand Down Expand Up @@ -63,7 +64,7 @@ public float getProtectionModifier(IToolStackView tool, ModifierEntry modifier,
if (condition.matches(tool, modifier) && this.source.matches(source) && this.entity.matches(context.getEntity())) {
// if this modifier also has an enchantment, subtract out that enchantment value
// used for fire protection to subtract out the 2 protection from vanilla
if (subtract != null && Util.isInList(subtract.slots, slotType)) {
if (subtract != null && LogicHelper.isInList(subtract.slots, slotType)) {
float scaledLevel = modifier.getEffectiveLevel(tool);
modifierValue += amount.compute(scaledLevel) - subtract.getDamageProtection(Mth.floor(scaledLevel), source);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import slimeknights.tconstruct.library.modifiers.modules.ModifierModule;
import slimeknights.tconstruct.library.tools.nbt.IToolContext;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.utils.JsonUtils;
import slimeknights.tconstruct.tools.TinkerModifiers;

import java.util.List;
Expand Down Expand Up @@ -85,7 +84,7 @@ private record BlockReplacement(IJsonPredicate<BlockState> target, BlockState st
public static BlockReplacement deserialize(JsonObject json) {
return new BlockReplacement(
BlockPredicate.LOADER.getAndDeserialize(json, "target"),
JsonUtils.convertToBlockState(json), // pulling from this object directly means the keys used are block and properties
JsonHelper.convertToBlockState(json), // pulling from this object directly means the keys used are block and properties
MODIFIER_LEVEL.getAndDeserialize(json, "modifier_level")
);
}
Expand All @@ -94,7 +93,7 @@ public static BlockReplacement deserialize(JsonObject json) {
public JsonObject serialize() {
JsonObject json = new JsonObject();
json.add("target", BlockPredicate.LOADER.serialize(target));
JsonUtils.serializeBlockState(state, json);
JsonHelper.serializeBlockState(state, json);
MODIFIER_LEVEL.serializeInto(json, "modifier_level", level);
return json;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import slimeknights.mantle.data.GenericLoaderRegistry.IGenericLoader;
import slimeknights.mantle.util.JsonHelper;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.ModifierHook;
Expand All @@ -16,7 +17,6 @@
import slimeknights.tconstruct.library.tools.context.ToolRebuildContext;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.nbt.ModDataNBT;
import slimeknights.tconstruct.library.utils.JsonUtils;

import java.util.List;

Expand Down Expand Up @@ -65,7 +65,7 @@ public IGenericLoader<? extends ModifierModule> getLoader() {
public TankCapacityModule deserialize(JsonObject json) {
int capacity = GsonHelper.getAsInt(json, "capacity");
boolean scaleCapacity = GsonHelper.getAsBoolean(json, "scale_capacity");
ResourceLocation capacityKey = JsonUtils.getResourceLocation(json, "capacity_key", DEFAULT_CAPACITY_KEY);
ResourceLocation capacityKey = JsonHelper.getResourceLocation(json, "capacity_key", DEFAULT_CAPACITY_KEY);
return new TankCapacityModule(capacityKey, capacity, scaleCapacity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.minecraftforge.fluids.capability.IFluidHandler.FluidAction;
import slimeknights.mantle.client.TooltipKey;
import slimeknights.mantle.data.GenericLoaderRegistry.IGenericLoader;
import slimeknights.mantle.util.JsonHelper;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.modifiers.Modifier;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
Expand All @@ -29,7 +30,6 @@
import slimeknights.tconstruct.library.tools.nbt.IToolContext;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.nbt.ModDataNBT;
import slimeknights.tconstruct.library.utils.JsonUtils;

import javax.annotation.Nullable;
import java.util.List;
Expand Down Expand Up @@ -230,9 +230,9 @@ public IGenericLoader<? extends ModifierModule> getLoader() {
public TankModule deserialize(JsonObject json) {
int capacity = GsonHelper.getAsInt(json, "capacity");
boolean scaleCapacity = GsonHelper.getAsBoolean(json, "scale_capacity");
ResourceLocation capacityKey = JsonUtils.getResourceLocation(json, "capacity_key", DEFAULT_CAPACITY_KEY);
ResourceLocation fluidKey = JsonUtils.getResourceLocation(json, "fluid_key", DEFAULT_CAPACITY_KEY);
ResourceLocation ownerKey = JsonUtils.getResourceLocation(json, "owner_key", DEFAULT_CAPACITY_KEY);
ResourceLocation capacityKey = JsonHelper.getResourceLocation(json, "capacity_key", DEFAULT_CAPACITY_KEY);
ResourceLocation fluidKey = JsonHelper.getResourceLocation(json, "fluid_key", DEFAULT_CAPACITY_KEY);
ResourceLocation ownerKey = JsonHelper.getResourceLocation(json, "owner_key", DEFAULT_CAPACITY_KEY);
return new TankModule(capacityKey, capacity, scaleCapacity, fluidKey, ownerKey);
}

Expand Down
136 changes: 0 additions & 136 deletions src/main/java/slimeknights/tconstruct/library/utils/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.event.OnDatapackSyncEvent;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.IForgeRegistry;
import net.minecraftforge.registries.IForgeRegistryEntry;
import slimeknights.mantle.network.packet.ISimplePacket;
Expand All @@ -24,9 +19,7 @@

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;

/** Helpers for a few JSON related tasks */
public class JsonUtils {
Expand Down Expand Up @@ -167,20 +160,6 @@ public static JsonElement serializeItemStack(ItemStack result) {
}
}

/**
* Gets a resource location from JSON, throwing a nice exception if invalid
* @param json JSON object
* @param key Key to fetch
* @param fallback Fallback if key is not present
* @return Resource location parsed
*/
public static ResourceLocation getResourceLocation(JsonObject json, String key, ResourceLocation fallback) {
if (json.has(key)) {
return JsonHelper.getResourceLocation(json, key);
}
return fallback;
}

/**
* Parses a color as a string
* @param color Color to parse
Expand All @@ -206,119 +185,4 @@ public static int parseColor(@Nullable String color) {
public static String colorToString(int color) {
return String.format("%06X", color);
}


/* Block States */

/**
* Converts the given JSON element into a block state
* @param element Element to convert
* @param key Element key
* @return Block state
* @throws JsonSyntaxException if a property does not parse or the element is the wrong type
*/
public static BlockState convertToBlockState(JsonElement element, String key) {
// primitive means its a block directly
if (element.isJsonPrimitive()) {
return JsonHelper.convertToEntry(ForgeRegistries.BLOCKS, element, key).defaultBlockState();
}
if (element.isJsonObject()) {
return convertToBlockState(element.getAsJsonObject());
}
throw new JsonSyntaxException("Expected " + key + " to be a string or an object, was " + GsonHelper.getType(element));
}

/**
* Converts the given JSON element into a block state
* @param parent Parent containing the block state
* @param key Element key
* @return Block state
* @throws JsonSyntaxException if a property does not parse or the element is missing or the wrong type
*/
public static BlockState getAsBlockState(JsonObject parent, String key) {
if (parent.has(key)) {
return convertToBlockState(parent.get(key), key);
}
throw new JsonSyntaxException("Missing " + key + ", expected to find a string or an object");
}

/**
* Sets the property
* @param state State before changes
* @param property Property to set
* @param name Value name
* @param <T> Type of property
* @return State with the property
* @throws JsonSyntaxException if the property has no element with the given name
*/
private static <T extends Comparable<T>> BlockState setValue(BlockState state, Property<T> property, String name) {
Optional<T> value = property.getValue(name);
if (value.isPresent()) {
return state.setValue(property, value.get());
}
throw new JsonSyntaxException("Property " + property + " does not contain value " + name);
}

/**
* Converts the given JSON object into a block state
* @param json Json object containing "block" and "properties"
* @return Block state
* @throws JsonSyntaxException if any property name or property value is invalid
*/
public static BlockState convertToBlockState(JsonObject json) {
Block block = JsonHelper.getAsEntry(ForgeRegistries.BLOCKS, json, "block");
BlockState state = block.defaultBlockState();
if (json.has("properties")) {
StateDefinition<Block,BlockState> definition = block.getStateDefinition();
for (Entry<String,JsonElement> entry : GsonHelper.getAsJsonObject(json, "properties").entrySet()) {
String key = entry.getKey();
Property<?> property = definition.getProperty(key);
if (property == null) {
throw new JsonSyntaxException("Property " + key + " does not exist in block " + block);
}
state = setValue(state, property, GsonHelper.convertToString(entry.getValue(), key));
}
}
return state;
}

/**
* Serializes the given block state to JSON, essentially writes all values that differ from the state.
* @param state State
* @return JsonPrimitive of the block name if it matches the default state, JsonObject otherwise
*/
public static JsonElement serializeBlockState(BlockState state) {
Block block = state.getBlock();
if (state == block.defaultBlockState()) {
return new JsonPrimitive(Objects.requireNonNull(block.getRegistryName()).toString());
}
return serializeBlockState(state, new JsonObject());
}

/** Serializes the property if it differs in the default state */
private static <T extends Comparable<T>> void serializeProperty(BlockState serialize, Property<T> property, BlockState defaultState, JsonObject json) {
T value = serialize.getValue(property);
if (!value.equals(defaultState.getValue(property))) {
json.addProperty(property.getName(), property.getName(value));
}
}

/**
* Serializes the given block state to JSON, essentially writes all values that differ from the state
* @param state State
* @return JsonObject containing properties that differ from the default state
*/
public static JsonObject serializeBlockState(BlockState state, JsonObject json) {
Block block = state.getBlock();
json.addProperty("block", Objects.requireNonNull(block.getRegistryName()).toString());
BlockState defaultState = block.defaultBlockState();
JsonObject properties = new JsonObject();
for (Property<?> property : block.getStateDefinition().getProperties()) {
serializeProperty(state, property, defaultState, properties);
}
if (properties.size() > 0) {
json.add("properties", properties);
}
return json;
}
}
10 changes: 0 additions & 10 deletions src/main/java/slimeknights/tconstruct/library/utils/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,4 @@ public static BlockHitResult createTraceResult(BlockPos pos, Direction sideHit,
public static <B extends BlockEntity> ClientboundBlockEntityDataPacket createBEPacket(B be, Function<? super B,CompoundTag> tagFunction) {
return new ClientboundBlockEntityDataPacket(be.getBlockPos(), be.getType(), tagFunction.apply(be));
}

/** Quick helper to search an array for a given value by reference equality, designed mainly for enums. */
public static <T> boolean isInList(T[] slots, T predicate) {
for (T slot : slots) {
if (predicate.equals(slot)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.enchantment.Enchantments;
import slimeknights.mantle.util.LogicHelper;
import slimeknights.tconstruct.library.modifiers.dynamic.EnchantmentModifier;
import slimeknights.tconstruct.library.modifiers.impl.IncrementalModifier;
import slimeknights.tconstruct.library.modifiers.modules.armor.ProtectionModule;
import slimeknights.tconstruct.library.tools.context.EquipmentContext;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.utils.RestrictedCompoundTag;
import slimeknights.tconstruct.library.utils.TooltipKey;
import slimeknights.tconstruct.library.utils.Util;

import javax.annotation.Nullable;
import java.util.List;
Expand All @@ -37,7 +37,7 @@ public float getProtectionModifier(IToolStackView tool, int level, EquipmentCont
if (!source.isBypassMagic() && !source.isBypassInvul() && source.isFire()) {
// we already got floored level * 2 boost from the vanilla enchantment on armor, so cancel that out
float scaledLevel = getEffectiveLevel(tool, level);
if (Util.isInList(Enchantments.FIRE_PROTECTION.slots, slotType)) {
if (LogicHelper.isInList(Enchantments.FIRE_PROTECTION.slots, slotType)) {
modifierValue += scaledLevel * 2.5f - Math.floor(scaledLevel) * 2f;
} else {
modifierValue += scaledLevel * 2.5f;
Expand Down

0 comments on commit 3e04b5d

Please sign in to comment.