I'm going crazy.
This commit is contained in:
parent
27c7fd16cc
commit
96005d8c86
25 changed files with 207 additions and 110 deletions
|
|
@ -7,6 +7,7 @@ import io.github.adrianvic.nemesiseye.reflection.Glimmer;
|
|||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class Config {
|
||||
|
|
@ -19,6 +20,7 @@ public class Config {
|
|||
|
||||
public void load() {
|
||||
policies = glim.loadPoliciesFromFile(glim.loadConfigFile());
|
||||
policies.sort(Comparator.comparingInt(Policy::weight).reversed());
|
||||
}
|
||||
|
||||
// TODO: Implement config saving
|
||||
|
|
|
|||
|
|
@ -4,23 +4,31 @@ import io.github.adrianvic.nemesiseye.policy.Action;
|
|||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.player.PlayerHarvestBlockEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Events {
|
||||
public static void onBlockBreak(BlockBreakEvent event) {
|
||||
event.setCancelled(!Validator.can(event.getPlayer(), Action.BREAK));
|
||||
event.setCancelled(!Validator.can(event.getPlayer(), List.of(Action.BREAK, Action.USE_ENCHANTMENT), event));
|
||||
}
|
||||
|
||||
public static void onInteractionEvent(PlayerInteractEvent event) {
|
||||
if (event.getItem() != null) {
|
||||
event.setCancelled(!Validator.can(event.getPlayer(), Action.INTERACT));
|
||||
event.setCancelled(!Validator.can(event.getPlayer(), Action.INTERACT, event));
|
||||
}
|
||||
}
|
||||
|
||||
public static void onBlockPlaceEvent(BlockPlaceEvent event) {
|
||||
event.setCancelled(!Validator.can(event.getPlayer(), Action.PLACE, event));
|
||||
}
|
||||
|
||||
public static void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
|
||||
if (event.getDamager() instanceof Player) {
|
||||
event.setCancelled(!Validator.can((HumanEntity) event.getDamager(), Action.HIT));
|
||||
event.setCancelled(!Validator.can((HumanEntity) event.getDamager(), List.of(Action.HIT, Action.USE_ENCHANTMENT), event));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,50 +1,51 @@
|
|||
package io.github.adrianvic.nemesiseye;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.policy.Action;
|
||||
import io.github.adrianvic.nemesiseye.policy.NodeHandler;
|
||||
import io.github.adrianvic.nemesiseye.policy.Policy;
|
||||
import io.github.adrianvic.nemesiseye.policy.PolicyNode;
|
||||
import io.github.adrianvic.nemesiseye.reflection.Glimmer;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.event.Event;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Validator {
|
||||
private final static Glimmer glim = Nemesis.getInstance().getGlimmer();
|
||||
|
||||
public static boolean can(HumanEntity entity, Action action) {
|
||||
return checkAgainstEntity(entity, action);
|
||||
}
|
||||
|
||||
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) {
|
||||
for (NodeHandler handler : node.getHandler()) {
|
||||
if (!handler.allows(entity, node, action)) {
|
||||
public static boolean can(HumanEntity entity, List<Action> actions, Event event) {
|
||||
for (Action action : actions) {
|
||||
System.out.println(action);
|
||||
if (!can(entity, action, event)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static List<PolicyNode> getNodesForPolicies(List<Policy> policies) {
|
||||
List<PolicyNode> nodes = new ArrayList<>();
|
||||
for (Policy p : policies) {
|
||||
nodes.addAll(p.nodes());
|
||||
public static boolean can(HumanEntity entity, Action action, Event event) {
|
||||
boolean restricted = false;
|
||||
boolean allowed = false;
|
||||
|
||||
for (Policy policy : getPoliciesForEntity(entity)) {
|
||||
|
||||
boolean matches = policy.matches(entity, action, event);
|
||||
|
||||
switch (policy.effect()) {
|
||||
case ALLOW:
|
||||
if (matches) return true;
|
||||
break;
|
||||
|
||||
case DENY:
|
||||
if (matches) return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return nodes;
|
||||
|
||||
if (restricted) {
|
||||
return allowed;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public static List<Policy> getPoliciesForEntity(HumanEntity entity) {
|
||||
List<Policy> ps = Config.getInstance().getPolicies();
|
||||
List<Policy> result = new ArrayList<>();
|
||||
|
|
|
|||
|
|
@ -22,8 +22,7 @@ public class PolicyInfo implements Subcommand {
|
|||
Showing info for policy %s%s%s:
|
||||
Type: %s
|
||||
Nodes: %s
|
||||
%s
|
||||
""", ChatColor.GREEN, policy.name(), ChatColor.WHITE, policy.getClass().getTypeName(), policy.nodes().size(), policy.nodeAllowlist() ? "Is allowlist" : "Is blacklist"));
|
||||
""", ChatColor.GREEN, policy.name(), ChatColor.WHITE, policy.getClass().getTypeName(), policy.nodes().size()));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -6,5 +6,6 @@ public enum Action {
|
|||
HIT,
|
||||
CRAFT,
|
||||
EQUIP,
|
||||
PLACE,
|
||||
USE_ENCHANTMENT
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,5 +3,4 @@ package io.github.adrianvic.nemesiseye.policy;
|
|||
public enum Effect {
|
||||
DENY,
|
||||
ALLOW,
|
||||
ALLOWONLY
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,8 @@
|
|||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.event.Event;
|
||||
|
||||
public interface NodeHandler {
|
||||
boolean check(HumanEntity entity, PolicyNode node, Action action);
|
||||
|
||||
default boolean allows(HumanEntity entity, PolicyNode node, Action action) {
|
||||
boolean isWhitelist = (node.effect() == Effect.ALLOWONLY);
|
||||
return check(entity, node, action) ? !isWhitelist : isWhitelist;
|
||||
}
|
||||
|
||||
boolean check(HumanEntity entity, PolicyNode node, Action action, Event event);
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.policy.handlers.attackWith;
|
||||
import io.github.adrianvic.nemesiseye.policy.handlers.bePlaced;
|
||||
import io.github.adrianvic.nemesiseye.policy.handlers.useEnchantment;
|
||||
import io.github.adrianvic.nemesiseye.policy.handlers.useItem;
|
||||
|
||||
|
|
@ -11,7 +11,8 @@ public class NodeHandlers {
|
|||
private static final Map<Action, NodeHandler> handlers = new HashMap<>();
|
||||
|
||||
static {
|
||||
handlers.put(Action.HIT, new attackWith());
|
||||
handlers.put(Action.HIT, new useItem());
|
||||
handlers.put(Action.PLACE, new bePlaced());
|
||||
handlers.put(Action.INTERACT, new useItem());
|
||||
handlers.put(Action.USE_ENCHANTMENT, new useEnchantment());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,14 @@
|
|||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.event.Event;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Policy {
|
||||
String name();
|
||||
List<PolicyNode> nodes();
|
||||
boolean nodeAllowlist();
|
||||
|
||||
boolean policyAllowList();
|
||||
boolean applies(HumanEntity entity);
|
||||
Effect effect();
|
||||
|
|
@ -15,4 +16,12 @@ public interface Policy {
|
|||
default void addNode(PolicyNode node) {
|
||||
nodes().add(node);
|
||||
}
|
||||
default boolean matches(HumanEntity entity, Action action, Event event) {
|
||||
for (PolicyNode node : nodes()) {
|
||||
if (node.matches(entity, action, event)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.Config;
|
||||
import io.github.adrianvic.nemesiseye.DataShifter;
|
||||
import org.apache.logging.log4j.status.StatusLogger;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.event.Event;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public record PolicyNode(List<Action> actions, List<Object> values, Effect effect) {
|
||||
public record PolicyNode(List<Action> actions, List<Object> values) {
|
||||
public static List<PolicyNode> parseNodes(List<Map<Object,Object>> raw, Effect effect) {
|
||||
List<PolicyNode> nodes = new ArrayList<>();
|
||||
|
||||
|
|
@ -17,7 +17,6 @@ public record PolicyNode(List<Action> actions, List<Object> values, Effect effec
|
|||
for (Map.Entry<Object, Object> rawNode : m.entrySet()) {
|
||||
List<Action> nodeActions = new ArrayList<>();
|
||||
List<Object> nodeValues = new ArrayList<>();
|
||||
Effect nodeEffect = effect;
|
||||
|
||||
if (rawNode.getKey() instanceof List<?> rawTypes && rawNode.getValue() instanceof Map<?,?> rawNodeValues) {
|
||||
for (Object rawType : rawTypes) {
|
||||
|
|
@ -37,16 +36,13 @@ public record PolicyNode(List<Action> actions, List<Object> values, Effect effec
|
|||
nodeValues.add(v);
|
||||
}
|
||||
}
|
||||
|
||||
if (semiParsedNodeValue.get("effect") instanceof String efc) {
|
||||
Effect type = DataShifter.enumOrDefault(Effect.class, efc, null);
|
||||
if (type != null) {
|
||||
nodeEffect = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nodes.add(new PolicyNode(nodeActions, nodeValues, nodeEffect));
|
||||
if (!nodeActions.isEmpty() && !nodeValues.isEmpty()) {
|
||||
PolicyNode newNode = new PolicyNode(nodeActions, nodeValues);
|
||||
nodes.add(newNode);
|
||||
System.out.println(newNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
return nodes;
|
||||
|
|
@ -59,4 +55,15 @@ public record PolicyNode(List<Action> actions, List<Object> values, Effect effec
|
|||
}
|
||||
return handlers;
|
||||
}
|
||||
|
||||
public boolean matches(HumanEntity entity, Action action, Event event) {
|
||||
if (!actions.contains(action)) return false;
|
||||
|
||||
for (NodeHandler handler : getHandler()) {
|
||||
if (handler.check(entity, this, action, event)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,25 +1,25 @@
|
|||
package io.github.adrianvic.nemesiseye.policy.handlers;
|
||||
|
||||
import io.github.adrianvic.nemesiseye.DataShifter;
|
||||
import io.github.adrianvic.nemesiseye.Nemesis;
|
||||
import io.github.adrianvic.nemesiseye.policy.Action;
|
||||
import io.github.adrianvic.nemesiseye.policy.NodeHandler;
|
||||
import io.github.adrianvic.nemesiseye.policy.PolicyNode;
|
||||
import io.github.adrianvic.nemesiseye.reflection.Glimmer;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
|
||||
public class attackWith implements NodeHandler {
|
||||
|
||||
private final static Glimmer glim = Nemesis.getInstance().getGlimmer();
|
||||
|
||||
public class bePlaced implements NodeHandler {
|
||||
@Override
|
||||
public boolean check(HumanEntity entity, PolicyNode node, Action action) {
|
||||
if (action == Action.HIT) {
|
||||
public boolean check(HumanEntity entity, PolicyNode node, Action action, Event event) {
|
||||
if (event instanceof BlockPlaceEvent bpe) {
|
||||
String type = bpe.getBlock().getType().toString();
|
||||
|
||||
for (String s : DataShifter.parseValueToStringList(node.values())) {
|
||||
boolean matches = DataShifter.safeMatches(s, glim.getItemInMainHandHumanEntity(entity).getType().toString());
|
||||
if (matches) return false;
|
||||
if (DataShifter.safeMatches(s, type)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -7,17 +7,18 @@ import io.github.adrianvic.nemesiseye.policy.NodeHandler;
|
|||
import io.github.adrianvic.nemesiseye.policy.PolicyNode;
|
||||
import io.github.adrianvic.nemesiseye.reflection.Glimmer;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class useEnchantment implements NodeHandler {
|
||||
private final Glimmer glim = Nemesis.getInstance().getGlimmer();
|
||||
|
||||
@Override
|
||||
public boolean check(HumanEntity entity, PolicyNode node, Action action) {
|
||||
public boolean check(HumanEntity entity, PolicyNode node, Action action, Event event) {
|
||||
ItemStack item = glim.getItemInMainHandHumanEntity(entity);
|
||||
|
||||
if (!glim.hasItemMeta(item)) return true;
|
||||
if (!glim.hasAnyEnchantment(item)) return true;
|
||||
if (!glim.hasItemMeta(item)) return false;
|
||||
if (!glim.hasAnyEnchantment(item)) return false;
|
||||
|
||||
boolean matches = glim.hasEnchantment(item,
|
||||
DataShifter.parseValueToStringMap(node.values()));
|
||||
|
|
|
|||
|
|
@ -7,19 +7,22 @@ import io.github.adrianvic.nemesiseye.policy.NodeHandler;
|
|||
import io.github.adrianvic.nemesiseye.policy.PolicyNode;
|
||||
import io.github.adrianvic.nemesiseye.reflection.Glimmer;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.event.Event;
|
||||
|
||||
public class useItem implements NodeHandler {
|
||||
|
||||
private final Glimmer glim = Nemesis.getInstance().getGlimmer();
|
||||
|
||||
@Override
|
||||
public boolean check(HumanEntity entity, PolicyNode node, Action action) {
|
||||
public boolean check(HumanEntity entity, PolicyNode node, Action action, Event event) {
|
||||
String type = glim.getItemInMainHandHumanEntity(entity).getType().toString();
|
||||
|
||||
for (String s : DataShifter.parseValueToStringList(node.values())) {
|
||||
boolean matches = DataShifter.safeMatches(s, type);
|
||||
if (matches) return false;
|
||||
if (DataShifter.safeMatches(s, type)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,6 @@ import java.util.Map;
|
|||
public class GlobalPolicyParser implements PolicyParser {
|
||||
@Override
|
||||
public Policy parse(Core corePolicy, Map<?, ?> raw) {
|
||||
return new GlobalPolicy(corePolicy.name(), corePolicy.nodes(), corePolicy.nodeAllowlist(), corePolicy.policyAllowList(), corePolicy.effect(), corePolicy.weight());
|
||||
return new GlobalPolicy(corePolicy.name(), corePolicy.nodes(), corePolicy.policyAllowList(), corePolicy.effect(), corePolicy.weight());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import io.github.adrianvic.nemesiseye.policy.policies.Core;
|
|||
import io.github.adrianvic.nemesiseye.policy.policies.LocationPolicy;
|
||||
import io.github.adrianvic.nemesiseye.reflection.Glimmer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
|
|
|||
|
|
@ -24,6 +24,6 @@ public class PermissionPolicyParser implements PolicyParser {
|
|||
}
|
||||
}
|
||||
|
||||
return new PermissionPolicy(corePolicy.name(), permissions, corePolicy.nodes(), corePolicy.nodeAllowlist(), corePolicy.policyAllowList(), corePolicy.effect(), corePolicy.weight());
|
||||
return new PermissionPolicy(corePolicy.name(), permissions, corePolicy.nodes(), corePolicy.policyAllowList(), corePolicy.effect(), corePolicy.weight());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,6 @@ public class PlayerNamePolicyParser implements PolicyParser {
|
|||
}
|
||||
}
|
||||
|
||||
return new PlayerNamePolicy(corePolicy.name(), names, corePolicy.nodes(), corePolicy.nodeAllowlist(), corePolicy.effect(), corePolicy.policyAllowList(), corePolicy.weight());
|
||||
return new PlayerNamePolicy(corePolicy.name(), names, corePolicy.nodes(), corePolicy.effect(), corePolicy.policyAllowList(), corePolicy.weight());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import org.bukkit.entity.HumanEntity;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
public record GlobalPolicy(String name, List<PolicyNode> nodes, boolean nodeAllowlist, boolean policyAllowList, Effect effect, int weight) implements Policy {
|
||||
public record GlobalPolicy(String name, List<PolicyNode> nodes, boolean policyAllowList, Effect effect, int weight) implements Policy {
|
||||
public boolean applies(HumanEntity entity) {
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,16 +4,19 @@ import io.github.adrianvic.nemesiseye.policy.Effect;
|
|||
import io.github.adrianvic.nemesiseye.policy.Policy;
|
||||
import io.github.adrianvic.nemesiseye.policy.PolicyNode;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.permissions.Permission;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public record PermissionPolicy(String name, List<String> permissions, List<PolicyNode> nodes, boolean nodeAllowlist, boolean policyAllowList, Effect effect, int weight) implements Policy {
|
||||
public record PermissionPolicy(String name, List<String> permissions, List<PolicyNode> nodes, boolean policyAllowList, Effect effect, int weight) implements Policy {
|
||||
|
||||
@Override
|
||||
public boolean applies(HumanEntity entity) {
|
||||
return true;
|
||||
for (String perm : permissions) {
|
||||
if (entity.hasPermission(perm)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import org.bukkit.entity.HumanEntity;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
public record PlayerNamePolicy(String name, List<String> playerName, List<PolicyNode> nodes, boolean nodeAllowlist, Effect effect, boolean policyAllowList, int weight) implements Policy {
|
||||
public record PlayerNamePolicy(String name, List<String> playerName, List<PolicyNode> nodes, Effect effect, boolean policyAllowList, int weight) implements Policy {
|
||||
|
||||
@Override
|
||||
public boolean applies(HumanEntity entity) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue