From 76400a6caeda2fc317856cad91371b5f95805fae Mon Sep 17 00:00:00 2001 From: gamer147 Date: Mon, 24 Nov 2025 15:39:19 -0500 Subject: [PATCH] Action might work? --- .../java/mod/treestar/shopmod/ShopMod.java | 6 ++ .../mod/treestar/shopmod/ShopOpenAction.java | 92 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/main/java/mod/treestar/shopmod/ShopOpenAction.java diff --git a/src/main/java/mod/treestar/shopmod/ShopMod.java b/src/main/java/mod/treestar/shopmod/ShopMod.java index 65a7040..f99c773 100644 --- a/src/main/java/mod/treestar/shopmod/ShopMod.java +++ b/src/main/java/mod/treestar/shopmod/ShopMod.java @@ -2,9 +2,11 @@ package mod.treestar.shopmod; import mod.treestar.shopmod.categoryprovider.JsonShopCategoryProvider; import mod.treestar.shopmod.itemprovider.JsonShopItemProvider; +import mod.treestar.shopmod.ShopOpenAction; import org.gotti.wurmunlimited.modloader.interfaces.Configurable; import org.gotti.wurmunlimited.modloader.interfaces.Initable; import org.gotti.wurmunlimited.modloader.interfaces.WurmServerMod; +import org.gotti.wurmunlimited.modsupport.actions.ModActions; import java.util.Properties; import java.util.logging.Level; @@ -33,11 +35,15 @@ public class ShopMod implements WurmServerMod, Initable, Configurable { @Override public void init() { + ModActions.init(); + ShopService shopService = ShopService.getInstance(); shopService.registerCategoryProvider(new JsonShopCategoryProvider(categoryJsonPath)); shopService.registerItemProvider(new JsonShopItemProvider(itemJsonPath)); + new ShopOpenAction(shopService, shopName, enableTokenAccess, enableMailboxAccess); + logger.log(Level.INFO, String.format("Initialized shop '%s' (token access: %s, mailbox access: %s)", shopName, enableTokenAccess, enableMailboxAccess)); } diff --git a/src/main/java/mod/treestar/shopmod/ShopOpenAction.java b/src/main/java/mod/treestar/shopmod/ShopOpenAction.java new file mode 100644 index 0000000..d25f84f --- /dev/null +++ b/src/main/java/mod/treestar/shopmod/ShopOpenAction.java @@ -0,0 +1,92 @@ +package mod.treestar.shopmod; + +import com.wurmonline.server.NoSuchPlayerException; +import com.wurmonline.server.behaviours.Action; +import com.wurmonline.server.behaviours.ActionEntry; +import com.wurmonline.server.creatures.Creature; +import com.wurmonline.server.items.Item; +import com.wurmonline.server.items.ItemList; +import org.gotti.wurmunlimited.modsupport.actions.ActionPerformer; +import org.gotti.wurmunlimited.modsupport.actions.BehaviourProvider; +import org.gotti.wurmunlimited.modsupport.actions.ModActions; +import com.wurmonline.server.questions.ShopQuestion; + +import java.util.Collections; +import java.util.List; + +/** + * Adds an action to open the shop from supported interactables (village tokens, mailboxes). + */ +public class ShopOpenAction implements ActionPerformer, BehaviourProvider { + private final ShopService shopService; + private final String shopName; + private final boolean allowTokens; + private final boolean allowMailboxes; + private final ActionEntry actionEntry; + + public ShopOpenAction(ShopService shopService, String shopName, boolean allowTokens, boolean allowMailboxes) { + this.shopService = shopService; + this.shopName = shopName; + this.allowTokens = allowTokens; + this.allowMailboxes = allowMailboxes; + this.actionEntry = ActionEntry.createEntry( + (short) ModActions.getNextActionId(), + "Open shop", + "opening shop", + new int[] { 23 } + ); + ModActions.registerAction(this.actionEntry); + ModActions.registerActionPerformer(this); + ModActions.registerBehaviourProvider(this); + } + + @Override + public short getActionId() { + return actionEntry.getNumber(); + } + + @Override + public boolean action(Action action, Creature performer, Item target, short actionId, float counter) { + if (actionId != getActionId()) { + return false; + } + if (!isValidTarget(target)) { + return true; + } + try { + new ShopQuestion(performer.getWurmId(), shopName, shopService).sendQuestion(); + } catch (NoSuchPlayerException e) { + performer.getCommunicator().sendNormalServerMessage("Unable to open shop right now."); + } + return true; + } + + @Override + public boolean action(Action action, Creature performer, Creature target, short actionId, float counter) { + return false; // not supported + } + + @Override + public List getBehavioursFor(Creature performer, Item target) { + if (isValidTarget(target)) { + return Collections.singletonList(actionEntry); + } + return null; + } + + @Override + public List getBehavioursFor(Creature performer, Creature target) { + return null; + } + + private boolean isValidTarget(Item target) { + if (target == null) return false; + if (allowTokens && target.getTemplateId() == ItemList.villageToken) { + return true; + } + if (allowMailboxes && target.isMailBox()) { + return true; + } + return false; + } +}