Skip to content

Latest commit

 

History

History
389 lines (300 loc) · 13.1 KB

File metadata and controls

389 lines (300 loc) · 13.1 KB

Code Examples

Complete working examples for common hook implementations.

Block Break Protection (Territory-Based)

public class TerritoryBlockBreakHook {
    private final TerritoryManager territories;

    public TerritoryBlockBreakHook(TerritoryManager territories) {
        this.territories = territories;
    }

    public int evaluate(UUID playerUuid, String worldName, int x, int y, int z) {
        Territory territory = territories.getTerritoryAt(worldName, x, y, z);
        if (territory == null) return 0; // Wilderness — allow

        if (territory.isMember(playerUuid)) return 0; // Member — allow
        if (territory.hasPermission(playerUuid, "build")) return 0; // Has build perm — allow

        return 1; // DENY_WITH_MESSAGE
    }

    public String fetchDenyReason(UUID playerUuid, String worldName, int x, int y, int z) {
        Territory territory = territories.getTerritoryAt(worldName, x, y, z);
        return "&#FF5555You cannot break blocks in &gold" + territory.getName() + "&#FF5555!";
    }

    public int evaluatePickup(UUID playerUuid, String worldName, int x, int y, int z) {
        // Same logic for interactive item pickup
        return evaluate(playerUuid, worldName, x, y, z);
    }

    public String fetchPickupDenyReason(UUID playerUuid, String worldName, int x, int y, int z) {
        return fetchDenyReason(playerUuid, worldName, x, y, z);
    }
}

Explosion Protection (Simple World Check)

public class WorldExplosionHook {
    private final Set<String> protectedWorlds;

    public WorldExplosionHook(Set<String> protectedWorlds) {
        this.protectedWorlds = protectedWorlds;
    }

    public int evaluateExplosion(World world, int x, int y, int z) {
        if (protectedWorlds.contains(world.getName())) {
            return 2; // DENY_SILENT
        }
        return 0; // ALLOW
    }
}

Mob Spawn Blocking (Region-Based)

public class RegionSpawnHook {
    private final RegionManager regions;

    public RegionSpawnHook(RegionManager regions) {
        this.regions = regions;
    }

    public int evaluateCreatureSpawn(String worldName, int x, int y, int z) {
        Region region = regions.getRegionAt(worldName, x, y, z);
        if (region == null) return 0; // No region — allow

        return region.hasFlag("deny-mob-spawn") ? 2 : 0;
    }
}

Portal Access Control

public class PortalAccessHook {
    private final PermissionManager perms;

    public PortalAccessHook(PermissionManager perms) {
        this.perms = perms;
    }

    public int evaluateGateway(UUID playerUuid, String worldName, int x, int y, int z) {
        if (perms.hasPermission(playerUuid, "portal.use." + worldName)) {
            return 0; // ALLOW
        }
        return 1; // DENY_WITH_MESSAGE
    }

    public String fetchGatewayDenyReason(UUID playerUuid, String worldName, int x, int y, int z) {
        return "&#FF5555You don't have permission to use portals in this world!";
    }
}

Command Blocking (Whitelist Pattern)

public class CommandWhitelistHook {
    private final Map<String, Set<String>> worldCommandWhitelist;

    public CommandWhitelistHook(Map<String, Set<String>> worldCommandWhitelist) {
        this.worldCommandWhitelist = worldCommandWhitelist;
    }

    public int evaluateCommand(Player player, String command) {
        String baseCommand = command.split(" ")[0].toLowerCase();
        if (baseCommand.startsWith("/")) baseCommand = baseCommand.substring(1);

        String worldName = getPlayerWorldName(player);
        if (worldName == null) return 0; // Unknown world — allow

        Set<String> whitelist = worldCommandWhitelist.get(worldName);
        if (whitelist == null) return 0; // No whitelist for this world — allow
        if (whitelist.contains(baseCommand)) return 0; // Whitelisted — allow

        return 1; // DENY_WITH_MESSAGE
    }

    public String fetchCommandDenyReason(Player player, String command) {
        String worldName = getPlayerWorldName(player);
        return "&#FF5555This command is not available in &gold" + worldName + "&#FF5555!";
    }
}

Keep Inventory in Safe Zones

public class SafeZoneDeathHook {
    private final ZoneManager zones;

    public SafeZoneDeathHook(ZoneManager zones) {
        this.zones = zones;
    }

    public int evaluateDeathLoot(UUID playerUuid, String worldName, int x, int y, int z) {
        Zone zone = zones.getZoneAt(worldName, x, y, z);
        if (zone != null && zone.hasFlag("keep-inventory")) {
            return 2; // Keep inventory (DENY = prevent drops)
        }
        return 0; // Drop normally
    }
}

PvP Protection (Territory-Based)

public class TerritoryPvPHook {
    private final TerritoryManager territories;

    public TerritoryPvPHook(TerritoryManager territories) {
        this.territories = territories;
    }

    public int evaluateEntityDamage(UUID attackerUuid, UUID targetUuid, String worldName,
                                     int x, int y, int z) {
        // Only restrict PvP (player vs player)
        if (targetUuid == null) return 0; // Allow hitting mobs

        Territory territory = territories.getTerritoryAt(worldName, x, y, z);
        if (territory == null) return 0; // Wilderness — allow PvP

        if (!territory.hasFlag("pvp")) {
            return 1; // PvP disabled in this territory
        }
        return 0; // ALLOW
    }

    public String fetchEntityDamageDenyReason(UUID attackerUuid, UUID targetUuid,
                                               String worldName, int x, int y, int z) {
        Territory territory = territories.getTerritoryAt(worldName, x, y, z);
        return "&#FF5555PvP is disabled in &gold" + territory.getName() + "&#FF5555!";
    }
}

Container Protection (Territory-Based)

public class TerritoryContainerHook {
    private final TerritoryManager territories;

    public TerritoryContainerHook(TerritoryManager territories) {
        this.territories = territories;
    }

    public int evaluateContainerOpen(UUID playerUuid, String worldName, int x, int y, int z) {
        Territory territory = territories.getTerritoryAt(worldName, x, y, z);
        if (territory == null) return 0; // Wilderness — allow
        if (territory.isMember(playerUuid)) return 0; // Member — allow

        return 1; // DENY_WITH_MESSAGE
    }

    public String fetchContainerOpenDenyReason(UUID playerUuid, String worldName,
                                                int x, int y, int z) {
        Territory territory = territories.getTerritoryAt(worldName, x, y, z);
        return "&#FF5555You cannot open containers in &gold" + territory.getName() + "&#FF5555!";
    }
}

Block Place Protection (Territory-Based)

public class TerritoryBlockPlaceHook {
    private final TerritoryManager territories;

    public TerritoryBlockPlaceHook(TerritoryManager territories) {
        this.territories = territories;
    }

    public int evaluateBlockPlace(UUID playerUuid, String worldName, int x, int y, int z) {
        Territory territory = territories.getTerritoryAt(worldName, x, y, z);
        if (territory == null) return 0; // Wilderness — allow
        if (territory.isMember(playerUuid)) return 0;
        if (territory.hasPermission(playerUuid, "build")) return 0;

        return 1; // DENY_WITH_MESSAGE
    }

    public String fetchBlockPlaceDenyReason(UUID playerUuid, String worldName,
                                             int x, int y, int z) {
        Territory territory = territories.getTerritoryAt(worldName, x, y, z);
        return "&#FF5555You cannot place blocks in &gold" + territory.getName() + "&#FF5555!";
    }
}

Hammer Protection (Territory-Based)

public class TerritoryHammerHook {
    private final TerritoryManager territories;

    public TerritoryHammerHook(TerritoryManager territories) {
        this.territories = territories;
    }

    public int evaluateHammer(UUID playerUuid, String worldName, int x, int y, int z) {
        Territory territory = territories.getTerritoryAt(worldName, x, y, z);
        if (territory == null) return 0; // Wilderness — allow
        if (territory.isMember(playerUuid)) return 0; // Member — allow
        if (territory.hasPermission(playerUuid, "build")) return 0;

        return 1; // DENY_WITH_MESSAGE
    }

    public String fetchHammerDenyReason(UUID playerUuid, String worldName, int x, int y, int z) {
        Territory territory = territories.getTerritoryAt(worldName, x, y, z);
        return "&#FF5555You cannot use the hammer in &gold" + territory.getName() + "&#FF5555!";
    }
}

