Added basis for registering/checking WorldGuard flags

This commit is contained in:
Auxilor 2020-12-17 16:43:56 +00:00
parent da78944387
commit 20c39bc84f
6 changed files with 124 additions and 4 deletions

View File

@ -27,7 +27,14 @@ import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
@SuppressWarnings({"unchecked", "deprecation"})
@ -78,6 +85,8 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
Bukkit.getPluginManager().addPermission(permission);
}
//WorldguardManager.registerFlag(this.getPermissionName() + "-enabled", true);
if(type.getRequiredToExtend() != null && !type.getRequiredToExtend().isInstance(this)) {
Logger.error("Enchantment " + key + " has type " + this.getType().getName() + " but doesn't extend " + type.getRequiredToExtend().getName());
return;

View File

@ -10,12 +10,20 @@ import com.willfp.ecoenchants.nms.TridentStack;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.*;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Trident;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.event.entity.*;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;

View File

@ -0,0 +1,59 @@
package com.willfp.ecoenchants.integrations.worldguard;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import java.util.HashSet;
import java.util.Set;
public class WorldguardManager {
private static final Set<WorldguardWrapper> worldguardWrappers = new HashSet<>();
/**
* Register a new WorldGuard integration
*
* @param worldguard The integration to register
*/
public static void register(WorldguardWrapper worldguard) {
worldguardWrappers.add(worldguard);
}
/**
* Register a new StateFlag with worldguard
*
* @param flagName The name of the flag
* @param defaultValue The default value for the flag to have
*/
public static void registerFlag(String flagName, boolean defaultValue) {
worldguardWrappers.forEach(worldguardWrapper -> worldguardWrapper.registerFlag(flagName, defaultValue));
}
/**
* Get if an enchant is enabled at a player's location
*
* @param enchant The enchantment to check
* @param player The player to query
* @return If the enchantment is enabled at a player's location
*/
public static boolean enabledForPlayer(EcoEnchant enchant, LivingEntity player) {
if (!(player instanceof Player)) return true;
if(worldguardWrappers.isEmpty()) return true;
return worldguardWrappers.stream().anyMatch(worldguardWrapper -> worldguardWrapper.enabledForPlayer(enchant, (Player) player, player.getLocation()));
}
/**
* Get if an enchant is enabled at a specific location
*
* @param enchant The enchantment to check
* @param player The player to query
* @param location The location to query
* @return If the enchantment is enabled at a player's location
*/
public static boolean enabledForPlayer(EcoEnchant enchant, LivingEntity player, Location location) {
if (!(player instanceof Player)) return true;
if(worldguardWrappers.isEmpty()) return true;
return worldguardWrappers.stream().anyMatch(worldguardWrapper -> worldguardWrapper.enabledForPlayer(enchant, (Player) player, location));
}
}

View File

@ -0,0 +1,10 @@
package com.willfp.ecoenchants.integrations.worldguard;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import org.bukkit.Location;
import org.bukkit.entity.Player;
public interface WorldguardWrapper {
void registerFlag(String name, boolean def);
boolean enabledForPlayer(EcoEnchant enchant, Player player, Location location);
}

View File

@ -0,0 +1,29 @@
package com.willfp.ecoenchants.integrations.worldguard.plugins;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.integrations.worldguard.WorldguardWrapper;
import org.bukkit.Location;
import org.bukkit.entity.Player;
public class WorldguardIntegrationImpl implements WorldguardWrapper {
private static final FlagRegistry REGISTRY = WorldGuard.getInstance().getFlagRegistry();
@Override
public void registerFlag(String name, boolean def) {
StateFlag flag = new StateFlag(name, def);
if(REGISTRY.get(name) == null) {
REGISTRY.register(flag);
}
}
@Override
public boolean enabledForPlayer(EcoEnchant enchant, Player player, Location location) {
if(WorldGuard.getInstance().getPlatform().getSessionManager().hasBypass(WorldGuardPlugin.inst().wrapPlayer(player), BukkitAdapter.adapt(location.getWorld()))) return true;
return WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().testState(BukkitAdapter.adapt(location), WorldGuardPlugin.inst().wrapPlayer(player), (StateFlag) REGISTRY.get(enchant.getKey().getKey() + "-enabled"));
}
}

View File

@ -36,6 +36,8 @@ import com.willfp.ecoenchants.integrations.mcmmo.McmmoManager;
import com.willfp.ecoenchants.integrations.mcmmo.plugins.McmmoIntegrationImpl;
import com.willfp.ecoenchants.integrations.placeholder.PlaceholderManager;
import com.willfp.ecoenchants.integrations.placeholder.plugins.PlaceholderIntegrationPAPI;
import com.willfp.ecoenchants.integrations.worldguard.WorldguardManager;
import com.willfp.ecoenchants.integrations.worldguard.plugins.WorldguardIntegrationImpl;
import com.willfp.ecoenchants.util.interfaces.Callable;
import com.willfp.ecoenchants.util.interfaces.EcoRunnable;
import com.willfp.ecoenchants.util.internal.updater.PlayerJoinListener;
@ -115,7 +117,10 @@ public class Loader {
final HashMap<String, Callable> integrations = new HashMap<String, Callable>() {{
// AntiGrief
put("WorldGuard", () -> AntigriefManager.register(new AntigriefWorldGuard()));
put("WorldGuard", () -> {
AntigriefManager.register(new AntigriefWorldGuard());
WorldguardManager.register(new WorldguardIntegrationImpl());
});
put("GriefPrevention", () -> AntigriefManager.register(new AntigriefGriefPrevention()));
put("FactionsUUID", () -> AntigriefManager.register(new AntigriefFactionsUUID()));
put("Towny", () -> AntigriefManager.register(new AntigriefTowny()));