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
This commit is contained in:
Andreas Troelsen
2020-08-09 12:42:44 +02:00
parent b0969c655c
commit 7fc0473a72
2 changed files with 8 additions and 37 deletions
@@ -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<Material> helmets = EnumSet.of(
Material.LEATHER_HELMET,
Material.IRON_HELMET,
Material.CHAINMAIL_HELMET,
Material.GOLDEN_HELMET,
Material.DIAMOND_HELMET
);
private static EnumSet<Material> chestplates = EnumSet.of(
Material.LEATHER_CHESTPLATE,
Material.IRON_CHESTPLATE,
Material.CHAINMAIL_CHESTPLATE,
Material.GOLDEN_CHESTPLATE,
Material.DIAMOND_CHESTPLATE
);
private static EnumSet<Material> leggings = EnumSet.of(
Material.LEATHER_LEGGINGS,
Material.IRON_LEGGINGS,
Material.CHAINMAIL_LEGGINGS,
Material.GOLDEN_LEGGINGS,
Material.DIAMOND_LEGGINGS
);
private static EnumSet<Material> 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;