Search in sources :

Example 1 with Silverfish

use of org.bukkit.entity.Silverfish in project RedLib by Redempt.

the class ProtectionRegistrations method registerProtections.

public static void registerProtections() {
    ProtectionListener.protect(BlockBreakEvent.class, ProtectionType.BREAK_BLOCK, e -> e.getPlayer(), e -> e.getBlock());
    ProtectionListener.protect(BlockPlaceEvent.class, ProtectionType.PLACE_BLOCK, e -> e.getPlayer(), e -> e.getBlock());
    ProtectionListener.protect(PlayerInteractEvent.class, ProtectionType.INTERACT, e -> e.getPlayer(), e -> {
        if (e.getAction() != Action.RIGHT_CLICK_BLOCK || e.getClickedBlock() == null || (RedLib.MID_VERSION >= 13 && !e.getClickedBlock().getType().isInteractable())) {
            return null;
        }
        if (e.getClickedBlock().getState() instanceof InventoryHolder && !e.getPlayer().isSneaking()) {
            return null;
        }
        return e.getClickedBlock();
    });
    Set<String> farmlandNames = new HashSet<>();
    Collections.addAll(farmlandNames, "FARMLAND", "SOIL");
    ProtectionListener.protect(PlayerInteractEvent.class, ProtectionType.TRAMPLE, e -> e.getPlayer(), e -> {
        if (e.getAction() == Action.PHYSICAL && e.getClickedBlock() != null && farmlandNames.contains(e.getClickedBlock().getType().toString())) {
            return e.getClickedBlock();
        }
        return null;
    });
    ProtectionListener.protect(InventoryOpenEvent.class, ProtectionType.CONTAINER_ACCESS, e -> (Player) e.getPlayer(), e -> getBlock(e.getInventory()));
    ProtectionListener.protectMultiBlock(EntityExplodeEvent.class, ProtectionType.ENTITY_EXPLOSION, e -> null, (e, b) -> e.blockList().remove(b), e -> e.blockList());
    ProtectionListener.protectMultiBlock(BlockExplodeEvent.class, ProtectionType.BLOCK_EXPLOSION, e -> null, (e, b) -> e.blockList().remove(b), e -> e.blockList());
    ProtectionListener.protect(PlayerBucketFillEvent.class, ProtectionType.USE_BUCKETS, e -> e.getPlayer(), e -> e.getBlockClicked());
    ProtectionListener.protect(PlayerBucketEmptyEvent.class, ProtectionType.USE_BUCKETS, e -> e.getPlayer(), e -> e.getBlockClicked());
    ProtectionListener.protectMultiBlock(BlockPistonExtendEvent.class, ProtectionType.PISTONS, e -> null, (e, b) -> e.setCancelled(true), e -> {
        List<Block> blocks = new ArrayList<>(e.getBlocks());
        blocks.add(e.getBlock());
        return blocks;
    });
    ProtectionListener.protectMultiBlock(BlockPistonRetractEvent.class, ProtectionType.PISTONS, e -> null, (e, b) -> e.setCancelled(true), e -> {
        List<Block> blocks = new ArrayList<>(e.getBlocks());
        blocks.add(e.getBlock());
        return blocks;
    });
    ProtectionListener.protectNonCancellable(BlockRedstoneEvent.class, ProtectionType.REDSTONE, e -> null, e -> e.setNewCurrent(e.getOldCurrent()), e -> e.getBlock());
    ProtectionListener.protect(EntityChangeBlockEvent.class, ProtectionType.FALLING_BLOCK, e -> null, e -> {
        if (!(e.getEntity() instanceof FallingBlock)) {
            return null;
        }
        return e.getBlock();
    });
    ProtectionListener.protect(EntityChangeBlockEvent.class, ProtectionType.SILVERFISH, e -> null, e -> {
        if (!(e.getEntity() instanceof Silverfish)) {
            return null;
        }
        return e.getBlock();
    });
    ProtectionListener.protect(EntityChangeBlockEvent.class, ProtectionType.WITHER, e -> null, e -> {
        if (!(e.getEntity() instanceof Wither)) {
            return null;
        }
        return e.getBlock();
    });
    ProtectionListener.protect(BlockGrowEvent.class, ProtectionType.GROWTH, e -> null, e -> e.getBlock());
    ProtectionListener.protect(BlockSpreadEvent.class, ProtectionType.GROWTH, e -> null, e -> e.getNewState().getType() == Material.FIRE ? null : e.getBlock());
    ProtectionListener.protect(BlockFormEvent.class, ProtectionType.GROWTH, e -> null, e -> e.getBlock());
    ProtectionListener.protect(BlockFadeEvent.class, ProtectionType.FADE, e -> null, e -> e.getBlock());
    ProtectionListener.protect(BlockFromToEvent.class, ProtectionType.FLOW, e -> null, e -> e.getBlock(), e -> e.getToBlock());
    ProtectionListener.protect(BlockBurnEvent.class, ProtectionType.FIRE, e -> null, e -> e.getBlock());
    ProtectionListener.protect(BlockSpreadEvent.class, ProtectionType.FIRE, e -> null, e -> e.getNewState().getType() == Material.FIRE ? e.getBlock() : null);
    ProtectionListener.protect(CreatureSpawnEvent.class, ProtectionType.MOB_SPAWN, e -> null, e -> {
        if (e.getSpawnReason() == CreatureSpawnEvent.SpawnReason.CUSTOM) {
            return null;
        }
        return e.getEntity().getLocation().getBlock();
    });
    ProtectionListener.protectMultiBlock(PortalCreateEvent.class, ProtectionType.PORTAL_PAIRING, e -> null, (e, b) -> e.setCancelled(true), e -> {
        if (e.getReason() != PortalCreateEvent.CreateReason.NETHER_PAIR) {
            return null;
        }
        return e.getBlocks().stream().map(BlockState::getBlock).collect(Collectors.toList());
    });
    ProtectionListener.protectDirectional(BlockFromToEvent.class, ProtectionType.FLOW_IN, e -> null, e -> e.getBlock(), e -> Collections.singletonList(e.getToBlock()));
    ProtectionListener.protectDirectional(BlockPistonExtendEvent.class, ProtectionType.PISTONS_IN, e -> null, e -> e.getBlock(), e -> e.getBlocks().stream().map(b -> b.getRelative(e.getDirection())).collect(Collectors.toList()));
    ProtectionListener.protectDirectional(BlockPistonRetractEvent.class, ProtectionType.PISTONS_IN, e -> null, e -> e.getBlock(), e -> e.getBlocks());
    ProtectionListener.protectMultiBlock(StructureGrowEvent.class, ProtectionType.STRUCTURE_GROWTH, e -> null, (e, b) -> e.setCancelled(true), e -> e.getBlocks().stream().map(BlockState::getBlock).collect(Collectors.toList()));
    ProtectionListener.protectDirectional(StructureGrowEvent.class, ProtectionType.STRUCTURE_GROWTH_IN, e -> null, e -> e.getLocation().getBlock(), e -> e.getBlocks().stream().map(BlockState::getBlock).collect(Collectors.toList()));
    ProtectionListener.protect(EntityBlockFormEvent.class, ProtectionType.ENTITY_FORM_BLOCK, e -> e.getEntity() instanceof Player ? (Player) e.getEntity() : null, e -> e.getBlock());
    ProtectionListener.protectDirectional(InventoryMoveItemEvent.class, ProtectionType.CONTAINER_ACCESS, e -> null, e -> getBlock(e.getSource()), e -> Collections.singletonList(getBlock(e.getDestination())));
    ProtectionListener.protect(PlayerInteractEntityEvent.class, ProtectionType.INTERACT_ENTITY, PlayerEvent::getPlayer, e -> e.getRightClicked() instanceof StorageMinecart ? null : e.getRightClicked().getLocation().getBlock());
    ProtectionListener.protect(PlayerInteractEntityEvent.class, ProtectionType.CONTAINER_ACCESS, PlayerEvent::getPlayer, e -> e.getRightClicked() instanceof StorageMinecart ? e.getRightClicked().getLocation().getBlock() : null);
    ProtectionListener.protect(PlayerArmorStandManipulateEvent.class, ProtectionType.INTERACT_ENTITY, PlayerEvent::getPlayer, e -> e.getRightClicked().getLocation().getBlock());
    ProtectionListener.protect(HangingPlaceEvent.class, ProtectionType.PLACE_ENTITY, HangingPlaceEvent::getPlayer, HangingPlaceEvent::getBlock);
    ProtectionListener.protect(PlayerInteractEvent.class, ProtectionType.PLACE_ENTITY, e -> e.getPlayer(), e -> {
        if (e.getAction() != Action.RIGHT_CLICK_BLOCK || e.getItem() == null) {
            return null;
        }
        Material type = e.getItem().getType();
        if (type.toString().endsWith("BOAT") || type.toString().endsWith("MINECART") || type == Material.ARMOR_STAND) {
            return e.getClickedBlock();
        }
        return null;
    });
    ProtectionListener.protect(HangingBreakByEntityEvent.class, ProtectionType.INTERACT_ENTITY, e -> e.getRemover() instanceof Player ? (Player) e.getRemover() : null, e -> e.getCause() == HangingBreakEvent.RemoveCause.ENTITY ? getBlock(e.getEntity()) : null);
    ProtectionListener.protect(HangingBreakByEntityEvent.class, ProtectionType.ENTITY_EXPLOSION, e -> null, e -> e.getCause() == HangingBreakEvent.RemoveCause.EXPLOSION ? getBlock(e.getEntity()) : null);
    Set<Material> bannedDispenserItems = EnumSet.of(Material.FLINT_AND_STEEL, Material.WATER_BUCKET, Material.LAVA_BUCKET, Material.JACK_O_LANTERN, Material.PUMPKIN, Material.ARMOR_STAND);
    Arrays.stream(Material.values()).filter(m -> m.toString().endsWith("MINECART") || m.toString().endsWith("BOAT") || m.toString().endsWith("_BUCKET")).forEach(bannedDispenserItems::add);
    ProtectionListener.protect(BlockDispenseEvent.class, ProtectionType.DISPENSER_PLACE, e -> null, e -> bannedDispenserItems.contains(e.getItem().getType()) ? e.getBlock() : null);
    ProtectionListener.protectDirectional(BlockDispenseEvent.class, ProtectionType.DISPENSER_PLACE_IN, e -> null, e -> e.getBlock(), e -> {
        if (!bannedDispenserItems.contains(e.getItem().getType())) {
            return null;
        }
        Block rel = e.getBlock().getRelative(getFace(e.getBlock()));
        return Collections.singletonList(rel);
    });
    ProtectionListener.protectMultiBlock(BlockMultiPlaceEvent.class, ProtectionType.PLACE_BLOCK, e -> e.getPlayer(), (e, b) -> e.setCancelled(true), e -> e.getReplacedBlockStates().stream().map(BlockState::getBlock).collect(Collectors.toList()));
}
Also used : Wither(org.bukkit.entity.Wither) Arrays(java.util.Arrays) Wither(org.bukkit.entity.Wither) HangingBreakByEntityEvent(org.bukkit.event.hanging.HangingBreakByEntityEvent) PortalCreateEvent(org.bukkit.event.world.PortalCreateEvent) BlockFace(org.bukkit.block.BlockFace) Player(org.bukkit.entity.Player) org.bukkit.event.block(org.bukkit.event.block) Inventory(org.bukkit.inventory.Inventory) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) EntityChangeBlockEvent(org.bukkit.event.entity.EntityChangeBlockEvent) HangingBreakEvent(org.bukkit.event.hanging.HangingBreakEvent) BlockInventoryHolder(org.bukkit.inventory.BlockInventoryHolder) Block(org.bukkit.block.Block) InventoryMoveItemEvent(org.bukkit.event.inventory.InventoryMoveItemEvent) StorageMinecart(org.bukkit.entity.minecart.StorageMinecart) Directional(org.bukkit.block.data.Directional) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) Material(org.bukkit.Material) EnumSet(java.util.EnumSet) Minecart(org.bukkit.entity.Minecart) ProtectionType(redempt.redlib.protection.ProtectionPolicy.ProtectionType) StructureGrowEvent(org.bukkit.event.world.StructureGrowEvent) CreatureSpawnEvent(org.bukkit.event.entity.CreatureSpawnEvent) BlockState(org.bukkit.block.BlockState) Set(java.util.Set) PlayerInteractEntityEvent(org.bukkit.event.player.PlayerInteractEntityEvent) Collectors(java.util.stream.Collectors) PlayerBucketFillEvent(org.bukkit.event.player.PlayerBucketFillEvent) InventoryHolder(org.bukkit.inventory.InventoryHolder) RedLib(redempt.redlib.RedLib) Hanging(org.bukkit.entity.Hanging) List(java.util.List) PlayerBucketEmptyEvent(org.bukkit.event.player.PlayerBucketEmptyEvent) PlayerArmorStandManipulateEvent(org.bukkit.event.player.PlayerArmorStandManipulateEvent) InventoryOpenEvent(org.bukkit.event.inventory.InventoryOpenEvent) Dispenser(org.bukkit.material.Dispenser) PlayerEvent(org.bukkit.event.player.PlayerEvent) Silverfish(org.bukkit.entity.Silverfish) DoubleChest(org.bukkit.block.DoubleChest) EntityExplodeEvent(org.bukkit.event.entity.EntityExplodeEvent) FallingBlock(org.bukkit.entity.FallingBlock) HangingPlaceEvent(org.bukkit.event.hanging.HangingPlaceEvent) Collections(java.util.Collections) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) PlayerEvent(org.bukkit.event.player.PlayerEvent) Silverfish(org.bukkit.entity.Silverfish) Material(org.bukkit.Material) FallingBlock(org.bukkit.entity.FallingBlock) BlockState(org.bukkit.block.BlockState) StorageMinecart(org.bukkit.entity.minecart.StorageMinecart) HangingPlaceEvent(org.bukkit.event.hanging.HangingPlaceEvent) Block(org.bukkit.block.Block) FallingBlock(org.bukkit.entity.FallingBlock) BlockInventoryHolder(org.bukkit.inventory.BlockInventoryHolder) InventoryHolder(org.bukkit.inventory.InventoryHolder) HashSet(java.util.HashSet)

