diff --git a/src/games/stendhal/server/actions/CommandCenter.java b/src/games/stendhal/server/actions/CommandCenter.java index e5f9fe6fe04..d3fc0aa05eb 100644 --- a/src/games/stendhal/server/actions/CommandCenter.java +++ b/src/games/stendhal/server/actions/CommandCenter.java @@ -1,6 +1,6 @@ /* $Id$ */ /*************************************************************************** - * (C) Copyright 2003-2010 - Stendhal * + * (C) Copyright 2003-2024 - Stendhal * *************************************************************************** *************************************************************************** * * @@ -133,6 +133,7 @@ private static void registerActions() { WhoAction.register(); register("info", new InfoAction()); register("markscroll", new MarkScrollAction()); + register("shop_inventory", new ShopInventoryAction()); } /** diff --git a/src/games/stendhal/server/actions/ShopInventoryAction.java b/src/games/stendhal/server/actions/ShopInventoryAction.java new file mode 100644 index 00000000000..e7e74c32764 --- /dev/null +++ b/src/games/stendhal/server/actions/ShopInventoryAction.java @@ -0,0 +1,61 @@ +/*************************************************************************** + * Copyright © 2024 - Faiumoni e. V. * + *************************************************************************** + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +package games.stendhal.server.actions; + +import org.apache.log4j.Logger; + +import games.stendhal.server.entity.npc.MerchantNPC; +import games.stendhal.server.entity.npc.NPCList; +import games.stendhal.server.entity.npc.SpeakerNPC; +import games.stendhal.server.entity.npc.shop.ShopType; +import games.stendhal.server.entity.player.Player; +import games.stendhal.server.events.ShopInventoryEvent; +import marauroa.common.game.RPAction; + + +/** + * Action for requesting an NPC's shop inventory. + */ +public class ShopInventoryAction implements ActionListener { + + private static Logger logger = Logger.getLogger(ShopInventoryAction.class); + + + @Override + public void onAction(final Player player, final RPAction action) { + if (!action.has("npc")) { + logger.error("NPC name must be specified"); + return; + } + if (!action.has("type")) { + logger.error("Shop type must be specified"); + return; + } + + final String typeName = action.get("type"); + final ShopType type = ShopType.fromString(typeName); + if (type == null) { + logger.error("Unrecognized shop type \"" + typeName + "\""); + return; + } + + final String npcName = action.get("npc"); + final SpeakerNPC npc = NPCList.get().get(npcName); + if (npc == null || !(npc instanceof MerchantNPC)) { + logger.error("Unrecognized merchant NPC \"" + npcName + "\""); + return; + } + + player.addEvent(new ShopInventoryEvent((MerchantNPC) npc, type)); + player.notifyWorldAboutChanges(); + } +} diff --git a/src/games/stendhal/server/core/engine/RPClassGenerator.java b/src/games/stendhal/server/core/engine/RPClassGenerator.java index 0796b32db31..28d503d8653 100644 --- a/src/games/stendhal/server/core/engine/RPClassGenerator.java +++ b/src/games/stendhal/server/core/engine/RPClassGenerator.java @@ -83,6 +83,7 @@ import games.stendhal.server.events.PrivateTextEvent; import games.stendhal.server.events.ProgressStatusEvent; import games.stendhal.server.events.ReachedAchievementEvent; +import games.stendhal.server.events.ShopInventoryEvent; import games.stendhal.server.events.ShowItemListEvent; import games.stendhal.server.events.ShowOutfitListEvent; import games.stendhal.server.events.SoundEvent; @@ -349,6 +350,10 @@ public void createRPClassesWithoutBaking() { BestiaryEvent.generateRPClass(); } + // shops + if (!RPClass.hasRPClass("shop_inventory")) { + ShopInventoryEvent.generateRPClass(); + } if (!RPClass.hasRPClass(Events.OUTFIT_LIST)) { ShowOutfitListEvent.generateRPClass(); } diff --git a/src/games/stendhal/server/events/ShopInventoryEvent.java b/src/games/stendhal/server/events/ShopInventoryEvent.java new file mode 100644 index 00000000000..1769c65e56f --- /dev/null +++ b/src/games/stendhal/server/events/ShopInventoryEvent.java @@ -0,0 +1,51 @@ +/*************************************************************************** + * Copyright © 2024 - Faiumoni e. V. * + *************************************************************************** + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +package games.stendhal.server.events; + +import org.apache.log4j.Logger; + +import games.stendhal.server.entity.npc.MerchantNPC; +import games.stendhal.server.entity.npc.shop.ShopInventory; +import games.stendhal.server.entity.npc.shop.ShopType; +import marauroa.common.game.Definition.Type; +import marauroa.common.game.RPClass; +import marauroa.common.game.RPEvent; + + +/** + * Event that retrieves an NPC's shop inventory. + */ +public class ShopInventoryEvent extends RPEvent { + + private static Logger logger = Logger.getLogger(ShopInventoryEvent.class); + + + public static void generateRPClass() { + final RPClass rpclass = new RPClass("shop_inventory"); + rpclass.addAttribute("type", Type.STRING); + rpclass.addAttribute("contents", Type.STRING); + } + + public ShopInventoryEvent(final MerchantNPC npc, final ShopType type) { + final ShopInventory inv = npc.getInventory(type); + if (inv == null) { + logger.warn("Shop type " + type.toString() + " does not exist for NPC " + npc.getName()); + return; + } + put("type", type.toString()); + put("contents", buildInventoryList(inv)); + } + + private String buildInventoryList(final ShopInventory inv) { + return inv.toString().split("(")[1].split(")")[0]; + } +}