summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPepich <benedikt.abel@yahoo.de>2017-07-08 12:12:30 +0200
committerPepich <benedikt.abel@yahoo.de>2017-07-08 12:12:30 +0200
commitca3aee41a63263c15211dddaaac9402a10c0b2d7 (patch)
tree6ccb034d43a3be93f241d43151d03716bbf5a097
parent25294b4e5f9ea1c64d7c3e671839844f09444ff5 (diff)
Preparation for APIv4, general code cleanup
-rw-r--r--src/com/redstoner/annotations/Commands.java12
-rw-r--r--src/com/redstoner/coremods/moduleLoader/ModuleLoader.java123
-rw-r--r--src/com/redstoner/misc/CommandHolderType.java13
-rw-r--r--src/com/redstoner/misc/Main.java4
-rw-r--r--src/com/redstoner/misc/Utils.java86
5 files changed, 125 insertions, 113 deletions
diff --git a/src/com/redstoner/annotations/Commands.java b/src/com/redstoner/annotations/Commands.java
new file mode 100644
index 0000000..1388de4
--- /dev/null
+++ b/src/com/redstoner/annotations/Commands.java
@@ -0,0 +1,12 @@
+package com.redstoner.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Target;
+
+import com.redstoner.misc.CommandHolderType;
+
+@Target(ElementType.TYPE)
+public @interface Commands
+{
+ CommandHolderType value();
+}
diff --git a/src/com/redstoner/coremods/moduleLoader/ModuleLoader.java b/src/com/redstoner/coremods/moduleLoader/ModuleLoader.java
index 555d841..bedb50e 100644
--- a/src/com/redstoner/coremods/moduleLoader/ModuleLoader.java
+++ b/src/com/redstoner/coremods/moduleLoader/ModuleLoader.java
@@ -3,6 +3,7 @@ package com.redstoner.coremods.moduleLoader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
@@ -12,7 +13,6 @@ import java.util.HashMap;
import java.util.List;
import org.bukkit.Bukkit;
-import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
@@ -23,6 +23,7 @@ import com.nemez.cmdmgr.Command;
import com.nemez.cmdmgr.Command.AsyncType;
import com.nemez.cmdmgr.CommandManager;
import com.redstoner.annotations.AutoRegisterListener;
+import com.redstoner.annotations.Commands;
import com.redstoner.annotations.Debugable;
import com.redstoner.annotations.Version;
import com.redstoner.coremods.debugger.Debugger;
@@ -32,12 +33,10 @@ import com.redstoner.misc.VersionHelper;
import com.redstoner.modules.CoreModule;
import com.redstoner.modules.Module;
-import net.minecraft.server.v1_12_R1.MinecraftServer;
-
/** The module loader, mother of all modules. Responsible for loading and taking care of all modules.
*
* @author Pepich */
-@Version(major = 3, minor = 2, revision = 5, compatible = 2)
+@Version(major = 4, minor = 0, revision = 0, compatible = 2)
public final class ModuleLoader implements CoreModule
{
private static ModuleLoader instance;
@@ -47,6 +46,7 @@ public final class ModuleLoader implements CoreModule
private static HashMap<Module, URLClassLoader> loaders = new HashMap<Module, URLClassLoader>();
private static File configFile;
private static FileConfiguration config;
+ private static boolean debugMode = false;
private ModuleLoader()
{
@@ -60,6 +60,7 @@ public final class ModuleLoader implements CoreModule
catch (MalformedURLException e)
{
System.out.println("Sumtin is wong with ya filesüstem m8. Fix eeeet or I won't werk!");
+ Bukkit.getPluginManager().disablePlugin(Main.plugin);
}
}
@@ -129,6 +130,19 @@ public final class ModuleLoader implements CoreModule
e.printStackTrace();
}
}
+ if (!config.contains("debugMode"))
+ {
+ config.set("debugMode", false);
+ try
+ {
+ config.save(configFile);
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ }
+ debugMode = config.getBoolean("debugMode");
for (String s : coremods)
if (!s.startsWith("#"))
ModuleLoader.addDynamicModule(s);
@@ -136,6 +150,7 @@ public final class ModuleLoader implements CoreModule
for (String s : autoload)
if (!s.startsWith("#"))
ModuleLoader.addDynamicModule(s);
+ enableModules();
}
/** This method will add a module to the module list, without enabling it.</br>
@@ -257,13 +272,36 @@ public final class ModuleLoader implements CoreModule
{
if (module.onEnable())
{
+ modules.put(module, true);
if (VersionHelper.isCompatible(VersionHelper.create(2, 0, 0, -1), module.getClass()))
CommandManager.registerCommand(module.getCommandString(), module, Main.plugin);
- modules.put(module, true);
- if (VersionHelper.isCompatible(VersionHelper.create(3, 0, 0, 3), module.getClass()))
+ if (VersionHelper.isCompatible(VersionHelper.create(4, 0, 0, 3), module.getClass()))
{
module.postEnable();
}
+ if (VersionHelper.isCompatible(VersionHelper.create(4, 0, 0, 4), module.getClass()))
+ {
+ Commands ann = module.getClass().getAnnotation(Commands.class);
+ if (ann != null)
+ {
+ switch (ann.value())
+ {
+ case File:
+ File f = new File(module.getClass().getName() + ".cmd");
+ CommandManager.registerCommand(f, module, Main.plugin);
+ break;
+ case Stream:
+ InputStream stream = module.getClass()
+ .getResourceAsStream(module.getClass().getSimpleName() + ".cmd");
+ CommandManager.registerCommand(stream, module, Main.plugin);
+ case String:
+ CommandManager.registerCommand(module.getCommandString(), module, Main.plugin);
+ break;
+ case None:
+ break;
+ }
+ }
+ }
Utils.info("Loaded module " + module.getClass().getName());
if (module.getClass().isAnnotationPresent(AutoRegisterListener.class) && (module instanceof Listener))
{
@@ -366,19 +404,12 @@ public final class ModuleLoader implements CoreModule
public static final void addDynamicModule(String name)
{
- Object[] status = getServerStatus();
for (Module m : modules.keySet())
{
if (m.getClass().getName().equals(name))
{
Utils.info(
"Found existing module, attempting override. WARNING! This operation will halt the main thread until it is completed.");
- Utils.info("Current server status:");
- Utils.info("Current system time: " + status[0]);
- Utils.info("Current tick: " + status[1]);
- Utils.info("Last TPS: " + status[2]);
- Utils.info("Entity count: " + status[3]);
- Utils.info("Player count: " + status[4]);
Utils.info("Attempting to load new class definition before disabling and removing the old module:");
boolean differs = false;
Utils.info("Old class definition: Class@" + m.getClass().hashCode());
@@ -446,18 +477,24 @@ public final class ModuleLoader implements CoreModule
Utils.info("Version of remote class: " + VersionHelper.getString(newVersion));
if (oldVersion.equals(newVersion))
{
- Utils.error("Detected equal module versions, aborting now...");
- try
- {
- cl.close();
- }
- catch (IOException e)
+ Utils.error("Detected equal module versions, " + (debugMode
+ ? " aborting now... Set debugMode to true in your config if you want to continue!"
+ : " continueing anyways."));
+ if (!debugMode)
{
- e.printStackTrace();
+ try
+ {
+ cl.close();
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ return;
}
- return;
}
- Utils.info("Versions differ, disabling old module:");
+ else
+ Utils.info("Versions differ, disabling old module:");
disableModule(m);
Utils.info("Disabled module, overriding the implementation:");
modules.remove(m);
@@ -474,14 +511,6 @@ public final class ModuleLoader implements CoreModule
loaders.put(module, cl);
Utils.info("Successfully updated class definition. Enabling new implementation:");
enableLoadedModule(module);
- Object[] newStatus = getServerStatus();
- Utils.info("Task complete! Took " + ((long) newStatus[0] - (long) status[0]) + "ms to finish!");
- Utils.info("Current server status:");
- Utils.info("Current system time: " + newStatus[0]);
- Utils.info("Current tick: " + newStatus[1]);
- Utils.info("Last TPS: " + newStatus[2]);
- Utils.info("Entity count: " + newStatus[3]);
- Utils.info("Player count: " + newStatus[4]);
return;
}
}
@@ -507,31 +536,16 @@ public final class ModuleLoader implements CoreModule
public static final void removeDynamicModule(String name)
{
- Object[] status = getServerStatus();
for (Module m : modules.keySet())
{
if (m.getClass().getName().equals(name))
{
Utils.info(
"Found existing module, attempting unload. WARNING! This operation will halt the main thread until it is completed.");
- Utils.info("Current server status:");
- Utils.info("Current system time: " + status[0]);
- Utils.info("Current tick: " + status[1]);
- Utils.info("Last TPS: " + status[2]);
- Utils.info("Entity count: " + status[3]);
- Utils.info("Player count: " + status[4]);
Utils.info("Attempting to disable module properly:");
disableModule(m);
modules.remove(m);
- Utils.info("Disabled module, overriding the implementation:");
- Object[] newStatus = getServerStatus();
- Utils.info("Task complete! Took " + ((long) newStatus[0] - (long) status[0]) + "ms to finish!");
- Utils.info("Current server status:");
- Utils.info("Current system time: " + newStatus[0]);
- Utils.info("Current tick: " + newStatus[1]);
- Utils.info("Last TPS: " + newStatus[2]);
- Utils.info("Entity count: " + newStatus[3]);
- Utils.info("Player count: " + newStatus[4]);
+ Utils.info("Disabled module.");
return;
}
}
@@ -544,23 +558,6 @@ public final class ModuleLoader implements CoreModule
Utils.error("Couldn't find module! Couldn't ");
}
- @SuppressWarnings("deprecation")
- private static final Object[] getServerStatus()
- {
- final Object[] status = new Object[5];
- status[0] = System.currentTimeMillis();
- status[1] = MinecraftServer.currentTick;
- status[2] = MinecraftServer.getServer().recentTps[0];
- int i = 0;
- for (World w : Bukkit.getWorlds())
- {
- i += w.getEntities().size();
- }
- status[3] = i;
- status[4] = Bukkit.getOnlinePlayers().size();
- return status;
- }
-
/** Finds a module by name for other modules to reference it.
*
* @param name the name of the module. Use the full path if you are not sure about the module's SimpleClassName being unique.
diff --git a/src/com/redstoner/misc/CommandHolderType.java b/src/com/redstoner/misc/CommandHolderType.java
new file mode 100644
index 0000000..19b9dda
--- /dev/null
+++ b/src/com/redstoner/misc/CommandHolderType.java
@@ -0,0 +1,13 @@
+package com.redstoner.misc;
+
+import com.redstoner.annotations.Version;
+
+/** @author Pepich */
+@Version(major = 4, minor = 0, revision = 0, compatible = -1)
+public enum CommandHolderType
+{
+ Stream,
+ File,
+ @Deprecated String,
+ None
+}
diff --git a/src/com/redstoner/misc/Main.java b/src/com/redstoner/misc/Main.java
index e6ef372..6c42f34 100644
--- a/src/com/redstoner/misc/Main.java
+++ b/src/com/redstoner/misc/Main.java
@@ -10,7 +10,7 @@ import com.redstoner.misc.mysql.MysqlHandler;
/** Main class. Duh.
*
* @author Pepich */
-@Version(major = 3, minor = 2, revision = 0, compatible = -1)
+@Version(major = 4, minor = 0, revision = 0, compatible = -1)
public class Main extends JavaPlugin
{
public static JavaPlugin plugin;
@@ -19,9 +19,11 @@ public class Main extends JavaPlugin
public void onEnable()
{
plugin = this;
+ // Configger.init();
Debugger.init();
MysqlHandler.init();
ModuleLoader.init();
+ // Load modules from config
ModuleLoader.loadFromConfig();
// And enable them
ModuleLoader.enableModules();
diff --git a/src/com/redstoner/misc/Utils.java b/src/com/redstoner/misc/Utils.java
index 973f31f..d449e6f 100644
--- a/src/com/redstoner/misc/Utils.java
+++ b/src/com/redstoner/misc/Utils.java
@@ -1,10 +1,10 @@
package com.redstoner.misc;
import java.text.SimpleDateFormat;
+import java.util.ArrayList;
import java.util.Date;
import org.bukkit.Bukkit;
-import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@@ -15,7 +15,7 @@ import com.redstoner.coremods.debugger.Debugger;
/** The utils class containing utility functions. Those include but are not limited to sending formatted messages, broadcasts and more.
*
* @author Pepich */
-@Version(major = 1, minor = 3, revision = 8, compatible = 1)
+@Version(major = 4, minor = 0, revision = 0, compatible = 1)
public final class Utils
{
/** The SimpleDateFormat used for getting the current date. */
@@ -63,8 +63,7 @@ public final class Utils
{
if (prefix == null)
prefix = "§8[§2" + getCaller() + "§8]: ";
- sendMessage(recipient, ChatColor.translateAlternateColorCodes(alternateColorCode, prefix).replace("&§", "&"),
- ChatColor.translateAlternateColorCodes(alternateColorCode, message).replace("&§", "&"));
+ sendMessage(recipient, colorify(prefix, alternateColorCode), colorify(message, alternateColorCode));
}
/** Invokes sendErrorMessage. This method will additionally translate alternate color codes for you.
@@ -77,21 +76,7 @@ public final class Utils
{
if (prefix == null)
prefix = "§8[§c" + getCaller() + "§8]: ";
- sendErrorMessage(recipient,
- ChatColor.translateAlternateColorCodes(alternateColorCode, prefix).replace("&§", "&"),
- ChatColor.translateAlternateColorCodes(alternateColorCode, message).replace("&§", "&"));
- }
-
- /** This method broadcasts a message to all players (and console) that are allowed by the filter. Set the filter to NULL to broadcast to everyone.</br>
- * This will not be logged to console except when you return true in the filter.
- *
- * @param message the message to be sent around
- * @param filter the BroadcastFilter to be applied.</br>
- * Write a class implementing the interface and pass it to this method, the "sendTo()" method will be called for each recipient.
- * @return the amount of people that received the message. */
- public static int broadcast(String prefix, String message, BroadcastFilter filter)
- {
- return broadcast(prefix, message, filter, null);
+ sendErrorMessage(recipient, colorify(prefix, '&'), colorify(message, '&'));
}
/** This method broadcasts a message to all players (and console) that are allowed by the filter. Set the filter to NULL to broadcast to everyone.</br>
@@ -106,8 +91,8 @@ public final class Utils
{
if (prefix == null)
prefix = "§8[§2" + getCaller() + "§8]: ";
- return broadcast(ChatColor.translateAlternateColorCodes(alternateColorCode, prefix).replace("&§", "&"),
- ChatColor.translateAlternateColorCodes(alternateColorCode, message).replace("&§", "&"), filter, null);
+ message = colorify(message, alternateColorCode);
+ return broadcast(prefix, message, filter);
}
/** This method broadcasts a message to all players and console that are allowed by the filter. Set the filter to NULL to broadcast to everyone.</br>
@@ -121,20 +106,16 @@ public final class Utils
* @param logmessage the log message to appear in console. Set to null to not log this (you can still log the original message by returning true in the filter).
* @return the amount of people that received the message. */
@Debugable
- public static int broadcast(String prefix, String message, BroadcastFilter filter, String logmessage)
+ public static int broadcast(String prefix, String message, BroadcastFilter filter)
{
if (prefix == null)
prefix = "§8[§2" + getCaller() + "§8]: ";
- Debugger.notifyMethod(message, filter, logmessage);
- if (logmessage != null)
- sendMessage(Bukkit.getConsoleSender(), prefix, logmessage);
if (filter == null)
{
for (Player p : Bukkit.getOnlinePlayers())
p.sendMessage(prefix + message);
- if (logmessage == null)
- Bukkit.getConsoleSender().sendMessage(prefix + message);
- return Bukkit.getOnlinePlayers().size();
+ Bukkit.getConsoleSender().sendMessage(prefix + message);
+ return Bukkit.getOnlinePlayers().size() + 1;
}
else
{
@@ -145,12 +126,11 @@ public final class Utils
p.sendMessage(prefix + message);
count++;
}
- if (logmessage == null)
- if (filter.sendTo(Bukkit.getConsoleSender()))
- {
- Bukkit.getConsoleSender().sendMessage(prefix + message);
- count++;
- }
+ if (filter.sendTo(Bukkit.getConsoleSender()))
+ {
+ Bukkit.getConsoleSender().sendMessage(prefix + message);
+ count++;
+ }
return count;
}
}
@@ -173,8 +153,7 @@ public final class Utils
Debugger.notifyMethod(message);
String classname = getCaller();
String prefix = "§8[§2" + classname + "§8]: ";
- Bukkit.getConsoleSender()
- .sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + "§7" + message).replace("&§", "&"));
+ Bukkit.getConsoleSender().sendMessage(colorify(prefix + "§7" + message, Bukkit.getConsoleSender(), '&'));
}
/** Prints a warning message into console. Supports "&" color codes.
@@ -186,8 +165,7 @@ public final class Utils
Debugger.notifyMethod(message);
String classname = getCaller();
String prefix = "§e[WARN]: §8[§e" + classname + "§8]: ";
- Bukkit.getConsoleSender()
- .sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + "§7" + message).replace("&§", "&"));
+ Bukkit.getConsoleSender().sendMessage(colorify(prefix + "§7" + message, Bukkit.getConsoleSender(), '&'));
}
/** Used to make an error output to console. Supports "&" color codes.
@@ -199,8 +177,7 @@ public final class Utils
Debugger.notifyMethod(message);
String classname = getCaller();
String prefix = "§c[ERROR]: §8[§c" + classname + "§8]: ";
- Bukkit.getConsoleSender()
- .sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + "§7" + message).replace("&§", "&"));
+ Bukkit.getConsoleSender().sendMessage(colorify(prefix + "§7" + message, Bukkit.getConsoleSender(), '&'));
}
/** This method will find the next parent caller and return their class name, omitting package names.
@@ -221,11 +198,13 @@ public final class Utils
*
* @param directCaller used to prevent this method from returning the caller itself. Null if supposed to be ignored.
* @return the name of the calling class. */
- public static final String getCaller(String directCaller)
+ public static final String getCaller(ArrayList<String> directCaller)
{
+ if (directCaller == null || directCaller.size() == 0)
+ return getCaller();
StackTraceElement[] stackTrace = (new Exception()).getStackTrace();
- String classname = (directCaller == null ? "Utils" : directCaller);
- for (int i = 0; classname.equals(directCaller) || classname.equals("Utils"); i++)
+ String classname = "Utils";
+ for (int i = 0; directCaller.contains(classname) || classname.equals("Utils"); i++)
{
classname = stackTrace[i].getClassName().replaceAll(".*\\.", "");
}
@@ -269,7 +248,16 @@ public final class Utils
if (sender instanceof Player)
return ((Player) sender).getDisplayName();
else
- return "&9" + sender.getName();
+ return "§9" + sender.getName();
+ }
+
+ /** This method "colorifies" a message.
+ *
+ * @param message the message to be colored.
+ * @return the colorified message. */
+ public static String colorify(String message, char alternateColorcode)
+ {
+ return colorify(message, Bukkit.getConsoleSender(), alternateColorcode);
}
/** This method "colorifies" a message using proper permissions.
@@ -277,14 +265,14 @@ public final class Utils
* @param message the message to be colored.
* @param sender the command sender whose permissions shall be applied.
* @return the colorified message. */
- public static String colorify(String message, CommandSender sender)
+ public static String colorify(String message, CommandSender sender, char alternateColorcode)
{
if (sender.hasPermission("essentials.chat.color"))
- message = message.replaceAll("&([0-9a-fA-FrR])", "§$1");
+ message = message.replaceAll(alternateColorcode + "([0-9a-fA-FrR])", "§$1");
if (sender.hasPermission("essentials.chat.format"))
- message = message.replaceAll("&(l-oL-OrR)", "§$1");
+ message = message.replaceAll(alternateColorcode + "(l-oL-OrR)", "§$1");
if (sender.hasPermission("essentials.chat.magic"))
- message = message.replaceAll("&([kKrR])", "§$1");
- return message.replace("&§", "&");
+ message = message.replaceAll(alternateColorcode + "([kKrR])", "§$1");
+ return message.replace(alternateColorcode + "§", alternateColorcode + "");
}
}