mirror of
https://github.com/HibiscusMC/HMCCosmetics.git
synced 2025-12-19 15:09:19 +00:00
feat: rewrite CosmeticSlot to allow for a consumers to run
This commit is contained in:
@@ -11,6 +11,7 @@ import com.hibiscusmc.hmccosmetics.user.CosmeticUserProvider;
|
|||||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUsers;
|
import com.hibiscusmc.hmccosmetics.user.CosmeticUsers;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import me.lojosho.hibiscuscommons.nms.NMSHandlers;
|
import me.lojosho.hibiscuscommons.nms.NMSHandlers;
|
||||||
|
import me.lojosho.shaded.configurate.ConfigurationNode;
|
||||||
import org.bukkit.Color;
|
import org.bukkit.Color;
|
||||||
import org.jetbrains.annotations.ApiStatus;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -19,6 +20,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main API class for HMCCosmetics. This class provides methods to interact with the plugin.
|
* The main API class for HMCCosmetics. This class provides methods to interact with the plugin.
|
||||||
@@ -144,8 +146,8 @@ public final class HMCCosmeticsAPI {
|
|||||||
* @param id the id for the cosmetic slot
|
* @param id the id for the cosmetic slot
|
||||||
* @return the {@link CosmeticSlot} associated with the given id
|
* @return the {@link CosmeticSlot} associated with the given id
|
||||||
*/
|
*/
|
||||||
public static @NotNull CosmeticSlot registerCosmeticSlot(@NotNull String id) {
|
public static @NotNull CosmeticSlot registerCosmeticSlot(@NotNull String id, BiConsumer<String, ConfigurationNode> consumer) {
|
||||||
return CosmeticSlot.register(id);
|
return CosmeticSlot.register(id, consumer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,30 +1,44 @@
|
|||||||
package com.hibiscusmc.hmccosmetics.cosmetic;
|
package com.hibiscusmc.hmccosmetics.cosmetic;
|
||||||
|
|
||||||
|
import com.hibiscusmc.hmccosmetics.cosmetic.types.*;
|
||||||
|
import me.lojosho.shaded.configurate.ConfigurationNode;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class CosmeticSlot {
|
public class CosmeticSlot {
|
||||||
private static final ConcurrentHashMap<String, CosmeticSlot> REGISTRY = new ConcurrentHashMap<>();
|
private static final ConcurrentHashMap<String, CosmeticSlot> REGISTRY = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public static final CosmeticSlot HELMET = register("HELMET");
|
public static final CosmeticSlot HELMET = register("HELMET", CosmeticArmorType::new);
|
||||||
public static final CosmeticSlot CHESTPLATE = register("CHESTPLATE");
|
public static final CosmeticSlot CHESTPLATE = register("CHESTPLATE", CosmeticArmorType::new);
|
||||||
public static final CosmeticSlot LEGGINGS = register("LEGGINGS");
|
public static final CosmeticSlot LEGGINGS = register("LEGGINGS", CosmeticArmorType::new);
|
||||||
public static final CosmeticSlot BOOTS = register("BOOTS");
|
public static final CosmeticSlot BOOTS = register("BOOTS", CosmeticArmorType::new);
|
||||||
public static final CosmeticSlot MAINHAND = register("MAINHAND");
|
public static final CosmeticSlot MAINHAND = register("MAINHAND", CosmeticMainhandType::new);
|
||||||
public static final CosmeticSlot OFFHAND = register("OFFHAND");
|
public static final CosmeticSlot OFFHAND = register("OFFHAND", CosmeticArmorType::new);
|
||||||
public static final CosmeticSlot BACKPACK = register("BACKPACK");
|
public static final CosmeticSlot BACKPACK = register("BACKPACK", CosmeticBackpackType::new);
|
||||||
public static final CosmeticSlot BALLOON = register("BALLOON");
|
public static final CosmeticSlot BALLOON = register("BALLOON", CosmeticBalloonType::new);
|
||||||
public static final CosmeticSlot EMOTE = register("EMOTE");
|
public static final CosmeticSlot EMOTE = register("EMOTE", CosmeticEmoteType::new);
|
||||||
public static final CosmeticSlot CUSTOM = register("CUSTOM");
|
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
private final BiConsumer<String, ConfigurationNode> consumer;
|
||||||
|
|
||||||
private CosmeticSlot(@NotNull String name) {
|
private CosmeticSlot(@NotNull String name, @NotNull BiConsumer<String, ConfigurationNode> consumer) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
this.consumer = consumer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accepts the given id and configuration node to run the consumer relating to the ConsumerSlot
|
||||||
|
* @param id The id of the cosmetic
|
||||||
|
* @param config The configuration node of the cosmetic
|
||||||
|
*/
|
||||||
|
public void accept(@NotNull String id, @NotNull ConfigurationNode config) {
|
||||||
|
consumer.accept(id, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -33,9 +47,9 @@ public class CosmeticSlot {
|
|||||||
* @return The slot that was registered or already exists.
|
* @return The slot that was registered or already exists.
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public static CosmeticSlot register(@NotNull String name) {
|
public static CosmeticSlot register(@NotNull String name, @NotNull BiConsumer<String, ConfigurationNode> consumer) {
|
||||||
name = name.toUpperCase();
|
name = name.toUpperCase();
|
||||||
return REGISTRY.computeIfAbsent(name, key -> new CosmeticSlot(key));
|
return REGISTRY.computeIfAbsent(name, key -> new CosmeticSlot(key, consumer));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -102,18 +102,12 @@ public class Cosmetics {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
String slot = slotNode.getString("");
|
String slot = slotNode.getString("");
|
||||||
if (!CosmeticSlot.contains(slot)) {
|
CosmeticSlot cosmeticSlot = CosmeticSlot.valueOf(slot);
|
||||||
|
if (cosmeticSlot == null) {
|
||||||
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 (slot) {
|
cosmeticSlot.accept(id, cosmeticConfig);
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -28,6 +28,8 @@ import java.util.*;
|
|||||||
|
|
||||||
public class HMCCPacketManager extends PacketManager {
|
public class HMCCPacketManager extends PacketManager {
|
||||||
|
|
||||||
|
private static final List<CosmeticSlot> EQUIPMENT_SLOTS = List.of(CosmeticSlot.HELMET, CosmeticSlot.CHESTPLATE, CosmeticSlot.LEGGINGS, CosmeticSlot.BOOTS, CosmeticSlot.MAINHAND, CosmeticSlot.OFFHAND);
|
||||||
|
|
||||||
public static void sendEntitySpawnPacket(
|
public static void sendEntitySpawnPacket(
|
||||||
final @NotNull Location location,
|
final @NotNull Location location,
|
||||||
final int entityId,
|
final int entityId,
|
||||||
@@ -91,8 +93,7 @@ public class HMCCPacketManager extends PacketManager {
|
|||||||
CosmeticSlot cosmeticSlot,
|
CosmeticSlot cosmeticSlot,
|
||||||
List<Player> sendTo
|
List<Player> sendTo
|
||||||
) {
|
) {
|
||||||
if (cosmeticSlot == CosmeticSlot.BACKPACK || cosmeticSlot == CosmeticSlot.CUSTOM || cosmeticSlot == CosmeticSlot.BALLOON || cosmeticSlot == CosmeticSlot.EMOTE) return;
|
if (!EQUIPMENT_SLOTS.contains(cosmeticSlot)) return;
|
||||||
|
|
||||||
equipmentSlotUpdate(entityId, HMCCInventoryUtils.getEquipmentSlot(cosmeticSlot), user.getUserCosmeticItem(cosmeticSlot), sendTo);
|
equipmentSlotUpdate(entityId, HMCCInventoryUtils.getEquipmentSlot(cosmeticSlot), user.getUserCosmeticItem(cosmeticSlot), sendTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user