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;
|
2021-12-04 15:40:09 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.core.Direction;
|
|
|
|
import net.minecraft.nbt.CompoundTag;
|
|
|
|
import net.minecraft.network.Connection;
|
|
|
|
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
|
|
|
import net.minecraft.server.level.ServerLevel;
|
|
|
|
import net.minecraft.world.entity.item.ItemEntity;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.level.ChunkPos;
|
|
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.world.level.block.entity.BlockEntityType;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
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;
|
2018-10-13 20:35:18 +02:00
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
public class BlockEntityImpl extends BlockEntity {
|
2018-11-13 00:36:47 +01:00
|
|
|
|
2018-11-23 17:11:35 +01:00
|
|
|
public int redstonePower;
|
2020-10-19 21:26:32 +02:00
|
|
|
private LazyOptional<IItemHandler> itemHandler;
|
|
|
|
private LazyOptional<IAuraContainer> auraContainer;
|
2018-11-13 00:36:47 +01:00
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
public BlockEntityImpl(BlockEntityType<?> type, BlockPos pos, BlockState state) {
|
|
|
|
super(type, pos, state);
|
2020-01-21 21:04:44 +01:00
|
|
|
}
|
|
|
|
|
2018-10-13 20:35:18 +02:00
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void saveAdditional(CompoundTag compound) {
|
2018-11-04 16:38:09 +01:00
|
|
|
this.writeNBT(compound, SaveType.TILE);
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void load(CompoundTag compound) {
|
2018-11-04 16:38:09 +01:00
|
|
|
this.readNBT(compound, SaveType.TILE);
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
public void writeNBT(CompoundTag compound, SaveType type) {
|
2018-11-13 00:36:47 +01:00
|
|
|
if (type != SaveType.BLOCK) {
|
2021-12-04 15:40:09 +01:00
|
|
|
super.saveAdditional(compound);
|
2020-01-21 21:04:44 +01:00
|
|
|
compound.putInt("redstone", this.redstonePower);
|
2018-11-13 00:36:47 +01:00
|
|
|
}
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
public void readNBT(CompoundTag compound, SaveType type) {
|
2018-11-13 00:36:47 +01:00
|
|
|
if (type != SaveType.BLOCK) {
|
2021-12-04 15:40:09 +01:00
|
|
|
super.load(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
|
2021-12-04 15:40:09 +01:00
|
|
|
public final ClientboundBlockEntityDataPacket getUpdatePacket() {
|
|
|
|
return ClientboundBlockEntityDataPacket.create(this, e -> {
|
2021-12-15 16:30:22 +01:00
|
|
|
var compound = new CompoundTag();
|
2021-12-04 15:40:09 +01:00
|
|
|
this.writeNBT(compound, SaveType.SYNC);
|
|
|
|
return compound;
|
|
|
|
});
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public final CompoundTag getUpdateTag() {
|
2021-12-15 16:30:22 +01:00
|
|
|
var compound = new CompoundTag();
|
2018-11-04 16:38:09 +01:00
|
|
|
this.writeNBT(compound, SaveType.SYNC);
|
2018-10-13 20:35:18 +02:00
|
|
|
return compound;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void handleUpdateTag(CompoundTag tag) {
|
2018-11-04 16:38:09 +01:00
|
|
|
this.readNBT(tag, SaveType.SYNC);
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt) {
|
|
|
|
super.onDataPacket(net, pkt);
|
|
|
|
this.readNBT(pkt.getTag(), SaveType.SYNC);
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void sendToClients() {
|
2021-12-04 15:40:09 +01:00
|
|
|
var world = (ServerLevel) this.getLevel();
|
|
|
|
var entities = world.getChunkSource().chunkMap.getPlayers(new ChunkPos(this.getBlockPos()), false);
|
2021-12-15 16:30:22 +01:00
|
|
|
var packet = this.getUpdatePacket();
|
2021-12-04 15:40:09 +01:00
|
|
|
for (var e : entities)
|
|
|
|
e.connection.send(packet);
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|
2018-10-18 13:34:37 +02:00
|
|
|
|
2020-10-19 21:26:32 +02:00
|
|
|
public IItemHandlerModifiable getItemHandler() {
|
2018-10-18 13:34:37 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-19 21:26:32 +02:00
|
|
|
public IAuraContainer getAuraContainer() {
|
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-10-19 21:26:32 +02:00
|
|
|
if (this.itemHandler == null) {
|
|
|
|
IItemHandler handler = this.getItemHandler();
|
|
|
|
this.itemHandler = handler == null ? LazyOptional.empty() : LazyOptional.of(() -> handler);
|
|
|
|
}
|
|
|
|
return this.itemHandler.cast();
|
2021-12-23 13:27:52 +01:00
|
|
|
} else if (capability == NaturesAuraAPI.CAP_AURA_CONTAINER) {
|
2020-10-19 21:26:32 +02:00
|
|
|
if (this.auraContainer == null) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var container = this.getAuraContainer();
|
2020-10-19 21:26:32 +02:00
|
|
|
this.auraContainer = container == null ? LazyOptional.empty() : LazyOptional.of(() -> container);
|
|
|
|
}
|
|
|
|
return this.auraContainer.cast();
|
2018-10-18 13:34:37 +02:00
|
|
|
} else {
|
|
|
|
return super.getCapability(capability, facing);
|
|
|
|
}
|
|
|
|
}
|
2018-11-04 16:38:09 +01:00
|
|
|
|
2020-10-19 21:26:32 +02:00
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void setRemoved() {
|
|
|
|
super.setRemoved();
|
2020-10-19 21:26:32 +02:00
|
|
|
if (this.itemHandler != null)
|
|
|
|
this.itemHandler.invalidate();
|
|
|
|
if (this.auraContainer != null)
|
|
|
|
this.auraContainer.invalidate();
|
|
|
|
}
|
|
|
|
|
2018-11-04 16:38:09 +01:00
|
|
|
public void dropInventory() {
|
2020-10-19 21:26:32 +02:00
|
|
|
IItemHandler handler = this.getItemHandler();
|
2018-11-04 16:38:09 +01:00
|
|
|
if (handler != null) {
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var i = 0; i < handler.getSlots(); i++) {
|
|
|
|
var stack = handler.getStackInSlot(i);
|
2018-11-04 16:38:09 +01:00
|
|
|
if (!stack.isEmpty()) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var item = new ItemEntity(this.level, this.worldPosition.getX() + 0.5, this.worldPosition.getY() + 0.5, this.worldPosition.getZ() + 0.5, stack);
|
2021-12-04 15:40:09 +01:00
|
|
|
this.level.addFreshEntity(item);
|
2018-11-04 16:38:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 16:05:52 +01:00
|
|
|
public void modifyDrop(ItemStack regularItem) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var compound = new CompoundTag();
|
2018-11-04 16:38:09 +01:00
|
|
|
this.writeNBT(compound, SaveType.BLOCK);
|
|
|
|
if (!compound.isEmpty()) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!regularItem.hasTag()) regularItem.setTag(new CompoundTag());
|
2020-01-23 16:05:52 +01:00
|
|
|
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()) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var compound = stack.getTag().getCompound("data");
|
2021-12-04 15:40:09 +01:00
|
|
|
if (compound != null) this.readNBT(compound, SaveType.BLOCK);
|
2018-11-04 16:38:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 15:44:31 +02:00
|
|
|
public boolean canGenerateRightNow(int toAdd) {
|
2019-02-25 13:36:05 +01:00
|
|
|
if (this.wantsLimitRemover()) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var below = this.level.getBlockState(this.worldPosition.below());
|
2019-02-25 13:36:05 +01:00
|
|
|
if (below.getBlock() == ModBlocks.GENERATOR_LIMIT_REMOVER)
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-15 16:30:22 +01:00
|
|
|
var aura = IAuraChunk.getAuraInArea(this.level, this.worldPosition, 35);
|
2019-02-16 01:43:40 +01:00
|
|
|
return aura + toAdd <= IAuraChunk.DEFAULT_AURA * 2;
|
|
|
|
}
|
|
|
|
|
2019-02-25 13:36:05 +01:00
|
|
|
public boolean wantsLimitRemover() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-30 15:44:31 +02:00
|
|
|
public void generateAura(int amount) {
|
|
|
|
while (amount > 0) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var spot = IAuraChunk.getLowestSpot(this.level, this.worldPosition, 35, this.worldPosition);
|
2021-12-04 15:40:09 +01:00
|
|
|
amount -= IAuraChunk.getAuraChunk(this.level, spot).storeAura(spot, amount);
|
2021-03-30 15:44:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-04 16:38:09 +01:00
|
|
|
public enum SaveType {
|
2021-12-04 15:40:09 +01:00
|
|
|
TILE, SYNC, BLOCK
|
2018-11-04 16:38:09 +01:00
|
|
|
}
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|