0

APIv4, support for spigot 1.12 (and up) #6

Merged
Pepich merged 13 commits from APIv4_spigot_1.12 into master 2017-10-31 15:53:31 +00:00
5 changed files with 125 additions and 113 deletions
Showing only changes of commit ca3aee41a6 - Show all commits

View File

@ -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();
}

View File

@ -3,6 +3,7 @@ package com.redstoner.coremods.moduleLoader;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
@ -12,7 +13,6 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration; 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.Command.AsyncType;
import com.nemez.cmdmgr.CommandManager; import com.nemez.cmdmgr.CommandManager;
import com.redstoner.annotations.AutoRegisterListener; import com.redstoner.annotations.AutoRegisterListener;
import com.redstoner.annotations.Commands;
import com.redstoner.annotations.Debugable; import com.redstoner.annotations.Debugable;
import com.redstoner.annotations.Version; import com.redstoner.annotations.Version;
import com.redstoner.coremods.debugger.Debugger; import com.redstoner.coremods.debugger.Debugger;
@ -32,12 +33,10 @@ import com.redstoner.misc.VersionHelper;
import com.redstoner.modules.CoreModule; import com.redstoner.modules.CoreModule;
import com.redstoner.modules.Module; 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. /** The module loader, mother of all modules. Responsible for loading and taking care of all modules.
* *
* @author Pepich */ * @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 public final class ModuleLoader implements CoreModule
{ {
private static ModuleLoader instance; 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 HashMap<Module, URLClassLoader> loaders = new HashMap<Module, URLClassLoader>();
private static File configFile; private static File configFile;
private static FileConfiguration config; private static FileConfiguration config;
private static boolean debugMode = false;
private ModuleLoader() private ModuleLoader()
{ {
@ -60,6 +60,7 @@ public final class ModuleLoader implements CoreModule
catch (MalformedURLException e) catch (MalformedURLException e)
{ {
System.out.println("Sumtin is wong with ya filesüstem m8. Fix eeeet or I won't werk!"); 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(); 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) for (String s : coremods)
if (!s.startsWith("#")) if (!s.startsWith("#"))
ModuleLoader.addDynamicModule(s); ModuleLoader.addDynamicModule(s);
@ -136,6 +150,7 @@ public final class ModuleLoader implements CoreModule
for (String s : autoload) for (String s : autoload)
if (!s.startsWith("#")) if (!s.startsWith("#"))
ModuleLoader.addDynamicModule(s); ModuleLoader.addDynamicModule(s);
enableModules();
} }
/** This method will add a module to the module list, without enabling it.</br> /** 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()) if (module.onEnable())
{ {
modules.put(module, true);
if (VersionHelper.isCompatible(VersionHelper.create(2, 0, 0, -1), module.getClass())) if (VersionHelper.isCompatible(VersionHelper.create(2, 0, 0, -1), module.getClass()))
CommandManager.registerCommand(module.getCommandString(), module, Main.plugin); CommandManager.registerCommand(module.getCommandString(), module, Main.plugin);
modules.put(module, true); if (VersionHelper.isCompatible(VersionHelper.create(4, 0, 0, 3), module.getClass()))
if (VersionHelper.isCompatible(VersionHelper.create(3, 0, 0, 3), module.getClass()))
{ {
module.postEnable(); 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()); Utils.info("Loaded module " + module.getClass().getName());
if (module.getClass().isAnnotationPresent(AutoRegisterListener.class) && (module instanceof Listener)) 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) public static final void addDynamicModule(String name)
{ {
Object[] status = getServerStatus();
for (Module m : modules.keySet()) for (Module m : modules.keySet())
{ {
if (m.getClass().getName().equals(name)) if (m.getClass().getName().equals(name))
{ {
Utils.info( Utils.info(
"Found existing module, attempting override. WARNING! This operation will halt the main thread until it is completed."); "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:"); Utils.info("Attempting to load new class definition before disabling and removing the old module:");
boolean differs = false; boolean differs = false;
Utils.info("Old class definition: Class@" + m.getClass().hashCode()); 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)); Utils.info("Version of remote class: " + VersionHelper.getString(newVersion));
if (oldVersion.equals(newVersion)) if (oldVersion.equals(newVersion))
{ {
Utils.error("Detected equal module versions, aborting now..."); Utils.error("Detected equal module versions, " + (debugMode
try ? " aborting now... Set debugMode to true in your config if you want to continue!"
: " continueing anyways."));
if (!debugMode)
{ {
cl.close(); try
{
cl.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return;
} }
catch (IOException e)
{
e.printStackTrace();
}
return;
} }
Utils.info("Versions differ, disabling old module:"); else
Utils.info("Versions differ, disabling old module:");
disableModule(m); disableModule(m);
Utils.info("Disabled module, overriding the implementation:"); Utils.info("Disabled module, overriding the implementation:");
modules.remove(m); modules.remove(m);
@ -474,14 +511,6 @@ public final class ModuleLoader implements CoreModule
loaders.put(module, cl); loaders.put(module, cl);
Utils.info("Successfully updated class definition. Enabling new implementation:"); Utils.info("Successfully updated class definition. Enabling new implementation:");
enableLoadedModule(module); 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; return;
} }
} }
@ -507,31 +536,16 @@ public final class ModuleLoader implements CoreModule
public static final void removeDynamicModule(String name) public static final void removeDynamicModule(String name)
{ {
Object[] status = getServerStatus();
for (Module m : modules.keySet()) for (Module m : modules.keySet())
{ {
if (m.getClass().getName().equals(name)) if (m.getClass().getName().equals(name))
{ {
Utils.info( Utils.info(
"Found existing module, attempting unload. WARNING! This operation will halt the main thread until it is completed."); "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:"); Utils.info("Attempting to disable module properly:");
disableModule(m); disableModule(m);
modules.remove(m); modules.remove(m);
Utils.info("Disabled module, overriding the implementation:"); Utils.info("Disabled 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; return;
} }
} }
@ -544,23 +558,6 @@ public final class ModuleLoader implements CoreModule
Utils.error("Couldn't find module! Couldn't "); 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. /** 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. * @param name the name of the module. Use the full path if you are not sure about the module's SimpleClassName being unique.

View File

@ -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
}

View File

@ -10,7 +10,7 @@ import com.redstoner.misc.mysql.MysqlHandler;
/** Main class. Duh. /** Main class. Duh.
* *
* @author Pepich */ * @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 class Main extends JavaPlugin
{ {
public static JavaPlugin plugin; public static JavaPlugin plugin;
@ -19,9 +19,11 @@ public class Main extends JavaPlugin
public void onEnable() public void onEnable()
{ {
plugin = this; plugin = this;
// Configger.init();
Debugger.init(); Debugger.init();
MysqlHandler.init(); MysqlHandler.init();
ModuleLoader.init(); ModuleLoader.init();
// Load modules from config
ModuleLoader.loadFromConfig(); ModuleLoader.loadFromConfig();
// And enable them // And enable them
ModuleLoader.enableModules(); ModuleLoader.enableModules();

View File

@ -1,10 +1,10 @@
package com.redstoner.misc; package com.redstoner.misc;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; 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. /** The utils class containing utility functions. Those include but are not limited to sending formatted messages, broadcasts and more.
* *
* @author Pepich */ * @author Pepich */
@Version(major = 1, minor = 3, revision = 8, compatible = 1) @Version(major = 4, minor = 0, revision = 0, compatible = 1)
public final class Utils public final class Utils
{ {
/** The SimpleDateFormat used for getting the current date. */ /** The SimpleDateFormat used for getting the current date. */
@ -63,8 +63,7 @@ public final class Utils
{ {
if (prefix == null) if (prefix == null)
prefix = "§8[§2" + getCaller() + "§8]: "; prefix = "§8[§2" + getCaller() + "§8]: ";
sendMessage(recipient, ChatColor.translateAlternateColorCodes(alternateColorCode, prefix).replace("", "&"), sendMessage(recipient, colorify(prefix, alternateColorCode), colorify(message, alternateColorCode));
ChatColor.translateAlternateColorCodes(alternateColorCode, message).replace("", "&"));
} }
/** Invokes sendErrorMessage. This method will additionally translate alternate color codes for you. /** Invokes sendErrorMessage. This method will additionally translate alternate color codes for you.
@ -77,21 +76,7 @@ public final class Utils
{ {
if (prefix == null) if (prefix == null)
prefix = "§8[§c" + getCaller() + "§8]: "; prefix = "§8[§c" + getCaller() + "§8]: ";
sendErrorMessage(recipient, sendErrorMessage(recipient, colorify(prefix, '&'), colorify(message, '&'));
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);
} }
/** 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 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) if (prefix == null)
prefix = "§8[§2" + getCaller() + "§8]: "; prefix = "§8[§2" + getCaller() + "§8]: ";
return broadcast(ChatColor.translateAlternateColorCodes(alternateColorCode, prefix).replace("", "&"), message = colorify(message, alternateColorCode);
ChatColor.translateAlternateColorCodes(alternateColorCode, message).replace("", "&"), filter, null); 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> /** 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). * @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. */ * @return the amount of people that received the message. */
@Debugable @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) if (prefix == null)
prefix = "§8[§2" + getCaller() + "§8]: "; prefix = "§8[§2" + getCaller() + "§8]: ";
Debugger.notifyMethod(message, filter, logmessage);
if (logmessage != null)
sendMessage(Bukkit.getConsoleSender(), prefix, logmessage);
if (filter == null) if (filter == null)
{ {
for (Player p : Bukkit.getOnlinePlayers()) for (Player p : Bukkit.getOnlinePlayers())
p.sendMessage(prefix + message); p.sendMessage(prefix + message);
if (logmessage == null) Bukkit.getConsoleSender().sendMessage(prefix + message);
Bukkit.getConsoleSender().sendMessage(prefix + message); return Bukkit.getOnlinePlayers().size() + 1;
return Bukkit.getOnlinePlayers().size();
} }
else else
{ {
@ -145,12 +126,11 @@ public final class Utils
p.sendMessage(prefix + message); p.sendMessage(prefix + message);
count++; count++;
} }
if (logmessage == null) if (filter.sendTo(Bukkit.getConsoleSender()))
if (filter.sendTo(Bukkit.getConsoleSender())) {
{ Bukkit.getConsoleSender().sendMessage(prefix + message);
Bukkit.getConsoleSender().sendMessage(prefix + message); count++;
count++; }
}
return count; return count;
} }
} }
@ -173,8 +153,7 @@ public final class Utils
Debugger.notifyMethod(message); Debugger.notifyMethod(message);
String classname = getCaller(); String classname = getCaller();
String prefix = "§8[§2" + classname + "§8]: "; String prefix = "§8[§2" + classname + "§8]: ";
Bukkit.getConsoleSender() Bukkit.getConsoleSender().sendMessage(colorify(prefix + "§7" + message, Bukkit.getConsoleSender(), '&'));
.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + "§7" + message).replace("", "&"));
} }
/** Prints a warning message into console. Supports "&" color codes. /** Prints a warning message into console. Supports "&" color codes.
@ -186,8 +165,7 @@ public final class Utils
Debugger.notifyMethod(message); Debugger.notifyMethod(message);
String classname = getCaller(); String classname = getCaller();
String prefix = "§e[WARN]: §8[§e" + classname + "§8]: "; String prefix = "§e[WARN]: §8[§e" + classname + "§8]: ";
Bukkit.getConsoleSender() Bukkit.getConsoleSender().sendMessage(colorify(prefix + "§7" + message, Bukkit.getConsoleSender(), '&'));
.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + "§7" + message).replace("", "&"));
} }
/** Used to make an error output to console. Supports "&" color codes. /** Used to make an error output to console. Supports "&" color codes.
@ -199,8 +177,7 @@ public final class Utils
Debugger.notifyMethod(message); Debugger.notifyMethod(message);
String classname = getCaller(); String classname = getCaller();
String prefix = "§c[ERROR]: §8[§c" + classname + "§8]: "; String prefix = "§c[ERROR]: §8[§c" + classname + "§8]: ";
Bukkit.getConsoleSender() Bukkit.getConsoleSender().sendMessage(colorify(prefix + "§7" + message, Bukkit.getConsoleSender(), '&'));
.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + "§7" + message).replace("", "&"));
} }
/** This method will find the next parent caller and return their class name, omitting package names. /** 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. * @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. */ * @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(); StackTraceElement[] stackTrace = (new Exception()).getStackTrace();
String classname = (directCaller == null ? "Utils" : directCaller); String classname = "Utils";
for (int i = 0; classname.equals(directCaller) || classname.equals("Utils"); i++) for (int i = 0; directCaller.contains(classname) || classname.equals("Utils"); i++)
{ {
classname = stackTrace[i].getClassName().replaceAll(".*\\.", ""); classname = stackTrace[i].getClassName().replaceAll(".*\\.", "");
} }
@ -269,7 +248,16 @@ public final class Utils
if (sender instanceof Player) if (sender instanceof Player)
return ((Player) sender).getDisplayName(); return ((Player) sender).getDisplayName();
else 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. /** This method "colorifies" a message using proper permissions.
@ -277,14 +265,14 @@ public final class Utils
* @param message the message to be colored. * @param message the message to be colored.
* @param sender the command sender whose permissions shall be applied. * @param sender the command sender whose permissions shall be applied.
* @return the colorified message. */ * @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")) 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")) 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")) if (sender.hasPermission("essentials.chat.magic"))
message = message.replaceAll("&([kKrR])", "§$1"); message = message.replaceAll(alternateColorcode + "([kKrR])", "§$1");
return message.replace("", "&"); return message.replace(alternateColorcode + "§", alternateColorcode + "");
} }
} }