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

Added example dye menu with command /cosmetics dye [HAT | BACKPACK]

This commit is contained in:
HeroBrineGoat
2021-11-08 22:36:15 -05:00
parent 86e1a3a4cc
commit 6a2778693c
6 changed files with 272 additions and 2 deletions

View File

@@ -1,23 +1,34 @@
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.gui.DyeSelectorGui;
import io.github.fisher2911.hmccosmetics.message.MessageHandler;
import io.github.fisher2911.hmccosmetics.user.User;
import io.github.fisher2911.hmccosmetics.user.UserManager;
import me.mattstudios.mf.annotations.Command;
import me.mattstudios.mf.annotations.Default;
import me.mattstudios.mf.annotations.Permission;
import me.mattstudios.mf.annotations.SubCommand;
import me.mattstudios.mf.base.CommandBase;
import org.bukkit.Color;
import org.bukkit.entity.Player;
import java.util.Map;
import java.util.Optional;
@Command("cosmetics")
public class CosmeticsCommand extends CommandBase {
private final HMCCosmetics plugin;
private final UserManager userManager;
private final MessageHandler messageHandler;
private final CosmeticsMenu cosmeticsMenu;
public CosmeticsCommand(final HMCCosmetics plugin) {
this.plugin = plugin;
this.userManager = this.plugin.getUserManager();
this.messageHandler = this.plugin.getMessageHandler();
this.cosmeticsMenu = this.plugin.getCosmeticsMenu();
}
@@ -28,4 +39,30 @@ public class CosmeticsCommand extends CommandBase {
this.cosmeticsMenu.openDefault(player);
}
@SubCommand("dye")
@Permission(io.github.fisher2911.hmccosmetics.message.Permission.DYE_COMMAND)
public void dyeArmor(final Player player, String typeString) {
final Optional<User> optionalUser = this.userManager.get(player.getUniqueId());
if (optionalUser.isEmpty()) {
return;
}
final ArmorItem.Type type = ArmorItem.Type.valueOf(typeString);
final User user = optionalUser.get();
final ArmorItem armorItem = switch (type) {
case HAT -> user.getPlayerArmor().getHat();
case BACKPACK -> user.getPlayerArmor().getBackpack();
};
new DyeSelectorGui("test", 3, Map.of(
10, Color.fromRGB(1, 2, 3),
11, Color.fromRGB(0, 0, 255)
), armorItem
).getGui(user).open(player);
}
}

View File

