Search in sources :

Example 1 with BedExplodeEvent

use of com.palmergames.bukkit.towny.event.BedExplodeEvent in project Towny by TownyAdvanced.

the class TownyPlayerListener method onPlayerBlowsUpBedOrRespawnAnchor.

/**
 * Handles clicking on beds in the nether/respawn anchors in the overworld sending blocks to a map so we can track when explosions occur from beds.
 * Spigot API's BlockExplodeEvent#getBlock() always returns AIR for beds/anchors exploding, which is why this is necessary.
 * @param event PlayerInteractEvent
 */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerBlowsUpBedOrRespawnAnchor(PlayerInteractEvent event) {
    if (plugin.isError()) {
        event.setCancelled(true);
        return;
    }
    if (!TownyAPI.getInstance().isTownyWorld(event.getPlayer().getWorld()))
        return;
    Block block = event.getClickedBlock();
    Player player = event.getPlayer();
    if (event.hasBlock()) {
        Location blockLoc = block.getLocation();
        /*
			 * Catches respawn anchors blowing up and allows us to track their explosions.
			 */
        if (block.getType().name().equals("RESPAWN_ANCHOR")) {
            org.bukkit.block.data.type.RespawnAnchor anchor = ((org.bukkit.block.data.type.RespawnAnchor) block.getBlockData());
            if (anchor.getCharges() == 4)
                BukkitTools.getPluginManager().callEvent(new BedExplodeEvent(player, blockLoc, null, block.getType()));
            return;
        }
        /*
			 * Catches beds blowing up and allows us to track their explosions.
			 */
        if (Tag.BEDS.isTagged(block.getType()) && player.getWorld().getEnvironment().equals(Environment.NETHER)) {
            org.bukkit.block.data.type.Bed bed = ((org.bukkit.block.data.type.Bed) block.getBlockData());
            BukkitTools.getPluginManager().callEvent(new BedExplodeEvent(player, blockLoc, block.getRelative(bed.getFacing()).getLocation(), block.getType()));
            return;
        }
        /*
			 * Prevents setting the spawn point of the player using beds, 
			 * except in allowed plots (personally-owned and Inns)
			 */
        if (Tag.BEDS.isTagged(block.getType()) && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
            if (!TownySettings.getBedUse())
                return;
            boolean isOwner = false;
            boolean isInnPlot = false;
            boolean isEnemy = false;
            Translatable denialMessage = Translatable.of("msg_err_cant_use_bed");
            if (!TownyAPI.getInstance().isWilderness(blockLoc)) {
                TownBlock townblock = TownyAPI.getInstance().getTownBlock(blockLoc);
                Resident resident = TownyUniverse.getInstance().getResident(player.getUniqueId());
                Town town = townblock.getTownOrNull();
                if (resident == null || town == null)
                    return;
                isOwner = townblock.isOwner(resident);
                isInnPlot = townblock.getType() == TownBlockType.INN;
                // Prevent enemies and outlaws using the Inn plots.
                if (CombatUtil.isEnemyTownBlock(player, townblock.getWorldCoord()) || town.hasOutlaw(resident)) {
                    isEnemy = true;
                    denialMessage = Translatable.of("msg_err_no_sleep_in_enemy_inn");
                }
            }
            if (isEnemy || !isOwner && !isInnPlot) {
                // The player is not allowed to use the bed.
                // Fire a cancellable event prior to us denying bed use.
                PlayerDeniedBedUseEvent pdbue = new PlayerDeniedBedUseEvent(player, blockLoc, isEnemy, denialMessage);
                Bukkit.getPluginManager().callEvent(pdbue);
                if (!pdbue.isCancelled()) {
                    event.setCancelled(true);
                    TownyMessaging.sendErrorMsg(player, pdbue.getDenialMessage());
                }
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) Translatable(com.palmergames.bukkit.towny.object.Translatable) Town(com.palmergames.bukkit.towny.object.Town) Block(org.bukkit.block.Block) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) Resident(com.palmergames.bukkit.towny.object.Resident) TownBlock(com.palmergames.bukkit.towny.object.TownBlock) BedExplodeEvent(com.palmergames.bukkit.towny.event.BedExplodeEvent) PlayerDeniedBedUseEvent(com.palmergames.bukkit.towny.event.player.PlayerDeniedBedUseEvent) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Aggregations

BedExplodeEvent (com.palmergames.bukkit.towny.event.BedExplodeEvent)1 PlayerDeniedBedUseEvent (com.palmergames.bukkit.towny.event.player.PlayerDeniedBedUseEvent)1 Resident (com.palmergames.bukkit.towny.object.Resident)1 Town (com.palmergames.bukkit.towny.object.Town)1 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)1 Translatable (com.palmergames.bukkit.towny.object.Translatable)1 Location (org.bukkit.Location)1 Block (org.bukkit.block.Block)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1