Action might work?
This commit is contained in:
@@ -2,9 +2,11 @@ package mod.treestar.shopmod;
|
|||||||
|
|
||||||
import mod.treestar.shopmod.categoryprovider.JsonShopCategoryProvider;
|
import mod.treestar.shopmod.categoryprovider.JsonShopCategoryProvider;
|
||||||
import mod.treestar.shopmod.itemprovider.JsonShopItemProvider;
|
import mod.treestar.shopmod.itemprovider.JsonShopItemProvider;
|
||||||
|
import mod.treestar.shopmod.ShopOpenAction;
|
||||||
import org.gotti.wurmunlimited.modloader.interfaces.Configurable;
|
import org.gotti.wurmunlimited.modloader.interfaces.Configurable;
|
||||||
import org.gotti.wurmunlimited.modloader.interfaces.Initable;
|
import org.gotti.wurmunlimited.modloader.interfaces.Initable;
|
||||||
import org.gotti.wurmunlimited.modloader.interfaces.WurmServerMod;
|
import org.gotti.wurmunlimited.modloader.interfaces.WurmServerMod;
|
||||||
|
import org.gotti.wurmunlimited.modsupport.actions.ModActions;
|
||||||
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
@@ -33,11 +35,15 @@ public class ShopMod implements WurmServerMod, Initable, Configurable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
|
ModActions.init();
|
||||||
|
|
||||||
ShopService shopService = ShopService.getInstance();
|
ShopService shopService = ShopService.getInstance();
|
||||||
|
|
||||||
shopService.registerCategoryProvider(new JsonShopCategoryProvider(categoryJsonPath));
|
shopService.registerCategoryProvider(new JsonShopCategoryProvider(categoryJsonPath));
|
||||||
shopService.registerItemProvider(new JsonShopItemProvider(itemJsonPath));
|
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));
|
logger.log(Level.INFO, String.format("Initialized shop '%s' (token access: %s, mailbox access: %s)", shopName, enableTokenAccess, enableMailboxAccess));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
92
src/main/java/mod/treestar/shopmod/ShopOpenAction.java
Normal file
92
src/main/java/mod/treestar/shopmod/ShopOpenAction.java
Normal file
@@ -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<ActionEntry> getBehavioursFor(Creature performer, Item target) {
|
||||||
|
if (isValidTarget(target)) {
|
||||||
|
return Collections.singletonList(actionEntry);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<ActionEntry> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user