@@ -11,16 +11,20 @@ import org.jetbrains.annotations.Nullable;
public class ArmorItem extends GuiItem {
private final String id;
private final GuiAction<InventoryClickEvent> action;
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 String permission,
final String id,
final String permission,
final Type type) {
super(itemStack, action);
this.id = id;
this.action = action;
this.permission = permission;
this.type = type;
}
@@ -32,6 +36,7 @@ public class ArmorItem extends GuiItem {
final Type type) {
super(itemStack);
this.id = id;
this.action = null;
this.permission = permission;
this.type = type;
}
@@ -43,6 +48,7 @@ public class ArmorItem extends GuiItem {
final Type type) {
super(material);
this.id = id;
this.action = null;
this.permission = permission;
this.type = type;
}
@@ -55,14 +61,76 @@ public class ArmorItem extends GuiItem {
final Type type) {
super(material, action);
this.id = id;
this.action = action;
this.permission = permission;
this.type = type;
}
public ArmorItem(
@NotNull final ItemStack itemStack,
final GuiAction<InventoryClickEvent> action,
final String id, final String permission,
final Type type,
final boolean dyeable) {
super(itemStack, action);
this.id = id;
this.action = action;
this.permission = permission;
this.type = type;
this.dyeable = dyeable;
}
public ArmorItem(
@NotNull final ItemStack itemStack,
final String id,
final String permission,
final Type type,
final boolean dyeable) {
super(itemStack);
this.id = id;
this.action = null;
this.permission = permission;
this.type = type;
this.dyeable = dyeable;
}
public ArmorItem(
@NotNull final Material material,
final String id,
final String permission,
final Type type,
final boolean dyeable) {
super(material);
this.id = id;
this.action = null;
this.permission = permission;
this.type = type;
this.dyeable = dyeable;
}
public ArmorItem(
@NotNull final Material material,
@Nullable final GuiAction<InventoryClickEvent> action,
final String id,
final String permission,
final Type type,
final boolean dyeable) {
super(material, action);
this.id = id;
this.action = action;
this.permission = permission;
this.type = type;
this.dyeable = dyeable;
}
public String getId() {
return id;
}
public GuiAction<InventoryClickEvent> getAction() {
return this.action;
}
public String getPermission() {
return permission;
}
@@ -71,6 +139,10 @@ public class ArmorItem extends GuiItem {
return type;
}
public boolean isDyeable() {
return dyeable;
}
public enum Type {
HAT,

View File

@@ -0,0 +1,131 @@
package io.github.fisher2911.hmccosmetics.gui;
import dev.triumphteam.gui.builder.item.ItemBuilder;
import dev.triumphteam.gui.guis.Gui;
import io.github.fisher2911.hmccosmetics.config.ItemSerializer;
import io.github.fisher2911.hmccosmetics.inventory.PlayerArmor;
import io.github.fisher2911.hmccosmetics.user.User;
import io.github.fisher2911.hmccosmetics.util.builder.LeatherArmorBuilder;
import net.kyori.adventure.text.Component;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.checkerframework.checker.units.qual.A;
import java.util.HashMap;
import java.util.Map;
public class DyeSelectorGui {
private final String title;
private final int rows;
private final Map<Integer, Color> itemColors;
private final ArmorItem armorItem;
public DyeSelectorGui(
final String title,
final int rows,
final Map<Integer, Color> itemColors,
final ArmorItem armorItem) {
this.title = title;
this.rows = rows;
this.itemColors = itemColors;
this.armorItem = armorItem;
}
public Gui getGui(final User user) {
final Gui gui = Gui.gui().
title(Component.text(this.title)).
rows(rows).
create();
for (final var entry : itemColors.entrySet()) {
gui.setItem(entry.getKey(), ItemBuilder.from(Material.BLACK_DYE).
name(Component.text(
String.valueOf(entry.getValue().asRGB()))).asGuiItem());
}
gui.setDefaultClickAction(event -> {
event.setCancelled(true);
final ArmorItem.Type type = this.armorItem.getType();
final PlayerArmor playerArmor = user.getPlayerArmor();
if (playerArmor == null) {
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();
}
};
if (itemStack == null) {
return;
}
if (!(itemStack.getItemMeta() instanceof final LeatherArmorMeta itemMeta)) {
return;
}
final LeatherArmorBuilder leatherArmorBuilder =
LeatherArmorBuilder.from(itemStack);
final Color color = this.itemColors.get(event.getSlot());
if (color == null) {
return;
}
leatherArmorBuilder.color(color);
final ArmorItem armorItem = new ArmorItem(
leatherArmorBuilder.build(),
this.armorItem.getAction(),
this.armorItem.getId(),
this.armorItem.getPermission(),
this.armorItem.getType(),
this.armorItem.isDyeable()
);
switch (type) {
case HAT -> user.setHat(armorItem);
case BACKPACK -> user.setBackpack(armorItem);
}
});
return gui;
}
public String getTitle() {
return title;
}
public int getRows() {
return rows;
}
public Map<Integer, Color> getItemColors() {
return itemColors;
}
public ArmorItem getArmorItem() {
return armorItem;
}
}

View File

@@ -3,5 +3,6 @@ package io.github.fisher2911.hmccosmetics.message;
public class Permission {
public static final String DEFAULT_COMMAND = "hmccosmetics.cmd.default";
public static final String DYE_COMMAND = "hmccosmetics.cmd.dye";
}