remove arbitary item data

This commit is contained in:
Sen 2025-07-07 00:38:38 +02:00
parent 97b708bb23
commit 4e94a660ff
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
18 changed files with 210 additions and 392 deletions

View file

@ -36,7 +36,7 @@ public class GuiRename extends Gui implements FieldCallback, ButtonCallback {
} }
public GuiRename(int slot, ItemStack stack, Gui prev) { public GuiRename(int slot, ItemStack stack, Gui prev) {
this.itemName = stack.getItem().getDisplay(stack); this.itemName = stack.getItem().getDisplay();
this.origName = stack.hasDisplayName() ? TextColor.stripCodes(stack.getDisplayName()) : ""; this.origName = stack.hasDisplayName() ? TextColor.stripCodes(stack.getDisplayName()) : "";
this.slot = slot; this.slot = slot;
this.previous = prev; this.previous = prev;

View file

@ -181,7 +181,7 @@ public class EntityAIControlledByPlayer extends EntityAIBase
if (itemstack.size == 0) if (itemstack.size == 0)
{ {
ItemStack itemstack1 = new ItemStack(Items.fishing_rod); ItemStack itemstack1 = new ItemStack(Items.fishing_rod);
itemstack1.setTag(itemstack.getTag()); itemstack1.copyData(itemstack);
entityplayer.inventory.mainInventory[entityplayer.inventory.currentItem] = itemstack1; entityplayer.inventory.mainInventory[entityplayer.inventory.currentItem] = itemstack1;
} }
} }

View file

