Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.theviolentsquirrels.questsystem.tileentity;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.init.Blocks;
- import net.minecraft.init.Items;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.IChatComponent;
- import net.minecraft.util.ITickable;
- import net.theviolentsquirrels.questsystem.Reference;
- import net.theviolentsquirrels.questsystem.utility.TicksHelper;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- public class TileEntityBlockSieve extends TileEntity implements ITickable, IInventory {
- private String sieveCustomName;
- private ItemStack[] sieveItemStacks = new ItemStack[2];
- private int siftTime;
- private int siftTotalTime;
- private static final ArrayList<Item> siftableBlocks = new ArrayList<Item>(Arrays.asList(
- Item.getItemFromBlock(Blocks.dirt),
- Item.getItemFromBlock(Blocks.grass),
- Item.getItemFromBlock(Blocks.gravel),
- Item.getItemFromBlock(Blocks.sand)
- ));
- public static boolean isSiftable(ItemStack stack) {
- return (siftableBlocks.contains(stack.getItem()));
- }
- public boolean isSifting() {
- return (this.siftTime > 0);
- }
- public static int getSiftingTime() {
- return (TicksHelper.getTicksFromSeconds(Reference.SIEVE_SIFTING_TIME));
- }
- @Override
- public void update() {
- boolean startFlag, updateFlag;
- startFlag = this.isSifting();
- updateFlag = false;
- if (!this.worldObj.isRemote) {
- if (this.isSifting() || this.sieveItemStacks[0] != null) {
- if (!this.isSifting() && this.canSourceBeSifted()) {
- this.siftTime = 0;
- this.siftTotalTime = TileEntityBlockSieve.getSiftingTime();
- updateFlag = true;
- if (this.sieveItemStacks[0] != null) {
- --this.sieveItemStacks[0].stackSize;
- startFlag = true;
- if (this.sieveItemStacks[0].stackSize <= 0)
- this.sieveItemStacks[0] = null;
- }
- }
- if (startFlag || this.isSifting() && this.canSourceBeSifted()) {
- ++this.siftTime;
- if (this.siftTime == this.siftTotalTime) {
- this.siftTime = 0;
- this.siftTotalTime = TileEntityBlockSieve.getSiftingTime();
- this.siftBlock();
- updateFlag = true;
- }
- }
- }
- if (updateFlag) this.markDirty();
- }
- }
- @Override
- public void readFromNBT(NBTTagCompound compound) {
- NBTTagList nbttaglist;
- super.readFromNBT(compound);
- nbttaglist = compound.getTagList("Items", 10);
- this.sieveItemStacks = new ItemStack[this.getSizeInventory()];
- for (int i = 0; i < nbttaglist.tagCount(); ++i) {
- NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
- int j = nbttagcompound.getByte("Slot");
- if (j >= 0 && j < this.sieveItemStacks.length)
- this.sieveItemStacks[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
- }
- this.siftTime = compound.getShort("SiftTime");
- this.siftTotalTime = compound.getShort("SiftTotalTime");
- if (compound.hasKey("CustomName", 8))
- this.sieveCustomName = compound.getString("CustomName");
- }
- @Override
- public void writeToNBT(NBTTagCompound compound) {
- NBTTagList nbttaglist = new NBTTagList();
- super.writeToNBT(compound);
- compound.setShort("SiftTime", (short) this.siftTime);
- compound.setShort("SiftTotalTime", (short) this.siftTotalTime);
- for (int i = 0; i < this.sieveItemStacks.length; ++i) {
- if (this.sieveItemStacks[i] != null) {
- NBTTagCompound nbttagcompound = new NBTTagCompound();
- nbttagcompound.setByte("Slot", (byte) i);
- this.sieveItemStacks[i].writeToNBT(nbttagcompound);
- nbttaglist.appendTag(nbttagcompound);
- }
- }
- compound.setTag("Items", nbttaglist);
- if (this.hasCustomName())
- compound.setString("CustomName", this.sieveCustomName);
- }
- public boolean checkforResultItemStack() {
- ItemStack itemStack = new ItemStack(Items.flint);
- int sizeResult;
- if (this.sieveItemStacks[1] == null) return (true);
- if (!this.sieveItemStacks[1].isItemEqual(itemStack)) return (false);
- sizeResult = this.sieveItemStacks[1].stackSize + itemStack.stackSize;
- return (sizeResult <= this.getInventoryStackLimit() && sizeResult <= this.sieveItemStacks[1].getMaxStackSize());
- }
- public boolean canSourceBeSifted() {
- if (this.sieveItemStacks[0] == null) return (false);
- return (this.checkforResultItemStack());
- }
- public void siftBlock() {
- if (this.checkforResultItemStack()) {
- int fortune;
- ItemStack itemStack = new ItemStack(Items.flint);
- fortune = new Random().nextInt(100);
- if (fortune < Reference.SIEVE_SIFTING_CHANCE) {
- if (this.sieveItemStacks[1] == null)
- this.sieveItemStacks[1] = itemStack.copy();
- else if (this.sieveItemStacks[1].getItem() == itemStack.getItem())
- this.sieveItemStacks[1].stackSize += itemStack.stackSize;
- }
- }
- }
- @Override
- public boolean isUseableByPlayer(EntityPlayer player) {
- return (this.worldObj.getTileEntity(this.pos) == this &&
- player.getDistanceSq((double) this.pos.getX() + 0.5D,
- (double) this.pos.getY() + 0.5D,
- (double) this.pos.getZ() + 0.5D) <= 64.0D);
- }
- @Override
- public void clear() {
- for (int i = 0; i < this.sieveItemStacks.length; ++i)
- this.sieveItemStacks[i] = null;
- }
- @Override
- public int getSizeInventory() {
- return (this.sieveItemStacks.length);
- }
- @Override
- public ItemStack getStackInSlot(int index) {
- return (this.sieveItemStacks[index]);
- }
- @Override
- public ItemStack decrStackSize(int index, int count) {
- if (this.sieveItemStacks[index] != null) {
- if (this.sieveItemStacks[index].stackSize <= count) {
- ItemStack itemstack1 = this.sieveItemStacks[index];
- this.sieveItemStacks[index] = null;
- return itemstack1;
- } else {
- ItemStack itemstack = this.sieveItemStacks[index].splitStack(count);
- if (this.sieveItemStacks[index].stackSize == 0)
- this.sieveItemStacks[index] = null;
- return (itemstack);
- }
- } else return (null);
- }
- @Override
- public ItemStack removeStackFromSlot(int index) {
- if (this.sieveItemStacks[index] != null) {
- ItemStack itemstack = this.sieveItemStacks[index];
- this.sieveItemStacks[index] = null;
- return (itemstack);
- }
- else return (null);
- }
- @Override
- public void setInventorySlotContents(int index, ItemStack stack) {
- this.sieveItemStacks[index] = stack;
- if (stack != null && stack.stackSize > this.getInventoryStackLimit())
- stack.stackSize = this.getInventoryStackLimit();
- }
- /**
- * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
- */
- @Override
- public int getInventoryStackLimit() {
- return (64);
- }
- @Override
- public void openInventory(EntityPlayer player) {}
- @Override
- public void closeInventory(EntityPlayer player) {}
- /**
- * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
- *
- * @param index
- * @param stack
- */
- @Override
- public boolean isItemValidForSlot(int index, ItemStack stack) {
- return (index != 1 && isSiftable(stack));
- }
- @Override
- public int getField(int id) {
- switch (id) {
- case 0:
- return (this.siftTime);
- case 1:
- return (this.siftTotalTime);
- default:
- return (0);
- }
- }
- @Override
- public void setField(int id, int value) {
- switch (id) {
- case 0:
- this.siftTime = value; break;
- case 1:
- this.siftTotalTime = value; break;
- }
- }
- @Override
- public int getFieldCount() {
- return (2);
- }
- /**
- * Get the name of this object. For players this returns their username
- */
- @Override
- public String getName() {
- return (this.hasCustomName() ? this.sieveCustomName : "container.sieve");
- }
- /**
- * Returns true if this thing is named
- */
- @Override
- public boolean hasCustomName() {
- return (this.sieveCustomName != null && this.sieveCustomName.length() > 0);
- }
- /**
- * Get the formatted ChatComponent that will be used for the sender's username in chat
- */
- @Override
- public IChatComponent getDisplayName() {
- return (null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement