Skip to content

Commit

Permalink
Fix a crash upon deserialization of equipment types in recipes (#10221)
Browse files Browse the repository at this point in the history
Because the new tool type is called "none" instead of an empty string it becomes unable to be loaded, added fallback code so this can safely load again.
Updates a bunch of jdoc code to remove capital casing.
  • Loading branch information
Thodor12 authored Sep 15, 2024
1 parent bf1dc33 commit c30a69b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.minecolonies.api.equipment.registry;

import com.minecolonies.api.equipment.ModEquipmentTypes;
import com.minecolonies.api.util.constant.Constants;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -20,7 +21,7 @@ public final class EquipmentTypeEntry
private final ResourceLocation registryName;

/**
* The component for the human readable name.
* The component for the human-readable name.
*/
private final Component displayName;

Expand All @@ -39,10 +40,10 @@ public final class EquipmentTypeEntry
/**
* Constructor.
*
* @param displayName The human readable name of the equipment type
* @param isEquipment A predicate for determining if an itemstack is the equipment type
* @param itemLevel A function to return the item level of an item stack
* @param registryName The forge registry location of the equipment type
* @param displayName the human-readable name of the equipment type
* @param isEquipment a predicate for determining if an itemstack is the equipment type
* @param itemLevel a function to return the item level of an item stack
* @param registryName the forge registry location of the equipment type
*/
private EquipmentTypeEntry(
final Component displayName,
Expand All @@ -57,12 +58,12 @@ private EquipmentTypeEntry(
}

/**
* Parse a resource location from a serialized version for equipmenttypes.
* This is to help migrate to the new equipmenttype serialization which originally
* Parse a resource location from a serialized version for EquipmentTypes.
* This is to help migrate to the new EquipmentType serialization which originally
* used names and now uses resource locations.
*
* @param serialized The string representation of the equipment type
* @return The correct resource location
* @param serialized the string representation of the equipment type
* @return the correct resource location
*/
public static ResourceLocation parseResourceLocation(final String serialized)
{
Expand All @@ -71,28 +72,24 @@ public static ResourceLocation parseResourceLocation(final String serialized)
}

/**
* Parse a resource location from a serialized version for equipmenttypes.
* This is to help migrate to the new equipmenttype serialization which originally
* Parse a resource location from a serialized version for EquipmentTypes.
* This is to help migrate to the new EquipmentType serialization which originally
* used names and now uses resource locations.
*
* @param serialized A resource location read from a buffer
* @return The correct resource location
* @param serialized A resource location read from nbt
* @return the correct resource location
*/
public static ResourceLocation parseResourceLocation(final ResourceLocation serialized)
{
// Minecraft will never register an equipment type with us so these are
// the old non-resource-location serialized equipment types.
if (serialized.getNamespace().equals("minecraft")) {
return new ResourceLocation(Constants.MOD_ID, serialized.getPath());
}

return serialized;
final String namespace = serialized.getNamespace().equals("minecraft") ? Constants.MOD_ID : serialized.getNamespace();
final String path = serialized.getPath().isEmpty() ? ModEquipmentTypes.none.get().registryName.getPath() : serialized.getPath();
return new ResourceLocation(namespace, path);
}

/**
* Get the name of the forge registry location for the equipment type
*
* @return The resource location
* @return the resource location
*/
public ResourceLocation getRegistryName()
{
Expand All @@ -102,7 +99,7 @@ public ResourceLocation getRegistryName()
/**
* Get the display name of the equipment type
*
* @return the component for the human readable name.
* @return the component for the human-readable name.
*/
public Component getDisplayName()
{
Expand All @@ -113,7 +110,7 @@ public Component getDisplayName()
* Determine whether an item stack works as this equipment.
*
* @param itemStack to test
* @return Whether the item stack can act as the equipment.
* @return whether the item stack can act as the equipment.
*/
public boolean checkIsEquipment(ItemStack itemStack)
{
Expand All @@ -124,7 +121,7 @@ public boolean checkIsEquipment(ItemStack itemStack)
* Get the item level for this equipment type for a given item stack
*
* @param itemStack to test
* @return The item level
* @return the item level
*/
public int getMiningLevel(ItemStack itemStack)
{
Expand All @@ -142,7 +139,7 @@ public static class Builder
private ResourceLocation registryName;

/**
* The component for the human readable name.
* The component for the human-readable name.
*/
private Component displayName;

Expand Down Expand Up @@ -173,7 +170,7 @@ public Builder setRegistryName(final ResourceLocation registryName)
/**
* Set the display name for the new EquipmentTypeEntry
*
* @param displayName the new human readable name
* @param displayName the new human-readable name
* @return this
*/
public Builder setDisplayName(final Component displayName)
Expand All @@ -185,7 +182,7 @@ public Builder setDisplayName(final Component displayName)
/**
* Set the predicate for determining whether an item stack is the equipment type
*
* @param isEquipment The predicate
* @param isEquipment the predicate
* @return this
*/
public Builder setIsEquipment(final BiPredicate<ItemStack, EquipmentTypeEntry> isEquipment)
Expand All @@ -197,7 +194,7 @@ public Builder setIsEquipment(final BiPredicate<ItemStack, EquipmentTypeEntry> i
/**
* Set the function for getting the item level of an item stack for this tool type
*
* @param itemLevel The function
* @param itemLevel the function
* @return this
*/
public Builder setEquipmentLevel(final BiFunction<ItemStack, EquipmentTypeEntry, Integer> itemLevel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,9 @@ public RecipeStorage deserialize(@NotNull final IFactoryController controller, @
secOutputs.add(ItemStack.of(secOutputTag));
}

final ResourceLocation lootTable = nbt.contains(LOOT_TAG) ? new ResourceLocation(nbt.getString(LOOT_TAG)) : null;
final ResourceLocation lootTable = nbt.contains(LOOT_TAG) ? new ResourceLocation(nbt.getString(LOOT_TAG)) : null;
final EquipmentTypeEntry requiredTool = ModEquipmentTypes.getRegistry().getValue(EquipmentTypeEntry.parseResourceLocation(nbt.getString(TOOL_TAG)));

final String resLoc = nbt.getString(TOOL_TAG);
final EquipmentTypeEntry requiredTool = nbt.contains(TOOL_TAG) ? ModEquipmentTypes.getRegistry().getValue(EquipmentTypeEntry.parseResourceLocation(resLoc)) : ModEquipmentTypes.none.get();
return this.getNewInstance(token, input, gridSize, primaryOutput, intermediate, source, type, altOutputs.isEmpty() ? null : altOutputs, secOutputs.isEmpty() ? null : secOutputs, lootTable, requiredTool);
}

Expand Down

0 comments on commit c30a69b

Please sign in to comment.