package ru.will0376.money.client.block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.server.management.PlayerChunkMapEntry;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import java.util.ArrayList;
public class TestContainerTileEntity extends TileEntity {
public static final int SIZE = 56;
public ArrayList<String> players = new ArrayList<>();
public boolean infinity,FirstOpen;
public String Count,Price,Owner,Color;
public int Sales;
public ItemStackHandler itemStackHandler = new ItemStackHandler(SIZE)
{
@Override
protected void onContentsChanged(int slot) {
TestContainerTileEntity.this.markDirty();
if(slot == 0) {
sendUpdates();
}
}
};
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
if (compound.hasKey("items"))
itemStackHandler.deserializeNBT((NBTTagCompound) compound.getTag("items"));
if(compound.hasKey("players"))
players = deserializePlayersNBT(compound);
if(compound.hasKey("Infinity"))
infinity = compound.getBoolean("Infinity");
if(compound.hasKey("FirstOpen"))
FirstOpen = compound.getBoolean("FirstOpen");
if(compound.hasKey("Owner"))
Owner = compound.getString("Owner");
if(compound.hasKey("Count"))
Count = compound.getString("Count");
if(compound.hasKey("Price"))
Price = compound.getString("Price");
if(compound.hasKey("Sales"))
Sales = compound.getInteger("Sales");
if(compound.hasKey("Color"))
Color = compound.getString("Color");
}
public ArrayList deserializePlayersNBT(NBTTagCompound nbt)
{
ArrayList<String> tmp = new ArrayList<>();
NBTTagList tagList = nbt.getTagList("Players", 8);
for(int i = 0;i < tagList.tagCount();i++){
NBTTagCompound itemTags = tagList.getCompoundTagAt(i);
String player = itemTags.getString("Player");
tmp.add(player);
}
return tmp;
}
private NBTTagCompound playersToNbt(){
NBTTagList nbtTagList = new NBTTagList();
for (int i = 0; i < players.size(); i++)
{
NBTTagCompound player = new NBTTagCompound();
player.setString("Player", players.get(i));
nbtTagList.appendTag(player);
}
NBTTagCompound nbt = new NBTTagCompound();
nbt.setTag("Players", nbtTagList);
nbt.setInteger("Size", players.size());
return nbt;
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
compound.setTag("items", itemStackHandler.serializeNBT());
compound.setTag("players", playersToNbt());
compound.setBoolean("Infinity",infinity);
compound.setBoolean("FirstOpen",FirstOpen);
if(Owner != null)
compound.setString("Owner",Owner);
if(Count != null)
compound.setString("Count",Count);
if(Price != null)
compound.setString("Price",Price);
if(Color != null)
compound.setString("Color",Color);
compound.setInteger("Sales",Sales);
return compound;
}
public boolean canInteractWith(EntityPlayer playerIn) {
return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
}
@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
return true;
}
return super.hasCapability(capability, facing);
}
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(itemStackHandler);
}
return super.getCapability(capability, facing);
}
@Override
public void onDataPacket(net.minecraft.network.NetworkManager net, net.minecraft.network.play.server.SPacketUpdateTileEntity pkt) {
readFromNBT(pkt.getNbtCompound());
}
@Override
public SPacketUpdateTileEntity getUpdatePacket() {
NBTTagCompound tagCompound = new NBTTagCompound();
writeToNBT(tagCompound);
return new SPacketUpdateTileEntity(pos, 3, tagCompound);
}
public void sendUpdates(){
SPacketUpdateTileEntity packet = getUpdatePacket();
if (packet != null && world instanceof WorldServer) {
PlayerChunkMapEntry chunk = ((WorldServer)getWorld()).getPlayerChunkMap().getEntry(pos.getX() >> 4, pos.getZ() >> 4);
if (chunk != null) chunk.sendPacket(packet);
}
}
}