Advertisement
Guest User

Untitled

a guest
Feb 25th, 2016
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. package net.theviolentsquirrels.questsystem.tileentity;
  2.  
  3. import net.minecraft.block.state.IBlockState;
  4. import net.minecraft.nbt.NBTTagCompound;
  5. import net.minecraft.network.NetworkManager;
  6. import net.minecraft.network.Packet;
  7. import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
  8. import net.minecraft.tileentity.TileEntity;
  9. import net.minecraft.util.BlockPos;
  10. import net.minecraft.util.ITickable;
  11. import net.minecraft.world.World;
  12. import net.minecraftforge.fml.common.FMLLog;
  13. import net.theviolentsquirrels.questsystem.Reference;
  14. import net.theviolentsquirrels.questsystem.block.BlockBarrel;
  15. import net.theviolentsquirrels.questsystem.utility.TicksHelper;
  16.  
  17. public class            TileEntityBlockBarrel extends TileEntity implements ITickable {
  18.     private boolean     hasFullyFermented;
  19.     private int         currentCycle;
  20.     private int         ticksBeforeNewCycle;
  21.  
  22.     public void         startFermentation(int newCycle) {
  23.         this.setHasFullyFermented(!(newCycle < Reference.BARREL_MAX_CYCLE_LEVEL));
  24.         if (!this.hasFullyFermented()) {
  25.             this.currentCycle = newCycle;
  26.             this.ticksBeforeNewCycle = this.getTicksByCycleLevel();
  27.             FMLLog.info("New cycle --> level " + this.currentCycle + " with " + this.ticksBeforeNewCycle + " ticks.");
  28.         } else FMLLog.info("Barrel has fully fermented !");
  29.         this.sendUpdateBarrelTexture(newCycle);
  30.     }
  31.  
  32.     private int         getTicksByCycleLevel() {
  33.         int             fermentationTimeTicks, totalTicks;
  34.         float           multiplier;
  35.  
  36.         fermentationTimeTicks = TicksHelper.getTicksFromSeconds(Reference.BARREL_FERMENTATION_TIME);
  37.         multiplier = Reference.BARREL_CYCLE_MULTIPLIER;
  38.         totalTicks = TicksHelper.roundTicksFromFloat(this.currentCycle * multiplier * fermentationTimeTicks);
  39.         return (totalTicks == 0 ? fermentationTimeTicks : totalTicks);
  40.     }
  41.  
  42.     private void        sendUpdateBarrelTexture(int cycle) {
  43.         IBlockState     blockState;
  44.  
  45.         blockState = this.worldObj.getBlockState(this.pos).getBlock().getDefaultState()
  46.                 .withProperty(BlockBarrel.TYPE, BlockBarrel.EnumType.byMetadata(cycle));
  47.         this.worldObj.setBlockState(this.pos, blockState, 2);
  48.     }
  49.  
  50.     public boolean      hasFullyFermented() {
  51.         return (this.hasFullyFermented);
  52.     }
  53.  
  54.     public void         setHasFullyFermented(boolean hasFullyFermented) {
  55.         this.hasFullyFermented = hasFullyFermented;
  56.     }
  57.  
  58.     @Override
  59.     public void         update() {
  60.         if (!this.hasFullyFermented() && !this.worldObj.isRemote) {
  61.             --this.ticksBeforeNewCycle;
  62.             if (this.ticksBeforeNewCycle <= 0) {
  63.                 FMLLog.info("Cycle " + this.currentCycle + " done ! New cycle incoming ...");
  64.                 this.startFermentation(this.currentCycle + 1);
  65.             }
  66.         }
  67.     }
  68.  
  69.     @Override
  70.     public void         writeToNBT(NBTTagCompound compound) {
  71.         super.writeToNBT(compound);
  72.  
  73.         compound.setInteger("currentCycle", this.currentCycle);
  74.         compound.setInteger("ticksBeforeNewCycle", this.ticksBeforeNewCycle);
  75.     }
  76.  
  77.     @Override
  78.     public void         readFromNBT(NBTTagCompound compound) {
  79.         super.readFromNBT(compound);
  80.  
  81.         this.currentCycle = compound.getInteger("currentCycle");
  82.         this.ticksBeforeNewCycle = compound.getInteger("ticksBeforeNewCycle");
  83.     }
  84.  
  85.     /**
  86.      * Called when you receive a TileEntityData packet for the location this
  87.      * TileEntity is currently in. On the client, the NetworkManager will always
  88.      * be the remote server. On the server, it will be whomever is responsible for
  89.      * sending the packet.
  90.      *
  91.      * @param net       The NetworkManager the packet originated from
  92.      * @param pkt       The data packet
  93.      */
  94.     @Override
  95.     public void         onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
  96.         super.onDataPacket(net, pkt);
  97.         this.readFromNBT(pkt.getNbtCompound());
  98.     }
  99.  
  100.     /**
  101.      * Allows for a specialized description packet to be created. This is often used to sync tile entity data from the
  102.      * server to the client easily. For example this is used by signs to synchronise the text to be displayed.
  103.      */
  104.     @Override
  105.     public Packet       getDescriptionPacket() {
  106.         int             metadata;
  107.         NBTTagCompound  compound = new NBTTagCompound();
  108.  
  109.         this.writeToNBT(compound);
  110.         metadata = this.getBlockMetadata();
  111.         return (new S35PacketUpdateTileEntity(this.pos, metadata, compound));
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement