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

378 lines
14 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
2018-05-10 11:38:58 +02:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
import de.ellpeck.actuallyadditions.mod.util.VanillaPacketDispatcher;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
2015-06-15 22:06:07 +02:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
2016-03-18 23:47:22 +01:00
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
2014-12-12 15:40:01 +01:00
import net.minecraft.tileentity.TileEntity;
2016-06-05 02:16:52 +02:00
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
2018-08-04 04:22:20 +02:00
import net.minecraft.util.ResourceLocation;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
2016-06-05 02:16:52 +02:00
import net.minecraftforge.common.capabilities.Capability;
2016-11-26 08:58:42 +01:00
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
2016-06-05 02:16:52 +02:00
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
2014-12-12 15:40:01 +01:00
2019-05-02 09:10:29 +02:00
public abstract class TileEntityBase extends TileEntity implements ITickable {
2015-06-15 22:06:07 +02:00
2016-07-07 17:59:45 +02:00
public final String name;
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;
protected TileEntity[] tilesAround = new TileEntity[6];
protected boolean hasSavedDataOnChangeOrWorldStart;
2019-05-02 09:10:29 +02:00
public TileEntityBase(String name) {
this.name = name;
}
2019-05-02 09:10:29 +02:00
public static void init() {
2018-05-10 11:38:58 +02:00
ActuallyAdditions.LOGGER.info("Registering TileEntities...");
2016-10-30 19:30:19 +01:00
register(TileEntityCompost.class);
register(TileEntityFeeder.class);
register(TileEntityGiantChest.class);
register(TileEntityGiantChestMedium.class);
register(TileEntityGiantChestLarge.class);
register(TileEntityGrinder.class);
register(TileEntityFurnaceDouble.class);
register(TileEntityInputter.class);
register(TileEntityFishingNet.class);
register(TileEntityFurnaceSolar.class);
register(TileEntityHeatCollector.class);
register(TileEntityItemRepairer.class);
register(TileEntityBreaker.class);
register(TileEntityDropper.class);
register(TileEntityInputterAdvanced.class);
register(TileEntityPlacer.class);
register(TileEntityGrinderDouble.class);
register(TileEntityCanolaPress.class);
register(TileEntityFermentingBarrel.class);
register(TileEntityOilGenerator.class);
register(TileEntityCoalGenerator.class);
register(TileEntityPhantomItemface.class);
register(TileEntityPhantomLiquiface.class);
register(TileEntityPhantomEnergyface.class);
register(TileEntityPlayerInterface.class);
register(TileEntityPhantomPlacer.class);
register(TileEntityPhantomBreaker.class);
register(TileEntityFluidCollector.class);
register(TileEntityFluidPlacer.class);
register(TileEntityLavaFactoryController.class);
register(TileEntityCoffeeMachine.class);
register(TileEntityPhantomBooster.class);
register(TileEntityEnergizer.class);
register(TileEntityEnervator.class);
register(TileEntityXPSolidifier.class);
register(TileEntitySmileyCloud.class);
register(TileEntityLeafGenerator.class);
register(TileEntityDirectionalBreaker.class);
register(TileEntityRangedCollector.class);
register(TileEntityAtomicReconstructor.class);
register(TileEntityMiner.class);
register(TileEntityFireworkBox.class);
register(TileEntityPhantomRedstoneface.class);
register(TileEntityLaserRelayItem.class);
register(TileEntityLaserRelayEnergy.class);
2016-07-14 16:44:01 +02:00
register(TileEntityLaserRelayEnergyAdvanced.class);
register(TileEntityLaserRelayEnergyExtreme.class);
2016-10-30 19:30:19 +01:00
register(TileEntityLaserRelayItemWhitelist.class);
register(TileEntityItemViewer.class);
register(TileEntityDisplayStand.class);
register(TileEntityShockSuppressor.class);
register(TileEntityEmpowerer.class);
2016-09-01 20:41:50 +02:00
register(TileEntityLaserRelayFluids.class);
2016-09-14 21:42:03 +02:00
register(TileEntityBioReactor.class);
2016-10-31 18:03:18 +01:00
register(TileEntityFarmer.class);
register(TileEntityItemViewerHopping.class);
register(TileEntityBatteryBox.class);
2014-12-18 19:24:06 +01:00
}
2019-05-02 09:10:29 +02:00
private static void register(Class<? extends TileEntityBase> tileClass) {
try {
//This is hacky and dirty but it works so whatever
2018-08-04 04:22:20 +02:00
ResourceLocation name = new ResourceLocation(ActuallyAdditions.MODID, tileClass.newInstance().name);
2016-10-30 19:30:19 +01:00
GameRegistry.registerTileEntity(tileClass, name);
2019-05-02 09:10:29 +02:00
} catch (Exception e) {
2018-05-10 11:38:58 +02:00
ActuallyAdditions.LOGGER.fatal("Registering a TileEntity failed!", e);
}
}
@Override
2019-05-02 09:10:29 +02:00
public final NBTTagCompound writeToNBT(NBTTagCompound compound) {
this.writeSyncableNBT(compound, NBTType.SAVE_TILE);
return compound;
}
2015-10-03 10:16:18 +02:00
@Override
2019-05-02 09:10:29 +02:00
public final void readFromNBT(NBTTagCompound compound) {
this.readSyncableNBT(compound, NBTType.SAVE_TILE);
2015-10-03 10:16:18 +02:00
}
@Override
2019-05-02 09:10:29 +02:00
public final SPacketUpdateTileEntity getUpdatePacket() {
NBTTagCompound compound = new NBTTagCompound();
this.writeSyncableNBT(compound, NBTType.SYNC);
return new SPacketUpdateTileEntity(this.pos, -1, compound);
2015-10-03 10:16:18 +02:00
}
@Override
2019-05-02 09:10:29 +02:00
public final void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
this.readSyncableNBT(pkt.getNbtCompound(), NBTType.SYNC);
2016-02-01 17:49:55 +01:00
}
@Override
2019-05-02 09:10:29 +02:00
public final NBTTagCompound getUpdateTag() {
NBTTagCompound compound = new NBTTagCompound();
this.writeSyncableNBT(compound, NBTType.SYNC);
return compound;
2016-02-01 17:49:55 +01:00
}
@Override
2019-05-02 09:10:29 +02:00
public final void handleUpdateTag(NBTTagCompound compound) {
this.readSyncableNBT(compound, NBTType.SYNC);
}
2019-02-27 19:53:05 +01:00
2019-05-02 09:10:29 +02:00
public final void sendUpdate() {
if (this.world != null && !this.world.isRemote) VanillaPacketDispatcher.dispatchTEToNearbyPlayers(this);
2019-02-27 19:53:05 +01:00
/*
if(this.world != null && !this.world.isRemote){
NBTTagCompound compound = new NBTTagCompound();
this.writeSyncableNBT(compound, NBTType.SYNC);
2019-05-02 09:10:29 +02:00
NBTTagCompound data = new NBTTagCompound();
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));
}*/
}
2019-05-02 09:10:29 +02:00
public void writeSyncableNBT(NBTTagCompound compound, NBTType type) {
if (type != NBTType.SAVE_BLOCK) super.writeToNBT(compound);
2019-05-02 09:10:29 +02:00
if (type == NBTType.SAVE_TILE) {
compound.setBoolean("Redstone", this.isRedstonePowered);
2016-07-22 20:23:51 +02:00
compound.setInteger("TicksElapsed", this.ticksElapsed);
2016-11-27 23:04:26 +01:00
compound.setBoolean("StopDrop", this.stopFromDropping);
2019-05-02 09:10:29 +02:00
} else if (type == NBTType.SYNC && this.stopFromDropping) compound.setBoolean("StopDrop", this.stopFromDropping);
2019-05-02 09:10:29 +02:00
if (this.isRedstoneToggle() && (type != NBTType.SAVE_BLOCK || this.isPulseMode)) {
compound.setBoolean("IsPulseMode", this.isPulseMode);
}
2015-12-01 19:48:09 +01:00
}
2019-05-02 09:10:29 +02:00
public void readSyncableNBT(NBTTagCompound compound, NBTType type) {
if (type != NBTType.SAVE_BLOCK) super.readFromNBT(compound);
2019-05-02 09:10:29 +02:00
if (type == NBTType.SAVE_TILE) {
this.isRedstonePowered = compound.getBoolean("Redstone");
2016-07-22 20:23:51 +02:00
this.ticksElapsed = compound.getInteger("TicksElapsed");
2016-11-27 23:04:26 +01:00
this.stopFromDropping = compound.getBoolean("StopDrop");
2019-05-02 09:10:29 +02:00
} else if (type == NBTType.SYNC) this.stopFromDropping = compound.getBoolean("StopDrop");
2019-05-02 09:10:29 +02:00
if (this.isRedstoneToggle()) {
this.isPulseMode = compound.getBoolean("IsPulseMode");
}
2015-10-21 00:22:50 +02:00
}
2015-10-18 15:31:01 +02:00
2016-02-01 17:49:55 +01:00
@Override
2019-05-02 09:10:29 +02:00
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState) {
return !oldState.getBlock().isAssociatedBlock(newState.getBlock());
}
2019-05-02 09:10:29 +02:00
public String getNameForTranslation() {
return "container." + ActuallyAdditions.MODID + "." + this.name + ".name";
2016-11-20 13:46:35 +01:00
}
@Override
2019-05-02 09:10:29 +02:00
public ITextComponent getDisplayName() {
return new TextComponentTranslation(this.getNameForTranslation());
}
@Override
2019-05-02 09:10:29 +02:00
public final void update() {
2016-02-01 17:49:55 +01:00
this.updateEntity();
}
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
2019-05-02 09:10:29 +02:00
public void updateEntity() {
2016-02-01 17:49:55 +01:00
this.ticksElapsed++;
2019-05-02 09:10:29 +02:00
if (!this.world.isRemote) {
if (this.shareEnergy) {
ISharingEnergyProvider provider = (ISharingEnergyProvider) this;
if (provider.doesShareEnergy()) {
int total = provider.getEnergyToSplitShare();
2019-05-02 09:10:29 +02:00
if (total > 0) {
EnumFacing[] sides = provider.getEnergyShareSides();
2019-05-02 09:10:29 +02:00
int amount = total / sides.length;
if (amount <= 0) {
amount = total;
}
2019-05-02 09:10:29 +02:00
for (EnumFacing side : sides) {
TileEntity tile = this.tilesAround[side.ordinal()];
2019-05-02 09:10:29 +02:00
if (tile != null && provider.canShareTo(tile)) {
WorldUtil.doEnergyInteraction(this, tile, side, amount);
}
}
}
}
}
2019-05-02 09:10:29 +02:00
if (this.shareFluid) {
ISharingFluidHandler handler = (ISharingFluidHandler) this;
if (handler.doesShareFluid()) {
2016-11-19 21:11:17 +01:00
int total = handler.getMaxFluidAmountToSplitShare();
2019-05-02 09:10:29 +02:00
if (total > 0) {
EnumFacing[] sides = handler.getFluidShareSides();
2019-05-02 09:10:29 +02:00
int amount = total / sides.length;
if (amount <= 0) {
amount = total;
}
2019-05-02 09:10:29 +02:00
for (EnumFacing side : sides) {
TileEntity tile = this.tilesAround[side.ordinal()];
2019-05-02 09:10:29 +02:00
if (tile != null) {
WorldUtil.doFluidInteraction(this, tile, side, amount);
}
}
}
}
}
2019-05-02 09:10:29 +02:00
if (!this.hasSavedDataOnChangeOrWorldStart) {
if (this.shouldSaveDataOnChangeOrWorldStart()) {
this.saveDataOnChangeOrWorldStart();
}
this.hasSavedDataOnChangeOrWorldStart = true;
}
}
}
2019-05-02 09:10:29 +02:00
public void saveDataOnChangeOrWorldStart() {
for (EnumFacing side : EnumFacing.values()) {
BlockPos pos = this.pos.offset(side);
2019-05-02 09:10:29 +02:00
if (this.world.isBlockLoaded(pos)) {
this.tilesAround[side.ordinal()] = this.world.getTileEntity(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;
2015-12-25 15:24:40 +01:00
this.markDirty();
2015-12-19 10:30:39 +01:00
}
2019-05-02 09:10:29 +02:00
public boolean canPlayerUse(EntityPlayer player) {
return player.getDistanceSq(this.getPos().getX() + 0.5D, this.pos.getY() + 0.5D, this.pos.getZ() + 0.5D) <= 64 && !this.isInvalid() && this.world.getTileEntity(this.pos) == this;
}
2019-05-02 09:10:29 +02:00
protected boolean sendUpdateWithInterval() {
if (this.ticksElapsed % ConfigIntValues.TILE_ENTITY_UPDATE_INTERVAL.getValue() == 0) {
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;
}
}
2016-06-05 02:16:52 +02:00
@Override
2019-05-02 09:10:29 +02:00
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
2016-06-05 02:16:52 +02:00
return this.getCapability(capability, facing) != null;
}
@SuppressWarnings("unchecked")
2019-02-27 19:53:05 +01:00
@Override
2019-05-02 09:10:29 +02:00
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
IItemHandler handler = this.getItemHandler(facing);
2019-05-02 09:10:29 +02:00
if (handler != null) { return (T) handler; }
} else if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) {
2016-06-05 02:16:52 +02:00
IFluidHandler tank = this.getFluidHandler(facing);
2019-05-02 09:10:29 +02:00
if (tank != null) { return (T) tank; }
} else if (capability == CapabilityEnergy.ENERGY) {
2016-11-26 08:58:42 +01:00
IEnergyStorage storage = this.getEnergyStorage(facing);
2019-05-02 09:10:29 +02:00
if (storage != null) { return (T) storage; }
2016-11-26 08:58:42 +01:00
}
2016-06-05 02:16:52 +02:00
return super.getCapability(capability, facing);
}
2019-05-02 09:10:29 +02:00
public IFluidHandler getFluidHandler(EnumFacing facing) {
2016-06-05 02:16:52 +02:00
return null;
}
2019-05-02 09:10:29 +02:00
public IEnergyStorage getEnergyStorage(EnumFacing facing) {
2016-11-26 08:58:42 +01:00
return null;
}
2019-05-02 09:10:29 +02:00
public IItemHandler getItemHandler(EnumFacing 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
}
}