Skip to content

Commit

Permalink
Action & event for retrieving NPC shop inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Jul 15, 2024
1 parent 69a0352 commit c41b655
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/games/stendhal/server/actions/CommandCenter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* $Id$ */
/***************************************************************************
* (C) Copyright 2003-2010 - Stendhal *
* (C) Copyright 2003-2024 - Stendhal *
***************************************************************************
***************************************************************************
* *
Expand Down Expand Up @@ -133,6 +133,7 @@ private static void registerActions() {
WhoAction.register();
register("info", new InfoAction());
register("markscroll", new MarkScrollAction());
register("shop_inventory", new ShopInventoryAction());
}

/**
Expand Down
61 changes: 61 additions & 0 deletions src/games/stendhal/server/actions/ShopInventoryAction.java
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();
}
}
5 changes: 5 additions & 0 deletions src/games/stendhal/server/core/engine/RPClassGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
51 changes: 51 additions & 0 deletions src/games/stendhal/server/events/ShopInventoryEvent.java
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];
}
}

0 comments on commit c41b655

Please sign in to comment.