generated from srnyx/plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.1.6: Dependency download improvements
- Small improvements for 'AnnoyingMessage' - Merged 'AnnoyingCooldown#check()' into 'AnnoyingCooldown#isOnCooldown()' - 'AnnoyingUtility#getString(AnnoyingPlugin, String)' now checks if 'AnnoyingPlugin#messages' is 'null' - Added 'ApiCommand' to give the API plugin a command to get the API's version - Added 'version' message to 'messages.yml' - Added 'commands' and 'permissions' sections to 'plugin.yml' - Added 'AnnoyingCommandRegister' - This has methods to register commands from another plugin - Used when downloading/enabling dependencies - Added test JAR - The test JAR is a plugin that uses the API - This will allow me to more easily test stuff before release - The plugin contains a simple command, listener, dependency, and a few other tests - Improved 'AnnoyingDependency' - Added '#enableAfterDownload' option, if 'true', AnnoyingAPI will attempt to load/enable the plugin once it's downloaded - Added '#getFile()' to get the 'File' of the new JAR file of the dependency - Changed '#isInstalled()' to '#isNotInstalled()' because it was always inverted - Improved 'AnnoyingDownload' - '#dependencies' is now a 'List', so they will be installed in the order of when they were added (roughly, threads mess it up a bit) - Removed '#finishTask', 'FinishTask', and 'AnnoyingOptions#dependencyFinishTask' - If the Spigot plugin is external and the external URL ends with '.jar', it will treat it as a 'Platform.EXTERNAL' - '#finish' (now '#finish(AnnoyingDependency, boolean)') has been improved to allow dependencies to be loaded, enabled, and their commands registered - Added '#registerCommands(Plugin)' to register a plugin's (dependency) commands - Improved 'AnnoyingPlugin' - Added static '#missingDependencies' to track the missing dependencies from ALL plugins using the API - Added static '#commandRegister' to initialize a new 'AnnoyingCommandRegister' - Moved API option setting from the constructor to '#onEnable()' - Adjusted algorithm for getting missing dependencies - Only run 'AnnoyingDownload#downloadPlugins' from the API instance
- Loading branch information
Showing
18 changed files
with
610 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/main/java/xyz/srnyx/annoyingapi/AnnoyingCommandRegister.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package xyz.srnyx.annoyingapi; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.command.Command; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
|
||
/** | ||
* A class that registers commands using the Brigadier API | ||
* <p><b>All</b> credit for this class goes to <a href="https://spigotmc.org/resources/authors/623700">realEntity303</a>, author of <a href="https://spigotmc.org/resources/88135">PlugManX</a> | ||
*/ | ||
public class AnnoyingCommandRegister { | ||
private String nmsVersion; | ||
private Class<?> minecraftServerClass; | ||
private Method getServerMethod; | ||
private Field vanillaCommandDispatcherField; | ||
private Constructor<?> bukkitcommandWrapperConstructor; | ||
private Method registerMethod; | ||
private Method syncCommandsMethod; | ||
private Method aMethod; | ||
|
||
/** | ||
* Initialize the command register | ||
*/ | ||
public AnnoyingCommandRegister() { | ||
// nmsVersion | ||
try { | ||
this.nmsVersion = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3]; | ||
} catch (final ArrayIndexOutOfBoundsException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
// minecraftServerClass | ||
try { | ||
this.minecraftServerClass = Class.forName("net.minecraft.server." + this.nmsVersion + ".MinecraftServer"); | ||
} catch (final ClassNotFoundException e) { | ||
try { | ||
this.minecraftServerClass = Class.forName("net.minecraft.server.MinecraftServer"); | ||
} catch (final ClassNotFoundException e2) { | ||
e2.addSuppressed(e); | ||
e2.printStackTrace(); | ||
} | ||
} | ||
|
||
// getServerMethod | ||
try { | ||
this.getServerMethod = this.minecraftServerClass.getMethod("getServer"); | ||
this.getServerMethod.setAccessible(true); | ||
} catch (final NoSuchMethodException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
// vanillaCommandDispatcherField | ||
try { | ||
this.vanillaCommandDispatcherField = this.minecraftServerClass.getDeclaredField("vanillaCommandDispatcher"); | ||
this.vanillaCommandDispatcherField.setAccessible(true); | ||
} catch (final NoSuchFieldException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
// bukkitcommandWrapperConstructor | ||
try { | ||
this.bukkitcommandWrapperConstructor = Class.forName("org.bukkit.craftbukkit." + this.nmsVersion + ".command.BukkitCommandWrapper").getDeclaredConstructor(Class.forName("org.bukkit.craftbukkit." + this.nmsVersion + ".CraftServer"), Command.class); | ||
this.bukkitcommandWrapperConstructor.setAccessible(true); | ||
} catch (final NoSuchMethodException | ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
// registerMethod | ||
try { | ||
this.registerMethod = Class.forName("org.bukkit.craftbukkit." + this.nmsVersion + ".command.BukkitCommandWrapper").getMethod("register", com.mojang.brigadier.CommandDispatcher.class, String.class); | ||
this.registerMethod.setAccessible(true); | ||
} catch (final NoSuchMethodException | ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
// syncCommandsMethod | ||
try { | ||
this.syncCommandsMethod = Class.forName("org.bukkit.craftbukkit." + this.nmsVersion + ".CraftServer").getDeclaredMethod("syncCommands"); | ||
this.syncCommandsMethod.setAccessible(true); | ||
} catch (final NoSuchMethodException | ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* Register a command from another plugin | ||
* | ||
* @param command the command to register | ||
* @param fallbackPrefix the fallback command prefix | ||
*/ | ||
public void register(@NotNull Command command, @NotNull String fallbackPrefix) { | ||
if (nmsVersion == null) return; | ||
|
||
// Get commandDispatcher | ||
final Object commandDispatcher; | ||
try { | ||
commandDispatcher = this.vanillaCommandDispatcherField.get(this.getServerMethod.invoke(this.minecraftServerClass)); | ||
} catch (final IllegalAccessException | InvocationTargetException e) { | ||
e.printStackTrace(); | ||
return; | ||
} | ||
|
||
// Get aMethod | ||
if (this.aMethod == null) try { | ||
this.aMethod = commandDispatcher.getClass().getDeclaredMethod("a"); | ||
this.aMethod.setAccessible(true); | ||
} catch (final NoSuchMethodException e) { | ||
e.printStackTrace(); | ||
return; | ||
} | ||
|
||
// Register command | ||
try { | ||
this.registerMethod.invoke(this.bukkitcommandWrapperConstructor.newInstance(Bukkit.getServer(), command), this.aMethod.invoke(commandDispatcher), fallbackPrefix); | ||
} catch (final IllegalAccessException | InvocationTargetException | InstantiationException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* Refreshes the command list | ||
*/ | ||
public void sync() { | ||
try { | ||
this.syncCommandsMethod.invoke(Bukkit.getServer()); | ||
} catch (final IllegalAccessException | InvocationTargetException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.