package net.voterank.votescript; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.regex.Pattern; /** * What your game does for each type of reward: items, donator days, titles, * teleports and your own. * *
 * VoteActions<Player> actions = new VoteActions<>();
 * actions.on("item", (p, i) -> p.getInventory().freeSlots() > 0, (p, i) -> p.getInventory().add(i.number(0, 1, 65535), i.amount));
 * actions.on("vip/gold", (p, i) -> p.addDonatorDays(i.amount));
 * actions.on("teleport/home", (p, i) -> !p.inCombat(), (p, i) -> p.moveTo(3222, 3222, 0));
 * // in VoteServer.Game
 * public boolean give(String name, VoteScript.Claim claim) {
 *     Player p = World.getPlayer(name);
 *     return p != null && actions.give(p, claim);
 * }
 * // and tell VoteScript what is possible
 * VoteServer.start(folder, game, actions);
 * 
* * A type alone ("xp") takes every value the dashboard sends; a type with a * value ("vip/gold") only that one. Rewards that nothing here can give are not * sent: they stay waiting. Names and numbers come from the dashboard, never * code: check them before you use them. Use on the game thread only. */ public final class VoteActions

{ /** Does it. Only called when the checks of the whole claim said yes. */ public interface Give

{ void give(P player, VoteScript.Item item) throws Exception; } /** Is it possible right now? False keeps the whole claim for later. Must not change anything. */ public interface Can

{ boolean can(P player, VoteScript.Item item); } private static final class Entry

{ final Can

can; final Give

give; Entry(Can

can, Give

give) { this.can = can; this.give = give; } } private static final Pattern TYPE = Pattern.compile("[a-z]{2,16}(/[A-Za-z0-9_.:/-]{1,90})?"); private static final Pattern KEY = Pattern.compile("[a-z]{2,16}(/-?[A-Za-z0-9_.:][A-Za-z0-9_.:,-]{0,79}){1,4}"); private static final Pattern CUSTOM = Pattern.compile("custom/[A-Za-z0-9][A-Za-z0-9_.:,/-]{0,79}"); private final Map> entries = new TreeMap<>(); private final Map max = new HashMap<>(); public VoteActions() { for (String days : new String[]{"vip", "group", "permission", "title", "unlock", "until"}) max.put(days, 365); max.put("boost", 1440); max.put("effect", 3600); max.put("xp", 100_000_000); } /** @param type "item", "vip", "vip/gold", "storage/30001", … */ public VoteActions

on(String type, Give

give) { return on(type, null, give); } public VoteActions

on(String type, Can

can, Give

give) { if (!TYPE.matcher(type).matches()) throw new IllegalArgumentException(type); entries.put(type, new Entry<>(can, give)); return this; } /** The most one claim can give of a type (days, minutes, seconds or amount), whatever the dashboard says. */ public VoteActions

limit(String type, int most) { max.put(type, most); return this; } /** What VoteScript is told. */ public List supported() { return new ArrayList<>(entries.keySet()); } private Entry

find(VoteScript.Item i) { if (i.amount < 1) return null; if (i.type().equals("item")) { Entry

e = entries.get("item/" + i.key); return e != null ? e : entries.get("item"); } if (!(i.type().equals("custom") ? CUSTOM : KEY).matcher(i.key).matches()) return null; for (String k = i.key; ; k = k.substring(0, k.lastIndexOf('/'))) { Entry

e = entries.get(k); if (e != null) return e; if (k.indexOf('/') < 0) return null; } } private VoteScript.Item limited(VoteScript.Item i) { Integer most = max.get(i.type()); return most == null || i.amount <= most ? i : new VoteScript.Item(i.key, i.name, most); } /** Everything in the claim is possible right now. */ public boolean can(P player, VoteScript.Claim claim) { for (VoteScript.Item i : claim.items) { Entry

e = find(i); if (e != null && e.can != null && !e.can.can(player, limited(i))) return false; } return true; } /** * Checks everything, then gives everything, teleports last. False: nothing * was given. An error in one reward is written down and does not stop, or * repeat, the others. */ public boolean give(P player, VoteScript.Claim claim) { if (!can(player, claim)) return false; for (int pass = 0; pass < 2; pass++) { for (VoteScript.Item i : claim.items) { if (i.type().equals("teleport") != (pass == 1)) continue; Entry

e = find(i); if (e == null) { System.err.println("[VoteScript] Nothing here gives " + i.amount + "x " + i.key + "; " + player + " did not get it."); continue; } VoteScript.Item l = limited(i); if (l != i) System.err.println("[VoteScript] " + i.key + " is kept at " + l.amount + " instead of " + i.amount + "."); try { e.give.give(player, l); } catch (Exception | Error x) { System.err.println("[VoteScript] " + i.amount + "x " + i.key + " failed for " + player + ". Check what they got."); x.printStackTrace(); } } } return true; } static List list(VoteActions a) { return a == null ? Collections.emptyList() : a.supported(); } }