Search in sources :

Example 1 with TileState

use of org.bukkit.block.TileState 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 TileState

use of org.bukkit.block.TileState in project MissileWarfare by koiboi-dev.

the class GroundMissileLauncher method fireMissile.

public boolean fireMissile(Dispenser disp, MissileClass missile) {
    TileState state = (TileState) disp.getBlock().getState();
    PersistentDataContainer cont = state.getPersistentDataContainer();
    int[] coords = cont.get(new NamespacedKey(MissileWarfare.getInstance(), "coords"), PersistentDataType.INTEGER_ARRAY);
    Integer alt = cont.get(new NamespacedKey(MissileWarfare.getInstance(), "alt"), PersistentDataType.INTEGER);
    if (coords == null) {
        MissileWarfare.getInstance().getServer().broadcastMessage("Missile cannot fire at : " + new Vector(disp.getBlock().getLocation().getX(), disp.getBlock().getLocation().getY(), disp.getBlock().getLocation().getZ()) + " Invalid Coordinates!");
        return false;
    } else if (VariantsAPI.isInRange((int) disp.getLocation().distanceSquared(new Vector(coords[0], 0, coords[1]).toLocation(disp.getWorld())), missile.type)) {
        MissileWarfare.getInstance().getServer().broadcastMessage("Missile cannot fire at : " + disp.getBlock().getLocation() + " Target out of distance!");
        return false;
    }
    if (alt == null) {
        alt = 120;
    }
    if (MissileWarfare.plugin.getConfig().getBoolean("logging.logMissileShots")) {
        Player result = null;
        double lastDistance = Double.MAX_VALUE;
        for (Player p : disp.getWorld().getPlayers()) {
            double distance = disp.getLocation().distanceSquared(p.getLocation());
            if (distance < lastDistance) {
                lastDistance = distance;
                result = p;
            }
        }
        MissileWarfare.getInstance().getLogger().info("Missile Shot || Location: " + disp.getBlock().getLocation() + " Target: " + new Vector(coords[0], 0, coords[1]) + " Nearest Player: " + result.getName());
        if (MissileWarfare.getInstance().getConfig().getBoolean("logging.broadcastMissileShots")) {
            final String playername = result.getName();
            new BukkitRunnable() {

                @Override
                public void run() {
                    MissileWarfare.getInstance().getServer().broadcastMessage("Missile Shot! Launcher Coords: " + disp.getBlock().getLocation().toVector() + " Nearest Player: " + playername);
                }
            }.runTaskLater(MissileWarfare.getInstance(), 20L * MissileWarfare.getInstance().getConfig().getLong("logging.waitTimeBeforeBroadcast"));
        }
    }
    MissileController _missile = new MissileController(true, disp.getBlock().getLocation().add(new Vector(0.5, 1.35, 0.5)).toVector(), new Vector(coords[0], 0, coords[1]), (float) missile.speed, disp.getBlock().getWorld(), missile.power, missile.accuracy, missile.type, alt);
    _missile.FireMissile();
    return true;
}
Also used : TileState(org.bukkit.block.TileState) Player(org.bukkit.entity.Player) NamespacedKey(org.bukkit.NamespacedKey) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MissileController(me.kaiyan.missilewarfare.Missiles.MissileController) Vector(org.bukkit.util.Vector) PersistentDataContainer(org.bukkit.persistence.PersistentDataContainer)

Example 3 with TileState

use of org.bukkit.block.TileState in project MissileWarfare by koiboi-dev.

the class GroundMissileLauncher method onBlockRightClick.

