Advertisement
Guest User

TileEntityMiner

a guest
Jul 22nd, 2016
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. package com.jlgm.projfact.tileentity;
  2.  
  3. import java.util.Random;
  4.  
  5. import javax.annotation.Nullable;
  6.  
  7. import com.jlgm.projfact.block.BlockOreCluster;
  8. import com.jlgm.projfact.lib.ProjFactInventoryHelper;
  9.  
  10. import net.minecraft.block.state.IBlockState;
  11. import net.minecraft.inventory.IInventory;
  12. import net.minecraft.item.ItemStack;
  13. import net.minecraft.nbt.NBTTagCompound;
  14. import net.minecraft.network.play.server.SPacketUpdateTileEntity;
  15. import net.minecraft.tileentity.TileEntity;
  16. import net.minecraft.util.EnumFacing;
  17. import net.minecraft.util.ITickable;
  18.  
  19. public class TileEntityMiner extends TileEntity implements ITickable{
  20.    
  21.     private int cooldown = 10;
  22.     private int drillHeadDamage = 0;
  23.     public float ticks;
  24.     public float finalSpeed;
  25.     public float degrees;
  26.     public boolean doAnimation;
  27.     public String drillHeadType;
  28.     public int color;
  29.    
  30.     public void saveTileEntity(){
  31.         IBlockState state = this.worldObj.getBlockState(pos);
  32.         this.worldObj.notifyBlockUpdate(pos, state, state, 3);
  33.     }
  34.    
  35.     @Override
  36.     public NBTTagCompound writeToNBT(NBTTagCompound tagCompound){
  37.         super.writeToNBT(tagCompound);
  38.         System.out.println("writeToNBT() DrillHeadDamage: " + this.drillHeadDamage);
  39.         System.out.println("writeToNBT() Color: " + this.color);
  40.         System.out.println("writeToNBT() DoAnimation: " + this.doAnimation);
  41.         tagCompound.setInteger("DrillHeadDamage", this.drillHeadDamage);
  42.         tagCompound.setInteger("Color", this.color);
  43.         tagCompound.setBoolean("DoAnimation", this.doAnimation);
  44.         return tagCompound;
  45.     }
  46.    
  47.     @Override
  48.     public void readFromNBT(NBTTagCompound tagCompound){
  49.         super.readFromNBT(tagCompound);
  50.         this.drillHeadDamage = tagCompound.getInteger("DrillHeadDamage");
  51.         this.color = tagCompound.getInteger("Color");
  52.         this.doAnimation = tagCompound.getBoolean("DoAnimation");
  53.         System.out.println("readFromNBT() DrillHeadDamage: " + tagCompound.getInteger("DrillHeadDamage"));
  54.         System.out.println("readFromNBT() Color: " + tagCompound.getInteger("Color"));
  55.         System.out.println("readFromNBT() DoAnimation: " + tagCompound.getBoolean("DoAnimation"));
  56.     }
  57.    
  58.     @Override
  59.     @Nullable
  60.     public SPacketUpdateTileEntity getUpdatePacket(){
  61.         NBTTagCompound tagCompound = new NBTTagCompound();
  62.         writeToNBT(tagCompound);
  63.         System.out.println("getUpdatePacket() DrillHeadDamage: " + tagCompound.getInteger("DrillHeadDamage"));
  64.         System.out.println("getUpdatePacket() Color: " + tagCompound.getInteger("Color"));
  65.         System.out.println("getUpdatePacket() DoAnimation: " + tagCompound.getBoolean("DoAnimation"));
  66.         System.out.println("getUpdatePacket()");
  67.         return new SPacketUpdateTileEntity(this.pos, this.getBlockMetadata(), tagCompound);
  68.     }
  69.    
  70.     @Override
  71.     public void onDataPacket(net.minecraft.network.NetworkManager net, net.minecraft.network.play.server.SPacketUpdateTileEntity pkt){
  72.         NBTTagCompound tag = pkt.getNbtCompound();
  73.         System.out.println("onDataPacket() DrillHeadDamage: " + tag.getInteger("DrillHeadDamage"));
  74.         System.out.println("onDataPacket() Color: " + tag.getInteger("Color"));
  75.         System.out.println("onDataPacket() DoAnimation: " + tag.getBoolean("DoAnimation"));
  76.         readFromNBT(tag);
  77.         System.out.println("onDataPacket()");
  78.     }
  79.    
  80.     public TileEntityMiner(){
  81.         Random rand = new Random();
  82.         this.ticks = rand.nextInt(10000);
  83.         this.finalSpeed = 0;
  84.         this.degrees = rand.nextInt(359);
  85.         this.doAnimation = false;
  86.     }
  87.    
  88.     @Override
  89.     public void update() {
  90.         if(doAnimation){
  91.             ticks++;
  92.             finalSpeed = (float) (ticks * 0.025);
  93.             degrees = (float) (degrees + (20 + (Math.sin(finalSpeed) * 20)));
  94.             if (this.worldObj != null && !this.worldObj.isRemote){
  95.                 if(this.worldObj.getBlockState(pos.down()).getBlock() instanceof BlockOreCluster){
  96.                     BlockOreCluster blockOreCluster = (BlockOreCluster) this.worldObj.getBlockState(pos.down()).getBlock();
  97.                     if(drillHeadDamage < 1000){
  98.                         if(cooldown <= 1){
  99.                             IInventory iinventory = ProjFactInventoryHelper.getInventoryAtPosition(this.worldObj, pos.getX(), pos.getY() + 1, pos.getZ());
  100.                             if(ProjFactInventoryHelper.addItemStackToInventory(iinventory, EnumFacing.DOWN, new ItemStack(blockOreCluster.getOre()))){
  101.                                 drillHeadDamage++;
  102.                             }
  103.                             cooldown = 10;
  104.                         }else{
  105.                             --cooldown;
  106.                         }
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement