Search in sources :

Example 1 with BlockDispenseHandler

use of io.github.thebusybiscuit.slimefun4.core.handlers.BlockDispenseHandler in project MissileWarfare by koiboi-dev.

the class AntiMissileLauncher method preRegister.

@Override
public void preRegister() {
    // cancel thing on place
    BlockPlaceHandler blockPlaceHandler = new BlockPlaceHandler(false) {

        @Override
        public void onPlayerPlace(BlockPlaceEvent event) {
            BlockData data = event.getBlockPlaced().getBlockData();
            ((Directional) data).setFacing(BlockFace.UP);
            event.getBlockPlaced().setBlockData(data);
            Block block = event.getBlockPlaced();
            // Block bottom = world.getBlockAt(event.getBlock().getLocation().subtract(new Vector(0, 2, 0)));
            if (correctlyBuilt(block)) {
                event.getPlayer().sendMessage(Translations.get("messages.launchers.createantiair.success"));
            } else {
                event.getPlayer().sendMessage(Translations.get("messages.launchers.createantiair.failure"));
            }
        }
    };
    addItemHandler(blockPlaceHandler);
    BlockDispenseHandler blockDispenseHandler = this::blockDispense;
    addItemHandler(blockDispenseHandler);
    addItemHandler(new BlockTicker() {

        @Override
        public boolean isSynchronized() {
            return true;
        }

        @Override
        public void tick(Block block, SlimefunItem slimefunItem, Config config) {
            TileState state = (TileState) block.getState();
            PersistentDataContainer cont = state.getPersistentDataContainer();
            if (!block.isBlockPowered()) {
                List<MissileController> missiles = MissileWarfare.activemissiles;
                MissileController locked = null;
                if (!missiles.isEmpty()) {
                    for (MissileController missile : missiles) {
                        if (block.getLocation().distanceSquared(missile.pos.toLocation(missile.world)) < range && missile.isgroundmissile) {
                            locked = missile;
                            break;
                        }
                    }
                }
                state.update();
                try {
                    if (locked != null && cont.get(new NamespacedKey(MissileWarfare.getInstance(), "timesincelastshot"), PersistentDataType.INTEGER) <= System.currentTimeMillis()) {
                        cont.set(new NamespacedKey(MissileWarfare.getInstance(), "timesincelastshot"), PersistentDataType.INTEGER, (int) System.currentTimeMillis() + 1000);
                        fireMissile((Dispenser) block.getState(), locked);
                    }
                } catch (NullPointerException e) {
                    cont.set(new NamespacedKey(MissileWarfare.getInstance(), "timesincelastshot"), PersistentDataType.INTEGER, Integer.MIN_VALUE);
                    state.update();
                }
            }
        }
    });
}
Also used : Dispenser(org.bukkit.block.Dispenser) BlockPlaceEvent(org.bukkit.event.block.BlockPlaceEvent) Config(me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config) SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem) BlockTicker(me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker) Directional(org.bukkit.block.data.Directional) PersistentDataContainer(org.bukkit.persistence.PersistentDataContainer) TileState(org.bukkit.block.TileState) BlockPlaceHandler(io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler) NamespacedKey(org.bukkit.NamespacedKey) Block(org.bukkit.block.Block) BlockDispenseHandler(io.github.thebusybiscuit.slimefun4.core.handlers.BlockDispenseHandler) List(java.util.List) MissileController(me.kaiyan.missilewarfare.Missiles.MissileController) BlockData(org.bukkit.block.data.BlockData)

Example 2 with BlockDispenseHandler

use of io.github.thebusybiscuit.slimefun4.core.handlers.BlockDispenseHandler in project MissileWarfare by koiboi-dev.

the class GroundMissileLauncher method preRegister.

