ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBase.java

301 lines
10 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityBase.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2014-12-12 15:40:01 +01:00
import de.ellpeck.actuallyadditions.mod.util.VanillaPacketDispatcher;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2024-03-02 21:23:08 +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.world.entity.player.Player;
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-04 20:21:48 +01:00
import net.neoforged.neoforge.energy.IEnergyStorage;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
import net.neoforged.neoforge.items.IItemHandler;
import javax.annotation.Nullable;
2024-03-02 21:23:08 +01:00
public abstract class TileEntityBase extends BlockEntity {
2015-06-15 22:06:07 +02:00
2016-06-01 00:39:35 +02:00
public boolean isRedstonePowered;
public boolean isPulseMode;
2016-11-27 23:04:26 +01:00
public boolean stopFromDropping;
2016-12-18 17:50:31 +01:00
protected int ticksElapsed;
2024-03-02 21:23:08 +01:00
protected BlockEntity[] tilesAround = new BlockEntity[6];
protected boolean hasSavedDataOnChangeOrWorldStart;
2024-03-02 21:23:08 +01:00
public TileEntityBase(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
}
@Override
2024-03-02 21:23:08 +01:00
protected void saveAdditional(CompoundTag compound) {
super.saveAdditional(compound);
this.writeSyncableNBT(compound, NBTType.SAVE_TILE);
}
// TODO: [port] remove if the above is correct
// @Override
// public final CompoundNBT writeToNBT(CompoundNBT compound) {
// this.writeSyncableNBT(compound, NBTType.SAVE_TILE);
// return compound;
// }
2015-10-03 10:16:18 +02:00
@Override
2024-03-02 21:23:08 +01:00
public void load(CompoundTag compound) {
this.readSyncableNBT(compound, NBTType.SAVE_TILE);
2015-10-03 10:16:18 +02:00
}
2024-03-02 21:23:08 +01:00
// TODO: [port] remove if the above is correct
// @Override
// public final void readFromNBT(CompoundNBT compound) {
// this.readSyncableNBT(compound, NBTType.SAVE_TILE);
// }
@Nullable
2015-10-03 10:16:18 +02:00
@Override
2024-03-02 21:23:08 +01:00
public ClientboundBlockEntityDataPacket getUpdatePacket() {
CompoundTag compound = new CompoundTag();
this.writeSyncableNBT(compound, NBTType.SYNC);
2024-03-02 21:23:08 +01:00
return ClientboundBlockEntityDataPacket.create(this);
2015-10-03 10:16:18 +02:00
}
@Override
2024-03-02 21:23:08 +01:00
public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt) {
2024-03-13 00:30:34 +01:00
if (pkt.getTag() != null) //TODO: pkt.getTag() is nullable. Hopping Item Interface will throw in the log when placed because of this
this.readSyncableNBT(pkt.getTag(), NBTType.SYNC);
2016-02-01 17:49:55 +01:00
}
@Override
2024-03-02 21:23:08 +01:00
public final CompoundTag getUpdateTag() {
CompoundTag compound = new CompoundTag();
this.writeSyncableNBT(compound, NBTType.SYNC);
return compound;
2016-02-01 17:49:55 +01:00
}
@Override
2024-03-02 21:23:08 +01:00
public void handleUpdateTag(CompoundTag tag) {
this.readSyncableNBT(tag, NBTType.SYNC);
}
2019-02-27 19:53:05 +01:00
2019-05-02 09:10:29 +02:00
public final void sendUpdate() {
if (this.level != null && !this.level.isClientSide) {
2021-02-26 22:15:48 +01:00
VanillaPacketDispatcher.dispatchTEToNearbyPlayers(this);
}
2019-02-27 19:53:05 +01:00
/*
if(this.world != null && !this.world.isRemote){
2021-02-26 22:15:48 +01:00
CompoundNBT compound = new CompoundNBT();
this.writeSyncableNBT(compound, NBTType.SYNC);
2019-05-02 10:43:27 +02:00
2021-02-26 22:15:48 +01:00
CompoundNBT data = new CompoundNBT();
data.setTag("Data", compound);
data.setInteger("X", this.pos.getX());
data.setInteger("Y", this.pos.getY());
data.setInteger("Z", this.pos.getZ());
PacketHandler.theNetwork.sendToAllTracking(new PacketServerToClient(data, PacketHandler.TILE_ENTITY_HANDLER), new TargetPoint(this.world.provider.getDimension(), this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), 0));
}*/
}
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
2021-02-26 22:15:48 +01:00
if (type != NBTType.SAVE_BLOCK) {
2024-03-02 21:23:08 +01:00
super.saveAdditional(compound);
2021-02-26 22:15:48 +01:00
}
2019-05-02 09:10:29 +02:00
if (type == NBTType.SAVE_TILE) {
compound.putBoolean("Redstone", this.isRedstonePowered);
compound.putInt("TicksElapsed", this.ticksElapsed);
compound.putBoolean("StopDrop", this.stopFromDropping);
2021-02-26 22:15:48 +01:00
} else if (type == NBTType.SYNC && this.stopFromDropping) {
compound.putBoolean("StopDrop", this.stopFromDropping);
2021-02-26 22:15:48 +01:00
}
2019-05-02 09:10:29 +02:00
if (this.isRedstoneToggle() && (type != NBTType.SAVE_BLOCK || this.isPulseMode)) {
compound.putBoolean("IsPulseMode", this.isPulseMode);
}
2015-12-01 19:48:09 +01:00
}
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag compound, NBTType type) {
2021-02-26 22:15:48 +01:00
if (type != NBTType.SAVE_BLOCK) {
2024-03-02 21:23:08 +01:00
super.load(compound); // FIXME: [port] flag as possible crash source
2021-02-26 22:15:48 +01:00
}
2019-05-02 09:10:29 +02:00
if (type == NBTType.SAVE_TILE) {
this.isRedstonePowered = compound.getBoolean("Redstone");
this.ticksElapsed = compound.getInt("TicksElapsed");
2016-11-27 23:04:26 +01:00
this.stopFromDropping = compound.getBoolean("StopDrop");
2021-02-26 22:15:48 +01:00
} else if (type == NBTType.SYNC) {
this.stopFromDropping = compound.getBoolean("StopDrop");
}
if (this.isRedstoneToggle() && compound.contains("IsPulseMode")) {
this.isPulseMode = compound.getBoolean("IsPulseMode");
}
2015-10-21 00:22:50 +02:00
}
2015-10-18 15:31:01 +02:00
// TODO: [port] eval if still required in some way
// @Override
// public boolean shouldRefresh(World world, BlockPos pos, BlockState oldState, BlockState newState) {
// return !oldState.getBlock().isAssociatedBlock(newState.getBlock());
// }
2022-06-05 22:36:27 +02:00
@Deprecated
2019-05-02 09:10:29 +02:00
public String getNameForTranslation() {
return "removeme";// "container." + ActuallyAdditions.MODID + "." + this.name + ".name";
2016-11-20 13:46:35 +01:00
}
// @Override
// public ITextComponent getDisplayName() {
// return new TranslationTextComponent(this.getNameForTranslation());
// }
2019-05-02 09:10:29 +02:00
public int getComparatorStrength() {
2016-11-28 12:51:45 +01:00
return 0;
}
2019-02-27 19:53:05 +01:00
private boolean shareEnergy = this instanceof ISharingEnergyProvider;
private boolean shareFluid = this instanceof ISharingFluidHandler;
2016-11-28 12:51:45 +01:00
2024-03-02 21:23:08 +01:00
protected void clientTick() {
2016-02-01 17:49:55 +01:00
this.ticksElapsed++;
2024-03-02 21:23:08 +01:00
}
2024-03-02 21:23:08 +01:00
protected void serverTick() {
this.ticksElapsed++;
if (this.shareEnergy) {
ISharingEnergyProvider provider = (ISharingEnergyProvider) this;
if (provider.doesShareEnergy()) {
int total = provider.getEnergyToSplitShare();
if (total > 0) {
Direction[] sides = provider.getEnergyShareSides();
2024-03-02 21:23:08 +01:00
int amount = total / sides.length;
if (amount <= 0) {
amount = total;
}
for (Direction side : sides) {
BlockEntity tile = this.tilesAround[side.ordinal()];
if (tile != null && provider.canShareTo(tile)) {
2024-03-04 20:21:48 +01:00
WorldUtil.doEnergyInteraction(this.level, this.getBlockPos(), tile.getBlockPos(), side, amount);
}
}
}
}
2024-03-02 21:23:08 +01:00
}
2024-03-02 21:23:08 +01:00
if (this.shareFluid) {
ISharingFluidHandler handler = (ISharingFluidHandler) this;
if (handler.doesShareFluid()) {
int total = handler.getMaxFluidAmountToSplitShare();
if (total > 0) {
Direction[] sides = handler.getFluidShareSides();
2024-03-02 21:23:08 +01:00
int amount = total / sides.length;
if (amount <= 0) {
amount = total;
}
2024-03-02 21:23:08 +01:00
for (Direction side : sides) {
BlockEntity tile = this.tilesAround[side.ordinal()];
if (tile != null) {
2024-03-04 20:21:48 +01:00
WorldUtil.doFluidInteraction(this.level, this.getBlockPos(), tile.getBlockPos(), side, amount);
}
}
}
}
2024-03-02 21:23:08 +01:00
}
2024-03-02 21:23:08 +01:00
if (!this.hasSavedDataOnChangeOrWorldStart) {
if (this.shouldSaveDataOnChangeOrWorldStart()) {
this.saveDataOnChangeOrWorldStart();
}
2024-03-02 21:23:08 +01:00
this.hasSavedDataOnChangeOrWorldStart = true;
}
}
2019-05-02 09:10:29 +02:00
public void saveDataOnChangeOrWorldStart() {
for (Direction side : Direction.values()) {
BlockPos pos = this.worldPosition.relative(side);
if (this.level.hasChunkAt(pos)) {
this.tilesAround[side.ordinal()] = this.level.getBlockEntity(pos);
}
}
2016-02-01 17:49:55 +01:00
}
2019-05-02 09:10:29 +02:00
public boolean shouldSaveDataOnChangeOrWorldStart() {
return this instanceof ISharingEnergyProvider || this instanceof ISharingFluidHandler;
}
2019-05-02 09:10:29 +02:00
public void setRedstonePowered(boolean powered) {
2015-12-19 10:30:39 +01:00
this.isRedstonePowered = powered;
this.setChanged();
2015-12-19 10:30:39 +01:00
}
2024-03-02 21:23:08 +01:00
public boolean canPlayerUse(Player player) {
return player.distanceToSqr(this.getBlockPos().getX() + 0.5D, this.getBlockPos().getY() + 0.5D, this.getBlockPos().getZ() + 0.5D) <= 64 && !this.isRemoved() && this.level.getBlockEntity(this.worldPosition) == this;
}
2019-05-02 09:10:29 +02:00
protected boolean sendUpdateWithInterval() {
if (this.ticksElapsed % 5 == 0) { //TODO was a config TILE_ENTITY_UPDATE_INTERVAL
this.sendUpdate();
2015-11-19 22:13:52 +01:00
return true;
2019-05-02 09:10:29 +02:00
} else {
2015-11-19 22:13:52 +01:00
return false;
}
}
2024-03-04 20:21:48 +01:00
public IFluidHandler getFluidHandler(Direction facing) {
return null;
2016-06-05 02:16:52 +02:00
}
2024-03-04 20:21:48 +01:00
public IEnergyStorage getEnergyStorage(Direction facing) {
return null;
2016-11-26 08:58:42 +01:00
}
2024-03-04 20:21:48 +01:00
public IItemHandler getItemHandler(Direction facing) {
return null;
}
2019-05-02 09:10:29 +02:00
public boolean isRedstoneToggle() {
return false;
}
2019-05-02 09:10:29 +02:00
public void activateOnPulse() {
}
2019-05-02 09:10:29 +02:00
public boolean respondsToPulses() {
return this.isRedstoneToggle() && this.isPulseMode;
}
2018-07-07 12:07:22 +02:00
public enum NBTType {
/**
* Use when normal writeToNBT/readToNBT is expected.
*/
SAVE_TILE,
2018-07-07 12:07:22 +02:00
/**
* Use when data needs to be sent to the client.
*/
SYNC,
2018-07-07 12:07:22 +02:00
/**
* Wat
*/
SAVE_BLOCK
}
2021-02-26 22:15:48 +01:00
}