Deny player setting on armor in creative?

Discussion in 'Spigot Plugin Development' started by FroZenKeyboard, Jun 5, 2017.

  1. I am trying to deny the player setting on armor while the player is in creative. But it doesn't work. And yes i've registered my listener.

    Code (Text):
            @EventHandler
            public void onArmorSlot(InventoryClickEvent event) {
                if (event.getSlotType().equals(SlotType.ARMOR) && event.getWhoClicked().getGameMode() == GameMode.CREATIVE) {
                    event.setCancelled(true);
            }
            }
     
  2. Use == when checking slottype instead of #equals
     
    • Agree Agree x 1
  3. e.getPlayer().updateInventory() as suggest above,
    if you cant find the slot number, simply on inventory click event, print the clicked slot.
     
  4. I had this issue a year ago, no good solution was available so I just removed the Item afterwards.
     
  5. This will not be sufficient to deny putting on armor. There are other ways to do so. For example:

    shift+click on armor in your lower inventory
    right click while holding armor in hand
     
  6. I don't see how that would make any difference. As far as I know, .equals / .equalsIgnoreCase works more often than ==.
     
  7. Maybe by learning Java you would understand it;

    https://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java
    https://stackoverflow.com/questions/1643067/whats-the-difference-between-equals-and

    Little example

    Code (Text):

    In Java, == always just compares two references (for non-primitives, that is) - i.e. it tests whether the two operands refer to the same object.

    However, the equals method can be overridden - so two distinct objects can still be equal.

    For example:

    String x = "hello";
    String y = new String(new char[] { 'h', 'e', 'l', 'l', 'o' });

    System.out.println(x == y); // false
    System.out.println(x.equals(y)); // true
    Additionally, it's worth being aware that any two equal string constants (primarily string literals, but also combinations of string constants via concatenation) will end up referring to the same string. For example:

    String x = "hello";
    String y = "he" + "llo";
    System.out.println(x == y); // true!
    Here x and y are references to the same string, because y is a compile-time constant equal to "hello".
     
    Its the same as checking 2 diamond_chestplates but both having different names, they are both diamond_chestplate. If you check with == it will return true. But checking with #equals() will return false as they are not the same but they are the same type etc.
    == is also used to compare enums and stuff.
     
    • Agree Agree x 1
  8. It doesn't work more often, it simply can check different things. equals() can be (and is often) overwritten to compare values in a class. The equals method in Object is the same as '==' and that is what equals falls back to, if it is not overwritten.

    But for enums .equals() and '==' is the same so it is ok and (in my opinion) nicer to use '==' for enums, but you don't have to.

    .equalsIgnoreCase is for Strings
     
  9. Your point?
    I litteraly said in my last post that == doesn't work in some cases, lol. I'm well aware of the differences.

    Yeah, I was mainly refering to Strings when I said == sometimes doesn't work. If you say == is nicer/better for enums, I'll take your word on that :)
     
  10. It allways works they way it is supposed to work. But when you are comparing two ItemStacks you don't want to know whether they are the exact same object (which '==' would check for), but you want to know whether the material, the data, the displayname, the lore etc... are the same. That is what .equals() is for.
    In the case of an ItemStack .isSimilar() would be even better, but I think my example proves the point.
     
    • Agree Agree x 1
  11. like the guy above said, youll have to cancel right clicking armor event while in creative mode, shift click should be via inventoryclickevent,
    another way is via dispensers, youll hav eto cancel that too.