Skip to content

Commit

Permalink
Merge pull request #12 from Ceikry/master
Browse files Browse the repository at this point in the history
Add support for inserting new right click options, add woah's timer plugin
  • Loading branch information
Pazaz committed Sep 25, 2022
2 parents 9c6ada0 + 7613080 commit 58b45eb
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 5 deletions.
6 changes: 6 additions & 0 deletions client/src/main/java/plugin/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,10 @@ public void OnLogout() {}
* @param entry the entry
*/
public void DrawMiniMenu(MiniMenuEntry entry) {}

/**
* Called by the client code when we generate the MiniMenu
* @param currentEntries
*/
public void OnMiniMenuCreate(MiniMenuEntry[] currentEntries) {}
}
8 changes: 8 additions & 0 deletions client/src/main/java/plugin/PluginRepository.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package plugin;

import plugin.api.API;
import plugin.api.MiniMenuEntry;
import plugin.api.MiniMenuType;
import rt4.*;
Expand All @@ -24,6 +25,8 @@ public static void registerPlugin(PluginInfo info, Plugin plugin) {
loadedPlugins.put(info, plugin);
}

public static int lastMiniMenu;

public static void reloadPlugins() {
loadedPlugins.clear();
Init();
Expand Down Expand Up @@ -134,4 +137,9 @@ public static void OnLogout() {
public static void DrawMiniMenu(MiniMenuEntry entry) {
loadedPlugins.values().forEach((plugin) -> plugin.DrawMiniMenu(entry));
}

public static void OnMiniMenuCreate() {
API.customMiniMenuIndex = 0;
loadedPlugins.values().forEach((plugin) -> plugin.OnMiniMenuCreate(API.GetMiniMenuEntries()));
}
}
17 changes: 17 additions & 0 deletions client/src/main/java/plugin/api/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* @author ceikry
*/
public class API {
public static Runnable[] miniMenuCustomActions = new Runnable[10];
public static int customMiniMenuIndex = 0;

public static void DrawText(FontType fontType, FontColor color, TextModifier mod, String text, int screenX, int screenY) {
JagString js = JagString.of(text);

Expand Down Expand Up @@ -205,4 +208,18 @@ public static MiniMenuEntry[] GetMiniMenuEntries() {
}
return entries.toArray(new MiniMenuEntry[]{});
}

public static MiniMenuEntry InsertMiniMenuEntry(String verb, String subject, Runnable onClick) {
if (customMiniMenuIndex == 10) {
return null;
}

MiniMenuEntry entry = new MiniMenuEntry(MiniMenu.size);
entry.setVerb(verb);
entry.setSubject(subject);
MiniMenu.actions[MiniMenu.size] = (short) (9990 + customMiniMenuIndex);
miniMenuCustomActions[customMiniMenuIndex++] = onClick;
MiniMenu.size++;
return entry;
}
}
12 changes: 11 additions & 1 deletion client/src/main/java/plugin/api/MiniMenuAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ public enum MiniMenuAction {
LOC_3(49),
LOC_4(46),
LOC_5(1001),
LOC_EXAMINE(1004)
LOC_EXAMINE(1004),
CUSTOM_1(9990),
CUSTOM_2(9991),
CUSTOM_3(9992),
CUSTOM_4(9993),
CUSTOM_5(9994),
CUSTOM_6(9995),
CUSTOM_7(9996),
CUSTOM_8(9997),
CUSTOM_9(9998),
CUSTOM_10(9999),
;

public final short actionCode;
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/java/plugin/api/MiniMenuEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public MiniMenuType getType() {
if (color.equals("ffff00")) return MiniMenuType.NPC;
if (color.equals("ffffff")) return MiniMenuType.PLAYER;
if (color.equals("ff9040")) return MiniMenuType.OBJ;
else return MiniMenuType.TILE;
else return MiniMenuType.CUSTOM;
}

public String getVerb() {
Expand Down
3 changes: 2 additions & 1 deletion client/src/main/java/plugin/api/MiniMenuType.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public enum MiniMenuType {
LOCATION,
TILE,
OBJ,
OBJSTACK
OBJSTACK,
CUSTOM
}
3 changes: 3 additions & 0 deletions client/src/main/java/rt4/LoginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,9 @@ public static void method1841() {
} else {
SoftwareRaster.method2503();
}
if (!Cs1ScriptRunner.aBoolean108) {
PluginRepository.OnMiniMenuCreate();
}
MiniMenu.sort();
if (Cs1ScriptRunner.aBoolean108) {
if (InterfaceList.aBoolean298) {
Expand Down
14 changes: 12 additions & 2 deletions client/src/main/java/rt4/MiniMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.openrs2.deob.annotation.OriginalMember;
import org.openrs2.deob.annotation.Pc;
import plugin.PluginRepository;
import plugin.api.API;
import plugin.api.MiniMenuEntry;

public class MiniMenu {
Expand Down Expand Up @@ -388,7 +389,7 @@ public static void sort() {
while (!sorted) {
sorted = true;
for (@Pc(13) int i = 0; i < size - 1; i++) {
if (actions[i] < 1000 && actions[i + 1] > 1000) {
if ((actions[i] < 1000 && actions[i + 1] > 1000) || (actions[i] > 7000 && actions[i + 1] > actions[i])) {
@Pc(41) JagString local41 = opBases[i];
sorted = false;
opBases[i] = opBases[i + 1];
Expand Down Expand Up @@ -433,7 +434,12 @@ public static void remove(@OriginalArg(1) int i) {

@OriginalMember(owner = "client!wa", name = "a", descriptor = "(IZ)Lclient!na;")
public static JagString getOp(@OriginalArg(0) int i) {
return opBases[i].length() > 0 ? JagString.concatenate(new JagString[]{ops[i], LocalizedText.MINISEPARATOR, opBases[i]}) : ops[i];
try {
return opBases[i].length() > 0 ? JagString.concatenate(new JagString[]{ops[i], LocalizedText.MINISEPARATOR, opBases[i]}) : ops[i];
} catch (NullPointerException npe) {
int ui = 4;
return JagString.EMPTY;
}
}

@OriginalMember(owner = "client!i", name = "p", descriptor = "(II)V")
Expand Down Expand Up @@ -1130,6 +1136,10 @@ public static void doAction(@OriginalArg(1) int arg0) {
Protocol.outboundBuffer.ip2add(local36);
}
}
if (actionCode >= 7990 && actionCode <= 7999) {
int index = actionCode - 7990;
API.miniMenuCustomActions[index].run();
}
if (anInt5014 != 0) {
anInt5014 = 0;
InterfaceList.redraw(InterfaceList.getComponent(MiniMap.anInt5062));
Expand Down
144 changes: 144 additions & 0 deletions plugin-playground/src/main/kotlin/LoginTimer/plugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package LoginTimer

import plugin.Plugin
import plugin.annotations.PluginMeta
import plugin.api.API
import plugin.api.MiniMenuEntry
import rt4.Component
import rt4.JagString
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.concurrent.TimeUnit

/**
* Timer/SysTime that goes over the report abuse button
* @author Woah
*/
@PluginMeta(
author = "Woahscam, Ceikry",
description = "Displays the session time played, system time, or no time over the \"Report Abuse\" button.",
version = 1.2
)
class plugin : Plugin() {

private val COMPONENT_REPORT_ABUSE = 49217565
private val DEFAULT_TIME_MODE = 0
private val PLAY_TIME_TIME_MODE = 1
private val LOCAL_TIME_TIME_MODE = 2
private val TIME_MODE_EMPTY = 3
private val TIME_MODE_INITIALIZATION = 4

private var timeMode = TIME_MODE_INITIALIZATION
private var initTime: Long = 0
private var displayMessageCounter = 0

private var component: Component? = null

override fun Init() {
timeMode = TIME_MODE_INITIALIZATION
initTime = System.currentTimeMillis()
displayMessageCounter = 0
}

override fun Draw(timeDelta: Long) {
if (component == null)
return
when (timeMode) {
DEFAULT_TIME_MODE -> {
if (component != null) {
if (component?.id == COMPONENT_REPORT_ABUSE) {
component?.text = JagString.parse("Report Abuse")
timeMode = TIME_MODE_EMPTY
}
}
}
PLAY_TIME_TIME_MODE -> {
val passedTime = System.currentTimeMillis() - initTime
val hrs = TimeUnit.MILLISECONDS.toHours(passedTime).toInt() % 24
val min = TimeUnit.MILLISECONDS.toMinutes(passedTime).toInt() % 60
val sec = TimeUnit.MILLISECONDS.toSeconds(passedTime).toInt() % 60
val timeInHHMMSS = String.format("%02d:%02d:%02d", hrs, min, sec);
if (component != null) {
if (component?.id == COMPONENT_REPORT_ABUSE) {
component?.text = JagString.parse(timeInHHMMSS)
}
}
}
LOCAL_TIME_TIME_MODE -> {
val now = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("HH:mm:ss")
val formattedTime = now.format(formatter)
if (component != null) {
if (component?.id == COMPONENT_REPORT_ABUSE) {
component?.text = JagString.parse(formattedTime)
}
}
}
TIME_MODE_EMPTY -> { /*Nothing*/ }
TIME_MODE_INITIALIZATION -> {
val now = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("HH")
val formattedTime = now.format(formatter)
if (component != null) {
if (component?.id == COMPONENT_REPORT_ABUSE) {
val hour = formattedTime.toInt()
val welcomeString = when (hour) {
in 0..11 -> "Good morning!"
in 12..17 -> "Good afternoon!"
in 17..23 -> "Good evening!"
else -> { "Hello!" }
}
component?.text = JagString.parse(welcomeString)
if (displayMessageCounter in 0..400) {
displayMessageCounter++
}
if (displayMessageCounter > 400 && displayMessageCounter != -1) {
displayMessageCounter = -1
timeMode = PLAY_TIME_TIME_MODE
}
}
}
}
else -> {
timeMode = DEFAULT_TIME_MODE
}
}
}

// This is done in the main component draw so the users who plain in SD will have their time updated
// Without having to interact with the interface
override fun ComponentDraw(componentIndex: Int, component: Component?, screenX: Int, screenY: Int) {
if (component?.id == COMPONENT_REPORT_ABUSE)
this.component = component
}

override fun ProcessCommand(commandStr: String, args: Array<String?>?) {
if (commandStr.equals("::playedtime", ignoreCase = true)) {
timeMode = 1
} else if (commandStr.equals("::systime", ignoreCase = true)) {
timeMode = 2
} else if (commandStr.equals("::notime", ignoreCase = true)) {
timeMode = 0
}
}

override fun OnMiniMenuCreate(currentEntries: Array<out MiniMenuEntry>) {
var hasReportAbuse = false
for (entry in currentEntries) {
if (entry.verb == "Report Abuse")
hasReportAbuse = true
}

if (hasReportAbuse) {
API.InsertMiniMenuEntry("Play Time", "") {
timeMode = PLAY_TIME_TIME_MODE
}
API.InsertMiniMenuEntry("Local Time", "") {
timeMode = LOCAL_TIME_TIME_MODE
}
API.InsertMiniMenuEntry("Disable Timer", "") {
timeMode = DEFAULT_TIME_MODE
}
}
}
}

0 comments on commit 58b45eb

Please sign in to comment.