From c9dfa4e8bc9ebd98871d3436bb93bf351c3705a7 Mon Sep 17 00:00:00 2001 From: Steffion Date: Sat, 27 Jul 2013 19:37:01 +0200 Subject: [PATCH] First commit. --- src/nl/Steffion/BlockHunt/BlockHunt.java | 105 ++++++++++ .../Steffion/BlockHunt/Commands/CMDhelp.java | 125 ++++++++++++ .../Steffion/BlockHunt/Commands/CMDinfo.java | 32 +++ .../BlockHunt/Commands/CMDnotfound.java | 16 ++ .../BlockHunt/Commands/CMDreload.java | 25 +++ .../BlockHunt/Commands/DefaultCMD.java | 16 ++ .../Steffion/BlockHunt/Managers/CommandC.java | 75 +++++++ .../Steffion/BlockHunt/Managers/ConfigC.java | 53 +++++ .../Steffion/BlockHunt/Managers/ConfigM.java | 103 ++++++++++ src/nl/Steffion/BlockHunt/Managers/FileM.java | 69 +++++++ .../Steffion/BlockHunt/Managers/MessageM.java | 186 ++++++++++++++++++ .../Steffion/BlockHunt/Managers/PlayerM.java | 132 +++++++++++++ .../Serializables/LocationSerializable.java | 67 +++++++ .../Steffion/BlockHunt/Serializables/M.java | 9 + src/nl/Steffion/BlockHunt/W.java | 32 +++ src/plugin.yml | 14 ++ 16 files changed, 1059 insertions(+) create mode 100644 src/nl/Steffion/BlockHunt/BlockHunt.java create mode 100644 src/nl/Steffion/BlockHunt/Commands/CMDhelp.java create mode 100644 src/nl/Steffion/BlockHunt/Commands/CMDinfo.java create mode 100644 src/nl/Steffion/BlockHunt/Commands/CMDnotfound.java create mode 100644 src/nl/Steffion/BlockHunt/Commands/CMDreload.java create mode 100644 src/nl/Steffion/BlockHunt/Commands/DefaultCMD.java create mode 100644 src/nl/Steffion/BlockHunt/Managers/CommandC.java create mode 100644 src/nl/Steffion/BlockHunt/Managers/ConfigC.java create mode 100644 src/nl/Steffion/BlockHunt/Managers/ConfigM.java create mode 100644 src/nl/Steffion/BlockHunt/Managers/FileM.java create mode 100644 src/nl/Steffion/BlockHunt/Managers/MessageM.java create mode 100644 src/nl/Steffion/BlockHunt/Managers/PlayerM.java create mode 100644 src/nl/Steffion/BlockHunt/Serializables/LocationSerializable.java create mode 100644 src/nl/Steffion/BlockHunt/Serializables/M.java create mode 100644 src/nl/Steffion/BlockHunt/W.java create mode 100644 src/plugin.yml diff --git a/src/nl/Steffion/BlockHunt/BlockHunt.java b/src/nl/Steffion/BlockHunt/BlockHunt.java new file mode 100644 index 0000000..1cac2ff --- /dev/null +++ b/src/nl/Steffion/BlockHunt/BlockHunt.java @@ -0,0 +1,105 @@ +package nl.Steffion.BlockHunt; + +import nl.Steffion.BlockHunt.Managers.CommandC; +import nl.Steffion.BlockHunt.Managers.ConfigC; +import nl.Steffion.BlockHunt.Managers.MessageM; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; + +public class BlockHunt extends JavaPlugin implements Listener { + /* + * Made by @author Steffion, © 2013. + */ + + public void onEnable() { + W.newFiles(); + getServer().getPluginManager().registerEvents(this, this); + MessageM.sendFMessage(null, ConfigC.log_Enabled, true, "name-" + + W.pluginName, "version-" + W.pluginVersion, "autors-" + + W.pluginAutors); + } + + public void onDisable() { + MessageM.sendFMessage(null, ConfigC.log_Disabled, true, "name-" + + W.pluginName, "version-" + W.pluginVersion, "autors-" + + W.pluginAutors); + } + + /** + * Build a string. + * + * @param input + * @param startArg + * @return + */ + public static String argsBuild(String[] input, int startArg) { + if (input.length - startArg <= 0) { + return null; + } + StringBuilder sb = new StringBuilder(input[startArg]); + for (int i = ++startArg; i < input.length; i++) { + sb.append(' ').append(input[i]); + } + return sb.toString(); + } + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, + String[] args) { + Player player = null; + if (sender instanceof Player) { + player = (Player) sender; + } + + for (CommandC command : CommandC.values()) { + String[] commandArgsSplit = command.command.split("%"); + String[] argsSplit = commandArgsSplit[1].split("_"); + String[] argsSplitAlias = command.alias.split("%")[1].split("_"); + + if (cmd.getName().equalsIgnoreCase(commandArgsSplit[0])) { + int i = 0; + boolean equals = true; + + if (command.minLenght == 0) { + if (args.length == 0) { + equals = true; + } else { + equals = false; + } + } else { + if (args.length >= command.minLenght) { + for (String arg : argsSplit) { + for (String arga : argsSplitAlias) { + if (!arg.equalsIgnoreCase(args[i]) + && !arga.equalsIgnoreCase(args[i])) { + equals = false; + } + i = i + 1; + } + } + } else { + equals = false; + } + } + + if (equals) { + if (W.config.getFile().getBoolean( + command.enabled.getLocation())) { + command.cmd.exectue(player, cmd, label, args); + } else { + MessageM.sendFMessage(player, + ConfigC.error_commandNotEnabled, true); + } + + return true; + } + } + } + CommandC.NOT_FOUND.cmd.exectue(player, cmd, label, args); + return true; + } +} diff --git a/src/nl/Steffion/BlockHunt/Commands/CMDhelp.java b/src/nl/Steffion/BlockHunt/Commands/CMDhelp.java new file mode 100644 index 0000000..2d7078c --- /dev/null +++ b/src/nl/Steffion/BlockHunt/Commands/CMDhelp.java @@ -0,0 +1,125 @@ +package nl.Steffion.BlockHunt.Commands; + +import nl.Steffion.BlockHunt.W; +import nl.Steffion.BlockHunt.Managers.CommandC; +import nl.Steffion.BlockHunt.Managers.ConfigC; +import nl.Steffion.BlockHunt.Managers.MessageM; +import nl.Steffion.BlockHunt.Managers.PlayerM; +import nl.Steffion.BlockHunt.Managers.PlayerM.PermsC; + +import org.bukkit.command.Command; +import org.bukkit.entity.Player; + +public class CMDhelp extends DefaultCMD { + + @Override + public boolean exectue(Player player, Command cmd, String label, + String[] args) { + if (PlayerM.hasPerm(player, PermsC.help, true)) { + int amountCommands = 0; + for (CommandC command : CommandC.values()) { + if (command.usage != "-") { + amountCommands = amountCommands + 1; + } + } + + int maxPages = Math.round(amountCommands / 3); + if (maxPages <= 0) { + maxPages = 1; + } + + if (args.length == 1) { + int page = 1; + MessageM.sendFMessage(player, ConfigC.chat_headerhigh, false, + "header-" + W.pluginName + " %Nhelp page %A" + page + + "%N/%A" + maxPages); + int i = 1; + for (CommandC command : CommandC.values()) { + if (i <= 4) { + if (command.usage != "-") { + if (PlayerM.hasPerm(player, command.perm, false)) { + MessageM.sendMessage( + player, + "%A/" + + command.usage + + "%N - " + + W.messages.getFile().get( + command.help + .getLocation()), + false); + } else { + MessageM.sendMessage( + player, + "%W/" + + command.usage + + "%N - " + + W.messages.getFile().get( + command.help + .getLocation()), + false); + } + i = i + 1; + } + } + } + + MessageM.sendFMessage(player, ConfigC.chat_headerhigh, false, + "header-&oHelp Page"); + } else { + int page = 1; + try { + page = Integer.valueOf(args[1]); + } catch (NumberFormatException e) { + page = 1; + } + + if (maxPages < page) { + maxPages = page; + } + + MessageM.sendFMessage(player, ConfigC.chat_headerhigh, false, + "header-" + W.pluginName + " %Nhelp page %A" + page + + "%N/%A" + maxPages); + int i = 1; + for (CommandC command : CommandC.values()) { + if (i <= (page * 4) + 4) { + if (command.usage != "-") { + if (i >= ((page - 1) * 4) + 1 + && i <= ((page - 1) * 4) + 4) { + if (PlayerM + .hasPerm(player, command.perm, false)) { + MessageM.sendMessage( + player, + "%A/" + + command.usage + + "%N - " + + W.messages + .getFile() + .get(command.help + .getLocation()), + false); + } else { + MessageM.sendMessage( + player, + "%W/" + + command.usage + + "%N - " + + W.messages + .getFile() + .get(command.help + .getLocation()), + false); + } + } + i = i + 1; + } + } + } + + MessageM.sendFMessage(player, ConfigC.chat_headerhigh, false, + "header-&oHelp Page"); + } + } + return true; + } +} diff --git a/src/nl/Steffion/BlockHunt/Commands/CMDinfo.java b/src/nl/Steffion/BlockHunt/Commands/CMDinfo.java new file mode 100644 index 0000000..860a398 --- /dev/null +++ b/src/nl/Steffion/BlockHunt/Commands/CMDinfo.java @@ -0,0 +1,32 @@ +package nl.Steffion.BlockHunt.Commands; + +import nl.Steffion.BlockHunt.W; +import nl.Steffion.BlockHunt.Managers.CommandC; +import nl.Steffion.BlockHunt.Managers.ConfigC; +import nl.Steffion.BlockHunt.Managers.MessageM; +import nl.Steffion.BlockHunt.Managers.PlayerM; +import nl.Steffion.BlockHunt.Managers.PlayerM.PermsC; + +import org.bukkit.command.Command; +import org.bukkit.entity.Player; + +public class CMDinfo extends DefaultCMD { + + @Override + public boolean exectue(Player player, Command cmd, String label, + String[] args) { + if (PlayerM.hasPerm(player, PermsC.info, true)) { + MessageM.sendFMessage(player, ConfigC.chat_headerhigh, false, + "header-" + W.pluginName); + MessageM.sendMessage(player, "%A%name%%N made by %A%autors%%N.", + false, "name-" + W.pluginName, "autors-" + W.pluginAutors); + MessageM.sendMessage(player, "%NVersion: %A%version%%N.", false, + "version-" + W.pluginVersion); + MessageM.sendMessage(player, "%NType %A/%helpusage% %Nfor help.", + false, "helpusage-" + CommandC.HELP.usage); + MessageM.sendFMessage(player, ConfigC.chat_headerhigh, false, + "header-&oInfo Page"); + } + return true; + } +} diff --git a/src/nl/Steffion/BlockHunt/Commands/CMDnotfound.java b/src/nl/Steffion/BlockHunt/Commands/CMDnotfound.java new file mode 100644 index 0000000..57957ee --- /dev/null +++ b/src/nl/Steffion/BlockHunt/Commands/CMDnotfound.java @@ -0,0 +1,16 @@ +package nl.Steffion.BlockHunt.Commands; + +import nl.Steffion.BlockHunt.Managers.ConfigC; +import nl.Steffion.BlockHunt.Managers.MessageM; + +import org.bukkit.command.Command; +import org.bukkit.entity.Player; + +public class CMDnotfound extends DefaultCMD { + @Override + public boolean exectue(Player player, Command cmd, String label, + String[] args) { + MessageM.sendFMessage(player, ConfigC.error_commandNotFound, true); + return true; + } +} diff --git a/src/nl/Steffion/BlockHunt/Commands/CMDreload.java b/src/nl/Steffion/BlockHunt/Commands/CMDreload.java new file mode 100644 index 0000000..b96d6b0 --- /dev/null +++ b/src/nl/Steffion/BlockHunt/Commands/CMDreload.java @@ -0,0 +1,25 @@ +package nl.Steffion.BlockHunt.Commands; + +import nl.Steffion.BlockHunt.W; +import nl.Steffion.BlockHunt.Managers.ConfigC; +import nl.Steffion.BlockHunt.Managers.MessageM; +import nl.Steffion.BlockHunt.Managers.PlayerM; +import nl.Steffion.BlockHunt.Managers.PlayerM.PermsC; + +import org.bukkit.command.Command; +import org.bukkit.entity.Player; + +public class CMDreload extends DefaultCMD { + + @Override + public boolean exectue(Player player, Command cmd, String label, + String[] args) { + if (PlayerM.hasPerm(player, PermsC.reload, true)) { + W.config.load(); + W.messages.load(); + W.newFiles(); + MessageM.sendFMessage(player, ConfigC.normal_reloadedConfigs, true); + } + return true; + } +} diff --git a/src/nl/Steffion/BlockHunt/Commands/DefaultCMD.java b/src/nl/Steffion/BlockHunt/Commands/DefaultCMD.java new file mode 100644 index 0000000..716480e --- /dev/null +++ b/src/nl/Steffion/BlockHunt/Commands/DefaultCMD.java @@ -0,0 +1,16 @@ +package nl.Steffion.BlockHunt.Commands; + +import nl.Steffion.BlockHunt.Managers.MessageM; + +import org.bukkit.command.Command; +import org.bukkit.entity.Player; + +public class DefaultCMD { + + public boolean exectue(Player player, Command cmd, String label, + String[] args) { + MessageM.sendMessage(player, "%NExample of a Command!", true); + //TODO Place the command stuff here. + return true; + } +} diff --git a/src/nl/Steffion/BlockHunt/Managers/CommandC.java b/src/nl/Steffion/BlockHunt/Managers/CommandC.java new file mode 100644 index 0000000..f6a94dd --- /dev/null +++ b/src/nl/Steffion/BlockHunt/Managers/CommandC.java @@ -0,0 +1,75 @@ +package nl.Steffion.BlockHunt.Managers; + +import nl.Steffion.BlockHunt.W; +import nl.Steffion.BlockHunt.Commands.*; +import nl.Steffion.BlockHunt.Managers.PlayerM.PermsC; + +public enum CommandC { + /* + * Made by @author Steffion, © 2013. + */ + + INFO ("SteffionDefault%_", + "SteffionDefault%_", + new CMDinfo(), + ConfigC.commandEnabled_info, + PermsC.info, + ConfigC.help_info, + 0, + "-"), + INFO2 ("SteffionDefault%info_", + "SteffionDefault%i_", + new CMDinfo(), + ConfigC.commandEnabled_info, + PermsC.info, + ConfigC.help_info, + 1, + W.pluginName + " [info|i]"), + HELP ("SteffionDefault%help_", + "SteffionDefault%h_", + new CMDhelp(), + + ConfigC.commandEnabled_help, + PermsC.help, + ConfigC.help_help, + 1, + W.pluginName + " [pagenumber]"), + RELOAD ("SteffionDefault%reload_", + "SteffionDefault%r_", + new CMDreload(), + ConfigC.commandEnabled_reload, + PermsC.reload, + ConfigC.help_reload, + 1, + W.pluginName + " "), + NOT_FOUND ("%_", + "%_", + new CMDnotfound(), + null, + PermsC.info, + ConfigC.help_info, + 0, + "-"); + + public String command; + public String alias; + public DefaultCMD cmd; + public ConfigC enabled; + public PermsC perm; + public ConfigC help; + public int minLenght; + public String usage; + + private CommandC (String command, String alias, DefaultCMD cmd, + ConfigC enabled, PermsC perm, ConfigC help, int minLenght, + String usage) { + this.command = command; + this.alias = alias; + this.cmd = cmd; + this.enabled = enabled; + this.perm = perm; + this.help = help; + this.minLenght = minLenght; + this.usage = usage; + } +} diff --git a/src/nl/Steffion/BlockHunt/Managers/ConfigC.java b/src/nl/Steffion/BlockHunt/Managers/ConfigC.java new file mode 100644 index 0000000..6c4fc94 --- /dev/null +++ b/src/nl/Steffion/BlockHunt/Managers/ConfigC.java @@ -0,0 +1,53 @@ +package nl.Steffion.BlockHunt.Managers; + +import nl.Steffion.BlockHunt.W; + +public enum ConfigC { + /* + * Made by @author Steffion, © 2013. + */ + + chat_tag ("[" + W.pluginName + "] ", W.config), + chat_normal ("&b", W.config), + chat_warning ("&c", W.config), + chat_error ("&c", W.config), + chat_arg ("&e", W.config), + chat_header ("&9", W.config), + chat_headerhigh ("%H_______.[ %A%header%%H ]._______", W.config), + + commandEnabled_info (true, W.config), + commandEnabled_help (true, W.config), + commandEnabled_reload (true, W.config), + + log_Enabled ("%N%name%&a&k + %N%version% is now Enabled. Made by %A%autors%%N.", + W.messages), + log_Disabled ("%N%name%&c&k - %N%version% is now Disabled. Made by %A%autors%%N.", + W.messages), + + help_info ("%NDisplays the plugin's info.", W.messages), + help_help ("%NShows a list of commands.", W.messages), + help_reload ("%NReloads all configs.", W.messages), + + normal_reloadedConfigs ("&aReloaded all configs!", W.messages), + + error_noPermission ("%EYou don't have the permissions to do that!", + W.messages), + error_commandNotEnabled ("%EThis command has been disabled!", W.messages), + error_commandNotFound ("%ECouldn't find the command. Try %A/" + + W.pluginName + " help %Efor more info.", W.messages), + error_notEnoughArguments ("%EYou're missing arguments, correct syntax: %A/%syntax%", + W.messages); + + Object value; + ConfigM config; + + private ConfigC (Object value, ConfigM config) { + this.value = value; + this.config = config; + } + + public String getLocation() { + return this.name().replaceAll("_", "."); + + } +} diff --git a/src/nl/Steffion/BlockHunt/Managers/ConfigM.java b/src/nl/Steffion/BlockHunt/Managers/ConfigM.java new file mode 100644 index 0000000..f29da39 --- /dev/null +++ b/src/nl/Steffion/BlockHunt/Managers/ConfigM.java @@ -0,0 +1,103 @@ +package nl.Steffion.BlockHunt.Managers; + +import java.io.File; + +import nl.Steffion.BlockHunt.W; + +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +public class ConfigM { + /* + * Made by @author Steffion, © 2013. + */ + + String fileName; + File file; + FileConfiguration fileC; + ConfigurationSection fileCS; + String location; + + public ConfigM (String fileName, String location) { + this.fileName = fileName; + this.file = new File("plugins/" + W.pluginName + "/" + location, fileName + + ".yml"); + this.location = W.pluginName + "/" + location; + + this.fileC = new YamlConfiguration(); + this.checkFile(); + this.fileCS = fileC.getConfigurationSection(""); + this.load(); + } + + /** + * Check if file exists, if not create one. + */ + public void checkFile() { + if (!this.file.exists()) { + try { + this.file.getParentFile().mkdirs(); + this.file.createNewFile(); + W.newFiles.add(this.location + this.fileName); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * Save the file. + */ + public void save() { + try { + this.fileC.save(this.file); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Load the file. + */ + public void load() { + this.checkFile(); + if (this.file.exists()) { + try { + this.fileC.load(this.file); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * Add variables to the files if they don't exist. + */ + public static void setDefaults() { + for (ConfigC value : ConfigC.values()) { + value.config.load(); + String location = value.getLocation(); + if (value.config.getFile().get(location) == null) { + value.config.getFile().set(location, value.value); + value.config.save(); + } + } + } + + /** + * Get the File. + */ + public FileConfiguration getFile() { + return this.fileC; + } + + /** + * Get object from a Config. + * @param location + * @return + */ + public Object get(ConfigC location) { + return this.getFile().get(location.getLocation()); + } +} \ No newline at end of file diff --git a/src/nl/Steffion/BlockHunt/Managers/FileM.java b/src/nl/Steffion/BlockHunt/Managers/FileM.java new file mode 100644 index 0000000..e04a9ff --- /dev/null +++ b/src/nl/Steffion/BlockHunt/Managers/FileM.java @@ -0,0 +1,69 @@ +package nl.Steffion.BlockHunt.Managers; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class FileM { + /* + * Made by @author Steffion, © 2013. + */ + + public static void copyFolder(File src, File dest) throws IOException { + if (src.isDirectory()) { + if (!dest.exists()) { + dest.mkdir(); + // Bukkit.broadcastMessage("Directory copied from " + src + // + " to " + dest); + } + String files[] = src.list(); + + for (String file : files) { + File srcFile = new File(src, file); + File destFile = new File(dest, file); + copyFolder(srcFile, destFile); + } + + } else { + InputStream in = new FileInputStream(src); + OutputStream out = new FileOutputStream(dest); + + byte[] buffer = new byte[1024]; + + int length; + while ((length = in.read(buffer)) > 0) { + out.write(buffer, 0, length); + } + + in.close(); + out.close(); + // Bukkit.broadcastMessage("File copied from " + src + " to " + + // dest); + } + } + + public static void delete(File file) throws IOException { + if (file.isDirectory()) { + + if (file.list().length == 0) { + file.delete(); + } else { + String files[] = file.list(); + + for (String temp : files) { + File fileDelete = new File(file, temp); + delete(fileDelete); + } + + if (file.list().length == 0) { + file.delete(); + } + } + } else { + file.delete(); + } + } +} diff --git a/src/nl/Steffion/BlockHunt/Managers/MessageM.java b/src/nl/Steffion/BlockHunt/Managers/MessageM.java new file mode 100644 index 0000000..c5ff526 --- /dev/null +++ b/src/nl/Steffion/BlockHunt/Managers/MessageM.java @@ -0,0 +1,186 @@ +package nl.Steffion.BlockHunt.Managers; + +import nl.Steffion.BlockHunt.W; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +public class MessageM { + /* + * Made by @author Steffion, © 2013. + */ + + /** + * Send a message to a player. + * + * @param player + * @param message + * @param tag + * @param vars + */ + public static void sendMessage(Player player, String message, Boolean tag, + String... vars) { + if (player == null) { + Bukkit.getConsoleSender().sendMessage( + MessageM.replaceAll(CType.TAG(tag) + message, vars)); + } else { + player.sendMessage(MessageM.replaceAll(CType.TAG(tag) + message, + vars)); + } + } + + /** + * Send a message to a player from a Config. + * + * @param player + * @param location + * @param tag + * @param vars + */ + public static void sendFMessage(Player player, ConfigC location, + Boolean tag, String... vars) { + if (player == null) { + Bukkit.getConsoleSender().sendMessage( + MessageM.replaceAll( + CType.TAG(tag) + + location.config.getFile().get( + location.getLocation()), vars)); + } else { + player.sendMessage(MessageM.replaceAll(CType.TAG(tag) + + location.config.getFile().get(location.getLocation()), + vars)); + } + } + + /** + * Sends a message to all online players. Replaces %player% tag to the + * Player's name or "Console". + * + * @param message + * @param tag + * @param vars + */ + public static void broadcastMessage(String message, Boolean tag, + String... vars) { + for (Player player : Bukkit.getOnlinePlayers()) { + String pMessage = message.replaceAll("%player%", player.getName()); + player.sendMessage(MessageM.replaceAll(CType.TAG(tag) + pMessage, + vars)); + } + message = message.replaceAll("%player%", "Console"); + Bukkit.getConsoleSender().sendMessage( + MessageM.replaceAll(CType.TAG(tag) + message, vars)); + } + + /** + * Sends a message to all online players from a Config. Replaces %player% + * tag to the Player's name or "Console". + * + * @param location + * @param tag + * @param vars + */ + public static void broadcastFMessage(ConfigC location, Boolean tag, + String... vars) { + for (Player player : Bukkit.getOnlinePlayers()) { + String pMessage = location.config.getFile() + .get(location.getLocation()).toString() + .replaceAll("%player%", player.getName()); + player.sendMessage(MessageM.replaceAll(CType.TAG(tag) + pMessage, + vars)); + } + String message = location.config.getFile().get(location.getLocation()) + .toString().replaceAll("%player%", "Console"); + Bukkit.getConsoleSender().sendMessage( + MessageM.replaceAll(CType.TAG(tag) + message, vars)); + } + + /** + * Replace all variable codes. + * + * @param message + * @param vars + * @return + */ + public static String replaceAll(String message, String... vars) { + return MessageM.replaceColours(MessageM.replaceColourVars(MessageM + .replaceVars(message, vars))); + } + + /** + * Replace all colour codes. + * + * @param message + * @return + */ + public static String replaceColours(String message) { + return message.replaceAll("(&([a-fk-or0-9]))", "\u00A7$2"); + } + + /** + * Replace Colour codes from config. + * + * @param message + * @return + */ + public static String replaceColourVars(String message) { + message = message.replaceAll("%N", CType.NORMAL()); + message = message.replaceAll("%W", CType.WARNING()); + message = message.replaceAll("%E", CType.ERROR()); + message = message.replaceAll("%A", CType.ARG()); + message = message.replaceAll("%H", CType.HEADER()); + return message; + } + + /** + * Replace all variables. + * + * @param message + * @param vars + * @return Replaced String. + */ + public static String replaceVars(String message, String... vars) { + for (String var : vars) { + String[] split = var.split("-"); + message = message.replaceAll("%" + split[0] + "%", split[1]); + } + return message; + } + + public static class CType { + + public static String NORMAL() { + return (String) W.config.get(ConfigC.chat_normal); + } + + public static String WARNING() { + return (String) W.config.get(ConfigC.chat_warning); + } + + public static String ERROR() { + return (String) W.config.get(ConfigC.chat_error); + } + + public static String ARG() { + return (String) W.config.get(ConfigC.chat_arg); + } + + public static String HEADER() { + return (String) W.config.get(ConfigC.chat_header); + } + + public static String TAG() { + return (String) W.config.get(ConfigC.chat_header) + + (String) W.config.get(ConfigC.chat_tag); + } + + public static String TAG(Boolean enabled) { + if (enabled) { + return (String) W.config.get(ConfigC.chat_header) + + (String) W.config.get(ConfigC.chat_tag); + } else { + return ""; + } + } + } +} diff --git a/src/nl/Steffion/BlockHunt/Managers/PlayerM.java b/src/nl/Steffion/BlockHunt/Managers/PlayerM.java new file mode 100644 index 0000000..5082080 --- /dev/null +++ b/src/nl/Steffion/BlockHunt/Managers/PlayerM.java @@ -0,0 +1,132 @@ +package nl.Steffion.BlockHunt.Managers; + +import nl.Steffion.BlockHunt.W; + +import org.bukkit.entity.Player; + +public class PlayerM { + /* + * Made by @author Steffion, © 2013. + */ + + public static String main = W.pluginMainPermission; + + public enum PType { + ALL, PLAYER, MODERATOR, ADMIN, OP; + } + + public enum PermsC { + info (main + "info", PType.ALL), + help (main + "help", PType.ALL), + reload (main + "reload", PType.MODERATOR); + + public String perm; + public PType type; + + private PermsC (String perm, PType type) { + this.perm = perm; + this.type = type; + } + } + + /** + * Check if the player has a permission. + * + * @param player + * @param perm + * @param message + * @return + */ + public static boolean hasPerm(Player player, PermsC perm, Boolean message) { + PType type = perm.type; + if (player == null) { + return true; + } + + if (type == PType.ALL) { + return true; + } else if (type == PType.OP) { + if (player.isOp()) { + return true; + } + } else if (type == PType.ADMIN) { + if (player.hasPermission(main + "admin")) { + return true; + } + } else if (type == PType.MODERATOR) { + if (player.hasPermission(main + "moderator")) { + return true; + } + } else if (type == PType.PLAYER) { + if (player.hasPermission(main + "player")) { + return true; + } + } + + if (player.hasPermission("*")) { + return true; + } else if (player.hasPermission(main + "*")) { + return true; + } else if (player.hasPermission(main + perm)) { + return true; + } else if (player.hasPermission(main + perm + "*")) { + return true; + } else { + if (message) { + MessageM.sendFMessage(player, ConfigC.error_noPermission, true); + } + } + return false; + } + + /** + * Check if the player has a permission. + * + * @param player + * @param perm + * @param type + * @param message + * @return + */ + public static boolean hasPermRaw(Player player, String perm, PType type, + Boolean message) { + if (player == null) { + return true; + } + + if (type == PType.ALL) { + return true; + } else if (type == PType.OP) { + if (player.isOp()) { + return true; + } + } else if (type == PType.ADMIN) { + if (player.hasPermission(main + "admin")) { + return true; + } + } else if (type == PType.MODERATOR) { + if (player.hasPermission(main + "moderator")) { + return true; + } + } else if (type == PType.PLAYER) { + if (player.hasPermission(main + "player")) { + return true; + } + } + + if (player.hasPermission("*")) { + return true; + } else if (player.hasPermission(main + "*")) { + return true; + } else if (player.hasPermission(main + perm)) { + return true; + } else if (player.hasPermission(main + perm + "*")) { + return true; + } else { + if (message) { + MessageM.sendFMessage(player, ConfigC.error_noPermission, true); + } + } + return false; + } +} diff --git a/src/nl/Steffion/BlockHunt/Serializables/LocationSerializable.java b/src/nl/Steffion/BlockHunt/Serializables/LocationSerializable.java new file mode 100644 index 0000000..2465d66 --- /dev/null +++ b/src/nl/Steffion/BlockHunt/Serializables/LocationSerializable.java @@ -0,0 +1,67 @@ +package nl.Steffion.BlockHunt.Serializables; + +import java.util.HashMap; +import java.util.Map; + +import nl.Steffion.BlockHunt.Managers.MessageM; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.configuration.serialization.ConfigurationSerializable; +import org.bukkit.configuration.serialization.SerializableAs; + +@SerializableAs("Location") +public class LocationSerializable extends Location implements + ConfigurationSerializable { + public LocationSerializable (World world, double x, double y, double z, + float yaw, float pitch) { + super(world, x, y, z, yaw, pitch); + } + + public LocationSerializable (Location loc) { + super(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), + loc.getPitch()); + } + + @Override + public boolean equals(Object o) { + if (o instanceof LocationSerializable || o instanceof Location) { + Location loc = (Location) o; + return loc.getWorld().getName().equals(getWorld().getName()) + && loc.getX() == getX() && loc.getY() == getY() + && loc.getZ() == getZ() && loc.getYaw() == getYaw() + && loc.getPitch() == getPitch(); + } + return false; + } + + @Override + public Map serialize() { + Map map = new HashMap(); + map.put("w", getWorld().getName()); + map.put("x", getX()); + map.put("y", getY()); + map.put("z", getZ()); + if (getYaw() != 0D) + map.put("a", getYaw()); + if (getPitch() != 0D) + map.put("p", getPitch()); + return map; + } + + public static LocationSerializable deserialize(Map map) { + World w = Bukkit.getWorld((String) M.g(map, "w", "")); + if (w == null) { + MessageM.sendMessage( + null, + "%EError deserializing LocationSerializable - world not found!", + true); + return null; + } + return new LocationSerializable(w, (Double) M.g(map, "x", 0D), + (Double) M.g(map, "y", 0D), (Double) M.g(map, "z", 0D), + ((Double) M.g(map, "a", 0D)).floatValue(), ((Double) M.g(map, + "p", 0D)).floatValue()); + } +} diff --git a/src/nl/Steffion/BlockHunt/Serializables/M.java b/src/nl/Steffion/BlockHunt/Serializables/M.java new file mode 100644 index 0000000..f119413 --- /dev/null +++ b/src/nl/Steffion/BlockHunt/Serializables/M.java @@ -0,0 +1,9 @@ +package nl.Steffion.BlockHunt.Serializables; + +import java.util.Map; + +public class M { + public static Object g(Map map, String key, Object def) { + return (map.containsKey(key) ? map.get(key) : def); + } +} \ No newline at end of file diff --git a/src/nl/Steffion/BlockHunt/W.java b/src/nl/Steffion/BlockHunt/W.java new file mode 100644 index 0000000..fac9d2b --- /dev/null +++ b/src/nl/Steffion/BlockHunt/W.java @@ -0,0 +1,32 @@ +package nl.Steffion.BlockHunt; + +import java.util.ArrayList; + +import nl.Steffion.BlockHunt.Managers.ConfigM; +import nl.Steffion.BlockHunt.Managers.MessageM; + +public class W { + /* + * Made by @author Steffion, © 2013. + */ + + public static String pluginName = "BlockHunt"; + public static String pluginVersion = "v1.0.0"; + public static String engineVersion = "v1.1.0"; + public static String pluginAutors = "Steffion"; + public static String pluginMainPermission = pluginName + "."; + + public static ArrayList newFiles = new ArrayList(); + public static ConfigM config = new ConfigM("config", ""); + public static ConfigM messages = new ConfigM("messages", ""); + + public static void newFiles() { + ConfigM.setDefaults(); + for (String Filename : newFiles) { + MessageM.sendMessage(null, + "%WCouldn't find '%A%Filename%.yml%W' creating new one.", + true, "Filename-" + Filename); + } + newFiles.clear(); + } +} diff --git a/src/plugin.yml b/src/plugin.yml new file mode 100644 index 0000000..703b640 --- /dev/null +++ b/src/plugin.yml @@ -0,0 +1,14 @@ +name: BlockHunt +main: nl.Steffion.BlockHunt.BlockHunt +version: 1.0.0 +authors: + - Steffion +description: Description here. +commands: + BlockHunt: + usage: / + description: N/A + aliases: + - bh + - hideandseek + - hs \ No newline at end of file