Guest User

Untitled

a guest
Jun 5th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.55 KB | None | 0 0
  1.  
  2. import fr.rissalfa.slimemod.init.ItemInit;
  3.  
  4. import net.minecraft.inventory.ItemStackHelper;
  5. import net.minecraft.item.Item;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.nbt.NBTTagCompound;
  8. import net.minecraft.tileentity.TileEntity;
  9.  
  10.  
  11. import net.minecraft.util.ITickable;
  12. import net.minecraft.util.NonNullList;
  13. import net.minecraft.util.math.MathHelper;
  14. import net.minecraftforge.fml.relauncher.Side;
  15. import net.minecraftforge.fml.relauncher.SideOnly;
  16. import net.minecraftforge.items.IItemHandler;
  17.  
  18. import javax.annotation.Nonnull;
  19. public class TileEntityMachineBase extends TileEntity implements IItemHandler, ITickable {
  20.  
  21.     private NonNullList<ItemStack> machineItemStacks = NonNullList.withSize(4, ItemStack.EMPTY);
  22.  
  23.     private int machineRunTime;
  24.  
  25.  
  26.     private int currentItemRunTime;
  27.     private int processTime;
  28.     private int totalProcessTime;
  29.  
  30.     private String machineCustomName;
  31.  
  32.  
  33.  
  34.  
  35.     public void setCustomName(String name)
  36.     {
  37.         machineCustomName = name;
  38.         return;
  39.     }
  40.  
  41.     public int getSizeInventory()
  42.     {
  43.         return machineItemStacks.size();
  44.     }
  45.  
  46.     @Override
  47.     public int getSlots()
  48.     {
  49.         return 4;
  50.     }
  51.  
  52.     public boolean hasCustomName()
  53.     {
  54.         return this.machineCustomName != null && !this.machineCustomName.isEmpty();
  55.     }
  56.  
  57.  
  58.     @Override
  59.     public ItemStack getStackInSlot(int index)
  60.     {
  61.         return this.machineItemStacks.get(index);
  62.     }
  63.  
  64.     @Nonnull
  65.     @Override
  66.     public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
  67.         return null;
  68.     }
  69.  
  70.     @Nonnull
  71.     @Override
  72.     public ItemStack extractItem(int slot, int amount, boolean simulate) {
  73.         return null;
  74.     }
  75.  
  76.     @Override
  77.     public int getSlotLimit(int slot) {
  78.         return 64;
  79.     }
  80.  
  81.  
  82.     @Override
  83.     public void readFromNBT(NBTTagCompound compound)
  84.     {
  85.         super.readFromNBT(compound);
  86.  
  87.         this.machineItemStacks = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
  88.         ItemStackHelper.loadAllItems(compound, this.machineItemStacks);
  89.  
  90.         this.machineRunTime = compound.getInteger("RunTime");
  91.         this.processTime = compound.getInteger("ProcessTime");
  92.         this.totalProcessTime = compound.getInteger("ProcessTimeTotal");
  93.         this.currentItemRunTime = getItemRunTime(this.machineItemStacks.get(2));
  94.  
  95.         if (compound.hasKey("CustomName", 8)) this.setCustomName("CustomName");
  96.     }
  97.  
  98.     @Override
  99.     public NBTTagCompound writeToNBT(NBTTagCompound compound)
  100.     {
  101.         super.writeToNBT(compound);
  102.         compound.setInteger("RunTime", (short) this.machineRunTime);
  103.         compound.setInteger("ProcessTime", (short) this.processTime);
  104.         compound.setInteger("ProcessTimeTotal", (short) this.totalProcessTime);
  105.         ItemStackHelper.saveAllItems(compound, this.machineItemStacks);
  106.  
  107.         if (this.hasCustomName())compound.setString("CustomName", this.machineCustomName);
  108.  
  109.         return compound;
  110.     }
  111.  
  112.  
  113.     public boolean isRunning() {
  114.         return this.machineRunTime > 0;
  115.     }
  116.  
  117.  
  118.     @SideOnly(Side.CLIENT)
  119.     public static boolean isRunning(IItemHandler itemHandler) {return itemHandler.getStackInSlot(0).getCount() > 0;}
  120.  
  121.     @Override
  122.     public void update()
  123.     {
  124.         boolean flag = this.isRunning();
  125.         boolean flag1 = false;
  126.  
  127.         if(this.isRunning()) --this.machineRunTime;
  128.  
  129.         if(!this.world.isRemote)
  130.         {
  131.             ItemStack stack = (ItemStack)this.machineItemStacks.get(2);
  132.  
  133.             if(this.isRunning() || !stack.isEmpty() && !((((ItemStack)this.machineItemStacks.get(0)).isEmpty()) || ((ItemStack)this.machineItemStacks.get(1)).isEmpty()))
  134.             {
  135.                 if(!this.isRunning() && this.canProcess())
  136.                 {
  137.                     this.machineRunTime = getItemRunTime(stack);
  138.                     this.currentItemRunTime = this.machineRunTime;
  139.  
  140.                     if(this.isRunning())
  141.                     {
  142.                         flag1 = true;
  143.  
  144.                         if(!stack.isEmpty())
  145.                         {
  146.                             Item item = stack.getItem();
  147.                             stack.shrink(1);
  148.  
  149.                             if(stack.isEmpty())
  150.                             {
  151.                                 ItemStack item1 = item.getContainerItem(stack);
  152.                                 this.machineItemStacks.set(2, item1);
  153.                             }
  154.                         }
  155.                     }
  156.                 }
  157.                 if(this.isRunning() && this.canProcess())
  158.                 {
  159.                     ++this.processTime;
  160.  
  161.                     if(this.processTime == this.totalProcessTime)
  162.                     {
  163.                         this.processTime = 0;
  164.                         this.totalProcessTime = this.getProcessTime((ItemStack)this.machineItemStacks.get(0), (ItemStack)this.machineItemStacks.get(1));
  165.                         this.processItem();
  166.                         flag1 = true;
  167.                     }
  168.                 }
  169.                 else this.processTime = 0;
  170.             }
  171.             else if(!this.isRunning() && this.processTime > 0)
  172.             {
  173.                 this.processTime = MathHelper.clamp(this.processTime - 2, 0, this.totalProcessTime);
  174.             }
  175.             if(flag != this.isRunning())
  176.             {
  177.                 flag1 = true;
  178.                 MachineBaseBlock.setState(this.world, this.pos);
  179.             }
  180.         }
  181.         if(flag1) this.markDirty();
  182.     }
  183.  
  184.     public int getProcessTime(ItemStack stack, ItemStack stack1)
  185.     {
  186.         return 200;
  187.     }
  188.  
  189.  
  190.     private boolean canProcess()
  191.     {
  192.         if ((this.machineItemStacks.get(0)).isEmpty() || (this.machineItemStacks.get(1)).isEmpty()) return false;
  193.         else
  194.         {
  195.             ItemStack itemstack = MachineBaseRecipes.instance().getProcessResult(this.machineItemStacks.get(0), this.machineItemStacks.get(1));
  196.  
  197.             if (itemstack.isEmpty()) return false;
  198.             else
  199.             {
  200.                 ItemStack itemstack1 = this.machineItemStacks.get(3);
  201.                 if (itemstack1.isEmpty()) return true;
  202.                 else if (!itemstack1.isItemEqual(itemstack)) return false;
  203.                 else if (itemstack1.getCount() + itemstack.getCount() <= this.getSlotLimit(3) && itemstack1.getCount() + itemstack.getCount() <= itemstack1.getMaxStackSize()) return true;
  204.                 else return itemstack1.getCount() + itemstack.getCount() <= itemstack.getMaxStackSize();
  205.             }
  206.         }
  207.     }
  208.  
  209.     public void processItem()
  210.     {
  211.         if (this.canProcess())
  212.         {
  213.             ItemStack input1 = this.machineItemStacks.get(0);
  214.             ItemStack input2 = this.machineItemStacks.get(1);
  215.             ItemStack result = MachineBaseRecipes.instance().getProcessResult(input1, input2);
  216.             ItemStack output = this.machineItemStacks.get(3);
  217.  
  218.             if (output.isEmpty()) this.machineItemStacks.set(3, result.copy());
  219.             else if (output.getItem() == result.getItem()) output.grow(result.getCount());
  220.  
  221.             input1.shrink(1);
  222.             input2.shrink(2);
  223.         }
  224.     }
  225.  
  226.  
  227.     public static int getItemRunTime(ItemStack energy)
  228.     {
  229.         if (energy.isEmpty()) return 0;
  230.         else
  231.         {
  232.             Item item = energy.getItem();
  233.  
  234.             if (item == ItemInit.BATTERY)return 200;
  235.  
  236.             else return 0;
  237.  
  238.         }
  239.     }
  240.  
  241.     public static boolean isItemEnergy(ItemStack stack) {
  242.         return getItemRunTime(stack) > 0;
  243.     }
  244.  
  245. }
Add Comment
Please, Sign In to add comment