2018-10-13 20:35:18 +02:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
2019-02-16 01:43:40 +01:00
|
|
|
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
|
|
|
|
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;
|
2024-03-10 11:29:12 +01:00
|
|
|
import net.neoforged.neoforge.capabilities.Capabilities;
|
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;
|
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
|
|
|
|
2018-11-04 16:38:09 +01:00
|
|
|
public void dropInventory() {
|
2024-03-10 11:29:12 +01:00
|
|
|
var handler = this.level.getCapability(Capabilities.ItemHandler.BLOCK, this.worldPosition, this.getBlockState(), this, null);
|
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()) {
|
2023-02-15 23:52:52 +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");
|
2023-02-15 23:52:52 +01:00
|
|
|
if (compound != null)
|
|
|
|
this.readNBT(compound, SaveType.BLOCK);
|
2018-11-04 16:38:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-16 19:03:26 +01:00
|
|
|
public boolean canUseRightNow(int toUse) {
|
|
|
|
if (this.allowsLowerLimiter()) {
|
|
|
|
for (var dir : Direction.values()) {
|
|
|
|
var offset = this.worldPosition.relative(dir);
|
|
|
|
if (this.level.getBlockState(offset).getBlock() == ModBlocks.LOWER_LIMITER) {
|
|
|
|
var aura = IAuraChunk.getAuraInArea(this.level, this.worldPosition, 35);
|
|
|
|
return aura - toUse > 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-02-16 19:03:26 +01:00
|
|
|
public boolean allowsLowerLimiter() {
|
|
|
|
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
|
|
|
}
|
2024-03-10 11:29:12 +01:00
|
|
|
|
2018-10-13 20:35:18 +02:00
|
|
|
}
|