diff --git a/src/main/java/com/volmit/iris/object/tile/TileData.java b/src/main/java/com/volmit/iris/object/tile/TileData.java new file mode 100644 index 000000000..e75418712 --- /dev/null +++ b/src/main/java/com/volmit/iris/object/tile/TileData.java @@ -0,0 +1,105 @@ +package com.volmit.iris.object.tile; + +import com.volmit.iris.util.KList; +import net.querz.nbt.tag.CompoundTag; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.block.TileState; +import org.bukkit.block.data.BlockData; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public interface TileData extends Cloneable { + public static int id = 0; + public static final KList> registry = setup(); + + static KList> setup() { + KList> registry = new KList<>(); + + registry.add(new TileSign()); + + return registry; + } + + public static TileData read(DataInputStream s) throws Throwable { + int id = s.readShort(); + TileData d = registry.get(id).getClass().getConstructor().newInstance(); + d.fromBinary(s); + return d; + } + + public static void setTileState(Block block, TileData data) + { + if(data.isApplicable(block.getBlockData())) + { + data.toBukkitTry(block.getState()); + } + } + + public static TileData getTileState(Block block) + { + for(TileData i : registry) + { + BlockData data = block.getBlockData(); + + if(i.isApplicable(data)) + { + try { + TileData s = i.getClass().getConstructor().newInstance(); + s.fromBukkitTry(block.getState()); + return s; + } catch (Throwable e) { + e.printStackTrace(); + } + } + } + + return null; + } + + public boolean isApplicable(BlockData data); + + public void toBukkit(T t); + + public void fromBukkit(T t); + + public default boolean toBukkitTry(BlockState t) + { + try { + toBukkit((T) t); + return true; + } + + catch(Throwable e) + { + + } + + return false; + } + + public default boolean fromBukkitTry(BlockState t) + { + try { + fromBukkit((T) t); + return true; + } + + catch(Throwable e) + { + + } + + return false; + } + + public TileData clone(); + + public void toBinary(DataOutputStream out) throws IOException; + + public void toNBT(CompoundTag tag); + + public void fromBinary(DataInputStream in) throws IOException; +}