Skip to content

Instantly share code, notes, and snippets.

@dazsim
Created April 29, 2017 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dazsim/4f864b6190641f5f3e461ac094e40ff3 to your computer and use it in GitHub Desktop.
Save dazsim/4f864b6190641f5f3e461ac094e40ff3 to your computer and use it in GitHub Desktop.
package com.immersiveminds.beimcraft.tile;
import com.immersiveminds.beimcraft.api.BlockPos;
import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
public class TileBlock extends TileEntity {
public int insulation, sustainability, stability, currentStability,thermalTimer, theodoliteTimer, breakTimer,cost;
public boolean insulated, sustainable, stable;
public BlockPos parent,chain;
public boolean hasParent,hasChain,isParent;
public TileBlock(boolean ins, boolean sus, boolean stab, int ins1, int sus1, int stab1,int cost)
{
System.out.println("This is a :"+this.toString());
System.out.println("this is a tile block");
this.insulated = ins;
this.sustainable = sus;
this.stable = stab;
this.insulation = ins1;
this.sustainability = sus1;
this.stability = stab1;
this.currentStability = this.stability;
this.breakTimer = 20;
this.cost = cost;
this.hasParent = false;
this.hasChain = false;
this.isParent = false;
//this.getWorldObj().setBlockMetadataWithNotify(this.xCoord, this.yCoord,this.zCoord,this.getWorldObj().getBlockMetadata(this.xCoord, this.yCoord, this.zCoord)+1,3);
//this.blockMetadata = this.getBlockMetadata();
//System.out.println("setup: "+this.blockMetadata);
//this.blockMetadata += 1;
//this.markDirty();
}
@Override
public boolean canUpdate()
{
return true;
}
@Override
public void updateEntity()
{
//this is where we check what sustainability/insulation levels are and update graphics
//client side
System.out.println("This is a :"+this.toString());
if (this.getWorldObj().isRemote==true)
{
//System.out.println("X: "+this.xCoord+" Y:"+this.yCoord+" Z: "+this.zCoord+" : "+this.breakTimer);
if (this.breakTimer>0)
{
this.breakTimer--;
if (this.breakTimer==0)
{
//time to break this block.
worldObj.setBlockToAir(this.xCoord, this.yCoord, this.zCoord);
this.markDirty();
worldObj.removeTileEntity(this.xCoord, this.yCoord, this.zCoord);
worldObj.markTileEntityChunkModified(this.xCoord, this.yCoord, this.zCoord, (TileEntity)this);
}
this.markDirty();
//System.out.println("counting down: "+this.breakTimer);
//System.out.println("Timer Running : "+this.blockMetadata + " " + this.breakTimer);
if ((this.blockMetadata>>3 & 1) == 0) //if bit 3 not set then set it.
{
//System.out.println("set meta to display ");
this.getWorldObj().setBlockMetadataWithNotify(this.xCoord, this.yCoord,this.zCoord,this.getWorldObj().getBlockMetadata(this.xCoord, this.yCoord, this.zCoord)+8,3);
this.markDirty();
//System.out.println(this.breakTimer+" X: "+this.xCoord+" Y:"+this.yCoord+" Z: "+this.zCoord+" : "+this.blockMetadata );
}
}
}
//server side
if (this.getWorldObj().isRemote==false)
{
if (this.breakTimer>0)
{
this.breakTimer--;
if (this.breakTimer==0)
{
//time to break this block.
worldObj.setBlockToAir(this.xCoord, this.yCoord, this.zCoord);
this.markDirty();
}
this.markDirty();
//System.out.println("counting down: "+this.breakTimer);
//System.out.println("Timer Running : "+this.blockMetadata + " " + this.breakTimer);
if ((this.blockMetadata>>3 & 1) == 0) //if bit 3 not set then set it.
{
//System.out.println("set meta to display ");
this.getWorldObj().setBlockMetadataWithNotify(this.xCoord, this.yCoord,this.zCoord,this.getWorldObj().getBlockMetadata(this.xCoord, this.yCoord, this.zCoord)+8,3);
this.markDirty();
//System.out.println(this.breakTimer+" X: "+this.xCoord+" Y:"+this.yCoord+" Z: "+this.zCoord+" : "+this.blockMetadata );
}
} else
{
//System.out.println("meta: "+this.getBlockMetadata());
//System.out.println("Timer Expired: "+this.blockMetadata);
if (((this.blockMetadata>>3) & 1)==1) //if bit 3 is set, set to 0.
{
//System.out.println(this.breakTimer+" X: "+this.xCoord+" Y:"+this.yCoord+" Z: "+this.zCoord+" : "+this.blockMetadata );
//this.blockMetadata -= 8;
//System.out.println("update");
//p_149689_1_.setBlockMetadataWithNotify(p_149689_2_, p_149689_3_, p_149689_4_, b0, 3);
this.getWorldObj().setBlockMetadataWithNotify(this.xCoord, this.yCoord,this.zCoord,this.blockMetadata-8,3);
this.markDirty();
}
}
}
//This is where the code for wall stability should live.
if ((!hasChain) || (!hasParent))
{
//If the block below this one is a valid chain block. attach and adopt parent
TileEntity te = this.worldObj.getTileEntity(this.xCoord, this.yCoord-1, this.zCoord);
if (te!=null)
{
if (te instanceof TileBlock)
{
TileBlock tb = (TileBlock)te;
if ((!tb.hasParent) || (!tb.isParent))
{
//block below does not have parent/is parent
//TODO: tell block to check for parent blocks
//inherit new found parent/stability info
//if unstable, flag and set break timer
System.out.println("hazy on whats happening here");
}
} else
{
//is a tile but not a tileblock
//we should ignore this - we dont want buildings resting on furnaces or beacons
//if unstable, flag and set break timer.
//default to stability of 3?
System.out.println("set stability to 3. this is a tile entity but not one of ours");
}
} else
{
//te is null
//this could be a dirt block or gravel or something. check and set up stability stuff
//for now set default stability to 3 for dirt, 5 for stone, 7 for concrete, 8 for support
//System.out.println("vanilla block support goes here.");
}
}
}
@Override
public boolean shouldRefresh(Block oldBlock, Block newBlock, int oldMeta, int newMeta, World world, int x, int y, int z)
{
if (oldBlock!=newBlock)
return true;
return false;
}
@Override
public void writeToNBT(NBTTagCompound par1)
{
super.writeToNBT(par1);
par1.setBoolean("insulated", this.insulated);
par1.setBoolean("sustainable", this.sustainable);
par1.setBoolean("stable", this.stable);
par1.setInteger("insulation", this.insulation);
par1.setInteger("sustainability", this.sustainability);
par1.setInteger("stability", this.stability);
par1.setInteger("breaktimer", this.breakTimer);
par1.setInteger("theodolitetimer", this.theodoliteTimer);
par1.setInteger("thermaltimer", this.thermalTimer);
par1.setInteger("cost", this.cost);
}
@Override
public void readFromNBT(NBTTagCompound par1)
{
super.readFromNBT(par1);
this.insulated = par1.getBoolean("insulated");
this.sustainable = par1.getBoolean("sustainable");
this.stable = par1.getBoolean("stable");
this.insulation = par1.getInteger("insulation");
this.sustainability = par1.getInteger("sustainability");
this.stability = par1.getInteger("stability");
this.breakTimer = par1.getInteger("breaktimer");
this.thermalTimer = par1.getInteger("thermaltimer");
this.theodoliteTimer = par1.getInteger("theodolitetimer");
this.cost = par1.getInteger("cost");
}
@Override
public Packet getDescriptionPacket()
{
NBTTagCompound nbttagcompound = new NBTTagCompound();
this.writeToNBT(nbttagcompound);
return new S35PacketUpdateTileEntity(this.xCoord,this.yCoord,this.zCoord,1,nbttagcompound);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment