Fix HelpCommand not working without static CommandsExecutor

This commit is contained in:
P0nk
2023-03-31 07:57:31 +02:00
parent 699da37f06
commit 785f74ed21
7 changed files with 33 additions and 13 deletions

View File

@@ -14,9 +14,9 @@ var staff_heading = "!";
var levels = ["Common", "Donator", "JrGM", "GM", "SuperGM", "Developer", "Admin"];
var commands;
// Expectation: "ce" bound to an instance of java.client.command.CommandsExecutor
function writeHeavenMSCommands() {
const CommandsExecutor = Java.type('client.command.CommandsExecutor');
commands = CommandsExecutor.getInstance().getGmCommands(); // TODO: fix. CommandsExecutor is now injected rather than being a singleton, so this does not work anymore.
commands = ce.getGmCommands();
}
function start() {

View File

@@ -6,5 +6,9 @@ import server.shop.ShopFactory;
/**
* @author Ponk
*/
public record CommandContext(DropProvider dropProvider, ShopFactory shopFactory) {
public record CommandContext(CommandsExecutor commandsExecutor, DropProvider dropProvider, ShopFactory shopFactory) {
public CommandContext with(CommandsExecutor ce) {
return new CommandContext(ce, this.dropProvider, this.shopFactory);
}
}

View File

@@ -52,7 +52,7 @@ public class CommandsExecutor {
private Pair<List<String>, List<String>> levelCommandsCursor;
public CommandsExecutor(CommandContext commandContext) {
this.commandContext = commandContext;
this.commandContext = commandContext.with(this);
registerCommands();
}

View File

@@ -28,6 +28,8 @@ import client.command.Command;
import client.command.CommandContext;
import constants.id.NpcId;
import java.util.Map;
public class HelpCommand extends Command {
{
setDescription("Show available commands.");
@@ -35,6 +37,7 @@ public class HelpCommand extends Command {
@Override
public void execute(Client client, String[] params, CommandContext ctx) {
client.getAbstractPlayerInteraction().openNpc(NpcId.STEWARD, "commands");
Map<String, Object> bindings = Map.of("ce", ctx.commandsExecutor());
client.getAbstractPlayerInteraction().openNpc(NpcId.STEWARD, "commands", bindings);
}
}

View File

@@ -980,7 +980,7 @@ public class Server {
FredrickProcessor fredrickProcessor = new FredrickProcessor(noteService);
DropProvider dropProvider = new DropProvider(new DropDao(connection));
ShopFactory shopFactory = new ShopFactory(new ShopDao(connection));
CommandContext commandContext = new CommandContext(dropProvider, shopFactory);
CommandContext commandContext = new CommandContext(null, dropProvider, shopFactory);
CommandsExecutor commandsExecutor = new CommandsExecutor(commandContext);
ChannelDependencies channelDependencies = new ChannelDependencies(noteService, fredrickProcessor,
makerProcessor, dropProvider, commandsExecutor, shopFactory);

View File

@@ -346,13 +346,17 @@ public class AbstractPlayerInteraction {
}
public void openNpc(int npcid, String script) {
openNpc(npcid, script, Collections.emptyMap());
}
public void openNpc(int npcId, String script, Map<String, Object> additionalBindings) {
if (c.getCM() != null) {
return;
}
c.removeClickedNPC();
NPCScriptManager.getInstance().dispose(c);
NPCScriptManager.getInstance().start(c, npcid, script);
NPCScriptManager.getInstance().startWithBindings(c, npcId, script, additionalBindings);
}
public int getQuestStatus(int id) {

View File

@@ -37,6 +37,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static java.util.Collections.emptyMap;
/**
* @author Matze
*/
@@ -65,19 +67,24 @@ public class NPCScriptManager extends AbstractScriptManager {
}
public boolean start(Client c, int npc, int oid, Character chr) {
return startNpcScript(c, npc, oid, null, chr);
return startNpcScript(c, npc, oid, null, chr, emptyMap());
}
public boolean start(Client c, int npc, String fileName) {
return startNpcScript(c, npc, -1, fileName, null);
return startNpcScript(c, npc, -1, fileName, null, emptyMap());
}
public boolean startNpcScript(Client c, int npc, int oid, String fileName, Character chr) {
return startWithBindings(c, npc, oid, fileName, chr, false, "cm");
public void startWithBindings(Client c, int npc, String fileName, Map<String, Object> bindings) {
startNpcScript(c, npc, -1, fileName, null, bindings);
}
private boolean startNpcScript(Client c, int npc, int oid, String fileName, Character chr,
Map<String, Object> additionalBindings) {
return startWithBindings(c, npc, oid, fileName, chr, false, "cm", additionalBindings);
}
public boolean startItemScript(Client c, ScriptedItem scriptItem) {
return startWithBindings(c, scriptItem.getNpc(), -1, scriptItem.getScript(), null, true, "im");
return startWithBindings(c, scriptItem.getNpc(), -1, scriptItem.getScript(), null, true, "im", emptyMap());
}
public void startCpqScript(String filename, Client c, int npc, List<PartyCharacter> chrs) {
@@ -111,7 +118,8 @@ public class NPCScriptManager extends AbstractScriptManager {
}
}
private boolean startWithBindings(Client c, int npc, int oid, String fileName, Character chr, boolean itemScript, String engineName) {
private boolean startWithBindings(Client c, int npc, int oid, String fileName, Character chr, boolean itemScript,
String engineName, Map<String, Object> bindings) {
try {
final NPCConversationManager cm = new NPCConversationManager(c, npc, oid, fileName, itemScript);
if (cms.containsKey(c)) {
@@ -138,6 +146,7 @@ public class NPCScriptManager extends AbstractScriptManager {
return false;
}
engine.put(engineName, cm);
bindings.forEach(engine::put);
Invocable iv = (Invocable) engine;
scripts.put(c, iv);