private void onBlockRightClick(PlayerRightClickEvent event) {
    // Stick/Blaze Rod Method
    if (event.getItem().getType() == Material.STICK) {
        event.cancel();
        TileState state = (TileState) Objects.requireNonNull(event.getInteractEvent().getClickedBlock()).getState();
        PersistentDataContainer cont = state.getPersistentDataContainer();
        try {
            if (event.getPlayer().isSneaking()) {
                int[] coords = cont.get(new NamespacedKey(MissileWarfare.getInstance(), "coords"), PersistentDataType.INTEGER_ARRAY);
                float dist = (float) new Vector(coords[0], 0, coords[1]).distance(new Vector(event.getInteractEvent().getClickedBlock().getX(), 0, event.getInteractEvent().getClickedBlock().getY()));
                event.getPlayer().sendMessage(Translations.get("messages.launchers.ground.coords").replace("{xcoord}", String.valueOf(coords[0])).replace("{ycoord}", String.valueOf(coords[1])).replace("{dist}", String.valueOf(dist)));
                return;
            }
        } catch (NullPointerException e) {
            cont.set(new NamespacedKey(MissileWarfare.getInstance(), "coords"), PersistentDataType.INTEGER_ARRAY, new int[] { 0, 0 });
            state.update();
        }
        try {
            if (cont.get(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER) == 0) {
                Prompt askCoordY = new StringPrompt() {

                    @Override
                    public String getPromptText(ConversationContext conversationContext) {
                        return "Input Coordinates Z, Input exit to cancel";
                    }

                    @Override
                    public Prompt acceptInput(ConversationContext conversationContext, String s) {
                        try {
                            cont.set(new NamespacedKey(MissileWarfare.getInstance(), "coords"), PersistentDataType.INTEGER_ARRAY, new int[] { cont.get(new NamespacedKey(MissileWarfare.getInstance(), "coords"), PersistentDataType.INTEGER_ARRAY)[0], Integer.parseInt(s) });
                        } catch (NumberFormatException e) {
                            conversationContext.getForWhom().sendRawMessage(Translations.get("messages.launchers.ground.setting.notanumber"));
                            cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 0);
                            state.update();
                            return END_OF_CONVERSATION;
                        }
                        conversationContext.getForWhom().sendRawMessage("Z: " + Integer.parseInt(s));
                        cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 0);
                        state.update();
                        return END_OF_CONVERSATION;
                    }
                };
                Prompt askCoordX = new StringPrompt() {

                    @Override
                    public String getPromptText(ConversationContext conversationContext) {
                        return "Input Coordinates X, Input exit to cancel";
                    }

                    @Override
                    public Prompt acceptInput(ConversationContext conversationContext, String s) {
                        try {
                            cont.set(new NamespacedKey(MissileWarfare.getInstance(), "coords"), PersistentDataType.INTEGER_ARRAY, new int[] { Integer.parseInt(s), 0 });
                        } catch (NumberFormatException e) {
                            conversationContext.getForWhom().sendRawMessage(Translations.get("messages.launchers.ground.setting.notacoord"));
                            cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 0);
                            state.update();
                            return END_OF_CONVERSATION;
                        }
                        conversationContext.getForWhom().sendRawMessage("X: " + Integer.parseInt(s));
                        return askCoordY;
                    }
                };
                ConversationFactory cf = new ConversationFactory(MissileWarfare.getInstance());
                Conversation conversation = cf.withFirstPrompt(askCoordX).withLocalEcho(false).withEscapeSequence("exit").withTimeout(20).buildConversation(event.getPlayer());
                conversation.begin();
                cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 1);
            }
        } catch (NullPointerException e) {
            cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 0);
            state.update();
        }
    } else if (event.getItem().getType() == Material.BLAZE_ROD) {
        event.cancel();
        TileState state = (TileState) Objects.requireNonNull(event.getInteractEvent().getClickedBlock()).getState();
        PersistentDataContainer cont = state.getPersistentDataContainer();
        try {
            if (cont.get(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER) == 0) {
                Prompt askCruiseAlt = new StringPrompt() {

                    @Override
                    public String getPromptText(ConversationContext conversationContext) {
                        return "Input Cruise Altitude, Input exit to cancel";
                    }

                    @Override
                    public Prompt acceptInput(ConversationContext conversationContext, String s) {
                        try {
                            cont.set(new NamespacedKey(MissileWarfare.getInstance(), "alt"), PersistentDataType.INTEGER, Integer.valueOf(s));
                        } catch (NumberFormatException e) {
                            conversationContext.getForWhom().sendRawMessage(Translations.get("messages.launchers.ground.setting.notanumber"));
                            cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 0);
                            state.update();
                            return END_OF_CONVERSATION;
                        }
                        conversationContext.getForWhom().sendRawMessage(Translations.get("messages.launchers.ground.setting.cruisealt") + Integer.parseInt(s));
                        cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 0);
                        state.update();
                        return END_OF_CONVERSATION;
                    }
                };
                ConversationFactory cf = new ConversationFactory(MissileWarfare.getInstance());
                Conversation conversation = cf.withFirstPrompt(askCruiseAlt).withLocalEcho(false).withEscapeSequence("exit").withTimeout(20).buildConversation(event.getPlayer());
                conversation.begin();
            }
        } catch (NullPointerException e) {
            cont.set(new NamespacedKey(MissileWarfare.getInstance(), "Conversing"), PersistentDataType.INTEGER, 0);
            state.update();
        }
    } else if (SlimefunItem.getByItem(event.getItem()) == SlimefunItem.getById("PLAYERLIST")) {
        event.cancel();
        TileState state = (TileState) event.getClickedBlock().get().getBlockData();
        PersistentDataContainer cont = state.getPersistentDataContainer();
        cont.set(new NamespacedKey(MissileWarfare.getInstance(), "groupid"), PersistentDataType.STRING, event.getItem().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(MissileWarfare.getInstance(), "id"), PersistentDataType.STRING));
        state.update();
    }
}
Also used : PersistentDataContainer(org.bukkit.persistence.PersistentDataContainer) TileState(org.bukkit.block.TileState) NamespacedKey(org.bukkit.NamespacedKey) Vector(org.bukkit.util.Vector)

Example 4 with TileState

use of org.bukkit.block.TileState in project MissileWarfare by koiboi-dev.

the class GroundMissileLauncher method blockDispense.

private void blockDispense(BlockDispenseEvent blockDispenseEvent, Dispenser dispenser, Block block, SlimefunItem slimefunItem) {
    blockDispenseEvent.setCancelled(true);
    TileState state = (TileState) dispenser.getBlock().getState();
    PersistentDataContainer cont = state.getPersistentDataContainer();
    if (cont.get(new NamespacedKey(MissileWarfare.getInstance(), "canfire"), PersistentDataType.INTEGER) != 1) {
        MissileWarfare.getInstance().getServer().broadcastMessage("Missile at : " + dispenser.getBlock().getLocation().toVector() + " Is unable to fire: Missing GREEN_CONCRETE Below");
    }
    new BukkitRunnable() {

        @Override
        public void run() {
            if (dispenser.getBlock().getRelative(BlockFace.DOWN).getType() == Material.GREEN_CONCRETE) {
                fireMissile(dispenser);
            }
        }
    }.runTaskLater(MissileWarfare.getInstance(), 1);
}
Also used : TileState(org.bukkit.block.TileState) NamespacedKey(org.bukkit.NamespacedKey) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) PersistentDataContainer(org.bukkit.persistence.PersistentDataContainer)

Example 5 with TileState

use of org.bukkit.block.TileState 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)

Aggregations

TileState (org.bukkit.block.TileState)25 NamespacedKey (org.bukkit.NamespacedKey)14 Block (org.bukkit.block.Block)11 PersistentDataContainer (org.bukkit.persistence.PersistentDataContainer)11 BlockState (org.bukkit.block.BlockState)8 ItemType (ch.njol.skript.aliases.ItemType)7 BlockData (org.bukkit.block.data.BlockData)6 EventHandler (org.bukkit.event.EventHandler)5 FixedMetadataValue (org.bukkit.metadata.FixedMetadataValue)5 Value (ch.njol.skript.variables.SerializedVariable.Value)4 NBTCompound (de.tr7zw.changeme.nbtapi.NBTCompound)4 Metadatable (org.bukkit.metadata.Metadatable)4 PersistentDataHolder (org.bukkit.persistence.PersistentDataHolder)4 NBTContainer (de.tr7zw.changeme.nbtapi.NBTContainer)3 BlockDispenseHandler (io.github.thebusybiscuit.slimefun4.core.handlers.BlockDispenseHandler)3 BlockPlaceHandler (io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler)3 HashMap (java.util.HashMap)3 List (java.util.List)3 MetadataValue (org.bukkit.metadata.MetadataValue)3 Vector (org.bukkit.util.Vector)3