Skip to content

Commit

Permalink
Cleanup ResourceLocation extensions
Browse files Browse the repository at this point in the history
Ensure every extension has a tryParse method to shadow the one from resourcel location. Delete other methods that are replicated by parser
  • Loading branch information
KnightMiner committed Mar 18, 2024
1 parent a70f7c6 commit 99eaa10
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static ToolModel deserialize(JsonObject json, JsonDeserializationContext
// modifiers first
List<ModifierId> firstModifiers = Collections.emptyList();
if (json.has("first_modifiers")) {
firstModifiers = JsonHelper.parseList(json, "first_modifiers", ModifierId::convertFromJson);
firstModifiers = JsonHelper.parseList(json, "first_modifiers", ModifierId.PARSER::convertFromJson);
}
return new ToolModel(parts, isLarge, offset, smallModifierRoots, largeModifierRoots, firstModifiers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public ModifierNBT getModifiers(IToolContext tool) {
public static final IGenericLoader<HasModifierPredicate> LOADER = new IGenericLoader<>() {
@Override
public HasModifierPredicate deserialize(JsonObject json) {
ModifierId modifier = ModifierId.getFromJson(json, "modifier");
ModifierId modifier = ModifierId.PARSER.getFromJson(json, "modifier");
IntRange level = DEFAULT_RANGE;
if (json.has("level")) {
level = MAX_RANGE.getAndDeserialize(json, "level");
Expand All @@ -91,15 +91,15 @@ public void serialize(HasModifierPredicate object, JsonObject json) {

@Override
public HasModifierPredicate fromNetwork(FriendlyByteBuf buffer) {
ModifierId modifier = ModifierId.fromNetwork(buffer);
ModifierId modifier = ModifierId.PARSER.fromNetwork(buffer);
IntRange level = IntRange.fromNetwork(buffer);
ModifierCheck check = buffer.readEnum(ModifierCheck.class);
return new HasModifierPredicate(modifier, level, check);
}

@Override
public void toNetwork(HasModifierPredicate object, FriendlyByteBuf buffer) {
object.modifier.toNetwork(buffer);
buffer.writeResourceLocation(object.modifier);
object.level.toNetwork(buffer);
buffer.writeEnum(object.check);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package slimeknights.tconstruct.library.materials.definition;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import slimeknights.tconstruct.library.tools.part.IMaterialItem;
Expand Down Expand Up @@ -73,24 +71,4 @@ public boolean matchesVariant(MaterialVariantId other) {
public static MaterialId tryParse(String string) {
return PARSER.tryParse(string);
}

/**
* Gets a resource location from JSON, throwing a nice exception if invalid
* @param json JSON object
* @param key Key to fetch
* @return Resource location parsed
*/
public static MaterialId fromJson(JsonObject json, String key) {
return PARSER.getFromJson(json, key);
}

/**
* Gets a resource location from JSON, throwing a nice exception if invalid
* @param json JSON object
* @param key Key to fetch
* @return Resource location parsed
*/
public static MaterialId convertJson(JsonElement json, String key) {
return PARSER.convertFromJson(json, key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import net.minecraft.resources.ResourceLocation;
import slimeknights.tconstruct.library.utils.IdParser;

import javax.annotation.Nullable;

/**
* This is just a copy of ResourceLocation for type safety.
*/
Expand All @@ -20,4 +22,14 @@ public MaterialStatsId(String namespaceIn, String pathIn) {
public MaterialStatsId(ResourceLocation resourceLocation) {
super(resourceLocation.getNamespace(), resourceLocation.getPath());
}

/**
* Creates a new material stat ID from the given string
* @param string String
* @return Material ID, or null if invalid
*/
@Nullable
public static MaterialStatsId tryParse(String string) {
return PARSER.tryParse(string);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public int compareTo(ModifierEntry other) {
* @return Parsed JSON
*/
public static ModifierEntry fromJson(JsonObject json) {
return new ModifierEntry(ModifierId.getFromJson(json, "name"), GsonHelper.getAsInt(json, "level", 1));
return new ModifierEntry(ModifierId.PARSER.getFromJson(json, "name"), GsonHelper.getAsInt(json, "level", 1));
}

/**
Expand Down Expand Up @@ -137,15 +137,15 @@ public JsonObject toJson() {
* @return Read entry
*/
public static ModifierEntry read(FriendlyByteBuf buffer) {
return new ModifierEntry(ModifierId.fromNetwork(buffer), buffer.readVarInt());
return new ModifierEntry(ModifierId.PARSER.fromNetwork(buffer), buffer.readVarInt());
}

/**
* Writes this modifier entry to the packet buffer
* @param buffer Buffer instance
*/
public void write(FriendlyByteBuf buffer) {
getId().toNetwork(buffer);
buffer.writeResourceLocation(getId());
buffer.writeVarInt(level);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package slimeknights.tconstruct.library.modifiers;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import slimeknights.tconstruct.library.utils.IdParser;

Expand Down Expand Up @@ -35,34 +32,4 @@ public ModifierId(ResourceLocation resourceLocation) {
public static ModifierId tryParse(String string) {
return PARSER.tryParse(string);
}

/**
* Gets a modifier ID from JSON, throwing a nice exception if invalid
* @param json JSON object
* @param key Key to fetch
* @return Resource location parsed
*/
public static ModifierId getFromJson(JsonObject json, String key) {
return PARSER.getFromJson(json, key);
}

/**
* Gets a modifier ID from JSON, throwing a nice exception if invalid
* @param json JSON object
* @param key Key to fetch
* @return Resource location parsed
*/
public static ModifierId convertFromJson(JsonElement json, String key) {
return PARSER.convertFromJson(json, key);
}

/** Writes an ID to the packet buffer */
public void toNetwork(FriendlyByteBuf buf) {
buf.writeUtf(toString());
}

/** Reads an ID from the packet buffer */
public static ModifierId fromNetwork(FriendlyByteBuf buf) {
return new ModifierId(buf.readUtf(Short.MAX_VALUE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static class Serializer implements LoggingRecipeSerializer<ModifierSalvag
public ModifierSalvage fromJson(ResourceLocation id, JsonObject json) {
Ingredient toolIngredient = Ingredient.fromJson(JsonHelper.getElement(json, "tools"));
int maxToolSize = GsonHelper.getAsInt(json, "max_tool_size", ITinkerStationRecipe.DEFAULT_TOOL_STACK_SIZE);
ModifierId modifier = ModifierId.getFromJson(json, "modifier");
ModifierId modifier = ModifierId.PARSER.getFromJson(json, "modifier");
int minLevel = JsonUtils.getIntMin(json, "min_level", 1);
int maxLevel = GsonHelper.getAsInt(json, "max_level", Integer.MAX_VALUE);
if (maxLevel < minLevel) {
Expand All @@ -123,7 +123,7 @@ public ModifierSalvage fromJson(ResourceLocation id, JsonObject json) {
public ModifierSalvage fromNetworkSafe(ResourceLocation id, FriendlyByteBuf buffer) {
Ingredient toolIngredient = Ingredient.fromNetwork(buffer);
int maxToolSize = buffer.readVarInt();
ModifierId modifier = ModifierId.fromNetwork(buffer);
ModifierId modifier = ModifierId.PARSER.fromNetwork(buffer);
int minLevel = buffer.readVarInt();
int maxLevel = buffer.readVarInt();
SlotCount slots = SlotCount.read(buffer);
Expand All @@ -134,7 +134,7 @@ public ModifierSalvage fromNetworkSafe(ResourceLocation id, FriendlyByteBuf buff
public void toNetworkSafe(FriendlyByteBuf buffer, ModifierSalvage recipe) {
recipe.toolIngredient.toNetwork(buffer);
buffer.writeVarInt(recipe.getMaxToolSize());
recipe.modifier.toNetwork(buffer);
buffer.writeResourceLocation(recipe.modifier);
buffer.writeVarInt(recipe.minLevel);
buffer.writeVarInt(recipe.maxLevel);
SlotCount.write(recipe.slots, buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static class Serializer extends AbstractModifierRecipe.Serializer<Swappab
@Override
protected ModifierEntry readResult(JsonObject json) {
JsonObject result = GsonHelper.getAsJsonObject(json, "result");
return new ModifierEntry(ModifierId.getFromJson(result, "name"), 1);
return new ModifierEntry(ModifierId.PARSER.getFromJson(result, "name"), 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import slimeknights.tconstruct.library.utils.IdParser;
import slimeknights.tconstruct.library.utils.Util;

import javax.annotation.Nullable;

/**
* This is a copy of resource location with a couple extra helpers
*/
Expand Down Expand Up @@ -40,4 +42,14 @@ public Component getDisplayName() {
public ResourceLocation getTexture() {
return new ResourceLocation(getNamespace(), "gui/tinker_pattern/" + getPath());
}

/**
* Tries to create a pattern from the given string, for NBT parsing
* @param string String
* @return Tool stat ID, or null of invalid
*/
@Nullable
public static Pattern tryParse(String string) {
return PARSER.tryParse(string);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ModifierMaterialRepairSerializer<T extends Recipe<?> & IModifierMat
@Override
public T fromJson(ResourceLocation id, JsonObject json) {
ModifierId modifier = new ModifierId(JsonHelper.getResourceLocation(json, "modifier"));
MaterialId repairMaterial = MaterialId.fromJson(json, "repair_material");
MaterialId repairMaterial = MaterialId.PARSER.getFromJson(json, "repair_material");
return factory.create(id, modifier, repairMaterial);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ModifierRepairRecipeSerializer<T extends Recipe<?> & IModifierRepai

@Override
public T fromJson(ResourceLocation id, JsonObject json) {
ModifierId modifier = ModifierId.getFromJson(json, "modifier");
ModifierId modifier = ModifierId.PARSER.getFromJson(json, "modifier");
Ingredient ingredient = Ingredient.fromJson(JsonHelper.getElement(json, "ingredient"));
int repairAmount = GsonHelper.getAsInt(json, "repair_amount");
return factory.create(id, modifier, ingredient, repairAmount);
Expand All @@ -32,15 +32,15 @@ public T fromJson(ResourceLocation id, JsonObject json) {
@Nullable
@Override
public T fromNetworkSafe(ResourceLocation id, FriendlyByteBuf buffer) {
ModifierId modifier = ModifierId.fromNetwork(buffer);
ModifierId modifier = ModifierId.PARSER.fromNetwork(buffer);
Ingredient ingredient = Ingredient.fromNetwork(buffer);
int repairAmount = buffer.readVarInt();
return factory.create(id, modifier, ingredient, repairAmount);
}

@Override
public void toNetworkSafe(FriendlyByteBuf buffer, T recipe) {
recipe.getModifier().toNetwork(buffer);
buffer.writeResourceLocation(recipe.getModifier());
recipe.getIngredient().toNetwork(buffer);
buffer.writeVarInt(recipe.getRepairAmount());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class SpecializedRepairRecipeSerializer<T extends Recipe<?> & ISpecialize
@Override
public T fromJson(ResourceLocation id, JsonObject json) {
Ingredient tool = Ingredient.fromJson(JsonHelper.getElement(json, "tool"));
MaterialId repairMaterial = MaterialId.fromJson(json, "repair_material");
MaterialId repairMaterial = MaterialId.PARSER.getFromJson(json, "repair_material");
return factory.create(id, tool, repairMaterial);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public int getInt(IToolStat<? extends Number> stat) {
/** Reads a tool stat ID from a NBT string */
@Nullable
static IToolStat<?> readStatIdFromNBT(String name) {
ToolStatId statName = ToolStatId.tryCreate(name);
ToolStatId statName = ToolStatId.tryParse(name);
if (statName != null) {
IToolStat<?> stat = ToolStats.getToolStat(statName);
if (stat != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ToolStatId(String value) {
* @return Tool stat ID, or null of invalid
*/
@Nullable
public static ToolStatId tryCreate(String string) {
public static ToolStatId tryParse(String string) {
return PARSER.tryParse(string);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static void processStats(IModDataView persistentData, ResourceLocation k
if (persistentData.contains(key, Tag.TAG_COMPOUND)) {
CompoundTag nbt = persistentData.getCompound(key);
for (String name : nbt.getAllKeys()) {
ToolStatId id = ToolStatId.tryCreate(name);
ToolStatId id = ToolStatId.tryParse(name);
if (id != null) {
IToolStat<?> stat = ToolStats.getToolStat(id);
if (stat != null) {
Expand Down Expand Up @@ -114,7 +114,7 @@ private static void addToTooltip(IModDataView persistentData, ResourceLocation g
boolean first = true;
for (String key : stats.getAllKeys()) {
// ignore invalid stat names
ToolStatId id = ToolStatId.tryCreate(key);
ToolStatId id = ToolStatId.tryParse(key);
if (id != null) {
IToolStat<?> stat = ToolStats.getToolStat(id);
if (stat != null) {
Expand Down

0 comments on commit 99eaa10

Please sign in to comment.