Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.theviolentsquirrels.questsystem.tileentity;
- import net.minecraft.block.state.IBlockState;
- 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.util.BlockPos;
- import net.minecraft.util.ITickable;
- import net.minecraft.world.World;
- import net.minecraftforge.fml.common.FMLLog;
- import net.theviolentsquirrels.questsystem.Reference;
- import net.theviolentsquirrels.questsystem.block.BlockBarrel;
- import net.theviolentsquirrels.questsystem.utility.TicksHelper;
- public class TileEntityBlockBarrel extends TileEntity implements ITickable {
- private boolean hasFullyFermented;
- private int currentCycle;
- private int ticksBeforeNewCycle;
- public void startFermentation(int newCycle) {
- this.setHasFullyFermented(!(newCycle < Reference.BARREL_MAX_CYCLE_LEVEL));
- if (!this.hasFullyFermented()) {
- this.currentCycle = newCycle;
- this.ticksBeforeNewCycle = this.getTicksByCycleLevel();
- FMLLog.info("New cycle --> level " + this.currentCycle + " with " + this.ticksBeforeNewCycle + " ticks.");
- } else FMLLog.info("Barrel has fully fermented !");
- this.sendUpdateBarrelTexture(newCycle);
- }
- private int getTicksByCycleLevel() {
- int fermentationTimeTicks, totalTicks;
- float multiplier;
- fermentationTimeTicks = TicksHelper.getTicksFromSeconds(Reference.BARREL_FERMENTATION_TIME);
- multiplier = Reference.BARREL_CYCLE_MULTIPLIER;
- totalTicks = TicksHelper.roundTicksFromFloat(this.currentCycle * multiplier * fermentationTimeTicks);
- return (totalTicks == 0 ? fermentationTimeTicks : totalTicks);
- }
- private void sendUpdateBarrelTexture(int cycle) {
- IBlockState blockState;
- blockState = this.worldObj.getBlockState(this.pos).getBlock().getDefaultState()
- .withProperty(BlockBarrel.TYPE, BlockBarrel.EnumType.byMetadata(cycle));
- this.worldObj.setBlockState(this.pos, blockState, 2);
- }
- public boolean hasFullyFermented() {
- return (this.hasFullyFermented);
- }
- public void setHasFullyFermented(boolean hasFullyFermented) {
- this.hasFullyFermented = hasFullyFermented;
- }
- @Override
- public void update() {
- if (!this.hasFullyFermented() && !this.worldObj.isRemote) {
- --this.ticksBeforeNewCycle;
- if (this.ticksBeforeNewCycle <= 0) {
- FMLLog.info("Cycle " + this.currentCycle + " done ! New cycle incoming ...");
- this.startFermentation(this.currentCycle + 1);
- }
- }
- }
- @Override
- public void writeToNBT(NBTTagCompound compound) {
- super.writeToNBT(compound);
- compound.setInteger("currentCycle", this.currentCycle);
- compound.setInteger("ticksBeforeNewCycle", this.ticksBeforeNewCycle);
- }
- @Override
- public void readFromNBT(NBTTagCompound compound) {
- super.readFromNBT(compound);
- this.currentCycle = compound.getInteger("currentCycle");
- this.ticksBeforeNewCycle = compound.getInteger("ticksBeforeNewCycle");
- }
- /**
- * Called when you receive a TileEntityData packet for the location this
- * TileEntity is currently in. On the client, the NetworkManager will always
- * be the remote server. On the server, it will be whomever is responsible for
- * sending the packet.
- *
- * @param net The NetworkManager the packet originated from
- * @param pkt The data packet
- */
- @Override
- public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
- super.onDataPacket(net, pkt);
- this.readFromNBT(pkt.getNbtCompound());
- }
- /**
- * Allows for a specialized description packet to be created. This is often used to sync tile entity data from the
- * server to the client easily. For example this is used by signs to synchronise the text to be displayed.
- */
- @Override
- public Packet getDescriptionPacket() {
- int metadata;
- NBTTagCompound compound = new NBTTagCompound();
- this.writeToNBT(compound);
- metadata = this.getBlockMetadata();
- return (new S35PacketUpdateTileEntity(this.pos, metadata, compound));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement