9
0
mirror of https://github.com/HibiscusMC/HMCCosmetics.git synced 2025-12-19 15:09:19 +00:00

feat: migrate over to using a registry for CosmeticSlot than enums

This commit is contained in:
LoJoSho
2025-01-17 14:45:08 -06:00
parent c31d2e1aee
commit c522581d46
9 changed files with 116 additions and 80 deletions

View File

@@ -152,7 +152,7 @@ public class CosmeticCommand implements CommandExecutor {
TagResolver placeholders =
TagResolver.resolver(Placeholder.parsed("cosmetic", cosmetic.getId()),
TagResolver.resolver(Placeholder.parsed("player", player.getName())),
TagResolver.resolver(Placeholder.parsed("cosmeticslot", cosmetic.getSlot().name())));
TagResolver.resolver(Placeholder.parsed("cosmeticslot", cosmetic.getSlot().toString())));
if (!silent) MessagesUtil.sendMessage(player, "equip-cosmetic", placeholders);
@@ -187,11 +187,12 @@ public class CosmeticCommand implements CommandExecutor {
if (args[1].equalsIgnoreCase("all")) {
cosmeticSlots = user.getSlotsWithCosmetics();
} else {
if (!EnumUtils.isValidEnum(CosmeticSlot.class, args[1].toUpperCase())) {
String rawSlot = args[1].toUpperCase();
if (!CosmeticSlot.contains(rawSlot)) {
if (!silent) MessagesUtil.sendMessage(sender, "invalid-slot");
return true;
}
cosmeticSlots = Set.of(CosmeticSlot.valueOf(args[1].toUpperCase()));
cosmeticSlots = Set.of(CosmeticSlot.valueOf(rawSlot));
}
for (CosmeticSlot cosmeticSlot : cosmeticSlots) {
@@ -203,7 +204,7 @@ public class CosmeticCommand implements CommandExecutor {
TagResolver placeholders =
TagResolver.resolver(Placeholder.parsed("cosmetic", user.getCosmetic(cosmeticSlot).getId()),
TagResolver.resolver(Placeholder.parsed("player", player.getName())),
TagResolver.resolver(Placeholder.parsed("cosmeticslot", cosmeticSlot.name())));
TagResolver.resolver(Placeholder.parsed("cosmeticslot", cosmeticSlot.toString())));
if (!silent) MessagesUtil.sendMessage(player, "unequip-cosmetic", placeholders);
@@ -306,11 +307,12 @@ public class CosmeticCommand implements CommandExecutor {
return true;
}
if (!EnumUtils.isValidEnum(CosmeticSlot.class, args[1])) {
String rawSlot = args[1];
if (!CosmeticSlot.contains(rawSlot)) {
if (!silent) MessagesUtil.sendMessage(player, "invalid-slot");
return true;
}
CosmeticSlot slot = CosmeticSlot.valueOf(args[1]);
CosmeticSlot slot = CosmeticSlot.valueOf(rawSlot);
Cosmetic cosmetic = user.getCosmetic(slot);
if (args.length >= 3) {

View File

@@ -94,7 +94,7 @@ public class CosmeticCommandTabComplete implements TabCompleter {
}
case "dye" -> {
for (CosmeticSlot slot : user.getDyeableSlots()) {
completions.add(slot.name());
completions.add(slot.toString());
}
}
case "setwardrobesetting" -> {

View File

@@ -1,14 +1,58 @@
package com.hibiscusmc.hmccosmetics.cosmetic;
public enum CosmeticSlot {
HELMET,
CHESTPLATE,
LEGGINGS,
BOOTS,
MAINHAND,
OFFHAND,
BACKPACK,
BALLOON,
EMOTE,
CUSTOM
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class CosmeticSlot {
private static final ConcurrentHashMap<String, CosmeticSlot> REGISTRY = new ConcurrentHashMap<>();
public static final CosmeticSlot HELMET = new CosmeticSlot("HELMET");
public static final CosmeticSlot CHESTPLATE = new CosmeticSlot("CHESTPLATE");
public static final CosmeticSlot LEGGINGS = new CosmeticSlot("LEGGINGS");
public static final CosmeticSlot BOOTS = new CosmeticSlot("BOOTS");
public static final CosmeticSlot MAINHAND = new CosmeticSlot("MAINHAND");
public static final CosmeticSlot OFFHAND = new CosmeticSlot("OFFHAND");
public static final CosmeticSlot BACKPACK = new CosmeticSlot("BACKPACK");
public static final CosmeticSlot BALLOON = new CosmeticSlot("BALLOON");
public static final CosmeticSlot EMOTE = new CosmeticSlot("EMOTE");
public static final CosmeticSlot CUSTOM = new CosmeticSlot("CUSTOM");
private final String name;
private CosmeticSlot(@NotNull String name) {
this.name = name;
REGISTRY.put(name, this);
}
public static CosmeticSlot register(@NotNull String name) {
name = name.toUpperCase();
return REGISTRY.computeIfAbsent(name, key -> new CosmeticSlot(key));
}
public static Map<String, CosmeticSlot> values() {
return Collections.unmodifiableMap(REGISTRY);
}
public static CosmeticSlot valueOf(@NotNull String name) {
name = name.toUpperCase();
return REGISTRY.get(name);
}
/**
* Checks if the registry contains a slot with the given name.
* @param name
* @return
*/
public static boolean contains(@NotNull String name) {
name = name.toUpperCase();
return REGISTRY.containsKey(name);
}
@Override
public String toString() {
return name;
}
}

View File

@@ -94,28 +94,25 @@ public class Cosmetics {
private static void setupCosmetics(@NotNull CommentedConfigurationNode config) {
for (ConfigurationNode cosmeticConfig : config.childrenMap().values()) {
try {
String id = cosmeticConfig.key().toString();
MessagesUtil.sendDebugMessages("Attempting to add " + id);
ConfigurationNode slotNode = cosmeticConfig.node("slot");
if (slotNode.virtual()) {
MessagesUtil.sendDebugMessages("Unable to create " + id + " because there is no slot defined!", Level.WARNING);
continue;
}
if (!EnumUtils.isValidEnum(CosmeticSlot.class, slotNode.getString())) {
MessagesUtil.sendDebugMessages("Unable to create " + id + " because " + slotNode.getString() + " is not a valid slot!", Level.WARNING);
continue;
}
switch (CosmeticSlot.valueOf(slotNode.getString())) {
case BALLOON -> new CosmeticBalloonType(id, cosmeticConfig);
case BACKPACK -> new CosmeticBackpackType(id, cosmeticConfig);
case MAINHAND -> new CosmeticMainhandType(id, cosmeticConfig);
case EMOTE -> new CosmeticEmoteType(id, cosmeticConfig);
case HELMET, CHESTPLATE, LEGGINGS, BOOTS, OFFHAND -> new CosmeticArmorType(id, cosmeticConfig);
default -> new CosmeticTypeRegisterEvent(id, cosmeticConfig).callEvent();
}
} catch (Exception e) {
if (Settings.isDebugMode()) e.printStackTrace();
String id = cosmeticConfig.key().toString();
MessagesUtil.sendDebugMessages("Attempting to add " + id);
ConfigurationNode slotNode = cosmeticConfig.node("slot");
if (slotNode.virtual()) {
MessagesUtil.sendDebugMessages("Unable to create " + id + " because there is no slot defined!", Level.WARNING);
continue;
}
String slot = slotNode.getString("");
if (!CosmeticSlot.contains(slot)) {
MessagesUtil.sendDebugMessages("Unable to create " + id + " because " + slotNode.getString() + " is not a valid slot!", Level.WARNING);
continue;
}
switch (slot) {
case "BALLOON" -> new CosmeticBalloonType(id, cosmeticConfig);
case "BACKPACK" -> new CosmeticBackpackType(id, cosmeticConfig);
case "MAINHAND" -> new CosmeticMainhandType(id, cosmeticConfig);
case "EMOTE" -> new CosmeticEmoteType(id, cosmeticConfig);
case "HELMET", "CHESTPLATE", "LEGGINGS", "BOOTS", "OFFHAND" -> new CosmeticArmorType(id, cosmeticConfig);
default -> new CosmeticTypeRegisterEvent(id, cosmeticConfig).callEvent();
}
}
}

View File

@@ -13,7 +13,7 @@ public class ActionUnequip extends Action {
@Override
public void run(CosmeticUser user, String raw) {
if (!EnumUtils.isValidEnum(CosmeticSlot.class, raw)) return;
if (!CosmeticSlot.contains(raw)) return;
CosmeticSlot slot = CosmeticSlot.valueOf(raw);
user.removeCosmeticSlot(slot);

View File

@@ -122,7 +122,8 @@ public class HMCPlaceholderExpansion extends PlaceholderExpansion {
if (placeholderArgs.size() >= 2) {
String args1 = placeholderArgs.get(1);
if (EnumUtils.isValidEnum(CosmeticSlot.class, args1.toUpperCase())) {
String rawSlot = args1.toUpperCase();
if (CosmeticSlot.contains(rawSlot)) {
return TranslationUtil.getTranslation("equipped-cosmetic", String.valueOf(user.getCosmetic(CosmeticSlot.valueOf(args1.toUpperCase())) != null));
}
@@ -150,7 +151,7 @@ public class HMCPlaceholderExpansion extends PlaceholderExpansion {
if (placeholderArgs.size() >= 2) {
String args1 = placeholderArgs.get(1).toUpperCase(); // changes offhand to OFFHAND
if (!EnumUtils.isValidEnum(CosmeticSlot.class, args1)) return null;
if (!CosmeticSlot.contains(args1)) return null;
CosmeticSlot slot = CosmeticSlot.valueOf(args1);
int amount = 0;

View File

@@ -192,7 +192,7 @@ public class CosmeticUser {
public void removeCosmetics() {
// Small optimization could be made, but Concurrent modification prevents us from both getting and removing
for (CosmeticSlot slot : CosmeticSlot.values()) {
for (CosmeticSlot slot : CosmeticSlot.values().values()) {
removeCosmeticSlot(slot);
}
}

View File

@@ -314,7 +314,7 @@ public class UserWardrobeManager {
int nextyaw = HMCCServerUtils.getNextYaw(yaw, rotationSpeed);
data.set(nextyaw);
for (CosmeticSlot slot : CosmeticSlot.values()) {
for (CosmeticSlot slot : CosmeticSlot.values().values()) {
HMCCPacketManager.equipmentSlotUpdate(NPC_ID, user, slot, viewer);
}

View File

@@ -12,10 +12,24 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot.*;
public class HMCCInventoryUtils {
private static final Map<CosmeticSlot, EquipmentSlot> SLOT_MAP = new HashMap<>();
static {
SLOT_MAP.put(CosmeticSlot.HELMET, EquipmentSlot.HEAD);
SLOT_MAP.put(CosmeticSlot.CHESTPLATE, EquipmentSlot.CHEST);
SLOT_MAP.put(CosmeticSlot.LEGGINGS, EquipmentSlot.LEGS);
SLOT_MAP.put(CosmeticSlot.BOOTS, EquipmentSlot.FEET);
SLOT_MAP.put(CosmeticSlot.OFFHAND, EquipmentSlot.OFF_HAND);
SLOT_MAP.put(CosmeticSlot.MAINHAND, EquipmentSlot.HAND);
}
/**
* Converts from the Bukkit item slots to ProtocolLib item slots. Will produce a null if an improper bukkit item slot is sent through
* @param slot The BUKKIT item slot to convert.
@@ -57,9 +71,9 @@ public class HMCCInventoryUtils {
public static CosmeticSlot getItemSlotToCosmeticSlot(final EnumWrappers.ItemSlot slot) {
return switch (slot) {
case HEAD -> CosmeticSlot.HELMET;
case CHEST -> CosmeticSlot.CHESTPLATE;
case LEGS -> CosmeticSlot.LEGGINGS;
case HEAD -> HELMET;
case CHEST -> CHESTPLATE;
case LEGS -> LEGGINGS;
case FEET -> CosmeticSlot.BOOTS;
case OFFHAND -> CosmeticSlot.OFFHAND;
case MAINHAND -> CosmeticSlot.MAINHAND;
@@ -72,9 +86,9 @@ public class HMCCInventoryUtils {
case HAND -> CosmeticSlot.MAINHAND;
case OFF_HAND -> CosmeticSlot.OFFHAND;
case FEET -> CosmeticSlot.BOOTS;
case LEGS -> CosmeticSlot.LEGGINGS;
case CHEST -> CosmeticSlot.CHESTPLATE;
case HEAD -> CosmeticSlot.HELMET;
case LEGS -> LEGGINGS;
case CHEST -> CHESTPLATE;
case HEAD -> HELMET;
default -> null;
};
}
@@ -84,13 +98,13 @@ public class HMCCInventoryUtils {
public static CosmeticSlot BukkitCosmeticSlot(int slot) {
switch (slot) {
case 36 -> {
return CosmeticSlot.HELMET;
return HELMET;
}
case 37 -> {
return CosmeticSlot.CHESTPLATE;
return CHESTPLATE;
}
case 38 -> {
return CosmeticSlot.LEGGINGS;
return LEGGINGS;
}
case 39 -> {
return CosmeticSlot.BOOTS;
@@ -109,13 +123,13 @@ public class HMCCInventoryUtils {
public static CosmeticSlot NMSCosmeticSlot(int slot) {
switch (slot) {
case 5 -> {
return CosmeticSlot.HELMET;
return HELMET;
}
case 6 -> {
return CosmeticSlot.CHESTPLATE;
return CHESTPLATE;
}
case 7 -> {
return CosmeticSlot.LEGGINGS;
return LEGGINGS;
}
case 8 -> {
return CosmeticSlot.BOOTS;
@@ -132,29 +146,7 @@ public class HMCCInventoryUtils {
@Contract(pure = true)
@Nullable
public static EquipmentSlot getEquipmentSlot(@NotNull CosmeticSlot slot) {
switch (slot) {
case HELMET -> {
return EquipmentSlot.HEAD;
}
case CHESTPLATE -> {
return EquipmentSlot.CHEST;
}
case LEGGINGS -> {
return EquipmentSlot.LEGS;
}
case BOOTS -> {
return EquipmentSlot.FEET;
}
case OFFHAND -> {
return EquipmentSlot.OFF_HAND;
}
case MAINHAND -> {
return EquipmentSlot.HAND;
}
default -> {
return null;
}
}
return SLOT_MAP.get(slot);
}
public static EquipmentSlot getEquipmentSlot(@NotNull EnumWrappers.ItemSlot slot) {