<commit_before>package org.gestern.gringotts.dependency;

import static org.gestern.gringotts.Language.LANG;
import static org.gestern.gringotts.Permissions.createvault_worldguard;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.gestern.gringotts.Gringotts;
import org.gestern.gringotts.accountholder.AccountHolder;
import org.gestern.gringotts.accountholder.AccountHolderProvider;
import org.gestern.gringotts.event.PlayerVaultCreationEvent;

import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.domains.DefaultDomain;
import com.sk89q.worldguard.protection.GlobalRegionManager;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;

public class WorldGuardHandler implements DependencyHandler, AccountHolderProvider {

    private final WorldGuardPlugin plugin;

    public WorldGuardHandler(WorldGuardPlugin plugin) {
        this.plugin = plugin;

        if (plugin != null) {
            Bukkit.getPluginManager().registerEvents(new WorldGuardListener(), Gringotts.G);
            Gringotts.G.registerAccountHolderProvider("worldguard", this);
        }
    }

    @Override
    public boolean enabled() {
        return plugin.isEnabled();
    }

    @Override
    public boolean exists() {
        return plugin != null;
    }


    @Override
    public WorldGuardAccountHolder getAccountHolder(String id) {
        // FIXME use something more robust than - as world-id delimiter
        // try explicit world+id first 
        String[] parts = id.split("-", 2);
        if (parts.length == 2) {
            WorldGuardAccountHolder wgah = getAccountHolder(parts[0], parts[1]);
            if (wgah != null) return wgah;
        }

        // try bare id in all worlds
        GlobalRegionManager manager = plugin.getGlobalRegionManager();
        for (World world: Bukkit.getWorlds()) {
            RegionManager worldManager = manager.get(world);
            if (worldManager.hasRegion(id)) {
                ProtectedRegion region = worldManager.getRegion(id);
                return new WorldGuardAccountHolder(world.getName(), region);
            }
        }

        return null;
    }

    /**
     * Get account holder for known world and region id.
     * @param world
     * @param id
     * @return
     */
    public WorldGuardAccountHolder getAccountHolder(String world, String id) {
        World w = Bukkit.getWorld(world);
        if (w == null) return null;
        RegionManager manager = plugin.getRegionManager(w);
        if (manager == null) return null;

        if (manager.hasRegion(id)) {
            ProtectedRegion region = manager.getRegion(id);
            return new WorldGuardAccountHolder(world, region);
        }

        return null;
    }

    public class WorldGuardListener implements Listener {

        @EventHandler
        public void vaultCreated(PlayerVaultCreationEvent event) {
            // some listener already claimed this event
            if (event.isValid()) return;

            if (event.getType().equals("region")) {
                Player player = event.getCause().getPlayer();
                if (!createvault_worldguard.allowed(player)) {
                    player.sendMessage(LANG.plugin_faction_noVaultPerm);
                    return;
                }

                String regionId = event.getCause().getLine(2);
                String[] regionComponents = regionId.split("-",1);

                WorldGuardAccountHolder owner = null;
                if (regionComponents.length == 1) {
                    // try to guess the world
                    owner = getAccountHolder(regionComponents[0]);
                } else {
                    String world = regionComponents[0];
                    String id = regionComponents[1];
                    owner = getAccountHolder(world, id);
                }


                if (owner != null && owner.region.hasMembersOrOwners()) {
                    DefaultDomain regionOwners = owner.region.getOwners();
                    if (regionOwners.contains(player.getName())) {
                        event.setOwner(owner);
                        event.setValid(true);
                    }
                }
            }<commit_msg>enable creation of worldguard vaults for others<commit_after>package org.gestern.gringotts.dependency;

import static org.gestern.gringotts.Language.LANG;
import static org.gestern.gringotts.Permissions.*;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.gestern.gringotts.Gringotts;
import org.gestern.gringotts.accountholder.AccountHolder;
import org.gestern.gringotts.accountholder.AccountHolderProvider;
import org.gestern.gringotts.event.PlayerVaultCreationEvent;

import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.domains.DefaultDomain;
import com.sk89q.worldguard.protection.GlobalRegionManager;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;

public class WorldGuardHandler implements DependencyHandler, AccountHolderProvider {

    private final WorldGuardPlugin plugin;

    public WorldGuardHandler(WorldGuardPlugin plugin) {
        this.plugin = plugin;

        if (plugin != null) {
            Bukkit.getPluginManager().registerEvents(new WorldGuardListener(), Gringotts.G);
            Gringotts.G.registerAccountHolderProvider("worldguard", this);
        }
    }

    @Override
    public boolean enabled() {
        return plugin.isEnabled();
    }

    @Override
    public boolean exists() {
        return plugin != null;
    }


    @Override
    public WorldGuardAccountHolder getAccountHolder(String id) {
        // FIXME use something more robust than - as world-id delimiter
        // try explicit world+id first 
        String[] parts = id.split("-", 2);
        if (parts.length == 2) {
            WorldGuardAccountHolder wgah = getAccountHolder(parts[0], parts[1]);
            if (wgah != null) return wgah;
        }

        // try bare id in all worlds
        GlobalRegionManager manager = plugin.getGlobalRegionManager();
        for (World world: Bukkit.getWorlds()) {
            RegionManager worldManager = manager.get(world);
            if (worldManager.hasRegion(id)) {
                ProtectedRegion region = worldManager.getRegion(id);
                return new WorldGuardAccountHolder(world.getName(), region);
            }
        }

        return null;
    }

    /**
     * Get account holder for known world and region id.
     * @param world
     * @param id
     * @return
     */
    public WorldGuardAccountHolder getAccountHolder(String world, String id) {
        World w = Bukkit.getWorld(world);
        if (w == null) return null;
        RegionManager manager = plugin.getRegionManager(w);
        if (manager == null) return null;

        if (manager.hasRegion(id)) {
            ProtectedRegion region = manager.getRegion(id);
            return new WorldGuardAccountHolder(world, region);
        }

        return null;
    }

    public class WorldGuardListener implements Listener {

        @EventHandler
        public void vaultCreated(PlayerVaultCreationEvent event) {
            // some listener already claimed this event
            if (event.isValid()) return;

            if (event.getType().equals("region")) {
                Player player = event.getCause().getPlayer();
                if (!createvault_worldguard.allowed(player)) {
                    player.sendMessage(LANG.plugin_faction_noVaultPerm);
                    return;
                }

                String regionId = event.getCause().getLine(2);
                String[] regionComponents = regionId.split("-",1);

                WorldGuardAccountHolder owner = null;
                if (regionComponents.length == 1) {
                    // try to guess the world
                    owner = getAccountHolder(regionComponents[0]);
                } else {
                    String world = regionComponents[0];
                    String id = regionComponents[1];
                    owner = getAccountHolder(world, id);
                }

                if (owner != null && (owner.region.hasMembersOrOwners() || createvault_forothers.allowed(player))) {
                    DefaultDomain regionOwners = owner.region.getOwners();
                    if (regionOwners.contains(player.getName())) {
                        event.setOwner(owner);
                        event.setValid(true);
                    }
                }
            }
{'fraction_non_alphanumeric': 0.0929389096679134, 'fraction_numerical': 0.004533605349654313, 'mean_word_length': 2.2453107760205957, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6542150', 'n_tokens_mistral': 2366, 'n_tokens_neox': 2227, 'n_words': 503}