/* * WorldGuard, a suite of tools for Minecraft * Copyright (C) sk89q * Copyright (C) WorldGuard team and contributors * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ package com.sk89q.worldguard.bukkit.listener; import static com.google.common.base.Preconditions.checkNotNull; import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.WorldGuard; import com.sk89q.worldguard.bukkit.BukkitConfigurationManager; import com.sk89q.worldguard.bukkit.BukkitPlayer; import com.sk89q.worldguard.bukkit.BukkitWorldConfiguration; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.cause.Cause; import com.sk89q.worldguard.bukkit.util.Entities; import com.sk89q.worldguard.config.WorldConfiguration; import com.sk89q.worldguard.domains.Association; import com.sk89q.worldguard.protection.association.DelayedRegionOverlapAssociation; import com.sk89q.worldguard.protection.association.Associables; import com.sk89q.worldguard.protection.association.RegionAssociable; import com.sk89q.worldguard.protection.regions.RegionQuery; import io.papermc.lib.PaperLib; import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.Listener; /** * Abstract listener to ease creation of listeners. */ class AbstractListener implements Listener { private final WorldGuardPlugin plugin; /** * Construct the listener. * * @param plugin an instance of WorldGuardPlugin */ public AbstractListener(WorldGuardPlugin plugin) { checkNotNull(plugin); this.plugin = plugin; } /** * Register events. */ public void registerEvents() { plugin.getServer().getPluginManager().registerEvents(this, plugin); } /** * Get the plugin. * * @return the plugin */ protected static WorldGuardPlugin getPlugin() { return WorldGuardPlugin.inst(); } /** * Get the global configuration. * * @return the configuration */ protected static BukkitConfigurationManager getConfig() { return getPlugin().getConfigManager(); } /** * Get the world configuration given a world. * * @param world The world to get the configuration for. * @return The configuration for {@code world} */ protected static BukkitWorldConfiguration getWorldConfig(String world) { return getConfig().get(world); } protected static BukkitWorldConfiguration getWorldConfig(org.bukkit.World world) { return getWorldConfig(world.getName()); } /** * Get the world configuration given a player. * * @param player The player to get the wold from * @return The {@link WorldConfiguration} for the player's world */ protected static BukkitWorldConfiguration getWorldConfig(LocalPlayer player) { return getWorldConfig(((BukkitPlayer) player).getPlayer().getWorld()); } /** * Return whether region support is enabled. * * @param world the world * @return true if region support is enabled */ protected static boolean isRegionSupportEnabled(org.bukkit.World world) { return getWorldConfig(world).useRegions; } protected RegionAssociable createRegionAssociable(Cause cause) { Object rootCause = cause.getRootCause(); if (!cause.isKnown()) { return Associables.constant(Association.NON_MEMBER); } else if (rootCause instanceof Player player && !Entities.isNPC(player)) { return getPlugin().wrapPlayer(player); } else if (rootCause instanceof OfflinePlayer offlinePlayer) { return getPlugin().wrapOfflinePlayer(offlinePlayer); } else if (rootCause instanceof Entity entity) { RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery(); BukkitWorldConfiguration config = getWorldConfig(entity.getWorld()); Location loc; if (PaperLib.isPaper() && config.usePaperEntityOrigin) { loc = entity.getOrigin(); // Origin world may be null, and thus a Location with a null world created, which cannot be adapted to a WorldEdit location if (loc == null || loc.getWorld() == null) { loc = entity.getLocation(); } } else { loc = entity.getLocation(); } return new DelayedRegionOverlapAssociation(query, BukkitAdapter.adapt(loc), config.useMaxPriorityAssociation); } else if (rootCause instanceof Block block) { RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery(); Location loc = block.getLocation(); return new DelayedRegionOverlapAssociation(query, BukkitAdapter.adapt(loc), getWorldConfig(loc.getWorld()).useMaxPriorityAssociation); } else { return Associables.constant(Association.NON_MEMBER); } } }