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:
@@ -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) {
|
||||||
|
|||||||
@@ -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" -> {
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,7 +94,6 @@ 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");
|
||||||
@@ -102,21 +101,19 @@ public class Cosmetics {
|
|||||||
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;
|
||||||
}
|
}
|
||||||
if (!EnumUtils.isValidEnum(CosmeticSlot.class, slotNode.getString())) {
|
String slot = 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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,10 +12,24 @@ 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;
|
||||||
|
|
||||||
|
import static com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot.*;
|
||||||
|
|
||||||
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.
|
||||||
@@ -57,9 +71,9 @@ public class HMCCInventoryUtils {
|
|||||||
|
|
||||||
public static CosmeticSlot getItemSlotToCosmeticSlot(final EnumWrappers.ItemSlot slot) {
|
public static CosmeticSlot getItemSlotToCosmeticSlot(final EnumWrappers.ItemSlot slot) {
|
||||||
return switch (slot) {
|
return switch (slot) {
|
||||||
case HEAD -> CosmeticSlot.HELMET;
|
case HEAD -> HELMET;
|
||||||
case CHEST -> CosmeticSlot.CHESTPLATE;
|
case CHEST -> CHESTPLATE;
|
||||||
case LEGS -> CosmeticSlot.LEGGINGS;
|
case LEGS -> LEGGINGS;
|
||||||
case FEET -> CosmeticSlot.BOOTS;
|
case FEET -> CosmeticSlot.BOOTS;
|
||||||
case OFFHAND -> CosmeticSlot.OFFHAND;
|
case OFFHAND -> CosmeticSlot.OFFHAND;
|
||||||
case MAINHAND -> CosmeticSlot.MAINHAND;
|
case MAINHAND -> CosmeticSlot.MAINHAND;
|
||||||
@@ -72,9 +86,9 @@ public class HMCCInventoryUtils {
|
|||||||
case HAND -> CosmeticSlot.MAINHAND;
|
case HAND -> CosmeticSlot.MAINHAND;
|
||||||
case OFF_HAND -> CosmeticSlot.OFFHAND;
|
case OFF_HAND -> CosmeticSlot.OFFHAND;
|
||||||
case FEET -> CosmeticSlot.BOOTS;
|
case FEET -> CosmeticSlot.BOOTS;
|
||||||
case LEGS -> CosmeticSlot.LEGGINGS;
|
case LEGS -> LEGGINGS;
|
||||||
case CHEST -> CosmeticSlot.CHESTPLATE;
|
case CHEST -> CHESTPLATE;
|
||||||
case HEAD -> CosmeticSlot.HELMET;
|
case HEAD -> HELMET;
|
||||||
default -> null;
|
default -> null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -84,13 +98,13 @@ public class HMCCInventoryUtils {
|
|||||||
public static CosmeticSlot BukkitCosmeticSlot(int slot) {
|
public static CosmeticSlot BukkitCosmeticSlot(int slot) {
|
||||||
switch (slot) {
|
switch (slot) {
|
||||||
case 36 -> {
|
case 36 -> {
|
||||||
return CosmeticSlot.HELMET;
|
return HELMET;
|
||||||
}
|
}
|
||||||
case 37 -> {
|
case 37 -> {
|
||||||
return CosmeticSlot.CHESTPLATE;
|
return CHESTPLATE;
|
||||||
}
|
}
|
||||||
case 38 -> {
|
case 38 -> {
|
||||||
return CosmeticSlot.LEGGINGS;
|
return LEGGINGS;
|
||||||
}
|
}
|
||||||
case 39 -> {
|
case 39 -> {
|
||||||
return CosmeticSlot.BOOTS;
|
return CosmeticSlot.BOOTS;
|
||||||
@@ -109,13 +123,13 @@ public class HMCCInventoryUtils {
|
|||||||
public static CosmeticSlot NMSCosmeticSlot(int slot) {
|
public static CosmeticSlot NMSCosmeticSlot(int slot) {
|
||||||
switch (slot) {
|
switch (slot) {
|
||||||
case 5 -> {
|
case 5 -> {
|
||||||
return CosmeticSlot.HELMET;
|
return HELMET;
|
||||||
}
|
}
|
||||||
case 6 -> {
|
case 6 -> {
|
||||||
return CosmeticSlot.CHESTPLATE;
|
return CHESTPLATE;
|
||||||
}
|
}
|
||||||
case 7 -> {
|
case 7 -> {
|
||||||
return CosmeticSlot.LEGGINGS;
|
return LEGGINGS;
|
||||||
}
|
}
|
||||||
case 8 -> {
|
case 8 -> {
|
||||||
return CosmeticSlot.BOOTS;
|
return CosmeticSlot.BOOTS;
|
||||||
@@ -132,29 +146,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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user