From 02c3245c6f0b446ee5022e0b723dd9b63e9276bd Mon Sep 17 00:00:00 2001 From: RadBuilder Date: Fri, 30 Jun 2017 00:49:57 -0700 Subject: [PATCH] Added world checking to commands Added world checking to commands to make sure that cosmetics and menus can only be opened/used in worlds that cosmetics are enabled in. --- .../ultracosmetics/UltraCosmeticsData.java | 11 +++++ .../subcommands/SubCommandGadgets.java | 7 +++ .../command/subcommands/SubCommandMenu.java | 5 ++ .../subcommands/SubCommandSelfView.java | 7 +++ .../command/subcommands/SubCommandToggle.java | 46 +++++++++++++++---- .../subcommands/SubCommandTreasure.java | 8 ++++ 6 files changed, 75 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/be/isach/ultracosmetics/UltraCosmeticsData.java b/core/src/main/java/be/isach/ultracosmetics/UltraCosmeticsData.java index 84986ace3..b999f9bae 100644 --- a/core/src/main/java/be/isach/ultracosmetics/UltraCosmeticsData.java +++ b/core/src/main/java/be/isach/ultracosmetics/UltraCosmeticsData.java @@ -5,6 +5,7 @@ import be.isach.ultracosmetics.version.VersionManager; import org.bukkit.Bukkit; +import java.util.List; import java.util.UUID; /** @@ -87,6 +88,11 @@ public static UltraCosmeticsData get() { private VersionManager versionManager; private UltraCosmetics ultraCosmetics; + + /** + * A list of worlds where cosmetics are enabled. + */ + private List enabledWorlds; /** * A String that items that shouldn't be picked up are given. Randomly generated each time the server starts. @@ -178,6 +184,7 @@ public void initConfigFields() { this.customCommandBackArrow = ultraCosmetics.getConfig().getBoolean("Categories.Back-To-Main-Menu-Custom-Command.Enabled"); this.customBackMenuCommand = ultraCosmetics.getConfig().getString("Categories.Back-To-Main-Menu-Custom-Command.Command").replace("/", ""); this.closeAfterSelect = ultraCosmetics.getConfig().getBoolean("Categories.Close-GUI-After-Select"); + this.enabledWorlds = ultraCosmetics.getConfig().getStringList("Enabled-Worlds"); } public boolean isAmmoEnabled() { @@ -252,4 +259,8 @@ public boolean isUsingVaultEconomy() { public final String getItemNoPickupString() { return this.itemNoPickupString; } + + public List getEnabledWorlds() { + return this.enabledWorlds; + } } diff --git a/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandGadgets.java b/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandGadgets.java index 888bc105a..53ef2148c 100644 --- a/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandGadgets.java +++ b/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandGadgets.java @@ -1,7 +1,9 @@ package be.isach.ultracosmetics.command.subcommands; import be.isach.ultracosmetics.UltraCosmetics; +import be.isach.ultracosmetics.UltraCosmeticsData; import be.isach.ultracosmetics.command.SubCommand; +import be.isach.ultracosmetics.config.MessageManager; import be.isach.ultracosmetics.player.UltraPlayer; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; @@ -20,6 +22,11 @@ public SubCommandGadgets(UltraCosmetics ultraCosmetics) { @Override protected void onExePlayer(Player sender, String... args) { + if (!UltraCosmeticsData.get().getEnabledWorlds().contains(sender.getWorld().getName())) { + sender.sendMessage(MessageManager.getMessage("World-Disabled")); + return; + } + UltraPlayer customPlayer = getUltraCosmetics().getPlayerManager().getUltraPlayer(sender); customPlayer.setGadgetsEnabled(!customPlayer.hasGadgetsEnabled()); } diff --git a/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandMenu.java b/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandMenu.java index ee0f0b2f7..c7529f205 100644 --- a/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandMenu.java +++ b/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandMenu.java @@ -31,6 +31,11 @@ public SubCommandMenu(UltraCosmetics ultraCosmetics) { @Override protected void onExePlayer(Player sender, String... args) { + if (!UltraCosmeticsData.get().getEnabledWorlds().contains(sender.getWorld().getName())) { + sender.sendMessage(MessageManager.getMessage("World-Disabled")); + return; + } + if (args.length < 2) { sender.sendMessage(getMenuList()); return; diff --git a/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandSelfView.java b/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandSelfView.java index 6ec533a82..ddeaa7402 100644 --- a/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandSelfView.java +++ b/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandSelfView.java @@ -1,7 +1,9 @@ package be.isach.ultracosmetics.command.subcommands; import be.isach.ultracosmetics.UltraCosmetics; +import be.isach.ultracosmetics.UltraCosmeticsData; import be.isach.ultracosmetics.command.SubCommand; +import be.isach.ultracosmetics.config.MessageManager; import be.isach.ultracosmetics.player.UltraPlayer; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; @@ -20,6 +22,11 @@ public SubCommandSelfView(UltraCosmetics ultraCosmetics) { @Override protected void onExePlayer(Player sender, String... args) { + if (!UltraCosmeticsData.get().getEnabledWorlds().contains(sender.getWorld().getName())) { + sender.sendMessage(MessageManager.getMessage("World-Disabled")); + return; + } + UltraPlayer customPlayer = getUltraCosmetics().getPlayerManager().getUltraPlayer(sender); customPlayer.setSeeSelfMorph(!customPlayer.canSeeSelfMorph()); } diff --git a/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandToggle.java b/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandToggle.java index d384e0c74..6b3a97c01 100644 --- a/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandToggle.java +++ b/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandToggle.java @@ -1,6 +1,7 @@ package be.isach.ultracosmetics.command.subcommands; import be.isach.ultracosmetics.UltraCosmetics; +import be.isach.ultracosmetics.UltraCosmeticsData; import be.isach.ultracosmetics.command.SubCommand; import be.isach.ultracosmetics.config.MessageManager; import be.isach.ultracosmetics.cosmetics.Category; @@ -19,24 +20,41 @@ * @since 12-21-2015 */ public class SubCommandToggle extends SubCommand { - - + + public SubCommandToggle(UltraCosmetics ultraCosmetics) { super("Toggles a cosmetic.", "ultracosmetics.command.toggle", "/uc toggle [player]", ultraCosmetics, "toggle"); } - + @Override protected void onExePlayer(Player sender, String... args) { UltraPlayer ultraPlayer = getUltraCosmetics().getPlayerManager().getUltraPlayer(sender); - + if (args.length < 3) { sender.sendMessage(MessageManager.getMessage("Prefix") + " §c§l" + getUsage()); return; } - + String type = args[1].toLowerCase(); String cosm = args[2].toLowerCase(); - + + if (args.length > 3) { + try { + if (!UltraCosmeticsData.get().getEnabledWorlds().contains(Bukkit.getPlayer(args[3]).getWorld().getName())) { + sender.sendMessage(MessageManager.getMessage("World-Disabled")); + return; + } + } catch (Exception e) { + sender.sendMessage(MessageManager.getMessage("Prefix") + " §c§lInvalid player."); + return; + } + } else { + if (!UltraCosmeticsData.get().getEnabledWorlds().contains(sender.getWorld().getName())) { + sender.sendMessage(MessageManager.getMessage("World-Disabled")); + return; + } + } + Object[] categories = Arrays.stream(Category.values()).filter(category -> category.isEnabled() && category.toString().toLowerCase().startsWith(type)).toArray(); if (categories.length == 1) { Category category = (Category) categories[0]; @@ -77,17 +95,27 @@ protected void onExePlayer(Player sender, String... args) { sender.sendMessage(MessageManager.getMessage("Prefix") + " §c§lInvalid category."); } } - + @Override protected void onExeConsole(ConsoleCommandSender sender, String... args) { if (args.length < 4) { sender.sendMessage(MessageManager.getMessage("Prefix") + " §c§l/uc toggle "); return; } - + String type = args[1].toLowerCase(); String cosm = args[2].toLowerCase(); - + + try { + if (!UltraCosmeticsData.get().getEnabledWorlds().contains(Bukkit.getPlayer(args[3]).getWorld().getName())) { + sender.sendMessage(MessageManager.getMessage("World-Disabled")); + return; + } + } catch (Exception e) { + sender.sendMessage(MessageManager.getMessage("Prefix") + " §c§lInvalid player."); + return; + } + Object[] categories = Arrays.stream(Category.values()).filter(category -> category.isEnabled() && category.toString().toLowerCase().startsWith(type)).toArray(); if (categories.length == 1) { Category category = (Category) categories[0]; diff --git a/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandTreasure.java b/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandTreasure.java index fb7206b13..0b10f1c6d 100644 --- a/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandTreasure.java +++ b/core/src/main/java/be/isach/ultracosmetics/command/subcommands/SubCommandTreasure.java @@ -1,7 +1,9 @@ package be.isach.ultracosmetics.command.subcommands; import be.isach.ultracosmetics.UltraCosmetics; +import be.isach.ultracosmetics.UltraCosmeticsData; import be.isach.ultracosmetics.command.SubCommand; +import be.isach.ultracosmetics.config.MessageManager; import be.isach.ultracosmetics.manager.TreasureChestManager; import be.isach.ultracosmetics.util.MathUtils; import org.bukkit.Bukkit; @@ -45,6 +47,12 @@ private void common(CommandSender sender, String... args) { sender.sendMessage("§c§lPlayer " + args[1] + " not found!"); return; } + + if (!UltraCosmeticsData.get().getEnabledWorlds().contains(opener.getWorld().getName())) { + sender.sendMessage(MessageManager.getMessage("World-Disabled")); + return; + } + double x, y, z; if (!MathUtils.isDouble(args[2])) { sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + args[2] + " isn't a number!");