2018-10-13 20:35:18 +02:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
2018-10-20 21:19:08 +02:00
|
|
|
import de.ellpeck.naturesaura.aura.Capabilities;
|
2018-10-21 12:51:13 +02:00
|
|
|
import de.ellpeck.naturesaura.aura.container.IAuraContainer;
|
2018-10-13 20:35:18 +02:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.network.NetworkManager;
|
|
|
|
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
|
|
|
|
import net.minecraft.server.management.PlayerChunkMapEntry;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2018-10-18 13:34:37 +02:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2018-10-13 20:35:18 +02:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraft.world.WorldServer;
|
2018-10-18 13:34:37 +02:00
|
|
|
import net.minecraftforge.common.capabilities.Capability;
|
|
|
|
import net.minecraftforge.items.CapabilityItemHandler;
|
|
|
|
import net.minecraftforge.items.IItemHandlerModifiable;
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
2018-10-13 20:35:18 +02:00
|
|
|
|
|
|
|
public class TileEntityImpl extends TileEntity {
|
|
|
|
@Override
|
|
|
|
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState) {
|
|
|
|
return oldState.getBlock() != newState.getBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-10-13 20:45:32 +02:00
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
2018-10-13 20:35:18 +02:00
|
|
|
this.writeNBT(compound, false);
|
2018-10-14 14:27:18 +02:00
|
|
|
return compound;
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-10-13 20:45:32 +02:00
|
|
|
public void readFromNBT(NBTTagCompound compound) {
|
2018-10-13 20:35:18 +02:00
|
|
|
this.readNBT(compound, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeNBT(NBTTagCompound compound, boolean syncing) {
|
2018-10-14 14:27:18 +02:00
|
|
|
super.writeToNBT(compound);
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void readNBT(NBTTagCompound compound, boolean syncing) {
|
2018-10-14 14:27:18 +02:00
|
|
|
super.readFromNBT(compound);
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final SPacketUpdateTileEntity getUpdatePacket() {
|
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
|
|
|
this.writeNBT(compound, true);
|
|
|
|
return new SPacketUpdateTileEntity(this.pos, 0, compound);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final NBTTagCompound getUpdateTag() {
|
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
|
|
|
this.writeNBT(compound, true);
|
|
|
|
return compound;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleUpdateTag(NBTTagCompound tag) {
|
|
|
|
this.readNBT(tag, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
|
|
|
|
super.onDataPacket(net, packet);
|
|
|
|
this.readNBT(packet.getNbtCompound(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendToClients() {
|
|
|
|
WorldServer world = (WorldServer) this.getWorld();
|
|
|
|
PlayerChunkMapEntry entry = world.getPlayerChunkMap().getEntry(this.getPos().getX() >> 4, this.getPos().getZ() >> 4);
|
|
|
|
if (entry != null) {
|
2018-10-14 16:12:33 +02:00
|
|
|
entry.sendPacket(this.getUpdatePacket());
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-18 13:34:37 +02:00
|
|
|
|
|
|
|
public IItemHandlerModifiable getItemHandler(EnumFacing facing) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-10-20 21:19:08 +02:00
|
|
|
public IAuraContainer getAuraContainer(EnumFacing facing) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-10-18 13:34:37 +02:00
|
|
|
@Override
|
|
|
|
public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
|
|
|
|
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
|
|
|
|
return this.getItemHandler(facing) != null;
|
2018-10-20 21:19:08 +02:00
|
|
|
} else if (capability == Capabilities.auraContainer) {
|
|
|
|
return this.getAuraContainer(facing) != null;
|
2018-10-18 13:34:37 +02:00
|
|
|
} else {
|
|
|
|
return super.hasCapability(capability, facing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
|
|
|
|
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
|
|
|
|
return (T) this.getItemHandler(facing);
|
2018-10-20 21:19:08 +02:00
|
|
|
} else if (capability == Capabilities.auraContainer) {
|
|
|
|
return (T) this.getAuraContainer(facing);
|
2018-10-18 13:34:37 +02:00
|
|
|
} else {
|
|
|
|
return super.getCapability(capability, facing);
|
|
|
|
}
|
|
|
|
}
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|