mirror of
https://github.com/HibiscusMC/HMCCosmetics.git
synced 2025-12-29 20:09:13 +00:00
Cleaned up some code
This commit is contained in:
@@ -3,6 +3,7 @@ package io.github.fisher2911.hmccosmetics.command;
|
||||
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
|
||||
import io.github.fisher2911.hmccosmetics.gui.ArmorItem;
|
||||
import io.github.fisher2911.hmccosmetics.gui.CosmeticsMenu;
|
||||
import io.github.fisher2911.hmccosmetics.message.Message;
|
||||
import io.github.fisher2911.hmccosmetics.message.MessageHandler;
|
||||
import io.github.fisher2911.hmccosmetics.message.Messages;
|
||||
import io.github.fisher2911.hmccosmetics.message.Placeholder;
|
||||
@@ -76,6 +77,7 @@ public class CosmeticsCommand extends CommandBase {
|
||||
final ArmorItem armorItem = switch (type) {
|
||||
case HAT -> user.getPlayerArmor().getHat();
|
||||
case BACKPACK -> user.getPlayerArmor().getBackpack();
|
||||
case OFF_HAND -> user.getPlayerArmor().getOffHand();
|
||||
};
|
||||
|
||||
this.cosmeticsMenu.openDyeSelectorGui(user, armorItem);
|
||||
@@ -123,34 +125,19 @@ public class CosmeticsCommand extends CommandBase {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (armorItem.getType()) {
|
||||
case BACKPACK -> {
|
||||
user.setBackpack(armorItem, this.plugin);
|
||||
this.messageHandler.sendMessage(
|
||||
player,
|
||||
Messages.SET_BACKPACK
|
||||
);
|
||||
this.messageHandler.sendMessage(
|
||||
sender,
|
||||
Messages.SET_OTHER_BACKPACK,
|
||||
Map.of(Placeholder.PLAYER, player.getName(),
|
||||
Placeholder.TYPE, id)
|
||||
);
|
||||
}
|
||||
case HAT -> {
|
||||
user.setHat(armorItem, this.plugin);
|
||||
this.messageHandler.sendMessage(
|
||||
player,
|
||||
Messages.SET_HAT
|
||||
);
|
||||
this.messageHandler.sendMessage(
|
||||
sender,
|
||||
Messages.SET_OTHER_HAT,
|
||||
Map.of(Placeholder.PLAYER, player.getName(),
|
||||
Placeholder.TYPE, id)
|
||||
);
|
||||
}
|
||||
}
|
||||
final Message setMessage = Messages.getSetMessage(armorItem.getType());
|
||||
final Message setOtherMessage = Messages.getSetOtherMessage(armorItem.getType());
|
||||
this.userManager.setItem(user, armorItem);
|
||||
this.messageHandler.sendMessage(
|
||||
player,
|
||||
setMessage
|
||||
);
|
||||
this.messageHandler.sendMessage(
|
||||
sender,
|
||||
setOtherMessage,
|
||||
Map.of(Placeholder.PLAYER, player.getName(),
|
||||
Placeholder.TYPE, id)
|
||||
);
|
||||
}
|
||||
|
||||
@SubCommand("remove")
|
||||
@@ -171,34 +158,19 @@ public class CosmeticsCommand extends CommandBase {
|
||||
try {
|
||||
final ArmorItem.Type type = ArmorItem.Type.valueOf(typeString.toUpperCase());
|
||||
|
||||
switch (type) {
|
||||
case HAT -> {
|
||||
user.removeHat(this.plugin);
|
||||
this.messageHandler.sendMessage(
|
||||
player,
|
||||
Messages.REMOVED_HAT
|
||||
);
|
||||
this.messageHandler.sendMessage(
|
||||
sender,
|
||||
Messages.SET_OTHER_HAT,
|
||||
Map.of(Placeholder.PLAYER, player.getName(),
|
||||
Placeholder.TYPE, "none")
|
||||
);
|
||||
}
|
||||
case BACKPACK -> {
|
||||
user.removeBackpack(this.plugin);
|
||||
this.messageHandler.sendMessage(
|
||||
player,
|
||||
Messages.REMOVED_BACKPACK
|
||||
);
|
||||
this.messageHandler.sendMessage(
|
||||
sender,
|
||||
Messages.SET_OTHER_BACKPACK,
|
||||
Map.of(Placeholder.PLAYER, player.getName(),
|
||||
Placeholder.TYPE, "none")
|
||||
);
|
||||
}
|
||||
}
|
||||
final Message setMessage = Messages.getSetMessage(type);
|
||||
final Message setOtherMessage = Messages.getSetOtherMessage(type);
|
||||
this.userManager.removeItem(user, type);
|
||||
this.messageHandler.sendMessage(
|
||||
player,
|
||||
setMessage
|
||||
);
|
||||
this.messageHandler.sendMessage(
|
||||
sender,
|
||||
setOtherMessage,
|
||||
Map.of(Placeholder.PLAYER, player.getName(),
|
||||
Placeholder.TYPE, "none")
|
||||
);
|
||||
} catch (final IllegalArgumentException exception) {
|
||||
this.messageHandler.sendMessage(
|
||||
player,
|
||||
|
||||
@@ -4,12 +4,11 @@ import dev.triumphteam.gui.guis.GuiItem;
|
||||
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
|
||||
import io.github.fisher2911.hmccosmetics.gui.ArmorItem;
|
||||
import io.github.fisher2911.hmccosmetics.hook.HookManager;
|
||||
import io.github.fisher2911.hmccosmetics.message.Adventure;
|
||||
import io.github.fisher2911.hmccosmetics.util.Keys;
|
||||
import io.github.fisher2911.hmccosmetics.util.StringUtils;
|
||||
import io.github.fisher2911.hmccosmetics.util.Utils;
|
||||
import io.github.fisher2911.hmccosmetics.util.builder.ItemBuilder;
|
||||
import io.github.fisher2911.hmccosmetics.util.builder.ColorBuilder;
|
||||
import io.github.fisher2911.hmccosmetics.util.builder.ItemBuilder;
|
||||
import io.github.fisher2911.hmccosmetics.util.builder.SkullBuilder;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -19,7 +18,6 @@ import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
@@ -231,7 +229,9 @@ public class ItemSerializer implements TypeSerializer<GuiItem> {
|
||||
lockedLore,
|
||||
permission,
|
||||
cosmeticType,
|
||||
dyeable);
|
||||
dyeable,
|
||||
-1
|
||||
);
|
||||
|
||||
} catch (final IllegalArgumentException exception) {
|
||||
return dev.triumphteam.gui.builder.item.ItemBuilder.from(
|
||||
|
||||
@@ -56,15 +56,11 @@ public abstract class Database {
|
||||
final String hat = playerArmor.getHat().getId();
|
||||
final String backpack = playerArmor.getBackpack().getId();
|
||||
|
||||
final int dye = user.getDye();
|
||||
|
||||
statement.setString(1, user.getUuid().toString());
|
||||
statement.setString(2, backpack);
|
||||
statement.setString(3, hat);
|
||||
statement.setInt(4, dye);
|
||||
statement.setString(5, backpack);
|
||||
statement.setString(6, hat);
|
||||
statement.setInt(7, dye);
|
||||
|
||||
statement.executeUpdate();
|
||||
} catch (final SQLException exception) {
|
||||
@@ -108,7 +104,8 @@ public abstract class Database {
|
||||
new PlayerArmor(
|
||||
hat,
|
||||
backpack,
|
||||
dye
|
||||
// todo
|
||||
null
|
||||
),
|
||||
armorStandId
|
||||
);
|
||||
|
||||
@@ -2,9 +2,12 @@ package io.github.fisher2911.hmccosmetics.gui;
|
||||
|
||||
import dev.triumphteam.gui.components.GuiAction;
|
||||
import dev.triumphteam.gui.guis.GuiItem;
|
||||
import io.github.fisher2911.hmccosmetics.util.builder.ColorBuilder;
|
||||
import io.github.fisher2911.hmccosmetics.util.builder.ItemBuilder;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -20,64 +23,7 @@ public class ArmorItem extends GuiItem {
|
||||
private final String permission;
|
||||
private final Type type;
|
||||
private boolean dyeable;
|
||||
|
||||
public ArmorItem(
|
||||
@NotNull final ItemStack itemStack,
|
||||
final GuiAction<InventoryClickEvent> action,
|
||||
final String id,
|
||||
final List<String> lockedLore,
|
||||
final String permission,
|
||||
final Type type) {
|
||||
super(itemStack, action);
|
||||
this.id = id;
|
||||
this.lockedLore = lockedLore;
|
||||
this.action = action;
|
||||
this.permission = permission;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public ArmorItem(
|
||||
@NotNull final ItemStack itemStack,
|
||||
final String id,
|
||||
final List<String> lockedLore,
|
||||
final String permission,
|
||||
final Type type) {
|
||||
super(itemStack);
|
||||
this.id = id;
|
||||
this.lockedLore = lockedLore;
|
||||
this.action = null;
|
||||
this.permission = permission;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public ArmorItem(
|
||||
@NotNull final Material material,
|
||||
final String id,
|
||||
final List<String> lockedLore,
|
||||
final String permission,
|
||||
final Type type) {
|
||||
super(material);
|
||||
this.id = id;
|
||||
this.lockedLore = lockedLore;
|
||||
this.action = null;
|
||||
this.permission = permission;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public ArmorItem(
|
||||
@NotNull final Material material,
|
||||
@Nullable final GuiAction<InventoryClickEvent> action,
|
||||
final String id,
|
||||
final List<String> lockedLore,
|
||||
final String permission,
|
||||
final Type type) {
|
||||
super(material, action);
|
||||
this.id = id;
|
||||
this.lockedLore = lockedLore;
|
||||
this.action = action;
|
||||
this.permission = permission;
|
||||
this.type = type;
|
||||
}
|
||||
private final int dye;
|
||||
|
||||
public ArmorItem(
|
||||
@NotNull final ItemStack itemStack,
|
||||
@@ -86,14 +32,15 @@ public class ArmorItem extends GuiItem {
|
||||
final List<String> lockedLore,
|
||||
final String permission,
|
||||
final Type type,
|
||||
final boolean dyeable) {
|
||||
final int dye) {
|
||||
super(itemStack, action);
|
||||
|
||||
this.id = id;
|
||||
this.lockedLore = lockedLore;
|
||||
this.action = action;
|
||||
this.permission = permission;
|
||||
this.type = type;
|
||||
this.dyeable = dyeable;
|
||||
this.dye = dye;
|
||||
}
|
||||
|
||||
public ArmorItem(
|
||||
@@ -102,14 +49,14 @@ public class ArmorItem extends GuiItem {
|
||||
final List<String> lockedLore,
|
||||
final String permission,
|
||||
final Type type,
|
||||
final boolean dyeable) {
|
||||
final int dye) {
|
||||
super(itemStack);
|
||||
this.id = id;
|
||||
this.lockedLore = lockedLore;
|
||||
this.action = null;
|
||||
this.permission = permission;
|
||||
this.type = type;
|
||||
this.dyeable = dyeable;
|
||||
this.dye = dye;
|
||||
}
|
||||
|
||||
public ArmorItem(
|
||||
@@ -118,14 +65,14 @@ public class ArmorItem extends GuiItem {
|
||||
final List<String> lockedLore,
|
||||
final String permission,
|
||||
final Type type,
|
||||
final boolean dyeable) {
|
||||
final int dye) {
|
||||
super(material);
|
||||
this.id = id;
|
||||
this.lockedLore = lockedLore;
|
||||
this.action = null;
|
||||
this.permission = permission;
|
||||
this.type = type;
|
||||
this.dyeable = dyeable;
|
||||
this.dye = dye;
|
||||
}
|
||||
|
||||
public ArmorItem(
|
||||
@@ -135,7 +82,80 @@ public class ArmorItem extends GuiItem {
|
||||
final List<String> lockedLore,
|
||||
final String permission,
|
||||
final Type type,
|
||||
final boolean dyeable) {
|
||||
final int dye) {
|
||||
super(material, action);
|
||||
this.id = id;
|
||||
this.lockedLore = lockedLore;
|
||||
this.action = action;
|
||||
this.permission = permission;
|
||||
this.type = type;
|
||||
this.dye = dye;
|
||||
}
|
||||
|
||||
public ArmorItem(
|
||||
@NotNull final ItemStack itemStack,
|
||||
final GuiAction<InventoryClickEvent> action,
|
||||
final String id,
|
||||
final List<String> lockedLore,
|
||||
final String permission,
|
||||
final Type type,
|
||||
final boolean dyeable,
|
||||
final int dye) {
|
||||
super(itemStack, action);
|
||||
this.id = id;
|
||||
this.lockedLore = lockedLore;
|
||||
this.action = action;
|
||||
this.permission = permission;
|
||||
this.type = type;
|
||||
this.dyeable = dyeable;
|
||||
this.dye = dye;
|
||||
}
|
||||
|
||||
public ArmorItem(
|
||||
@NotNull final ItemStack itemStack,
|
||||
final String id,
|
||||
final List<String> lockedLore,
|
||||
final String permission,
|
||||
final Type type,
|
||||
final boolean dyeable,
|
||||
final int dye) {
|
||||
super(itemStack);
|
||||
this.id = id;
|
||||
this.lockedLore = lockedLore;
|
||||
this.action = null;
|
||||
this.permission = permission;
|
||||
this.type = type;
|
||||
this.dyeable = dyeable;
|
||||
this.dye = dye;
|
||||
}
|
||||
|
||||
public ArmorItem(
|
||||
@NotNull final Material material,
|
||||
final String id,
|
||||
final List<String> lockedLore,
|
||||
final String permission,
|
||||
final Type type,
|
||||
final boolean dyeable,
|
||||
final int dye) {
|
||||
super(material);
|
||||
this.id = id;
|
||||
this.lockedLore = lockedLore;
|
||||
this.action = null;
|
||||
this.permission = permission;
|
||||
this.type = type;
|
||||
this.dyeable = dyeable;
|
||||
this.dye = dye;
|
||||
}
|
||||
|
||||
public ArmorItem(
|
||||
@NotNull final Material material,
|
||||
@Nullable final GuiAction<InventoryClickEvent> action,
|
||||
final String id,
|
||||
final List<String> lockedLore,
|
||||
final String permission,
|
||||
final Type type,
|
||||
final boolean dyeable,
|
||||
final int dye) {
|
||||
super(material, action);
|
||||
this.id = id;
|
||||
this.lockedLore = lockedLore;
|
||||
@@ -143,6 +163,7 @@ public class ArmorItem extends GuiItem {
|
||||
this.permission = permission;
|
||||
this.type = type;
|
||||
this.dyeable = dyeable;
|
||||
this.dye = dye;
|
||||
}
|
||||
|
||||
public static ArmorItem empty(final Type type) {
|
||||
@@ -151,7 +172,8 @@ public class ArmorItem extends GuiItem {
|
||||
"",
|
||||
new ArrayList<>(),
|
||||
"",
|
||||
type
|
||||
type,
|
||||
-1
|
||||
);
|
||||
}
|
||||
|
||||
@@ -179,21 +201,45 @@ public class ArmorItem extends GuiItem {
|
||||
return dyeable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getItemStack() {
|
||||
return this.color(super.getItemStack());
|
||||
}
|
||||
|
||||
public ItemStack getItemStack(final boolean allowed) {
|
||||
final ItemStack itemStack;
|
||||
|
||||
if (allowed) {
|
||||
return this.getItemStack();
|
||||
itemStack = super.getItemStack();
|
||||
} else {
|
||||
itemStack = ItemBuilder.from(this.getItemStack()).
|
||||
lore(this.lockedLore).
|
||||
build();
|
||||
}
|
||||
|
||||
return ItemBuilder.from(this.getItemStack()).
|
||||
lore(lockedLore).
|
||||
return this.color(itemStack);
|
||||
}
|
||||
|
||||
private ItemStack color(final ItemStack itemStack) {
|
||||
if (this.dye == -1 || !ColorBuilder.canBeColored(itemStack)) {
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
return ColorBuilder.from(itemStack).
|
||||
color(Color.fromRGB(this.dye)).
|
||||
build();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return this.getItemStack().getType() == Material.AIR;
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
|
||||
HAT,
|
||||
|
||||
BACKPACK
|
||||
BACKPACK,
|
||||
|
||||
OFF_HAND
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import io.github.fisher2911.hmccosmetics.message.MessageHandler;
|
||||
import io.github.fisher2911.hmccosmetics.message.Messages;
|
||||
import io.github.fisher2911.hmccosmetics.message.Placeholder;
|
||||
import io.github.fisher2911.hmccosmetics.user.User;
|
||||
import io.github.fisher2911.hmccosmetics.user.UserManager;
|
||||
import io.github.fisher2911.hmccosmetics.util.StringUtils;
|
||||
import io.github.fisher2911.hmccosmetics.util.builder.ItemBuilder;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -26,6 +27,7 @@ import java.util.Optional;
|
||||
public class CosmeticGui {
|
||||
|
||||
protected final HMCCosmetics plugin;
|
||||
protected final UserManager userManager;
|
||||
protected final MessageHandler messageHandler;
|
||||
protected final String title;
|
||||
protected final int rows;
|
||||
@@ -39,6 +41,7 @@ public class CosmeticGui {
|
||||
final int rows,
|
||||
final Map<Integer, GuiItem> guiItemMap) {
|
||||
this.plugin = plugin;
|
||||
this.userManager = this.plugin.getUserManager();
|
||||
this.messageHandler = this.plugin.getMessageHandler();
|
||||
this.title = title;
|
||||
this.rows = rows;
|
||||
@@ -76,12 +79,14 @@ public class CosmeticGui {
|
||||
|
||||
final ArmorItem hat = playerArmor.getHat();
|
||||
final ArmorItem backpack = playerArmor.getBackpack();
|
||||
final ArmorItem offHand = playerArmor.getOffHand();
|
||||
|
||||
final ArmorItem.Type type = armorItem.getType();
|
||||
|
||||
final String id = switch (type) {
|
||||
case HAT -> hat.getId();
|
||||
case BACKPACK -> backpack.getId();
|
||||
case OFF_HAND -> offHand.getId();
|
||||
};
|
||||
|
||||
placeholders.put(
|
||||
@@ -140,20 +145,13 @@ public class CosmeticGui {
|
||||
|
||||
final ArmorItem.Type type = armorItem.getType();
|
||||
|
||||
switch (type) {
|
||||
case HAT -> {
|
||||
final boolean set = user.setOrUnsetHat(armorItem, this.messageHandler, this.plugin);
|
||||
if (set) {
|
||||
actionIfSet.execute(event);
|
||||
}
|
||||
}
|
||||
case BACKPACK -> {
|
||||
final boolean set = user.setOrUnsetBackpack(armorItem, this.messageHandler, this.plugin);
|
||||
if (set) {
|
||||
actionIfSet.execute(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
final ArmorItem setTo = this.userManager.setOrUnset(
|
||||
user,
|
||||
armorItem,
|
||||
Messages.getSetMessage(type),
|
||||
Messages.getRemovedMessage(type));
|
||||
if (setTo.isEmpty()) return;
|
||||
actionIfSet.execute(event);
|
||||
}
|
||||
|
||||
public void open(final HumanEntity humanEntity) {
|
||||
|
||||
@@ -67,24 +67,7 @@ public class DyeSelectorGui extends CosmeticGui {
|
||||
return;
|
||||
}
|
||||
|
||||
final ItemStack itemStack = switch (type) {
|
||||
case HAT -> {
|
||||
final ArmorItem hatItem = playerArmor.getHat();
|
||||
|
||||
if (hatItem == null) {
|
||||
yield null;
|
||||
}
|
||||
yield hatItem.getItemStack();
|
||||
}
|
||||
case BACKPACK -> {
|
||||
final ArmorItem backpackItem = playerArmor.getBackpack();
|
||||
|
||||
if (backpackItem == null) {
|
||||
yield null;
|
||||
}
|
||||
yield backpackItem.getItemStack();
|
||||
}
|
||||
};
|
||||
final ItemStack itemStack = playerArmor.getItem(type).getItemStack();
|
||||
|
||||
if (itemStack == null) {
|
||||
return;
|
||||
@@ -96,17 +79,11 @@ public class DyeSelectorGui extends CosmeticGui {
|
||||
|
||||
final GuiItem guiItem = this.guiItemMap.get(event.getSlot());
|
||||
|
||||
if (!(guiItem instanceof final ColorItem colorItem)) {
|
||||
if (!(guiItem instanceof ColorItem)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Color color = colorItem.getColor();
|
||||
user.setDye(color.asRGB());
|
||||
|
||||
switch (type) {
|
||||
case HAT -> user.setHat(armorItem, this.plugin);
|
||||
case BACKPACK -> user.setBackpack(armorItem, this.plugin);
|
||||
}
|
||||
this.userManager.setItem(user, armorItem);
|
||||
});
|
||||
|
||||
return gui;
|
||||
@@ -115,6 +92,7 @@ public class DyeSelectorGui extends CosmeticGui {
|
||||
@Override
|
||||
public void open(final HumanEntity player) {
|
||||
final Optional<User> optionalUser = this.plugin.getUserManager().get(player.getUniqueId());
|
||||
optionalUser.ifPresent(user -> this.getGui(user, user.getLastSetItem()).open(player));
|
||||
// todo
|
||||
// optionalUser.ifPresent(user -> this.getGui(user, user.getLastSetItem()).open(player));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,78 +7,57 @@ import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PlayerArmor {
|
||||
|
||||
private ArmorItem hat;
|
||||
private ArmorItem backpack;
|
||||
private int dye;
|
||||
private final Map<ArmorItem.Type, ArmorItem> armorItems;
|
||||
|
||||
public PlayerArmor(ArmorItem hat, final ArmorItem backpack, final int dye) {
|
||||
this.dye = dye;
|
||||
this.setHat(hat);
|
||||
this.backpack = backpack;
|
||||
public PlayerArmor(ArmorItem hat, final ArmorItem backpack, final ArmorItem offHand) {
|
||||
this.armorItems = new EnumMap<>(ArmorItem.Type.class);
|
||||
this.armorItems.put(hat.getType(), hat);
|
||||
this.armorItems.put(backpack.getType(), hat);
|
||||
this.armorItems.put(offHand.getType(), offHand);
|
||||
}
|
||||
|
||||
public static PlayerArmor empty() {
|
||||
return new PlayerArmor(
|
||||
new ArmorItem(
|
||||
new ItemStack(Material.AIR),
|
||||
"",
|
||||
new ArrayList<>(),
|
||||
"",
|
||||
ArmorItem.Type.HAT
|
||||
),
|
||||
new ArmorItem(
|
||||
new ItemStack(Material.AIR),
|
||||
"",
|
||||
new ArrayList<>(),
|
||||
"",
|
||||
ArmorItem.Type.BACKPACK
|
||||
),
|
||||
-1);
|
||||
}
|
||||
|
||||
public ArmorItem getHat() {
|
||||
return hat;
|
||||
}
|
||||
|
||||
public void setHat(final ArmorItem hat) {
|
||||
this.hat = this.color(hat);
|
||||
}
|
||||
|
||||
public ArmorItem getBackpack() {
|
||||
return backpack;
|
||||
}
|
||||
|
||||
public void setBackpack(final ArmorItem backpack) {
|
||||
this.backpack = this.color(backpack);
|
||||
}
|
||||
|
||||
private ArmorItem color(final ArmorItem armorItem) {
|
||||
if (this.dye == -1 || !ColorBuilder.canBeColored(armorItem.getItemStack())) {
|
||||
return armorItem;
|
||||
}
|
||||
|
||||
final ColorBuilder colorBuilder =
|
||||
ColorBuilder.from(armorItem.getItemStack()).
|
||||
color(Color.fromRGB(this.dye));
|
||||
return new ArmorItem(
|
||||
colorBuilder.build(),
|
||||
armorItem.getAction(),
|
||||
armorItem.getId(),
|
||||
armorItem.getLockedLore(),
|
||||
armorItem.getPermission(),
|
||||
armorItem.getType(),
|
||||
armorItem.isDyeable()
|
||||
ArmorItem.empty(ArmorItem.Type.HAT),
|
||||
ArmorItem.empty(ArmorItem.Type.BACKPACK),
|
||||
ArmorItem.empty(ArmorItem.Type.OFF_HAND)
|
||||
);
|
||||
}
|
||||
|
||||
public int getDye() {
|
||||
return this.dye;
|
||||
public ArmorItem getHat() {
|
||||
return this.armorItems.get(ArmorItem.Type.HAT);
|
||||
}
|
||||
|
||||
public void setDye(final int dye) {
|
||||
this.dye = dye;
|
||||
public void setHat(final ArmorItem hat) {
|
||||
this.armorItems.put(ArmorItem.Type.HAT, hat);
|
||||
}
|
||||
|
||||
public ArmorItem getBackpack() {
|
||||
return this.armorItems.get(ArmorItem.Type.BACKPACK);
|
||||
}
|
||||
|
||||
public void setBackpack(final ArmorItem backpack) {
|
||||
this.armorItems.put(ArmorItem.Type.BACKPACK, backpack);
|
||||
}
|
||||
|
||||
public ArmorItem getOffHand() {
|
||||
return this.armorItems.get(ArmorItem.Type.OFF_HAND);
|
||||
}
|
||||
|
||||
public void setOffHand(final ArmorItem offHand) {
|
||||
this.armorItems.put(ArmorItem.Type.OFF_HAND, offHand);
|
||||
}
|
||||
|
||||
public ArmorItem getItem(final ArmorItem.Type type) {
|
||||
return this.armorItems.get(type);
|
||||
}
|
||||
|
||||
public ArmorItem setItem(final ArmorItem armorItem) {
|
||||
return this.armorItems.put(armorItem.getType(), armorItem);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,7 @@ public class RespawnListener implements Listener {
|
||||
Bukkit.getScheduler().runTaskLater(this.plugin, () -> {
|
||||
final Player player = event.getPlayer();
|
||||
final Optional<User> optionalUser = this.userManager.get(player.getUniqueId());
|
||||
|
||||
optionalUser.ifPresent(user -> user.setHat(user.getPlayerArmor().getHat(), this.plugin));
|
||||
optionalUser.ifPresent(user -> this.userManager.setItem(user, user.getPlayerArmor().getHat()));
|
||||
}, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.fisher2911.hmccosmetics.message;
|
||||
|
||||
import io.github.fisher2911.hmccosmetics.gui.ArmorItem;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
public class Messages {
|
||||
@@ -16,6 +17,10 @@ public class Messages {
|
||||
new Message("set-backpack", "Set backpack");
|
||||
public static final Message REMOVED_BACKPACK =
|
||||
new Message("removed-backpack", "Removed backpack");
|
||||
public static final Message SET_OFF_HAND =
|
||||
new Message("set-off-hand", "Set off hand");
|
||||
public static final Message REMOVED_OFF_HAND =
|
||||
new Message("removed-off-hand", "Removed off hand");
|
||||
public static final Message MUST_BE_PLAYER =
|
||||
new Message("must-be-player", "You must be a player to do this!");
|
||||
public static final Message RELOADED =
|
||||
@@ -44,7 +49,36 @@ public class Messages {
|
||||
Placeholder.PLAYER + " to " + Placeholder.TYPE + "."
|
||||
);
|
||||
public static final Message SET_OTHER_HAT = new Message(
|
||||
"set-other-backpack", ChatColor.GREEN + "You have set the helment of " +
|
||||
"set-other-backpack", ChatColor.GREEN + "You have set the helmet of " +
|
||||
Placeholder.PLAYER + " to " + Placeholder.TYPE + "."
|
||||
);
|
||||
public static final Message SET_OTHER_OFF_HAND = new Message(
|
||||
"set-other-off-hand", ChatColor.GREEN + "You have set the off hand of " +
|
||||
Placeholder.PLAYER + " to " + Placeholder.TYPE + "."
|
||||
);
|
||||
|
||||
public static Message getSetMessage(final ArmorItem.Type type) {
|
||||
return switch (type) {
|
||||
case HAT -> Messages.SET_HAT;
|
||||
case BACKPACK -> Messages.SET_BACKPACK;
|
||||
case OFF_HAND -> Messages.SET_OFF_HAND;
|
||||
};
|
||||
}
|
||||
|
||||
public static Message getRemovedMessage(final ArmorItem.Type type) {
|
||||
return switch (type) {
|
||||
case HAT -> Messages.REMOVED_HAT;
|
||||
case BACKPACK -> Messages.REMOVED_BACKPACK;
|
||||
case OFF_HAND -> Messages.REMOVED_OFF_HAND;
|
||||
};
|
||||
}
|
||||
|
||||
public static Message getSetOtherMessage(final ArmorItem.Type type) {
|
||||
return switch (type) {
|
||||
case HAT -> Messages.SET_OTHER_HAT;
|
||||
case BACKPACK -> Messages.SET_OTHER_BACKPACK;
|
||||
case OFF_HAND -> Messages.SET_OTHER_OFF_HAND;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,19 +7,13 @@ import com.comphenix.protocol.events.PacketContainer;
|
||||
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.gui.ArmorItem;
|
||||
import io.github.fisher2911.hmccosmetics.inventory.PlayerArmor;
|
||||
import io.github.fisher2911.hmccosmetics.message.MessageHandler;
|
||||
import io.github.fisher2911.hmccosmetics.message.Messages;
|
||||
import io.github.fisher2911.hmccosmetics.message.Placeholder;
|
||||
import io.github.fisher2911.hmccosmetics.util.builder.ColorBuilder;
|
||||
import io.github.fisher2911.hmccosmetics.util.builder.ItemBuilder;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -35,7 +29,6 @@ public class User {
|
||||
|
||||
private final UUID uuid;
|
||||
private final PlayerArmor playerArmor;
|
||||
private ArmorItem lastSetItem;
|
||||
|
||||
private boolean hasArmorStand;
|
||||
private final int armorStandId;
|
||||
@@ -44,7 +37,6 @@ public class User {
|
||||
this.uuid = uuid;
|
||||
this.playerArmor = playerArmor;
|
||||
this.armorStandId = armorStandId;
|
||||
this.lastSetItem = playerArmor.getHat();
|
||||
}
|
||||
|
||||
public @Nullable Player getPlayer() {
|
||||
@@ -59,106 +51,30 @@ public class User {
|
||||
return playerArmor;
|
||||
}
|
||||
|
||||
public void setPlayerArmor(final PlayerArmor playerArmor) {
|
||||
protected void setPlayerArmor(final PlayerArmor playerArmor) {
|
||||
this.playerArmor.setBackpack(playerArmor.getBackpack());
|
||||
this.playerArmor.setHat(playerArmor.getHat());
|
||||
}
|
||||
|
||||
public void removeAllCosmetics() {
|
||||
this.setPlayerArmor(PlayerArmor.empty());
|
||||
}
|
||||
|
||||
public void removeHat(final HMCCosmetics plugin) {
|
||||
this.setHat(ArmorItem.empty(ArmorItem.Type.HAT), plugin);
|
||||
}
|
||||
|
||||
public void removeBackpack(final HMCCosmetics plugin) {
|
||||
this.setBackpack(ArmorItem.empty(ArmorItem.Type.BACKPACK), plugin);
|
||||
protected void removeAllCosmetics() {
|
||||
for (final ArmorItem.Type type : ArmorItem.Type.values()) {
|
||||
this.removeItem(type);
|
||||
}
|
||||
}
|
||||
|
||||
public int getArmorStandId() {
|
||||
return armorStandId;
|
||||
}
|
||||
|
||||
public void setBackpack(final ArmorItem backpack, final HMCCosmetics plugin) {
|
||||
this.playerArmor.setBackpack(backpack);
|
||||
this.lastSetItem = backpack;
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin,
|
||||
() -> plugin.getDatabase().saveUser(this));
|
||||
protected ArmorItem setItem(final ArmorItem armorItem) {
|
||||
return this.playerArmor.setItem(armorItem);
|
||||
}
|
||||
|
||||
protected ArmorItem removeItem(final ArmorItem.Type type) {
|
||||
return this.setItem(ArmorItem.empty(type));
|
||||
}
|
||||
|
||||
// return true if backpack was set
|
||||
public boolean setOrUnsetBackpack(
|
||||
final ArmorItem backpack,
|
||||
final MessageHandler messageHandler,
|
||||
final HMCCosmetics plugin) {
|
||||
|
||||
final Player player = this.getPlayer();
|
||||
|
||||
if (player == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (backpack.getId().equals(this.playerArmor.getBackpack().getId())) {
|
||||
this.setBackpack(ArmorItem.empty(ArmorItem.Type.BACKPACK), plugin);
|
||||
|
||||
messageHandler.sendMessage(
|
||||
player,
|
||||
Messages.REMOVED_BACKPACK
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
this.setBackpack(backpack, plugin);
|
||||
messageHandler.sendMessage(
|
||||
player,
|
||||
Messages.SET_BACKPACK
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public void setHat(ArmorItem hat, final HMCCosmetics plugin) {
|
||||
this.playerArmor.setHat(hat);
|
||||
this.lastSetItem = hat;
|
||||
plugin.getUserManager().updateHat(this);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin,
|
||||
() -> plugin.getDatabase().saveUser(this));
|
||||
}
|
||||
|
||||
// return true if hat was set
|
||||
public boolean setOrUnsetHat(
|
||||
final ArmorItem hat,
|
||||
final MessageHandler messageHandler,
|
||||
final HMCCosmetics plugin) {
|
||||
|
||||
final Player player = this.getPlayer();
|
||||
|
||||
if (player == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (hat.getId().equals(this.playerArmor.getHat().getId())) {
|
||||
this.setHat(ArmorItem.empty(ArmorItem.Type.HAT), plugin);
|
||||
|
||||
messageHandler.sendMessage(
|
||||
player,
|
||||
Messages.REMOVED_HAT
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
this.setHat(hat, plugin);
|
||||
messageHandler.sendMessage(
|
||||
player,
|
||||
Messages.SET_HAT
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void spawnArmorStand(final Player other) {
|
||||
final Player player = this.getPlayer();
|
||||
@@ -297,18 +213,6 @@ public class User {
|
||||
}
|
||||
}
|
||||
|
||||
public ArmorItem getLastSetItem() {
|
||||
return lastSetItem;
|
||||
}
|
||||
|
||||
public int getDye() {
|
||||
return this.playerArmor.getDye();
|
||||
}
|
||||
|
||||
public void setDye(final int dye) {
|
||||
this.playerArmor.setDye(dye);
|
||||
}
|
||||
|
||||
public boolean hasArmorStand() {
|
||||
return hasArmorStand;
|
||||
}
|
||||
|
||||
@@ -8,8 +8,13 @@ import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.comphenix.protocol.events.PacketEvent;
|
||||
import com.comphenix.protocol.wrappers.EnumWrappers;
|
||||
import com.comphenix.protocol.wrappers.Pair;
|
||||
import com.google.common.xml.XmlEscapers;
|
||||
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
|
||||
import io.github.fisher2911.hmccosmetics.gui.ArmorItem;
|
||||
import io.github.fisher2911.hmccosmetics.inventory.PlayerArmor;
|
||||
import io.github.fisher2911.hmccosmetics.message.Message;
|
||||
import io.github.fisher2911.hmccosmetics.message.MessageHandler;
|
||||
import io.github.fisher2911.hmccosmetics.message.Messages;
|
||||
import io.github.fisher2911.hmccosmetics.message.Placeholder;
|
||||
import io.github.fisher2911.hmccosmetics.util.Keys;
|
||||
import io.github.fisher2911.hmccosmetics.util.builder.ItemBuilder;
|
||||
@@ -31,6 +36,7 @@ import java.util.UUID;
|
||||
public class UserManager {
|
||||
|
||||
private final HMCCosmetics plugin;
|
||||
private final MessageHandler messageHandler;
|
||||
|
||||
private final Map<UUID, User> userMap = new HashMap<>();
|
||||
private final Map<Integer, User> armorStandIdMap = new HashMap<>();
|
||||
@@ -39,6 +45,7 @@ public class UserManager {
|
||||
|
||||
public UserManager(final HMCCosmetics plugin) {
|
||||
this.plugin = plugin;
|
||||
this.messageHandler = this.plugin.getMessageHandler();
|
||||
this.registerPacketListener();
|
||||
}
|
||||
|
||||
@@ -128,32 +135,6 @@ public class UserManager {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// not sure if this fixes anything, removed for now
|
||||
// protocolManager.addPacketListener(new PacketAdapter(
|
||||
// this.plugin,
|
||||
// ListenerPriority.NORMAL,
|
||||
// PacketType.Play.Server.ENTITY_DESTROY) {
|
||||
// @Override
|
||||
// public void onPacketReceiving(PacketEvent event) {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onPacketSending(final PacketEvent event) {
|
||||
// if (event.getPacketType() == PacketType.Play.Server.ENTITY_DESTROY) {
|
||||
// final int id = event.getPacket().getIntegers().read(0);
|
||||
//
|
||||
// final User user = armorStandIdMap.get(id);
|
||||
//
|
||||
// if (user == null) return;
|
||||
//
|
||||
// if (!user.hasArmorStand()) return;
|
||||
//
|
||||
// user.spawnArmorStand();
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
public void setFakeHelmet(final User user) {
|
||||
@@ -198,6 +179,55 @@ public class UserManager {
|
||||
}
|
||||
}
|
||||
|
||||
public void setItem(final User user, final ArmorItem armorItem) {
|
||||
user.setItem(armorItem);
|
||||
switch (armorItem.getType()) {
|
||||
case HAT -> this.setFakeHelmet(user);
|
||||
case OFF_HAND -> /* todo */ {}
|
||||
}
|
||||
}
|
||||
|
||||
public void removeItem(final User user, final ArmorItem.Type type) {
|
||||
this.setItem(user, ArmorItem.empty(type));
|
||||
}
|
||||
|
||||
// returns set item
|
||||
public ArmorItem setOrUnset(
|
||||
final User user,
|
||||
final ArmorItem armorItem,
|
||||
final Message removeMessage,
|
||||
final Message setMessage) {
|
||||
final Player player = user.getPlayer();
|
||||
|
||||
final ArmorItem empty = ArmorItem.empty(armorItem.getType());
|
||||
|
||||
if (player == null) {
|
||||
return empty;
|
||||
}
|
||||
|
||||
final ArmorItem check = user.getPlayerArmor().getItem(armorItem.getType());
|
||||
|
||||
final ArmorItem.Type type = armorItem.getType();
|
||||
|
||||
if (armorItem.getId().equals(check.getId())) {
|
||||
user.setItem(ArmorItem.empty(type));
|
||||
|
||||
messageHandler.sendMessage(
|
||||
player,
|
||||
removeMessage
|
||||
);
|
||||
|
||||
return empty;
|
||||
}
|
||||
|
||||
user.setItem(armorItem);
|
||||
messageHandler.sendMessage(
|
||||
player,
|
||||
setMessage
|
||||
);
|
||||
return armorItem;
|
||||
}
|
||||
|
||||
public void removeAll() {
|
||||
for (final var user : this.userMap.values()) {
|
||||
user.despawnAttached();
|
||||
|
||||
@@ -4,9 +4,14 @@ set-hat: "<white> <gradient:#6D9DC5:#45CDE9>Applied hat!"
|
||||
removed-hat: "<white> <gradient:#6D9DC5:#45CDE9>Removed hat!"
|
||||
set-backpack: "<white> <gradient:#6D9DC5:#45CDE9>Applied backpack!"
|
||||
removed-backpack: "<white> <gradient:#6D9DC5:#45CDE9>Removed backpack!"
|
||||
set-off-hand: "<white> <gradient:#6D9DC5:#45CDE9>Applied offhand!"
|
||||
removed-off-hand: "<white> <gradient:#6D9DC5:#45CDE9>Removed offhand!"
|
||||
must-be-player: "<red>You must be a player to do this!"
|
||||
reloaded: "<white> <gradient:#00ff87:#60efff>Config files reloaded!"
|
||||
invalid-type: "<white> <red>Invalid cosmetic type, please use <u>hat</u> or <u>backpack</u>!"
|
||||
invalid-type: "<white> <red>Invalid cosmetic type, please use <u>hat</u>, <u>backpack</u> or <u>off_hand</u>!"
|
||||
set-other-backpack: "<white><gradient:#6D9DC5:#45CDE9>You have set the backpack of %player% to %type%"
|
||||
set-other-hat: "<white><gradient:#6D9DC5:#45CDE9>You have set the hat of %player% to %type%"
|
||||
set-other-off-hand: "<white><gradient:#6D9DC5:#45CDE9>You have set the off hand of %player% to %type%"
|
||||
help-command: "<#6D9DC5><st> </st> <white> <gradient:#40B7D6:#6D9DC5>HMCCosmetics - Help</gradient> <white> <#6D9DC5> <st> </st>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user