From 8ad681d9f707db72332bc10d348d0e7013ed2488 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Wed, 12 Sep 2018 10:49:58 -0500 Subject: [PATCH] Initial commit to Gitea --- .gitignore | 4 + pom.xml | 156 ++++++++ .../xyz/etztech/deluxegroups/DeluxeGroup.java | 139 +++++++ .../etztech/deluxegroups/DeluxeGroups.java | 237 +++++++++++ .../xyz/etztech/deluxegroups/DeluxeUtil.java | 47 +++ .../deluxegroups/command/CommandGroup.java | 372 ++++++++++++++++++ .../deluxegroups/command/CommandMain.java | 71 ++++ .../listeners/AsyncPlayerChatListener.java | 125 ++++++ .../listeners/SessionListener.java | 31 ++ src/main/resources/config.yml | 13 + src/main/resources/groups.yml | 2 + src/main/resources/plugin.yml | 40 ++ 12 files changed, 1237 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/xyz/etztech/deluxegroups/DeluxeGroup.java create mode 100644 src/main/java/xyz/etztech/deluxegroups/DeluxeGroups.java create mode 100644 src/main/java/xyz/etztech/deluxegroups/DeluxeUtil.java create mode 100644 src/main/java/xyz/etztech/deluxegroups/command/CommandGroup.java create mode 100644 src/main/java/xyz/etztech/deluxegroups/command/CommandMain.java create mode 100644 src/main/java/xyz/etztech/deluxegroups/listeners/AsyncPlayerChatListener.java create mode 100644 src/main/java/xyz/etztech/deluxegroups/listeners/SessionListener.java create mode 100644 src/main/resources/config.yml create mode 100644 src/main/resources/groups.yml create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3d13e3f --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +*.iml +target/ +dependency-reduced-pom.xml \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..1208607 --- /dev/null +++ b/pom.xml @@ -0,0 +1,156 @@ + + 4.0.0 + xyz.etztech + DeluxeGroups + + 1.1 + jar + + + + DeluxeGroups + A plugin used to add group channels to DeluxeChat. (or any chat plugin, really) + http://www.etztech.xyz + + + + Zlib License + http://opensource.org/licenses/Zlib + Copyright (c) 2017 EtzTech + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. + + + + + + EtzTech + http://www.etztech.xyz + + + + + + EtzTech + xyz.etztech.deluxegroups.DeluxeGroups + UTF-8 + + + + + org.spigotmc + spigot-api + 1.13.1-R0.1-SNAPSHOT + + + org.dynmap + DynmapCoreAPI + 2.0 + + + xyz.etztech + EtzCore + 1.0 + + + commons-lang + commons-lang + 2.6 + + + + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/groups/public/ + + + dynmap-repo + http://repo.mikeprimm.com + + + etztech-repo + http://repo.etztech.xyz + + + mvn-repo + https://mvnrepository.com/artifact/ + + + jcenter + http://jcenter.bintray.com + + + jitpack.io + https://jitpack.io + + + + + src/main/java + clean install + + + src/main/resources + + true + + plugin.yml + config.yml + + + + src/main/resources + + false + + **/*.java + plugin.yml + + + + + + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.0.2 + + + + true + lib/ + xyz.etztech.deluxegroups.DeluxeGroups + + + + + + + \ No newline at end of file diff --git a/src/main/java/xyz/etztech/deluxegroups/DeluxeGroup.java b/src/main/java/xyz/etztech/deluxegroups/DeluxeGroup.java new file mode 100644 index 0000000..d17ad81 --- /dev/null +++ b/src/main/java/xyz/etztech/deluxegroups/DeluxeGroup.java @@ -0,0 +1,139 @@ +package xyz.etztech.deluxegroups; + +import net.md_5.bungee.api.chat.BaseComponent; +import org.apache.commons.lang.StringUtils; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; + +import java.io.FileWriter; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.List; + +public class DeluxeGroup { + + private String name; + private String password; + private boolean permanent; + private ArrayList groupList; + private FileWriter file; + private PrintWriter log; + + public DeluxeGroup() { + this.name = ""; + this.password = ""; + this.permanent = false; + this.groupList = new ArrayList<>(); + } + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + try { + this.file = new FileWriter(DeluxeGroups.getInstance().getDataFolder().getAbsolutePath()+"/logs/"+name.toLowerCase()+".log", true); + this.log = new PrintWriter(this.file); + } catch (Exception e) { + DeluxeGroups.getInstance().log("Could not connect to group log for " + name + "."); + } + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public boolean getPermanent() { + return permanent; + } + + public void setPermanent(boolean permanent) { + this.permanent = permanent; + } + + public ArrayList getGroupList() { + return groupList; + } + + public void setGroupList(ArrayList groupList) { + this.groupList = groupList; + } + + public boolean hasPassword() { + return StringUtils.isNotBlank(this.password); + } + + public void add(Player player) { + this.groupList.add(player); + } + + public void remove(Player player) { + for (int i = 0; i < this.groupList.size(); i++) { + if (this.groupList.get(i).getUniqueId().toString().equals(player.getUniqueId().toString())) { + this.groupList.remove(i); + return; + } + } + } + + public void remove(String uuid) { + for (int i = 0; i < this.groupList.size(); i++) { + if (this.groupList.get(i).getUniqueId().toString().equals(uuid)) { + this.groupList.remove(i); + return; + } + } + } + + public boolean contains(Player player) { + return this.groupList.contains(player); + } + + public ArrayList getOnline() { + ArrayList onlineList = new ArrayList(); + for (OfflinePlayer player : this.groupList) { + if (player.isOnline()) { + onlineList.add(player); + } + } + return onlineList; + } + + public int getSize() { + return this.groupList.size(); + } + + public List getUUIDList() { + ArrayList uuidList = new ArrayList<>(); + for (OfflinePlayer player : this.groupList) { + uuidList.add(player.getUniqueId().toString()); + } + return uuidList; + } + + public void sendMessage(String message) { + //DeluxeUtil.log(this.log, message); + Player on; + for (OfflinePlayer player : this.groupList) { + if (player.isOnline()) { + on = Bukkit.getPlayer(player.getName()); + on.sendMessage(ChatColor.AQUA + message); + } + } + } + + public void log(String player, String chat) { + String message = player + ": " + chat; + DeluxeUtil.log(this.log, message, false); + } + + +} diff --git a/src/main/java/xyz/etztech/deluxegroups/DeluxeGroups.java b/src/main/java/xyz/etztech/deluxegroups/DeluxeGroups.java new file mode 100644 index 0000000..593b28e --- /dev/null +++ b/src/main/java/xyz/etztech/deluxegroups/DeluxeGroups.java @@ -0,0 +1,237 @@ +package xyz.etztech.deluxegroups; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.OfflinePlayer; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.dynmap.DynmapCommonAPI; +import xyz.etztech.core.api.IMinecraftManager; +import xyz.etztech.deluxegroups.command.CommandGroup; +import xyz.etztech.deluxegroups.command.CommandMain; +import xyz.etztech.deluxegroups.listeners.AsyncPlayerChatListener; +import xyz.etztech.deluxegroups.listeners.SessionListener; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.*; +import java.util.logging.Logger; + +public class DeluxeGroups extends JavaPlugin { + + private static DeluxeGroups instance; + + public static FileConfiguration config; + public static FileConfiguration groupConfig; + private Logger log = Logger.getLogger( "Minecraft" ); + + protected static Map inGroup = new HashMap(); + protected static Map groups = new HashMap(); + + // Objects that can be reloaded + AsyncPlayerChatListener chatListener; + + // Dynmap API + private static DynmapCommonAPI dynmap = null; + + // MinecraftManager API + private static IMinecraftManager minecraftManager = null; + + + + @Override + public void onEnable() { + + instance = this; + saveDefaultConfig(); + reloadConfig(); + + try { + File logs = new File(getDataFolder().getAbsolutePath()+"/logs/"); + logs.mkdir(); + } catch (Exception e) {} + + + if (isEnabled()) { + + // Dynmap integration + if (Bukkit.getPluginManager().isPluginEnabled("dynmap")) { + dynmap = (DynmapCommonAPI) Bukkit.getPluginManager().getPlugin("dynmap"); + Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "IMPORTANT: Disabling Dynmap Game->Web Chat. All web chat will be handled by DeluxeGroups."); + dynmap.setDisableChatToWebProcessing(true); + getServer().getPluginManager().registerEvents(new SessionListener(), this); + } + + // MCM integration + if (Bukkit.getPluginManager().isPluginEnabled("MinecraftManager")) { + minecraftManager = (IMinecraftManager) Bukkit.getPluginManager().getPlugin("MinecraftManager"); + Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "IMPORTANT: Disabling MinecraftManager global logging. All global logging will be handled by DeluxeGroups."); + minecraftManager.logOverride(true); + } + + // Add Commands + CommandMain cmdMain = new CommandMain(this); + this.getCommand("deluxegroups").setExecutor(cmdMain); + CommandGroup cmdGroup = new CommandGroup(this); + this.getCommand("group").setExecutor(cmdGroup); + + + + // Add Listeners + chatListener = new AsyncPlayerChatListener(this); + getServer().getPluginManager().registerEvents(chatListener, this); + } + } + + @Override + public void onDisable() { + log("Saving groups."); + String root; + for (DeluxeGroup group : groups.values()) { + root = group.getName().toLowerCase(); + groupConfig.createSection(root); + groupConfig.set(root + ".name", group.getName()); + groupConfig.set(root + ".password", group.getPassword()); + groupConfig.set(root + ".permanent", group.getPermanent()); + groupConfig.set(root + ".players", group.getSize() > 0 ? group.getUUIDList() : null); + } + File groupf = new File(getDataFolder(), "groups.yml"); + try { + PrintWriter pw = new PrintWriter(groupf); + pw.write("# This is a file for saving all your groups\n" + + "# Do not manually edit this file!\n\n"); + pw.close(); + } catch (Exception ex) {} + try { + groupf.createNewFile(); + groupConfig.save(groupf); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void loadConfig() { + config = Bukkit.getPluginManager().getPlugin("DeluxeGroups").getConfig(); + } + + @Override + public void reloadConfig() { + super.reloadConfig(); + loadConfig(); + loadGroupConfig(); + if (chatListener != null) { + chatListener.reload(); + } + } + public void loadGroupConfig() { + File groupf = new File(getDataFolder(), "groups.yml"); + + if (!groupf.exists()) { + groupf.getParentFile().mkdirs(); + saveResource("groups.yml", false); + } + groupConfig = new YamlConfiguration(); + + try { + groupConfig.load(groupf); + int loaded = 0; + OfflinePlayer offlinePlayer; + DeluxeGroup group; + for (String key : groupConfig.getKeys(false)) { + group = new DeluxeGroup(); + group.setName(groupConfig.getString(key + ".name")); + group.setPassword(groupConfig.getString(key + ".password")); + group.setPermanent(groupConfig.getBoolean(key + ".permanent")); + ArrayList playerList = new ArrayList<>(); + for (String uuid : groupConfig.getStringList(key + ".players")) { + offlinePlayer = Bukkit.getOfflinePlayer(UUID.fromString(uuid)); + playerList.add(offlinePlayer); + setInGroup(uuid, group.getName()); + } + group.setGroupList(playerList); + addGroup(group); + loaded++; + } + log("Loaded " + loaded + " groups!"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void log(String message) { + log.info( "[DeluxeGroups]: " + message ); + } + + public static DeluxeGroups getInstance() { + return instance; + } + + public static Collection getGroups() { + return groups.values(); + } + + public static void addGroup(DeluxeGroup group) { + groups.put(group.getName().toLowerCase(), group); + } + + public static void removeGroup(DeluxeGroup group) { + groups.remove(group.getName().toLowerCase()); + } + + public static DeluxeGroup getGroup(String name) { + name = name.toLowerCase(); + if (groups.containsKey(name)) { + return groups.get(name); + } + return null; + } + + public static void setInGroup(String uuid, String group) { + inGroup.put(uuid, group.toLowerCase()); + } + + public static void removeFromInGroup(String uuid) { + inGroup.remove(uuid); + } + + public static String getInGroup(String uuid) { + if (inGroup.containsKey(uuid)) { + return inGroup.get(uuid); + } + return null; + } + + public static void addToGroup(Player uuid, String group) { + group = group.toLowerCase(); + if (groups.containsKey(group)) { + groups.get(group).add(uuid); + } + } + + public static void removeFromGroup(Player player, String group) { + group = group.toLowerCase(); + if (groups.containsKey(group)) { + groups.get(group).remove(player); + } + } + + public static void removeFromGroup(OfflinePlayer player, String group) { + group = group.toLowerCase(); + if (groups.containsKey(group)) { + groups.get(group).remove(player.getUniqueId().toString()); + } + } + + public static DynmapCommonAPI getDynmap() { + return dynmap; + } + + public static IMinecraftManager getMinecraftManager() { + return minecraftManager; + } + +} + diff --git a/src/main/java/xyz/etztech/deluxegroups/DeluxeUtil.java b/src/main/java/xyz/etztech/deluxegroups/DeluxeUtil.java new file mode 100644 index 0000000..b4c53a8 --- /dev/null +++ b/src/main/java/xyz/etztech/deluxegroups/DeluxeUtil.java @@ -0,0 +1,47 @@ +package xyz.etztech.deluxegroups; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.io.PrintWriter; +import java.text.SimpleDateFormat; +import java.util.*; + +public class DeluxeUtil { + + public static void sms(Player player, String message) { + player.sendMessage(message); + } + + public static UUID asUUID(String uuid) { + try { + if (uuid.contains("-") && uuid.length() == 36) { + return UUID.fromString(uuid); + } else if (uuid.length() == 32) { + return UUID.fromString(uuid.substring(0, 8) + "-" + uuid.substring(8, 12) + "-" + uuid.substring(12, 16) + + "-" + uuid.substring(16, 20) + "-" + uuid.substring(20, 32)); + } else { + return null; + } + } catch (Exception ex) { + return null; + } + } + + public static void log(PrintWriter logger, String message, boolean sendToConsole) { + if (sendToConsole) { + Bukkit.getServer().getConsoleSender().sendMessage(message); + } + Date date = new Date(); + SimpleDateFormat sdf = new SimpleDateFormat("M/d/yy h:m a"); + String text = "[" + sdf.format(date) + "] " + message; + logger.append(text + "\n"); + logger.flush(); + } + + public static Boolean resolveBoolean(String text) { + List yes = new ArrayList<>(Arrays.asList("yes", "true", "1")); + List no = new ArrayList<>(Arrays.asList("no", "false", "0")); + return yes.contains(text) ? true : no.contains(text) ? false : null; + } +} diff --git a/src/main/java/xyz/etztech/deluxegroups/command/CommandGroup.java b/src/main/java/xyz/etztech/deluxegroups/command/CommandGroup.java new file mode 100644 index 0000000..b0f6bbc --- /dev/null +++ b/src/main/java/xyz/etztech/deluxegroups/command/CommandGroup.java @@ -0,0 +1,372 @@ +package xyz.etztech.deluxegroups.command; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.OfflinePlayer; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import xyz.etztech.deluxegroups.DeluxeGroup; +import xyz.etztech.deluxegroups.DeluxeGroups; +import xyz.etztech.deluxegroups.DeluxeUtil; + +public class CommandGroup + implements CommandExecutor +{ + DeluxeGroups plugin; + + public CommandGroup(DeluxeGroups paramDeluxeGroups) + { + this.plugin = paramDeluxeGroups; + } + + public boolean onCommand(CommandSender sender, Command command, String base, String[] args) + { + if (!(sender instanceof Player)) + { + sender.sendMessage("This command is not supported in console yet!"); + return true; + } + Player localPlayer1 = (Player)sender; + + if (args.length == 0) { + if (DeluxeGroups.getInGroup(localPlayer1.getUniqueId().toString()) != null) { + DeluxeGroup deluxeGroup = DeluxeGroups.getGroup(DeluxeGroups.getInGroup(localPlayer1.getUniqueId().toString())); + DeluxeUtil.sms(localPlayer1, ChatColor.AQUA + "You are in group " + deluxeGroup.getName()); + } else { + DeluxeUtil.sms(localPlayer1, ChatColor.RED + "You are not in a group!"); + } + } else { + String subcommand = args[0]; + if ("create".equals(subcommand.toLowerCase())) { + createGroup(localPlayer1, args); + } else if ("join".equals(subcommand.toLowerCase())) { + joinGroup(localPlayer1, args); + } else if ("leave".equals(subcommand.toLowerCase())) { + leaveGroup(localPlayer1, args, false); + } else if ("list".equals(subcommand.toLowerCase())) { + listGroup(localPlayer1, args); + } else if ("password".equals(subcommand.toLowerCase())) { + modifyPassword(localPlayer1, args); + } else if ("permanent".equals(subcommand.toLowerCase())) { + modifyPermanent(localPlayer1, args); + } else if ("kick".equals(subcommand.toLowerCase())) { + kick(localPlayer1, args); + } else { + help(localPlayer1); + } + } + + return true; + } + + private boolean groupExists(String groupName) { + for (DeluxeGroup group : DeluxeGroups.getGroups()) { + if (group.getName().toLowerCase().equals(groupName.toLowerCase())) { + return true; + } + } + return false; + } + + private void createGroup(Player player, String[] args) { + if (args.length < 2) { + DeluxeUtil.sms(player, ChatColor.RED + Usage.CREATE.getFullUsage(player)); + return; + } + if (DeluxeGroups.getInGroup(player.getUniqueId().toString()) != null) { + DeluxeUtil.sms(player, ChatColor.RED + "You are already in a group! Leave it first to create a new one."); + return; + } + DeluxeGroup cg = new DeluxeGroup(); + if (!groupExists(args[1])) { + cg.setName(args[1]); + if (args.length == 2) { + DeluxeUtil.sms(player, ChatColor.AQUA + "Creating " + args[1] + " with no password."); + } else if ( args.length == 3) { + DeluxeUtil.sms(player, ChatColor.RED + "You didn't confirm your password."); + return; + } else if (args.length == 4) { + String password1 = args[2]; + String password2 = args[3]; + if (password1.equals(password2)) { + cg.setPassword(args[2]); + DeluxeUtil.sms(player, ChatColor.AQUA + "Creating " + args[1] + " with password " + args[2] + "."); + } else { + DeluxeUtil.sms(player, ChatColor.RED + "Your passwords did not match!"); + return; + } + } else { + DeluxeUtil.sms(player, ChatColor.RED + "Too many parameters!"); + return; + } + cg.add(player); + DeluxeGroups.setInGroup(player.getUniqueId().toString(), args[1]); + DeluxeGroups.addGroup(cg); + } else { + DeluxeUtil.sms(player, ChatColor.RED + "A group with that name already exists!"); + } + } + + private void joinGroup(Player player, String[] args) { + if (args.length < 2) { + DeluxeUtil.sms(player, ChatColor.RED + Usage.JOIN.getFullUsage(player)); + return; + } + if (groupExists(args[1])) { + if (DeluxeGroups.getInGroup(player.getUniqueId().toString()) != null) { + if (args[1].toLowerCase().equals(DeluxeGroups.getInGroup(player.getUniqueId().toString()).toLowerCase())) { + DeluxeUtil.sms(player, ChatColor.RED + "You are already in " + + DeluxeGroups.getInGroup(player.getUniqueId().toString())); + return; + } + leaveGroup(player, args, true); + } + DeluxeGroup group = DeluxeGroups.getGroup(args[1]); + if (args.length == 2) { + if (!group.hasPassword()) { + group.sendMessage(player.getName() + " has joined the group!"); + DeluxeUtil.sms(player, ChatColor.AQUA + "You have joined " + group.getName() + "!"); + DeluxeGroups.setInGroup(player.getUniqueId().toString(), group.getName()); + DeluxeGroups.addToGroup(player, group.getName()); + } else { + DeluxeUtil.sms(player, ChatColor.RED + group.getName() + " is password protected."); + } + } else if ( args.length == 3) { + if (group.hasPassword() && args[2].equals(group.getPassword())) { + group.sendMessage(player.getName() + " has joined the group!"); + DeluxeUtil.sms(player, ChatColor.AQUA + "You have joined " + group.getName() + "!"); + DeluxeGroups.setInGroup(player.getUniqueId().toString(), group.getName()); + DeluxeGroups.addToGroup(player, group.getName()); + } else if (!group.hasPassword()) { + group.sendMessage(player.getName() + " has joined the group!"); + DeluxeUtil.sms(player, ChatColor.AQUA + "You have joined " + group.getName() + "!"); + DeluxeGroups.setInGroup(player.getUniqueId().toString(), group.getName()); + DeluxeGroups.addToGroup(player, group.getName()); + } else { + DeluxeUtil.sms(player, ChatColor.RED + "Incorrect password."); + } + } else { + DeluxeUtil.sms(player, ChatColor.RED + "Too many parameters!"); + } + } else { + DeluxeUtil.sms(player, ChatColor.RED + "A group with that name does not exist!"); + } + } + + private void listGroup(Player player, String[] args) { + if (args.length == 1) { + if (DeluxeGroups.getInGroup(player.getUniqueId().toString()) != null) { + DeluxeGroup deluxeGroup = DeluxeGroups.getGroup(DeluxeGroups.getInGroup(player.getUniqueId().toString())); + StringBuilder message = new StringBuilder(ChatColor.DARK_AQUA + "===== " + deluxeGroup.getName() + " ====="); + for (OfflinePlayer groupPlayer : deluxeGroup.getGroupList()) { + if (groupPlayer.isOnline()) { + message.append("\n" + ChatColor.AQUA + groupPlayer.getName() + " (" + ChatColor.GREEN + "online" + ChatColor.AQUA + ")"); + } else { + message.append("\n" + ChatColor.AQUA + groupPlayer.getName() + " (" + ChatColor.RED + "offline" + ChatColor.AQUA + ")"); + } + } + DeluxeUtil.sms(player, message.toString()); + } else { + DeluxeUtil.sms(player, ChatColor.RED + "You are not in a group!"); + } + } else if (args.length == 2) { + if (player.hasPermission("deluxegroups.admin")) { + DeluxeGroup carrotGroup = DeluxeGroups.getGroup(args[1]); + if (carrotGroup != null) { + StringBuilder message = new StringBuilder(ChatColor.DARK_AQUA + "===== " + carrotGroup.getName() + " ====="); + for (OfflinePlayer groupPlayer : carrotGroup.getGroupList()) { + if (groupPlayer.isOnline()) { + message.append("\n" + ChatColor.AQUA + groupPlayer.getName() + " (" + ChatColor.GREEN + "online" + ChatColor.AQUA + ")"); + } else { + message.append("\n" + ChatColor.AQUA + groupPlayer.getName() + " (" + ChatColor.RED + "offline" + ChatColor.AQUA + ")"); + } + } + DeluxeUtil.sms(player, message.toString()); + } else { + DeluxeUtil.sms(player, ChatColor.RED + "There is no group with that name!"); + } + } else { + DeluxeUtil.sms(player, ChatColor.RED + "You do not have permission to use this command!"); + } + } else { + DeluxeUtil.sms(player, ChatColor.RED + Usage.LIST.getFullUsage(player)); + } + } + + private void leaveGroup(Player player, String[] args, boolean fromJoin) { + if (DeluxeGroups.getInGroup(player.getUniqueId().toString()) != null) { + DeluxeGroups.removeFromGroup(player, DeluxeGroups.getInGroup(player.getUniqueId().toString())); + DeluxeGroup leaveGroup = DeluxeGroups.getGroup(DeluxeGroups.getInGroup(player.getUniqueId().toString())); + DeluxeGroups.removeFromInGroup(player.getUniqueId().toString()); + DeluxeUtil.sms(player, ChatColor.AQUA + "You have left " + leaveGroup.getName()); + leaveGroup.sendMessage(ChatColor.AQUA + player.getName() + " has left the group."); + if (leaveGroup.getSize() < 1 && !leaveGroup.getPermanent()) { + // Disband + DeluxeGroups.removeGroup(leaveGroup); + } + } else if (!fromJoin){ + DeluxeUtil.sms(player, ChatColor.RED + "You are not in a group!"); + } + } + + private void modifyPassword(Player player, String[] args) { + if (!player.hasPermission("deluxegroups.admin")) { + DeluxeUtil.sms(player, ChatColor.RED + "You do not have permission to use this command!"); + return; + } + if (args.length < 4) { + DeluxeUtil.sms(player, ChatColor.RED + Usage.PASSWORD.getFullUsage(player)); + return; + } + DeluxeGroup group = DeluxeGroups.getGroup(args[1]); + if (group != null) { + if (args[2].equals(args[3])) { + DeluxeGroups.getGroup(args[1]).setPassword(args[2]); + DeluxeUtil.sms(player, ChatColor.GREEN + "Password for " + group.getName() + " changed to '" + group.getPassword() + "'."); + } else { + DeluxeUtil.sms(player, ChatColor.RED + "Passwords did not match."); + } + } else { + DeluxeUtil.sms(player, ChatColor.RED + "Group '" + args[1] + "' does not exist."); + } + } + + private void modifyPermanent(Player player, String[] args) { + if (!player.hasPermission("deluxegroups.admin")) { + DeluxeUtil.sms(player, ChatColor.RED + "You do not have permission to use this command!"); + return; + } + if (args.length < 3) { + DeluxeUtil.sms(player, ChatColor.RED + Usage.PERMANENT.getFullUsage(player)); + return; + } + DeluxeGroup group = DeluxeGroups.getGroup(args[1]); + if (group != null) { + Boolean perm = DeluxeUtil.resolveBoolean(args[2]); + if (perm != null) { + DeluxeGroups.getGroup(args[1]).setPermanent(perm); + DeluxeUtil.sms(player, ChatColor.GREEN + group.getName() + " is " + (group.getPermanent() ? "now" : "no longer") + " permanent."); + if (!group.getPermanent() && group.getSize() < 1) { + // Disband + DeluxeGroups.removeGroup(group); + } + } else { + DeluxeUtil.sms(player, ChatColor.RED + Usage.PERMANENT.getFullUsage(player)); + } + } else { + DeluxeUtil.sms(player, ChatColor.RED + "Group '" + args[1] + "' does not exist."); + } + } + + private void kick(Player player, String[] args) { + if (!player.hasPermission("deluxegroups.admin")) { + DeluxeUtil.sms(player, ChatColor.RED + "You do not have permission to use this command!"); + return; + } + if (args.length < 2) { + DeluxeUtil.sms(player, ChatColor.RED + Usage.KICK.getFullUsage(player)); + return; + } + Player argPlayer = Bukkit.getPlayer(args[1]); + if (argPlayer != null) { + if (DeluxeGroups.getInGroup(argPlayer.getUniqueId().toString()) != null) { + DeluxeGroup group = DeluxeGroups.getGroup(DeluxeGroups.getInGroup(argPlayer.getUniqueId().toString())); + DeluxeGroups.removeFromGroup(argPlayer, group.getName()); + //group.remove(argPlayer.getUniqueId().toString()); + DeluxeGroups.removeFromInGroup(argPlayer.getUniqueId().toString()); + DeluxeUtil.sms(player, ChatColor.GREEN + argPlayer.getName() + " was kicked from " + group.getName() + "."); + DeluxeUtil.sms(argPlayer, ChatColor.RED + "You were kicked from " + group.getName() + "."); + } else { + DeluxeUtil.sms(player, ChatColor.RED + argPlayer.getName() + " is not in a group."); + } + } else { + OfflinePlayer offPlayer; + try { + offPlayer = Bukkit.getOfflinePlayer(DeluxeUtil.asUUID(args[1])); + } catch (Exception ex) { + offPlayer = null; + } + if (offPlayer != null) { + if (DeluxeGroups.getInGroup(offPlayer.getUniqueId().toString()) != null) { + DeluxeGroup group = DeluxeGroups.getGroup(DeluxeGroups.getInGroup(offPlayer.getUniqueId().toString())); + DeluxeGroups.removeFromGroup(offPlayer, group.getName()); + DeluxeGroups.removeFromInGroup(offPlayer.getUniqueId().toString()); + DeluxeUtil.sms(player, ChatColor.GREEN + offPlayer.getName() + " was kicked from " + group.getName() + "."); + if (offPlayer.isOnline()) { + Player onPlayer = Bukkit.getPlayer(offPlayer.getUniqueId()); + DeluxeUtil.sms(onPlayer, ChatColor.RED + "You were kicked from " + group.getName() + "."); + } + } else { + DeluxeUtil.sms(player, ChatColor.RED + offPlayer.getName() + " is not in a group."); + } + } else { + DeluxeUtil.sms(player, ChatColor.RED + "Player not found."); + } + } + } + + private void help(Player player) { + StringBuilder message = new StringBuilder(ChatColor.GOLD + "===== Group Help ====="); + for (Usage usage : Usage.values()) { + if (usage.canUse(player)) { + message.append("\n" + ChatColor.GREEN + usage.getFullUsage(player)); + } + } + DeluxeUtil.sms(player, message.toString()); + } + + enum Usage { + + CREATE("/group create [ ]"), + JOIN("/group join []"), + LEAVE("/group leave"), + LIST("/group list []", "deluxegroups.admin", "/group list"), + PASSWORD("/group password [ ]", "deluxegroups.admin"), + PERMANENT("/group permanent ", "deluxegroups.admin"), + KICK("/group kick ", "deluxegroups.admin"); + + private String usage; + private String permission; + private String alternate; + + Usage(String usage) { + this.usage = usage; + this.permission = null; + this.alternate = null; + } + + Usage(String usage, String permission) { + this.usage = usage; + this.permission = permission; + this.alternate = null; + } + + Usage(String usage, String permission, String alternate) { + this.usage = usage; + this.permission = permission; + this.alternate = alternate; + } + + public String getUsage() { + return usage; + } + + public String getPermission() { + return permission; + } + + public String getAlternate() { + return alternate; + } + + public boolean canUse(Player player) { + return (permission != null && player.hasPermission(permission)) || alternate != null; + } + + public String getFullUsage(Player player) { + return (permission == null || player.hasPermission(permission)) ? usage : alternate; + } + } +} diff --git a/src/main/java/xyz/etztech/deluxegroups/command/CommandMain.java b/src/main/java/xyz/etztech/deluxegroups/command/CommandMain.java new file mode 100644 index 0000000..2c0bd2a --- /dev/null +++ b/src/main/java/xyz/etztech/deluxegroups/command/CommandMain.java @@ -0,0 +1,71 @@ +package xyz.etztech.deluxegroups.command; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import xyz.etztech.deluxegroups.DeluxeGroups; + +public class CommandMain implements CommandExecutor { + + DeluxeGroups plugin; + + + public CommandMain(DeluxeGroups plugin) { + this.plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String base, String[] args) { + if (args.length == 0) { + other(sender); + } else if (args.length == 1) { + switch (args[0].toLowerCase()) { + case "help": + sender.sendMessage(ChatColor.GOLD + "----- DeluxeGroups Commands -----"); + sender.sendMessage(ChatColor.YELLOW + "/dg reload - Reload the config"); + sender.sendMessage(ChatColor.GOLD + "----- DeluxeGroups Group Commands -----"); + sender.sendMessage(ChatColor.YELLOW + "/group - Display your group"); + sender.sendMessage(ChatColor.YELLOW + "/group create - Create a new group."); + sender.sendMessage(ChatColor.YELLOW + "/group join - Join a group"); + sender.sendMessage(ChatColor.YELLOW + "/group leave - Leave a group"); + sender.sendMessage(ChatColor.YELLOW + "/group list - List the players in a group"); + sender.sendMessage(ChatColor.YELLOW + "/group password - Modify the password for your group"); + sender.sendMessage(ChatColor.YELLOW + "/group kick - Kick a player from the group"); + break; + case "reload": + reload(sender); + break; + default: + other(sender); + } + } + + return true; + } + + private void other(CommandSender sender) { + String version = Bukkit.getPluginManager().getPlugin("DeluxeGroups").getDescription().getVersion(); + sender.sendMessage(ChatColor.GOLD + "----- DeluxeGroups v" + version + " -----"); + sender.sendMessage(ChatColor.YELLOW + "Developed by EtzTech"); + sender.sendMessage(ChatColor.YELLOW + "Use \"/dg help\" for help."); + } + + + private void reload(CommandSender sender) { + if (!sender.hasPermission("deluxegroups.admin")) { + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command."); + return; + } + try { + sender.sendMessage("Reloading DeluxeGroups..."); + this.plugin.reloadConfig(); + sender.sendMessage("DeluxeGroups reloaded."); + } catch (Exception ex) { + sender.sendMessage("DeluxeGroups reload was interrupted."); + } + } + + +} diff --git a/src/main/java/xyz/etztech/deluxegroups/listeners/AsyncPlayerChatListener.java b/src/main/java/xyz/etztech/deluxegroups/listeners/AsyncPlayerChatListener.java new file mode 100644 index 0000000..ae121df --- /dev/null +++ b/src/main/java/xyz/etztech/deluxegroups/listeners/AsyncPlayerChatListener.java @@ -0,0 +1,125 @@ +package xyz.etztech.deluxegroups.listeners; + +import org.apache.commons.lang.StringUtils; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.AsyncPlayerChatEvent; +import org.dynmap.DynmapCommonAPI; +import xyz.etztech.core.api.IMinecraftManager; +import xyz.etztech.deluxegroups.DeluxeGroup; +import xyz.etztech.deluxegroups.DeluxeGroups; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class AsyncPlayerChatListener implements Listener { + + private DeluxeGroups plugin; + private List groupPrefixes; + + public AsyncPlayerChatListener(DeluxeGroups deluxeGroups) { + this.plugin = deluxeGroups; + reload(); + } + + public void reload() { + this.groupPrefixes = new ArrayList<>(); + this.groupPrefixes.addAll(trimmed(this.plugin.getConfig().getStringList("prefix"))); + } + + @EventHandler(priority=EventPriority.HIGH) + public void onChat(AsyncPlayerChatEvent event) { + + Player sender = event.getPlayer(); + String chat = event.getMessage(); + + if (!sender.hasPermission("deluxegroups.group.chat")) { + return; + } + + DynmapCommonAPI dynmap = DeluxeGroups.getDynmap(); + IMinecraftManager minecraftManager = DeluxeGroups.getMinecraftManager(); + + + if (DeluxeGroups.getInGroup(sender.getUniqueId().toString()) != null) { + boolean groupChat = false; + for (String prefix : this.groupPrefixes) { + // If chat starts with 1 prefix, it is group chat. + if (chat.startsWith(prefix)) { + groupChat = true; + chat = chat.substring(1); + // If chat still starts with the prefix, it means there were two and we are delegating to normal chat + if (chat.startsWith(prefix)) { + event.setMessage(chat); + + // Normal chat + if (!event.isCancelled()) { + if (dynmap != null) { + dynmap.postPlayerMessageToWeb(sender.getName(), sender.getName(), chat); + //dynmap.sendBroadcastToWeb(sender.getName(), chat); + } + if (minecraftManager != null) { + minecraftManager.globalLog(sender, chat); + } + } + return; + } + break; + } + } + + if (groupChat) { + String groupName = DeluxeGroups.getInGroup(sender.getUniqueId().toString()); + DeluxeGroup group = DeluxeGroups.getGroup(groupName); + group.log(sender.getPlayer().getName(), chat); + + String format = plugin.getConfig().getString("format.custom"); + if (StringUtils.isNotBlank(format)) { + chat = format.replace("", group.getName()).replace("", chat); + } + String color = plugin.getConfig().getString("format.color"); + if (StringUtils.isNotBlank(color)) { + chat = ChatColor.getByChar(color.replace("&", "")) + chat; + } + + List uuids = group.getUUIDList(); + + Iterator recipients = event.getRecipients().iterator(); + while (recipients.hasNext()) { + Player player = (Player) recipients.next(); + if (!uuids.contains(player.getUniqueId().toString())) { + recipients.remove(); + } + } + event.setMessage(chat); + return; + } + } + + // Normal chat + if (!event.isCancelled()) { + if (dynmap != null) { + dynmap.postPlayerMessageToWeb(sender.getName(), sender.getName(), chat); + //dynmap.sendBroadcastToWeb(sender.getName(), chat); + } + if (minecraftManager != null) { + minecraftManager.globalLog(sender, chat); + } + } + } + + private List trimmed(List list) { + for (int i = 0; i < list.size(); i++) { + list.set(i, list.get(i).trim()); + } + return list; + } + + + + +} diff --git a/src/main/java/xyz/etztech/deluxegroups/listeners/SessionListener.java b/src/main/java/xyz/etztech/deluxegroups/listeners/SessionListener.java new file mode 100644 index 0000000..7eb9111 --- /dev/null +++ b/src/main/java/xyz/etztech/deluxegroups/listeners/SessionListener.java @@ -0,0 +1,31 @@ +package xyz.etztech.deluxegroups.listeners; + + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.dynmap.DynmapCommonAPI; +import xyz.etztech.deluxegroups.DeluxeGroups; + +public class SessionListener implements Listener { + + @EventHandler + public void onLogin(PlayerLoginEvent event) { + handleEvent(event.getPlayer(), true); + } + + @EventHandler + public void onLogout(PlayerQuitEvent event) { + handleEvent(event.getPlayer(), false); + } + + private void handleEvent(Player player, boolean loggedIn) { + DynmapCommonAPI dynmap = DeluxeGroups.getDynmap(); + if (dynmap != null) { + dynmap.postPlayerJoinQuitToWeb(player.getName(), player.getName(), loggedIn); + //dynmap.sendBroadcastToWeb("", player.getName() + " " + (loggedIn ? "joined" : "left") + " the server"); + } + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..7a6118f --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,13 @@ +# A list of prefixes that can be used to initiate group chat +prefix: + - "$" + - "`" + +# To avoid messing up other chat plugins, DG can really only alter the message safely +# So, we have a few options to make players aware of group chat vs global chat +# If you don't want anything, leave these as-is +format: + color: '' + # Custom format. The variables and are available and will be + # replaced by the group name and chat message respectively + custom: '' \ No newline at end of file diff --git a/src/main/resources/groups.yml b/src/main/resources/groups.yml new file mode 100644 index 0000000..4cd966a --- /dev/null +++ b/src/main/resources/groups.yml @@ -0,0 +1,2 @@ +# This is a file for saving all your groups +# Do not manually edit this file! \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..f03f47a --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,40 @@ +name: ${name} +version: ${version} +description: ${description} +author: ${author} +website: ${url} +main: ${mainClass} +softdepend: [dynmap, MinecraftManager] +commands: + deluxegroups: + description: Base DeluxeGroups command + aliases: [deluxegroup, dg] + group: + description: Group commands + aliases: [] +permissions: + deluxegroups.admin: + description: Allows use of DeluxeGroups admin commands + default: op + deluxegroups.group.create: + description: Allows players to create a group. + default: op + deluxegroups.group.join: + description: Allows players to join (or leave) a group. + default: op + deluxegroups.group.chat: + description: Allows players to chat in a group. + default: op + deluxegroups.group.*: + description: Wildcard permission for DeluxeGroups group commands + default: op + children: + deluxegroups.group.create: true + deluxegroups.group.join: true + deluxegroups.group.chat: true + deluxegroups.*: + description: Wildcard permission for DeluxeGroups + default: op + children: + deluxegroups.admin: true + deluxegroups.group.*: true \ No newline at end of file