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

View File

@ -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, `<notready-1>` 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.

View File

@ -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;