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

190 lines
6.2 KiB
Java
Raw Normal View History

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;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
import net.minecraft.entity.item.ItemEntity;
2018-11-04 16:38:09 +01:00
import net.minecraft.item.ItemStack;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.CompoundNBT;
2018-10-13 20:35:18 +02:00
import net.minecraft.network.NetworkManager;
2019-10-20 22:30:49 +02:00
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
2018-10-13 20:35:18 +02:00
import net.minecraft.server.management.PlayerChunkMapEntry;
import net.minecraft.tileentity.TileEntity;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.Direction;
2018-10-13 20:35:18 +02:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
2019-10-20 22:30:49 +02:00
import net.minecraft.world.ServerWorld;
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
public int redstonePower;
2018-11-13 00:36:47 +01:00
2018-10-13 20:35:18 +02:00
@Override
2019-10-20 22:30:49 +02:00
public boolean shouldRefresh(World world, BlockPos pos, BlockState oldState, BlockState newState) {
2018-10-13 20:35:18 +02:00
return oldState.getBlock() != newState.getBlock();
}
@Override
2019-10-20 22:30:49 +02:00
public CompoundNBT writeToNBT(CompoundNBT 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
2019-10-20 22:30:49 +02:00
public void readFromNBT(CompoundNBT compound) {
2018-11-04 16:38:09 +01:00
this.readNBT(compound, SaveType.TILE);
2018-10-13 20:35:18 +02:00
}
2019-10-20 22:30:49 +02:00
public void writeNBT(CompoundNBT 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);
compound.setInteger("redstone", this.redstonePower);
2018-11-13 00:36:47 +01:00
}
2018-10-13 20:35:18 +02:00
}
2019-10-20 22:30:49 +02:00
public void readNBT(CompoundNBT 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);
this.redstonePower = compound.getInteger("redstone");
2018-11-13 00:36:47 +01:00
}
2018-10-13 20:35:18 +02:00
}
2019-03-19 17:21:06 +01:00
public void onRedstonePowerChange(int newPower) {
this.redstonePower = newPower;
2019-02-22 19:06:47 +01:00
}
2018-10-13 20:35:18 +02:00
@Override
2019-10-20 22:30:49 +02:00
public final SUpdateTileEntityPacket getUpdatePacket() {
CompoundNBT compound = new CompoundNBT();
2018-11-04 16:38:09 +01:00
this.writeNBT(compound, SaveType.SYNC);
2019-10-20 22:30:49 +02:00
return new SUpdateTileEntityPacket(this.pos, 0, compound);
2018-10-13 20:35:18 +02:00
}
@Override
2019-10-20 22:30:49 +02:00
public final CompoundNBT getUpdateTag() {
CompoundNBT compound = new CompoundNBT();
2018-11-04 16:38:09 +01:00
this.writeNBT(compound, SaveType.SYNC);
2018-10-13 20:35:18 +02:00
return compound;
}
@Override
2019-10-20 22:30:49 +02:00
public void handleUpdateTag(CompoundNBT tag) {
2018-11-04 16:38:09 +01:00
this.readNBT(tag, SaveType.SYNC);
2018-10-13 20:35:18 +02:00
}
@Override
2019-10-20 22:30:49 +02:00
public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket packet) {
2018-10-13 20:35:18 +02:00
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() {
2019-10-20 22:30:49 +02:00
ServerWorld world = (ServerWorld) this.getWorld();
2018-10-13 20:35:18 +02:00
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
2019-10-20 22:30:49 +02:00
public IItemHandlerModifiable getItemHandler(Direction facing) {
2018-10-18 13:34:37 +02:00
return null;
}
2019-10-20 22:30:49 +02:00
public IAuraContainer getAuraContainer(Direction facing) {
2018-10-20 21:19:08 +02:00
return null;
}
2018-10-18 13:34:37 +02:00
@Override
2019-10-20 22:30:49 +02:00
public boolean hasCapability(Capability<?> capability, @Nullable Direction facing) {
2018-10-18 13:34:37 +02:00
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
2019-10-20 22:30:49 +02:00
public <T> T getCapability(Capability<T> capability, @Nullable Direction facing) {
2018-10-18 13:34:37 +02:00
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()) {
2019-10-20 22:30:49 +02:00
ItemEntity item = new ItemEntity(this.world,
2018-11-04 16:38:09 +01:00
this.pos.getX() + 0.5, this.pos.getY() + 0.5, this.pos.getZ() + 0.5,
stack);
this.world.spawnEntity(item);
}
}
}
}
2019-10-20 22:30:49 +02:00
public ItemStack getDrop(BlockState state, int fortune) {
2018-11-04 16:38:09 +01:00
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));
2019-10-20 22:30:49 +02:00
CompoundNBT compound = new CompoundNBT();
2018-11-04 16:38:09 +01:00
this.writeNBT(compound, SaveType.BLOCK);
if (!compound.isEmpty()) {
2019-10-20 22:30:49 +02:00
stack.setTagCompound(new CompoundNBT());
2018-11-04 16:38:09 +01:00
stack.getTagCompound().setTag("data", compound);
}
return stack;
}
public void loadDataOnPlace(ItemStack stack) {
if (stack.hasTagCompound()) {
2019-10-20 22:30:49 +02:00
CompoundNBT compound = stack.getTagCompound().getCompoundTag("data");
2018-11-04 16:38:09 +01:00
if (compound != null)
this.readNBT(compound, SaveType.BLOCK);
}
}
2019-02-16 01:43:40 +01:00
public boolean canGenerateRightNow(int range, int toAdd) {
if (this.wantsLimitRemover()) {
2019-10-20 22:30:49 +02:00
BlockState below = this.world.getBlockState(this.pos.down());
if (below.getBlock() == ModBlocks.GENERATOR_LIMIT_REMOVER)
return true;
}
2019-02-16 01:43:40 +01:00
int aura = IAuraChunk.getAuraInArea(this.world, this.pos, range);
return aura + toAdd <= IAuraChunk.DEFAULT_AURA * 2;
}
public boolean wantsLimitRemover() {
return false;
}
2018-11-04 16:38:09 +01:00
public enum SaveType {
TILE,
SYNC,
BLOCK
}
2018-10-13 20:35:18 +02:00
}