9
0
mirror of https://github.com/HibiscusMC/HMCCosmetics.git synced 2025-12-30 20:39:13 +00:00

Move cosmetic item creation to Cosmetic

This commit is contained in:
LoJoSho
2023-01-03 16:31:09 -06:00
parent 8d190c25c8
commit 2aa6b50938
4 changed files with 25 additions and 71 deletions

View File

@@ -1,15 +1,21 @@
package com.hibiscusmc.hmccosmetics.cosmetic;
import com.hibiscusmc.hmccosmetics.config.serializer.ItemSerializer;
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
import com.hibiscusmc.hmccosmetics.util.MessagesUtil;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import java.util.logging.Level;
public class Cosmetic {
private String id;
private String permission;
private ItemStack item;
private CosmeticSlot slot;
private boolean dyable;
@@ -20,6 +26,9 @@ public class Cosmetic {
} else {
this.permission = null;
}
if (!config.node("item").virtual()) this.item = generateItemStack(config.node("item"));
MessagesUtil.sendDebugMessages("Slot: " + config.node("slot").getString());
setSlot(CosmeticSlot.valueOf(config.node("slot").getString()));
@@ -72,6 +81,20 @@ public class Cosmetic {
@Nullable
public ItemStack getItem() {
return null; // Override
return item.clone();
}
protected ItemStack generateItemStack(ConfigurationNode config) {
try {
ItemStack item = ItemSerializer.INSTANCE.deserialize(ItemStack.class, config);
if (item == null) {
MessagesUtil.sendDebugMessages("Unable to create item for " + getId(), Level.SEVERE);
return new ItemStack(Material.AIR);
}
return item;
} catch (SerializationException e) {
MessagesUtil.sendDebugMessages("Fatal error encountered for " + getId() + " regarding Serialization of item", Level.SEVERE);
throw new RuntimeException(e);
}
}
}

View File

@@ -19,13 +19,11 @@ import java.util.logging.Level;
public class CosmeticArmorType extends Cosmetic {
private ItemStack itemStack;
private EquipmentSlot equipSlot;
public CosmeticArmorType(String id, ConfigurationNode config) {
super(id, config);
this.itemStack = generateItemStack(config.node("item"));
this.equipSlot = InventoryUtils.getEquipmentSlot(getSlot());
}
@@ -35,26 +33,9 @@ public class CosmeticArmorType extends Cosmetic {
PacketManager.equipmentSlotUpdate(player, getSlot(), PlayerUtils.getNearbyPlayers(player));
}
@Override
public ItemStack getItem() {
return this.itemStack.clone();
}
public EquipmentSlot getEquipSlot() {
return this.equipSlot;
}
private ItemStack generateItemStack(ConfigurationNode config) {
try {
ItemStack item = ItemSerializer.INSTANCE.deserialize(ItemStack.class, config);
if (item == null) {
MessagesUtil.sendDebugMessages("Unable to create item for " + getId(), Level.SEVERE);
return new ItemStack(Material.AIR);
}
return item;
} catch (SerializationException e) {
MessagesUtil.sendDebugMessages("Fatal error encountered for " + getId() + " regarding Serialization of item", Level.SEVERE);
throw new RuntimeException(e);
}
}
}

View File

@@ -17,15 +17,12 @@ import java.util.logging.Level;
public class CosmeticBackpackType extends Cosmetic {
private ItemStack backpackItem;
ConfigurationNode config;
public CosmeticBackpackType(String id, ConfigurationNode config) {
super(id, config);
this.config = config;
this.backpackItem = generateItemStack(config.node("item"));
}
@Override
@@ -58,27 +55,4 @@ public class CosmeticBackpackType extends Cosmetic {
user.getBackpackEntity().setRotation(loc.getYaw(), loc.getPitch());
user.showBackpack();
}
@Override
public ItemStack getItem() {
if (this.backpackItem == null ) {
MessagesUtil.sendDebugMessages("Backpack item was null for " + getId());
this.backpackItem = generateItemStack(config.node("item"));
}
return this.backpackItem.clone();
}
private ItemStack generateItemStack(ConfigurationNode config) {
try {
ItemStack item = ItemSerializer.INSTANCE.deserialize(ItemStack.class, config);
if (item == null) {
MessagesUtil.sendDebugMessages("Unable to create item for " + getId(), Level.SEVERE);
return new ItemStack(Material.AIR);
}
return item;
} catch (SerializationException e) {
MessagesUtil.sendDebugMessages("Fatal error encountered for " + getId() + " regarding Serialization of item", Level.SEVERE);
throw new RuntimeException(e);
}
}
}

View File

@@ -16,13 +16,8 @@ import java.util.logging.Level;
public class CosmeticMainhandType extends Cosmetic {
private ItemStack itemStack;
public CosmeticMainhandType(String id, ConfigurationNode config) {
super(id, config);
this.itemStack = generateItemStack(config.node("item"));
}
@Override
@@ -32,23 +27,4 @@ public class CosmeticMainhandType extends Cosmetic {
PacketManager.equipmentSlotUpdate(player.getEntityId(), user, getSlot(), PlayerUtils.getNearbyPlayers(player));
}
private ItemStack generateItemStack(ConfigurationNode config) {
try {
ItemStack item = ItemSerializer.INSTANCE.deserialize(ItemStack.class, config);
if (item == null) {
MessagesUtil.sendDebugMessages("Unable to create item for " + getId(), Level.SEVERE);
return new ItemStack(Material.AIR);
}
return item;
} catch (SerializationException e) {
MessagesUtil.sendDebugMessages("Fatal error encountered for " + getId() + " regarding Serialization of item", Level.SEVERE);
throw new RuntimeException(e);
}
}
@Override
public ItemStack getItem() {
return itemStack.clone();
}
}