No chat to show

This project's files are all here to explore.

Files
src
pom.xml
Code
17 1,503
src/main/java/com/example/antixray/ProximityAntiXray.java
package com.example.antixray;

import com.comphenix.protocol.ProtocolLibrary;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

/**
 * Entry point of ProximityAntiXray.
 *
 * <p>Wires together the settings, the block-state table, the chunk rewriter,
 * the proximity reveal service, the packet listeners and the command.</p>
 */
public final class ProximityAntiXray extends JavaPlugin {

    private volatile Settings settings;
    private BlockStateTable table;
    private PlayerStateManager states;
    private ChunkObfuscator obfuscator;
    private RevealService reveal;
    private boolean packetListenersRegistered;

    @Override
    public void onEnable() {
        saveDefaultConfig();

        if (!getServer().getPluginManager().isPluginEnabled("ProtocolLib")) {
            getLogger().severe("ProtocolLib is required but is not enabled. Disabling.");
            getServer().getPluginManager().disablePlugin(this);
            return;
        }

        this.settings = Settings.load(getConfig(), getLogger());

        try {
            this.table = BlockStateTable.load(getLogger());
        } catch (ReflectiveOperationException | RuntimeException ex) {
            getLogger().severe("Could not read the block state registry: " + ex);
            getLogger().severe("This server version is not supported by this build. Disabling.");
            getServer().getPluginManager().disablePlugin(this);
            return;
        }
        this.table.apply(settings);

        this.states = new PlayerStateManager();
        this.obfuscator = new ChunkObfuscator(table, getLogger());
        this.reveal = new RevealService(this);

        new PacketListeners(this).register();
        this.packetListenersRegistered = true;

        getServer().getPluginManager().registerEvents(new PlayerListener(this), this);

        PluginCommand command = getCommand("ore");
        if (command != null) {
            OreCommand handler = new OreCommand(this);
            command.setExecutor(handler);
            command.setTabCompleter(handler);
        }

        // Players already online (e.g. after a plugin manager reload) get a state object.
        for (Player player : Bukkit.getOnlinePlayers()) {
            states.getOrCreate(player.getUniqueId());
        }

        getLogger().info("Enabled. Hiding " + settings.hiddenOres().size()
                + " ore types, reveal distance " + settings.revealDistance() + ".");
    }

    @Override
    public void onDisable() {
        if (packetListenersRegistered) {
            ProtocolLibrary.getProtocolManager().removePacketListeners(this);
        }
    }

    /**
     * Re-reads config.yml, rebuilds the ore lookup table and re-checks every online player.
     */
    public void reloadSettings() {
        reloadConfig();
        Settings fresh = Settings.load(getConfig(), getLogger());
        table.apply(fresh);          // new chunk packets use the new ore list immediately
        this.settings = fresh;       // distances / messages are read through this reference
        for (Player player : Bukkit.getOnlinePlayers()) {
            reveal.requestCheck(player, player.getLocation());
        }
    }

    public Settings settings() { return settings; }
    public BlockStateTable table() { return table; }
    public PlayerStateManager states() { return states; }
    public ChunkObfuscator obfuscator() { return obfuscator; }
    public RevealService reveal() { return reveal; }
}