-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Action & event for retrieving NPC shop inventory
- Loading branch information
1 parent
69a0352
commit c41b655
Showing
4 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/games/stendhal/server/actions/ShopInventoryAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
} | ||
} |