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

Added API

This commit is contained in:
HeroBrineGoat
2022-01-28 17:29:25 -05:00
parent 85fc9d9056
commit 4aa3ecc147
10 changed files with 284 additions and 6 deletions

View File

@@ -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;
}
}

View File

@@ -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<User> 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<User> 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<User> userOptional = plugin.getUserManager().get(uuid);
if (userOptional.isEmpty()) return -1;
return userOptional.get().getArmorStandId();
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -259,6 +259,10 @@ public class ArmorItem extends GuiItem {
this.action = action;
}
public ArmorItem copy() {
return new ArmorItem(this);
}
public enum Type {
HAT,

View File

@@ -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<Integer> slotsClicked, final Inventory inventory) {
final Optional<User> optionalUser = this.userManager.get(player.getUniqueId());

View File

@@ -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;

View File

@@ -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);

View File

@@ -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;