2018-10-13 20:35:18 +02:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
2018-11-12 22:04:40 +01:00
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
2019-02-16 01:43:40 +01:00
|
|
|
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
|
2018-11-11 13:26:19 +01:00
|
|
|
import de.ellpeck.naturesaura.api.aura.container.IAuraContainer;
|
2019-02-16 01:43:40 +01:00
|
|
|
import de.ellpeck.naturesaura.blocks.ModBlocks;
|
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 {
|
2018-11-13 00:36:47 +01:00
|
|
|
|
2018-11-23 17:11:35 +01:00
|
|
|
public int redstonePower;
|
2018-11-13 00:36:47 +01:00
|
|
|
|
2018-10-13 20:35:18 +02:00
|
|
|
@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) {
|
2018-11-13 00:36:47 +01:00
|
|
|
if (type != SaveType.BLOCK) {
|
2018-11-04 16:38:09 +01:00
|
|
|
super.writeToNBT(compound);
|
2018-11-23 17:11:35 +01:00
|
|
|
compound.setInteger("redstone", this.redstonePower);
|
2018-11-13 00:36:47 +01:00
|
|
|
}
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|
|
|
|
|
2018-11-04 16:38:09 +01:00
|
|
|
public void readNBT(NBTTagCompound compound, SaveType type) {
|
2018-11-13 00:36:47 +01:00
|
|
|
if (type != SaveType.BLOCK) {
|
2018-11-04 16:38:09 +01:00
|
|
|
super.readFromNBT(compound);
|
2018-11-23 17:11:35 +01:00
|
|
|
this.redstonePower = compound.getInteger("redstone");
|
2018-11-13 00:36:47 +01:00
|
|
|
}
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|
|
|
|
|
2019-02-16 01:43:40 +01:00
|
|
|
public void onRedstonePowerChange() {
|
2019-01-27 13:57:34 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-22 19:06:47 +01:00
|
|
|
public void onRedstonePulse(){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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-11-12 22:04:40 +01:00
|
|
|
} else if (capability == NaturesAuraAPI.capAuraContainer) {
|
2018-10-20 21:19:08 +02:00
|
|
|
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-11-12 22:04:40 +01:00
|
|
|
} else if (capability == NaturesAuraAPI.capAuraContainer) {
|
2018-10-20 21:19:08 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-16 01:43:40 +01:00
|
|
|
public boolean canGenerateRightNow(int range, int toAdd) {
|
|
|
|
IBlockState below = this.world.getBlockState(this.pos.down());
|
|
|
|
if (below.getBlock() == ModBlocks.GENERATOR_LIMIT_REMOVER)
|
|
|
|
return true;
|
|
|
|
int aura = IAuraChunk.getAuraInArea(this.world, this.pos, range);
|
|
|
|
return aura + toAdd <= IAuraChunk.DEFAULT_AURA * 2;
|
|
|
|
}
|
|
|
|
|
2018-11-04 16:38:09 +01:00
|
|
|
public enum SaveType {
|
|
|
|
TILE,
|
|
|
|
SYNC,
|
|
|
|
BLOCK
|
|
|
|
}
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|