<commit_before>package com.github.hoqhuuep.islandcraft.realestate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;

import com.github.hoqhuuep.islandcraft.realestate.event.IslandAbandonEvent;
import com.github.hoqhuuep.islandcraft.realestate.event.IslandLoadEvent;
import com.github.hoqhuuep.islandcraft.realestate.event.IslandPurchaseEvent;
import com.github.hoqhuuep.islandcraft.realestate.event.IslandRegenerateEvent;
import com.github.hoqhuuep.islandcraft.realestate.event.IslandRenameEvent;
import com.github.hoqhuuep.islandcraft.realestate.event.IslandRepossessEvent;
import com.github.hoqhuuep.islandcraft.realestate.event.IslandTaxEvent;

public class RealEstateManager {
    private final RealEstateDatabase database;
    private final ConfigurationSection config;
    private final Map<String, IslandDeed> lastIsland;
    private final Map<String, Geometry> geometryMap;

    public RealEstateManager(final RealEstateDatabase database, final ConfigurationSection config) {
        this.database = database;
        this.config = config;
        lastIsland = new HashMap<String, IslandDeed>();
        geometryMap = new HashMap<String, Geometry>();
    }

    /**
     * To be called when a chunk is loaded. Creates WorldGuard regions if they do not exist.
     * 
     * @param x
     * @param z
     */
    public void onLoad(final Location location, final long worldSeed) {
        final World world = location.getWorld();
        if (world == null) {
            // Not ready
            return;
        }
        final Geometry geometry = getGeometry(world.getName());
        if (geometry == null) {
            // Not an IslandCraft world
            return;
        }
        for (final SerializableLocation island : geometry.getOuterIslands(location)) {
            IslandDeed deed = database.loadIsland(island);
            if (deed == null) {
                deed = new IslandDeed();
                deed.setId(new SerializableLocation(island.getWorld(), island.getX(), island.getY(), island.getZ()));
                deed.setInnerRegion(geometry.getInnerRegion(island));
                deed.setOuterRegion(geometry.getOuterRegion(island));
                deed.setOwner(null);
                deed.setTax(-1);
                if (geometry.isSpawn(island)) {
                    deed.setStatus(IslandStatus.RESERVED);
                    deed.setTitle("Spawn Island");
                } else if (geometry.isResource(island, worldSeed)) {
                    deed.setStatus(IslandStatus.RESOURCE);
                    deed.setTitle("Resource Island");
                } else {
                    deed.setStatus(IslandStatus.NEW);
                    deed.setTitle("New Island");
                }
                database.saveIsland(deed);
            }
            Bukkit.getPluginManager().callEvent(new IslandLoadEvent(deed));
        }
    }

    /**
     * To be called when a player tries to abandon the island at their current location.
     * 
     * @param player
     */
    public final void onAbandon(final Player player) {
        final Geometry geometry = getGeometry(player.getWorld().getName());
        if (geometry == null) {
            message(player, "island-abandon-world-error");
            return;
        }
        final Location location = player.getLocation();
        final SerializableLocation island = geometry.getInnerIsland(location);
        if (geometry.isOcean(island)) {
            message(player, "island-abandon-ocean-error");
            return;
        }<commit_msg>Reduced spam of IslandLoadEvents. Now called only the first time an island is loaded, until the server restarts. (Before was every time a chunk in that island was loaded)<commit_after>package com.github.hoqhuuep.islandcraft.realestate;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;

import com.github.hoqhuuep.islandcraft.realestate.event.IslandAbandonEvent;
import com.github.hoqhuuep.islandcraft.realestate.event.IslandLoadEvent;
import com.github.hoqhuuep.islandcraft.realestate.event.IslandPurchaseEvent;
import com.github.hoqhuuep.islandcraft.realestate.event.IslandRegenerateEvent;
import com.github.hoqhuuep.islandcraft.realestate.event.IslandRenameEvent;
import com.github.hoqhuuep.islandcraft.realestate.event.IslandRepossessEvent;
import com.github.hoqhuuep.islandcraft.realestate.event.IslandTaxEvent;

public class RealEstateManager {
    private final RealEstateDatabase database;
    private final ConfigurationSection config;
    private final Map<String, IslandDeed> lastIsland;
    private final Map<String, Geometry> geometryMap;
    private final Set<SerializableLocation> loadedIslands;

    public RealEstateManager(final RealEstateDatabase database, final ConfigurationSection config) {
        this.database = database;
        this.config = config;
        lastIsland = new HashMap<String, IslandDeed>();
        geometryMap = new HashMap<String, Geometry>();
        loadedIslands = new HashSet<SerializableLocation>();
    }

    /**
     * To be called when a chunk is loaded. Creates WorldGuard regions if they do not exist.
     * 
     * @param x
     * @param z
     */
    public void onLoad(final Location location, final long worldSeed) {
        final World world = location.getWorld();
        if (world == null) {
            // Not ready
            return;
        }
        final Geometry geometry = getGeometry(world.getName());
        if (geometry == null) {
            // Not an IslandCraft world
            return;
        }
        for (final SerializableLocation island : geometry.getOuterIslands(location)) {
            if (loadedIslands.contains(island)) {
                // Only load once, until server is rebooted
                continue;
            }
            IslandDeed deed = database.loadIsland(island);
            if (deed == null) {
                deed = new IslandDeed();
                deed.setId(new SerializableLocation(island.getWorld(), island.getX(), island.getY(), island.getZ()));
                deed.setInnerRegion(geometry.getInnerRegion(island));
                deed.setOuterRegion(geometry.getOuterRegion(island));
                deed.setOwner(null);
                deed.setTax(-1);
                if (geometry.isSpawn(island)) {
                    deed.setStatus(IslandStatus.RESERVED);
                    deed.setTitle("Spawn Island");
                } else if (geometry.isResource(island, worldSeed)) {
                    deed.setStatus(IslandStatus.RESOURCE);
                    deed.setTitle("Resource Island");
                } else {
                    deed.setStatus(IslandStatus.NEW);
                    deed.setTitle("New Island");
                }
                database.saveIsland(deed);
            }
            loadedIslands.add(island);
            Bukkit.getPluginManager().callEvent(new IslandLoadEvent(deed));
        }
    }

    /**
     * To be called when a player tries to abandon the island at their current location.
     * 
     * @param player
     */
    public final void onAbandon(final Player player) {
        final Geometry geometry = getGeometry(player.getWorld().getName());
        if (geometry == null) {
            message(player, "island-abandon-world-error");
            return;
        }
        final Location location = player.getLocation();
        final SerializableLocation island = geometry.getInnerIsland(location);
        if (geometry.isOcean(island)) {
            message(player, "island-abandon-ocean-error");
            return;
        }
{'fraction_non_alphanumeric': 0.09595112601820796, 'fraction_numerical': 0.00023957834211787255, 'mean_word_length': 2.5755888650963596, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '7493189', 'n_tokens_mistral': 2389, 'n_tokens_neox': 2288, 'n_words': 490}