From 4aa3ecc147024c2ee845fd4a5886df5d8a7c6d22 Mon Sep 17 00:00:00 2001 From: HeroBrineGoat <76707404+MasterOfTheFish@users.noreply.github.com> Date: Fri, 28 Jan 2022 17:29:25 -0500 Subject: [PATCH] Added API --- .idea/modules/HMCCosmetics.main.iml | 1 + .../hmccosmetics/api/CosmeticItem.java | 96 +++++++++++++++++++ .../hmccosmetics/api/HMCCosmeticsAPI.java | 74 ++++++++++++++ .../api/event/CosmeticChangeEvent.java | 31 ++++++ .../api/event/CosmeticItemEvent.java | 46 +++++++++ .../hmccosmetics/gui/ArmorItem.java | 4 + .../hmccosmetics/listener/ClickListener.java | 22 +++++ .../hmccosmetics/packet/PacketManager.java | 2 - .../hmccosmetics/user/UserManager.java | 11 ++- .../util/builder/ItemBuilder.java | 3 - 10 files changed, 284 insertions(+), 6 deletions(-) create mode 100644 src/main/java/io/github/fisher2911/hmccosmetics/api/CosmeticItem.java create mode 100644 src/main/java/io/github/fisher2911/hmccosmetics/api/HMCCosmeticsAPI.java create mode 100644 src/main/java/io/github/fisher2911/hmccosmetics/api/event/CosmeticChangeEvent.java create mode 100644 src/main/java/io/github/fisher2911/hmccosmetics/api/event/CosmeticItemEvent.java diff --git a/.idea/modules/HMCCosmetics.main.iml b/.idea/modules/HMCCosmetics.main.iml index 85578b10..207307e6 100644 --- a/.idea/modules/HMCCosmetics.main.iml +++ b/.idea/modules/HMCCosmetics.main.iml @@ -4,6 +4,7 @@ + SPIGOT MCP ADVENTURE diff --git a/src/main/java/io/github/fisher2911/hmccosmetics/api/CosmeticItem.java b/src/main/java/io/github/fisher2911/hmccosmetics/api/CosmeticItem.java new file mode 100644 index 00000000..60663748 --- /dev/null +++ b/src/main/java/io/github/fisher2911/hmccosmetics/api/CosmeticItem.java @@ -0,0 +1,96 @@ +package io.github.fisher2911.hmccosmetics.api; + +import io.github.fisher2911.hmccosmetics.gui.ArmorItem; +import org.bukkit.Color; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; + +import java.util.ArrayList; + +/** + * Wrapper for ArmorItem used internally for convenience and safety + */ +public class CosmeticItem { + + private final ArmorItem armorItem; + + public CosmeticItem(final ArmorItem armorItem) { + this.armorItem = armorItem; + } + + /** + * + * @param itemStack the {@link org.bukkit.inventory.ItemStack} display item + * @param id the id of the item + * @param type the cosmetic item type + * @param dyeable whether the item can be dyed + * @param rgb from Bukkit's {@link Color#asRGB()} + */ + + public CosmeticItem(final ItemStack itemStack, final String id, final ArmorItem.Type type, final boolean dyeable, final int rgb) { + this.armorItem = new ArmorItem(itemStack, id, new ArrayList<>(), "", type, dyeable, rgb); + } + + /** + * + * @param material the {@link org.bukkit.Material} display item + * @param id the id of the item + * @param type the cosmetic item type + * @param dyeable whether the item can be dyed + * @param rgb from Bukkit's {@link Color#asRGB()} + */ + + public CosmeticItem(final Material material, final String id, final ArmorItem.Type type, final boolean dyeable, final int rgb) { + this.armorItem = new ArmorItem(material, id, new ArrayList<>(), "", type, dyeable, rgb); + } + + /** + * + * @param itemStack the {@link org.bukkit.inventory.ItemStack} display item + * @param id the id of the item + * @param type the cosmetic item type + */ + + public CosmeticItem(final ItemStack itemStack, final String id, final ArmorItem.Type type) { + this(itemStack, id, type, false, -1); + } + + /** + * + * @param material the {@link org.bukkit.Material} display item + * @param id the id of the item + * @param type the cosmetic item type + */ + + public CosmeticItem(final Material material, final String id, final ArmorItem.Type type) { + this(material, id, type, false, -1); + } + + public ItemStack getColored() { + return this.armorItem.getColored(); + } + + public ItemStack getItemStack() { + return this.armorItem.getItemStack(); + } + + public String getId() { + return this.armorItem.getId(); + } + + public ArmorItem.Type getType() { + return this.armorItem.getType(); + } + + public boolean isDyeable() { + return this.armorItem.isDyeable(); + } + + public int getColor() { + return this.armorItem.getDye(); + } + + public ArmorItem getArmorItem() { + return this.armorItem; + } +} diff --git a/src/main/java/io/github/fisher2911/hmccosmetics/api/HMCCosmeticsAPI.java b/src/main/java/io/github/fisher2911/hmccosmetics/api/HMCCosmeticsAPI.java new file mode 100644 index 00000000..195ab73d --- /dev/null +++ b/src/main/java/io/github/fisher2911/hmccosmetics/api/HMCCosmeticsAPI.java @@ -0,0 +1,74 @@ +package io.github.fisher2911.hmccosmetics.api; + +import io.github.fisher2911.hmccosmetics.HMCCosmetics; +import io.github.fisher2911.hmccosmetics.gui.ArmorItem; +import io.github.fisher2911.hmccosmetics.user.User; +import io.github.fisher2911.hmccosmetics.user.UserManager; +import org.jetbrains.annotations.Nullable; + +import java.util.Optional; +import java.util.UUID; + +public class HMCCosmeticsAPI { + + private static final HMCCosmetics plugin; + + static { + plugin = HMCCosmetics.getPlugin(HMCCosmetics.class); + } + + /** + * + * This will attempt to get the {@link io.github.fisher2911.hmccosmetics.api.CosmeticItem} that the + * user is wearing. It returns an empty {@link io.github.fisher2911.hmccosmetics.api.CosmeticItem} if the user + * is not found, or if the user is not wearing a cosmetic + * @param uuid the uuid of the user + * @param type the type of cosmetic being retrieved + * @return the current cosmetic of the player + */ + public static CosmeticItem getUserCurrentItem(final UUID uuid, final ArmorItem.Type type) { + final Optional userOptional = plugin.getUserManager().get(uuid); + if (userOptional.isEmpty()) return new CosmeticItem(ArmorItem.empty(type)); + return new CosmeticItem(userOptional.get().getPlayerArmor().getItem(type)); + } + + /** + * + * @param uuid the uuid of the user whose cosmetic is being set + * @param cosmeticItem the cosmetic being set + * @return true if the cosmetic was set, or else false + */ + public static boolean setCosmeticItem(final UUID uuid, final CosmeticItem cosmeticItem) { + final UserManager userManager = plugin.getUserManager(); + final Optional userOptional = userManager.get(uuid); + if (userOptional.isEmpty()) return false; + + userManager.setItem(userOptional.get(), cosmeticItem.getArmorItem()); + return true; + } + + /** + * + * @param id the id of the cosmetic item being retrieved + * @return null if the cosmetic was not found, or a copy of the cosmetic item + */ + + @Nullable + public static CosmeticItem getCosmeticFromId(final String id) { + final ArmorItem armorItem = plugin.getCosmeticManager().getArmorItem(id); + if (armorItem == null) return null; + return new CosmeticItem(new ArmorItem(armorItem)); + } + + /** + * + * @param uuid the uuid of the user whose armor stand id is being retrieved + * @return the armor stand id, or -1 if the user is not found + */ + + public static int getUserArmorStandId(final UUID uuid) { + final Optional userOptional = plugin.getUserManager().get(uuid); + if (userOptional.isEmpty()) return -1; + return userOptional.get().getArmorStandId(); + } +} diff --git a/src/main/java/io/github/fisher2911/hmccosmetics/api/event/CosmeticChangeEvent.java b/src/main/java/io/github/fisher2911/hmccosmetics/api/event/CosmeticChangeEvent.java new file mode 100644 index 00000000..8f06b9db --- /dev/null +++ b/src/main/java/io/github/fisher2911/hmccosmetics/api/event/CosmeticChangeEvent.java @@ -0,0 +1,31 @@ +package io.github.fisher2911.hmccosmetics.api.event; + +import io.github.fisher2911.hmccosmetics.api.CosmeticItem; +import io.github.fisher2911.hmccosmetics.user.User; + +/** + * Called when a user changes their equipped cosmetic + */ +public class CosmeticChangeEvent extends CosmeticItemEvent { + + private CosmeticItem removed; + private final User user; + + public CosmeticChangeEvent(final CosmeticItem cosmeticItem, final CosmeticItem removed, final User user) { + super(cosmeticItem); + this.removed = removed; + this.user = user; + } + + public User getUser() { + return user; + } + + public CosmeticItem getRemoved() { + return removed; + } + + public void setRemoved(final CosmeticItem removed) { + this.removed = removed; + } +} diff --git a/src/main/java/io/github/fisher2911/hmccosmetics/api/event/CosmeticItemEvent.java b/src/main/java/io/github/fisher2911/hmccosmetics/api/event/CosmeticItemEvent.java new file mode 100644 index 00000000..55589e87 --- /dev/null +++ b/src/main/java/io/github/fisher2911/hmccosmetics/api/event/CosmeticItemEvent.java @@ -0,0 +1,46 @@ +package io.github.fisher2911.hmccosmetics.api.event; + +import io.github.fisher2911.hmccosmetics.api.CosmeticItem; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public abstract class CosmeticItemEvent extends Event implements Cancellable { + + private static final HandlerList HANDLERS = new HandlerList(); + + private CosmeticItem cosmeticItem; + private boolean cancelled; + + public CosmeticItemEvent(final CosmeticItem cosmeticItem) { + this.cosmeticItem = cosmeticItem; + this.cancelled = false; + } + + public CosmeticItem getCosmeticItem() { + return this.cosmeticItem; + } + + public void setCosmeticItem(final CosmeticItem cosmeticItem) { + this.cosmeticItem = cosmeticItem; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(final boolean cancelled) { + this.cancelled = cancelled; + } + + public static HandlerList getHandlerList() { + return HANDLERS; + } + + @Override + public HandlerList getHandlers() { + return HANDLERS; + } +} diff --git a/src/main/java/io/github/fisher2911/hmccosmetics/gui/ArmorItem.java b/src/main/java/io/github/fisher2911/hmccosmetics/gui/ArmorItem.java index ec1530cd..7ede3f39 100644 --- a/src/main/java/io/github/fisher2911/hmccosmetics/gui/ArmorItem.java +++ b/src/main/java/io/github/fisher2911/hmccosmetics/gui/ArmorItem.java @@ -259,6 +259,10 @@ public class ArmorItem extends GuiItem { this.action = action; } + public ArmorItem copy() { + return new ArmorItem(this); + } + public enum Type { HAT, diff --git a/src/main/java/io/github/fisher2911/hmccosmetics/listener/ClickListener.java b/src/main/java/io/github/fisher2911/hmccosmetics/listener/ClickListener.java index 4288e84f..f2f75dcf 100644 --- a/src/main/java/io/github/fisher2911/hmccosmetics/listener/ClickListener.java +++ b/src/main/java/io/github/fisher2911/hmccosmetics/listener/ClickListener.java @@ -3,10 +3,16 @@ package io.github.fisher2911.hmccosmetics.listener; import com.comphenix.protocol.wrappers.EnumWrappers; import com.destroystokyo.paper.event.player.PlayerArmorChangeEvent; import io.github.fisher2911.hmccosmetics.HMCCosmetics; +import io.github.fisher2911.hmccosmetics.api.CosmeticItem; +import io.github.fisher2911.hmccosmetics.api.event.CosmeticChangeEvent; +import io.github.fisher2911.hmccosmetics.api.event.CosmeticItemEvent; +import io.github.fisher2911.hmccosmetics.gui.ArmorItem; import io.github.fisher2911.hmccosmetics.user.User; import io.github.fisher2911.hmccosmetics.user.UserManager; +import io.github.fisher2911.hmccosmetics.util.builder.ItemBuilder; import io.th0rgal.oraxen.utils.armorequipevent.ArmorEquipEvent; import org.bukkit.Bukkit; +import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.CreatureSpawner; @@ -27,6 +33,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.spigotmc.event.entity.EntityMountEvent; +import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.Set; @@ -75,6 +82,21 @@ public class ClickListener implements Listener { this.userManager.get(player.getUniqueId()).ifPresent(this::doRunnable); } + // todo - REMOVE!!! + @EventHandler + public void onCosmeticEquip(final CosmeticChangeEvent event) { + final CosmeticItem previous = event.getCosmeticItem(); + event.setCosmeticItem( + new CosmeticItem( + ItemBuilder.from(Material.DIAMOND_BLOCK).build(), + "random-id", + previous.getType(), + true, + Color.fromRGB(50, 100, 150).asRGB() + ) + ); + } + private void fixInventory(final Player player, final Set slotsClicked, final Inventory inventory) { final Optional optionalUser = this.userManager.get(player.getUniqueId()); diff --git a/src/main/java/io/github/fisher2911/hmccosmetics/packet/PacketManager.java b/src/main/java/io/github/fisher2911/hmccosmetics/packet/PacketManager.java index d9f027e1..fd523ba8 100644 --- a/src/main/java/io/github/fisher2911/hmccosmetics/packet/PacketManager.java +++ b/src/main/java/io/github/fisher2911/hmccosmetics/packet/PacketManager.java @@ -7,7 +7,6 @@ import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.wrappers.EnumWrappers; import com.comphenix.protocol.wrappers.Pair; import it.unimi.dsi.fastutil.ints.IntArrayList; -import net.minecraft.network.protocol.Packet; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.EntityType; @@ -15,7 +14,6 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; import java.util.List; import java.util.UUID; diff --git a/src/main/java/io/github/fisher2911/hmccosmetics/user/UserManager.java b/src/main/java/io/github/fisher2911/hmccosmetics/user/UserManager.java index 44553959..d5cc5b22 100644 --- a/src/main/java/io/github/fisher2911/hmccosmetics/user/UserManager.java +++ b/src/main/java/io/github/fisher2911/hmccosmetics/user/UserManager.java @@ -4,6 +4,8 @@ import com.comphenix.protocol.wrappers.EnumWrappers; import com.comphenix.protocol.wrappers.Pair; import com.comphenix.protocol.wrappers.WrappedDataWatcher; import io.github.fisher2911.hmccosmetics.HMCCosmetics; +import io.github.fisher2911.hmccosmetics.api.CosmeticItem; +import io.github.fisher2911.hmccosmetics.api.event.CosmeticChangeEvent; import io.github.fisher2911.hmccosmetics.concurrent.Threads; import io.github.fisher2911.hmccosmetics.config.CosmeticSettings; import io.github.fisher2911.hmccosmetics.config.Settings; @@ -179,7 +181,14 @@ public class UserManager { } public void setItem(final User user, final ArmorItem armorItem) { - user.setItem(armorItem); + ArmorItem previous = user.getPlayerArmor().getItem(armorItem.getType()); + + final CosmeticChangeEvent event = + new CosmeticChangeEvent(new CosmeticItem(armorItem.copy()), new CosmeticItem(previous.copy()), user); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) return; + + user.setItem(event.getCosmeticItem().getArmorItem()); Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> { switch (armorItem.getType()) { case HAT, OFF_HAND -> this.updateCosmetics(user); diff --git a/src/main/java/io/github/fisher2911/hmccosmetics/util/builder/ItemBuilder.java b/src/main/java/io/github/fisher2911/hmccosmetics/util/builder/ItemBuilder.java index f0abbeec..7c78355d 100644 --- a/src/main/java/io/github/fisher2911/hmccosmetics/util/builder/ItemBuilder.java +++ b/src/main/java/io/github/fisher2911/hmccosmetics/util/builder/ItemBuilder.java @@ -1,10 +1,7 @@ package io.github.fisher2911.hmccosmetics.util.builder; -import io.github.fisher2911.hmccosmetics.message.Adventure; import io.github.fisher2911.hmccosmetics.message.Placeholder; -import io.github.fisher2911.hmccosmetics.util.StringUtils; import net.kyori.adventure.text.Component; -import net.minecraft.network.PacketListener; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment;