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
|
|
|
|
*
|
2016-05-16 22:54:42 +02:00
|
|
|
* © 2015-2016 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
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
|
2016-01-17 20:46:51 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.network.PacketUpdateTileEntity;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
|
2016-01-07 23:42:42 +01:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
2016-05-06 23:23:29 +02:00
|
|
|
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;
|
2016-01-07 23:42:42 +01:00
|
|
|
import net.minecraft.util.ITickable;
|
2016-03-18 23:47:22 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2015-03-07 02:23:31 +01:00
|
|
|
import net.minecraft.world.World;
|
2016-06-05 02:16:52 +02:00
|
|
|
import net.minecraftforge.common.capabilities.Capability;
|
|
|
|
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
|
|
|
import net.minecraftforge.fluids.capability.IFluidHandler;
|
2016-01-17 20:46:51 +01:00
|
|
|
import net.minecraftforge.fml.common.network.NetworkRegistry;
|
2016-01-07 18:20:59 +01:00
|
|
|
import net.minecraftforge.fml.common.registry.GameRegistry;
|
2014-12-12 15:40:01 +01:00
|
|
|
|
2016-01-07 23:42:42 +01:00
|
|
|
public abstract class TileEntityBase extends TileEntity implements ITickable{
|
2015-06-15 22:06:07 +02:00
|
|
|
|
2016-05-19 20:05:12 +02:00
|
|
|
public final String name;
|
2016-06-01 00:39:35 +02:00
|
|
|
public boolean isRedstonePowered;
|
2016-05-07 02:49:03 +02:00
|
|
|
protected int ticksElapsed;
|
2015-11-18 23:11:24 +01:00
|
|
|
|
2016-05-06 23:23:29 +02:00
|
|
|
public TileEntityBase(String name){
|
|
|
|
this.name = "container."+ModUtil.MOD_ID+"."+name;
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO Change for next major update to use the name variable automatically
|
2014-12-18 19:24:06 +01:00
|
|
|
public static void init(){
|
2015-07-01 21:32:48 +02:00
|
|
|
ModUtil.LOGGER.info("Registering TileEntities...");
|
2015-11-14 21:27:36 +01:00
|
|
|
|
2016-04-20 21:39:03 +02:00
|
|
|
GameRegistry.registerTileEntity(TileEntityCompost.class, ModUtil.MOD_ID+":tileEntityCompost");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityFeeder.class, ModUtil.MOD_ID+":tileEntityFeeder");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityGiantChest.class, ModUtil.MOD_ID+":tileEntityGiantChest");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityGrinder.class, ModUtil.MOD_ID+":tileEntityGrinder");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityFurnaceDouble.class, ModUtil.MOD_ID+":tileEntityFurnaceDouble");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityInputter.class, ModUtil.MOD_ID+":tileEntityInputter");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityFishingNet.class, ModUtil.MOD_ID+":tileEntityFishingNet");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityFurnaceSolar.class, ModUtil.MOD_ID+":tileEntityFurnaceSolar");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityHeatCollector.class, ModUtil.MOD_ID+":tileEntityHeatCollector");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityItemRepairer.class, ModUtil.MOD_ID+":tileEntityRepairer");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityGreenhouseGlass.class, ModUtil.MOD_ID+":tileEntityGreenhouseGlass");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityBreaker.class, ModUtil.MOD_ID+":tileEntityBreaker");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityDropper.class, ModUtil.MOD_ID+":tileEntityDropper");
|
2016-05-14 17:50:10 +02:00
|
|
|
GameRegistry.registerTileEntity(TileEntityInputterAdvanced.class, ModUtil.MOD_ID+":tileEntityInputterAdvanced");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityPlacer.class, ModUtil.MOD_ID+":tileEntityPlacer");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityGrinderDouble.class, ModUtil.MOD_ID+":tileEntityGrinderDouble");
|
2016-04-20 21:39:03 +02:00
|
|
|
GameRegistry.registerTileEntity(TileEntityCanolaPress.class, ModUtil.MOD_ID+":tileEntityCanolaPress");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityFermentingBarrel.class, ModUtil.MOD_ID+":tileEntityFermentingBarrel");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityOilGenerator.class, ModUtil.MOD_ID+":tileEntityOilGenerator");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityCoalGenerator.class, ModUtil.MOD_ID+":tileEntityCoalGenerator");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityPhantomItemface.class, ModUtil.MOD_ID+":tileEntityPhantomItemface");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityPhantomLiquiface.class, ModUtil.MOD_ID+":tileEntityPhantomLiquiface");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityPhantomEnergyface.class, ModUtil.MOD_ID+":tileEntityPhantomEnergyface");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityPhantomPlacer.class, ModUtil.MOD_ID+":tileEntityPhantomPlacer");
|
2016-05-14 17:50:10 +02:00
|
|
|
GameRegistry.registerTileEntity(TileEntityPhantomBreaker.class, ModUtil.MOD_ID+":tileEntityPhantomBreaker");
|
2016-04-20 21:39:03 +02:00
|
|
|
GameRegistry.registerTileEntity(TileEntityFluidCollector.class, ModUtil.MOD_ID+":tileEntityFluidCollector");
|
2016-05-14 17:50:10 +02:00
|
|
|
GameRegistry.registerTileEntity(TileEntityFluidPlacer.class, ModUtil.MOD_ID+":tileEntityFluidPlacer");
|
2016-04-20 21:39:03 +02:00
|
|
|
GameRegistry.registerTileEntity(TileEntityLavaFactoryController.class, ModUtil.MOD_ID+":tileEntityLavaFactoryController");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityCoffeeMachine.class, ModUtil.MOD_ID+":tileEntityCoffeeMachine");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityPhantomBooster.class, ModUtil.MOD_ID+":tileEntityPhantomBooster");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityEnergizer.class, ModUtil.MOD_ID+":tileEntityEnergizer");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityEnervator.class, ModUtil.MOD_ID+":tileEntityEnervator");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityXPSolidifier.class, ModUtil.MOD_ID+":tileEntityXPSolidifier");
|
|
|
|
GameRegistry.registerTileEntity(TileEntitySmileyCloud.class, ModUtil.MOD_ID+":tileEntityCloud");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityLeafGenerator.class, ModUtil.MOD_ID+":tileEntityLeafGenerator");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityDirectionalBreaker.class, ModUtil.MOD_ID+":tileEntityDirectionalBreaker");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityRangedCollector.class, ModUtil.MOD_ID+":tileEntityRangedCollector");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityAtomicReconstructor.class, ModUtil.MOD_ID+":tileEntityAtomicReconstructor");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityMiner.class, ModUtil.MOD_ID+":tileEntityMiner");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityFireworkBox.class, ModUtil.MOD_ID+":tileEntityFireworkBox");
|
2016-05-05 17:29:39 +02:00
|
|
|
GameRegistry.registerTileEntity(TileEntityPhantomRedstoneface.class, ModUtil.MOD_ID+":tileEntityPhantomRedstoneface");
|
2016-05-14 17:50:10 +02:00
|
|
|
GameRegistry.registerTileEntity(TileEntityLaserRelayItem.class, ModUtil.MOD_ID+":tileEntityLaserRelayItem");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityLaserRelayEnergy.class, ModUtil.MOD_ID+":tileEntityLaserRelay");
|
|
|
|
GameRegistry.registerTileEntity(TileEntityLaserRelayItemWhitelist.class, ModUtil.MOD_ID+":tileEntityLaserRelayItemWhitelist");
|
2016-05-08 21:09:58 +02:00
|
|
|
GameRegistry.registerTileEntity(TileEntityItemViewer.class, ModUtil.MOD_ID+":tileItemViewer");
|
2016-05-26 13:10:14 +02:00
|
|
|
GameRegistry.registerTileEntity(TileEntityBookletStand.class, ModUtil.MOD_ID+":tileEntityBookletStand");
|
2014-12-18 19:24:06 +01:00
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2015-10-18 15:28:06 +02:00
|
|
|
@Override
|
2016-05-26 13:10:14 +02:00
|
|
|
public void readFromNBT(NBTTagCompound compound){
|
2015-10-18 15:28:06 +02:00
|
|
|
super.readFromNBT(compound);
|
|
|
|
this.readSyncableNBT(compound, false);
|
|
|
|
}
|
|
|
|
|
2016-05-29 23:49:35 +02:00
|
|
|
|
2015-10-18 15:28:06 +02:00
|
|
|
@Override
|
2016-05-26 13:10:14 +02:00
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound compound){
|
|
|
|
compound = super.writeToNBT(compound);
|
|
|
|
this.writeSyncableNBT(compound, false);
|
|
|
|
return compound;
|
2015-10-18 15:28:06 +02:00
|
|
|
}
|
|
|
|
|
2015-10-03 10:16:18 +02:00
|
|
|
@Override
|
2016-05-26 13:10:14 +02:00
|
|
|
public SPacketUpdateTileEntity getUpdatePacket(){
|
|
|
|
NBTTagCompound compound = this.getUpdateTag();
|
2016-01-17 20:46:51 +01:00
|
|
|
if(compound != null){
|
2016-05-26 13:10:14 +02:00
|
|
|
return new SPacketUpdateTileEntity(this.pos, 0, compound);
|
2016-01-17 20:46:51 +01:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
return null;
|
|
|
|
}
|
2015-10-03 10:16:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-26 13:10:14 +02:00
|
|
|
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt){
|
2016-01-17 20:46:51 +01:00
|
|
|
if(pkt != null){
|
|
|
|
this.receiveSyncCompound(pkt.getNbtCompound());
|
|
|
|
}
|
2015-10-03 10:16:18 +02:00
|
|
|
}
|
|
|
|
|
2015-03-07 02:23:31 +01:00
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState){
|
2016-01-07 23:42:42 +01:00
|
|
|
return !(oldState.getBlock().isAssociatedBlock(newState.getBlock()));
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|
2015-10-18 15:28:06 +02:00
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
public void receiveSyncCompound(NBTTagCompound compound){
|
|
|
|
this.readSyncableNBT(compound, true);
|
|
|
|
}
|
|
|
|
|
2016-05-26 13:10:14 +02:00
|
|
|
@Override
|
|
|
|
public NBTTagCompound getUpdateTag(){
|
|
|
|
NBTTagCompound tag = super.getUpdateTag();
|
2016-02-01 17:49:55 +01:00
|
|
|
this.writeSyncableNBT(tag, true);
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
2016-06-04 00:26:33 +02:00
|
|
|
@Override
|
|
|
|
public void handleUpdateTag(NBTTagCompound compound){
|
|
|
|
this.receiveSyncCompound(compound);
|
|
|
|
}
|
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, boolean isForSync){
|
2016-05-26 13:10:14 +02:00
|
|
|
if(!isForSync){
|
|
|
|
compound.setBoolean("Redstone", this.isRedstonePowered);
|
|
|
|
}
|
2015-12-21 22:18:33 +01:00
|
|
|
if(this instanceof IRedstoneToggle){
|
|
|
|
compound.setBoolean("IsPulseMode", ((IRedstoneToggle)this).isPulseMode());
|
|
|
|
}
|
2015-12-01 19:48:09 +01:00
|
|
|
}
|
|
|
|
|
2015-10-21 00:22:50 +02:00
|
|
|
public void readSyncableNBT(NBTTagCompound compound, boolean isForSync){
|
2016-05-26 13:10:14 +02:00
|
|
|
if(!isForSync){
|
|
|
|
this.isRedstonePowered = compound.getBoolean("Redstone");
|
|
|
|
}
|
2015-12-21 22:18:33 +01:00
|
|
|
if(this instanceof IRedstoneToggle){
|
|
|
|
((IRedstoneToggle)this).toggle(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
|
2016-05-26 13:10:14 +02:00
|
|
|
public void update(){
|
2016-02-01 17:49:55 +01:00
|
|
|
this.updateEntity();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateEntity(){
|
|
|
|
this.ticksElapsed++;
|
|
|
|
}
|
|
|
|
|
2016-05-26 13:10:14 +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
|
|
|
}
|
|
|
|
|
2016-05-06 23:23: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.worldObj.getTileEntity(this.pos) == this;
|
|
|
|
}
|
|
|
|
|
2016-05-26 13:10:14 +02:00
|
|
|
protected boolean sendUpdateWithInterval(){
|
2015-12-01 19:48:09 +01:00
|
|
|
if(this.ticksElapsed%ConfigIntValues.TILE_ENTITY_UPDATE_INTERVAL.getValue() == 0){
|
2015-12-01 17:28:50 +01:00
|
|
|
this.sendUpdate();
|
2015-11-19 22:13:52 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return false;
|
2015-11-18 23:11:24 +01:00
|
|
|
}
|
2015-10-18 15:28:06 +02:00
|
|
|
}
|
2015-12-01 17:28:50 +01:00
|
|
|
|
2016-05-26 13:10:14 +02:00
|
|
|
public void sendUpdate(){
|
2016-05-13 16:58:16 +02:00
|
|
|
if(!this.worldObj.isRemote){
|
2016-05-26 13:10:14 +02:00
|
|
|
NBTTagCompound compound = this.getUpdateTag();
|
2016-05-13 16:58:16 +02:00
|
|
|
if(compound != null){
|
|
|
|
PacketHandler.theNetwork.sendToAllAround(new PacketUpdateTileEntity(compound, this.getPos()), new NetworkRegistry.TargetPoint(this.worldObj.provider.getDimension(), this.getPos().getX(), this.getPos().getY(), this.getPos().getZ(), 64));
|
|
|
|
}
|
2016-05-12 19:37:31 +02:00
|
|
|
}
|
2016-01-17 20:46:51 +01:00
|
|
|
}
|
2016-06-05 02:16:52 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasCapability(Capability<?> capability, EnumFacing facing){
|
|
|
|
return this.getCapability(capability, facing) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <T> T getCapability(Capability<T> capability, EnumFacing facing){
|
|
|
|
if(capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY){
|
|
|
|
IFluidHandler tank = this.getFluidHandler(facing);
|
|
|
|
if(tank != null){
|
|
|
|
return (T)tank;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.getCapability(capability, facing);
|
|
|
|
}
|
|
|
|
|
|
|
|
public IFluidHandler getFluidHandler(EnumFacing facing){
|
|
|
|
return null;
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
}
|