Advertisement
lethinh

TileSolderingStation

May 18th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.39 KB | None | 0 0
  1. package thinh.eligibleadapter.tile;
  2.  
  3. import cofh.api.energy.ItemEnergyContainer;
  4. import net.minecraft.entity.player.EntityPlayer;
  5. import net.minecraft.init.Items;
  6. import net.minecraft.init.SoundEvents;
  7. import net.minecraft.item.ItemStack;
  8. import net.minecraft.item.crafting.IRecipe;
  9. import net.minecraft.item.crafting.ShapedRecipes;
  10. import net.minecraft.nbt.NBTTagCompound;
  11. import net.minecraft.util.SoundCategory;
  12. import net.minecraftforge.oredict.OreDictionary;
  13. import net.minecraftforge.oredict.ShapelessOreRecipe;
  14. import thinh.eligibleadapter.crafting.solder.SolderCraftingManager;
  15. import thinh.eligibleadapter.gui.ContainerSolderingStation;
  16. import thinh.eligibleadapter.gui.GuiSolderingStation;
  17. import thinh.eligibleadapter.network.PacketHandler;
  18. import thinh.eligibleadapter.network.PacketSyncPersistentCrafting;
  19. import thinh.eligibleadapter.tile.inventory.ItemHandlerPersistentCrafting;
  20. import thinh.eligibleadapter.utils.InventoryUtils;
  21.  
  22. import javax.annotation.Nonnull;
  23. import java.util.ArrayList;
  24. import java.util.Iterator;
  25. import java.util.List;
  26.  
  27. public class TileSolderingStation extends AbstractTileInventoryPowered implements IAbstractTileHandler, INeedsUpdateTile {
  28.  
  29.     public final int SOLDER_TIME_REQUIRED = 200;
  30.     public int processTicks = 0;
  31.     public static final ItemHandlerPersistentCrafting craftMatrix = new ItemHandlerPersistentCrafting(null, new TileSolderingStation(), TileSolderingStation.contents, 9, 3, 3);
  32.     private ItemStack OUTPUT = null;
  33.  
  34.     public TileSolderingStation() {
  35.         super(11, "soldering_station");
  36.     }
  37.  
  38.     /**
  39.      * NBT
  40.      */
  41.     @Override public void readFromNBT(NBTTagCompound compound) {
  42.         super.readFromNBT(compound);
  43.         this.processTicks = compound.getInteger("processTicks");
  44.     }
  45.  
  46.     @Nonnull @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  47.         compound.setInteger("processTicks", this.processTicks);
  48.         return super.writeToNBT(compound);
  49.     }
  50.  
  51.     /**
  52.      * ITickable
  53.      */
  54.     @Override public void update() {
  55.         if (!this.worldObj.isRemote) {
  56.             boolean flag = false;
  57.             int SOLDER_ENERGY_REQUIRED = 2000;
  58.  
  59.             if (this.energy.getEnergyStored() >= SOLDER_ENERGY_REQUIRED) {
  60.                 if (this.canSolder()) {
  61.                     ++this.processTicks;
  62.  
  63.                     if (this.processTicks >= this.SOLDER_TIME_REQUIRED) {
  64.                         this.worldObj.playSound(null, this.pos, SoundEvents.BLOCK_REDSTONE_TORCH_BURNOUT, SoundCategory.NEUTRAL, 0.3F, this.worldObj.rand.nextFloat() * 0.1F + 0.9F);
  65.                         this.processTicks = 0;
  66.                         this.solderItems();
  67.                         flag = true;
  68.                         this.extractEnergy(SOLDER_ENERGY_REQUIRED);
  69.                     }
  70.                 } else {
  71.                     this.processTicks = 0;
  72.                 }
  73.             } else {
  74.                 this.processTicks = 0;
  75.             }
  76.  
  77.             if (flag) {
  78.                 this.markDirty();
  79.             }
  80.         }
  81.     }
  82.  
  83.     /**
  84.      * ItemHandlerBase
  85.      */
  86.     @Override public boolean isItemValidForSlot(int index, ItemStack stack) {
  87.         if (index == 0) {
  88.             return stack.getItem() == new ItemEnergyContainer();
  89.         } else {
  90.             if (index >= 2) {
  91.                 if (this.OUTPUT != null) {
  92.                     ItemStack stackInSlot = TileSolderingStation.contents.getStackInSlot(index);
  93.                     return stackInSlot != null && stackInSlot.isItemEqual(stack);
  94.                 }
  95.  
  96.                 return this.isSolderingInput(index - 2, stack);
  97.             }
  98.         }
  99.  
  100.         return false;
  101.     }
  102.  
  103.     @Override public boolean canExtractItem(int index, ItemStack stack) {
  104.         return index == 1;
  105.     }
  106.  
  107.     @Override public void markDirty() {
  108.         super.markDirty();
  109.  
  110.         if (TileSolderingStation.craftMatrix.container != null) {
  111.             TileSolderingStation.craftMatrix.container.detectAndSendChanges();
  112.         }
  113.  
  114.         PacketHandler.sendToServer(new PacketSyncPersistentCrafting());
  115.     }
  116.  
  117.     /**
  118.      * IAbstractTileHandler
  119.      */
  120.     @Override public String getName() {
  121.         return this.name;
  122.     }
  123.  
  124.     @Override public String setName(String name) {
  125.         return this.name = name;
  126.     }
  127.  
  128.     @Override public Object getGuiServer(EntityPlayer player, int ID, int x, int y, int z) {
  129.         return new ContainerSolderingStation(this, player.inventory);
  130.     }
  131.  
  132.     @Override public Object getGuiClient(EntityPlayer player, int ID, int x, int y, int z) {
  133.         return new GuiSolderingStation(this, player.inventory);
  134.     }
  135.  
  136.     /**
  137.      * INeedsUpdateTile
  138.      */
  139.     @Override public int getField(int id) {
  140.         switch (id) {
  141.             case 0:
  142.                 return this.processTicks;
  143.             case 1:
  144.                 return this.getEnergyStored();
  145.             default:
  146.                 return 0;
  147.         }
  148.     }
  149.  
  150.     @Override public void setField(int id, int value) {
  151.         switch (id) {
  152.             case 0:
  153.                 this.processTicks = value;
  154.                 break;
  155.             case 1:
  156.                 this.setEnergyStored(value);
  157.                 break;
  158.         }
  159.     }
  160.  
  161.     @Override public int getFieldCount() {
  162.         return 2;
  163.     }
  164.  
  165.     /**
  166.      * ================================ HELPERS START ================================
  167.      */
  168.  
  169.     private boolean canSolder() {
  170.         ItemStack itemstack = this.OUTPUT;
  171.  
  172.         if (itemstack == null) {
  173.             return false;
  174.         }
  175.         if (TileSolderingStation.contents.getStackInSlot(1) == null) {
  176.             return true;
  177.         }
  178.         if (!TileSolderingStation.contents.getStackInSlot(1).isItemEqual(itemstack)) {
  179.             return false;
  180.         }
  181.         int result = TileSolderingStation.contents.getStackInSlot(1).stackSize + itemstack.stackSize;
  182.         return result <= TileSolderingStation.contents.getStackLimit() && result <= itemstack.getMaxStackSize();
  183.     }
  184.  
  185.     public void updateInput() {
  186.         this.OUTPUT = SolderCraftingManager.findMatchingRecipe(TileSolderingStation.craftMatrix, this.worldObj);
  187.     }
  188.  
  189.     private void solderItems() {
  190.         if (this.canSolder()) {
  191.             ItemStack result = this.OUTPUT;
  192.  
  193.             if (TileSolderingStation.contents.getStackInSlot(1) == null) {
  194.                 TileSolderingStation.contents.setStackInSlot(1, result.copy());
  195.             } else {
  196.                 if (TileSolderingStation.contents.getStackInSlot(1).isItemEqual(result)) {
  197.                     if (TileSolderingStation.contents.getStackInSlot(1).stackSize + result.stackSize > 64) {
  198.                         for (int i = 0; i < TileSolderingStation.contents.getStackInSlot(1).stackSize + result.stackSize - 64; ++i) {
  199.                             InventoryUtils.dropItemStack(this.worldObj, this.pos, result);
  200.                         }
  201.  
  202.                         TileSolderingStation.contents.getStackInSlot(1).stackSize = 64;
  203.                     } else {
  204.                         TileSolderingStation.contents.getStackInSlot(1).stackSize += result.stackSize;
  205.                     }
  206.                 }
  207.             }
  208.  
  209.             for (int i = 0; i < TileSolderingStation.craftMatrix.getSlots(); ++i) {
  210.                 if (TileSolderingStation.craftMatrix.getStackInSlot(i) != null && ItemStack.areItemStacksEqual(TileSolderingStation.craftMatrix.getStackInSlot(i), (new ItemStack(Items.WATER_BUCKET)))) {
  211.                     TileSolderingStation.craftMatrix.setStackInSlot(i, new ItemStack(Items.BUCKET));
  212.                 } else {
  213.                     TileSolderingStation.craftMatrix.extractItem(i,
  214.                             1, false);
  215.                 }
  216.             }
  217.  
  218.             this.updateInput();
  219.         }
  220.     }
  221.  
  222.     private boolean isSolderingInput(int index, ItemStack stack) {
  223.         for (IRecipe recipe : SolderCraftingManager.recipes) {
  224.             if (recipe instanceof ShapedRecipes) {
  225.                 if (index >= ((ShapedRecipes) recipe).recipeItems.length) {
  226.                     continue;
  227.                 }
  228.  
  229.                 ItemStack itemstack1 = ((ShapedRecipes) recipe).recipeItems[index];
  230.                 if (stack.getItem() == itemstack1.getItem() && (itemstack1.getItemDamage() == 32767 || stack.getItemDamage() == itemstack1.getItemDamage())) {
  231.                     for (int i = 0; i < ((ShapedRecipes) recipe).recipeItems.length; i++) {
  232.                         if (i == index) {
  233.                             continue;
  234.                         }
  235.  
  236.                         ItemStack itemstack2 = ((ShapedRecipes) recipe).recipeItems[i];
  237.                         if (stack.getItem() == itemstack2.getItem() && (itemstack2.getItemDamage() == 32767 || stack.getItemDamage() == itemstack2.getItemDamage())) {
  238.                             ItemStack itemStack3 = TileSolderingStation.contents.getStackInSlot(index + 2);
  239.                             ItemStack itemStack4 = TileSolderingStation.contents.getStackInSlot(i + 2);
  240.                             return itemStack3 == null || itemStack4 != null && itemStack3.stackSize < itemStack4.stackSize;
  241.                         }
  242.                     }
  243.  
  244.                     return true;
  245.                 }
  246.             } else {
  247.                 if (recipe instanceof ShapelessOreRecipe) {
  248.                     List<Object> required = new ArrayList<>(((ShapelessOreRecipe) recipe).getInput());
  249.  
  250.                     Iterator<Object> req = required.iterator();
  251.  
  252.                     int match = 0;
  253.  
  254.                     while (req.hasNext()) {
  255.                         Object next = req.next();
  256.  
  257.                         if (next instanceof ItemStack) {
  258.                             if (OreDictionary.itemMatches((ItemStack) next,
  259.                                     stack,
  260.                                     false)) {
  261.                                 match++;
  262.                             }
  263.                         } else {
  264.                             if (next instanceof List) {
  265.                                 for (ItemStack itemStack : ((List<ItemStack>) next)) {
  266.                                     if (OreDictionary.itemMatches(itemStack,
  267.                                             stack,
  268.                                             false)) {
  269.                                         match++;
  270.                                         break;
  271.                                     }
  272.                                 }
  273.                             }
  274.                         }
  275.                     }
  276.  
  277.                     if (match == 0) {
  278.                         continue;
  279.                     }
  280.  
  281.                     if (match == 1) {
  282.                         return true;
  283.                     }
  284.  
  285.                     // Shapeless recipe can go into (match) number of slots
  286.                     int slotsFilled = 0;
  287.                     for (int i = 2; i <= 11; ++i) {
  288.                         ItemStack inMatrix = TileSolderingStation.contents.getStackInSlot(i);
  289.                         if (inMatrix != null && inMatrix.isItemEqual(stack)) {
  290.                             slotsFilled++;
  291.                         }
  292.                     }
  293.  
  294.                     if (slotsFilled < match) {
  295.                         return TileSolderingStation.contents.getStackInSlot(index + 2) == null;
  296.                     }
  297.  
  298.                     return this.worldObj.rand.nextInt(match) == 0;
  299.                 }
  300.             }
  301.         }
  302.  
  303.         return false;
  304.     }
  305.  
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement