9
0
mirror of https://github.com/HibiscusMC/HMCCosmetics.git synced 2025-12-28 19:39:14 +00:00

Added command to set other players cosmetics

This commit is contained in:
HeroBrineGoat
2022-01-15 21:48:11 -05:00
parent 705664441a
commit 1d988ed809
8 changed files with 136 additions and 7 deletions

View File

@@ -4,6 +4,7 @@ import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import io.github.fisher2911.hmccosmetics.command.CosmeticsCommand;
import io.github.fisher2911.hmccosmetics.cosmetic.CosmeticManager;
import io.github.fisher2911.hmccosmetics.gui.ArmorItem;
import io.github.fisher2911.hmccosmetics.gui.CosmeticsMenu;
import io.github.fisher2911.hmccosmetics.listener.ClickListener;
@@ -18,6 +19,7 @@ import org.bstats.bukkit.Metrics;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
@@ -25,6 +27,7 @@ public class HMCCosmetics extends JavaPlugin {
private ProtocolManager protocolManager;
private UserManager userManager;
private CosmeticManager cosmeticManager;
private MessageHandler messageHandler;
private CosmeticsMenu cosmeticsMenu;
private CommandManager commandManager;
@@ -37,6 +40,7 @@ public class HMCCosmetics extends JavaPlugin {
protocolManager = ProtocolLibrary.getProtocolManager();
this.messageHandler = new MessageHandler(this);
this.userManager = new UserManager(this);
this.cosmeticManager = new CosmeticManager(new HashMap<>());
this.cosmeticsMenu = new CosmeticsMenu(this);
this.messageHandler.load();
this.cosmeticsMenu.load();
@@ -80,6 +84,9 @@ public class HMCCosmetics extends JavaPlugin {
map(ArmorItem.Type::toString).
collect(Collectors.toList())
);
this.commandManager.getCompletionHandler().register("#ids",
resolver ->
this.cosmeticManager.getAll().stream().map(ArmorItem::getId).collect(Collectors.toList()));
this.commandManager.register(new CosmeticsCommand(this));
}
@@ -91,6 +98,10 @@ public class HMCCosmetics extends JavaPlugin {
return userManager;
}
public CosmeticManager getCosmeticManager() {
return cosmeticManager;
}
public CosmeticsMenu getCosmeticsMenu() {
return cosmeticsMenu;
}

View File

@@ -7,6 +7,7 @@ import io.github.fisher2911.hmccosmetics.gui.DyeSelectorGui;
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.user.User;
import io.github.fisher2911.hmccosmetics.user.UserManager;
import me.mattstudios.mf.annotations.Command;
@@ -94,13 +95,66 @@ public class CosmeticsCommand extends CommandBase {
public void helpCommand(final CommandSender sender) {
Bukkit.getScheduler().runTaskAsynchronously(
this.plugin,
() -> {
this.messageHandler.sendMessage(
sender,
Messages.HELP_COMMAND
);
}
() -> this.messageHandler.sendMessage(
sender,
Messages.HELP_COMMAND
)
);
}
@SubCommand("add")
@Permission(io.github.fisher2911.hmccosmetics.message.Permission.SET_COSMETIC_COMMAND)
public void setCommand(final CommandSender sender, final Player player, @Completion("#ids") final String id) {
final Optional<User> userOptional = this.userManager.get(player.getUniqueId());
if (userOptional.isEmpty()) {
this.messageHandler.sendMessage(
sender,
Messages.INVALID_USER
);
return;
}
final User user = userOptional.get();
final ArmorItem armorItem = this.plugin.getCosmeticManager().getArmorItem(id);
if (armorItem == null) {
this.messageHandler.sendMessage(
sender,
Messages.ITEM_NOT_FOUND
);
return;
}
switch (armorItem.getType()) {
case BACKPACK -> {
user.setBackpack(armorItem);
this.messageHandler.sendMessage(
player,
Messages.SET_BACKPACK
);
this.messageHandler.sendMessage(
sender,
Messages.SET_OTHER_BACKPACK,
Map.of(Placeholder.PLAYER, player.getName(),
Placeholder.ITEM, id)
);
}
case HAT -> {
user.setHat(armorItem, this.userManager);
this.messageHandler.sendMessage(
player,
Messages.SET_HAT
);
this.messageHandler.sendMessage(
sender,
Messages.SET_OTHER_HAT,
Map.of(Placeholder.PLAYER, player.getName(),
Placeholder.ITEM, id)
);
}
}
}
}

View File

@@ -0,0 +1,30 @@
package io.github.fisher2911.hmccosmetics.cosmetic;
import io.github.fisher2911.hmccosmetics.gui.ArmorItem;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
public class CosmeticManager {
private final Map<String, ArmorItem> armorItemMap;
public CosmeticManager(final Map<String, ArmorItem> armorItemMap) {
this.armorItemMap = armorItemMap;
}
@Nullable
public ArmorItem getArmorItem(final String id) {
return this.armorItemMap.get(id);
}
public void addArmorItem(final ArmorItem armorItem) {
this.armorItemMap.put(armorItem.getId(), armorItem);
}
public Collection<ArmorItem> getAll() {
return this.armorItemMap.values();
}
}

View File

@@ -5,6 +5,7 @@ import io.github.fisher2911.hmccosmetics.HMCCosmetics;
import io.github.fisher2911.hmccosmetics.config.DyeGuiSerializer;
import io.github.fisher2911.hmccosmetics.config.GuiSerializer;
import io.github.fisher2911.hmccosmetics.config.ItemSerializer;
import io.github.fisher2911.hmccosmetics.cosmetic.CosmeticManager;
import io.github.fisher2911.hmccosmetics.user.User;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
@@ -23,11 +24,13 @@ public class CosmeticsMenu {
public static final String DYE_MENU = "dye-menu";
private final HMCCosmetics plugin;
private final CosmeticManager cosmeticManager;
private final Map<String, CosmeticGui> guiMap = new HashMap<>();
public CosmeticsMenu(final HMCCosmetics plugin) {
this.plugin = plugin;
this.cosmeticManager = this.plugin.getCosmeticManager();
}
public void openMenu(final String id, final HumanEntity humanEntity) {
@@ -124,6 +127,17 @@ public class CosmeticsMenu {
this.plugin.getLogger().info("Loaded dye gui: " + id);
continue;
}
final CosmeticGui gui = source.get(CosmeticGui.class);
if (gui == null) continue;
for (final GuiItem guiItem : gui.guiItemMap.values()) {
if (guiItem instanceof final ArmorItem item) {
this.cosmeticManager.addArmorItem(item);
}
}
this.guiMap.put(id, source.get(CosmeticGui.class));
this.plugin.getLogger().info("Loaded gui: " + id);
} catch (final ConfigurateException exception) {

View File

@@ -1,5 +1,7 @@
package io.github.fisher2911.hmccosmetics.message;
import org.bukkit.ChatColor;
public class Messages {
public static final Message NO_PERMISSION =
@@ -20,6 +22,10 @@ public class Messages {
new Message("reloaded", "Config reloaded");
public static final Message INVALID_TYPE =
new Message("invalid-type", "Invalid type");
public static final Message INVALID_USER =
new Message("invalid-user", ChatColor.RED + "That user's data cannot be found!");
public static final Message ITEM_NOT_FOUND =
new Message("item-not-found", ChatColor.RED + "That item could not be found!");
public static final Message HELP_COMMAND =
new Message("help-command", "<#6D9DC5><st> </st> <gradient:#40B7D6:#6D9DC5>HMCCosmetics - Help</gradient><#6D9DC5> <st> </st>\n" +
"\n" +
@@ -32,4 +38,13 @@ public class Messages {
"\n" +
"\n" +
"<st> </st>");
public static final Message SET_OTHER_BACKPACK = new Message(
"set-other-backpack", ChatColor.GREEN + "You have set the backpack of " +
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 " +
Placeholder.PLAYER + " to " + Placeholder.TYPE + "."
);
}

View File

@@ -6,5 +6,6 @@ public class Permission {
public static final String DYE_COMMAND = "hmccosmetics.cmd.dye";
public static final String RELOAD_COMMAND = "hmccosmetics.cmd.reload";
public static final String HELP_COMMAND = "hmccosmetics.cmd.help";
public static final String SET_COSMETIC_COMMAND = "hmccosmetics.cmd.set";
}

View File

@@ -7,6 +7,7 @@ public class Placeholder {
public static final String ITEM = "%item%";
public static final String FILE = "%file%";
public static final String PLAYER = "%player%";
public static final String ENABLED = "%enabled%";
public static final String ALLOWED = "%allowed%";
}