Use Protection (Territory-Based)

public class TerritoryUseHook {
    private final TerritoryManager territories;

    public TerritoryUseHook(TerritoryManager territories) {
        this.territories = territories;
    }

    public int evaluateUse(UUID playerUuid, String worldName, int x, int y, int z) {
        Territory territory = territories.getTerritoryAt(worldName, x, y, z);
        if (territory == null) return 0; // Wilderness — allow
        if (territory.isMember(playerUuid)) return 0; // Member — allow
        if (territory.hasPermission(playerUuid, "interact")) return 0;

        return 1; // DENY_WITH_MESSAGE
    }

    public String fetchUseDenyReason(UUID playerUuid, String worldName, int x, int y, int z) {
        Territory territory = territories.getTerritoryAt(worldName, x, y, z);
        return "&#FF5555You cannot interact with blocks in &gold" + territory.getName() + "&#FF5555!";
    }
}

Seat Protection (Territory-Based)

public class TerritorySeatHook {
    private final TerritoryManager territories;

    public TerritorySeatHook(TerritoryManager territories) {
        this.territories = territories;
    }

    public int evaluateSeat(UUID playerUuid, String worldName, int x, int y, int z) {
        Territory territory = territories.getTerritoryAt(worldName, x, y, z);
        if (territory == null) return 0; // Wilderness — allow
        if (territory.isMember(playerUuid)) return 0; // Member — allow

        return 2; // DENY_SILENT (no message for seating)
    }
}

Respawn Override (Faction Home)

public class FactionRespawnHook {
    private final FactionManager factions;

    public FactionRespawnHook(FactionManager factions) {
        this.factions = factions;
    }

    public double[] evaluateRespawn(UUID playerUuid, String worldName,
                                     int deathX, int deathY, int deathZ) {
        // Check if the player died in claimed territory
        Faction faction = factions.getPlayerFaction(playerUuid);
        if (faction == null) return null; // No faction — use default respawn

        Territory territory = factions.getTerritoryAt(worldName, deathX, deathY, deathZ);
        if (territory == null) return null; // Wilderness — use default respawn

        // If dying in own or ally territory, respawn at faction home
        if (territory.getOwner().equals(faction) || territory.isAllied(faction)) {
            double[] home = faction.getHome();
            if (home != null) return home; // [x, y, z]
        }

        return null; // Use default respawn
    }
}

Full Registration Example

public class MyProtectionPlugin extends JavaPlugin {

    public MyProtectionPlugin(JavaPluginInit init) {
        super(init);
    }

    @Override
    protected void setup() {
        super.setup();

        // Check if HyperProtect-Mixin is available
        if (!"true".equals(System.getProperty("hyperprotect.bridge.active"))) {
            getLogger().warning("HyperProtect-Mixin not found! Protection hooks disabled.");
            return;
        }

        // Get bridge array
        @SuppressWarnings("unchecked")
        AtomicReferenceArray<Object> bridge = (AtomicReferenceArray<Object>)
            System.getProperties().get("hyperprotect.bridge");

        if (bridge == null) {
            getLogger().warning("HyperProtect-Mixin bridge array not initialized!");
            return;
        }

        // Register hooks at their slot indices
        TerritoryManager territories = new TerritoryManager();
        bridge.set(0, new TerritoryBlockBreakHook(territories));       // block_break
        bridge.set(1, new WorldExplosionHook(Set.of("spawn", "hub"))); // explosion
        bridge.set(8, new RegionSpawnHook(territories));               // mob_spawn
        bridge.set(10, new PortalAccessHook(getPermissionManager()));  // portal
        bridge.set(16, new TerritoryPvPHook(territories));             // entity_damage
        bridge.set(17, new TerritoryContainerHook(territories));       // container_open
        bridge.set(18, new TerritoryBlockPlaceHook(territories));      // block_place
        bridge.set(19, new TerritoryHammerHook(territories));         // hammer
        bridge.set(20, new TerritoryUseHook(territories));            // use
        bridge.set(21, new TerritorySeatHook(territories));           // seat
        bridge.set(22, new FactionRespawnHook(getFactionManager()));  // respawn

        // Mark spawn protection as ready
        bridge.set(13, Boolean.TRUE);  // spawn_ready

        getLogger().info("Protection hooks registered!");
    }
}