More plugin system work

This commit is contained in:
ceikry 2022-07-10 09:58:08 -05:00 committed by Ceikry
parent a6577064b0
commit 8dfbcb9423
8 changed files with 91 additions and 9 deletions

View file

@ -6,6 +6,7 @@ import rt4.Player;
/** /**
* The base plugin class which is meant to be extended by plugins. * The base plugin class which is meant to be extended by plugins.
* Contains callbacks to many parts of the internal client code. * Contains callbacks to many parts of the internal client code.
* @author ceikry
*/ */
public abstract class Plugin { public abstract class Plugin {
long timeOfLastDraw; long timeOfLastDraw;
@ -55,4 +56,6 @@ public abstract class Plugin {
* @param screenY the Y coordinate on the screen for overhead drawing * @param screenY the Y coordinate on the screen for overhead drawing
*/ */
public void NPCOverheadDraw(Npc npc, int screenX, int screenY) {} public void NPCOverheadDraw(Npc npc, int screenX, int screenY) {}
public void ProcessCommand(String commandStr, String[] args) {}
} }

View file

@ -1,12 +1,14 @@
package plugin; package plugin;
import rt4.GlobalJsonConfig; import rt4.GlobalJsonConfig;
import rt4.JagString;
import rt4.Npc; import rt4.Npc;
import rt4.Player; import rt4.Player;
import java.io.File; import java.io.File;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Objects; import java.util.Objects;
@ -90,4 +92,10 @@ public class PluginRepository {
public static void PlayerOverheadDraw(Player player, int screenX, int screenY) { public static void PlayerOverheadDraw(Player player, int screenX, int screenY) {
loadedPlugins.values().forEach((plugin) -> plugin.PlayerOverheadDraw(player, screenX, screenY)); loadedPlugins.values().forEach((plugin) -> plugin.PlayerOverheadDraw(player, screenX, screenY));
} }
public static void ProcessCommand(JagString commandStr) {
String[] tokens = commandStr.toString().split(" ");
String[] args = Arrays.copyOfRange(tokens, 1, tokens.length);
loadedPlugins.values().forEach((plugin) -> plugin.ProcessCommand(tokens[0], args));
}
} }

View file

@ -1,9 +1,11 @@
package plugin.api; package plugin.api;
import rt4.Font; import rt4.*;
import rt4.Fonts;
import rt4.JagString;
/**
* API used for writing plugins, so dozens of plugins don't break when we rename shit :)
* @author ceikry
*/
public class API { public class API {
public static void DrawText(FontType fontType, FontColor color, TextModifier mod, String text, int screenX, int screenY) { public static void DrawText(FontType fontType, FontColor color, TextModifier mod, String text, int screenX, int screenY) {
JagString js = JagString.parse(text); JagString js = JagString.parse(text);
@ -41,4 +43,8 @@ public class API {
break; break;
} }
} }
public static boolean PlayerHasPrivilege(Privileges privilege) {
return LoginManager.staffModLevel >= privilege.ordinal();
}
} }

View file

@ -0,0 +1,7 @@
package plugin.api;
public enum Privileges {
NONE,
PMOD,
JMOD
}

View file

@ -109,6 +109,7 @@ public class Cheat {
@OriginalMember(owner = "client!k", name = "a", descriptor = "(Lclient!na;Z)V") @OriginalMember(owner = "client!k", name = "a", descriptor = "(Lclient!na;Z)V")
public static void execute(@OriginalArg(0) JagString arg0) { public static void execute(@OriginalArg(0) JagString arg0) {
PluginRepository.ProcessCommand(arg0);
// if (LoginManager.staffModLevel >= 2) { // if (LoginManager.staffModLevel >= 2) {
@Pc(18) int local18; @Pc(18) int local18;
@Pc(38) int local38; @Pc(38) int local38;

View file

@ -21,7 +21,46 @@ test {
useJUnitPlatform() useJUnitPlatform()
} }
task buildPlugins { task initializeNewPlugin {
dependsOn(classes) doLast {
def pluginFile = new File("src/main/java/MyPlugin")
pluginFile.mkdirs()
def props = new File(rootProject.project("plugin-playground").projectDir.absolutePath + File.separator + "src/main/java/MyPlugin/plugin.properties").text = """
AUTHOR='Me'
DESCRIPTION='Make sure to rename both the MyPlugin folder and the package statement in plugin.java!
VERSION=-1.1
"""
def java = new File(rootProject.project("plugin-playground").projectDir.absolutePath + File.separator + "src/main/java/MyPlugin/plugin.java").text = """
package MyPlugin;
import plugin.Plugin;
public class plugin extends Plugin {
@Override
public void Init() {
//Init() is called when the plugin is loaded
}
@Override
public void Update() {
//Update() is called once per tick (600ms)
}
@Override
public void Draw(long deltaTime) {
//Draw() is called once per frame, with deltaTime being the time since last frame.
}
//Check the source of plugin.Plugin for more methods you can override! Happy hacking! <3
//There are also many methods to aid in plugin development in plugin.api.API
}
"""
}
}
task buildPlugins(type: Copy, dependsOn: classes) {
def pluginsPath = rootProject.project("client").projectDir.absolutePath + File.separator + "plugins"
from "build/classes/java/main"
into pluginsPath
} }

View file

@ -1,15 +1,19 @@
package OverheadDebugPlugin; package OverheadDebugPlugin;
import plugin.Plugin; import plugin.Plugin;
import plugin.api.API; import plugin.api.*;
import plugin.api.FontColor;
import plugin.api.FontType;
import plugin.api.TextModifier;
import rt4.*; import rt4.*;
/**
* @author ceikry
*/
public class plugin extends Plugin { public class plugin extends Plugin {
private boolean isEnabled = false;
@Override @Override
public void PlayerOverheadDraw(Player player, int screenX, int screenY) { public void PlayerOverheadDraw(Player player, int screenX, int screenY) {
if (!isEnabled) return;
API.DrawText( API.DrawText(
FontType.SMALL, FontType.SMALL,
FontColor.YELLOW, FontColor.YELLOW,
@ -22,6 +26,8 @@ public class plugin extends Plugin {
@Override @Override
public void NPCOverheadDraw(Npc npc, int screenX, int screenY) { public void NPCOverheadDraw(Npc npc, int screenX, int screenY) {
if (!isEnabled) return;
String npcSb = String npcSb =
(npc.type.name.strEquals(JagString.parse("null")) (npc.type.name.strEquals(JagString.parse("null"))
? npc.type.getMultiNpc() != null ? npc.type.getMultiNpc() != null
@ -44,4 +50,13 @@ public class plugin extends Plugin {
screenY screenY
); );
} }
@Override
public void ProcessCommand(String commandStr, String[] args) {
if (!API.PlayerHasPrivilege(Privileges.JMOD)) return;
if (commandStr.equalsIgnoreCase("::npcdebug")) {
isEnabled = !isEnabled;
}
}
} }

View file

@ -0,0 +1,3 @@
AUTHOR="Ceikry"
DESCRIPTION="Renders helpful debug text over the heads of players and NPCs."
VERSION=1.0