[UTIL] Setting maxStackSize to a Item

Discussion in 'Resources' started by unforgiven5232, Sep 17, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    unforgiven5232

    I've found this very difficult for me and i couldn't find anything to help me with this. So please leave a like and or a comment if you find this helpfull

    Code:
     
    @EventHandler
    public void PickUp(PlayerPickupItemEvent event){
     
    try {
    Field f = Item.class.getDeclaredField("maxStackSize");
    f.setAccessible(true);
    try {
    f.setInt(Item.APPLE, 3);
    } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
     
    } catch (NoSuchFieldException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
     
    }
    
    The catches look very nasty but I haven't found a way to remove them or shorten them. For the item you want to be the max size for you put Item.(The item you want) and the int that follows it is the size. Thanks for viewing this and hopefully you find this usefull.


    Edit: some items wont work such as materials so for that you need Item i = Item.byId[#]
     
  2. Offline

    Debels

    Nice, Didn't know you could do this, btw change all the catch with a multi catch so its a bit more compact


    Code:
    @EventHandler
    public void PickUp(PlayerPickupItemEvent event){
     
    try {
    Field f = Item.class.getDeclaredField("maxStackSize");
    f.setAccessible(true);
    f.setInt(Item.APPLE, 3);
    } catch (IllegalArgumentException | SecurityException | IllegalAccessException | NoSuchFieldException  e) {
    e.printStackTrace();
    }
    }
     
  3. Offline

    Loogeh


    Keep in mind that this is only available in Java 7 (I think)
     
  4. Offline

    stirante

    You can do:
    Code:
    catch (Throwable t) {}
    to catch ALL things that can be thrown.
     
  5. Offline

    CreatorTom

    You forgot some things. When you put something in a chest, it will lagg so hard. So you need to add a InventoryClickEvent to solve that
     
  6. Offline

    Loogeh

    stirante Well, I use Exception not Throwable but I was just saying it only works in Java 7 because someone who's not aware of that and doesn't have Java 7 might try it and it won't work and they will be confused as to why.
     
Thread Status:
Not open for further replies.

Share This Page