Refactor CommandsExecutor - is no longer static singleton
Preparing for change in Command#handle, which is going to take a CommandContext as an additional argument. This way we can pass in command dependencies in a safe way instead of requiring them to access static methods.
This commit is contained in:
6
src/main/java/client/command/CommandContext.java
Normal file
6
src/main/java/client/command/CommandContext.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package client.command;
|
||||
|
||||
import database.drop.DropProvider;
|
||||
|
||||
public record CommandContext(DropProvider dropProvider) {
|
||||
}
|
||||
@@ -43,16 +43,27 @@ import java.util.List;
|
||||
|
||||
public class CommandsExecutor {
|
||||
private static final Logger log = LoggerFactory.getLogger(CommandsExecutor.class);
|
||||
private static final CommandsExecutor instance = new CommandsExecutor();
|
||||
private static final char USER_HEADING = '@';
|
||||
private static final char GM_HEADING = '!';
|
||||
|
||||
private final HashMap<String, Command> registeredCommands = new HashMap<>();
|
||||
private final List<Pair<List<String>, List<String>>> commandsNameDesc = new ArrayList<>();
|
||||
private final CommandContext commandContext;
|
||||
private Pair<List<String>, List<String>> levelCommandsCursor;
|
||||
|
||||
public static CommandsExecutor getInstance() {
|
||||
return instance;
|
||||
public CommandsExecutor(CommandContext commandContext) {
|
||||
this.commandContext = commandContext;
|
||||
registerCommands();
|
||||
}
|
||||
|
||||
private void registerCommands() {
|
||||
registerLv0Commands();
|
||||
registerLv1Commands();
|
||||
registerLv2Commands();
|
||||
registerLv3Commands();
|
||||
registerLv4Commands();
|
||||
registerLv5Commands();
|
||||
registerLv6Commands();
|
||||
}
|
||||
|
||||
public static boolean isCommand(Client client, String content) {
|
||||
@@ -63,16 +74,6 @@ public class CommandsExecutor {
|
||||
return heading == USER_HEADING;
|
||||
}
|
||||
|
||||
private CommandsExecutor() {
|
||||
registerLv0Commands();
|
||||
registerLv1Commands();
|
||||
registerLv2Commands();
|
||||
registerLv3Commands();
|
||||
registerLv4Commands();
|
||||
registerLv5Commands();
|
||||
registerLv6Commands();
|
||||
}
|
||||
|
||||
public List<Pair<List<String>, List<String>>> getGmCommands() {
|
||||
return commandsNameDesc;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net;
|
||||
|
||||
import client.command.CommandsExecutor;
|
||||
import client.processor.action.MakerProcessor;
|
||||
import client.processor.npc.FredrickProcessor;
|
||||
import database.drop.DropProvider;
|
||||
@@ -9,7 +10,7 @@ import java.util.Objects;
|
||||
|
||||
public record ChannelDependencies(
|
||||
NoteService noteService, FredrickProcessor fredrickProcessor, MakerProcessor makerProcessor,
|
||||
DropProvider dropProvider
|
||||
DropProvider dropProvider, CommandsExecutor commandsExecutor
|
||||
) {
|
||||
|
||||
public ChannelDependencies {
|
||||
@@ -17,5 +18,6 @@ public record ChannelDependencies(
|
||||
Objects.requireNonNull(fredrickProcessor);
|
||||
Objects.requireNonNull(makerProcessor);
|
||||
Objects.requireNonNull(dropProvider);
|
||||
Objects.requireNonNull(commandsExecutor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ public final class PacketProcessor {
|
||||
registerHandler(RecvOpcode.WORLD_TRANSFER, new TransferWorldHandler());
|
||||
registerHandler(RecvOpcode.CHANGE_CHANNEL, new ChangeChannelHandler());
|
||||
registerHandler(RecvOpcode.STRANGE_DATA, LoginRequiringNoOpHandler.getInstance());
|
||||
registerHandler(RecvOpcode.GENERAL_CHAT, new GeneralChatHandler());
|
||||
registerHandler(RecvOpcode.GENERAL_CHAT, new GeneralChatHandler(channelDeps.commandsExecutor()));
|
||||
registerHandler(RecvOpcode.WHISPER, new WhisperHandler());
|
||||
registerHandler(RecvOpcode.NPC_TALK, new NPCTalkHandler());
|
||||
registerHandler(RecvOpcode.NPC_TALK_MORE, new NPCMoreTalkHandler());
|
||||
|
||||
@@ -25,6 +25,7 @@ import client.Character;
|
||||
import client.Client;
|
||||
import client.Family;
|
||||
import client.SkillFactory;
|
||||
import client.command.CommandContext;
|
||||
import client.command.CommandsExecutor;
|
||||
import client.inventory.Item;
|
||||
import client.inventory.ItemFactory;
|
||||
@@ -930,7 +931,6 @@ public class Server {
|
||||
log.info("Cosmic is now online after {} ms.", initDuration.toMillis());
|
||||
|
||||
OpcodeConstants.generateOpcodeNames();
|
||||
CommandsExecutor.getInstance();
|
||||
|
||||
for (Channel ch : this.getAllChannels()) {
|
||||
ch.reloadEventScriptManager();
|
||||
@@ -977,8 +977,10 @@ public class Server {
|
||||
MakerProcessor makerProcessor = new MakerProcessor(new MakerInfoProvider(new MakerDao(connection)));
|
||||
FredrickProcessor fredrickProcessor = new FredrickProcessor(noteService);
|
||||
DropProvider dropProvider = new DropProvider(new DropDao(connection));
|
||||
CommandContext commandContext = new CommandContext(dropProvider);
|
||||
CommandsExecutor commandsExecutor = new CommandsExecutor(commandContext);
|
||||
ChannelDependencies channelDependencies = new ChannelDependencies(noteService, fredrickProcessor,
|
||||
makerProcessor, dropProvider);
|
||||
makerProcessor, dropProvider, commandsExecutor);
|
||||
|
||||
PacketProcessor.registerGameHandlerDependencies(channelDependencies);
|
||||
|
||||
|
||||
@@ -34,6 +34,11 @@ import tools.PacketCreator;
|
||||
|
||||
public final class GeneralChatHandler extends AbstractPacketHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(GeneralChatHandler.class);
|
||||
private final CommandsExecutor commandsExecutor;
|
||||
|
||||
public GeneralChatHandler(CommandsExecutor commandsExecutor) {
|
||||
this.commandsExecutor = commandsExecutor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePacket(InPacket p, Client c) {
|
||||
@@ -51,7 +56,7 @@ public final class GeneralChatHandler extends AbstractPacketHandler {
|
||||
}
|
||||
char heading = s.charAt(0);
|
||||
if (CommandsExecutor.isCommand(c, s)) {
|
||||
CommandsExecutor.getInstance().handle(c, s);
|
||||
commandsExecutor.handle(c, s);
|
||||
} else if (heading != '/') {
|
||||
int show = p.readByte();
|
||||
if (chr.getMap().isMuted() && !chr.isGM()) {
|
||||
@@ -70,4 +75,4 @@ public final class GeneralChatHandler extends AbstractPacketHandler {
|
||||
chr.getAutobanManager().spam(7);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user