NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityImpl.java

163 lines
5.4 KiB
Java
Raw Normal View History

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;
import de.ellpeck.naturesaura.aura.container.IAuraContainer;
2018-11-04 16:38:09 +01:00
import net.minecraft.block.Block;
2018-10-13 20:35:18 +02:00
import net.minecraft.block.state.IBlockState;
2018-11-04 16:38:09 +01:00
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
2018-10-13 20:35:18 +02:00
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;
2018-11-04 16:38:09 +01:00
import net.minecraftforge.items.IItemHandler;
2018-10-18 13:34:37 +02:00
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-11-04 16:38:09 +01:00
this.writeNBT(compound, SaveType.TILE);
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-11-04 16:38:09 +01:00
this.readNBT(compound, SaveType.TILE);
2018-10-13 20:35:18 +02:00
}
2018-11-04 16:38:09 +01:00
public void writeNBT(NBTTagCompound compound, SaveType type) {
if (type != SaveType.BLOCK)
super.writeToNBT(compound);
2018-10-13 20:35:18 +02:00
}
2018-11-04 16:38:09 +01:00
public void readNBT(NBTTagCompound compound, SaveType type) {
if (type != SaveType.BLOCK)
super.readFromNBT(compound);
2018-10-13 20:35:18 +02:00
}
@Override
public final SPacketUpdateTileEntity getUpdatePacket() {
NBTTagCompound compound = new NBTTagCompound();
2018-11-04 16:38:09 +01:00
this.writeNBT(compound, SaveType.SYNC);
2018-10-13 20:35:18 +02:00
return new SPacketUpdateTileEntity(this.pos, 0, compound);
}
@Override
public final NBTTagCompound getUpdateTag() {
NBTTagCompound compound = new NBTTagCompound();
2018-11-04 16:38:09 +01:00
this.writeNBT(compound, SaveType.SYNC);
2018-10-13 20:35:18 +02:00
return compound;
}
@Override
public void handleUpdateTag(NBTTagCompound tag) {
2018-11-04 16:38:09 +01:00
this.readNBT(tag, SaveType.SYNC);
2018-10-13 20:35:18 +02:00
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
super.onDataPacket(net, packet);
2018-11-04 16:38:09 +01:00
this.readNBT(packet.getNbtCompound(), SaveType.SYNC);
2018-10-13 20:35:18 +02:00
}
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-11-04 16:38:09 +01:00
public void dropInventory() {
IItemHandler handler = this.getItemHandler(null);
if (handler != null) {
for (int i = 0; i < handler.getSlots(); i++) {
ItemStack stack = handler.getStackInSlot(i);
if (!stack.isEmpty()) {
EntityItem item = new EntityItem(this.world,
this.pos.getX() + 0.5, this.pos.getY() + 0.5, this.pos.getZ() + 0.5,
stack);
this.world.spawnEntity(item);
}
}
}
}
public ItemStack getDrop(IBlockState state, int fortune) {
Block block = state.getBlock();
ItemStack stack = new ItemStack(
block.getItemDropped(state, this.world.rand, fortune),
block.quantityDropped(state, fortune, this.world.rand),
block.damageDropped(state));
NBTTagCompound compound = new NBTTagCompound();
this.writeNBT(compound, SaveType.BLOCK);
if (!compound.isEmpty()) {
stack.setTagCompound(new NBTTagCompound());
stack.getTagCompound().setTag("data", compound);
}
return stack;
}
public void loadDataOnPlace(ItemStack stack) {
if (stack.hasTagCompound()) {
NBTTagCompound compound = stack.getTagCompound().getCompoundTag("data");
if (compound != null)
this.readNBT(compound, SaveType.BLOCK);
}
}
public enum SaveType {
TILE,
SYNC,
BLOCK
}
2018-10-13 20:35:18 +02:00
}