@ -3,6 +3,7 @@ package common.enchantment;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.Map.Entry; import java.util.Map.Entry;
import common.collect.Lists; import common.collect.Lists;
@ -16,7 +17,6 @@ import common.item.ItemStack;
import common.item.material.ItemEnchantedBook; import common.item.material.ItemEnchantedBook;
import common.rng.Random; import common.rng.Random;
import common.rng.WeightedList; import common.rng.WeightedList;
import common.tags.TagObject;
public class EnchantmentHelper public class EnchantmentHelper
{ {
@ -34,7 +34,7 @@ public class EnchantmentHelper
} }
else else
{ {
List<TagObject> list = stack.getEnchantmentTagList(); Integer list = stack.getEnchantment(enchID);
if (list == null) if (list == null)
{ {
@ -42,18 +42,7 @@ public class EnchantmentHelper
} }
else else
{ {
for (int i = 0; i < list.size(); ++i) return list.intValue();
{
Enchantment j = Enchantment.getEnchantment(list.get(i).getString("id"));
if (j == enchID)
{
int k = list.get(i).getShort("lvl");
return k;
}
}
return 0;
} }
} }
} }
@ -65,17 +54,13 @@ public class EnchantmentHelper
map.put(book.getEnchantment(), book.getLevel()); map.put(book.getEnchantment(), book.getLevel());
return map; return map;
} }
List<TagObject> list = stack.getEnchantmentTagList(); Set<Entry<Enchantment, Integer>> list = stack.getEnchantments();
if (list != null) if (list != null)
{ {
for (int i = 0; i < list.size(); ++i) for (Entry<Enchantment, Integer> enc : list)
{ {
Enchantment j = Enchantment.getEnchantment(list.get(i).getString("id")); map.put(enc.getKey(), enc.getValue());
if(j != null) {
int k = list.get(i).getShort("lvl");
map.put(j, k);
}
} }
} }
@ -88,34 +73,9 @@ public class EnchantmentHelper
{ {
Entry<Enchantment, Integer> entry = new Random().pick(Lists.newArrayList(enchMap.entrySet())); Entry<Enchantment, Integer> entry = new Random().pick(Lists.newArrayList(enchMap.entrySet()));
stack.setItem(ItemEnchantedBook.getEnchantedBook(entry.getKey(), entry.getValue())); stack.setItem(ItemEnchantedBook.getEnchantedBook(entry.getKey(), entry.getValue()));
if(stack.hasTag())
stack.getTag().remove("ench");
} }
else { else {
List<TagObject> list = Lists.newArrayList(); stack.setEnchantments(enchMap);
Iterator<Enchantment> iterator = enchMap.keySet().iterator();
while (iterator.hasNext())
{
Enchantment enchantment = iterator.next();
TagObject tag = new TagObject();
tag.setString("id", enchantment.getName());
tag.setShort("lvl", (short)enchMap.get(enchantment).intValue());
list.add(tag);
}
if (list.size() > 0)
{
if (!(stack.getItem() instanceof ItemEnchantedBook))
{
stack.setTagInfo("ench", list);
}
}
else if (stack.hasTag())
{
stack.getTag().remove("ench");
}
} }
} }
@ -153,19 +113,13 @@ public class EnchantmentHelper
{ {
if (stack != null) if (stack != null)
{ {
List<TagObject> list = stack.getEnchantmentTagList(); Set<Entry<Enchantment, Integer>> list = stack.getEnchantments();
if (list != null) if (list != null)
{ {
for (int i = 0; i < list.size(); ++i) for (Entry<Enchantment, Integer> enc : list)
{ {
String j = list.get(i).getString("id"); modifier.calculateModifier(enc.getKey(), enc.getValue());
int k = list.get(i).getShort("lvl");
if (Enchantment.getEnchantment(j) != null)
{
modifier.calculateModifier(Enchantment.getEnchantment(j), k);
}
} }
} }
} }

View file

@ -223,11 +223,7 @@ public class EntityItem extends Entity
{ {
return false; return false;
} }
else if (itemstack1.hasTag() ^ itemstack.hasTag()) else if (!itemstack1.dataEquals(itemstack))
{
return false;
}
else if (itemstack1.hasTag() && !itemstack1.getTag().equals(itemstack.getTag()))
{ {
return false; return false;
} }

View file

@ -1368,11 +1368,11 @@ public abstract class EntityNPC extends EntityLiving
flag = itemsword.getDamageVsEntity() > itemsword1.getDamageVsEntity(); flag = itemsword.getDamageVsEntity() > itemsword1.getDamageVsEntity();
} }
else { else {
flag = stack.getItemDamage() > old.getItemDamage() || stack.hasTag() && !old.hasTag(); flag = stack.getItemDamage() > old.getItemDamage() || stack.isItemEnchanted() && !old.isItemEnchanted();
} }
} }
else if(stack.getItem() instanceof ItemBow && old.getItem() instanceof ItemBow) { else if(stack.getItem() instanceof ItemBow && old.getItem() instanceof ItemBow) {
flag = stack.hasTag() && !old.hasTag(); flag = stack.isItemEnchanted() && !old.isItemEnchanted();
} }
else if(stack.getItem() instanceof ItemGunBase && !(old.getItem() instanceof ItemBow)) { else if(stack.getItem() instanceof ItemGunBase && !(old.getItem() instanceof ItemBow)) {
flag = true; flag = true;
@ -1395,7 +1395,7 @@ public abstract class EntityNPC extends EntityLiving
flag = itemarmor.damageReduceAmount > itemarmor1.damageReduceAmount; flag = itemarmor.damageReduceAmount > itemarmor1.damageReduceAmount;
} }
else { else {
flag = stack.getItemDamage() > old.getItemDamage() || stack.hasTag() && !old.hasTag(); flag = stack.getItemDamage() > old.getItemDamage() || stack.isItemEnchanted() && !old.isItemEnchanted();
} }
} }
else { else {

View file

@ -787,7 +787,6 @@ public abstract class CraftingRegistry
/** Is the ItemStack that you get when craft the recipe. */ /** Is the ItemStack that you get when craft the recipe. */
private final ItemStack recipeOutput; private final ItemStack recipeOutput;
private boolean copyIngredientTag;
public ShapedRecipes(int width, int height, ItemStack[] p_i1917_3_, ItemStack output) public ShapedRecipes(int width, int height, ItemStack[] p_i1917_3_, ItemStack output)
{ {
@ -893,22 +892,7 @@ public abstract class CraftingRegistry
*/ */
public ItemStack getCraftingResult(InventoryCrafting inv) public ItemStack getCraftingResult(InventoryCrafting inv)
{ {
ItemStack itemstack = this.getRecipeOutput().copy(); return this.getRecipeOutput().copy();
if (this.copyIngredientTag)
{
for (int i = 0; i < inv.getSizeInventory(); ++i)
{
ItemStack itemstack1 = inv.getStackInSlot(i);
if (itemstack1 != null && itemstack1.hasTag())
{
itemstack.setTag(itemstack1.getTag().copy());
}
}
}
return itemstack;
} }
/** /**

View file

@ -330,14 +330,11 @@ public class ContainerEnchantment extends Container
return null; return null;
} }
if (itemstack1.hasTag() && itemstack1.size == 1) if (itemstack1.size >= 1)
{ {
((Slot)this.inventorySlots.get(0)).putStack(itemstack1.copy()); ItemStack st = itemstack1.copy();
itemstack1.size = 0; st.size = 1;
} ((Slot)this.inventorySlots.get(0)).putStack(st);
else if (itemstack1.size >= 1)
{
((Slot)this.inventorySlots.get(0)).putStack(new ItemStack(itemstack1.getItem()));
--itemstack1.size; --itemstack1.size;
} }
} }

View file

@ -52,10 +52,7 @@ public class InventoryHelper
stack.size -= i; stack.size -= i;
EntityItem entityitem = new EntityItem(worldIn, x + (double)f, y + (double)f1, z + (double)f2, new ItemStack(stack.getItem(), i)); EntityItem entityitem = new EntityItem(worldIn, x + (double)f, y + (double)f1, z + (double)f2, new ItemStack(stack.getItem(), i));
if (stack.hasTag()) entityitem.getEntityItem().copyData(stack);
{
entityitem.getEntityItem().setTag(stack.getTag().copy());
}
float f3 = 0.05F; float f3 = 0.05F;
entityitem.motionX = RANDOM.gaussian() * (double)f3; entityitem.motionX = RANDOM.gaussian() * (double)f3;

View file

@ -201,10 +201,7 @@ public class InventoryPlayer implements IInventory
{ {
this.mainInventory[j] = new ItemStack(item, 0); this.mainInventory[j] = new ItemStack(item, 0);
if (itemStackIn.hasTag()) this.mainInventory[j].copyData(itemStackIn);
{
this.mainInventory[j].setTag(itemStackIn.getTag().copy());
}
} }
int k = i; int k = i;

View file

@ -87,7 +87,7 @@ public class Item {
return this.maxDamage > 0; return this.maxDamage > 0;
} }
public final String getDisplay(ItemStack stack) { public final String getDisplay() {
return this.display; return this.display;
} }

View file

@ -4,6 +4,7 @@ import java.text.DecimalFormat;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
import common.attributes.Attribute; import common.attributes.Attribute;
import common.attributes.UsageSlot; import common.attributes.UsageSlot;
@ -30,7 +31,11 @@ public final class ItemStack
public int size; public int size;
private Item item; private Item item;
private TagObject tag; private String name;
private int color = 0xffffffff;
private int damage;
private int repairCost;
private Map<Enchantment, Integer> enchantments;
public ItemStack(Item item) public ItemStack(Item item)
{ {
@ -62,9 +67,13 @@ public final class ItemStack
public static ItemStack readFromTag(TagObject tag) public static ItemStack readFromTag(TagObject tag)
{ {
ItemStack itemstack = new ItemStack(); Item item = tag.hasString("id") ? ItemRegistry.byName(tag.getString("id")) : null;
itemstack.readTags(tag); int size = tag.hasInt("size") ? tag.getInt("size") : 1;
return itemstack.getItem() != null ? itemstack : null; if(item == null || size < 1)
return null;
ItemStack stack = new ItemStack(item, Math.min(item.getMaxAmount(), size));
stack.readTag(tag);
return stack;
} }
public static ItemStack getStack(String name, ItemStack def) { public static ItemStack getStack(String name, ItemStack def) {
@ -74,8 +83,26 @@ public final class ItemStack
return item == null ? def : new ItemStack(item); return item == null ? def : new ItemStack(item);
} }
private ItemStack() public boolean dataEquals(ItemStack other) {
{ if(this.color != other.color || this.damage != other.damage || this.repairCost != other.repairCost || (this.name != null) != (other.name != null) || (this.name != null && !this.name.equals(other.name)) || (this.enchantments != null) != (other.enchantments != null) || (this.enchantments != null && this.enchantments.size() != other.enchantments.size()))
return false;
if(this.enchantments == null)
return true;
for(Entry<Enchantment, Integer> ench : this.enchantments.entrySet()) {
if(!ench.getValue().equals(other.enchantments.get(ench.getKey())))
return false;
}
return true;
}
public void copyData(ItemStack stack) {
this.color = stack.color;
this.damage = stack.damage;
this.repairCost = stack.repairCost;
this.name = stack.name;
this.enchantments = stack.enchantments == null ? null : Maps.newEnumMap(Enchantment.class);
if(this.enchantments != null)
this.enchantments.putAll(stack.enchantments);
} }
/** /**
@ -85,10 +112,7 @@ public final class ItemStack
{ {
ItemStack itemstack = new ItemStack(this.item, amount); ItemStack itemstack = new ItemStack(this.item, amount);
if (this.tag != null) itemstack.copyData(this);
{
itemstack.tag = this.tag.copy();
}
this.size -= amount; this.size -= amount;
return itemstack; return itemstack;
@ -146,21 +170,54 @@ public final class ItemStack
tag.setString("id", ItemRegistry.getName(this.item)); tag.setString("id", ItemRegistry.getName(this.item));
if(this.size != 1) if(this.size != 1)
tag.setInt("size", this.size); tag.setInt("size", this.size);
if(this.tag != null) if(this.color != 0xffffffff)
tag.setObject("tag", this.tag); tag.setInt("color", this.color);
if(this.damage != 0)
tag.setInt("dmg", this.damage);
if(this.repairCost != 0)
tag.setInt("cost", this.repairCost);
if(this.name != null)
tag.setString("name", this.name);
if(this.enchantments != null) {
List<TagObject> list = Lists.newArrayList();
for(Entry<Enchantment, Integer> ench : this.enchantments.entrySet()) {
TagObject enc = new TagObject();
enc.setString("id", ench.getKey().getName());
if(ench.getValue() != 1)
enc.setShort("lvl", ench.getValue().shortValue());
list.add(enc);
}
tag.setList("ench", list);
}
return tag; return tag;
} }
private void readTags(TagObject tag) public void readTag(TagObject tag)
{ {
this.item = tag.hasString("id") ? ItemRegistry.byName(tag.getString("id")) : null; this.color = tag.hasInt("color") ? tag.getInt("color") : 0xffffffff;
this.size = tag.hasInt("size") ? tag.getInt("size") : 1; this.color = (this.color & 0xff000000) != 0 ? 0xffffffff : this.color;
this.tag = tag.hasObject("tag") ? tag.getObject("tag") : null; this.damage = tag.hasInt("dmg") ? Math.max(0, tag.getInt("dmg")) : 0;
this.repairCost = tag.hasInt("cost") ? Math.max(0, tag.getInt("cost")) : 0;
this.name = tag.hasString("name") ? tag.getString("name") : null;
this.name = this.name != null && this.name.length() > 32 ? this.name.substring(0, 32) : this.name;
this.enchantments = tag.hasList("ench") ? Maps.newEnumMap(Enchantment.class) : null;
if(this.enchantments != null) {
List<TagObject> list = tag.getList("ench");
for(TagObject enc : list) {
if(!enc.hasString("id"))
continue;
Enchantment ench = Enchantment.getEnchantment(enc.getString("id"));
if(ench != null)
this.enchantments.put(ench, enc.hasShort("lvl") ? (int)enc.getShort("lvl") : 1);
}
if(this.enchantments.isEmpty())
this.enchantments = null;
}
} }
public int getMaxStackSize() public int getMaxStackSize()
{ {
return this.getItem().getMaxAmount(); return this.item.getMaxAmount();
} }
public boolean isStackable() public boolean isStackable()
@ -178,29 +235,6 @@ public final class ItemStack
return this.isItemStackDamageable() && this.getItemDamage() > 0; return this.isItemStackDamageable() && this.getItemDamage() > 0;
} }
public int getItemDamage()
{
return this.isItemStackDamageable() && this.tag != null && this.tag.hasInt("dmg") ? this.tag.getInt("dmg") : 0;
}
public void setItemDamage(int damage)
{
if(!this.isItemStackDamageable())
return;
if(damage <= 0) {
if(this.tag != null) {
this.tag.remove("dmg");
if(this.tag.isEmpty())
this.tag = null;
}
}
else {
if(this.tag == null)
this.tag = new TagObject();
this.tag.setInt("dmg", damage);
}
}
public int getMaxDamage() public int getMaxDamage()
{ {
return this.item.getMaxDamage(); return this.item.getMaxDamage();
@ -319,17 +353,14 @@ public final class ItemStack
{ {
ItemStack itemstack = new ItemStack(this.item, this.size); ItemStack itemstack = new ItemStack(this.item, this.size);
if (this.tag != null) itemstack.copyData(this);
{
itemstack.tag = this.tag.copy();
}
return itemstack; return itemstack;
} }
public static boolean areItemStackTagsEqual(ItemStack stackA, ItemStack stackB) public static boolean areItemStackTagsEqual(ItemStack stackA, ItemStack stackB)
{ {
return stackA == null && stackB == null ? true : (stackA != null && stackB != null ? (stackA.tag == null && stackB.tag != null ? false : stackA.tag == null || stackA.tag.equals(stackB.tag)) : false); return stackA == null && stackB == null ? true : (stackA != null && stackB != null ? stackA.dataEquals(stackB) : false);
} }
/** /**
@ -345,7 +376,7 @@ public final class ItemStack
*/ */
private boolean isItemStackEqual(ItemStack other) private boolean isItemStackEqual(ItemStack other)
{ {
return this.size != other.size ? false : (this.item != other.item ? false : (this.tag == null && other.tag != null ? false : this.tag == null || this.tag.equals(other.tag))); return this.size != other.size ? false : (this.item != other.item ? false : this.dataEquals(other));
} }
/** /**
@ -431,36 +462,21 @@ public final class ItemStack
this.getItem().onPlayerStoppedUsing(this, worldIn, playerIn, timeLeft); this.getItem().onPlayerStoppedUsing(this, worldIn, playerIn, timeLeft);
} }
public boolean hasTag() public int getItemDamage()
{ {
return this.tag != null; return this.isItemStackDamageable() ? this.damage : 0;
} }
public TagObject getTag() public void setItemDamage(int damage)
{ {
return this.tag; if(!this.isItemStackDamageable())
} return;
this.damage = Math.max(0, damage);
public List<TagObject> getEnchantmentTagList()
{
return this.tag == null ? null : this.tag.getList("ench");
}
public void setTag(TagObject tag)
{
this.tag = tag;
} }
public String getDisplayName() public String getDisplayName()
{ {
String s = this.getItem().getDisplay(this); return this.name != null ? this.name : this.getItem().getDisplay();
if (this.tag != null && this.tag.hasString("Name"))
{
s = this.tag.getString("Name");
}
return s;
} }
public String getColoredName() { public String getColoredName() {
@ -471,36 +487,20 @@ public final class ItemStack
return TextColor.DGRAY + "[" + this.getColor() + this.getDisplayName() + TextColor.DGRAY + "]" + reset; return TextColor.DGRAY + "[" + this.getColor() + this.getDisplayName() + TextColor.DGRAY + "]" + reset;
} }
public ItemStack setStackDisplayName(String displayName) public void setStackDisplayName(String displayName)
{ {
if (this.tag == null) this.name = displayName != null && !displayName.isEmpty() ? displayName : null;
{ this.name = this.name != null && this.name.length() > 32 ? this.name.substring(0, 32) : this.name;
this.tag = new TagObject();
}
this.tag.setString("Name", displayName);
return this;
} }
public void clearCustomName() public void clearCustomName()
{ {
if (this.tag != null) this.name = null;
{
if (this.tag.hasString("Name"))
{
this.tag.remove("Name");
if (this.tag.isEmpty())
{
this.setTag(null);
}
}
}
} }
public boolean hasDisplayName() public boolean hasDisplayName()
{ {
return this.tag != null && this.tag.hasString("Name"); return this.name != null;
} }
public List<String> getTooltip(EntityNPC playerIn) public List<String> getTooltip(EntityNPC playerIn)
@ -515,22 +515,11 @@ public final class ItemStack
if(this.item.canBeDyed()) if(this.item.canBeDyed())
list.add("Farbe: #" + Integer.toHexString(this.getDyeColor()).toUpperCase()); list.add("Farbe: #" + Integer.toHexString(this.getDyeColor()).toUpperCase());
if (this.hasTag()) if (this.enchantments != null)
{ {
List<TagObject> ench = this.getEnchantmentTagList(); for (Entry<Enchantment, Integer> enc : this.enchantments.entrySet())
if (ench != null)
{ {
for (int j = 0; j < ench.size(); ++j) list.add(enc.getKey().getFormattedName(enc.getValue()));
{
Enchantment k = Enchantment.getEnchantment(ench.get(j).getString("id"));
if (k != null)
{
int l = ench.get(j).getShort("lvl");
list.add(k.getFormattedName(l));
}
}
} }
} }
@ -567,27 +556,22 @@ public final class ItemStack
} }
} }
if (this.isItemStackDamageable()) if (this.item.getMaxDamage() > 0)
{ {
list.add(String.format("Haltbarkeit: %d" + (this.isItemDamaged() ? " / %d" : ""), list.add(String.format("Haltbarkeit: %d" + (this.damage > 0 ? " / %d" : ""),
this.isItemDamaged() ? (this.getMaxDamage() - this.getItemDamage()) : this.getMaxDamage(), this.getMaxDamage())); this.damage > 0 ? (this.item.getMaxDamage() - this.damage) : this.item.getMaxDamage(), this.item.getMaxDamage()));
} }
if(this.getRepairCost() > 0) if(this.repairCost > 0)
list.add("Reparaturkosten: " + this.getRepairCost() + " Mana"); list.add("Reparaturkosten: " + this.repairCost + " Mana");
if(this.getMaxStackSize() == 1) if(this.item.getMaxAmount() == 1)
list.add("Nicht stapelbar"); list.add("Nicht stapelbar");
else else
list.add("Stapelbar bis " + this.getMaxStackSize()); list.add("Stapelbar bis " + this.item.getMaxAmount());
list.add(TextColor.GRAY + ItemRegistry.getName(this.item)); list.add(TextColor.GRAY + ItemRegistry.getName(this.item));
if (this.hasTag())
{
list.add(TextColor.GRAY + String.format("Tags: %d", this.getTag().keySet().size()));
}
return list; return list;
} }
@ -601,146 +585,70 @@ public final class ItemStack
return this.getItem().getColor(this); return this.getItem().getColor(this);
} }
/**
* True if it is a tool and has no enchantments to begin with
*/
public boolean isItemEnchantable() public boolean isItemEnchantable()
{ {
return !this.getItem().canEnchant(this) ? false : !this.isItemEnchanted(); return !this.getItem().canEnchant(this) ? false : !this.isItemEnchanted();
} }
/** public Set<Entry<Enchantment, Integer>> getEnchantments()
* Adds an enchantment with a desired level on the ItemStack. {
*/ return this.enchantments == null ? null : this.enchantments.entrySet();
}
public Integer getEnchantment(Enchantment ench)
{
return this.enchantments == null ? null : this.enchantments.get(ench);
}
public void addEnchantment(Enchantment ench, int level) public void addEnchantment(Enchantment ench, int level)
{ {
if (this.tag == null) if (this.enchantments == null)
{ this.enchantments = Maps.newEnumMap(Enchantment.class);
this.setTag(new TagObject()); this.enchantments.put(ench, level);
} }
if (!this.tag.hasList("ench"))
{
this.tag.setList("ench", Lists.newArrayList());
}
List<TagObject> list = this.tag.getList("ench");
TagObject tag = new TagObject();
tag.setString("id", ench.getName());
tag.setShort("lvl", (short)(/* (byte) */ level));
list.add(tag);
}
/**
* Removes an enchantment from the ItemStack.
*/
public boolean removeEnchantment(Enchantment ench) { public boolean removeEnchantment(Enchantment ench) {
if(this.tag == null) { if(this.enchantments == null || this.enchantments.remove(ench) == null)
return false; return false;
} if(this.enchantments.isEmpty())
if(!this.tag.hasList("ench")) { this.enchantments = null;
return false;
}
List<TagObject> oldEnch = this.tag.getList("ench");
List<TagObject> newEnch = Lists.newArrayList();
boolean changed = false;
TagObject tag;
for(int z = 0; z < oldEnch.size(); z++) {
tag = oldEnch.get(z);
if(Enchantment.getEnchantment(tag.getString("id")) != ench) {
newEnch.add(tag);
}
else {
changed = true;
}
}
if(!changed) {
return false;
}
if(newEnch.size() == 0) {
this.tag.remove("ench");
if(this.tag.isEmpty()) {
this.tag = null;
}
}
else {
this.tag.setList("ench", newEnch);
}
return true; return true;
} }
/**
* Removes all enchantments from the ItemStack.
*/
public boolean clearEnchantments() { public boolean clearEnchantments() {
if(this.tag == null) { if(this.enchantments != null) {
return false; this.enchantments = null;
}
if(!this.tag.hasList("ench")) {
return false;
}
this.tag.remove("ench");
if(this.tag.isEmpty()) {
this.tag = null;
}
return true; return true;
} }
return false;
}
/**
* True if the item has enchantment data
*/
public boolean isItemEnchanted() public boolean isItemEnchanted()
{ {
return this.tag != null && this.tag.hasList("ench"); return this.enchantments != null;
} }
public void setTagInfo(String key, TagObject value) public void setEnchantments(Map<Enchantment, Integer> enchMap)
{ {
if (this.tag == null) if(enchMap == null || enchMap.isEmpty()) {
{ this.enchantments = null;
this.setTag(new TagObject()); return;
} }
if(this.enchantments == null)
this.tag.setObject(key, value); this.enchantments = Maps.newEnumMap(Enchantment.class);
} else
this.enchantments.clear();
public void setTagInfo(String key, List<TagObject> value) this.enchantments.putAll(enchMap);
{
if (this.tag == null)
{
this.setTag(new TagObject());
}
this.tag.setList(key, value);
} }
public int getRepairCost() public int getRepairCost()
{ {
return this.hasTag() && this.tag.hasInt("RepairCost") ? this.tag.getInt("RepairCost") : 0; return this.repairCost;
} }
public void setRepairCost(int cost) public void setRepairCost(int cost)
{ {
if(cost == 0) { this.repairCost = Math.max(0, cost);
if(this.tag == null) {
return;
}
if(!this.tag.hasInt("RepairCost")) {
return;
}
this.tag.remove("RepairCost");
if(this.tag.isEmpty()) {
this.tag = null;
}
return;
}
if (!this.hasTag())
{
this.tag = new TagObject();
}
this.tag.setInt("RepairCost", cost);
} }
public Map<Attribute, Float> getAttributeModifiers(UsageSlot slot) { public Map<Attribute, Float> getAttributeModifiers(UsageSlot slot) {
@ -754,45 +662,41 @@ public final class ItemStack
this.item = newItem; this.item = newItem;
} }
public boolean canBeDyed()
{
return this.item.canBeDyed();
}
public boolean hasColor() public boolean hasColor()
{ {
return this.item.canBeDyed() && this.tag != null && this.tag.hasInt("color"); return this.item.canBeDyed() && this.color != 0xffffffff;
}
public int getRawColor()
{
if(!this.item.canBeDyed())
return -1;
return this.color;
} }
public int getDyeColor() public int getDyeColor()
{ {
if(!this.item.canBeDyed()) if(!this.item.canBeDyed())
return -1; return -1;
return this.color != 0xffffffff ? this.color : this.item.getDefaultColor();
if (this.tag != null && this.tag.hasInt("color"))
{
return this.tag.getInt("color");
}
return this.item.getDefaultColor();
} }
public void removeColor() public void removeColor()
{ {
if(!this.item.canBeDyed()) if(!this.item.canBeDyed())
throw new UnsupportedOperationException("Kann diesen Gegenstand nicht entfärben!"); throw new UnsupportedOperationException("Kann diesen Gegenstand nicht entfärben!");
if (this.tag != null) this.color = 0xffffffff;
{
if (this.tag.hasInt("color"))
{
this.tag.remove("color");
if(this.tag.isEmpty())
this.tag = null;
}
}
} }
public void setColor(int color) public void setColor(int color)
{ {
if(!this.item.canBeDyed()) if(!this.item.canBeDyed())
throw new UnsupportedOperationException("Kann diesen Gegenstand nicht einfärben!"); throw new UnsupportedOperationException("Kann diesen Gegenstand nicht einfärben!");
if (this.tag == null) this.color = (color & 0xff000000) != 0 ? 0xffffffff : color;
this.tag = new TagObject();
this.tag.setInt("color", color);
} }
} }

View file

@ -11,8 +11,6 @@ import common.item.ItemStack;
import common.model.Model; import common.model.Model;
import common.model.ModelProvider; import common.model.ModelProvider;
import common.model.Transforms; import common.model.Transforms;
import common.tags.TagObject;
import common.tileentity.TileEntity;
import common.util.BlockPos; import common.util.BlockPos;
import common.util.Facing; import common.util.Facing;
import common.world.State; import common.world.State;
@ -81,7 +79,6 @@ public class ItemBlock extends Item
if (iblockstate1.getBlock() == this.block) if (iblockstate1.getBlock() == this.block)
{ {
setTileData(worldIn, playerIn, pos, stack);
this.block.onBlockPlacedBy(worldIn, pos, iblockstate1, playerIn, stack); this.block.onBlockPlacedBy(worldIn, pos, iblockstate1, playerIn, stack);
} }
@ -97,37 +94,6 @@ public class ItemBlock extends Item
} }
} }
public static boolean setTileData(World world, EntityNPC player, BlockPos pos, ItemStack stack)
{
if (stack.hasTag() && stack.getTag().hasObject("BlockEntityTag"))
{
TileEntity tile = world.getTileEntity(pos);
if (tile != null)
{
if (!world.client && !player.connection.isAdmin())
{
return false;
}
TagObject tag = new TagObject();
TagObject tileTag = tag.copy();
tile.writeTags(tag);
TagObject stackTag = stack.getTag().getObject("BlockEntityTag");
tag.merge(stackTag);
if (!tag.equals(tileTag))
{
tile.readTags(tag);
tile.markDirty();
return true;
}
}
}
return false;
}
public boolean canPlaceBlockOnSide(World worldIn, BlockPos pos, Facing side, EntityNPC player, ItemStack stack) public boolean canPlaceBlockOnSide(World worldIn, BlockPos pos, Facing side, EntityNPC player, ItemStack stack)
{ {
Block block = worldIn.getState(pos).getBlock(); Block block = worldIn.getState(pos).getBlock();

View file

@ -74,7 +74,7 @@ public class ItemSign extends Item
--stack.size; --stack.size;
TileEntity tileentity = worldIn.getTileEntity(pos); TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntitySign && !ItemBlock.setTileData(worldIn, playerIn, pos, stack)) if (tileentity instanceof TileEntitySign)
{ {
playerIn.openEditSign((TileEntitySign)tileentity); playerIn.openEditSign((TileEntitySign)tileentity);
} }

View file

@ -40,7 +40,6 @@ public class ItemSmallBlock extends Item {
newState = world.getState(pos); newState = world.getState(pos);
if(newState.getBlock() == this.block) { if(newState.getBlock() == this.block) {
ItemBlock.setTileData(world, player, pos, stack);
newState.getBlock().onBlockPlacedBy(world, pos, newState, player, stack); newState.getBlock().onBlockPlacedBy(world, pos, newState, player, stack);
} }

View file

@ -51,7 +51,7 @@ public class ItemCarrotOnAStick extends Item
if (itemStackIn.size == 0) if (itemStackIn.size == 0)
{ {
ItemStack itemstack = new ItemStack(Items.fishing_rod); ItemStack itemstack = new ItemStack(Items.fishing_rod);
itemstack.setTag(itemStackIn.getTag()); itemstack.copyData(itemStackIn);
return itemstack; return itemstack;
} }
} }

