Advertisement
Guest User

Fix clickSlot in Container

a guest
Jul 11th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.25 KB | None | 0 0
  1. /* Credit Mr-J:
  2.      *      https://github.com/Mr-J/AdvancedBackpackMod/blob/master/unrelated/slotClick%2BComments%2BRename%2BHelpers.java.txt
  3.      *
  4.      * values for flag:
  5.      * 0 = standard single click
  6.      * 1 = single click + shift modifier
  7.      * 2 = hotbar key is pressed (keys 0-9)
  8.      * 3 = click with the middle button
  9.      * 4 = click outside of the current gui window
  10.      * 5 = button pressed & hold with the cursor holding an itemstack
  11.      * 6 = double left click
  12.      */
  13.  
  14.     @Override
  15.     public ItemStack slotClick(int slotId, int button, int flags, EntityPlayer player)
  16.     {
  17.         ItemStack result = null;
  18.         ItemStack movedItemStack;
  19.         InventoryPlayer playerInventory = player.inventory;
  20.         int sizeOrID;
  21.  
  22.         if (flags == 5) // DRAGGED DISTRIBUTION
  23.         {
  24.             int l = this.getDistributeState();
  25.             this.setDistributeState(func_94532_c(button));
  26.  
  27.             if ((l != 1 || this.getDistributeState() != 2) && l != this.getDistributeState())
  28.             {
  29.                 this.func_94533_d();
  30.             } else if (playerInventory.getItemStack() == null)
  31.             {
  32.                 this.func_94533_d();
  33.             } else if (this.getDistributeState() == 0)
  34.             {
  35.                 this.setPressedKeyInRange(func_94529_b(button));
  36.  
  37.                 if (func_94528_d(this.getPressedKeyInRange()))
  38.                 {
  39.                     this.setDistributeState(1);
  40.                     this.getDistributeSlotSet().clear();
  41.                 } else
  42.                 {
  43.                     this.func_94533_d();
  44.                 }
  45.             } else if (this.getDistributeState() == 1)
  46.             {
  47.                 Slot slot = (Slot) this.inventorySlots.get(slotId);
  48.  
  49.                 if (slot != null && func_94527_a(slot, playerInventory.getItemStack(), true) &&
  50.                         slot.isItemValid(playerInventory.getItemStack()) &&
  51.                         playerInventory.getItemStack().stackSize > this.getDistributeSlotSet().size() &&
  52.                         this.canDragIntoSlot(slot))
  53.                 {
  54.                     this.getDistributeSlotSet().add(slot);
  55.                 }
  56.             } else if (this.getDistributeState() == 2)
  57.             {
  58.                 if (!this.getDistributeSlotSet().isEmpty())
  59.                 {
  60.                     movedItemStack = playerInventory.getItemStack().copy();
  61.                     sizeOrID = playerInventory.getItemStack().stackSize;
  62.                     Iterator iterator = this.getDistributeSlotSet().iterator();
  63.  
  64.                     while (iterator.hasNext())
  65.                     {
  66.                         Slot slot1 = (Slot) iterator.next();
  67.  
  68.                         if (slot1 != null && func_94527_a(slot1, playerInventory.getItemStack(), true) &&
  69.                                 slot1.isItemValid(playerInventory.getItemStack()) &&
  70.                                 playerInventory.getItemStack().stackSize >= this.getDistributeSlotSet().size() &&
  71.                                 this.canDragIntoSlot(slot1))
  72.                         {
  73.                             ItemStack itemstack1 = movedItemStack.copy();
  74.                             int j1 = slot1.getHasStack() ? slot1.getStack().stackSize : 0;
  75.                             func_94525_a(this.getDistributeSlotSet(), this.getPressedKeyInRange(), itemstack1, j1);
  76.  
  77.                             if (itemstack1.stackSize > itemstack1.getMaxStackSize())
  78.                             {
  79.                                 itemstack1.stackSize = itemstack1.getMaxStackSize();
  80.                             }
  81.  
  82.                             if (itemstack1.stackSize > slot1.getSlotStackLimit())
  83.                             {
  84.                                 itemstack1.stackSize = slot1.getSlotStackLimit();
  85.                             }
  86.  
  87.                             sizeOrID -= itemstack1.stackSize - j1;
  88.                             slot1.putStack(itemstack1);
  89.                         }
  90.                     }
  91.  
  92.                     movedItemStack.stackSize = sizeOrID;
  93.  
  94.                     if (movedItemStack.stackSize <= 0)
  95.                     {
  96.                         movedItemStack = null;
  97.                     }
  98.  
  99.                     playerInventory.setItemStack(movedItemStack);
  100.                 }
  101.  
  102.                 this.func_94533_d();
  103.             } else
  104.             {
  105.                 this.func_94533_d();
  106.             }
  107.         } else if (this.getDistributeState() != 0)
  108.         {
  109.             this.func_94533_d();
  110.         } else // NORMAL SLOTCLICK
  111.         {
  112.             Slot targetSlotCopy;
  113.             int l1;
  114.             ItemStack itemstack5;
  115.  
  116.             if ((flags == 0 || flags == 1) && (button == 0 || button == 1))
  117.             {
  118.                 // Not valid stack
  119.                 if (slotId == -999)
  120.                 {
  121.                         /*
  122.                          * on leftclick drop the complete itemstack from the inventory
  123.                          * on rightclick drop a single item from the itemstack
  124.                          */
  125.                     if (playerInventory.getItemStack() != null && slotId == -999)
  126.                     {
  127.                         if (button == 0)
  128.                         {
  129.                             player.dropPlayerItemWithRandomChoice(playerInventory.getItemStack(), true);
  130.                             playerInventory.setItemStack((ItemStack) null);
  131.                         }
  132.  
  133.                         if (button == 1)
  134.                         {
  135.                             player.dropPlayerItemWithRandomChoice(playerInventory.getItemStack().splitStack(1), true);
  136.  
  137.                             if (playerInventory.getItemStack().stackSize == 0)
  138.                             {
  139.                                 playerInventory.setItemStack((ItemStack) null);
  140.                             }
  141.                         }
  142.                     }
  143.                 } else if (flags == 1) // SHIFT-CLICK
  144.                 {
  145.                     while (true)
  146.                     {
  147.  
  148.                         if (slotId < 0) // Invalid Slot
  149.                         {
  150.                             return null;
  151.                         }
  152.  
  153.                         targetSlotCopy = (Slot) this.inventorySlots.get(slotId);
  154.  
  155.                         //if targetSlotCopy is not null and the stack inside the slot can be moved
  156.                         if (targetSlotCopy != null && targetSlotCopy.canTakeStack(player))
  157.                         {
  158.                             //transfer the picked up stack to targetSlotID in the player inverntory
  159.                             movedItemStack = this.transferStackInSlot(player, slotId);
  160.  
  161.                             //if the movedItemStack was not transferred completely
  162.                             if (movedItemStack != null)
  163.                             {
  164.                                 Item item = movedItemStack.getItem();
  165.  
  166.                                 //set the return value to the rest
  167.                                 result = movedItemStack.copy();
  168.  
  169.                                 if (targetSlotCopy.getStack() != null && targetSlotCopy.getStack().getItem() == item)
  170.                                 {
  171.                                     // Original recursive bugged MC code
  172.                                     // this.retrySlotClick(targetSlotID, mouseButtonPressed, true, player);
  173.                                     // variables are:
  174.                                     //      * [0]:: targetSlotID -> same as entry
  175.                                     //      * [1]:: mouseButtonPressed -> same as entry
  176.                                     //      * [2]:: true -> const -> 1
  177.                                     //              if (flags == 1) -> same as entry
  178.                                     //      * [3]:: player -> same as entry
  179.                                     //
  180.                                     // Final call: slotClick(slotId, button, 1, player)
  181.  
  182.                                     /* Execution path summery:
  183.                                      *  [entry]->
  184.                                      *      {{
  185.                                      *          [set only] result -> null
  186.                                      *          [set first] movedItemStack -> null
  187.                                      *          [const] playerInventory -> player.inventory
  188.                                      *          [unused] sizeOrID -> 0
  189.                                      *      }}
  190.                                      *      [if (1 == 5) -> false]
  191.                                      *      [else if (this.getDistributeState() != 0)] -> false]
  192.                                      *      [else -> true] ->
  193.                                      *          {{
  194.                                      *              [set first] targetSlotCopy -> null;
  195.                                      *              [unused] l1 -> 0;
  196.                                      *              [unused] itemstack5 -> null;
  197.                                      *          }}
  198.                                      *          [if ((flags == 0 || flags == 1) && (button == 0 || button == 1)) -> true] ->
  199.                                      *              {{}}
  200.                                      *              [if (slotId == -999) -> false]
  201.                                      *              [else if (flags == 1) -> true]
  202.                                      *                  {{loop body}}
  203.                                      *
  204.                                     */
  205.                                     continue; //retry with the shift-click
  206.                                 }
  207.                             }
  208.                         }
  209.  
  210.                         break; //Keep current route
  211.                     }
  212.                 } else //if a click with NO shift modifier is performed
  213.                 {
  214.                     if (slotId < 0)
  215.                     {
  216.                         return null;
  217.                     }
  218.  
  219.                     targetSlotCopy = (Slot) this.inventorySlots.get(slotId);
  220.  
  221.                     if (targetSlotCopy != null)
  222.                     {
  223.                         movedItemStack = targetSlotCopy.getStack();
  224.                         ItemStack itemstack4 = playerInventory.getItemStack();
  225.  
  226.                         if (movedItemStack != null)
  227.                         {
  228.                             result = movedItemStack.copy();
  229.                         }
  230.  
  231.                         if (movedItemStack == null)
  232.                         {
  233.                             if (itemstack4 != null && targetSlotCopy.isItemValid(itemstack4))
  234.                             {
  235.                                 l1 = button == 0 ? itemstack4.stackSize : 1;
  236.  
  237.                                 if (l1 > targetSlotCopy.getSlotStackLimit())
  238.                                 {
  239.                                     l1 = targetSlotCopy.getSlotStackLimit();
  240.                                 }
  241.  
  242.                                 if (itemstack4.stackSize >= l1)
  243.                                 {
  244.                                     targetSlotCopy.putStack(itemstack4.splitStack(l1));
  245.                                 }
  246.  
  247.                                 if (itemstack4.stackSize == 0)
  248.                                 {
  249.                                     playerInventory.setItemStack((ItemStack) null);
  250.                                 }
  251.                             }
  252.                         } else if (targetSlotCopy.canTakeStack(player))
  253.                         {
  254.                             if (itemstack4 == null)
  255.                             {
  256.                                 l1 = button == 0 ? movedItemStack.stackSize : (movedItemStack.stackSize + 1) / 2;
  257.                                 itemstack5 = targetSlotCopy.decrStackSize(l1);
  258.                                 playerInventory.setItemStack(itemstack5);
  259.  
  260.                                 if (movedItemStack.stackSize == 0)
  261.                                 {
  262.                                     targetSlotCopy.putStack((ItemStack) null);
  263.                                 }
  264.  
  265.                                 targetSlotCopy.onPickupFromSlot(player, playerInventory.getItemStack());
  266.                             } else if (targetSlotCopy.isItemValid(itemstack4))
  267.                             {
  268.                                 if (movedItemStack.getItem() == itemstack4.getItem() && movedItemStack.getItemDamage() == itemstack4.getItemDamage() && ItemStack.areItemStackTagsEqual(movedItemStack, itemstack4))
  269.                                 {
  270.                                     l1 = button == 0 ? itemstack4.stackSize : 1;
  271.  
  272.                                     if (l1 > targetSlotCopy.getSlotStackLimit() - movedItemStack.stackSize)
  273.                                     {
  274.                                         l1 = targetSlotCopy.getSlotStackLimit() - movedItemStack.stackSize;
  275.                                     }
  276.  
  277.                                     if (l1 > itemstack4.getMaxStackSize() - movedItemStack.stackSize)
  278.                                     {
  279.                                         l1 = itemstack4.getMaxStackSize() - movedItemStack.stackSize;
  280.                                     }
  281.  
  282.                                     itemstack4.splitStack(l1);
  283.  
  284.                                     if (itemstack4.stackSize == 0)
  285.                                     {
  286.                                         playerInventory.setItemStack((ItemStack) null);
  287.                                     }
  288.  
  289.                                     movedItemStack.stackSize += l1;
  290.                                 } else if (itemstack4.stackSize <= targetSlotCopy.getSlotStackLimit())
  291.                                 {
  292.                                     targetSlotCopy.putStack(itemstack4);
  293.                                     playerInventory.setItemStack(movedItemStack);
  294.                                 }
  295.                             } else if (movedItemStack.getItem() == itemstack4.getItem() && itemstack4.getMaxStackSize() > 1 && (!movedItemStack.getHasSubtypes() || movedItemStack.getItemDamage() == itemstack4.getItemDamage()) && ItemStack.areItemStackTagsEqual(movedItemStack, itemstack4))
  296.                             {
  297.                                 l1 = movedItemStack.stackSize;
  298.  
  299.                                 if (l1 > 0 && l1 + itemstack4.stackSize <= itemstack4.getMaxStackSize())
  300.                                 {
  301.                                     itemstack4.stackSize += l1;
  302.                                     movedItemStack = targetSlotCopy.decrStackSize(l1);
  303.  
  304.                                     if (movedItemStack.stackSize == 0)
  305.                                     {
  306.                                         targetSlotCopy.putStack((ItemStack) null);
  307.                                     }
  308.  
  309.                                     targetSlotCopy.onPickupFromSlot(player, playerInventory.getItemStack());
  310.                                 }
  311.                             }
  312.                         }
  313.  
  314.                         targetSlotCopy.onSlotChanged();
  315.                     }
  316.                 }
  317.             }
  318.             // If a hotbar key is pressed (flag == 2)
  319.             else if (flags == 2 && button >= 0 && button < 9)
  320.             {
  321.                 targetSlotCopy = (Slot) this.inventorySlots.get(slotId);
  322.  
  323.                 if (targetSlotCopy.canTakeStack(player))
  324.                 {
  325.                     movedItemStack = playerInventory.getStackInSlot(button);
  326.                     boolean flag = movedItemStack == null || targetSlotCopy.inventory == playerInventory && targetSlotCopy.isItemValid(movedItemStack);
  327.                     l1 = -1;
  328.  
  329.                     if (!flag)
  330.                     {
  331.                         l1 = playerInventory.getFirstEmptyStack();
  332.                         flag |= l1 > -1;
  333.                     }
  334.  
  335.                     if (targetSlotCopy.getHasStack() && flag)
  336.                     {
  337.                         itemstack5 = targetSlotCopy.getStack();
  338.                         playerInventory.setInventorySlotContents(button, itemstack5.copy());
  339.  
  340.                         if ((targetSlotCopy.inventory != playerInventory || !targetSlotCopy.isItemValid(movedItemStack)) && movedItemStack != null)
  341.                         {
  342.                             if (l1 > -1)
  343.                             {
  344.                                 playerInventory.addItemStackToInventory(movedItemStack);
  345.                                 targetSlotCopy.decrStackSize(itemstack5.stackSize);
  346.                                 targetSlotCopy.putStack((ItemStack) null);
  347.                                 targetSlotCopy.onPickupFromSlot(player, itemstack5);
  348.                             }
  349.                         } else
  350.                         {
  351.                             targetSlotCopy.decrStackSize(itemstack5.stackSize);
  352.                             targetSlotCopy.putStack(movedItemStack);
  353.                             targetSlotCopy.onPickupFromSlot(player, itemstack5);
  354.                         }
  355.                     } else if (!targetSlotCopy.getHasStack() && movedItemStack != null && targetSlotCopy.isItemValid(movedItemStack))
  356.                     {
  357.                         playerInventory.setInventorySlotContents(button, (ItemStack) null);
  358.                         targetSlotCopy.putStack(movedItemStack);
  359.                     }
  360.                 }
  361.             } else if (flags == 3 && player.capabilities.isCreativeMode && playerInventory.getItemStack() == null && slotId >= 0)
  362.             {
  363.                 targetSlotCopy = (Slot) this.inventorySlots.get(slotId);
  364.  
  365.                 if (targetSlotCopy != null && targetSlotCopy.getHasStack())
  366.                 {
  367.                     movedItemStack = targetSlotCopy.getStack().copy();
  368.                     movedItemStack.stackSize = movedItemStack.getMaxStackSize();
  369.                     playerInventory.setItemStack(movedItemStack);
  370.                 }
  371.             } else if (flags == 4 && playerInventory.getItemStack() == null && slotId >= 0)
  372.             {
  373.                 targetSlotCopy = (Slot) this.inventorySlots.get(slotId);
  374.  
  375.                 if (targetSlotCopy != null && targetSlotCopy.getHasStack() && targetSlotCopy.canTakeStack(player))
  376.                 {
  377.                     movedItemStack = targetSlotCopy.decrStackSize(button == 0 ? 1 : targetSlotCopy.getStack().stackSize);
  378.                     targetSlotCopy.onPickupFromSlot(player, movedItemStack);
  379.                     player.dropPlayerItemWithRandomChoice(movedItemStack, true);
  380.                 }
  381.             } else if (flags == 6 && slotId >= 0)
  382.             {
  383.                 targetSlotCopy = (Slot) this.inventorySlots.get(slotId);
  384.                 movedItemStack = playerInventory.getItemStack();
  385.  
  386.                 if (movedItemStack != null && (targetSlotCopy == null || !targetSlotCopy.getHasStack() || !targetSlotCopy.canTakeStack(player)))
  387.                 {
  388.                     sizeOrID = button == 0 ? 0 : this.inventorySlots.size() - 1;
  389.                     l1 = button == 0 ? 1 : -1;
  390.  
  391.                     for (int i2 = 0; i2 < 2; ++i2)
  392.                     {
  393.                         for (int j2 = sizeOrID; j2 >= 0 && j2 < this.inventorySlots.size() && movedItemStack.stackSize < movedItemStack.getMaxStackSize(); j2 += l1)
  394.                         {
  395.                             Slot slot3 = (Slot) this.inventorySlots.get(j2);
  396.  
  397.                             if (slot3.getHasStack() && func_94527_a(slot3, movedItemStack, true) && slot3.canTakeStack(player) && this.func_94530_a(movedItemStack, slot3) && (i2 != 0 || slot3.getStack().stackSize != slot3.getStack().getMaxStackSize()))
  398.                             {
  399.                                 int k1 = Math.min(movedItemStack.getMaxStackSize() - movedItemStack.stackSize, slot3.getStack().stackSize);
  400.                                 ItemStack itemstack2 = slot3.decrStackSize(k1);
  401.                                 movedItemStack.stackSize += k1;
  402.  
  403.                                 if (itemstack2.stackSize <= 0)
  404.                                 {
  405.                                     slot3.putStack((ItemStack) null);
  406.                                 }
  407.  
  408.                                 slot3.onPickupFromSlot(player, itemstack2);
  409.                             }
  410.                         }
  411.                     }
  412.                 }
  413.  
  414.                 this.detectAndSendChanges();
  415.             }
  416.         }
  417.  
  418.         return result;
  419.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement