diff --git a/src/main/java/client/command/CommandContext.java b/src/main/java/client/command/CommandContext.java index 62253004c2..58f7c7b8a1 100644 --- a/src/main/java/client/command/CommandContext.java +++ b/src/main/java/client/command/CommandContext.java @@ -1,6 +1,7 @@ package client.command; import database.drop.DropProvider; +import server.ShopFactory; -public record CommandContext(DropProvider dropProvider) { +public record CommandContext(DropProvider dropProvider, ShopFactory shopFactory) { } diff --git a/src/main/java/client/command/commands/gm2/GmShopCommand.java b/src/main/java/client/command/commands/gm2/GmShopCommand.java index caebd8fdda..bf25f7d213 100644 --- a/src/main/java/client/command/commands/gm2/GmShopCommand.java +++ b/src/main/java/client/command/commands/gm2/GmShopCommand.java @@ -26,15 +26,16 @@ package client.command.commands.gm2; import client.Client; import client.command.Command; import client.command.CommandContext; -import server.ShopFactory; public class GmShopCommand extends Command { { setDescription("Open the GM shop."); } + private static final int GM_SHOP_ID = 1337; + @Override public void execute(Client c, String[] params, CommandContext ctx) { - ShopFactory.getInstance().getShop(1337).sendShop(c); + ctx.shopFactory().getShop(GM_SHOP_ID).sendShop(c); } } diff --git a/src/main/java/client/command/commands/gm3/ReloadShopsCommand.java b/src/main/java/client/command/commands/gm3/ReloadShopsCommand.java index a39bfd2550..5231118fcc 100644 --- a/src/main/java/client/command/commands/gm3/ReloadShopsCommand.java +++ b/src/main/java/client/command/commands/gm3/ReloadShopsCommand.java @@ -26,7 +26,6 @@ package client.command.commands.gm3; import client.Client; import client.command.Command; import client.command.CommandContext; -import server.ShopFactory; public class ReloadShopsCommand extends Command { @@ -36,6 +35,6 @@ public class ReloadShopsCommand extends Command { @Override public void execute(Client c, String[] params, CommandContext ctx) { - ShopFactory.getInstance().reloadShops(); + ctx.shopFactory().reloadShops(); } } diff --git a/src/main/java/net/ChannelDependencies.java b/src/main/java/net/ChannelDependencies.java index 46b066e90d..cd25a70014 100644 --- a/src/main/java/net/ChannelDependencies.java +++ b/src/main/java/net/ChannelDependencies.java @@ -4,13 +4,14 @@ import client.command.CommandsExecutor; import client.processor.action.MakerProcessor; import client.processor.npc.FredrickProcessor; import database.drop.DropProvider; +import server.ShopFactory; import service.NoteService; import java.util.Objects; public record ChannelDependencies( NoteService noteService, FredrickProcessor fredrickProcessor, MakerProcessor makerProcessor, - DropProvider dropProvider, CommandsExecutor commandsExecutor + DropProvider dropProvider, CommandsExecutor commandsExecutor, ShopFactory shopFactory ) { public ChannelDependencies { @@ -19,5 +20,6 @@ public record ChannelDependencies( Objects.requireNonNull(makerProcessor); Objects.requireNonNull(dropProvider); Objects.requireNonNull(commandsExecutor); + Objects.requireNonNull(shopFactory); } } diff --git a/src/main/java/net/PacketProcessor.java b/src/main/java/net/PacketProcessor.java index c6c5bcb534..96698bad6e 100644 --- a/src/main/java/net/PacketProcessor.java +++ b/src/main/java/net/PacketProcessor.java @@ -144,7 +144,7 @@ public final class PacketProcessor { registerHandler(RecvOpcode.STRANGE_DATA, LoginRequiringNoOpHandler.getInstance()); registerHandler(RecvOpcode.GENERAL_CHAT, new GeneralChatHandler(channelDeps.commandsExecutor())); registerHandler(RecvOpcode.WHISPER, new WhisperHandler()); - registerHandler(RecvOpcode.NPC_TALK, new NPCTalkHandler()); + registerHandler(RecvOpcode.NPC_TALK, new NPCTalkHandler(channelDeps.shopFactory())); registerHandler(RecvOpcode.NPC_TALK_MORE, new NPCMoreTalkHandler()); registerHandler(RecvOpcode.QUEST_ACTION, new QuestActionHandler()); registerHandler(RecvOpcode.GRENADE_EFFECT, new GrenadeEffectHandler()); @@ -160,7 +160,7 @@ public final class PacketProcessor { registerHandler(RecvOpcode.MAGIC_ATTACK, new MagicDamageHandler(channelDeps.dropProvider())); registerHandler(RecvOpcode.TAKE_DAMAGE, new TakeDamageHandler()); registerHandler(RecvOpcode.MOVE_PLAYER, new MovePlayerHandler()); - registerHandler(RecvOpcode.USE_CASH_ITEM, new UseCashItemHandler(channelDeps.noteService())); + registerHandler(RecvOpcode.USE_CASH_ITEM, new UseCashItemHandler(channelDeps.noteService(), channelDeps.shopFactory())); registerHandler(RecvOpcode.USE_ITEM, new UseItemHandler()); registerHandler(RecvOpcode.USE_RETURN_SCROLL, new UseItemHandler()); registerHandler(RecvOpcode.USE_UPGRADE_SCROLL, new ScrollHandler()); diff --git a/src/main/java/net/server/Server.java b/src/main/java/net/server/Server.java index 2feed8f403..07837600de 100644 --- a/src/main/java/net/server/Server.java +++ b/src/main/java/net/server/Server.java @@ -65,6 +65,7 @@ import org.apache.logging.log4j.LogManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import server.CashShop.CashItemFactory; +import server.ShopFactory; import server.SkillbookInformationProvider; import server.ThreadManager; import server.TimerManager; @@ -977,10 +978,11 @@ 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); + ShopFactory shopFactory = new ShopFactory(); + CommandContext commandContext = new CommandContext(dropProvider, shopFactory); CommandsExecutor commandsExecutor = new CommandsExecutor(commandContext); ChannelDependencies channelDependencies = new ChannelDependencies(noteService, fredrickProcessor, - makerProcessor, dropProvider, commandsExecutor); + makerProcessor, dropProvider, commandsExecutor, shopFactory); PacketProcessor.registerGameHandlerDependencies(channelDependencies); diff --git a/src/main/java/net/server/channel/handlers/NPCTalkHandler.java b/src/main/java/net/server/channel/handlers/NPCTalkHandler.java index 413b925642..0ead20e467 100644 --- a/src/main/java/net/server/channel/handlers/NPCTalkHandler.java +++ b/src/main/java/net/server/channel/handlers/NPCTalkHandler.java @@ -30,6 +30,8 @@ import net.packet.InPacket; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import scripting.npc.NPCScriptManager; +import server.Shop; +import server.ShopFactory; import server.life.NPC; import server.life.PlayerNPC; import server.maps.MapObject; @@ -37,6 +39,11 @@ import tools.PacketCreator; public final class NPCTalkHandler extends AbstractPacketHandler { private static final Logger log = LoggerFactory.getLogger(NPCTalkHandler.class); + private final ShopFactory shopFactory; + + public NPCTalkHandler(ShopFactory shopFactory) { + this.shopFactory = shopFactory; + } @Override public void handlePacket(InPacket p, Client c) { @@ -75,7 +82,7 @@ public final class NPCTalkHandler extends AbstractPacketHandler { } else { boolean hasNpcScript = NPCScriptManager.getInstance().start(c, npc.getId(), oid, null); if (!hasNpcScript) { - if (!npc.hasShop()) { + if (!hasShop(npc)) { log.warn("NPC {} ({}) is not coded", npc.getName(), npc.getId()); return; } else if (c.getPlayer().getShop() != null) { @@ -83,7 +90,7 @@ public final class NPCTalkHandler extends AbstractPacketHandler { return; } - npc.sendShop(c); + sendShop(npc, c); } } } @@ -97,4 +104,16 @@ public final class NPCTalkHandler extends AbstractPacketHandler { } } } -} \ No newline at end of file + + private boolean hasShop(NPC npc) { + return shopFactory.getShopForNPC(npc.getId()) != null; + } + + private void sendShop(NPC npc, Client c) { + Shop shop = shopFactory.getShopForNPC(npc.getId()); + if (shop == null) { + return; + } + shop.sendShop(c); + } +} diff --git a/src/main/java/net/server/channel/handlers/UseCashItemHandler.java b/src/main/java/net/server/channel/handlers/UseCashItemHandler.java index 1040376772..3470d30077 100644 --- a/src/main/java/net/server/channel/handlers/UseCashItemHandler.java +++ b/src/main/java/net/server/channel/handlers/UseCashItemHandler.java @@ -60,11 +60,14 @@ import static java.util.concurrent.TimeUnit.SECONDS; public final class UseCashItemHandler extends AbstractPacketHandler { private static final Logger log = LoggerFactory.getLogger(UseCashItemHandler.class); + private static final int MIU_MIU_SHOP_ID = 1338; private final NoteService noteService; + private final ShopFactory shopFactory; - public UseCashItemHandler(NoteService noteService) { + public UseCashItemHandler(NoteService noteService, ShopFactory shopFactory) { this.noteService = noteService; + this.shopFactory = shopFactory; } @Override @@ -508,7 +511,7 @@ public final class UseCashItemHandler extends AbstractPacketHandler { } } else if (itemType == 545) { // MiuMiu's travel store if (player.getShop() == null) { - Shop shop = ShopFactory.getInstance().getShop(1338); + Shop shop = shopFactory.getShop(MIU_MIU_SHOP_ID); if (shop != null) { shop.sendShop(c); remove(c, position, itemId); diff --git a/src/main/java/server/ShopFactory.java b/src/main/java/server/ShopFactory.java index ebecf59bfb..7038f761ad 100644 --- a/src/main/java/server/ShopFactory.java +++ b/src/main/java/server/ShopFactory.java @@ -28,12 +28,6 @@ import java.util.Map; * @author Matze */ public class ShopFactory { - private static final ShopFactory instance = new ShopFactory(); - - public static ShopFactory getInstance() { - return instance; - } - private final Map shops = new HashMap<>(); private final Map npcShops = new HashMap<>(); diff --git a/src/main/java/server/life/NPC.java b/src/main/java/server/life/NPC.java index 355aa97c12..25637e6062 100644 --- a/src/main/java/server/life/NPC.java +++ b/src/main/java/server/life/NPC.java @@ -22,7 +22,6 @@ package server.life; import client.Client; -import server.ShopFactory; import server.maps.MapObjectType; import tools.PacketCreator; @@ -34,14 +33,6 @@ public class NPC extends AbstractLoadedLife { this.stats = stats; } - public boolean hasShop() { - return ShopFactory.getInstance().getShopForNPC(getId()) != null; - } - - public void sendShop(Client c) { - ShopFactory.getInstance().getShopForNPC(getId()).sendShop(c); - } - @Override public void sendSpawnData(Client client) { client.sendPacket(PacketCreator.spawnNPC(this));