From 398f71255c5146f81af0ae64bd150b8c6a8d6e50 Mon Sep 17 00:00:00 2001
From: psrcek
Date: Thu, 2 Feb 2017 22:57:27 +0100
Subject: [PATCH] Created nametags
---
.../redstoner/modules/nametags/NameTags.java | 120 ++++++++++++++++++
src/com/redstoner/modules/nametags/Rank.java | 89 +++++++++++++
2 files changed, 209 insertions(+)
create mode 100644 src/com/redstoner/modules/nametags/NameTags.java
create mode 100644 src/com/redstoner/modules/nametags/Rank.java
diff --git a/src/com/redstoner/modules/nametags/NameTags.java b/src/com/redstoner/modules/nametags/NameTags.java
new file mode 100644
index 0000000..d4eee38
--- /dev/null
+++ b/src/com/redstoner/modules/nametags/NameTags.java
@@ -0,0 +1,120 @@
+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.scoreboard.Scoreboard;
+import org.bukkit.scoreboard.Team;
+
+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;
+
+@Version(major = 1, minor = 0, revision = 0)
+@AutoRegisterListener
+public class NameTags implements Module, Listener {
+ private boolean enabled = false;
+ private Scoreboard scoreboard = null;
+
+ @Override
+ public void onEnable() {
+ scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
+
+ for (Rank rank : Rank.values()) {
+ if (scoreboard.getTeam(rank.getScoreboardName()) == null) {
+ scoreboard.registerNewTeam(rank.getScoreboardName());
+ Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "scoreboard teams option " + rank.getScoreboardName() + " color " + rank.getColor());
+ }
+ }
+
+ enabled = true;
+ }
+
+ @Override
+ public void onDisable() {
+ enabled = false;
+ }
+
+ @Override
+ public boolean enabled() {
+ return enabled;
+ }
+
+ @Override
+ public String getCommandString() {
+ StringBuilder sb = new StringBuilder();
+
+ sb.append("command nametags {");
+ sb.append(" resort {");
+ sb.append(" help Re-sorts tab;");
+ sb.append(" perm utils.nametags.sort;");
+ sb.append(" run nt_resort;");
+ sb.append(" }");
+ sb.append("}");
+
+ return sb.toString();
+ }
+
+ @Command(hook = "nt_resort")
+ public void resortCmd(CommandSender sender) {
+ reSort();
+ Utils.sendMessage(sender, "Nametags", "&aResorted all players in tab! :P", '&');
+ }
+
+ @EventHandler
+ public void onPlayerJoin(PlayerJoinEvent e) {
+ reSort(e.getPlayer());
+ }
+
+ @EventHandler
+ public void onCommand(PlayerCommandPreprocessEvent e) {
+ if (!e.isCancelled()) {
+ String[] cmdSplit = e.getMessage().split(" ")[0].split(":");
+ String cmd = cmdSplit[cmdSplit.length - 1].replaceFirst("/", "");
+
+ if (cmd.equalsIgnoreCase("promote") || cmd.equalsIgnoreCase("demote")) {
+ reSort();
+ } else if (cmd.equalsIgnoreCase("pex")) {
+ String[] splitMsg = e.getMessage().split(" ");
+ if (splitMsg.length > 1 && (splitMsg[1].equalsIgnoreCase("promote") || splitMsg[1].equalsIgnoreCase("demote"))) {
+ reSort();
+ }
+ }
+ }
+ }
+
+ private void reSort() {
+ for (Player player : Bukkit.getOnlinePlayers()) {
+ reSort(player);
+ }
+ }
+
+ private void reSort(Player player) {
+ String playerName = player.getName();
+ Rank rank = Rank.VISITOR;
+
+ for (Rank r : Rank.values()) {
+ if (player.hasPermission(r.getPermission())) {
+ if (r.getPriority() > rank.getPriority()) {
+ rank = r;
+ }
+ }
+ }
+
+ for (Rank r : Rank.values()) {
+ Team team = scoreboard.getTeam(r.getScoreboardName());
+
+ if (team.hasEntry(playerName)) {
+ team.removeEntry(playerName);
+ }
+ }
+
+ scoreboard.getTeam(rank.getScoreboardName()).addEntry(playerName);
+ }
+}
diff --git a/src/com/redstoner/modules/nametags/Rank.java b/src/com/redstoner/modules/nametags/Rank.java
new file mode 100644
index 0000000..9959321
--- /dev/null
+++ b/src/com/redstoner/modules/nametags/Rank.java
@@ -0,0 +1,89 @@
+package com.redstoner.modules.nametags;
+
+public enum Rank {
+ VISITOR, MEMBER, BUILDER, TRUSTED, TRAININGMOD, MOD, ADMIN;
+
+ public String getScoreboardName() {
+ switch (this) {
+ case VISITOR:
+ return "g_visitor";
+ case MEMBER:
+ return "f_member";
+ case BUILDER:
+ return "e_builder";
+ case TRUSTED:
+ return "d_trusted";
+ case TRAININGMOD:
+ return "c_trainingmod";
+ case MOD:
+ return "b_mod";
+ case ADMIN:
+ return "a_admin";
+ default:
+ return "g_visitor";
+ }
+ }
+
+ public String getPermission() {
+ switch (this) {
+ case VISITOR:
+ return "group.visitor";
+ case MEMBER:
+ return "group.member";
+ case BUILDER:
+ return "group.builder";
+ case TRUSTED:
+ return "group.trusted";
+ case TRAININGMOD:
+ return "group.trainingmod";
+ case MOD:
+ return "group.mod";
+ case ADMIN:
+ return "group.admin";
+ default:
+ return "group.visitor";
+ }
+ }
+
+ public int getPriority() {
+ switch (this) {
+ case VISITOR:
+ return 1;
+ case MEMBER:
+ return 2;
+ case BUILDER:
+ return 3;
+ case TRUSTED:
+ return 4;
+ case TRAININGMOD:
+ return 5;
+ case MOD:
+ return 6;
+ case ADMIN:
+ return 7;
+ default:
+ return 1;
+ }
+ }
+
+ public String getColor() {
+ switch (this) {
+ case VISITOR:
+ return "gray";
+ case MEMBER:
+ return "white";
+ case BUILDER:
+ return "green";
+ case TRUSTED:
+ return "dark_aqua";
+ case TRAININGMOD:
+ return "red";
+ case MOD:
+ return "red";
+ case ADMIN:
+ return "dark_red";
+ default:
+ return "gray";
+ }
+ }
+}