Advertisement
Guest User

Untitled

a guest
Mar 4th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.03 KB | None | 0 0
  1. package net.theviolentsquirrels.questsystem.tileentity;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.init.Blocks;
  5. import net.minecraft.init.Items;
  6. import net.minecraft.inventory.IInventory;
  7. import net.minecraft.item.Item;
  8. import net.minecraft.item.ItemStack;
  9. import net.minecraft.nbt.NBTTagCompound;
  10. import net.minecraft.nbt.NBTTagList;
  11. import net.minecraft.tileentity.TileEntity;
  12. import net.minecraft.util.IChatComponent;
  13. import net.minecraft.util.ITickable;
  14. import net.theviolentsquirrels.questsystem.Reference;
  15. import net.theviolentsquirrels.questsystem.utility.TicksHelper;
  16.  
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Random;
  20.  
  21. public class TileEntityBlockSieve extends TileEntity implements ITickable, IInventory {
  22.     private String          sieveCustomName;
  23.     private ItemStack[]     sieveItemStacks = new ItemStack[2];
  24.  
  25.     private int             siftTime;
  26.     private int             siftTotalTime;
  27.  
  28.     private static final ArrayList<Item>    siftableBlocks = new ArrayList<Item>(Arrays.asList(
  29.             Item.getItemFromBlock(Blocks.dirt),
  30.             Item.getItemFromBlock(Blocks.grass),
  31.             Item.getItemFromBlock(Blocks.gravel),
  32.             Item.getItemFromBlock(Blocks.sand)
  33.     ));
  34.  
  35.     public static boolean   isSiftable(ItemStack stack) {
  36.         return (siftableBlocks.contains(stack.getItem()));
  37.     }
  38.  
  39.     public boolean          isSifting() {
  40.         return (this.siftTime > 0);
  41.     }
  42.  
  43.     public static int       getSiftingTime() {
  44.         return (TicksHelper.getTicksFromSeconds(Reference.SIEVE_SIFTING_TIME));
  45.     }
  46.  
  47.     @Override
  48.     public void             update() {
  49.         boolean             startFlag, updateFlag;
  50.  
  51.         startFlag = this.isSifting();
  52.         updateFlag = false;
  53.         if (!this.worldObj.isRemote) {
  54.             if (this.isSifting() || this.sieveItemStacks[0] != null) {
  55.                 if (!this.isSifting() && this.canSourceBeSifted()) {
  56.                     this.siftTime = 0;
  57.                     this.siftTotalTime = TileEntityBlockSieve.getSiftingTime();
  58.                     updateFlag = true;
  59.                     if (this.sieveItemStacks[0] != null) {
  60.                         --this.sieveItemStacks[0].stackSize;
  61.                         startFlag = true;
  62.                         if (this.sieveItemStacks[0].stackSize <= 0)
  63.                             this.sieveItemStacks[0] = null;
  64.                     }
  65.                 }
  66.                 if (startFlag || this.isSifting() && this.canSourceBeSifted()) {
  67.                     ++this.siftTime;
  68.                     if (this.siftTime == this.siftTotalTime) {
  69.                         this.siftTime = 0;
  70.                         this.siftTotalTime = TileEntityBlockSieve.getSiftingTime();
  71.                         this.siftBlock();
  72.                         updateFlag = true;
  73.                     }
  74.                 }
  75.             }
  76.             if (updateFlag) this.markDirty();
  77.         }
  78.     }
  79.  
  80.     @Override
  81.     public void             readFromNBT(NBTTagCompound compound) {
  82.         NBTTagList          nbttaglist;
  83.  
  84.         super.readFromNBT(compound);
  85.         nbttaglist = compound.getTagList("Items", 10);
  86.         this.sieveItemStacks = new ItemStack[this.getSizeInventory()];
  87.         for (int i = 0; i < nbttaglist.tagCount(); ++i) {
  88.             NBTTagCompound  nbttagcompound = nbttaglist.getCompoundTagAt(i);
  89.             int             j = nbttagcompound.getByte("Slot");
  90.  
  91.             if (j >= 0 && j < this.sieveItemStacks.length)
  92.                 this.sieveItemStacks[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
  93.         }
  94.         this.siftTime = compound.getShort("SiftTime");
  95.         this.siftTotalTime = compound.getShort("SiftTotalTime");
  96.         if (compound.hasKey("CustomName", 8))
  97.             this.sieveCustomName = compound.getString("CustomName");
  98.     }
  99.  
  100.     @Override
  101.     public void             writeToNBT(NBTTagCompound compound) {
  102.         NBTTagList          nbttaglist = new NBTTagList();
  103.  
  104.         super.writeToNBT(compound);
  105.         compound.setShort("SiftTime", (short) this.siftTime);
  106.         compound.setShort("SiftTotalTime", (short) this.siftTotalTime);
  107.         for (int i = 0; i < this.sieveItemStacks.length; ++i) {
  108.             if (this.sieveItemStacks[i] != null) {
  109.                 NBTTagCompound  nbttagcompound = new NBTTagCompound();
  110.  
  111.                 nbttagcompound.setByte("Slot", (byte) i);
  112.                 this.sieveItemStacks[i].writeToNBT(nbttagcompound);
  113.                 nbttaglist.appendTag(nbttagcompound);
  114.             }
  115.         }
  116.         compound.setTag("Items", nbttaglist);
  117.         if (this.hasCustomName())
  118.             compound.setString("CustomName", this.sieveCustomName);
  119.     }
  120.  
  121.     public boolean          checkforResultItemStack() {
  122.         ItemStack           itemStack = new ItemStack(Items.flint);
  123.         int                 sizeResult;
  124.  
  125.         if (this.sieveItemStacks[1] == null) return (true);
  126.         if (!this.sieveItemStacks[1].isItemEqual(itemStack)) return (false);
  127.         sizeResult = this.sieveItemStacks[1].stackSize + itemStack.stackSize;
  128.         return (sizeResult <= this.getInventoryStackLimit() && sizeResult <= this.sieveItemStacks[1].getMaxStackSize());
  129.     }
  130.  
  131.     public boolean          canSourceBeSifted() {
  132.         if (this.sieveItemStacks[0] == null) return (false);
  133.         return (this.checkforResultItemStack());
  134.     }
  135.  
  136.     public void             siftBlock() {
  137.         if (this.checkforResultItemStack()) {
  138.             int             fortune;
  139.             ItemStack       itemStack = new ItemStack(Items.flint);
  140.  
  141.             fortune = new Random().nextInt(100);
  142.             if (fortune < Reference.SIEVE_SIFTING_CHANCE) {
  143.                 if (this.sieveItemStacks[1] == null)
  144.                     this.sieveItemStacks[1] = itemStack.copy();
  145.                 else if (this.sieveItemStacks[1].getItem() == itemStack.getItem())
  146.                     this.sieveItemStacks[1].stackSize += itemStack.stackSize;
  147.             }
  148.         }
  149.     }
  150.  
  151.     @Override
  152.     public boolean          isUseableByPlayer(EntityPlayer player) {
  153.         return (this.worldObj.getTileEntity(this.pos) == this &&
  154.                 player.getDistanceSq((double) this.pos.getX() + 0.5D,
  155.                         (double) this.pos.getY() + 0.5D,
  156.                         (double) this.pos.getZ() + 0.5D) <= 64.0D);
  157.     }
  158.  
  159.     @Override
  160.     public void             clear() {
  161.         for (int i = 0; i < this.sieveItemStacks.length; ++i)
  162.             this.sieveItemStacks[i] = null;
  163.     }
  164.  
  165.     @Override
  166.     public int              getSizeInventory() {
  167.         return (this.sieveItemStacks.length);
  168.     }
  169.  
  170.     @Override
  171.     public ItemStack        getStackInSlot(int index) {
  172.         return (this.sieveItemStacks[index]);
  173.     }
  174.  
  175.     @Override
  176.     public ItemStack        decrStackSize(int index, int count) {
  177.         if (this.sieveItemStacks[index] != null) {
  178.             if (this.sieveItemStacks[index].stackSize <= count) {
  179.                 ItemStack   itemstack1 = this.sieveItemStacks[index];
  180.  
  181.                 this.sieveItemStacks[index] = null;
  182.                 return itemstack1;
  183.             } else {
  184.                 ItemStack   itemstack = this.sieveItemStacks[index].splitStack(count);
  185.  
  186.                 if (this.sieveItemStacks[index].stackSize == 0)
  187.                     this.sieveItemStacks[index] = null;
  188.                 return (itemstack);
  189.             }
  190.         } else return (null);
  191.     }
  192.  
  193.     @Override
  194.     public ItemStack        removeStackFromSlot(int index) {
  195.         if (this.sieveItemStacks[index] != null) {
  196.             ItemStack       itemstack = this.sieveItemStacks[index];
  197.  
  198.             this.sieveItemStacks[index] = null;
  199.             return (itemstack);
  200.         }
  201.         else return (null);
  202.     }
  203.  
  204.     @Override
  205.     public void             setInventorySlotContents(int index, ItemStack stack) {
  206.         this.sieveItemStacks[index] = stack;
  207.  
  208.         if (stack != null && stack.stackSize > this.getInventoryStackLimit())
  209.             stack.stackSize = this.getInventoryStackLimit();
  210.     }
  211.  
  212.     /**
  213.      * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
  214.      */
  215.     @Override
  216.     public int              getInventoryStackLimit() {
  217.         return (64);
  218.     }
  219.  
  220.     @Override
  221.     public void             openInventory(EntityPlayer player) {}
  222.  
  223.     @Override
  224.     public void             closeInventory(EntityPlayer player) {}
  225.  
  226.     /**
  227.      * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
  228.      *
  229.      * @param index
  230.      * @param stack
  231.      */
  232.     @Override
  233.     public boolean          isItemValidForSlot(int index, ItemStack stack) {
  234.         return (index != 1 && isSiftable(stack));
  235.     }
  236.  
  237.     @Override
  238.     public int              getField(int id) {
  239.         switch (id) {
  240.             case 0:
  241.                 return (this.siftTime);
  242.             case 1:
  243.                 return (this.siftTotalTime);
  244.             default:
  245.                 return (0);
  246.         }
  247.     }
  248.  
  249.     @Override
  250.     public void             setField(int id, int value) {
  251.         switch (id) {
  252.             case 0:
  253.                 this.siftTime = value; break;
  254.             case 1:
  255.                 this.siftTotalTime = value; break;
  256.         }
  257.     }
  258.  
  259.     @Override
  260.     public int              getFieldCount() {
  261.         return (2);
  262.     }
  263.  
  264.     /**
  265.      * Get the name of this object. For players this returns their username
  266.      */
  267.     @Override
  268.     public String           getName() {
  269.         return (this.hasCustomName() ? this.sieveCustomName : "container.sieve");
  270.     }
  271.  
  272.     /**
  273.      * Returns true if this thing is named
  274.      */
  275.     @Override
  276.     public boolean          hasCustomName() {
  277.         return (this.sieveCustomName != null && this.sieveCustomName.length() > 0);
  278.     }
  279.  
  280.     /**
  281.      * Get the formatted ChatComponent that will be used for the sender's username in chat
  282.      */
  283.     @Override
  284.     public IChatComponent   getDisplayName() {
  285.         return (null);
  286.     }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement