First working version of useEnchantment.
This commit is contained in:
parent
add3c34fad
commit
d9897910b9
70 changed files with 731 additions and 296 deletions
|
|
@ -1,8 +1,6 @@
|
|||
package io.github.adrianvic.regions;
|
||||
package io.github.adrianvic.nemesiseye;
|
||||
|
||||
import io.github.adrianvic.regions.policy.LocationPolicy;
|
||||
import io.github.adrianvic.regions.policy.PermissionPolicy;
|
||||
import io.github.adrianvic.regions.policy.PlayerNamePolicy;
|
||||
import io.github.adrianvic.nemesiseye.policy.LocationPolicy;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
|
|
@ -21,10 +19,10 @@ public class Config {
|
|||
}
|
||||
|
||||
public void load() {
|
||||
file = new File(Regions.getInstance().getDataFolder(), "settings.yml");
|
||||
file = new File(Nemesis.getInstance().getDataFolder(), "settings.yml");
|
||||
|
||||
if (!file.exists())
|
||||
Regions.getInstance().saveResource("settings.yml", false);
|
||||
Nemesis.getInstance().saveResource("settings.yml", false);
|
||||
|
||||
config = new YamlConfiguration();
|
||||
config.options().parseComments(true);
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +1,30 @@
|
|||
package io.github.adrianvic.regions;
|
||||
package io.github.adrianvic.nemesiseye;
|
||||
|
||||
import org.bukkit.Location;
|
||||
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 PlaceListener implements Listener {
|
||||
Validator val = new Validator();
|
||||
|
||||
public class EventListener implements Listener {
|
||||
@EventHandler
|
||||
public void onBlockBreak(BlockBreakEvent event) {
|
||||
event.setCancelled(!val.isHumanoidAbleToHarvest(event.getPlayer()));
|
||||
event.setCancelled(!Validator.canBreak(event.getPlayer()));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInteractionEvent(PlayerInteractEvent event) {
|
||||
if (event.getItem() != null) {
|
||||
event.setCancelled(!val.isItemValid(event.getItem().getType()));
|
||||
event.setCancelled(!Validator.canInteract(event.getPlayer()));
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
|
||||
if (event.getDamager() instanceof HumanEntity player) {
|
||||
boolean canceled = !val.isHumanoidAbleToHit(player);
|
||||
event.setCancelled(canceled);
|
||||
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
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package io.github.adrianvic.regions.policy;
|
||||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
|
|
@ -13,7 +13,7 @@ public record LocationPolicy(String name, List<ArrayList<BoundingBox>> locations
|
|||
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"));
|
||||
boolean allowlist = Boolean.TRUE.equals(m.get("allowList"));
|
||||
|
||||
// Nodes
|
||||
Object rawNodes = m.get("nodes");
|
||||
|
|
@ -25,7 +25,7 @@ public record LocationPolicy(String name, List<ArrayList<BoundingBox>> locations
|
|||
}
|
||||
}
|
||||
|
||||
List<PolicyNode> nodes = PolicyNode.parseNodes(nodeList);
|
||||
List<PolicyNode> nodes = PolicyNode.parseNodes(nodeList, allowlist);
|
||||
|
||||
// Parsing locations
|
||||
List<ArrayList<BoundingBox>> locations = new ArrayList<>();
|
||||
|
|
@ -58,7 +58,7 @@ public record LocationPolicy(String name, List<ArrayList<BoundingBox>> locations
|
|||
}
|
||||
locations.add(boxes);
|
||||
}
|
||||
out.add(new LocationPolicy(name, locations, nodes, allowList));
|
||||
out.add(new LocationPolicy(name, locations, nodes, allowlist));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package io.github.adrianvic.regions.policy;
|
||||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package io.github.adrianvic.regions.policy;
|
||||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import org.bukkit.permissions.Permission;
|
||||
|
||||
|
|
@ -1,6 +1,4 @@
|
|||
package io.github.adrianvic.regions.policy;
|
||||
|
||||
import org.bukkit.permissions.Permission;
|
||||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
|
@ -1,32 +1,34 @@
|
|||
package io.github.adrianvic.regions.policy;
|
||||
package io.github.adrianvic.nemesiseye.policy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public record PolicyNode(String type, List<String> values) {
|
||||
public static List<PolicyNode> parseNodes(List<Map<String,Object>> raw) {
|
||||
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<String> values = new ArrayList<>();
|
||||
List<Object> values = new ArrayList<>();
|
||||
Object val = entry.getValue();
|
||||
|
||||
|
||||
if (val instanceof String s) {
|
||||
values.add((s));
|
||||
values.add(s);
|
||||
} else if (val instanceof List<?> l) {
|
||||
for (Object o : l) {
|
||||
if (o instanceof String s) {
|
||||
values.add(s);
|
||||
}
|
||||
}
|
||||
values.addAll(l);
|
||||
} else if (val instanceof Map<?,?> map) {
|
||||
values.add(map);
|
||||
}
|
||||
nodes.add(new PolicyNode(type, values));
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
package io.github.adrianvic.regions;
|
||||
|
||||
public class DataShifter {
|
||||
}
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
package io.github.adrianvic.regions;
|
||||
|
||||
import io.github.adrianvic.regions.commands.ListPolicies;
|
||||
import io.github.adrianvic.regions.commands.PolicyInfo;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class Regions extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
getServer().getPluginManager().registerEvents(new PlaceListener(), this);
|
||||
Config.getInstance().load();
|
||||
getCommand("mrlistpolicies").setExecutor(new ListPolicies());
|
||||
getCommand("mrpolicyinfo").setExecutor(new PolicyInfo());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// Plugin shutdown logic
|
||||
}
|
||||
|
||||
public static Regions getInstance() {
|
||||
return getPlugin(Regions.class);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
package io.github.adrianvic.regions;
|
||||
|
||||
import io.github.adrianvic.regions.policy.PolicyNode;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Validator {
|
||||
public static boolean canInteract(HumanEntity entity) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean canBreak(HumanEntity entity) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean canHit(HumanEntity entity) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static List<PolicyNode> getPoliciesFor(HumanEntity entity) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
package io.github.adrianvic.regions.commands;
|
||||
|
||||
import io.github.adrianvic.regions.Config;
|
||||
import io.github.adrianvic.regions.policy.LocationPolicy;
|
||||
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.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ListPolicies implements CommandExecutor, TabCompleter {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String @NotNull [] strings) {
|
||||
List<String> rstr = new ArrayList<>();
|
||||
for (LocationPolicy p : Config.getInstance().getLocationPolicies()) {
|
||||
rstr.add(p.name());
|
||||
}
|
||||
commandSender.sendMessage(String.join(", ", rstr) + ".");
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String @NotNull [] strings) {
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
package io.github.adrianvic.regions.commands;
|
||||
|
||||
import io.github.adrianvic.regions.Config;
|
||||
import io.github.adrianvic.regions.policy.LocationPolicy;
|
||||
import io.github.adrianvic.regions.policy.PolicyNode;
|
||||
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.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PolicyInfo implements CommandExecutor, TabCompleter {
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String @NotNull [] strings) {
|
||||
List<LocationPolicy> policies = Config.getInstance().getLocationPolicies();
|
||||
for (LocationPolicy lp : policies) {
|
||||
if (lp.name().equals(strings[0])) {
|
||||
String locations = lp.locations().toString();
|
||||
|
||||
List<PolicyNode> nodes = lp.nodes();
|
||||
List<String> nodeNames = new ArrayList<>();
|
||||
for (PolicyNode n : nodes) {
|
||||
nodeNames.add(n.type());
|
||||
}
|
||||
String nodesStr = String.join(", ", nodeNames) + ".";
|
||||
|
||||
commandSender.sendMessage(String.format("""
|
||||
Showing info for policy "%s":
|
||||
Type: %s
|
||||
Locations: %s
|
||||
Nodes: %s
|
||||
%s
|
||||
""", lp.name(), "location", locations, nodesStr, lp.allowlist() ? "Is allowlist" : "Is blacklist"));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String @NotNull [] strings) {
|
||||
List<String> rstr = new ArrayList<>();
|
||||
for (LocationPolicy p : Config.getInstance().getLocationPolicies()) {
|
||||
rstr.add(p.name());
|
||||
}
|
||||
return rstr;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
package io.github.adrianvic.regions.policy;
|
||||
|
||||
public enum Action {
|
||||
INTERACT,
|
||||
BREAK,
|
||||
HIT
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
package io.github.adrianvic.regions.policy;
|
||||
|
||||
import io.github.adrianvic.regions.policy.handlers.attackWith;
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
public static NodeHandler get(String type) {
|
||||
return handlers.get(type);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
package io.github.adrianvic.regions.policy.handlers;
|
||||
|
||||
import io.github.adrianvic.regions.policy.Action;
|
||||
import io.github.adrianvic.regions.policy.NodeHandler;
|
||||
import io.github.adrianvic.regions.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) {
|
||||
if (node.values().contains(entity.getMainHand() )) return false; // nope
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,14 +1,10 @@
|
|||
name: regions
|
||||
name: "Eye-of-Nemesis"
|
||||
version: '1.0-SNAPSHOT'
|
||||
main: io.github.adrianvic.regions.Regions
|
||||
main: io.github.adrianvic.nemesiseye.Nemesis
|
||||
api-version: '1.21'
|
||||
author: 'Adrian Victor'
|
||||
website: https://github.io/adrianvic
|
||||
description: Change what players can do based in custom criteria.
|
||||
website: "https://github.io/adrianvic/NemesisEye"
|
||||
description: "Change what players can do based in custom criteria."
|
||||
commands:
|
||||
mrlistpolicies:
|
||||
description: "Lists all loaded policies"
|
||||
usage: "/mrlistpolicies"
|
||||
mrpolicyinfo:
|
||||
description: "Show info about a policy"
|
||||
usage: "/mrpolicyinfo"
|
||||
eye:
|
||||
usage: "/eye <option> (help for all available options)"
|
||||
|
|
@ -1,33 +1,122 @@
|
|||
Policies:
|
||||
Location:
|
||||
- name: "spawn-protection"
|
||||
allowlist: false
|
||||
# NO SPACES
|
||||
- name: "Beta-1.7.3-items-only"
|
||||
# Will deny anything that's not allowed by the nodes if set to true
|
||||
allowList: true
|
||||
nodes:
|
||||
# example PolicyNode fields — adapt to your actual structure
|
||||
canHit: false
|
||||
canHarvest: false
|
||||
message: "You cannot use tools here."
|
||||
locations:
|
||||
# first group (ArrayList<BoundingBox>)
|
||||
-
|
||||
# region 1 in group 1
|
||||
- corner1: { x: 100, y: 60, z: 100 }
|
||||
corner2: { x: 110, y: 70, z: 110 }
|
||||
# region 2 in group 1
|
||||
- corner1: { x: 120, y: 58, z: 95 }
|
||||
corner2: { x: 125, y: 64, z: 100 }
|
||||
# second group (another ArrayList<BoundingBox>)
|
||||
-
|
||||
- corner1: { x: -50, y: 50, z: -50 }
|
||||
corner2: { x: -40, y: 60, z: -40 }
|
||||
|
||||
- name: "farm-area"
|
||||
allowlist: true
|
||||
nodes:
|
||||
canHit: true
|
||||
canHarvest: true
|
||||
message: "Farming allowed."
|
||||
- useItem:
|
||||
- AIR
|
||||
- STONE
|
||||
- COBBLESTONE
|
||||
- "^(OAK|SPRUCE|BIRCH)_(LOG|SAPLING|PLANKS|LEAVES)$"
|
||||
- "^(DIAMOND|GOLD|IRON|COAL|LAPIS|REDSTONE)_ORE$"
|
||||
- "^(DIAMOND|GOLD|IRON|LAPIS)_BLOCK$"
|
||||
- GRAVEL
|
||||
- BEDROCK
|
||||
- SAND
|
||||
- SPONGE
|
||||
- WET_SPONGE
|
||||
- GLASS
|
||||
- LAPIS_LAZULI
|
||||
- COBWEB
|
||||
- PISTON
|
||||
- STICKY_PISTON
|
||||
- GRASS
|
||||
- DISPENSER
|
||||
- NOTE_BLOCK
|
||||
- SANDSTONE
|
||||
- RED_BED
|
||||
- "^(POWERED|DETECTOR)_RAIL$"
|
||||
- RAIL
|
||||
- SHORT_GRASS
|
||||
- "^(WHITE|BLACK|GREEN|YELLOW|PINK|PURPLE|CYAN|BLUE|RED|LIME|BROWN|LIGHT_GRAY|GRAY)_(WOOL|DYE)$"
|
||||
- POPPY
|
||||
- DANDELION
|
||||
- "^(RED|BROWN)_MUSHROOM$"
|
||||
- "^(OAK|COBBLESTONE)_SLAB$"
|
||||
- BRICK_BLOCK
|
||||
- TNT
|
||||
- BOOKSHELF
|
||||
- OBSIDIAN
|
||||
- MOSSY_COBBLESTONE
|
||||
- TORCH
|
||||
- SPAWNER
|
||||
- REDSTONE
|
||||
- CHEST
|
||||
- CRAFTING_TABLE
|
||||
- FARMLAND
|
||||
- FURNACE
|
||||
- SIGN
|
||||
- LADDER
|
||||
- "^(COBBLESTONE|OAK)_STAIRS$"
|
||||
- LEVER
|
||||
- "^(OAK|STONE)_PRESSURE_PLATE$"
|
||||
- "^(OAK|IRON)_DOOR$"
|
||||
- BLUE_ICE
|
||||
- REDSTONE_TORCH
|
||||
- STONE_BUTTON
|
||||
- SNOW
|
||||
- SNOW_BLOCK
|
||||
- CLAY
|
||||
- SUGAR_CANE
|
||||
- JUKEBOX
|
||||
- OAK_FENCE
|
||||
- PUMPKIN
|
||||
- NETHERRACK
|
||||
- SOUL_SAND
|
||||
- GLOWSTONE
|
||||
- JACK_O_LANTERN
|
||||
- CAKE
|
||||
- REPEATER
|
||||
- OAK_TRAPDOOR
|
||||
- "^(IRON|STONE|DIAMOND|WOODEN|GOLDEN)_(SHOVEL|AXE|PICKAXE|SWORD|HOE)$"
|
||||
- "^(IRON|LEATHER|DIAMOND|GOLDEN)_(HELMET|CHESTPLATE|LEGGINGS|BOOTS)$"
|
||||
- STICK
|
||||
- BOWL
|
||||
- MUSHROOM_STEW
|
||||
- FEATHER
|
||||
- STRING
|
||||
- GUNPOWDER
|
||||
- WHEAT_SEEDS
|
||||
- WHEAT
|
||||
- FLINT
|
||||
- FLINT_AND_STEEL
|
||||
- PORKCHOP
|
||||
- "^(COOKED|RAW)_(PORKCHOP|FISH)$"
|
||||
- PAINTING
|
||||
- GOLDEN_APPLE
|
||||
- BUCKET
|
||||
- "^(LAVA|MILK|WATER)_BUCKET$"
|
||||
- MINECART
|
||||
- SADDLE
|
||||
- SNOWBALL
|
||||
- OAK_BOAT
|
||||
- LEATHER
|
||||
- "^(FURNACE|CHEST)_MINECART$"
|
||||
- EGG
|
||||
- BOOK
|
||||
- PAPER
|
||||
- BRICK
|
||||
- SLIME_BALL
|
||||
- COMPASS
|
||||
- FISHING_ROD
|
||||
- CLOCK
|
||||
- GLOWSTONE_DUST
|
||||
- INK_SAC
|
||||
- BONE_MEAL
|
||||
- SUGAR
|
||||
- COOKIE
|
||||
- MAP
|
||||
- FILLED_MAP
|
||||
- SHEARS
|
||||
- MUSIC_DISK_CAT
|
||||
- MUSIC_DISK_13
|
||||
- DIRT
|
||||
- BREAD
|
||||
- useEnchantment:
|
||||
"gibberish": 999999
|
||||
locations:
|
||||
-
|
||||
- corner1: { x: 200, y: 62, z: 200 }
|
||||
corner2: { x: 220, y: 66, z: 220 }
|
||||
- corner1: { x: 2100, y: 256, z: 1400 }
|
||||
corner2: { x: 1000, y: -64, z: 2200 }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue