summaryrefslogtreecommitdiff
path: root/ihl/enviroment/LightBulbTileEntity.java
diff options
context:
space:
mode:
authorFoghrye4 <foghrye4@gmail.com>2016-04-11 19:44:54 +0300
committerFoghrye4 <foghrye4@gmail.com>2016-04-11 19:44:54 +0300
commit05c78126859231a68e199dc34613689bd0978e2f (patch)
tree050bea104a18c72905095d29f31bec2935a27a24 /ihl/enviroment/LightBulbTileEntity.java
Initial commit
Diffstat (limited to 'ihl/enviroment/LightBulbTileEntity.java')
-rw-r--r--ihl/enviroment/LightBulbTileEntity.java308
1 files changed, 308 insertions, 0 deletions
diff --git a/ihl/enviroment/LightBulbTileEntity.java b/ihl/enviroment/LightBulbTileEntity.java
new file mode 100644
index 0000000..0d8f399
--- /dev/null
+++ b/ihl/enviroment/LightBulbTileEntity.java
@@ -0,0 +1,308 @@
+package ihl.enviroment;
+
+import java.util.List;
+import java.util.Vector;
+
+import net.minecraft.block.Block;
+import net.minecraft.entity.player.EntityPlayer;
+import net.minecraft.item.ItemStack;
+import net.minecraft.nbt.NBTTagCompound;
+import net.minecraft.tileentity.TileEntity;
+import net.minecraft.world.World;
+import net.minecraftforge.common.MinecraftForge;
+import net.minecraftforge.common.util.ForgeDirection;
+import ic2.api.energy.event.EnergyTileLoadEvent;
+import ic2.api.energy.event.EnergyTileUnloadEvent;
+import ic2.api.energy.tile.IEnergySink;
+import ic2.api.network.INetworkDataProvider;
+import ic2.api.tile.IWrenchable;
+import ic2.core.IC2;
+import ic2.core.ITickCallback;
+import ic2.core.network.NetworkManager;
+import ihl.utils.IHLUtils;
+
+public class LightBulbTileEntity extends TileEntity implements IEnergySink, IWrenchable, INetworkDataProvider
+{
+ private boolean active = false;
+ private short facing = 0;
+ public boolean prevActive = false;
+ public short prevFacing = 0;
+ private double maxEnergy=1.1d;
+ private double energy;
+ public boolean addedToEnergyNet = false;
+ private boolean loaded = false;
+ private int ticker;
+
+ @Override
+ public void readFromNBT(NBTTagCompound nbttagcompound)
+ {
+ super.readFromNBT(nbttagcompound);
+ this.energy = nbttagcompound.getDouble("energy");
+ this.facing = nbttagcompound.getShort("facing");
+ }
+
+ @Override
+ public void writeToNBT(NBTTagCompound nbttagcompound)
+ {
+ super.writeToNBT(nbttagcompound);
+ nbttagcompound.setDouble("energy", this.energy);
+ nbttagcompound.setShort("facing", this.facing);
+ }
+
+ /**
+ * validates a tile entity
+ */
+ @Override
+ public void validate()
+ {
+ super.validate();
+ IC2.tickHandler.addSingleTickCallback(this.worldObj, new ITickCallback()
+ {
+ @Override
+ public void tickCallback(World world)
+ {
+ if (!LightBulbTileEntity.this.isInvalid() && world.blockExists(LightBulbTileEntity.this.xCoord, LightBulbTileEntity.this.yCoord, LightBulbTileEntity.this.zCoord))
+ {
+ LightBulbTileEntity.this.onLoaded();
+
+ if (LightBulbTileEntity.this.enableUpdateEntity())
+ {
+ world.loadedTileEntityList.add(LightBulbTileEntity.this);
+ }
+ }
+ }
+ });
+ }
+
+ /**
+ * invalidates a tile entity
+ */
+ @Override
+ public void invalidate()
+ {
+ super.invalidate();
+ if (this.loaded)
+ {
+ this.onUnloaded();
+ }
+ }
+
+ @Override
+ public void onChunkUnload()
+ {
+ super.onChunkUnload();
+ if (this.loaded)
+ {
+ this.onUnloaded();
+ }
+ }
+
+ public void onLoaded()
+ {
+ if (IC2.platform.isSimulating() && !this.addedToEnergyNet)
+ {
+ MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
+ this.addedToEnergyNet = true;
+ }
+ this.loaded = true;
+ }
+
+ public void onUnloaded()
+ {
+ if (IC2.platform.isSimulating())
+ {
+ if(this.addedToEnergyNet)
+ {
+ MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
+ this.addedToEnergyNet = false;
+ }
+ this.active=false;
+ this.updateLightState();
+ }
+ }
+
+ @Override
+ public final boolean canUpdate()
+ {
+ return false;
+ }
+
+ @Override
+ public void updateEntity()
+ {
+ if(++this.ticker % 4 == 0)
+ {
+ if(this.prevFacing != facing)
+ {
+ this.setFacing(facing);
+ }
+ if(this.energy>0)
+ {
+ this.energy--;
+ this.setActive(this.energy>0);
+ }
+ else
+ {
+ this.setActive(false);
+ }
+ }
+ }
+
+ protected void updateLightState()
+ {
+ int x,y,z;
+ int xyz[] = {0,0,1,0,0,-1,0,0};
+ Block block;
+ for(int i=0;i<=5;i++)
+ {
+ x=xCoord+xyz[i];
+ y=yCoord+xyz[i+1];
+ z=zCoord+xyz[i+2];
+ block = this.worldObj.getBlock(x,y,z);
+ if(block.isAir(this.worldObj, x,y,z))
+ {
+ if(this.getActive())
+ {
+ worldObj.setBlock(x, y, z, LightBulbBlock.glowningAir);
+ }
+ if(!this.getActive())
+ {
+ worldObj.setBlockToAir(x, y, z);
+ }
+ }
+ }
+ }
+
+ public boolean enableUpdateEntity()
+ {
+ return IC2.platform.isSimulating();
+ }
+
+ @Override
+ public boolean acceptsEnergyFrom(TileEntity emitter,
+ ForgeDirection direction) {
+ switch(direction)
+ {
+ case UP:
+ return this.getFacing()==0;
+ case DOWN:
+ return this.getFacing()==1;
+ case SOUTH:
+ return this.getFacing()==2;
+ case NORTH:
+ return this.getFacing()==3;
+ case EAST:
+ return this.getFacing()==4;
+ case WEST:
+ return this.getFacing()==5;
+ default:
+ return false;
+ }
+ }
+
+ @Override
+ public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side)
+ {
+ return false;
+ }
+
+ @Override
+ public short getFacing() {
+ return this.facing;
+ }
+
+ @Override
+ public void setFacing(short facing1)
+ {
+ if (IC2.platform.isSimulating()&&this.addedToEnergyNet)
+ {
+ MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
+ this.addedToEnergyNet = false;
+ }
+ this.facing=facing1;
+ if(IC2.platform.isSimulating())
+ {
+ if (this.prevFacing != facing)
+ {
+ IC2.network.get().updateTileEntityField(this, "facing");
+ }
+ }
+ this.prevFacing = facing;
+ if (IC2.platform.isSimulating()&&!this.addedToEnergyNet)
+ {
+ MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
+ this.addedToEnergyNet = true;
+ }
+ }
+
+ @Override
+ public List<String> getNetworkedFields()
+ {
+ Vector ret = new Vector(2);
+ ret.add("active");
+ ret.add("facing");
+ return ret;
+ }
+
+ @Override
+ public boolean wrenchCanRemove(EntityPlayer entityPlayer) {
+ return true;
+ }
+
+ @Override
+ public float getWrenchDropRate() {
+ return 1;
+ }
+
+ @Override
+ public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
+ return IHLUtils.getThisModItemStack("lightBulb");
+ }
+
+ @Override
+ public double getDemandedEnergy()
+ {
+ return this.maxEnergy-this.energy;
+ }
+
+ @Override
+ public int getSinkTier() {
+ return 1;
+ }
+
+ @Override
+ public double injectEnergy(ForgeDirection directionFrom, double amount, double voltage)
+ {
+ if (this.energy >= this.maxEnergy)
+ {
+ return amount;
+ }
+ else
+ {
+ this.energy += amount;
+ return 0.0D;
+ }
+ }
+
+ public boolean getActive()
+ {
+ return this.active;
+ }
+
+ public void setActive(boolean active1)
+ {
+ this.active = active1;
+
+ if (this.prevActive != active1)
+ {
+ IC2.network.get().updateTileEntityField(this, "active");
+ updateLightState();
+ }
+ this.prevActive = active1;
+ }
+ public void setActiveWithoutNotify(boolean active1)
+ {
+ this.active = active1;
+ this.prevActive = active1;
+ }
+}