Search in sources :

Example 6 with SpawnEntityEvent

use of com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent in project WorldGuard by EngineHub.

the class EventAbstractionListener method handleBlockRightClick.

/**
 * Handle the right click of a block while an item is held.
 *
 * @param event the original event
 * @param cause the list of cause
 * @param item the item
 * @param placed the placed block
 * @param <T> the event type
 */
private static <T extends Event & Cancellable> void handleBlockRightClick(T event, Cause cause, @Nullable ItemStack item, Block clicked, Block placed) {
    if (item != null && item.getType() == Material.TNT) {
        // Workaround for a bug that allowed TNT to trigger instantly if placed
        // next to redstone, without plugins getting the clicked place event
        // (not sure if this actually still happens) -- note Jun 2019 - happens with dispensers still, tho not players
        Events.fireToCancel(event, new UseBlockEvent(event, cause, clicked.getLocation(), Material.TNT));
        // Workaround for http://leaky.bukkit.org/issues/1034
        Events.fireToCancel(event, new PlaceBlockEvent(event, cause, placed.getLocation(), Material.TNT));
        return;
    }
    // Handle created Minecarts
    if (item != null && Materials.isMinecart(item.getType())) {
        EntityType entityType = Materials.getRelatedEntity(item.getType());
        if (entityType == null) {
            entityType = EntityType.MINECART;
        }
        Events.fireToCancel(event, new SpawnEntityEvent(event, cause, clicked.getLocation().add(0.5, 0, 0.5), entityType));
        return;
    }
    // Handle created boats
    if (item != null && Materials.isBoat(item.getType())) {
        Events.fireToCancel(event, new SpawnEntityEvent(event, cause, placed.getLocation().add(0.5, 0, 0.5), EntityType.BOAT));
        return;
    }
    // Handle created armor stands
    if (item != null && item.getType() == Material.ARMOR_STAND) {
        Events.fireToCancel(event, new SpawnEntityEvent(event, cause, placed.getLocation().add(0.5, 0, 0.5), EntityType.ARMOR_STAND));
        return;
    }
    if (item != null && item.getType() == Material.END_CRYSTAL) {
        /*&& placed.getType() == Material.BEDROCK) {*/
        // in vanilla you can only place them on bedrock but who knows what plugins will add
        // may be overprotective as a result, but better than being underprotective
        Events.fireToCancel(event, new SpawnEntityEvent(event, cause, placed.getLocation().add(0.5, 0, 0.5), EntityType.ENDER_CRYSTAL));
        return;
    }
    // Handle created spawn eggs
    if (item != null && Materials.isSpawnEgg(item.getType())) {
        Events.fireToCancel(event, new SpawnEntityEvent(event, cause, placed.getLocation().add(0.5, 0, 0.5), Materials.getEntitySpawnEgg(item.getType())));
        return;
    }
    // handle water/lava placement
    if (item != null && (item.getType() == Material.WATER_BUCKET || item.getType() == Material.LAVA_BUCKET)) {
        Events.fireToCancel(event, new PlaceBlockEvent(event, cause, placed.getLocation(), item.getType() == Material.WATER_BUCKET ? Material.WATER : Material.LAVA));
        return;
    }
}
Also used : EntityType(org.bukkit.entity.EntityType) UseBlockEvent(com.sk89q.worldguard.bukkit.event.block.UseBlockEvent) PlaceBlockEvent(com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent) SpawnEntityEvent(com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent)

Aggregations

SpawnEntityEvent (com.sk89q.worldguard.bukkit.event.entity.SpawnEntityEvent)6 EventHandler (org.bukkit.event.EventHandler)4 Block (org.bukkit.block.Block)3 FallingBlock (org.bukkit.entity.FallingBlock)3 Cause (com.sk89q.worldguard.bukkit.cause.Cause)2 PlaceBlockEvent (com.sk89q.worldguard.bukkit.event.block.PlaceBlockEvent)2 Material (org.bukkit.Material)2 Entity (org.bukkit.entity.Entity)2 LivingEntity (org.bukkit.entity.LivingEntity)2 Player (org.bukkit.entity.Player)2 ItemStack (org.bukkit.inventory.ItemStack)2 BreakBlockEvent (com.sk89q.worldguard.bukkit.event.block.BreakBlockEvent)1 UseBlockEvent (com.sk89q.worldguard.bukkit.event.block.UseBlockEvent)1 UseItemEvent (com.sk89q.worldguard.bukkit.event.inventory.UseItemEvent)1 World (org.bukkit.World)1 AreaEffectCloud (org.bukkit.entity.AreaEffectCloud)1 EntityType (org.bukkit.entity.EntityType)1 Item (org.bukkit.entity.Item)1 ThrownPotion (org.bukkit.entity.ThrownPotion)1 ProjectileSource (org.bukkit.projectiles.ProjectileSource)1