Items in jars should now preserve all of their original attributes

This commit is contained in:
Sollace 2021-02-14 23:13:12 +02:00
parent b5dc84a139
commit 63f2604e7e
4 changed files with 26 additions and 17 deletions

View file

@ -21,10 +21,11 @@ public interface ChameleonItem {
} }
default ItemStack createAppearanceStack(ItemStack stack, Item appearance) { default ItemStack createAppearanceStack(ItemStack stack, Item appearance) {
ItemStack newAppearance = new ItemStack(appearance, stack.getCount()); ItemStack newAppearance = appearance.getDefaultStack();
if (stack.hasTag()) { if (stack.hasTag()) {
newAppearance.setTag(stack.getTag().copy()); newAppearance.setTag(stack.getTag().copy());
} }
newAppearance.setCount(stack.getCount());
newAppearance.removeSubTag("appearance"); newAppearance.removeSubTag("appearance");
return newAppearance; return newAppearance;
} }
@ -41,7 +42,17 @@ public interface ChameleonItem {
return Items.AIR; return Items.AIR;
} }
default void setAppearance(ItemStack stack, Item appearance) { default ItemStack setAppearance(ItemStack stack, ItemStack appearance) {
stack.getOrCreateTag().putString("appearance", Registry.ITEM.getId(appearance).toString()); ItemStack result = stack.copy();
if (appearance.hasTag()) {
result.setTag(appearance.getTag().copy());
result.removeCustomName();
result.setDamage(stack.getDamage());
result.setCount(stack.getCount());
}
result.getOrCreateTag().putString("appearance", Registry.ITEM.getId(appearance.getItem()).toString());
return result;
} }
} }

View file

@ -16,10 +16,7 @@ public class JarInsertRecipe extends ItemCombinationRecipe {
public final ItemStack craft(CraftingInventory inventory) { public final ItemStack craft(CraftingInventory inventory) {
Pair<ItemStack, ItemStack> pair = runMatch(inventory); Pair<ItemStack, ItemStack> pair = runMatch(inventory);
ItemStack result = new ItemStack(UItems.FILLED_JAR); return UItems.FILLED_JAR.setAppearance(UItems.FILLED_JAR.getDefaultStack(), pair.getRight());
UItems.FILLED_JAR.setAppearance(result, pair.getRight().getItem());
return result;
} }
@Override @Override

View file

@ -15,12 +15,11 @@ import net.minecraft.entity.mob.CreeperEntity;
import net.minecraft.entity.passive.PigEntity; import net.minecraft.entity.passive.PigEntity;
import net.minecraft.entity.passive.VillagerEntity; import net.minecraft.entity.passive.VillagerEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.predicate.entity.EntityPredicates; import net.minecraft.predicate.entity.EntityPredicates;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
@ -101,19 +100,18 @@ public class ZapAppleItem extends AppleItem implements ChameleonItem {
} }
@Override @Override
public String getTranslationKey(ItemStack stack) { public Text getName(ItemStack stack) {
Item appearance = getAppearance(stack); return hasAppearance(stack) ? getAppearanceStack(stack).getName() : super.getName(stack);
return appearance == Items.AIR ? super.getTranslationKey() : appearance.getTranslationKey(stack);
} }
@Override @Override
public Toxicity getToxicity(ItemStack stack) { public Toxicity getToxicity(ItemStack stack) {
return getAppearance(stack) == Items.AIR ? Toxicity.SEVERE : Toxicity.SAFE; return hasAppearance(stack) ? Toxicity.SEVERE : Toxicity.SAFE;
} }
@Override @Override
public Rarity getRarity(ItemStack stack) { public Rarity getRarity(ItemStack stack) {
if (getAppearance(stack) == Items.AIR) { if (hasAppearance(stack)) {
return Rarity.EPIC; return Rarity.EPIC;
} }

View file

@ -2,6 +2,7 @@ package com.minelittlepony.unicopia.item;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf; import net.minecraft.network.PacketByteBuf;
@ -10,6 +11,7 @@ import net.minecraft.recipe.ShapelessRecipe;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper; import net.minecraft.util.JsonHelper;
import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.registry.Registry;
public class ZapAppleRecipe extends ShapelessRecipe { public class ZapAppleRecipe extends ShapelessRecipe {
@ -29,10 +31,11 @@ public class ZapAppleRecipe extends ShapelessRecipe {
throw new JsonParseException("Too many ingredients for shapeless recipe"); throw new JsonParseException("Too many ingredients for shapeless recipe");
} }
ItemStack stack = UItems.ZAP_APPLE.getDefaultStack(); Identifier id = new Identifier(JsonHelper.getString(json, "appearance"));
stack.getOrCreateTag().putString("appearance", JsonHelper.getString(json, "appearance"));
return new ZapAppleRecipe(identifier, group, stack, ingredients); return new ZapAppleRecipe(identifier, group, UItems.ZAP_APPLE.setAppearance(UItems.ZAP_APPLE.getDefaultStack(), Registry.ITEM.getOrEmpty(id).orElseThrow(() -> {
return new JsonSyntaxException("Unknown item '" + id + "'");
}).getDefaultStack()), ingredients);
} }
@Override @Override