Solved "Inventory element" alias

Discussion in 'Plugin Development' started by Marcoral, Aug 14, 2016.

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


    Hello everybody!
    Today my problem seems like:

    I want to make crafted tools and armor elements have a chance to be enchanted after the event.

    For example - I am crafting stone sword. When I click to my crafted item I want to have 5% chance that it is enchanted with unbreakable III.

    I know how to do it, but my problem refers to first part of code, which I'd write like this:

    public void onCraft(CraftItemEvent e) {
            if ((e.getCurrentItem().getType().equals(Material.LEATHER_HELMET) || e.getCurrentItem().getType().equals(Material.LEATHER_CHESTPLATE)) && e.getCursor().getType().equals(Material.AIR)) {
    Of course I'll have to add more items (not only Leather Helmet or Chestplate) and, as you can imagine, it would look veeery unprofessionally. I'd like to do this like
    if(e.getCurrentItem().getType() instanceof MyCustomListOfItemMaterials){
    but I simply don't know how to do it.

    Hope you'll understand me and find a sollution. Thank you!
  2. Math.random() returns a random number between 0 and 1

    So if the random number is >= 0.05 (5%) enchant your item.
  3. Offline


    Thank you for reply, but I wrote this
    For example - I am crafting stone sword. When I click to my crafted item I want to have 5% chance that it is enchanted with unbreakable III.
    just to show what am I going to do. I currently have problem with creating alias, which I could use instead of writing
    "if event item equals leather helmet or if event item equals leather chestplate or..."

    I just wish I could compare my event item with list of materials and check whether event item belongs to defined list of materials.
  4. Offline


    Still unresolved :/
  5. @Marcoral
    What you can do is use a nice little class called EnumSet and create an EnumSet of Materials. What we do is use the "EnumSet.of(<insert your materials here>)". Which will then give us a Set of said enums. What we then can do is use the "contains()" method to check if a certain Item Type is contained within the EnumSet.

    Here's an example.

    Let's say we have the following Enum:
    1. public enum MyExampleEnum {
    2. THIS,
    3. ENUM,
    4. IS,
    5. A,
    6. TEST
    7. }
    And then we create an EnumSet of 3 enum values from MyExampleEnum like this:
    1. EnumSet<MyExampleEnum> myEnumSet = EnumSet.of(THIS, IS, TEST)
    And then we have a enum value from the MyExampleEnum in the variable "enumValue". We can simply use the ".contains()" method to see if this enum is in the EnumSet.
    1. MyExampleEnum enumValue = THIS
    2. MyExampleEnum secondEnumValue = A
    3. myEnumSet.contains(enumValue) // Will be true
    4. myEnumSet.contains(secondEnumValue) // Will be false
    The first check will return true, since "THIS" is in the EnumSet, but "A" is not, so the second check will return false.
    Marcoral likes this.
Thread Status:
Not open for further replies.

Share This Page