From 72e09bac5bed3f258943d2b744a34d81373f71d0 Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 4 Dec 2021 19:34:03 +0800 Subject: [PATCH] v4.4.1 --- pom.xml | 2 +- .../St3venAU/plugins/ArmorStandTools/AST.java | 110 +++++++++++++++++- .../plugins/ArmorStandTools/Commands.java | 12 ++ .../plugins/ArmorStandTools/Config.java | 8 +- .../plugins/ArmorStandTools/MainListener.java | 84 +------------ src/main/resources/config.yml | 1 + src/main/resources/language.yml | 4 + 7 files changed, 134 insertions(+), 87 deletions(-) diff --git a/pom.xml b/pom.xml index 309e8e0..ba0bff6 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.gmail.st3venau.plugins ArmorStandTools -4.4.0 +4.4.1 ArmorStandTools diff --git a/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/AST.java b/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/AST.java index 31d8b05..4c1bfca 100644 --- a/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/AST.java +++ b/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/AST.java @@ -8,6 +8,9 @@ import com.sk89q.worldguard.protection.regions.RegionContainer; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.OfflinePlayer; +import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.command.PluginCommand; import org.bukkit.entity.ArmorStand; @@ -15,6 +18,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; +import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.metadata.MetadataValue; import org.bukkit.plugin.java.JavaPlugin; @@ -24,6 +28,7 @@ import java.util.AbstractMap; import java.util.HashMap; import java.util.UUID; import java.util.logging.Level; +import java.util.regex.Pattern; public class AST extends JavaPlugin { @@ -40,6 +45,8 @@ public class AST extends JavaPlugin { static AST plugin; static String nmsVersion; + static final Pattern MC_USERNAME_PATTERN = Pattern.compile("^[a-zA-Z0-9_]{1,16}$"); + @Override public void onLoad() { if (getServer().getPluginManager().getPlugin("WorldGuard") != null) { @@ -205,8 +212,15 @@ public class AST extends JavaPlugin { Bukkit.getScheduler().cancelTask(waitingForSkull.get(uuid).getValue()); waitingForSkull.remove(uuid); } - p.sendTitle(" ", ChatColor.GOLD + Config.enterName, 0, 600, 0); - p.sendMessage(ChatColor.GOLD + Config.enterName2 + " &"); + if(Config.useCommandForTextInput) { + String msg1 = ChatColor.GOLD + Config.enterNameC + ": " + ChatColor.GREEN + "/ast "; + p.sendTitle(" ", msg1, 0, 600, 0); + p.sendMessage(msg1); + p.sendMessage(ChatColor.GOLD + Config.enterNameC2 + ": " + ChatColor.GREEN + "/ast &"); + } else { + p.sendTitle(" ", ChatColor.GOLD + Config.enterName, 0, 600, 0); + p.sendMessage(ChatColor.GOLD + Config.enterName2 + " &"); + } int taskID = new BukkitRunnable() { @Override public void run() { @@ -224,8 +238,14 @@ public class AST extends JavaPlugin { Bukkit.getScheduler().cancelTask(waitingForName.get(uuid).getValue()); waitingForName.remove(uuid); } - p.sendTitle(" ", ChatColor.GOLD + Config.enterSkull, 0, 600, 0); - p.sendMessage(ChatColor.GOLD + Config.enterSkull); + if(Config.useCommandForTextInput) { + String msg1 = ChatColor.GOLD + Config.enterSkullC + ": " + ChatColor.GREEN + "/ast "; + p.sendTitle(" ", msg1, 0, 600, 0); + p.sendMessage(msg1); + } else { + p.sendTitle(" ", ChatColor.GOLD + Config.enterSkull, 0, 600, 0); + p.sendMessage(ChatColor.GOLD + Config.enterSkull); + } int taskID = new BukkitRunnable() { @Override public void run() { @@ -287,4 +307,86 @@ public class AST extends JavaPlugin { if(!Config.showDebugMessages) return; Bukkit.getLogger().log(Level.INFO, "[AST DEBUG] " + msg); } + + static ArmorStand getArmorStand(UUID uuid, World w) { + if (uuid != null && w != null) { + for (org.bukkit.entity.Entity e : w.getEntities()) { + if (e instanceof ArmorStand && e.getUniqueId().equals(uuid)) { + return (ArmorStand) e; + } + } + } + return null; + } + + @SuppressWarnings("deprecation") + static ItemStack getPlayerHead(String playerName) { + OfflinePlayer offlinePlayer = Bukkit.getServer().getPlayer(playerName); + if(offlinePlayer == null) { + offlinePlayer = Bukkit.getOfflinePlayer(playerName); + } + final ItemStack item = new ItemStack(Material.PLAYER_HEAD); + final SkullMeta meta = (SkullMeta) item.getItemMeta(); + if(meta == null) { + Bukkit.getLogger().warning("Skull item meta was null"); + return item; + } + meta.setOwningPlayer(offlinePlayer); + item.setItemMeta(meta); + return item; + } + + static boolean processInput(Player p, final String in) { + final UUID plrUuid = p.getUniqueId(); + final UUID uuid; + boolean name; + int taskId; + if(AST.waitingForName.containsKey(plrUuid)) { + uuid = AST.waitingForName.get(plrUuid).getKey(); + taskId = AST.waitingForName.get(plrUuid).getValue(); + name = true; + } else if(AST.waitingForSkull.containsKey(plrUuid)) { + uuid = AST.waitingForSkull.get(plrUuid).getKey(); + taskId = AST.waitingForSkull.get(plrUuid).getValue(); + name = false; + } else { + return false; + } + Bukkit.getScheduler().cancelTask(taskId); + new BukkitRunnable() { + @Override + public void run() { + final ArmorStand as = getArmorStand(uuid, p.getWorld()); + if (as != null) { + String input = ChatColor.translateAlternateColorCodes('&', in); + if(input.equals("&")) input = ""; + if(name) { + if (input.length() > 0) { + as.setCustomName(input); + as.setCustomNameVisible(true); + p.sendMessage(ChatColor.GREEN + Config.nameSet); + } else { + as.setCustomName(""); + as.setCustomNameVisible(false); + as.setCustomNameVisible(false); + p.sendMessage(ChatColor.GREEN + Config.nameRemoved); + } + } else { + if(MC_USERNAME_PATTERN.matcher(input).matches()) { + if(as.getEquipment() != null) { + as.getEquipment().setHelmet(getPlayerHead(input)); + p.sendMessage(ChatColor.GREEN + Config.skullSet); + } + } else { + p.sendMessage(ChatColor.RED + input + " " + Config.invalidName); + } + } + } + AST.waitingForName.remove(plrUuid); + AST.waitingForSkull.remove(plrUuid); + Utils.title(p, " "); + } + }.runTask(AST.plugin); + return true; + } } \ No newline at end of file diff --git a/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java b/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java index b4cc4e5..29bfd49 100644 --- a/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java +++ b/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/Commands.java @@ -25,6 +25,18 @@ class Commands implements CommandExecutor, TabCompleter { } String cmd = command.getName().toLowerCase(); if(cmd.equals("astools") || cmd.equals("ast")) { + if(Config.useCommandForTextInput && args.length > 0) { + StringBuilder sb = new StringBuilder(); + boolean space = false; + for(String s : args) { + if(space) sb.append(' '); + space = true; + sb.append(s); + } + if(AST.processInput(p, sb.toString())) { + return true; + } + } if (!Utils.hasPermissionNode(p, "astools.command")) { p.sendMessage(ChatColor.RED + Config.noCommandPerm); return true; diff --git a/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java b/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java index 0d8238f..95986a1 100644 --- a/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java +++ b/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/Config.java @@ -52,6 +52,7 @@ class Config { static boolean saveToolCreatesCommandBlock = true; static boolean logGeneratedSummonCommands = false; static boolean crouchRightClickOpensGUI = false; + static boolean useCommandForTextInput = false; static final ArrayList deniedCommands = new ArrayList<>(); @@ -72,7 +73,8 @@ class Config { errorExecutingCmd, isNotValidNumber, removedFromAs, listAssignedCmds, addACmd, removeACmd, cmdHelp, enterName, enterName2, enterSkull, inputTimeout, - nameSet, nameRemoved, skullSet; + nameSet, nameRemoved, skullSet, enterNameC, + enterNameC2, enterSkullC; static void reload() { reloadMainConfig(); @@ -109,6 +111,7 @@ class Config { saveToolCreatesCommandBlock = config.getBoolean("saveToolCreatesCommandBlock", true); logGeneratedSummonCommands = config.getBoolean("logGeneratedSummonCommands", false); crouchRightClickOpensGUI = config.getBoolean("crouchRightClickOpensGUI", false); + useCommandForTextInput = config.getBoolean("useCommandForTextInput", false); AST.activeTool.clear(); AST.selectedArmorStand.clear(); @@ -255,6 +258,9 @@ class Config { nameSet = languageConfig.getString("nameSet"); nameRemoved = languageConfig.getString("nameRemoved"); skullSet = languageConfig.getString("skullSet"); + enterNameC = languageConfig.getString("enterNameC"); + enterNameC2 = languageConfig.getString("enterNameC2"); + enterSkullC = languageConfig.getString("enterSkullC"); } private static ItemStack getItemStack(String configPath) { diff --git a/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java b/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java index 685ec55..a858462 100644 --- a/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java +++ b/src/main/java/com/gmail/St3venAU/plugins/ArmorStandTools/MainListener.java @@ -49,8 +49,6 @@ import java.util.regex.Pattern; @SuppressWarnings("CommentedOutCode") public class MainListener implements Listener { - private final Pattern MC_USERNAME_PATTERN = Pattern.compile("^[a-zA-Z0-9_]{1,16}$"); - @EventHandler public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent event) { if(!(event.getRightClicked() instanceof ArmorStand as)) return; @@ -359,75 +357,10 @@ public class MainListener implements Listener { @EventHandler public void onPlayerChat(final AsyncPlayerChatEvent event) { - final Player p = event.getPlayer(); - final UUID plrUuid = p.getUniqueId(); - final UUID uuid; - boolean name; - int taskId; - if(AST.waitingForName.containsKey(plrUuid)) { - uuid = AST.waitingForName.get(plrUuid).getKey(); - taskId = AST.waitingForName.get(plrUuid).getValue(); - name = true; - } else if(AST.waitingForSkull.containsKey(plrUuid)) { - uuid = AST.waitingForSkull.get(plrUuid).getKey(); - taskId = AST.waitingForSkull.get(plrUuid).getValue(); - name = false; - } else { - return; + if(Config.useCommandForTextInput) return; + if(AST.processInput(event.getPlayer(), event.getMessage())) { + event.setCancelled(true); } - event.setCancelled(true); - Bukkit.getScheduler().cancelTask(taskId); - new BukkitRunnable() { - @Override - public void run() { - final ArmorStand as = getArmorStand(uuid, p.getWorld()); - if (as != null) { - String input = ChatColor.translateAlternateColorCodes('&', event.getMessage()); - if(input.equals("&")) input = ""; - if(name) { - if (input.length() > 0) { - as.setCustomName(input); - as.setCustomNameVisible(true); - p.sendMessage(ChatColor.GREEN + Config.nameSet); - } else { - as.setCustomName(""); - as.setCustomNameVisible(false); - as.setCustomNameVisible(false); - p.sendMessage(ChatColor.GREEN + Config.nameRemoved); - } - } else { - if(MC_USERNAME_PATTERN.matcher(input).matches()) { - if(as.getEquipment() != null) { - as.getEquipment().setHelmet(getPlayerHead(input)); - p.sendMessage(ChatColor.GREEN + Config.skullSet); - } - } else { - p.sendMessage(ChatColor.RED + input + " " + Config.invalidName); - } - } - } - AST.waitingForName.remove(plrUuid); - AST.waitingForSkull.remove(plrUuid); - Utils.title(p, " "); - } - }.runTask(AST.plugin); - } - - @SuppressWarnings("deprecation") - private ItemStack getPlayerHead(String playerName) { - OfflinePlayer offlinePlayer = Bukkit.getServer().getPlayer(playerName); - if(offlinePlayer == null) { - offlinePlayer = Bukkit.getOfflinePlayer(playerName); - } - final ItemStack item = new ItemStack(Material.PLAYER_HEAD); - final SkullMeta meta = (SkullMeta) item.getItemMeta(); - if(meta == null) { - Bukkit.getLogger().warning("Skull item meta was null"); - return item; - } - meta.setOwningPlayer(offlinePlayer); - item.setItemMeta(meta); - return item; } @EventHandler @@ -443,17 +376,6 @@ public class MainListener implements Listener { } } - private ArmorStand getArmorStand(UUID uuid, World w) { - if (uuid != null && w != null) { - for (org.bukkit.entity.Entity e : w.getEntities()) { - if (e instanceof ArmorStand && e.getUniqueId().equals(uuid)) { - return (ArmorStand) e; - } - } - } - return null; - } - // Give all permissions to all players - for testing only /*@EventHandler public void onPlayerJoin(PlayerJoinEvent event) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e3e77d7..c9745a1 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -90,6 +90,7 @@ defaultASCmdCooldownTicks: 0 bypassWorldguardForASCmdExecution: false saveToolCreatesCommandBlock: true logGeneratedSummonCommands: false +useCommandForTextInput: false showDebugMessages: false # Uncomment the section below to deny players access to commands of your choice when they have any AST tools in their inventory diff --git a/src/main/resources/language.yml b/src/main/resources/language.yml index 1eac051..8f9079c 100644 --- a/src/main/resources/language.yml +++ b/src/main/resources/language.yml @@ -110,6 +110,10 @@ inputTimeout: 'Input timeout. Please try again.' nameSet: 'Armor stand name set' nameRemoved: 'Armor stand name removed' skullSet: 'Armor stand skull set' +#New since v4.4.1 +enterNameC: 'To set armor stand name' +enterNameC2: 'Or to remove the name' +enterSkullC: 'To set MC username for skull' ############################# # Tool names & descriptions # #############################