First working version of useEnchantment.
This commit is contained in:
parent
add3c34fad
commit
d9897910b9
70 changed files with 731 additions and 296 deletions
59
src/main/java/io/github/adrianvic/nemesiseye/Config.java
Normal file
59
src/main/java/io/github/adrianvic/nemesiseye/Config.java
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
package io.github.adrianvic.nemesiseye;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.policy.LocationPolicy;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
public class Config {
|
||||
private final static Config instance = new Config();
|
||||
private File file;
|
||||
private YamlConfiguration config;
|
||||
|
||||
private List<LocationPolicy> locationPolicies;
|
||||
// private List<PermissionPolicy> permissionPolicies;
|
||||
// private List<PlayerNamePolicy> playerNamePolicies;
|
||||
|
||||
private Config() {
|
||||
}
|
||||
|
||||
public void load() {
|
||||
file = new File(Nemesis.getInstance().getDataFolder(), "settings.yml");
|
||||
|
||||
if (!file.exists())
|
||||
Nemesis.getInstance().saveResource("settings.yml", false);
|
||||
|
||||
config = new YamlConfiguration();
|
||||
config.options().parseComments(true);
|
||||
|
||||
try {
|
||||
config.load(file);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
locationPolicies = LocationPolicy.parseLocationPolicy(config.getMapList("Policies.Location"));
|
||||
}
|
||||
|
||||
public void save() {
|
||||
try {
|
||||
config.save(file);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void set(String path, Object value) {
|
||||
config.set(path, value);
|
||||
save();
|
||||
}
|
||||
|
||||
public List<LocationPolicy> getLocationPolicies() {
|
||||
return locationPolicies;
|
||||
}
|
||||
|
||||
public static Config getInstance() {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package io.github.adrianvic.nemesiseye;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class DataShifter {
|
||||
public static boolean safeMatches(String expression, String against) {
|
||||
String cleanPattern = expression.trim();
|
||||
Pattern pattern = Pattern.compile(cleanPattern, Pattern.CASE_INSENSITIVE);
|
||||
return pattern.matcher(against).matches();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package io.github.adrianvic.nemesiseye;
|
||||
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
|
||||
public class EventListener implements Listener {
|
||||
@EventHandler
|
||||
public void onBlockBreak(BlockBreakEvent event) {
|
||||
event.setCancelled(!Validator.canBreak(event.getPlayer()));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInteractionEvent(PlayerInteractEvent event) {
|
||||
if (event.getItem() != null) {
|
||||
event.setCancelled(!Validator.canInteract(event.getPlayer()));
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
|
||||
if (event.getDamager() instanceof Player) {
|
||||
event.setCancelled(!Validator.canHit((HumanEntity) event.getDamager()));
|
||||
}
|
||||
}
|
||||
}
|
||||
23
src/main/java/io/github/adrianvic/nemesiseye/Nemesis.java
Normal file
23
src/main/java/io/github/adrianvic/nemesiseye/Nemesis.java
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package io.github.adrianvic.nemesiseye;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.commands.Eye;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class Nemesis extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
getServer().getPluginManager().registerEvents(new EventListener(), this);
|
||||
Config.getInstance().load();
|
||||
getCommand("eye").setExecutor(new Eye());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// Plugin shutdown logic
|
||||
}
|
||||
|
||||
public static Nemesis getInstance() {
|
||||
return getPlugin(Nemesis.class);
|
||||
}
|
||||
}
|
||||
63
src/main/java/io/github/adrianvic/nemesiseye/Validator.java
Normal file
63
src/main/java/io/github/adrianvic/nemesiseye/Validator.java
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package io.github.adrianvic.nemesiseye;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.policy.Action;
|
||||
import io.github.adrianvic.nemesiseye.policy.LocationPolicy;
|
||||
import io.github.adrianvic.nemesiseye.policy.PolicyNode;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.util.BoundingBox;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Validator {
|
||||
public static boolean canInteract(HumanEntity entity) {
|
||||
return checkAgainstEntity(entity, Action.INTERACT);
|
||||
}
|
||||
public static boolean canBreak(HumanEntity entity) {
|
||||
return checkAgainstEntity(entity, Action.BREAK);
|
||||
}
|
||||
|
||||
public static boolean canHit(HumanEntity entity) {
|
||||
return checkAgainstEntity(entity, Action.HIT);
|
||||
}
|
||||
|
||||
public static boolean checkAgainstEntity(HumanEntity entity, Action action) {
|
||||
return checkAgainstNodes(entity, getNodesForPolicies(getPoliciesForEntity(entity)), action);
|
||||
}
|
||||
|
||||
public static boolean checkAgainstNodes(HumanEntity entity, List<PolicyNode> nodes, Action action) {
|
||||
for (PolicyNode n : nodes) {
|
||||
if (!checkAgainstNode(entity, n, action)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean checkAgainstNode(HumanEntity entity, PolicyNode node, Action action) {
|
||||
boolean allowed = node.getHandler().allows(entity, node, action);
|
||||
return node.isWhitelist() != allowed;
|
||||
}
|
||||
|
||||
public static List<PolicyNode> getNodesForPolicies(List<LocationPolicy> policies) {
|
||||
List<PolicyNode> nodes = new ArrayList<>();
|
||||
for (LocationPolicy p : policies) {
|
||||
nodes.addAll(p.nodes());
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
public static List<LocationPolicy> getPoliciesForEntity(HumanEntity entity) {
|
||||
List<LocationPolicy> lps = Config.getInstance().getLocationPolicies();
|
||||
List<LocationPolicy> applyingLPS = new ArrayList<>();
|
||||
for (LocationPolicy lp : lps) {
|
||||
for (ArrayList<BoundingBox> boxes : lp.locations()) {
|
||||
for (BoundingBox box : boxes) {
|
||||
if (box.contains(entity.getLocation().toVector())) {
|
||||
applyingLPS.add(lp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return applyingLPS;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
package io.github.adrianvic.nemesiseye.commands;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.Nemesis;
|
||||
import io.github.adrianvic.nemesiseye.commands.sub.*;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Eye implements CommandExecutor, TabCompleter {
|
||||
private final Map<String, Subcommand> subs = new HashMap<>();
|
||||
|
||||
public Eye() {
|
||||
register(new Reload());
|
||||
register(new ListPolicies());
|
||||
register(new PolicyInfo());
|
||||
register(new CurrentPolicies());
|
||||
}
|
||||
|
||||
private void register(Subcommand sub) {
|
||||
subs.put(sub.name(), sub);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String @NotNull [] strings) {
|
||||
if (strings.length == 0) {
|
||||
commandSender.sendMessage("""
|
||||
%sEye of Nemesis%s version %s
|
||||
Usage: '/eye <command>'
|
||||
Use '/eye help' for a list of available commands
|
||||
""".formatted(ChatColor.RED, ChatColor.RESET, Nemesis.getInstance().getDescription().getVersion()));
|
||||
} else {
|
||||
Subcommand sub = subs.get(strings[0].toLowerCase());
|
||||
if (sub == null) {
|
||||
commandSender.sendMessage("Unknown command, try '/eye help' to list available commands.");
|
||||
return true;
|
||||
}
|
||||
return sub.execute(commandSender, Arrays.copyOfRange(strings, 1, strings.length));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String @NotNull [] strings) {
|
||||
if (strings.length == 1) {
|
||||
return new ArrayList<>(subs.keySet());
|
||||
}
|
||||
Subcommand sub = subs.get(strings[0].toLowerCase());
|
||||
if (sub != null) {
|
||||
return sub.onTabComplete(commandSender, Arrays.copyOfRange(strings, 1, strings.length));
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
package io.github.adrianvic.nemesiseye.commands.sub;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.Validator;
|
||||
import io.github.adrianvic.nemesiseye.policy.LocationPolicy;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CurrentPolicies implements Subcommand {
|
||||
@Override
|
||||
public String name() {
|
||||
return "currentpolicies";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender commandSender, String[] args) {
|
||||
List<LocationPolicy> policies = Validator.getPoliciesForEntity((HumanEntity) commandSender);
|
||||
List<String> pstrings = new ArrayList<>();
|
||||
for (LocationPolicy p : policies) {
|
||||
pstrings.add(" %s (%s nodes)".formatted(p.name(), p.nodes().size()));
|
||||
}
|
||||
if (pstrings.isEmpty()) {
|
||||
commandSender.sendMessage("No policies applying to you.");
|
||||
return true;
|
||||
}
|
||||
|
||||
commandSender.sendMessage(String.join(", ", pstrings + "."));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, String[] args) {
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
package io.github.adrianvic.nemesiseye.commands.sub;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.Config;
|
||||
import io.github.adrianvic.nemesiseye.policy.LocationPolicy;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ListPolicies implements Subcommand {
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "listpolicies";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender commandSender, String[] args) {
|
||||
List<String> rstr = new ArrayList<>();
|
||||
for (LocationPolicy p : Config.getInstance().getLocationPolicies()) {
|
||||
rstr.add(p.name());
|
||||
}
|
||||
commandSender.sendMessage(String.join(", ", rstr) + ".");
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, String[] args) {
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
package io.github.adrianvic.nemesiseye.commands.sub;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.Config;
|
||||
import io.github.adrianvic.nemesiseye.policy.LocationPolicy;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PolicyInfo implements Subcommand {
|
||||
@Override
|
||||
public String name() {
|
||||
return "policyinfo";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender commandSender, String[] strings) {
|
||||
List<LocationPolicy> policies = Config.getInstance().getLocationPolicies();
|
||||
for (LocationPolicy lp : policies) {
|
||||
if (lp.name().equals(strings[0])) {
|
||||
String locations = lp.locations().toString();
|
||||
|
||||
commandSender.sendMessage(String.format("""
|
||||
Showing info for policy "%s%s%s":
|
||||
Type: %s
|
||||
Locations: %s
|
||||
Nodes: %s
|
||||
%s
|
||||
""", ChatColor.UNDERLINE, lp.name(), ChatColor.RESET, "location", locations, lp.nodes().size(), lp.allowlist() ? "Is allowlist" : "Is blacklist"));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, String[] args) {
|
||||
List<String> rstr = new ArrayList<>();
|
||||
for (LocationPolicy p : Config.getInstance().getLocationPolicies()) {
|
||||
rstr.add(p.name());
|
||||
}
|
||||
return rstr;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package io.github.adrianvic.nemesiseye.commands.sub;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.Config;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Reload implements Subcommand {
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender commandSender, String [] strings) {
|
||||
Config.getInstance().load();
|
||||
commandSender.sendMessage("Reloading...");
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, String[] args) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "reload";
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package io.github.adrianvic.nemesiseye.commands.sub;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Subcommand {
|
||||
String name();
|
||||
@SuppressWarnings("SameReturnValue")
|
||||
boolean execute(CommandSender commandSender, String[] strings);
|
||||
List<String> onTabComplete(CommandSender sender, String[] strings);
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
public enum Action {
|
||||
INTERACT,
|
||||
BREAK,
|
||||
HIT,
|
||||
CRAFT,
|
||||
EQUIP
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.util.BoundingBox;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public record LocationPolicy(String name, List<ArrayList<BoundingBox>> locations, List<PolicyNode> nodes, boolean allowlist) {
|
||||
public static List<LocationPolicy> parseLocationPolicy(List<Map<?,?>> raw) {
|
||||
List<LocationPolicy> out = new ArrayList<>(raw.size());
|
||||
for (Map<?,?> m : raw) {
|
||||
String name = (String) m.get("name");
|
||||
boolean allowlist = Boolean.TRUE.equals(m.get("allowList"));
|
||||
|
||||
// Nodes
|
||||
Object rawNodes = m.get("nodes");
|
||||
List<Map<String, Object>> nodeList = new ArrayList<>();
|
||||
if (rawNodes instanceof List<?> list) {
|
||||
for (Object o : list) {
|
||||
if (o instanceof Map<?, ?> map)
|
||||
nodeList.add((Map<String, Object>) map);
|
||||
}
|
||||
}
|
||||
|
||||
List<PolicyNode> nodes = PolicyNode.parseNodes(nodeList, allowlist);
|
||||
|
||||
// Parsing locations
|
||||
List<ArrayList<BoundingBox>> locations = new ArrayList<>();
|
||||
Object rawGroups = m.get("locations");
|
||||
List<?> groups = rawGroups instanceof List ? (List<?>) rawGroups : List.of();
|
||||
|
||||
// Getting groups
|
||||
for (Object gObj : groups) {
|
||||
List<?> group = (List<?>) gObj;
|
||||
ArrayList<BoundingBox> boxes = new ArrayList<>(group.size());
|
||||
|
||||
// Now iterate over regions inside the group
|
||||
for (Object rObj : group) {
|
||||
Map<?,?> region = (Map<?, ?>) rObj;
|
||||
Map<?,?> c1 = (Map<?, ?>) region.get("corner1");
|
||||
Map<?,?> c2 = (Map<?, ?>) region.get("corner2");
|
||||
|
||||
double x1 = ((Number) c1.get("x")).doubleValue();
|
||||
double y1 = ((Number) c1.get("y")).doubleValue();
|
||||
double z1 = ((Number) c1.get("z")).doubleValue();
|
||||
|
||||
double x2 = ((Number) c2.get("x")).doubleValue();
|
||||
double y2 = ((Number) c2.get("y")).doubleValue();
|
||||
double z2 = ((Number) c2.get("z")).doubleValue();
|
||||
|
||||
Location loc1 = new Location(Bukkit.getWorlds().getFirst(), x1, y1, z1);
|
||||
Location loc2 = new Location(Bukkit.getWorlds().getFirst(), x2, y2, z2);
|
||||
|
||||
boxes.add(BoundingBox.of(loc1, loc2));
|
||||
}
|
||||
locations.add(boxes);
|
||||
}
|
||||
out.add(new LocationPolicy(name, locations, nodes, allowlist));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
|
||||
public interface NodeHandler {
|
||||
boolean allows(HumanEntity entity, PolicyNode node, Action action);
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.policy.handlers.attackWith;
|
||||
import io.github.adrianvic.nemesiseye.policy.handlers.useEnchantment;
|
||||
import io.github.adrianvic.nemesiseye.policy.handlers.useItem;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class NodeHandlers {
|
||||
private static final Map<String, NodeHandler> handlers = new HashMap<>();
|
||||
|
||||
static {
|
||||
handlers.put("attackWithItemInHand", new attackWith());
|
||||
handlers.put("useItem", new useItem());
|
||||
handlers.put("useEnchantment", new useEnchantment());
|
||||
}
|
||||
|
||||
public static NodeHandler get(String type) {
|
||||
return handlers.get(type);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class NodeValueParser {
|
||||
public static List<String> parseValueToStringList(List<Object> values) {
|
||||
List<String> result = new ArrayList<>();
|
||||
for (Object o : values) {
|
||||
if (o instanceof String) result.add((String) o);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Map<String, String> parseValueToStringMap(List<Object> values) {
|
||||
Map<String, String> result = new HashMap<>();
|
||||
|
||||
for (Object o : values) {
|
||||
if (o instanceof Map<?, ?> raw) {
|
||||
for (Map.Entry<?, ?> e : raw.entrySet()) {
|
||||
if (e.getKey() instanceof String k && e.getValue() instanceof String v) {
|
||||
result.put(k, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import org.bukkit.permissions.Permission;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public record PermissionPolicy(String name, ArrayList<Permission> permissions, PolicyNode nodes, boolean allowlist) {}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public record PlayerNamePolicy(String name, ArrayList<String> playerName, PolicyNode nodes, boolean allowlist) {}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public record PolicyNode(String type, List<Object> values, boolean isWhitelist) {
|
||||
public static List<PolicyNode> parseNodes(List<Map<String,Object>> raw, boolean isWhitelist) {
|
||||
List<PolicyNode> nodes = new ArrayList<>();
|
||||
|
||||
for (Map<String, Object> m : raw) {
|
||||
for (Map.Entry<String, Object> entry : m.entrySet()) {
|
||||
String type = entry.getKey();
|
||||
List<Object> values = new ArrayList<>();
|
||||
Object val = entry.getValue();
|
||||
|
||||
if (val instanceof String s) {
|
||||
values.add(s);
|
||||
} else if (val instanceof List<?> l) {
|
||||
values.addAll(l);
|
||||
} else if (val instanceof Map<?,?> map) {
|
||||
values.add(map);
|
||||
}
|
||||
|
||||
nodes.add(new PolicyNode(type, values, isWhitelist));
|
||||
}
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
public NodeHandler getHandler() {
|
||||
return NodeHandlers.get(type);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package io.github.adrianvic.nemesiseye.policy.handlers;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.DataShifter;
|
||||
import io.github.adrianvic.nemesiseye.policy.Action;
|
||||
import io.github.adrianvic.nemesiseye.policy.NodeHandler;
|
||||
import io.github.adrianvic.nemesiseye.policy.NodeValueParser;
|
||||
import io.github.adrianvic.nemesiseye.policy.PolicyNode;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
|
||||
public class attackWith implements NodeHandler {
|
||||
|
||||
@Override
|
||||
public boolean allows(HumanEntity entity, PolicyNode node, Action action) {
|
||||
if (action == Action.HIT) {
|
||||
for (String s : NodeValueParser.parseValueToStringList(node.values())) {
|
||||
if (DataShifter.safeMatches(s, entity.getInventory().getItemInMainHand().getType().toString())) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
package io.github.adrianvic.nemesiseye.policy.handlers;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.DataShifter;
|
||||
import io.github.adrianvic.nemesiseye.policy.Action;
|
||||
import io.github.adrianvic.nemesiseye.policy.NodeHandler;
|
||||
import io.github.adrianvic.nemesiseye.policy.NodeValueParser;
|
||||
import io.github.adrianvic.nemesiseye.policy.PolicyNode;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class useEnchantment implements NodeHandler {
|
||||
@Override
|
||||
public boolean allows(HumanEntity entity, PolicyNode node, Action action) {
|
||||
ItemStack item = entity.getInventory().getItemInMainHand();
|
||||
if (item.getItemMeta() == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Map<Enchantment, Integer> enchants = item.getItemMeta().getEnchants();
|
||||
|
||||
if (enchants.isEmpty()) {
|
||||
return !node.isWhitelist();
|
||||
}
|
||||
|
||||
Map<String, String> valuesmap = NodeValueParser.parseValueToStringMap(node.values());
|
||||
|
||||
for (Map.Entry<Enchantment, Integer> e : enchants.entrySet()) {
|
||||
String enchantment = e.getKey().getKey().getKey();
|
||||
String level = e.getValue().toString();
|
||||
|
||||
for (Map.Entry<String, String> entry : valuesmap.entrySet()) {
|
||||
if (DataShifter.safeMatches(entry.getKey().trim(), enchantment) && DataShifter.safeMatches(entry.getValue().trim(), level)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package io.github.adrianvic.nemesiseye.policy.handlers;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.DataShifter;
|
||||
import io.github.adrianvic.nemesiseye.policy.Action;
|
||||
import io.github.adrianvic.nemesiseye.policy.NodeHandler;
|
||||
import io.github.adrianvic.nemesiseye.policy.NodeValueParser;
|
||||
import io.github.adrianvic.nemesiseye.policy.PolicyNode;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
|
||||
public class useItem implements NodeHandler {
|
||||
|
||||
@Override
|
||||
public boolean allows(HumanEntity entity, PolicyNode node, Action action) {
|
||||
String type = entity.getInventory().getItemInMainHand().getType().toString();
|
||||
|
||||
for (String s : NodeValueParser.parseValueToStringList(node.values())) {
|
||||
if (DataShifter.safeMatches(s, type)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue