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

feat: equipped and locked cosmetic item in gui

This commit is contained in:
LoJoSho
2023-05-05 14:38:54 -05:00
parent cfe5f33a63
commit 91361a6373
6 changed files with 74 additions and 62 deletions

View File

@@ -17,6 +17,7 @@ public abstract class Cosmetic {
private String id;
private String permission;
private ItemStack item;
private String material;
private CosmeticSlot slot;
private boolean dyable;
@@ -29,7 +30,10 @@ public abstract class Cosmetic {
this.permission = null;
}
if (!config.node("item").virtual()) this.item = generateItemStack(config.node("item"));
if (!config.node("item").virtual()) {
this.material = config.node("item", "material").getString();
this.item = generateItemStack(config.node("item"));
}
MessagesUtil.sendDebugMessages("Slot: " + config.node("slot").getString());
@@ -76,6 +80,10 @@ public abstract class Cosmetic {
return this.dyable;
}
public String getMaterial() {
return material;
}
public abstract void update(CosmeticUser user);
@Nullable

View File

@@ -118,14 +118,12 @@ public class Menu {
List<Integer> slots = getSlots(slotString);
if (slots == null) {
MessagesUtil.sendDebugMessages("Slot is null for " + config.key().toString());
continue;
}
ItemStack item;
try {
item = ItemSerializer.INSTANCE.deserialize(ItemStack.class, config.node("item"));
//item = config.node("item").get(ItemStack.class);
@@ -145,24 +143,28 @@ public class Menu {
if (Types.isType(typeId)) type = Types.getType(typeId);
}
ItemStack originalItem = item.clone();
item = updateLore(user, item, type, config);
for (int slot : slots) {
ItemStack originalItem = item.clone();
item = updateItem(user, item, type, config, slot);
GuiItem guiItem = ItemBuilder.from(item).asGuiItem();
GuiItem guiItem = ItemBuilder.from(item).asGuiItem();
Type finalType = type;
guiItem.setAction(event -> {
final ClickType clickType = event.getClick();
if (finalType != null) finalType.run(user, config, clickType);
Type finalType = type;
guiItem.setAction(event -> {
MessagesUtil.sendDebugMessages("Selected slot " + slot);
final ClickType clickType = event.getClick();
if (finalType != null) finalType.run(user, config, clickType);
for (int i : slots) {
gui.updateItem(i, updateLore(user, originalItem.clone(), finalType, config));
MessagesUtil.sendDebugMessages("Updated slot " + i);
}
});
for (int guiSlot : slots) {
gui.updateItem(guiSlot, updateItem(user, originalItem.clone(), finalType, config, guiSlot));
}
gui.update();
MessagesUtil.sendDebugMessages("Updated slot " + slot);
});
MessagesUtil.sendDebugMessages("Added " + slots + " as " + guiItem + " in the menu");
gui.setItem(slots, guiItem);
MessagesUtil.sendDebugMessages("Added " + slots + " as " + guiItem + " in the menu");
gui.setItem(slot, guiItem);
}
}
return gui;
}
@@ -195,9 +197,9 @@ public class Menu {
@Contract("_, _, _, _ -> param2")
@NotNull
private ItemStack updateLore(CosmeticUser user, @NotNull ItemStack itemStack, Type type, ConfigurationNode config) {
private ItemStack updateItem(CosmeticUser user, @NotNull ItemStack itemStack, Type type, ConfigurationNode config, int slot) {
if (itemStack.hasItemMeta()) {
itemStack.setItemMeta(type.setLore(user, config, itemStack.getItemMeta()));
itemStack = type.setItem(user, config, itemStack);
}
return itemStack;
}

View File

@@ -2,6 +2,7 @@ package com.hibiscusmc.hmccosmetics.gui.type;
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.spongepowered.configurate.ConfigurationNode;
@@ -24,5 +25,5 @@ public abstract class Type {
public abstract void run(CosmeticUser user, ConfigurationNode config, ClickType clickType);
public abstract ItemMeta setLore(CosmeticUser user, ConfigurationNode config, ItemMeta itemMeta);
public abstract ItemStack setItem(CosmeticUser user, ConfigurationNode config, ItemStack itemStack);
}

View File

@@ -1,6 +1,7 @@
package com.hibiscusmc.hmccosmetics.gui.type.types;
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
import com.hibiscusmc.hmccosmetics.config.serializer.ItemSerializer;
import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetic;
import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetics;
import com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticArmorType;
@@ -17,12 +18,14 @@ import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import java.lang.invoke.TypeDescriptor;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@@ -96,55 +99,51 @@ public class TypeCosmetic extends Type {
}
@Override
public ItemMeta setLore(CosmeticUser user, @NotNull ConfigurationNode config, ItemMeta itemMeta) {
List<String> processedLore = new ArrayList<>();
public ItemStack setItem(CosmeticUser user, @NotNull ConfigurationNode config, ItemStack itemStack) {
ItemMeta itemMeta = itemStack.getItemMeta();
if (config.node("cosmetic").virtual()) return processLoreLines(user, itemMeta);;
if (config.node("cosmetic").virtual()) {
itemStack.setItemMeta(processLoreLines(user, itemMeta));
return itemStack;
};
String cosmeticName = config.node("cosmetic").getString();
Cosmetic cosmetic = Cosmetics.getCosmetic(cosmeticName);
if (cosmetic == null) {
return processLoreLines(user, itemMeta);
itemStack.setItemMeta(processLoreLines(user, itemMeta));
return itemStack;
}
if (user.canEquipCosmetic(cosmetic)) {
return processLoreLines(user, itemMeta);
} else {
ConfigurationNode itemConfig = config.node("item");
if (itemConfig.virtual()) return itemMeta;
if (itemConfig.node("locked-name").virtual() && itemConfig.node("locked-lore").virtual()) {
return processLoreLines(user, itemMeta);
if (user.hasCosmeticInSlot(cosmetic) && !config.node("equipped-item").virtual()) {
ConfigurationNode equippedItem = config.node("equipped-item");
try {
if (equippedItem.node("material").virtual()) equippedItem.node("material").set(config.node("item", "material").getString());
} catch (SerializationException e) {
// Nothing >:)
}
try {
List<String> lockedLore = itemMeta.getLore();
String lockedName = itemMeta.getDisplayName();
if (!itemConfig.node("locked-lore").virtual()) {
lockedLore = Utils.replaceIfNull(itemConfig.node("locked-lore").getList(String.class),
new ArrayList<String>()).
stream().map(StringUtils::parseStringToString).collect(Collectors.toList());
}
if (!itemConfig.node("locked-name").virtual()) {
lockedName = StringUtils.parseStringToString(Utils.replaceIfNull(itemConfig.node("locked-name").getString(), ""));
}
if (Hooks.isActiveHook("PlaceHolderAPI")) {
lockedName = PlaceholderAPI.setPlaceholders(user.getPlayer(), lockedName);
}
itemMeta.setDisplayName(lockedName);
if (itemMeta.hasLore()) {
itemMeta.getLore().clear();
for (String loreLine : lockedLore) {
if (Hooks.isActiveHook("PlaceHolderAPI")) loreLine = PlaceholderAPI.setPlaceholders(user.getPlayer(), loreLine);
processedLore.add(loreLine);
}
}
} catch (Exception e) {
e.printStackTrace();
itemStack = ItemSerializer.INSTANCE.deserialize(ItemStack.class, equippedItem);
} catch (SerializationException e) {
throw new RuntimeException(e);
}
return itemStack;
}
itemMeta.setLore(processedLore);
return itemMeta;
if (!user.canEquipCosmetic(cosmetic) && !config.node("locked-item").virtual()) {
ConfigurationNode lockedItem = config.node("locked-item");
try {
if (lockedItem.node("material").virtual()) lockedItem.node("material").set(config.node("item", "material").getString());
} catch (SerializationException e) {
// Nothing >:)
}
try {
itemStack = ItemSerializer.INSTANCE.deserialize(ItemStack.class, lockedItem);
//item = config.node("item").get(ItemStack.class);
} catch (SerializationException e) {
throw new RuntimeException(e);
}
return itemStack;
}
return itemStack;
}
@Contract("_, _ -> param2")

View File

@@ -6,6 +6,7 @@ import com.hibiscusmc.hmccosmetics.hooks.Hooks;
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.configurate.ConfigurationNode;
@@ -53,8 +54,9 @@ public class TypeEmpty extends Type {
@Override
@SuppressWarnings("Duplicates")
public ItemMeta setLore(CosmeticUser user, ConfigurationNode config, @NotNull ItemMeta itemMeta) {
public ItemStack setItem(CosmeticUser user, ConfigurationNode config, @NotNull ItemStack itemStack) {
List<String> processedLore = new ArrayList<>();
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta.hasLore()) {
for (String loreLine : itemMeta.getLore()) {
@@ -63,8 +65,8 @@ public class TypeEmpty extends Type {
processedLore.add(loreLine);
}
}
return itemMeta;
itemStack.setItemMeta(itemMeta);
return itemStack;
}
// That's it! Now, add it as a static in another one of your classes (such as your main class) and you are good to go.

View File

@@ -157,7 +157,7 @@ public class CosmeticUser {
}
public boolean hasCosmeticInSlot(Cosmetic cosmetic) {
if (getCosmetic(cosmetic.getSlot()) == null) return true;
if (getCosmetic(cosmetic.getSlot()) == null) return false;
if (cosmetic.getId() == getCosmetic(cosmetic.getSlot()).getId()) {
return true;
}