Example 2 with Silverfish

use of org.bukkit.entity.Silverfish in project CoreProtect by PlayPro.

the class EntityChangeBlockListener method onEntityChangeBlock.

@EventHandler(priority = EventPriority.MONITOR)
protected void onEntityChangeBlock(EntityChangeBlockEvent event) {
    World world = event.getBlock().getWorld();
    if (!event.isCancelled() && Config.getConfig(world).ENTITY_CHANGE) {
        // Can be sand/gravel
        Entity entity = event.getEntity();
        Block block = event.getBlock();
        Material newtype = event.getTo();
        Material type = event.getBlock().getType();
        String e = "";
        if (entity instanceof Enderman) {
            e = "#enderman";
        } else if (entity instanceof EnderDragon) {
            e = "#enderdragon";
        } else if (entity instanceof Fox) {
            e = "#fox";
        } else if (entity instanceof Wither) {
            e = "#wither";
        } else if (entity instanceof Turtle) {
            e = "#turtle";
        } else if (entity instanceof Ravager) {
            e = "#ravager";
        } else if (entity instanceof Silverfish) {
            if (newtype.equals(Material.AIR) || newtype.equals(Material.CAVE_AIR)) {
                e = "#silverfish";
            }
        }
        if (e.length() > 0) {
            if (newtype.equals(Material.AIR) || newtype.equals(Material.CAVE_AIR)) {
                Queue.queueBlockBreak(e, block.getState(), type, block.getBlockData().getAsString(), 0);
            } else {
                queueBlockPlace(e, block.getState(), type, block.getState(), newtype, -1, 0, event.getBlockData().getAsString());
            }
        }
    }
}
Also used : Wither(org.bukkit.entity.Wither) Entity(org.bukkit.entity.Entity) Turtle(org.bukkit.entity.Turtle) EnderDragon(org.bukkit.entity.EnderDragon) Enderman(org.bukkit.entity.Enderman) Block(org.bukkit.block.Block) Material(org.bukkit.Material) Silverfish(org.bukkit.entity.Silverfish) Ravager(org.bukkit.entity.Ravager) World(org.bukkit.World) Fox(org.bukkit.entity.Fox) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Material (org.bukkit.Material)2 Block (org.bukkit.block.Block)2 Silverfish (org.bukkit.entity.Silverfish)2 Wither (org.bukkit.entity.Wither)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 EnumSet (java.util.EnumSet)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 World (org.bukkit.World)1 BlockFace (org.bukkit.block.BlockFace)1 BlockState (org.bukkit.block.BlockState)1 DoubleChest (org.bukkit.block.DoubleChest)1 Directional (org.bukkit.block.data.Directional)1 EnderDragon (org.bukkit.entity.EnderDragon)1 Enderman (org.bukkit.entity.Enderman)1 Entity (org.bukkit.entity.Entity)1