View file

@ -2,7 +2,10 @@ package common.network;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Set;
import java.util.Map.Entry;
import common.enchantment.Enchantment;
import common.init.ItemRegistry; import common.init.ItemRegistry;
import common.item.ItemStack; import common.item.ItemStack;
import common.net.buffer.ByteBuf; import common.net.buffer.ByteBuf;
@ -119,16 +122,38 @@ public class PacketBuffer {
} }
this.writeShort(ItemRegistry.getId(stack.getItem())); this.writeShort(ItemRegistry.getId(stack.getItem()));
this.writeVarInt(stack.size); this.writeVarInt(stack.size);
this.writeTag(stack.getTag()); if(stack.canBeDyed())
this.writeVarInt(stack.getRawColor());
if(stack.isItemStackDamageable())
this.writeVarInt(stack.getItemDamage());
this.writeVarInt(stack.getRepairCost());
this.writeString(stack.hasDisplayName() ? stack.getDisplayName() : "");
Set<Entry<Enchantment, Integer>> ench = stack.getEnchantments();
this.writeVarInt(ench == null ? 0 : ench.size());
if(ench != null) {
for(Entry<Enchantment, Integer> entry : ench) {
this.writeEnumValue(entry.getKey());
this.writeShort(entry.getValue());
}
}
} }
public ItemStack readItemStack() throws IOException { public ItemStack readItemStack() throws IOException {
int id = this.readShort(); int id = this.readShort();
if(id < 0) if(id < 0)
return null; return null;
int amt = this.readVarInt(); ItemStack stack = new ItemStack(ItemRegistry.byId(id), this.readVarInt());
ItemStack stack = new ItemStack(ItemRegistry.byId(id), amt); if(stack.canBeDyed())
stack.setTag(this.readTag()); stack.setColor(this.readVarInt());
if(stack.isItemStackDamageable())
stack.setItemDamage(this.readVarInt());
stack.setRepairCost(this.readVarInt());
stack.setStackDisplayName(this.readString(32));
int num = this.readVarInt();
for(int z = 0; z < num; z++) {
Enchantment enc = this.readEnumValue(Enchantment.class);
stack.addEnchantment(enc, this.readShort());
}
return stack; return stack;
} }

View file

@ -31,9 +31,8 @@ public class MerchantRecipeList extends ArrayList<MerchantRecipe> {
} }
} }
private static boolean areItemsSimilar(ItemStack stack1, ItemStack stack2) { private static boolean areItemsSimilar(ItemStack given, ItemStack offer) {
return ItemStack.areItemsEqual(stack1, stack2) return ItemStack.areItemsEqual(given, offer) && given.dataEquals(offer);
&& (!stack2.hasTag() || stack1.hasTag() && TagObject.compare(stack2.getTag(), stack1.getTag()));
} }
public void fromTags(List<TagObject> list) { public void fromTags(List<TagObject> list) {

View file

@ -34,7 +34,7 @@ public class CommandItem extends Command {
ItemStack stack = ItemStack.getStack(item, null); ItemStack stack = ItemStack.getStack(item, null);
if(stack == null) if(stack == null)
throw new RunException("Gegenstand '%s' existiert nicht", item); throw new RunException("Gegenstand '%s' existiert nicht", item);
stack.setTag(tag); stack.readTag(tag);
int done = 0; int done = 0;
int given = 0; int given = 0;
for(EntityNPC player : players) { for(EntityNPC player : players) {