From e5222c6d24b6010b26c2a37cd85975b3508457cc Mon Sep 17 00:00:00 2001 From: Darkmans39 Date: Tue, 8 Oct 2019 03:57:22 +0000 Subject: [PATCH] Added a new command, a color string method to StringUtil, and a method to check if a block can be stacked in StackableManager. --- .gitlab-ci.yml | 2 +- .../skyblock/command/CommandManager.java | 3 +- .../commands/admin/StackableCommand.java | 120 ++++++++++++++++++ .../songoda/skyblock/listeners/Interact.java | 2 +- .../skyblock/stackable/StackableManager.java | 10 +- .../songoda/skyblock/utils/StringUtil.java | 6 + src/main/resources/language.yml | 11 ++ 7 files changed, 148 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/songoda/skyblock/command/commands/admin/StackableCommand.java diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 4efaa9b7..dfdb98d5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,7 +3,7 @@ image: gradle:alpine variables: name: "FabledSkyBlock" path: "/builds/$CI_PROJECT_PATH" - version: "2.0.3" + version: "2.0.4" before_script: - export GRADLE_USER_HOME=`pwd`/.gradle diff --git a/src/main/java/com/songoda/skyblock/command/CommandManager.java b/src/main/java/com/songoda/skyblock/command/CommandManager.java index 0ec84a08..de086955 100644 --- a/src/main/java/com/songoda/skyblock/command/CommandManager.java +++ b/src/main/java/com/songoda/skyblock/command/CommandManager.java @@ -110,7 +110,8 @@ public class CommandManager implements CommandExecutor, TabCompleter { new com.songoda.skyblock.command.commands.admin.SetSpawnCommand(), new com.songoda.skyblock.command.commands.admin.SettingsCommand(), new StructureCommand(), - new com.songoda.skyblock.command.commands.admin.UpgradeCommand() + new com.songoda.skyblock.command.commands.admin.UpgradeCommand(), + new StackableCommand() ); } diff --git a/src/main/java/com/songoda/skyblock/command/commands/admin/StackableCommand.java b/src/main/java/com/songoda/skyblock/command/commands/admin/StackableCommand.java new file mode 100644 index 00000000..ff24df1b --- /dev/null +++ b/src/main/java/com/songoda/skyblock/command/commands/admin/StackableCommand.java @@ -0,0 +1,120 @@ +package com.songoda.skyblock.command.commands.admin; + +import java.io.File; +import java.util.Set; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; + +import com.songoda.skyblock.command.SubCommand; +import com.songoda.skyblock.message.MessageManager; +import com.songoda.skyblock.stackable.Stackable; +import com.songoda.skyblock.stackable.StackableManager; +import com.songoda.skyblock.utils.StringUtil; + +public class StackableCommand extends SubCommand { + + @Override + public void onCommandByPlayer(Player player, String[] args) { + final MessageManager messageManager = skyblock.getMessageManager(); + + if (args.length == 0) { + player.sendMessage(StringUtil.color("&e/island admin stackable setsize &7- &f&osets the target block's stack size if applicable")); + return; + } + + final FileConfiguration messageConfig = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")).getFileConfiguration(); + + if (args[0].equalsIgnoreCase("setsize")) { + + if (args.length == 1) { + messageManager.sendMessage(player, messageConfig.getString("Command.Island.Admin.Stackable.Setsize.No-Arguments")); + return; + } + + int amount = 0; + + try { + amount = Integer.parseInt(args[1]); + } catch (NumberFormatException e) { + messageManager.sendMessage(player, (messageConfig.getString("Command.Island.Admin.Stackable.Setsize.Invalid-Number")).replace("%number%", args[1])); + return; + } + + final Block block = player.getTargetBlock((Set) null, 6); + + if (block == null) { + messageManager.sendMessage(player, messageConfig.getString("Command.Island.Admin.Stackable.Target.None")); + return; + } + + final StackableManager stackableManager = skyblock.getStackableManager(); + + if (!stackableManager.isStackableMaterial(block.getType())) { + messageManager.sendMessage(player, messageConfig.getString("Command.Island.Admin.Stackable.Target.Unstackable")); + return; + } + + final Location loc = block.getLocation(); + Stackable stack = stackableManager.getStack(loc, block.getType()); + + if (amount <= 1) { + messageManager.sendMessage(player, messageConfig.getString("Command.Island.Admin.Stackable.Target.Remove-Stack")); + if (stack != null) stackableManager.removeStack(stack); + return; + } + + final int oldSize; + + if (stack == null) { + stack = new Stackable(loc, block.getType()); + stackableManager.addStack(stack); + oldSize = 0; + } else { + oldSize = stack.getSize(); + } + + stack.setSize(amount); + + String input = messageConfig.getString("Command.Island.Admin.Stackable.Setsize.Success"); + + input = input.replace("%old_size%", Integer.toString(oldSize)); + input = input.replace("%new_size%", Integer.toString(amount)); + + messageManager.sendMessage(player, input); + } else { + messageManager.sendMessage(player, messageConfig.getString("Command.Island.Argument.Unrecognised.Message")); + } + + } + + @Override + public void onCommandByConsole(ConsoleCommandSender sender, String[] args) { + sender.sendMessage("SkyBlock | Error: You must be a player to perform that command."); + } + + @Override + public String getName() { + return "stackable"; + } + + @Override + public String getInfoMessagePath() { + return "Command.Island.Admin.Stackable.Info.Message"; + } + + @Override + public String[] getAliases() { + return new String[] { "stackables" }; + } + + @Override + public String[] getArguments() { + return new String[] { "setsize" }; + } + +} diff --git a/src/main/java/com/songoda/skyblock/listeners/Interact.java b/src/main/java/com/songoda/skyblock/listeners/Interact.java index 2aa4395c..eac79954 100644 --- a/src/main/java/com/songoda/skyblock/listeners/Interact.java +++ b/src/main/java/com/songoda/skyblock/listeners/Interact.java @@ -141,7 +141,7 @@ public class Interact implements Listener { if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { if (stackableManager != null - && stackableManager.getStackableMaterials().contains(event.getMaterial()) + && stackableManager.isStackableMaterial(event.getMaterial()) && event.getClickedBlock().getType() == event.getMaterial() && !player.isSneaking() && islandManager.hasPermission(player, block.getLocation(), "Place") && (!skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration().getBoolean("Island.Stackable.RequirePermission") || player.hasPermission("fabledskyblock.stackable"))) { diff --git a/src/main/java/com/songoda/skyblock/stackable/StackableManager.java b/src/main/java/com/songoda/skyblock/stackable/StackableManager.java index c42285df..4f4bb160 100644 --- a/src/main/java/com/songoda/skyblock/stackable/StackableManager.java +++ b/src/main/java/com/songoda/skyblock/stackable/StackableManager.java @@ -15,7 +15,7 @@ public class StackableManager { //ToDO: Should pobably be a GUI for this private final SkyBlock skyblock; - private List stackableMaterials = new ArrayList<>(); + private Set stackableMaterials = EnumSet.noneOf(Material.class); private Map stacks = new HashMap<>(); public StackableManager(SkyBlock skyblock) { @@ -66,8 +66,12 @@ public class StackableManager { stackableMaterials.clear(); } - public List getStackableMaterials() { - return Collections.unmodifiableList(stackableMaterials); + public Set getStackableMaterials() { + return Collections.unmodifiableSet(stackableMaterials); + } + + public boolean isStackableMaterial(Material material) { + return stackableMaterials.contains(material); } public Map getStacks() { diff --git a/src/main/java/com/songoda/skyblock/utils/StringUtil.java b/src/main/java/com/songoda/skyblock/utils/StringUtil.java index 0e9f4d8e..6590f449 100644 --- a/src/main/java/com/songoda/skyblock/utils/StringUtil.java +++ b/src/main/java/com/songoda/skyblock/utils/StringUtil.java @@ -3,6 +3,8 @@ package com.songoda.skyblock.utils; import java.util.regex.Matcher; import java.util.regex.Pattern; +import net.md_5.bungee.api.ChatColor; + public final class StringUtil { public static String capatilizeUppercaseLetters(String string) { @@ -19,4 +21,8 @@ public final class StringUtil { return stringBuilder.toString(); } + + public static String color(String input) { + return input == null ? null : ChatColor.translateAlternateColorCodes('&', input); + } } diff --git a/src/main/resources/language.yml b/src/main/resources/language.yml index bf2a9506..ea4a8eb1 100644 --- a/src/main/resources/language.yml +++ b/src/main/resources/language.yml @@ -142,6 +142,17 @@ Command: Message: '&f&oReload all loaded files.' Reloaded: Message: '&bSkyBlock &8| &aInfo&8: &eAll loaded files have been reloaded.' + Stackable: + Info: + Message: '&f&oManage stackables' + Target: + None: '&bSkyBlock &8| &cError&8: &eYou must be looking at a block.' + Unstackable: '&bSkyBlock &8| &cError&8: &eUnable to target unstackable block.' + Remove-Stack: '&bSkyBlock &8| &aInfo&8: &eSuccessfully removed stack if one was present.' + Setsize: + No-Arguments: '&bSkyBlock &8| &cError&8: &ePlease input a number that will become the new stack size.' + Invalid-Number: '&bSkyBlock &8| &cError&8: &eFailed to parse a number from %number%' + Success: '&bSkyBlock &8| &aInfo&8: &eSuccessfully changed the target block''s stack size from %old_size% to %new_size%' RemoveUpgrade: Invalid: Message: '&bSkyBlock &8| &cError&8: &eInvalid: /island admin removeupgrade '