Inject ShopFactory

This commit is contained in:
P0nk
2023-03-29 20:51:31 +02:00
parent 705efb4340
commit c71ca7f4d5
10 changed files with 42 additions and 30 deletions

View File

@@ -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) {
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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());

View File

@@ -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);

View File

@@ -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 {
}
}
}
}
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);
}
}

View File

@@ -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);

View File

@@ -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<Integer, Shop> shops = new HashMap<>();
private final Map<Integer, Shop> npcShops = new HashMap<>();

View File

@@ -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));