Skip to content

Instantly share code, notes, and snippets.

@dazsim
Created May 2, 2017 12:39
Show Gist options
  • Save dazsim/89a45e39868721a4af8374ba0ffd02d8 to your computer and use it in GitHub Desktop.
Save dazsim/89a45e39868721a4af8374ba0ffd02d8 to your computer and use it in GitHub Desktop.
TileBlock.java
package com.immersiveminds.beimcraft.tile;
import com.immersiveminds.beimcraft.BeimCraft;
import com.immersiveminds.beimcraft.api.BlockPos;
import com.immersiveminds.beimcraft.entity.ExtendedPlayer;
import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
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 String owner;
public TileBlock()
{
this.insulated = false;
this.sustainable = false;
this.stable = true;
this.insulation = 0;
this.sustainability = 0;
this.breakTimer = 0;
this.hasParent = false;
this.isParent = false;
this.owner = "Minecraft";
}
public TileBlock(boolean ins, boolean sus, boolean stab, int ins1, int sus1, int stab1,int cost,String owner)
{
this.insulated = ins;
this.sustainable = sus;
//this.stable = stab;
this.stable = true;
this.insulation = ins1;
this.sustainability = sus1;
this.stability = stab1;
this.currentStability = this.stability;
this.breakTimer = 0;
this.cost = cost;
this.hasParent = false;
this.hasChain = false;
this.isParent = false;
this.owner = owner;
//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;
}
public int checkStability(int x,int y, int z,int best)
{
TileEntity te = this.worldObj.getTileEntity(x, y, z);
if (te!=null)
{
if (te instanceof TileSupport)
{
//We found a support beam. Verify its stable and then attach if more stable.
TileSupport ts = (TileSupport)te;
//System.out.println("ts stab : "+ts.stability);
if (best< ts.stability)
{
ts.supportStabilityCheck();
if (ts.stable)
{
best = ts.stability;
this.stability = ts.stability;
this.isParent = true;
this.hasParent = false;
this.breakTimer = 0;
this.stable = true;
this.markDirty();
return this.stability;
} else
{
//abandon?
//System.out.println("abandon?");
if (best>0)
{
this.stability = best;
this.breakTimer = 0;
this.stable = true;
this.markDirty();
}
return best;
}
}
else if (best==ts.stability)
{
if (best>0)
{
this.stability = best;
this.breakTimer = 0;
this.stable = true;
this.markDirty();
}
return best;
}
} else
{
//System.out.println("We cant see the Support:"+te.getBlockType().getUnlocalizedName() );
}
if (te instanceof TileBlock)
{
TileBlock tb = (TileBlock)te;
//System.out.println("its a tileblock"+tb.stability);
if (tb.hasParent)
{
//if hasParent
if (tb.stability<best)
{
//do nothing
} else
if (tb.stability>1)
{
this.parent = tb.parent;
this.stability = tb.stability-1;
this.stable = true;
this.isParent = false;
this.hasParent = true;
//System.out.println("stable");
this.breakTimer = 0;
this.markDirty();
return this.stability;
} else if (tb.stability<=1)
{
if (stable)
{
this.breakTimer = 40;
this.isParent = false;
this.hasParent = false;
this.stability = 0;
this.stable = false;
//System.out.println("unstable");
this.markDirty();
return 0;
} else
{
//counting down to death
}
}
} else
if (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");
//If hasParent
if (!(tb.stability>best))
{
//do nothing
} else
{
this.parent = new BlockPos(tb.xCoord,tb.yCoord,tb.zCoord);
this.stability = tb.stability;
this.stable = true;
this.isParent = false;
this.hasParent = true;
this.breakTimer = 0;
//System.out.println("parent");
this.markDirty();
return tb.stability;
}
}
} else
{
//is a tile but not a tileblock
//can be valid floor blocks here.
//if unstable, flag and set break timer.
//default to stability of 3?
if (te instanceof TileFloor)
{
TileFloor tf = (TileFloor)te;
//System.out.println("this is a floor block!");
//I AM A FLOOR
if (!(tf.stability>best))
{
if (best>0)
{
this.stability = best;
this.breakTimer = 0;
this.stable = true;
this.markDirty();
}
return best;
} else
{
this.isParent = true;
this.hasParent = false;
this.stability = tf.stability;
this.breakTimer = 0;
this.stable = true;
this.markDirty();
return tf.stability;
}
} else
{
//System.out.println("set stability to 3. this is a tile entity but not one of ours");
if (this.stability>3)
{
//do nothing
} else
{
this.isParent = true;
this.hasParent = false;
//This is where we check the block to see what stability needs to be.
this.stability = 3;
this.breakTimer = 0;
this.stable = true;
//I AM A NON-HELPFUL TILEBLOCK
this.markDirty();
return 3;
}
}
}
} 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.");
Block tempBlock = this.worldObj.getBlock(x, y, z);
if (tempBlock!=null)
{
if (tempBlock.getUnlocalizedName().equals("tile.air"))
{
if (best==0)
{
if (this.stable==true)
{
this.breakTimer = 40;
this.isParent = false;
this.hasParent = false;
//this.stability = 3;
this.stable = false;
// System.out.println("unstable!");
this.stability = 0;
this.markDirty();
return 0;
} else
{
//we are counting down to death
}
} else
{
/*
if (best>0)
{
this.stability = best;
this.breakTimer = 0;
this.stable = true;
this.markDirty();
}*/
return best;
}
} else
{
//This is a generic block, set to stability 3 for now
this.isParent = true;
this.hasParent = false;
this.stability = 3;
this.stable = true;
//System.out.println("stability 3");
this.markDirty();
return 3;
}
} else
{
//System.out.println("This is null");
}
}
return this.stability;
}
@Override
public void updateEntity()
{
//this is where we check what sustainability/insulation levels are and update graphics
if (this.getWorldObj().isRemote)
{
//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);
//TODO: Fix this once blocks have an owner
//p.player.getDataWatcher().updateObject(ExtendedPlayer.BCOST_WATCHER,p.player.getDataWatcher().getWatchableObjectInt(ExtendedPlayer.BCOST_WATCHER) + BeimCraft.playerBreakBlockCost);
//p.player.getDataWatcher().updateObject(ExtendedPlayer.TCOST_WATCHER,p.player.getDataWatcher().getWatchableObjectInt(ExtendedPlayer.TCOST_WATCHER) + BeimCraft.playerBreakBlockCost);
}
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
int best = checkStability(this.xCoord,this.yCoord-1,this.zCoord,0);
//System.out.println("Best : "+best);
best = checkStability(this.xCoord-1,this.yCoord,this.zCoord,best);
//System.out.println("Best : "+best);
best = checkStability(this.xCoord+1,this.yCoord,this.zCoord,best);
//System.out.println("Best : "+best);
best = checkStability(this.xCoord,this.yCoord,this.zCoord-1,best);
//System.out.println("Best : "+best);
best = checkStability(this.xCoord,this.yCoord,this.zCoord+1,best);
//System.out.println("Best : "+best);
/*
TileEntity te = this.worldObj.getTileEntity(this.xCoord, this.yCoord-1, this.zCoord);
if (te!=null)
{
if (te instanceof TileBlock)
{
TileBlock tb = (TileBlock)te;
System.out.println("its a tileblock"+tb.stability);
if (tb.hasParent)
{
//if hasParent
if (tb.stability>1)
{
this.parent = tb.parent;
this.stability = tb.stability-1;
this.stable = true;
this.isParent = false;
this.hasParent = true;
System.out.println("stable");
this.markDirty();
} else if (tb.stability<=1)
{
if (stable)
{
this.breakTimer = 20;
this.isParent = false;
this.hasParent = false;
this.stability = 0;
this.stable = false;
System.out.println("unstable");
this.markDirty();
} else
{
//counting down to death
}
}
} else
if (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");
//If hasParent
this.parent = new BlockPos(tb.xCoord,tb.yCoord,tb.zCoord);
this.stability = tb.stability;
this.stable = true;
this.isParent = false;
this.hasParent = true;
//System.out.println("parent");
this.markDirty();
}
} else
{
//is a tile but not a tileblock
//can be valid floor blocks here.
//if unstable, flag and set break timer.
//default to stability of 3?
if (te instanceof TileFloor)
{
TileFloor tf = (TileFloor)te;
System.out.println("this is a floor block!");
//I AM A FLOOR
this.isParent = true;
this.hasParent = false;
this.stability = tf.stability;
this.stable = true;
this.markDirty();
} else
{
//System.out.println("set stability to 3. this is a tile entity but not one of ours");
this.isParent = true;
this.hasParent = false;
//This is where we check the block to see what stability needs to be.
this.stability = 3;
this.stable = true;
//I AM A NON-HELPFUL TILEBLOCK
this.markDirty();
}
}
} 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.");
Block tempBlock = this.worldObj.getBlock(this.xCoord, this.yCoord-1, this.zCoord);
if (tempBlock!=null)
{
if (tempBlock.getUnlocalizedName().equals("tile.air"))
{
if (this.stable==true)
{
this.breakTimer = 20;
this.isParent = false;
this.hasParent = false;
this.stability = 3;
this.stable = false;
System.out.println("stability 3");
this.markDirty();
} else
{
//we are counting down to death
}
}
//this is an empty block. lets set breakTimer to 100
} else
{
System.out.println("This is null");
}
}*/
//}
//check NSEW directions for better stability
//if ()
}
@Override
public boolean shouldRefresh(Block oldBlock, Block newBlock, int oldMeta, int newMeta, World world, int x, int y, int z)
{
return (!oldBlock.equals(newBlock));
}
@Override
public void writeToNBT(NBTTagCompound par1)
{
super.writeToNBT(par1);
par1.setInteger("meta", this.worldObj.getBlockMetadata(this.xCoord, this.yCoord, this.zCoord));
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);
par1.setString("owner", this.owner);
//
//getBlock(this.xCoord,this.yCoord,this.zCoord).getDamageValue(this.worldObj,this.xCoord, this.yCoord, this.zCoord));
//System.out.println(this.worldObj.getBlock(this.xCoord,this.yCoord,this.zCoord).getDamageValue(this.worldObj,this.xCoord, this.yCoord, this.zCoord));
}
@Override
public void readFromNBT(NBTTagCompound par1)
{
super.readFromNBT(par1);
this.worldObj.setBlockMetadataWithNotify(this.xCoord, this.yCoord, this.zCoord, par1.getInteger("meta"), 3);
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");
this.owner = par1.getString("owner");
//
}
@Override
public Packet getDescriptionPacket()
{
NBTTagCompound nbttagcompound = new NBTTagCompound();
this.writeToNBT(nbttagcompound);
return new S35PacketUpdateTileEntity(this.xCoord,this.yCoord,this.zCoord,1,nbttagcompound);
}
@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) {
readFromNBT(packet.func_148857_g());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment