Search in sources :

Example 1 with StorageMinecart

use of org.bukkit.entity.minecart.StorageMinecart 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 StorageMinecart

use of org.bukkit.entity.minecart.StorageMinecart in project ZEnchantmentCore by zivoric.

the class LegacyLootGenerateListener method onInteractMinecartChest.

@EventHandler
public void onInteractMinecartChest(PlayerInteractEntityEvent event) {
    if (event.getRightClicked() instanceof StorageMinecart) {
        StorageMinecart minecart = (StorageMinecart) event.getRightClicked();
        if (minecart.getLootTable() != null) {
            Entity looter = event.getPlayer();
            LootTable table = minecart.getLootTable();
            minecart.getInventory().forEach(item -> {
                if (item != null)
                    LootGenerateListener.addEnchantToItem(generator, table, item);
            });
        }
    }
}
Also used : LootTable(org.bukkit.loot.LootTable) Entity(org.bukkit.entity.Entity) StorageMinecart(org.bukkit.entity.minecart.StorageMinecart) EventHandler(org.bukkit.event.EventHandler)

Aggregations

StorageMinecart (org.bukkit.entity.minecart.StorageMinecart)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 Material (org.bukkit.Material)1 Block (org.bukkit.block.Block)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 Entity (org.bukkit.entity.Entity)1 FallingBlock (org.bukkit.entity.FallingBlock)1 Hanging (org.bukkit.entity.Hanging)1 Minecart (org.bukkit.entity.Minecart)1 Player (org.bukkit.entity.Player)1