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;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.entity.item.ItemEntity;
|
2020-01-22 23:21:52 +01:00
|
|
|
import net.minecraft.entity.player.ServerPlayerEntity;
|
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.tileentity.TileEntity;
|
2020-01-21 21:04:44 +01:00
|
|
|
import net.minecraft.tileentity.TileEntityType;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.util.Direction;
|
2020-01-22 23:21:52 +01:00
|
|
|
import net.minecraft.util.math.ChunkPos;
|
|
|
|
import net.minecraft.world.server.ServerWorld;
|
2018-10-18 13:34:37 +02:00
|
|
|
import net.minecraftforge.common.capabilities.Capability;
|
2020-01-21 21:04:44 +01:00
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2018-10-18 13:34:37 +02:00
|
|
|
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;
|
2020-01-22 23:21:52 +01:00
|
|
|
import java.util.stream.Stream;
|
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
|
|
|
|
2020-01-21 21:04:44 +01:00
|
|
|
public TileEntityImpl(TileEntityType<?> tileEntityTypeIn) {
|
|
|
|
super(tileEntityTypeIn);
|
|
|
|
}
|
|
|
|
|
2018-10-13 20:35:18 +02:00
|
|
|
@Override
|
2020-01-21 21:04:44 +01:00
|
|
|
public CompoundNBT write(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
|
2020-09-22 03:17:02 +02:00
|
|
|
public void read(BlockState state, 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) {
|
2020-01-21 21:04:44 +01:00
|
|
|
super.write(compound);
|
|
|
|
compound.putInt("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) {
|
2020-09-22 03:17:02 +02:00
|
|
|
super.read(this.getBlockState(), compound);
|
2020-01-21 21:04:44 +01:00
|
|
|
this.redstonePower = compound.getInt("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
|
2020-09-22 03:17:02 +02:00
|
|
|
public void handleUpdateTag(BlockState state, CompoundNBT tag) {
|
2018-11-04 16:38:09 +01:00
|
|
|
this.readNBT(tag, SaveType.SYNC);
|
2020-09-22 03:17:02 +02:00
|
|
|
|
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() {
|
2020-01-22 23:21:52 +01:00
|
|
|
ServerWorld world = (ServerWorld) this.getWorld();
|
|
|
|
Stream<ServerPlayerEntity> entities = world.getChunkProvider().chunkManager.getTrackingPlayers(new ChunkPos(this.getPos()), false);
|
2020-01-21 21:04:44 +01:00
|
|
|
SUpdateTileEntityPacket packet = this.getUpdatePacket();
|
2020-01-22 23:21:52 +01:00
|
|
|
entities.forEach(e -> e.connection.sendPacket(packet));
|
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
|
|
|
@Nullable
|
|
|
|
@Override
|
2020-01-21 21:04:44 +01:00
|
|
|
public <T> LazyOptional<T> getCapability(Capability<T> capability, @Nullable Direction facing) {
|
2018-10-18 13:34:37 +02:00
|
|
|
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
|
2020-01-21 21:04:44 +01:00
|
|
|
IItemHandler handler = this.getItemHandler(facing);
|
|
|
|
return handler == null ? LazyOptional.empty() : LazyOptional.of(() -> (T) handler);
|
2018-11-12 22:04:40 +01:00
|
|
|
} else if (capability == NaturesAuraAPI.capAuraContainer) {
|
2020-01-21 21:04:44 +01:00
|
|
|
IAuraContainer container = this.getAuraContainer(facing);
|
|
|
|
return container == null ? LazyOptional.empty() : LazyOptional.of(() -> (T) container);
|
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);
|
2020-01-21 21:04:44 +01:00
|
|
|
this.world.addEntity(item);
|
2018-11-04 16:38:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 16:05:52 +01:00
|
|
|
public void modifyDrop(ItemStack regularItem) {
|
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()) {
|
2020-01-23 16:05:52 +01:00
|
|
|
if (!regularItem.hasTag())
|
|
|
|
regularItem.setTag(new CompoundNBT());
|
|
|
|
regularItem.getTag().put("data", compound);
|
2018-11-04 16:38:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadDataOnPlace(ItemStack stack) {
|
2020-01-21 21:04:44 +01:00
|
|
|
if (stack.hasTag()) {
|
|
|
|
CompoundNBT compound = stack.getTag().getCompound("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) {
|
2019-02-25 13:36:05 +01:00
|
|
|
if (this.wantsLimitRemover()) {
|
2019-10-20 22:30:49 +02:00
|
|
|
BlockState below = this.world.getBlockState(this.pos.down());
|
2019-02-25 13:36:05 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-02-25 13:36:05 +01:00
|
|
|
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
|
|
|
}
|