1
0
mirror of https://github.com/chylex/Hardcore-Ender-Expansion.git synced 2025-03-15 16:15:46 +01:00

Remove now deprecated methods from ItemUtil, and remove NBTUtil completely

This commit is contained in:
chylex 2016-01-21 15:34:56 +01:00
parent 8fcb177fde
commit e3312323d6
2 changed files with 12 additions and 151 deletions
src/main/java/chylex/hee/system/util

View File

@ -1,28 +1,13 @@
package chylex.hee.system.util;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraftforge.common.util.Constants.NBT;
import chylex.hee.system.abstractions.nbt.NBT;
import chylex.hee.system.abstractions.nbt.NBTCompound;
import chylex.hee.system.abstractions.nbt.NBTList;
public final class ItemUtil{
private static final NBTTagCompound dummyTag = new NBTTagCompoundDummy();
public static NBTTagCompound getTagRoot(ItemStack is, boolean create){
NBTTagCompound nbt = is.getTagCompound();
if (nbt == null){
if (create)is.setTagCompound(nbt = new NBTTagCompound());
else return dummyTag;
}
return nbt;
}
public static NBTTagCompound getTagSub(ItemStack is, String key, boolean create){
NBTTagCompound root = getTagRoot(is,create), sub = root.getCompoundTag(key);
if (!root.hasKey(key) && create)root.setTag(key,sub);
public static NBTCompound getTagSub(ItemStack is, String key, boolean create){
NBTCompound root = NBT.item(is,create), sub = root.getCompound(key);
if (!root.hasKey(key) && create)root.setCompound(key,sub);
return sub;
}
@ -31,17 +16,17 @@ public final class ItemUtil{
}
public static void addLore(ItemStack is, String lore){
NBTTagCompound display = getTagSub(is,"display",true);
NBTTagList loreTag = display.getTagList("Lore",NBT.TAG_STRING);
NBTCompound display = getTagSub(is,"display",true);
NBTList loreTag = display.getList("Lore");
if (lore == null)loreTag = new NBTTagList();
else loreTag.appendTag(new NBTTagString(lore));
if (lore == null)loreTag = new NBTList();
else loreTag.appendString(lore);
display.setTag("Lore",loreTag);
display.setList("Lore",loreTag);
}
public static void setArmorColor(ItemStack is, int color){
getTagSub(is,"display",true).setInteger("color",color);
getTagSub(is,"display",true).setInt("color",color);
}
public static boolean canAddOneItemTo(ItemStack is, ItemStack itemToAdd){
@ -50,20 +35,4 @@ public final class ItemUtil{
ItemStack.areItemStackTagsEqual(is,itemToAdd) &&
is.stackSize+1 <= is.getMaxStackSize();
}
public static final class NBTTagCompoundDummy extends NBTTagCompound{
private NBTTagCompoundDummy(){}
@Override public void setBoolean(String key, boolean value){}
@Override public void setByte(String key, byte value){}
@Override public void setByteArray(String key, byte[] value){}
@Override public void setDouble(String key, double value){}
@Override public void setFloat(String key, float value){}
@Override public void setIntArray(String key, int[] value){}
@Override public void setInteger(String key, int value){}
@Override public void setLong(String key, long value){}
@Override public void setShort(String key, short value){}
@Override public void setString(String key, String value){}
@Override public void setTag(String key, NBTBase value){}
@Override public void removeTag(String key){}
}
}

View File

@ -1,108 +0,0 @@
package chylex.hee.system.util;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Stream;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTBase.NBTPrimitive;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraftforge.common.util.Constants.NBT;
public final class NBTUtil{
public static <T extends NBTBase> NBTTagList writeList(Stream<T> elementStream){
NBTTagList tag = new NBTTagList();
elementStream.forEach(ele -> tag.appendTag(ele));
return tag;
}
public static <T extends NBTBase> void writeList(NBTTagCompound parent, String key, Stream<T> elementStream){
parent.setTag(key,writeList(elementStream));
}
public static <T extends NBTBase> Stream<T> readList(NBTTagList list){
return list.tagList.stream();
}
public static Stream<String> readStringList(NBTTagCompound parent, String key){
return ((List<NBTTagString>)parent.getTagList(key,NBT.TAG_STRING).tagList).stream().map(tag -> tag.func_150285_a_());
}
public static Stream<NBTPrimitive> readNumericList(NBTTagCompound parent, String key){
return parent.hasKey(key) ? ((List<NBTPrimitive>)((NBTTagList)parent.getTag(key)).tagList).stream() : Stream.empty();
}
public static Stream<NBTTagCompound> readCompoundList(NBTTagCompound parent, String key){
return ((List<NBTTagCompound>)parent.getTagList(key,NBT.TAG_COMPOUND).tagList).stream();
}
public static NBTTagList writeInventory(IInventory inv){
NBTTagList list = new NBTTagList();
for(int slot = 0; slot < inv.getSizeInventory(); slot++){
ItemStack is = inv.getStackInSlot(slot);
if (is != null){
NBTTagCompound itemTag = is.writeToNBT(new NBTTagCompound());
itemTag.setByte("_",(byte)slot);
list.appendTag(itemTag);
}
}
return list;
}
public static void readInventory(NBTTagList list, IInventory inv){
for(int slot = 0; slot < list.tagCount(); slot++){
NBTTagCompound itemTag = list.getCompoundTagAt(slot);
inv.setInventorySlotContents(itemTag.getByte("_"),ItemStack.loadItemStackFromNBT(itemTag));
}
}
public static Set<String> getKeys(NBTTagCompound tag){
return tag.func_150296_c();
}
public static void forEachInt(NBTTagCompound tag, IStringIntConsumer consumer){
for(String key:getKeys(tag))consumer.accept(key,tag.getInteger(key));
}
public static void forEachLong(NBTTagCompound tag, IStringLongConsumer consumer){
for(String key:getKeys(tag))consumer.accept(key,tag.getLong(key));
}
public static void forEachCompoundTag(NBTTagCompound tag, BiConsumer<String,NBTTagCompound> consumer){
for(String key:getKeys(tag))consumer.accept(key,tag.getCompoundTag(key));
}
public static interface IStringIntConsumer{ void accept(String key, int value); }
public static interface IStringLongConsumer{ void accept(String key, long value); }
public static NBTTagCompound createCallbackTag(Runnable callback){
return new NBTTagCompoundCallback(callback);
}
private static final class NBTTagCompoundCallback extends NBTTagCompound{
private final Runnable callback;
NBTTagCompoundCallback(Runnable callback){
this.callback = callback;
}
@Override public void setTag(String key, NBTBase value){ super.setTag(key,value); callback.run(); }
@Override public void setByte(String key, byte value){ super.setByte(key,value); callback.run(); }
@Override public void setShort(String key, short value){ super.setShort(key,value); callback.run(); }
@Override public void setInteger(String key, int value){ super.setInteger(key,value); callback.run(); }
@Override public void setLong(String key, long value){ super.setLong(key,value); callback.run(); }
@Override public void setFloat(String key, float value){ super.setFloat(key,value); callback.run(); }
@Override public void setDouble(String key, double value){ super.setDouble(key,value); callback.run(); }
@Override public void setString(String key, String value){ super.setString(key,value); callback.run(); }
@Override public void setByteArray(String key, byte[] value){ super.setByteArray(key,value); callback.run(); }
@Override public void setIntArray(String key, int[] value){ super.setIntArray(key,value); callback.run(); }
}
private NBTUtil(){}
}