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

expose constructor

This commit is contained in:
Tech
2025-05-11 14:40:08 -04:00
parent b366f5930b
commit a1dafdee5b
5 changed files with 73 additions and 16 deletions

View File

@@ -2,33 +2,59 @@ package com.hibiscusmc.hmccosmetics.cosmetic;
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
import com.hibiscusmc.hmccosmetics.util.MessagesUtil;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import me.lojosho.hibiscuscommons.config.serializer.ItemSerializer;
import me.lojosho.shaded.configurate.ConfigurationNode;
import me.lojosho.shaded.configurate.serialize.SerializationException;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.logging.Level;
@Getter
@Setter
public abstract class Cosmetic {
protected static ItemStack UNDEFINED_DISPLAY_ITEM_STACK;
@Getter @Setter
static {
UNDEFINED_DISPLAY_ITEM_STACK = new ItemStack(Material.BARRIER);
UNDEFINED_DISPLAY_ITEM_STACK.editMeta(meta -> {
meta.displayName(Component.text("Undefined Item Display", NamedTextColor.RED));
meta.lore(List.of(
Component.text("Please check your configurations & console to", NamedTextColor.RED),
Component.text("ensure there are no errors.", NamedTextColor.RED)
));
});
}
/** Identifier of the cosmetic. */
private String id;
@Getter @Setter
/** Permission to use the cosmetic. */
private String permission;
/** The display {@link ItemStack} of the cosmetic. */
@Getter(AccessLevel.NONE) @Setter(AccessLevel.NONE)
private ItemStack item;
@Getter @Setter
/** The material string of the cosmetic. */
private String material;
@Getter @Setter
/** The {@link CosmeticSlot} this cosmetic occupies. */
private CosmeticSlot slot;
@Getter @Setter
/** Whether the cosmetic is dyeable or not. */
private boolean dyeable;
protected Cosmetic(String id, @NotNull ConfigurationNode config) {
protected Cosmetic(@NotNull String id, @NotNull ConfigurationNode config) {
this.id = id;
if (!config.node("permission").virtual()) {
@@ -39,23 +65,49 @@ public abstract class Cosmetic {
if (!config.node("item").virtual()) {
this.material = config.node("item", "material").getString();
this.item = generateItemStack(config.node("item"));
try {
this.item = generateItemStack(config.node("item"));
} catch(Exception ex) {
MessagesUtil.sendDebugMessages("Forcing %s to use undefined display".formatted(getId()));
this.item = UNDEFINED_DISPLAY_ITEM_STACK;
}
}
MessagesUtil.sendDebugMessages("Slot: " + config.node("slot").getString());
this.slot = CosmeticSlot.valueOf(config.node("slot").getString());
setSlot(CosmeticSlot.valueOf(config.node("slot").getString()));
setDyeable(config.node("dyeable").getBoolean(false));
this.dyeable = config.node("dyeable").getBoolean(false);
MessagesUtil.sendDebugMessages("Dyeable " + dyeable);
Cosmetics.addCosmetic(this);
}
protected Cosmetic(String id, String permission, ItemStack item, String material, CosmeticSlot slot, boolean dyeable) {
this.id = id;
this.permission = permission;
this.item = item;
this.material = material;
this.slot = slot;
this.dyeable = dyeable;
}
public boolean requiresPermission() {
return permission != null;
}
public abstract void update(CosmeticUser user);
/**
* Dispatched when an update is requested upon the cosmetic.
* @param user the user to preform the update against
*/
public final void update(CosmeticUser user) {
this.doUpdate(user);
}
/**
* Action preformed on the update.
* @param user the user to preform the update against
*/
protected void doUpdate(final CosmeticUser user) {
// NO-OP.
}
@Nullable
public ItemStack getItem() {
@@ -63,6 +115,11 @@ public abstract class Cosmetic {
return item.clone();
}
/**
* Generate an {@link ItemStack} from a {@link ConfigurationNode}.
* @param config the configuration node
* @return the {@link ItemStack}
*/
protected ItemStack generateItemStack(ConfigurationNode config) {
try {
ItemStack item = ItemSerializer.INSTANCE.deserialize(ItemStack.class, config);

View File

@@ -31,7 +31,7 @@ public class CosmeticArmorType extends Cosmetic {
}
@Override
public void update(@NotNull CosmeticUser user) {
protected void doUpdate(@NotNull CosmeticUser user) {
if (user.isInWardrobe()) return;
Entity entity = Bukkit.getEntity(user.getUniqueId());
if (entity == null) return;

View File

@@ -40,7 +40,7 @@ public class CosmeticBackpackType extends Cosmetic {
}
@Override
public void update(@NotNull CosmeticUser user) {
protected void doUpdate(@NotNull CosmeticUser user) {
Entity entity = user.getEntity();
if (entity == null) return;

View File

@@ -54,7 +54,7 @@ public class CosmeticBalloonType extends Cosmetic {
}
@Override
public void update(@NotNull CosmeticUser user) {
protected void doUpdate(@NotNull CosmeticUser user) {
Entity entity = Bukkit.getEntity(user.getUniqueId());
UserBalloonManager userBalloonManager = user.getBalloonManager();

View File

@@ -15,7 +15,7 @@ public class CosmeticMainhandType extends Cosmetic {
}
@Override
public void update(@NotNull CosmeticUser user) {
protected void doUpdate(@NotNull CosmeticUser user) {
Player player = user.getPlayer();
HMCCPacketManager.equipmentSlotUpdate(player.getEntityId(), user, getSlot(), HMCCPacketManager.getViewers(player.getLocation()));