commit 79c4c9c4a2c4c94b0e4887720ce30496e04abf7d Author: dragontamerfred Date: Sat Jul 21 21:30:05 2018 +0200 Initial commit diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..982b47b --- /dev/null +++ b/build.gradle @@ -0,0 +1,38 @@ +apply plugin: 'java' + +group = pluginGroup +version = 1.1 + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +repositories { + mavenCentral() + maven { + name = 'spigotmc-repo' + url = 'https://hub.spigotmc.org/nexus/content/groups/public/' + } + maven { + name = 'sonatype' + url = 'https://oss.sonatype.org/content/groups/public/' + } + maven { + name = 'nexus-hc' + url = 'http://nexus.hc.to/content/repositories/pub_releases' + } +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.12' + compile 'org.spigotmc:spigot-api:1.8.8-R0.1-SNAPSHOT' + compile 'net.milkbowl.vault:VaultAPI:1.6' + compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.23.1' +} + +import org.apache.tools.ant.filters.ReplaceTokens + +processResources { + from(sourceSets.main.resources.srcDirs) { + filter ReplaceTokens, tokens: [version: version] + } +} diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..9667a53 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,2 @@ +pluginGroup=com.tidefactions +pluginVersion=1.0-SNAPSHOT diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..4453cce --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save ( ) { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..f955316 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/java/com/tidefactions/chat/Commands/CommandAdminChat.java b/src/main/java/com/tidefactions/chat/Commands/CommandAdminChat.java new file mode 100644 index 0000000..a3996b9 --- /dev/null +++ b/src/main/java/com/tidefactions/chat/Commands/CommandAdminChat.java @@ -0,0 +1,50 @@ +package com.tidefactions.chat.Commands; + +import com.tidefactions.chat.Messages; +import com.tidefactions.chat.Types.ChatMode; +import com.tidefactions.chat.Utils.ChatUtils; +import net.milkbowl.vault.chat.Chat; +import org.apache.commons.lang.StringUtils; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class CommandAdminChat implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { + if (sender.hasPermission(ChatMode.ADMIN.getPermission())) { + if (args.length >= 1) { + if (sender instanceof Player) { + ChatUtils.sendMessageInChannel( + ((Player) sender), + ChatMode.ADMIN, + StringUtils.join(args, " "), + "Command"); + } else { + ChatUtils.sendMessageInChannelAsConsole( + ChatMode.ADMIN, + StringUtils.join(args, " "), + "Command"); + } + } else { + if (sender instanceof Player) { + if (ChatUtils.getChatMode(((Player) sender)) == ChatMode.ADMIN) { + ChatUtils.setChatMode(((Player) sender), ChatMode.PUBLIC); + sender.sendMessage(Messages.PREFIX + Messages.AC_TOGGLE_OFF); + } else { + ChatUtils.setChatMode(((Player) sender), ChatMode.ADMIN); + sender.sendMessage(Messages.PREFIX + Messages.AC_TOGGLE_ON); + } + } else { + sender.sendMessage(Messages.PREFIX + Messages.AC_CONSOLE); + } + } + } else { + sender.sendMessage(Messages.PREFIX + Messages.NO_PERMISSION); + } + return true; + } +} diff --git a/src/main/java/com/tidefactions/chat/Commands/CommandBuilderChat.java b/src/main/java/com/tidefactions/chat/Commands/CommandBuilderChat.java new file mode 100644 index 0000000..05f8d87 --- /dev/null +++ b/src/main/java/com/tidefactions/chat/Commands/CommandBuilderChat.java @@ -0,0 +1,49 @@ +package com.tidefactions.chat.Commands; + +import com.tidefactions.chat.Messages; +import com.tidefactions.chat.Types.ChatMode; +import com.tidefactions.chat.Utils.ChatUtils; +import org.apache.commons.lang.StringUtils; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class CommandBuilderChat implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { + if (sender.hasPermission(ChatMode.BUILDER.getPermission())) { + if (args.length >= 1) { + if (sender instanceof Player) { + ChatUtils.sendMessageInChannel( + ((Player) sender), + ChatMode.BUILDER, + StringUtils.join(args, " "), + "Command"); + } else { + ChatUtils.sendMessageInChannelAsConsole( + ChatMode.BUILDER, + StringUtils.join(args, " "), + "Command"); + } + } else { + if (sender instanceof Player) { + if (ChatUtils.getChatMode(((Player) sender)) == ChatMode.BUILDER) { + ChatUtils.setChatMode(((Player) sender), ChatMode.PUBLIC); + sender.sendMessage(Messages.PREFIX + Messages.BC_TOGGLE_OFF); + } else { + ChatUtils.setChatMode(((Player) sender), ChatMode.BUILDER); + sender.sendMessage(Messages.PREFIX + Messages.BC_TOGGLE_ON); + } + } else { + sender.sendMessage(Messages.PREFIX + Messages.BC_CONSOLE); + } + } + } else { + sender.sendMessage(Messages.PREFIX + Messages.NO_PERMISSION); + } + return true; + } +} diff --git a/src/main/java/com/tidefactions/chat/Commands/CommandPrefix.java b/src/main/java/com/tidefactions/chat/Commands/CommandPrefix.java new file mode 100644 index 0000000..6529401 --- /dev/null +++ b/src/main/java/com/tidefactions/chat/Commands/CommandPrefix.java @@ -0,0 +1,17 @@ +package com.tidefactions.chat.Commands; + +import com.tidefactions.chat.GUI.PrefixGui; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class CommandPrefix implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { + if (sender instanceof Player) + new PrefixGui().open(((Player) sender)); + return true; + } +} diff --git a/src/main/java/com/tidefactions/chat/Commands/CommandStaffChat.java b/src/main/java/com/tidefactions/chat/Commands/CommandStaffChat.java new file mode 100644 index 0000000..165976c --- /dev/null +++ b/src/main/java/com/tidefactions/chat/Commands/CommandStaffChat.java @@ -0,0 +1,49 @@ +package com.tidefactions.chat.Commands; + +import com.tidefactions.chat.Messages; +import com.tidefactions.chat.Types.ChatMode; +import com.tidefactions.chat.Utils.ChatUtils; +import org.apache.commons.lang.StringUtils; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class CommandStaffChat implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String s, String[] args) { + if (sender.hasPermission(ChatMode.STAFF.getPermission())) { + if (args.length >= 1) { + if (sender instanceof Player) { + ChatUtils.sendMessageInChannel( + ((Player) sender), + ChatMode.STAFF, + StringUtils.join(args, " "), + "Command"); + } else { + ChatUtils.sendMessageInChannelAsConsole( + ChatMode.STAFF, + StringUtils.join(args, " "), + "Command"); + } + } else { + if (sender instanceof Player) { + if (ChatUtils.getChatMode(((Player) sender)) == ChatMode.STAFF) { + ChatUtils.setChatMode(((Player) sender), ChatMode.PUBLIC); + sender.sendMessage(Messages.PREFIX + Messages.SC_TOGGLE_OFF); + } else { + ChatUtils.setChatMode(((Player) sender), ChatMode.STAFF); + sender.sendMessage(Messages.PREFIX + Messages.SC_TOGGLE_ON); + } + } else { + sender.sendMessage(Messages.PREFIX + Messages.SC_CONSOLE); + } + } + } else { + sender.sendMessage(Messages.PREFIX + Messages.NO_PERMISSION); + } + return true; + } +} diff --git a/src/main/java/com/tidefactions/chat/Config.java b/src/main/java/com/tidefactions/chat/Config.java new file mode 100644 index 0000000..a9bb2d9 --- /dev/null +++ b/src/main/java/com/tidefactions/chat/Config.java @@ -0,0 +1,46 @@ +package com.tidefactions.chat; + +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; + +import static com.tidefactions.chat.Main.colorCodes; + +public class Config { + // Chat formats + public static String GLOBAL_CHAT_FORMAT; + public static String STAFF_CHAT_FORMAT; + public static String ADMIN_CHAT_FORMAT; + public static String BUILDER_CHAT_FORMAT; + + // Console chat settings + public static String CONSOLE_PREFIX; + public static String CONSOLE_TITLE; + public static String CONSOLE_CHAT_COLOR; + + public static void load(File file) { + try { + if (!file.getParentFile().exists()) + file.getParentFile().mkdirs(); + if (!file.exists()) + Main.ExportResource("/config.yml", file); + YamlConfiguration config = new YamlConfiguration(); + config.load(file); + + // Chat formats + GLOBAL_CHAT_FORMAT = colorCodes(config.getString("format.global")); + STAFF_CHAT_FORMAT = colorCodes(config.getString("format.staff")); + ADMIN_CHAT_FORMAT = colorCodes(config.getString("format.admin")); + BUILDER_CHAT_FORMAT = colorCodes(config.getString("format.builder")); + // Console chat settings + CONSOLE_PREFIX = colorCodes(config.getString("console.prefix")); + CONSOLE_TITLE = colorCodes(config.getString("console.title")); + CONSOLE_CHAT_COLOR = colorCodes(config.getString("console.chat-color")); + } catch (IOException | InvalidConfigurationException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/tidefactions/chat/Databases/PrefixDatabase.java b/src/main/java/com/tidefactions/chat/Databases/PrefixDatabase.java new file mode 100644 index 0000000..1a5dc30 --- /dev/null +++ b/src/main/java/com/tidefactions/chat/Databases/PrefixDatabase.java @@ -0,0 +1,66 @@ +package com.tidefactions.chat.Databases; + +import com.tidefactions.chat.Types.Prefix; +import org.bukkit.entity.Player; + +import java.io.File; +import java.sql.*; + +public class PrefixDatabase { + + private static String url; + + public static void init(String path) { + File dbFile = new File(path + "/playerData.db"); + url = "jdbc:sqlite:" + path + "/playerData.db"; + if (!dbFile.getParentFile().exists()) + dbFile.getParentFile().mkdirs(); + if (!dbFile.exists()) { + createTable(); + } + } + + public static void createTable() { + // SQL statement for creating a new table + String sql = "CREATE TABLE IF NOT EXISTS prefixes (\n" + + " uuid TEXT PRIMARY KEY,\n" + + " prefix TEXT NOT NULL\n" + + ");"; + + try (Connection conn = DriverManager.getConnection(url); + Statement stmt = conn.createStatement()) { + stmt.execute(sql); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public static String getPrefixIDForPlayer(Player player) { + try (Connection conn = DriverManager.getConnection(url); + Statement stmt = conn.createStatement()) { + ResultSet result = stmt.executeQuery("SELECT * FROM prefixes WHERE uuid='" + player.getUniqueId() + "'"); + while (result.next()) { + return result.getString("prefix"); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return "Default"; + } + + public static void setPrefix(Player player, Prefix prefix) { + try (Connection conn = DriverManager.getConnection(url); + Statement stmt = conn.createStatement()) { + stmt.execute("DELETE FROM prefixes WHERE uuid='" + player.getUniqueId() + "'"); + } catch (SQLException e) { + e.printStackTrace(); + } + + try (Connection conn = DriverManager.getConnection(url); + Statement stmt = conn.createStatement()) { + stmt.execute("INSERT INTO prefixes ('uuid', 'prefix') VALUES ('" + player.getUniqueId() + "', '" + prefix.getName() + "')"); + } catch (SQLException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/tidefactions/chat/EventHandlers/ChatHandler.java b/src/main/java/com/tidefactions/chat/EventHandlers/ChatHandler.java new file mode 100644 index 0000000..4aa6511 --- /dev/null +++ b/src/main/java/com/tidefactions/chat/EventHandlers/ChatHandler.java @@ -0,0 +1,24 @@ +package com.tidefactions.chat.EventHandlers; + +import com.tidefactions.chat.Types.ChatMode; +import com.tidefactions.chat.Utils.ChatUtils; +import org.bukkit.Bukkit; +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; + +public class ChatHandler implements Listener { + + @EventHandler(priority = EventPriority.HIGH) + public void onAsyncChatEvent(AsyncPlayerChatEvent event) { + Player player = event.getPlayer(); + ChatUtils.sendMessageInChannel( + player, + ChatUtils.getChatMode(player), + event.getMessage(), + "Chat"); + event.setCancelled(true); + } +} diff --git a/src/main/java/com/tidefactions/chat/EventHandlers/InventoryHandler.java b/src/main/java/com/tidefactions/chat/EventHandlers/InventoryHandler.java new file mode 100644 index 0000000..90dc97e --- /dev/null +++ b/src/main/java/com/tidefactions/chat/EventHandlers/InventoryHandler.java @@ -0,0 +1,43 @@ +package com.tidefactions.chat.EventHandlers; + +import com.tidefactions.chat.GUI.PrefixGui; +import com.tidefactions.chat.Types.Prefix; +import com.tidefactions.chat.Utils.PrefixUtils; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; + +import java.util.List; + +public class InventoryHandler implements Listener{ + + @EventHandler + public void onClickEvent(InventoryClickEvent event) { + if (PrefixGui.views.contains(event.getView())) { + Player player = (Player) event.getWhoClicked(); + if (event.getCurrentItem() != null) { + if (event.getCurrentItem().getType() != Material.AIR) { + List lore = event.getCurrentItem().getItemMeta().getLore(); + Prefix prefix = PrefixUtils.getPrefixByID(lore.get(lore.size() - 1)); + if (prefix != null) { + if (player.hasPermission(prefix.getPermission())) { + PrefixUtils.setPrefixForPlayer(player, prefix); + PrefixGui.views.remove(event.getView()); + player.closeInventory(); + } + } + } + } + } + } + + @EventHandler + public void onCloseEvent(InventoryCloseEvent event) { + if (PrefixGui.views.contains(event.getView())) { + PrefixGui.views.remove(event.getView()); + } + } +} diff --git a/src/main/java/com/tidefactions/chat/Events/MessageSendInChannelEvent.java b/src/main/java/com/tidefactions/chat/Events/MessageSendInChannelEvent.java new file mode 100644 index 0000000..1004b47 --- /dev/null +++ b/src/main/java/com/tidefactions/chat/Events/MessageSendInChannelEvent.java @@ -0,0 +1,44 @@ +package com.tidefactions.chat.Events; + + +import com.tidefactions.chat.Types.ChatMode; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class MessageSendInChannelEvent extends Event { + + private static HandlerList handlerList = new HandlerList(); + + private String player; + private ChatMode chatMode; + private String message; + private String source; + + public MessageSendInChannelEvent(String player, ChatMode chatMode, String message, String source) { + this.player = player; + this.chatMode = chatMode; + this.message = message; + this.source = source; + } + + public String getPlayer() { + return player; + } + + public ChatMode getChatMode() { + return chatMode; + } + + public String getMessage() { + return message; + } + + public String getSource() { + return source; + } + + @Override + public HandlerList getHandlers() { + return handlerList; + } +} diff --git a/src/main/java/com/tidefactions/chat/GUI/PrefixGui.java b/src/main/java/com/tidefactions/chat/GUI/PrefixGui.java new file mode 100644 index 0000000..24fe947 --- /dev/null +++ b/src/main/java/com/tidefactions/chat/GUI/PrefixGui.java @@ -0,0 +1,51 @@ +package com.tidefactions.chat.GUI; + +import com.tidefactions.chat.Types.Prefix; +import com.tidefactions.chat.Utils.PrefixUtils; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; +import java.util.List; + +public class PrefixGui { + + public static List views = new ArrayList<>(); + + public void open(Player player) { + int size = (int) (Math.ceil(PrefixUtils.getPrefixes().size() / 9) * 9); + if (size < 9) + size = 9; + Inventory inv = Bukkit.createInventory(null, size, ChatColor.GREEN + "Prefix " + ChatColor.DARK_GRAY + " selection"); + String currentPrefixID = null; + if (PrefixUtils.getPrefixForPlayer(player) != null) { + currentPrefixID = PrefixUtils.getPrefixForPlayer(player).getName(); + } + for (Prefix prefix : PrefixUtils.getPrefixes()) { + if (player.hasPermission(prefix.getPermission())) { + ItemStack is = new ItemStack( + prefix.getItem(), + 1, + prefix.getItemMeta()); + ItemMeta im = is.getItemMeta(); + List lore = new ArrayList<>(); + if (currentPrefixID.equals(prefix.getName())) + lore.add(ChatColor.GREEN + "Active"); + + lore.addAll(prefix.getDescription()); + lore.add(ChatColor.BLACK + prefix.getName()); + im.setLore(lore); + im.setDisplayName(prefix.getPrefix()); + is.setItemMeta(im); + inv.addItem(is); + } + } + + views.add(player.openInventory(inv)); + } +} diff --git a/src/main/java/com/tidefactions/chat/Main.java b/src/main/java/com/tidefactions/chat/Main.java new file mode 100644 index 0000000..e2a6c1b --- /dev/null +++ b/src/main/java/com/tidefactions/chat/Main.java @@ -0,0 +1,84 @@ +package com.tidefactions.chat; + +import com.tidefactions.chat.Commands.CommandAdminChat; +import com.tidefactions.chat.Commands.CommandBuilderChat; +import com.tidefactions.chat.Commands.CommandPrefix; +import com.tidefactions.chat.Commands.CommandStaffChat; +import com.tidefactions.chat.Databases.PrefixDatabase; +import com.tidefactions.chat.EventHandlers.ChatHandler; +import com.tidefactions.chat.EventHandlers.InventoryHandler; +import com.tidefactions.chat.Utils.PrefixUtils; +import net.milkbowl.vault.chat.Chat; +import net.milkbowl.vault.permission.Permission; +import org.bukkit.ChatColor; +import org.bukkit.plugin.RegisteredServiceProvider; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.logging.Logger; + +public final class Main extends JavaPlugin { + + public static Logger logger; + public static Main plugin; + + public static Permission perms = null; + public static Chat chat = null; + + + //TODO list + //- Save and load current prefix + //- Announcement support + //- Titles + + @Override + public void onEnable() { + logger = this.getLogger(); + plugin = this; + setupPermissions(); + PrefixDatabase.init(getDataFolder().getPath()); + Config.load(new File(getDataFolder(), "config.yml")); + Messages.load(new File(getDataFolder(), "messages.yml")); + PrefixUtils.loadPrefixes(new File(plugin.getDataFolder(), "prefixes.yml")); + getServer().getPluginManager().registerEvents(new ChatHandler(), this); + getServer().getPluginManager().registerEvents(new InventoryHandler(), this); + getCommand("bc").setExecutor(new CommandBuilderChat()); + getCommand("sc").setExecutor(new CommandStaffChat()); + getCommand("ac").setExecutor(new CommandAdminChat()); + getCommand("prefix").setExecutor(new CommandPrefix()); + } + + @Override + public void onDisable() { + // Plugin shutdown logic + } + + public static void ExportResource(String resourceName, File output) { + try (InputStream stream = Main.class.getResourceAsStream(resourceName); OutputStream resStreamOut = new FileOutputStream(output)) { + if (stream == null) { + throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file."); + } + + int readBytes; + byte[] buffer = new byte[4096]; + while ((readBytes = stream.read(buffer)) > 0) { + resStreamOut.write(buffer, 0, readBytes); + } + } catch (Exception ex) { + //Fail silently + } + } + + public static String colorCodes(String string) { + return ChatColor.translateAlternateColorCodes('&', string); + } + + private boolean setupPermissions() { + RegisteredServiceProvider rsp = getServer().getServicesManager().getRegistration(Permission.class); + perms = rsp.getProvider(); + return perms != null; + } +} diff --git a/src/main/java/com/tidefactions/chat/Messages.java b/src/main/java/com/tidefactions/chat/Messages.java new file mode 100644 index 0000000..c56a4ca --- /dev/null +++ b/src/main/java/com/tidefactions/chat/Messages.java @@ -0,0 +1,57 @@ +package com.tidefactions.chat; + +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.io.IOException; + +import static com.tidefactions.chat.Main.colorCodes; + +public class Messages { + + public static String PREFIX; + public static String NO_PERMISSION; + public static String AC_CONSOLE; + public static String AC_TOGGLE_ON; + public static String AC_TOGGLE_OFF; + public static String SC_CONSOLE; + public static String SC_TOGGLE_ON; + public static String SC_TOGGLE_OFF; + public static String BC_CONSOLE; + public static String BC_TOGGLE_ON; + public static String BC_TOGGLE_OFF; + + + public static void load(File file) { + try { + if (!file.getParentFile().exists()) + file.getParentFile().mkdirs(); + if (!file.exists()) + Main.ExportResource("/messages.yml", file); + YamlConfiguration config = new YamlConfiguration(); + config.load(file); + + // Generic + PREFIX = colorCodes(config.getString("prefix")); + NO_PERMISSION = colorCodes(config.getString("no-permission")); + + // Ac messages + AC_CONSOLE = colorCodes(config.getString("ac-console")); + AC_TOGGLE_ON = colorCodes(config.getString("ac-toggle-on")); + AC_TOGGLE_OFF = colorCodes(config.getString("ac-toggle-off")); + + // Sc messages + SC_CONSOLE = colorCodes(config.getString("sc-console")); + SC_TOGGLE_ON = colorCodes(config.getString("sc-toggle-on")); + SC_TOGGLE_OFF = colorCodes(config.getString("sc-toggle-off")); + + // Bc messages + BC_CONSOLE = colorCodes(config.getString("bc-console")); + BC_TOGGLE_ON = colorCodes(config.getString("bc-toggle-on")); + BC_TOGGLE_OFF = colorCodes(config.getString("bc-toggle-off")); + } catch (IOException | InvalidConfigurationException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/tidefactions/chat/Types/ChatMode.java b/src/main/java/com/tidefactions/chat/Types/ChatMode.java new file mode 100644 index 0000000..986028b --- /dev/null +++ b/src/main/java/com/tidefactions/chat/Types/ChatMode.java @@ -0,0 +1,33 @@ +package com.tidefactions.chat.Types; + +import com.tidefactions.chat.Config; +import org.bukkit.ChatColor; + +public enum ChatMode { + PUBLIC("chat.public", "", Config.GLOBAL_CHAT_FORMAT), + STAFF("chat.staff", ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "STAFF" + ChatColor.DARK_GRAY + "] " + ChatColor.RESET, Config.STAFF_CHAT_FORMAT), + ADMIN("chat.admin", ChatColor.DARK_GRAY + "[" + ChatColor.RED + "ADMIN" + ChatColor.DARK_GRAY + "] " + ChatColor.RESET, Config.ADMIN_CHAT_FORMAT), + BUILDER("chat.builder", ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "BUILDER" + ChatColor.DARK_GRAY + "] " + ChatColor.RESET, Config.BUILDER_CHAT_FORMAT); + + private String permission; + private String prefix; + private String format; + + ChatMode(String permission, String prefix, String format) { + this.permission = permission; + this.prefix = prefix; + this.format = format; + } + + public String getPermission() { + return permission; + } + + public String getPrefix() { + return prefix; + } + + public String getFormat() { + return format; + } +} diff --git a/src/main/java/com/tidefactions/chat/Types/Prefix.java b/src/main/java/com/tidefactions/chat/Types/Prefix.java new file mode 100644 index 0000000..f087fdc --- /dev/null +++ b/src/main/java/com/tidefactions/chat/Types/Prefix.java @@ -0,0 +1,54 @@ +package com.tidefactions.chat.Types; + +import org.bukkit.ChatColor; +import org.bukkit.Material; + +import java.util.List; + +public class Prefix { + private String name; + private String permission; + private String prefix; + private Material item; + private Short itemMeta; + private List description; + private ChatColor chatColor; + + public Prefix(String name, String permission, String prefix, Material item, Integer itemMeta, List description, ChatColor chatColor) { + this.name = name; + this.permission = permission; + this.prefix = prefix; + this.item = item; + this.itemMeta = itemMeta.shortValue(); + this.description = description; + this.chatColor = chatColor; + } + + public String getName() { + return name; + } + + public String getPermission() { + return permission; + } + + public String getPrefix() { + return prefix; + } + + public Material getItem() { + return item; + } + + public Short getItemMeta() { + return itemMeta; + } + + public List getDescription() { + return description; + } + + public ChatColor getChatColor() { + return chatColor; + } +} diff --git a/src/main/java/com/tidefactions/chat/Utils/ChatUtils.java b/src/main/java/com/tidefactions/chat/Utils/ChatUtils.java new file mode 100644 index 0000000..305f352 --- /dev/null +++ b/src/main/java/com/tidefactions/chat/Utils/ChatUtils.java @@ -0,0 +1,69 @@ +package com.tidefactions.chat.Utils; + +import com.tidefactions.chat.Config; +import com.tidefactions.chat.Events.MessageSendInChannelEvent; +import com.tidefactions.chat.Types.ChatMode; +import com.tidefactions.chat.Types.Prefix; +import org.bukkit.Bukkit; +import org.bukkit.Sound; +import org.bukkit.entity.Player; + +import java.util.HashMap; +import java.util.UUID; + +public class ChatUtils { + + private static HashMap currentChatMode = new HashMap<>(); + + public static void sendMessageInChannel(Player player, ChatMode mode, String message, String source) { + String finalMessage = getMessageForChannel(player, mode, message); + MessageSendInChannelEvent event = new MessageSendInChannelEvent(player.getName(), mode, message, source); + Bukkit.getPluginManager().callEvent(event); + Bukkit.broadcast(finalMessage, mode.getPermission()); + if (message.contains("@")) { + String[] array = message.split(" "); + for (String word : array) + if (word.startsWith("@")) { + Player mentioned = Bukkit.getPlayer(word.replace("@", "")); + if (mentioned != null) + mentioned.playSound(mentioned.getLocation(), Sound.ORB_PICKUP, 1, 0); + } + } + } + + public static void sendMessageInChannelAsConsole(ChatMode mode, String message, String source) { + String finalMessage = getMessageForChannelAsConsole(mode, message); + MessageSendInChannelEvent event = new MessageSendInChannelEvent("CONSOLE", mode, message, source); + Bukkit.getPluginManager().callEvent(event); + Bukkit.broadcast(finalMessage, mode.getPermission()); + } + + public static String getMessageForChannel(Player player, ChatMode mode, String message) { + Prefix prefix = PrefixUtils.getPrefixForPlayer(player); + return mode.getFormat() + .replaceAll("", mode.getPrefix()) + .replaceAll("", prefix.getPrefix()) + .replaceAll("", player.getName()) + .replaceAll("", TitleUtils.getTitleForPlayer(player)) + .replaceAll("<CHAT_COLOR>", prefix.getChatColor().toString()) + .replaceAll("<MESSAGE>", message); + } + + public static String getMessageForChannelAsConsole(ChatMode mode, String message) { + return mode.getFormat() + .replaceAll("<MODE_PREFIX>", mode.getPrefix()) + .replaceAll("<PREFIX>", Config.CONSOLE_PREFIX) + .replaceAll("<PLAYER_NAME>", "CONSOLE") + .replaceAll("<TITLE>", Config.CONSOLE_TITLE) + .replaceAll("<CHAT_COLOR>", Config.CONSOLE_CHAT_COLOR) + .replaceAll("<MESSAGE>", message); + } + + public static void setChatMode(Player player, ChatMode mode) { + currentChatMode.put(player.getUniqueId(), mode); + } + + public static ChatMode getChatMode(Player player) { + return currentChatMode.getOrDefault(player.getUniqueId(), ChatMode.PUBLIC); + } +} diff --git a/src/main/java/com/tidefactions/chat/Utils/PrefixUtils.java b/src/main/java/com/tidefactions/chat/Utils/PrefixUtils.java new file mode 100644 index 0000000..1c15d40 --- /dev/null +++ b/src/main/java/com/tidefactions/chat/Utils/PrefixUtils.java @@ -0,0 +1,64 @@ +package com.tidefactions.chat.Utils; + +import com.tidefactions.chat.Databases.PrefixDatabase; +import com.tidefactions.chat.Main; +import com.tidefactions.chat.Types.Prefix; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; + +import java.io.File; +import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; + +import static com.tidefactions.chat.Main.logger; + +public class PrefixUtils { + + private static HashMap<String, Prefix> prefixes = new HashMap<>(); + + public static void loadPrefixes(File prefixFile) { + if (!prefixFile.getParentFile().exists()) + prefixFile.getParentFile().mkdirs(); + if (!prefixFile.exists()) + Main.ExportResource("/prefixes.yml", prefixFile); + try { + YamlConfiguration config = new YamlConfiguration(); + config.load(prefixFile); + for (String key : config.getKeys(false)) { + prefixes.put(key, new Prefix( + key, + config.getString(key + ".permission"), + ChatColor.translateAlternateColorCodes('&', config.getString(key + ".prefix")), + Material.getMaterial(config.getString(key + ".item")), + config.getInt(key + ".item-meta"), + config.getStringList(key + ".description"), + ChatColor.valueOf(config.getString(key + ".chat-color")) + )); + logger.info("Loaded prefix: " + key); + } + } catch (IOException | InvalidConfigurationException e) { + e.printStackTrace(); + } + + } + + public static Prefix getPrefixForPlayer(Player player) { + return prefixes.getOrDefault(PrefixDatabase.getPrefixIDForPlayer(player), prefixes.get("Default")); + } + + public static void setPrefixForPlayer(Player player, Prefix prefix) { + PrefixDatabase.setPrefix(player, prefix); + } + + public static Prefix getPrefixByID(String id) { + return prefixes.get(id.replaceAll(ChatColor.BLACK.toString(), "")); + } + + public static Collection<Prefix> getPrefixes() { + return prefixes.values(); + } +} diff --git a/src/main/java/com/tidefactions/chat/Utils/TitleUtils.java b/src/main/java/com/tidefactions/chat/Utils/TitleUtils.java new file mode 100644 index 0000000..7213132 --- /dev/null +++ b/src/main/java/com/tidefactions/chat/Utils/TitleUtils.java @@ -0,0 +1,11 @@ +package com.tidefactions.chat.Utils; + +import org.bukkit.entity.Player; + +public class TitleUtils { + + + public static String getTitleForPlayer(Player player) { + return ""; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..0166925 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,9 @@ +format: + global: "<MODE_PREFIX><PREFIX> &r<PLAYER_NAME><TITLE>&7: <CHAT_COLOR><MESSAGE>" + staff: "<MODE_PREFIX><PREFIX> &r<PLAYER_NAME>&7: <CHAT_COLOR><MESSAGE>" + admin: "<MODE_PREFIX><PREFIX> &r<PLAYER_NAME>&7: <CHAT_COLOR><MESSAGE>" + builder: "<MODE_PREFIX><PREFIX> &r<PLAYER_NAME>&7: <CHAT_COLOR><MESSAGE>" +console: + prefix: "&8[&4CONSOLE&8]" + title: "" + chat-color: "&4" \ No newline at end of file diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml new file mode 100644 index 0000000..e97a04b --- /dev/null +++ b/src/main/resources/messages.yml @@ -0,0 +1,11 @@ +prefix: "&8[&2Tide&8Chat] &f" +no-permission: "You do not have permission for this command." +ac-console: "Console cant use /ac without arguments it has to use /ac <message>." +ac-toggle-on: "You turned on admin chat." +ac-toggle-off: "You turned off admin chat." +sc-console: "Console cant use /sc without arguments it has to use /sc <message>." +sc-toggle-on: "You turned on staff chat." +sc-toggle-off: "You turned off staff chat." +bc-console: "Console cant use /bc without arguments it has to use /bc <message>." +bc-toggle-on: "You turned on builder chat." +bc-toggle-off: "You turned off builder chat." diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..ec4ff72 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,10 @@ +name: Chat +version: @version@ +main: com.tidefactions.chat.Main +authors: [dragontamerfred] +depend: [Vault] +commands: + bc: + sc: + ac: + prefix: \ No newline at end of file diff --git a/src/main/resources/prefixes.yml b/src/main/resources/prefixes.yml new file mode 100644 index 0000000..5cde82c --- /dev/null +++ b/src/main/resources/prefixes.yml @@ -0,0 +1,81 @@ +Default: + permission: 'prefix.default' + prefix: '&8[&fMember&8]' + item: 'WOOL' + item-meta: 0 + chat-color: "GRAY" + description: + - 'The default tag.' + - 'This is nothing special.' +Builder: + permission: 'prefix.builder' + prefix: '&8[&9Builder&8]' + item: 'WOOL' + item-meta: 11 + chat-color: "BLUE" + description: + - 'This special tag will' + - 'show everyone that you' + - 'helped with building.' +Helper: + permission: 'prefix.helper' + prefix: '&8[&eHelper&8]' + item: 'WOOL' + item-meta: 4 + chat-color: "YELLOW" + description: + - 'This special tag will' + - 'show everyone that you' + - 'are willing to help out.' +Moderator: + permission: 'prefix.moderator' + prefix: '&8[&6Moderator&8]' + item: 'STAINED_CLAY' + item-meta: 4 + chat-color: "GOLD" + description: + - 'This special tag will' + - 'show everyone that you' + - 'are a Moderator.' +Sr-Moderator: + permission: 'prefix.sr-moderator' + prefix: '&8[&6Sr.Moderator&8]' + item: 'GOLD_BLOCK' + item-meta: 0 + chat-color: "GOLD" + description: + - 'This special tag will' + - 'show everyone that you' + - 'have been a moderator' + - 'for a long time.' +Admin: + permission: 'prefix.admin' + prefix: '&8[&cAdmin&8]' + item: 'WOOL' + item-meta: 14 + chat-color: "RED" + description: + - 'This special tag will' + - 'show everyone that you' + - 'are an Admin.' +Developer: + permission: 'prefix.developer' + prefix: '&8[&bDeveloper&8]' + item: 'WOOL' + item-meta: 3 + chat-color: "AQUA" + description: + - 'This special tag will' + - 'show everyone that you' + - 'are an Developer for' + - 'the server.' +Owner: + permission: 'prefix.owner' + prefix: '&8[&4Owner&8]' + item: 'WOOL' + item-meta: 14 + chat-color: "DARK_RED" + description: + - 'This special tag will' + - 'show everyone that you' + - 'are, well the Owner.' \ No newline at end of file