oxguy3

handling clicks in the creative inventory of minecraft

Dec 30th, 2011
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.10 KB | None | 0 0
  1.     /**
  2.      * An override mouse click method to add control when using the creative inv. hack
  3.      *
  4.      * @author oxguy3
  5.      */
  6.     protected void mouseClicked(int i, int j, int k)
  7.     {
  8.         if (mc.playerController.isInCreativeMode()||!mc.thePlayer.shouldShowCreativeInventory) { //if not using the hack to get creative inv, or legitimately in creative gamemode
  9.            
  10.             super.mouseClicked(i, j, k); //use the vanilla code instead of my shit
  11.            
  12.         } else { //if using the hack to get creative inv
  13.            
  14.             if (mc.thePlayer instanceof EntityPlayerSP) { //if in SSP
  15.                 //super.super.mouseClicked(i, j, k);
  16.                
  17.                 if (k == 0 || k == 1) { //if left or right click
  18.                    
  19.                     int stackSize = 64;
  20.                     if (k == 1) { //if right click
  21.                         stackSize = 1;
  22.                     }
  23.                    
  24.                     Slot cslot = super.getSlotAtCoords(i, j);
  25.                     if (cslot!=null) { //if they actually clicked a slot
  26.                        
  27.                         ItemStack slotstack = cslot.getStack();
  28.                         ItemStack pstack = mc.thePlayer.inventory.getItemStack();
  29.                        
  30.                         if (slotstack!=null) { //if there is an item in that slot
  31.                            
  32.                             if(pstack==null) { //if the player is not holding an item with the cursor
  33.                                
  34.                                 if (cslot.slotNumber>=72&&cslot.slotNumber<=80) { //if hotbar slots, use the stack's size instead of 64
  35.                                     stackSize = slotstack.stackSize;
  36.                                 }
  37.                                
  38.                                 if (k == 1 && stackSize > 1) { //if right click and there's more than 1 item, only use half of the stack
  39.                                     stackSize = stackSize/2;
  40.                                 }
  41.                                
  42.                                
  43.                                 mc.thePlayer.inventory.setItemStack(new ItemStack(slotstack.itemID, stackSize, slotstack.getItemDamage()));
  44.                                
  45.                                
  46.                                 if (cslot.slotNumber>=72&&cslot.slotNumber<=80) { //hotbar slots
  47.                                    
  48.                                     if (k == 1 && stackSize > 1) { //if right click and there's more than 1 item
  49.                                        
  50.                                         int newslotsize = slotstack.stackSize-stackSize; //take what was left after halving the stack
  51.                                         cslot.putStack(new ItemStack(slotstack.itemID, newslotsize, slotstack.getItemDamage())); //put that remainder back in the slot
  52.                                        
  53.                                     } else { //if left click
  54.                                        
  55.                                         cslot.putStack(null); //remove the stack from the slot
  56.                                     }
  57.                                 }
  58.                                
  59.                             } else { //if the player is holding an item with the cursor
  60.                                
  61.                                 if (cslot.slotNumber>=72&&cslot.slotNumber<=80) { //if clicked in the hotbar slots
  62.                                    
  63.                                     //if they're the same item type and they're stackable and the slot stack isn't already full
  64.                                     if (pstack.isItemEqual(cslot.getStack())&&pstack.isStackable()&&cslot.getStack().stackSize<64) {
  65.                                         ItemStack cstack = cslot.getStack();
  66.                                        
  67.                                         if (k == 0) { //if left click
  68.                                            
  69.                                             while (cslot.getStack().stackSize<64&&pstack.stackSize>=1) { //while the slot isnt full and the hand isnt empty
  70.                                                 pstack.stackSize-=1;
  71.                                                
  72.                                                 //N.B. the 2nd param for the ItemStack constructor should NOT be changed to cstack!!!
  73.                                                 cslot.putStack(new ItemStack(cstack.itemID, cslot.getStack().stackSize+1, cstack.getItemDamage()));
  74.                                             }
  75.                                            
  76.                                         } else { //if right click
  77.                                             pstack.stackSize-=1;
  78.                                             cslot.putStack(new ItemStack(cstack.itemID, cstack.stackSize+1, cstack.getItemDamage()));
  79.                                            
  80.                                         }
  81.                                         if (pstack.stackSize==0) { //destroy the held stack if its empty, or else it'll generate dupes
  82.                                             mc.thePlayer.inventory.setItemStack(null);
  83.                                         }
  84.                                        
  85.                                     } else { //if the items aren't the same, and should simply be swapped
  86.                                        
  87.                                         ItemStack swappy = pstack.copy();
  88.                                         mc.thePlayer.inventory.setItemStack(slotstack); //switch the held item with the slot item
  89.                                         cslot.putStack(pstack);
  90.                                     }
  91.                                    
  92.                                 } else { //if not in the hotbar slots
  93.                                    
  94.                                     if (k == 0) { //if left click
  95.                                         mc.thePlayer.inventory.setItemStack(null);
  96.                                        
  97.                                     } else { //if right click
  98.                                         mc.thePlayer.inventory.setItemStack(new ItemStack(pstack.itemID, pstack.stackSize-1, pstack.getItemDamage()));
  99.                                     }
  100.                                 }
  101.                             }
  102.                            
  103.                            
  104.                         } else { //if there is not an item in that slot
  105.                            
  106.                            
  107.                             if (pstack!=null) { //if the player is holding an item with the cursor
  108.                                
  109.                                 if (cslot.slotNumber>=72&&cslot.slotNumber<=80) { //if clicked in hotbar slots
  110.                                    
  111.                                     if (k == 0) { //if left click
  112.                                         cslot.putStack(pstack);
  113.                                         mc.thePlayer.inventory.setItemStack(null);
  114.                                        
  115.                                     } else { //if right click
  116.                                         cslot.putStack(new ItemStack(pstack.itemID, 1, pstack.getItemDamage()));
  117.                                         mc.thePlayer.inventory.setItemStack(new ItemStack(pstack.itemID, pstack.stackSize-1, pstack.getItemDamage()));
  118.                                        
  119.                                         //this code has been moved to the end of the hotbar slots IF statement
  120.                                         /*if (pstack.stackSize==0) { //destroy the held stack if its empty, or else it'll generate dupes
  121.                                             mc.thePlayer.inventory.setItemStack(null);
  122.                                         }*/
  123.                                     }
  124.                                    
  125.                                 } else { //if not in the hotbar slots
  126.                                    
  127.                                     if (k == 0) { //if left click
  128.                                         mc.thePlayer.inventory.setItemStack(null);
  129.                                        
  130.                                     } else { //if right click
  131.                                         mc.thePlayer.inventory.setItemStack(new ItemStack(pstack.itemID, pstack.stackSize-1, pstack.getItemDamage()));
  132.                                     }
  133.                                    
  134.                                 }
  135.                                
  136.                                 if (pstack.stackSize==0) { //destroy the held stack if its empty, or else it'll generate dupes
  137.                                     mc.thePlayer.inventory.setItemStack(null);
  138.                                 }
  139.                             }
  140.                         }
  141.                     }
  142.                 }
  143.                
  144.             } else { //if in SMP
  145.                 super.mouseClicked(i, j, k);
  146.                
  147.             }
  148.         }
  149.     }
Advertisement
Add Comment
Please, Sign In to add comment