From 7fc0473a72514bae0a385a386c9ab648fa64766f Mon Sep 17 00:00:00 2001 From: Andreas Troelsen Date: Sun, 9 Aug 2020 12:42:44 +0200 Subject: [PATCH] Guess Equippables from Material name suffix. This commit changes the way Equippable wrappers are created, such that they more closely match the way class chest armor pieces are "guessed". That is, instead of looking directly at the Material value of the given item, we instead look at the name _suffix_, i.e. the part after the last underscore in the name, e.g. `BOOTS` in `IRON_BOOTS`. The neat thing about this approach is that it is compatible with future items that follow the same naming convention, such as Netherite armor pieces. The downside is that it is stringly typed and not particularly "pretty", and if the naming convention breaks or new items are introduced (such as the elytra), we will have to make modifications anyway. Fixes #636 --- changelog.md | 1 + .../MobArena/things/Equippable.java | 44 +++---------------- 2 files changed, 8 insertions(+), 37 deletions(-) diff --git a/changelog.md b/changelog.md index a6afc69..45f9ccb 100644 --- a/changelog.md +++ b/changelog.md @@ -14,6 +14,7 @@ These changes will (most likely) be included in the next version. - A new `ready` state is now available for arena sign templates. Signs are in this state when all players in the lobby have readied up, but the arena has not yet started due to a start delay timer. Check the wiki for details. - Arena signs now support dynamic list entry variables for 4 different player lists. As an example, `` results in the name of a player in the lobby who hasn't readied up yet. This is useful for visualizing who is holding up the lobby. Check the wiki for details. - Elytra are now supported chest pieces in class chests. +- Elytra and Netherite armor pieces now correctly auto-equip if specified in the generic `armor` node in classes in the config-file. - Boss names now support color codes. - The Root Target ability now uses potion effects (slowness, slow falling, and negative jump boost) instead of repeated teleports. This should make for a smoother root experience. - Config-files with missing `pet-items` nodes no longer errors. A missing `pet-items` node in `global-settings` is treated as empty, i.e. no pet items will be registered. diff --git a/src/main/java/com/garbagemule/MobArena/things/Equippable.java b/src/main/java/com/garbagemule/MobArena/things/Equippable.java index dfa99d8..b25659b 100644 --- a/src/main/java/com/garbagemule/MobArena/things/Equippable.java +++ b/src/main/java/com/garbagemule/MobArena/things/Equippable.java @@ -1,45 +1,13 @@ package com.garbagemule.MobArena.things; -import org.bukkit.Material; import org.bukkit.inventory.ItemStack; -import java.util.EnumSet; - class Equippable { @FunctionalInterface interface Wrapper { ItemStackThing wrap(ItemStack stack); } - private static EnumSet helmets = EnumSet.of( - Material.LEATHER_HELMET, - Material.IRON_HELMET, - Material.CHAINMAIL_HELMET, - Material.GOLDEN_HELMET, - Material.DIAMOND_HELMET - ); - private static EnumSet chestplates = EnumSet.of( - Material.LEATHER_CHESTPLATE, - Material.IRON_CHESTPLATE, - Material.CHAINMAIL_CHESTPLATE, - Material.GOLDEN_CHESTPLATE, - Material.DIAMOND_CHESTPLATE - ); - private static EnumSet leggings = EnumSet.of( - Material.LEATHER_LEGGINGS, - Material.IRON_LEGGINGS, - Material.CHAINMAIL_LEGGINGS, - Material.GOLDEN_LEGGINGS, - Material.DIAMOND_LEGGINGS - ); - private static EnumSet boots = EnumSet.of( - Material.LEATHER_BOOTS, - Material.IRON_BOOTS, - Material.CHAINMAIL_BOOTS, - Material.GOLDEN_BOOTS, - Material.DIAMOND_BOOTS - ); - static Wrapper getWrapperByPrefix(String prefix) { if (prefix.equals("helmet")) { return HelmetThing::new; @@ -60,17 +28,19 @@ class Equippable { } static Wrapper guessWrapperFromItemStack(ItemStack stack) { - Material type = stack.getType(); - if (helmets.contains(type)) { + String name = stack.getType().name(); + String[] parts = name.split("_"); + String suffix = parts[parts.length - 1]; + if (suffix.equals("HELMET")) { return HelmetThing::new; } - if (chestplates.contains(type)) { + if (suffix.equals("CHESTPLATE") || name.equals("ELYTRA")) { return ChestplateThing::new; } - if (leggings.contains(type)) { + if (suffix.equals("LEGGINGS")) { return LeggingsThing::new; } - if (boots.contains(type)) { + if (suffix.equals("BOOTS")) { return BootsThing::new; } return null;