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

Merge pull request #154 from HibiscusMC/cosmeticslot_registry

Migrate CosmeticSlot to a Registry
This commit is contained in:
LoJoSho
2025-01-17 15:24:58 -06:00
committed by GitHub
9 changed files with 102 additions and 68 deletions

View File

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

View File

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

View File

@@ -1,14 +1,58 @@
package com.hibiscusmc.hmccosmetics.cosmetic; package com.hibiscusmc.hmccosmetics.cosmetic;
public enum CosmeticSlot { import org.jetbrains.annotations.NotNull;
HELMET,
CHESTPLATE, import java.util.Collections;
LEGGINGS, import java.util.Map;
BOOTS, import java.util.concurrent.ConcurrentHashMap;
MAINHAND,
OFFHAND, public class CosmeticSlot {
BACKPACK, private static final ConcurrentHashMap<String, CosmeticSlot> REGISTRY = new ConcurrentHashMap<>();
BALLOON,
EMOTE, public static final CosmeticSlot HELMET = new CosmeticSlot("HELMET");
CUSTOM 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) { private static void setupCosmetics(@NotNull CommentedConfigurationNode config) {
for (ConfigurationNode cosmeticConfig : config.childrenMap().values()) { for (ConfigurationNode cosmeticConfig : config.childrenMap().values()) {
try { String id = cosmeticConfig.key().toString();
String id = cosmeticConfig.key().toString(); MessagesUtil.sendDebugMessages("Attempting to add " + id);
MessagesUtil.sendDebugMessages("Attempting to add " + id); ConfigurationNode slotNode = cosmeticConfig.node("slot");
ConfigurationNode slotNode = cosmeticConfig.node("slot"); if (slotNode.virtual()) {
if (slotNode.virtual()) { MessagesUtil.sendDebugMessages("Unable to create " + id + " because there is no slot defined!", Level.WARNING);
MessagesUtil.sendDebugMessages("Unable to create " + id + " because there is no slot defined!", Level.WARNING); continue;
continue; }
} String slot = slotNode.getString("");
if (!EnumUtils.isValidEnum(CosmeticSlot.class, slotNode.getString())) { if (!CosmeticSlot.contains(slot)) {
MessagesUtil.sendDebugMessages("Unable to create " + id + " because " + slotNode.getString() + " is not a valid slot!", Level.WARNING); MessagesUtil.sendDebugMessages("Unable to create " + id + " because " + slotNode.getString() + " is not a valid slot!", Level.WARNING);
continue; continue;
} }
switch (CosmeticSlot.valueOf(slotNode.getString())) { switch (slot) {
case BALLOON -> new CosmeticBalloonType(id, cosmeticConfig); case "BALLOON" -> new CosmeticBalloonType(id, cosmeticConfig);
case BACKPACK -> new CosmeticBackpackType(id, cosmeticConfig); case "BACKPACK" -> new CosmeticBackpackType(id, cosmeticConfig);
case MAINHAND -> new CosmeticMainhandType(id, cosmeticConfig); case "MAINHAND" -> new CosmeticMainhandType(id, cosmeticConfig);
case EMOTE -> new CosmeticEmoteType(id, cosmeticConfig); case "EMOTE" -> new CosmeticEmoteType(id, cosmeticConfig);
case HELMET, CHESTPLATE, LEGGINGS, BOOTS, OFFHAND -> new CosmeticArmorType(id, cosmeticConfig); case "HELMET", "CHESTPLATE", "LEGGINGS", "BOOTS", "OFFHAND" -> new CosmeticArmorType(id, cosmeticConfig);
default -> new CosmeticTypeRegisterEvent(id, cosmeticConfig).callEvent(); default -> new CosmeticTypeRegisterEvent(id, cosmeticConfig).callEvent();
}
} catch (Exception e) {
if (Settings.isDebugMode()) e.printStackTrace();
} }
} }
} }

View File

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

View File

@@ -122,7 +122,8 @@ public class HMCPlaceholderExpansion extends PlaceholderExpansion {
if (placeholderArgs.size() >= 2) { if (placeholderArgs.size() >= 2) {
String args1 = placeholderArgs.get(1); 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)); 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) { if (placeholderArgs.size() >= 2) {
String args1 = placeholderArgs.get(1).toUpperCase(); // changes offhand to OFFHAND 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); CosmeticSlot slot = CosmeticSlot.valueOf(args1);
int amount = 0; int amount = 0;

View File

@@ -192,7 +192,7 @@ public class CosmeticUser {
public void removeCosmetics() { public void removeCosmetics() {
// Small optimization could be made, but Concurrent modification prevents us from both getting and removing // 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); removeCosmeticSlot(slot);
} }
} }

View File

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

View File

@@ -12,10 +12,22 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class HMCCInventoryUtils { 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 * 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. * @param slot The BUKKIT item slot to convert.
@@ -132,29 +144,7 @@ public class HMCCInventoryUtils {
@Contract(pure = true) @Contract(pure = true)
@Nullable @Nullable
public static EquipmentSlot getEquipmentSlot(@NotNull CosmeticSlot slot) { public static EquipmentSlot getEquipmentSlot(@NotNull CosmeticSlot slot) {
switch (slot) { return SLOT_MAP.get(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;
}
}
} }
public static EquipmentSlot getEquipmentSlot(@NotNull EnumWrappers.ItemSlot slot) { public static EquipmentSlot getEquipmentSlot(@NotNull EnumWrappers.ItemSlot slot) {