mirror of
https://github.com/chylex/Hardcore-Ender-Expansion.git
synced 2025-04-10 20:15:42 +02:00
Add WIP NBT wrappers
This commit is contained in:
parent
48c6ef5f0f
commit
4139a1a27b
src/main/java/chylex/hee/system/abstractions/nbt
68
src/main/java/chylex/hee/system/abstractions/nbt/NBT.java
Normal file
68
src/main/java/chylex/hee/system/abstractions/nbt/NBT.java
Normal file
@ -0,0 +1,68 @@
|
||||
package chylex.hee.system.abstractions.nbt;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTBase;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
public final class NBT{
|
||||
private static final NBTCompound dummyTag = new NBTCompound(new NBTTagCompoundDummy());
|
||||
|
||||
public static NBTCompound wrap(NBTTagCompound tag){
|
||||
return new NBTCompound(tag);
|
||||
}
|
||||
|
||||
public static NBTCompound item(ItemStack is, boolean create){
|
||||
NBTTagCompound tag = is.getTagCompound();
|
||||
|
||||
if (tag == null){
|
||||
if (create)is.setTagCompound(tag = new NBTTagCompound());
|
||||
else return dummyTag;
|
||||
}
|
||||
|
||||
return new NBTCompound(tag);
|
||||
}
|
||||
|
||||
public static NBTCompound callback(Runnable callback){
|
||||
return new NBTCompound(new NBTTagCompoundCallback(callback));
|
||||
}
|
||||
|
||||
public static interface IStringIntConsumer{ void accept(String key, int value); }
|
||||
public static interface IStringLongConsumer{ void accept(String key, long value); }
|
||||
|
||||
private static final class NBTTagCompoundDummy extends NBTTagCompound{
|
||||
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){}
|
||||
}
|
||||
|
||||
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 NBT(){}
|
||||
}
|
@ -0,0 +1,219 @@
|
||||
package chylex.hee.system.abstractions.nbt;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.stream.DoubleStream;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTBase;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagDouble;
|
||||
import net.minecraft.nbt.NBTTagInt;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.nbt.NBTTagLong;
|
||||
import chylex.hee.system.util.NBTUtil.IStringIntConsumer;
|
||||
import chylex.hee.system.util.NBTUtil.IStringLongConsumer;
|
||||
|
||||
public class NBTCompound{
|
||||
private final NBTTagCompound tag;
|
||||
|
||||
public NBTCompound(NBTTagCompound tag){
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
public NBTTagCompound getUnderlyingTag(){
|
||||
return tag;
|
||||
}
|
||||
|
||||
// NEW HANDLING
|
||||
|
||||
public Set<String> keySet(){
|
||||
return tag.func_150296_c();
|
||||
}
|
||||
|
||||
public int size(){
|
||||
return tag.func_150296_c().size();
|
||||
}
|
||||
|
||||
public void forEach(BiConsumer<NBTCompound,String> consumer){
|
||||
for(String key:keySet())consumer.accept(this,key);
|
||||
}
|
||||
|
||||
public void forEachInt(IStringIntConsumer consumer){
|
||||
for(String key:keySet())consumer.accept(key,getInt(key));
|
||||
}
|
||||
|
||||
public void forEachLong(IStringLongConsumer consumer){
|
||||
for(String key:keySet())consumer.accept(key,getLong(key));
|
||||
}
|
||||
|
||||
public void forEachCompound(BiConsumer<String,NBTCompound> consumer){
|
||||
for(String key:keySet())consumer.accept(key,getCompound(key));
|
||||
}
|
||||
|
||||
// CUSTOM WRITERS AND READERS
|
||||
|
||||
public void writeInventory(String key, IInventory inventory){
|
||||
NBTTagList list = new NBTTagList();
|
||||
|
||||
for(int slot = 0; slot < inventory.getSizeInventory(); slot++){
|
||||
ItemStack is = inventory.getStackInSlot(slot);
|
||||
|
||||
if (is != null){
|
||||
NBTTagCompound itemTag = is.writeToNBT(new NBTTagCompound());
|
||||
itemTag.setByte("_",(byte)slot);
|
||||
list.appendTag(itemTag);
|
||||
}
|
||||
}
|
||||
|
||||
setTag(key,list);
|
||||
}
|
||||
|
||||
public void readInventory(String key, IInventory inventory){
|
||||
NBTTagList list = (NBTTagList)getTag(key);
|
||||
|
||||
for(int slot = 0; slot < list.tagCount(); slot++){
|
||||
NBTTagCompound itemTag = list.getCompoundTagAt(slot);
|
||||
inventory.setInventorySlotContents(itemTag.getByte("_"),ItemStack.loadItemStackFromNBT(itemTag));
|
||||
}
|
||||
}
|
||||
|
||||
public <T extends NBTBase> void writeList(String key, Stream<T> stream){
|
||||
NBTTagList tag = new NBTTagList();
|
||||
stream.forEach(tag::appendTag);
|
||||
setTag(key,tag);
|
||||
}
|
||||
|
||||
public void writeList(String key, IntStream stream){
|
||||
writeList(key,stream.mapToObj(NBTTagInt::new));
|
||||
}
|
||||
|
||||
public void writeList(String key, LongStream stream){
|
||||
writeList(key,stream.mapToObj(NBTTagLong::new));
|
||||
}
|
||||
|
||||
public void writeList(String key, DoubleStream stream){
|
||||
writeList(key,stream.mapToObj(NBTTagDouble::new));
|
||||
}
|
||||
|
||||
// DELEGATE SETTERS
|
||||
|
||||
public void setTag(String key, NBTBase value){
|
||||
tag.setTag(key,value);
|
||||
}
|
||||
|
||||
public void setBool(String key, boolean value){
|
||||
tag.setBoolean(key,value);
|
||||
}
|
||||
|
||||
public void setByte(String key, byte value){
|
||||
tag.setByte(key,value);
|
||||
}
|
||||
|
||||
public void setShort(String key, short value){
|
||||
tag.setShort(key,value);
|
||||
}
|
||||
|
||||
public void setInt(String key, int value){
|
||||
tag.setInteger(key,value);
|
||||
}
|
||||
|
||||
public void setLong(String key, long value){
|
||||
tag.setLong(key,value);
|
||||
}
|
||||
|
||||
public void setFloat(String key, float value){
|
||||
tag.setFloat(key,value);
|
||||
}
|
||||
|
||||
public void setDouble(String key, double value){
|
||||
tag.setDouble(key,value);
|
||||
}
|
||||
|
||||
public void setString(String key, String value){
|
||||
tag.setString(key,value);
|
||||
}
|
||||
|
||||
public void setByteArray(String key, byte[] value){
|
||||
tag.setByteArray(key,value);
|
||||
}
|
||||
|
||||
public void setIntArray(String key, int[] value){
|
||||
tag.setIntArray(key,value);
|
||||
}
|
||||
|
||||
public void setCompound(String key, NBTTagCompound value){
|
||||
tag.setTag(key,value);
|
||||
}
|
||||
|
||||
public void setCompound(String key, NBTCompound value){
|
||||
tag.setTag(key,value.getUnderlyingTag());
|
||||
}
|
||||
|
||||
// DELEGATE GETTERS
|
||||
|
||||
public NBTBase getTag(String key){
|
||||
return tag.getTag(key);
|
||||
}
|
||||
|
||||
public boolean getBool(String key){
|
||||
return tag.getBoolean(key);
|
||||
}
|
||||
|
||||
public byte getByte(String key){
|
||||
return tag.getByte(key);
|
||||
}
|
||||
|
||||
public short getShort(String key){
|
||||
return tag.getShort(key);
|
||||
}
|
||||
|
||||
public int getInt(String key){
|
||||
return tag.getInteger(key);
|
||||
}
|
||||
|
||||
public long getLong(String key){
|
||||
return tag.getLong(key);
|
||||
}
|
||||
|
||||
public float getFloat(String key){
|
||||
return tag.getFloat(key);
|
||||
}
|
||||
|
||||
public double getDouble(String key){
|
||||
return tag.getDouble(key);
|
||||
}
|
||||
|
||||
public String getString(String key){
|
||||
return tag.getString(key);
|
||||
}
|
||||
|
||||
public byte[] getByteArray(String key){
|
||||
return tag.getByteArray(key);
|
||||
}
|
||||
|
||||
public int[] getIntArray(String key){
|
||||
return tag.getIntArray(key);
|
||||
}
|
||||
|
||||
public NBTCompound getCompound(String key){
|
||||
return new NBTCompound(tag.getCompoundTag(key));
|
||||
}
|
||||
|
||||
public NBTList getList(String key){
|
||||
return new NBTList((NBTTagList)tag.getTag(key));
|
||||
}
|
||||
|
||||
// MISC DELEGATES
|
||||
|
||||
public boolean hasKey(String key){
|
||||
return tag.hasKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return tag.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package chylex.hee.system.abstractions.nbt;
|
||||
import java.util.List;
|
||||
import java.util.stream.DoubleStream;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
import net.minecraft.nbt.NBTBase;
|
||||
import net.minecraft.nbt.NBTBase.NBTPrimitive;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagDouble;
|
||||
import net.minecraft.nbt.NBTTagInt;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.nbt.NBTTagLong;
|
||||
import net.minecraft.nbt.NBTTagString;
|
||||
|
||||
public class NBTList{
|
||||
private final NBTTagList tag;
|
||||
|
||||
public NBTList(NBTTagList tag){
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
// APPENDING
|
||||
|
||||
public void appendTag(NBTBase value){
|
||||
tag.appendTag(value);
|
||||
}
|
||||
|
||||
public void appendString(String value){
|
||||
tag.appendTag(new NBTTagString(value));
|
||||
}
|
||||
|
||||
public void appendInt(int value){
|
||||
tag.appendTag(new NBTTagInt(value));
|
||||
}
|
||||
|
||||
public void appendLong(long value){
|
||||
tag.appendTag(new NBTTagLong(value));
|
||||
}
|
||||
|
||||
public void appendDouble(double value){
|
||||
tag.appendTag(new NBTTagDouble(value));
|
||||
}
|
||||
|
||||
// READING
|
||||
|
||||
public Stream<NBTPrimitive> readPrimitives(){
|
||||
return ((List<NBTPrimitive>)tag.tagList).stream();
|
||||
}
|
||||
|
||||
public IntStream readInts(){
|
||||
return readPrimitives().mapToInt(NBTPrimitive::func_150287_d);
|
||||
}
|
||||
|
||||
public LongStream readLongs(){
|
||||
return readPrimitives().mapToLong(NBTPrimitive::func_150291_c);
|
||||
}
|
||||
|
||||
public DoubleStream readDoubles(){
|
||||
return readPrimitives().mapToDouble(NBTPrimitive::func_150286_g);
|
||||
}
|
||||
|
||||
public Stream<String> readStrings(){
|
||||
return ((List<NBTTagString>)tag.tagList).stream().map(NBTTagString::func_150285_a_);
|
||||
}
|
||||
|
||||
public Stream<NBTCompound> readCompounds(){
|
||||
return ((List<NBTTagCompound>)tag.tagList).stream().map(NBTCompound::new);
|
||||
}
|
||||
|
||||
// DELEGATES
|
||||
|
||||
public int size(){
|
||||
return tag.tagCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return tag.toString();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user