@Override
public void preRegister() {
    // cancel thing on place
    BlockPlaceHandler blockPlaceHandler = new BlockPlaceHandler(false) {

        @Override
        public void onPlayerPlace(BlockPlaceEvent event) {
            World world = event.getBlock().getWorld();
            Block below = world.getBlockAt(event.getBlock().getLocation().subtract(new Vector(0, 1, 0)));
            BlockData data = event.getBlockPlaced().getBlockData();
            ((Directional) data).setFacing(BlockFace.UP);
            event.getBlockPlaced().setBlockData(data);
            // Block bottom = world.getBlockAt(event.getBlock().getLocation().subtract(new Vector(0, 2, 0)));
            if (below.getType() == Material.GREEN_CONCRETE) {
                event.getPlayer().sendMessage(Translations.get("messages.launchers.ground.create.success"));
                TileState state = (TileState) event.getBlockPlaced().getState();
                PersistentDataContainer cont = state.getPersistentDataContainer();
                cont.set(new NamespacedKey(MissileWarfare.getInstance(), "canfire"), PersistentDataType.INTEGER, 1);
                state.update();
            /*if (bottom.getType() == Material.GREEN_CONCRETE){
                        event.getPlayer().sendMessage("Created Small Launcher!");
                    }else{
                        event.getPlayer().sendMessage("Bottom Block is type: " + bottom.getType() + " It needs Type GREEN_CONCRETE");
                        event.setCancelled(true);
                    }*/
            } else {
                event.getPlayer().sendMessage(Translations.get("messages.launchers.ground.create.failure").replace("{type}", below.getType().name()));
            }
        }
    };
    addItemHandler(blockPlaceHandler);
    BlockUseHandler blockUseHandler = this::onBlockRightClick;
    addItemHandler(blockUseHandler);
    BlockDispenseHandler blockDispenseHandler = this::blockDispense;
    addItemHandler(blockDispenseHandler);
}
Also used : TileState(org.bukkit.block.TileState) BlockPlaceHandler(io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler) BlockPlaceEvent(org.bukkit.event.block.BlockPlaceEvent) NamespacedKey(org.bukkit.NamespacedKey) BlockUseHandler(io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler) Block(org.bukkit.block.Block) BlockDispenseHandler(io.github.thebusybiscuit.slimefun4.core.handlers.BlockDispenseHandler) World(org.bukkit.World) BlockData(org.bukkit.block.data.BlockData) Vector(org.bukkit.util.Vector) Directional(org.bukkit.block.data.Directional) PersistentDataContainer(org.bukkit.persistence.PersistentDataContainer)

Example 3 with BlockDispenseHandler

use of io.github.thebusybiscuit.slimefun4.core.handlers.BlockDispenseHandler in project MissileWarfare by koiboi-dev.

the class AntiElytraLauncher method preRegister.

@Override
public void preRegister() {
    // cancel thing on place
    BlockPlaceHandler blockPlaceHandler = new BlockPlaceHandler(false) {

        @Override
        public void onPlayerPlace(BlockPlaceEvent event) {
            BlockData data = event.getBlockPlaced().getBlockData();
            ((Directional) data).setFacing(BlockFace.UP);
            event.getBlockPlaced().setBlockData(data);
            Block block = event.getBlockPlaced();
            // Block bottom = world.getBlockAt(event.getBlock().getLocation().subtract(new Vector(0, 2, 0)));
            if (correctlyBuilt(block)) {
                event.getPlayer().sendMessage(Translations.get("messages.launchers.createantielytra.success"));
            } else {
                event.getPlayer().sendMessage(Translations.get("messages.launchers.createantielytra.failure"));
            }
        }
    };
    addItemHandler(blockPlaceHandler);
    BlockDispenseHandler blockDispenseHandler = this::blockDispense;
    addItemHandler(blockDispenseHandler);
    addItemHandler(new BlockTicker() {

        @Override
        public boolean isSynchronized() {
            return true;
        }

        @Override
        public void tick(Block block, SlimefunItem slimefunItem, Config config) {
            TileState state = (TileState) block.getState();
            PersistentDataContainer cont = state.getPersistentDataContainer();
            Player locked = null;
            if (!block.isBlockPowered()) {
                Collection<? extends Player> missiles = MissileWarfare.getInstance().getServer().getOnlinePlayers();
                if (!missiles.isEmpty()) {
                    for (Player player : missiles) {
                        if (block.getLocation().distanceSquared(player.getLocation()) < range) {
                            if (player.isGliding() && !PlayerID.targets.contains(player)) {
                                List<OfflinePlayer> ignore = PlayerID.players.get(cont.get(new NamespacedKey(MissileWarfare.getInstance(), "groupid"), PersistentDataType.STRING));
                                if (ignore == null) {
                                    locked = player;
                                    break;
                                } else if (ignore.contains(player)) {
                                    locked = player;
                                    break;
                                }
                            }
                        }
                    }
                }
                state.update();
                if (locked != null) {
                    UUID playerUUID = locked.getUniqueId();
                    PlayerID.targets.add(MissileWarfare.getInstance().getServer().getPlayer(playerUUID));
                    Player finalLocked = locked;
                    finalLocked.spigot().sendMessage(ChatMessageType.ACTION_BAR, new ComponentBuilder(Translations.get("messages.elytraattack.locking")).color(ChatColor.RED).create());
                    new BukkitRunnable() {

                        @Override
                        public void run() {
                            Player player = MissileWarfare.getInstance().getServer().getPlayer(playerUUID);
                            if (player == null) {
                                PlayerID.targets = new ArrayList<>();
                                return;
                            }
                            if (player.isGliding()) {
                                finalLocked.spigot().sendMessage(ChatMessageType.ACTION_BAR, new ComponentBuilder(Translations.get("messages.elytraattack.locked")).color(ChatColor.DARK_RED).create());
                                fireMissile((Dispenser) block.getState(), MissileWarfare.getInstance().getServer().getPlayer(playerUUID));
                            } else if (player.isInsideVehicle()) {
                                if (!player.getVehicle().isOnGround()) {
                                    finalLocked.spigot().sendMessage(ChatMessageType.ACTION_BAR, new ComponentBuilder(Translations.get("messages.elytraattack.locked")).color(ChatColor.DARK_RED).create());
                                    fireMissile((Dispenser) block.getState(), MissileWarfare.getInstance().getServer().getPlayer(playerUUID));
                                }
                            }
                        }
                    }.runTaskLater(MissileWarfare.getInstance(), 80);
                }
            }
        }
    });
    BlockUseHandler blockUseHandler = this::onBlockRightClick;
    addItemHandler(blockUseHandler);
}
Also used : Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) Dispenser(org.bukkit.block.Dispenser) BlockPlaceEvent(org.bukkit.event.block.BlockPlaceEvent) Config(me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config) BlockUseHandler(io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem) BlockTicker(me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker) Directional(org.bukkit.block.data.Directional) PersistentDataContainer(org.bukkit.persistence.PersistentDataContainer) TileState(org.bukkit.block.TileState) BlockPlaceHandler(io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler) NamespacedKey(org.bukkit.NamespacedKey) Block(org.bukkit.block.Block) BlockDispenseHandler(io.github.thebusybiscuit.slimefun4.core.handlers.BlockDispenseHandler) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) BlockData(org.bukkit.block.data.BlockData) UUID(java.util.UUID) ComponentBuilder(net.md_5.bungee.api.chat.ComponentBuilder)

Aggregations

BlockDispenseHandler (io.github.thebusybiscuit.slimefun4.core.handlers.BlockDispenseHandler)3 BlockPlaceHandler (io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler)3 NamespacedKey (org.bukkit.NamespacedKey)3 Block (org.bukkit.block.Block)3 TileState (org.bukkit.block.TileState)3 BlockData (org.bukkit.block.data.BlockData)3 Directional (org.bukkit.block.data.Directional)3 BlockPlaceEvent (org.bukkit.event.block.BlockPlaceEvent)3 PersistentDataContainer (org.bukkit.persistence.PersistentDataContainer)3 SlimefunItem (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem)2 BlockUseHandler (io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler)2 List (java.util.List)2 Config (me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config)2 BlockTicker (me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker)2 Dispenser (org.bukkit.block.Dispenser)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 UUID (java.util.UUID)1 MissileController (me.kaiyan.missilewarfare.Missiles.MissileController)1 ComponentBuilder (net.md_5.bungee.api.chat.ComponentBuilder)1