From 1731483792a7d8d3c5c2fe7f718d482c2cf203b9 Mon Sep 17 00:00:00 2001 From: Pepich Date: Mon, 6 Feb 2017 22:10:51 +0100 Subject: [PATCH] Added nametags (tabsorter) module --- src/com/redstoner/misc/Main.java | 3 +- .../redstoner/modules/nametags/Nametags.java | 141 ++++++++++++++++++ 2 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 src/com/redstoner/modules/nametags/Nametags.java diff --git a/src/com/redstoner/misc/Main.java b/src/com/redstoner/misc/Main.java index d4dce64..abc6656 100644 --- a/src/com/redstoner/misc/Main.java +++ b/src/com/redstoner/misc/Main.java @@ -19,6 +19,7 @@ import com.redstoner.modules.lagchunks.LagChunks; import com.redstoner.modules.loginsecurity.LoginSecurity; import com.redstoner.modules.misc.Misc; import com.redstoner.modules.motd.Motd; +import com.redstoner.modules.nametags.Nametags; import com.redstoner.modules.pmtoggle.Pmtoggle; import com.redstoner.modules.saylol.Saylol; import com.redstoner.modules.scriptutils.Scriptutils; @@ -61,7 +62,7 @@ public class Main extends JavaPlugin // TODO: ModuleLoader.addModule(Mentio.class); ModuleLoader.addModule(Misc.class); ModuleLoader.addModule(Motd.class); - // TODO: ModuleLoader.addModule(NameTags.class); + ModuleLoader.addModule(Nametags.class); ModuleLoader.addModule(Pmtoggle.class); // TODO: ModuleLoader.addModule(Reports.class); ModuleLoader.addModule(Saylol.class); diff --git a/src/com/redstoner/modules/nametags/Nametags.java b/src/com/redstoner/modules/nametags/Nametags.java new file mode 100644 index 0000000..257140b --- /dev/null +++ b/src/com/redstoner/modules/nametags/Nametags.java @@ -0,0 +1,141 @@ +package com.redstoner.modules.nametags; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.server.ServerCommandEvent; + +import com.nemez.cmdmgr.Command; +import com.redstoner.annotations.AutoRegisterListener; +import com.redstoner.annotations.Version; +import com.redstoner.misc.Utils; +import com.redstoner.modules.Module; + +@AutoRegisterListener +@Version(major = 1, minor = 0, revision = 0, compatible = 1) +public class Nametags implements Module, Listener +{ + private boolean enabled = false; + + @Override + public void onEnable() + { + enabled = true; + } + + @Override + public void onDisable() + { + enabled = false; + } + + @Override + public boolean enabled() + { + return enabled; + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) + { + sortSpecific(event.getPlayer()); + } + + @EventHandler + public void commandPreprocessEvent(PlayerCommandPreprocessEvent event) + { + if (event.getMessage().contains("promote") || event.getMessage().contains("demote") + || event.getMessage().matches("pex user .* group (set|add|leave)")) + { + String[] args = event.getMessage().split(" "); + for (String s : args) + { + Player p = Bukkit.getPlayer(s); + if (p != null) + sortSpecific(p); + } + } + } + + @EventHandler + public void consoleCommand(ServerCommandEvent event) + { + if (event.getCommand().matches("(pro|de)mote")) + { + String[] args = event.getCommand().split(" "); + for (String s : args) + { + Player p = Bukkit.getPlayer(s); + if (p != null) + sortSpecific(p); + } + } + } + + @Command(hook = "sort") + public boolean sortAll(CommandSender sender) + { + for (Player p : Bukkit.getOnlinePlayers()) + sortSpecific(p); + Utils.sendMessage(sender, null, "Sorted tab for ya!"); + return true; + } + + @Command(hook = "sortspecific") + public boolean sortSpecific(CommandSender sender, String player) + { + Player p = Bukkit.getPlayer(player); + if (p == null) + { + Utils.sendErrorMessage(sender, null, "That player couldn't be found!"); + return true; + } + else + sortSpecific(p); + Utils.sendMessage(sender, null, "Sorted §e" + player + " §7for ya!"); + return true; + } + + public void sortSpecific(Player player) + { + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), + "scoreboard teams join " + getTeam(player) + " " + player.getName()); + } + + private String getTeam(Player player) + { + String[] teams = new String[] {"admin", "mod", "trainingmod", "trusted", "builder", "member", "visitor"}; + char prefix = 'a'; + for (String team : teams) + { + if (player.hasPermission("group." + team)) + { + return prefix + "_" + team; + } + prefix++; + } + return "g_visitor"; + } + + // @noformat + @Override + public String getCommandString() + { + return "command tab {\n" + + " sort {\n" + + " help Resorts the entirety of tab.;\n" + + " run sort;\n" + + " }\n" + + " sort [string:player] {\n" + + " help Resorts one player.;\n" + + " run sortspecific player;\n" + + " }\n" + + " perm utils.tab.admin;\n" + + "}"; + } + // @format +}