Search in sources :

Example 1 with MissileController

use of me.kaiyan.missilewarfare.Missiles.MissileController 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 MissileController

use of me.kaiyan.missilewarfare.Missiles.MissileController in project MissileWarfare by koiboi-dev.

the class AntiMissileLauncher method fireMissile.

/*@Deprecated
        public void fireMissile(PlayerRightClickEvent event){
            Dispenser disp = (Dispenser) Objects.requireNonNull(event.getInteractEvent().getClickedBlock()).getState();
            int type = hasAmmo(disp.getInventory(), (SmallGtGMissile) itemMissile);
            if (type !=0){
                TileState state = (TileState) Objects.requireNonNull(event.getInteractEvent().getClickedBlock()).getState();
                PersistentDataContainer cont = state.getPersistentDataContainer();
                int[] coords = cont.get(new NamespacedKey(AdvancedWarfare.getInstance(), "coords"), PersistentDataType.INTEGER_ARRAY);
                event.getPlayer().sendMessage(Arrays.toString(coords));
                if (coords == null) {
                    event.getPlayer().sendMessage("You need to input coordinates!");
                    return;
                }
                MissileController missile = new MissileController(true, event.getInteractEvent().getClickedBlock().getLocation().add(new Vector(0.5, 1, 0.5)).toVector(), new Vector(coords[0], 0, coords[1]), 1, event.getPlayer().getWorld(), 3, 30);
                missile.FireMissile();
            }
        }
         */
public void fireMissile(Dispenser disp, MissileController target) {
    ItemStack missileitem = VariantsAPI.getOtherFirstMissile(disp.getInventory(), SlimefunItem.getById("ANTIAIRMISSILE"));
    if (SlimefunItem.getByItem(missileitem) == SlimefunItem.getById("ANTIAIRMISSILE")) {
        ItemUtils.consumeItem(missileitem, false);
        MissileController missile = new MissileController(false, disp.getBlock().getLocation().add(new Vector(0.5, 1.35, 0.5)).toVector(), new Vector(0, 0, 0), 8, disp.getWorld(), 3, 0, 0, new Vector(0, 0, 0));
        missile.FireMissileAtMissile(target);
    }
}
Also used : MissileController(me.kaiyan.missilewarfare.Missiles.MissileController) ItemStack(org.bukkit.inventory.ItemStack) SlimefunItemStack(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack) Vector(org.bukkit.util.Vector)

Example 3 with MissileController

use of me.kaiyan.missilewarfare.Missiles.MissileController 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 4 with MissileController

use of me.kaiyan.missilewarfare.Missiles.MissileController in project MissileWarfare by koiboi-dev.

the class MissileWarfare method onDisable.

@Override
public void onDisable() {
    for (MissileController missile : activemissiles) {
        try {
            missile.armourStand.remove();
            missile.update.cancel();
        } catch (NullPointerException e) {
            try {
                missile.update.cancel();
            } catch (NullPointerException ignored) {
            }
        }
    }
    PlayerID.savePlayers(new Config(new File(this.getDataFolder() + "/saveID.yml")));
// Logic for disabling the plugin...
}
Also used : MissileConfig(me.kaiyan.missilewarfare.Missiles.MissileConfig) Config(io.github.thebusybiscuit.slimefun4.libraries.dough.config.Config) MissileController(me.kaiyan.missilewarfare.Missiles.MissileController) File(java.io.File)

Example 5 with MissileController

use of me.kaiyan.missilewarfare.Missiles.MissileController in project MissileWarfare by koiboi-dev.

the class ManPad method itemUse.

private void itemUse(PlayerRightClickEvent event) {
    SlimefunItem manpad = event.getSlimefunItem().get();
    if (event.getPlayer().isSneaking() && !active.contains(event.getPlayer())) {
        event.getPlayer().sendMessage(Translations.get("messages.manpad.locking"));
        active.add(event.getPlayer());
        new BukkitRunnable() {

            public int scanLinepos = 0;

            public boolean scanLineReturn = false;

            @Override
            public void run() {
                // check if changed weapon
                if (!event.getSlimefunItem().isPresent()) {
                    event.getPlayer().sendMessage(Translations.get("messages.manpad.itemchanged"));
                } else if (event.getSlimefunItem().get() != manpad) {
                    event.getPlayer().sendMessage(Translations.get("messages.manpad.itemchanged"));
                }
                MissileController lockedmissile = null;
                // Get Missiles In Range
                List<MissileController> missiles = new ArrayList<>();
                for (MissileController missile : MissileWarfare.activemissiles) {
                    if (missile.isgroundmissile) {
                        if (missile.pos.distanceSquared(event.getPlayer().getLocation().toVector()) < range) {
                            missiles.add(missile);
                        }
                    }
                }
                // Get looked at missile
                float mindist = 100000;
                for (MissileController missile : missiles) {
                    Vector dir = event.getPlayer().getLocation().getDirection();
                    Location loc = event.getPlayer().getLocation();
                    Vector target = loc.add(dir.multiply(loc.distance(missile.pos.toLocation(event.getPlayer().getWorld())))).toVector();
                    float dist = (float) Math.floor(target.distanceSquared(missile.pos));
                    float acdist = (float) loc.distanceSquared(missile.pos.toLocation(event.getPlayer().getWorld()));
                    if (target.isInSphere(missile.pos, missile.speed + ((acdist / range) * 8)) && dist < mindist) {
                        lockedmissile = missile;
                        mindist = dist;
                    }
                }
                ComponentBuilder scanline = new ComponentBuilder();
                if (lockedmissile == null) {
                    scanline.append("[").color(ChatColor.WHITE);
                    if (scanLinepos == 0) {
                        scanLinepos++;
                        scanline.append("X").color(ChatColor.BLUE);
                        scanline.append("XX").color(ChatColor.WHITE);
                        event.getPlayer().playNote(event.getPlayer().getLocation(), Instrument.PIANO, Note.flat(1, Note.Tone.B));
                        scanLineReturn = false;
                    } else if (scanLinepos == 2) {
                        scanLinepos--;
                        scanLineReturn = true;
                        scanline.append("XX").color(ChatColor.WHITE);
                        scanline.append("X").color(ChatColor.BLUE);
                        event.getPlayer().playNote(event.getPlayer().getLocation(), Instrument.PIANO, Note.flat(1, Note.Tone.B));
                    } else if (!scanLineReturn) {
                        scanLinepos++;
                        scanline.append("X").color(ChatColor.WHITE);
                        scanline.append("X").color(ChatColor.BLUE);
                        scanline.append("X").color(ChatColor.WHITE);
                    } else if (scanLineReturn) {
                        scanLinepos--;
                        scanline.append("X").color(ChatColor.WHITE);
                        scanline.append("X").color(ChatColor.BLUE);
                        scanline.append("X").color(ChatColor.WHITE);
                    }
                    scanline.append("]").color(ChatColor.WHITE);
                } else {
                    scanline.append("MissileType: " + VariantsAPI.getStrVariantFromInt(lockedmissile.type)).color(ChatColor.GOLD);
                    scanline.append("[");
                    scanline.append("XXX").color(ChatColor.RED);
                    scanline.append("]").color(ChatColor.WHITE);
                    scanline.append("Dist: " + Math.round(event.getPlayer().getLocation().distance(lockedmissile.pos.toLocation(event.getPlayer().getWorld())))).color(ChatColor.GOLD);
                    event.getPlayer().playNote(event.getPlayer().getLocation(), Instrument.STICKS, Note.flat(1, Note.Tone.E));
                    event.getPlayer().playNote(event.getPlayer().getLocation(), Instrument.GUITAR, Note.flat(1, Note.Tone.F));
                    event.getPlayer().playNote(event.getPlayer().getLocation(), Instrument.FLUTE, Note.flat(1, Note.Tone.E));
                }
                // SendScanLine
                event.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, scanline.create());
                // Check if sneaking
                if (!event.getPlayer().isSneaking()) {
                    if (lockedmissile == null) {
                        event.getPlayer().sendMessage(Translations.get("messages.manpad.notarget"));
                        this.cancel();
                    } else {
                        active.remove(event.getPlayer());
                        MissileController missile = new MissileController(false, event.getPlayer().getLocation().toVector(), lockedmissile.pos, 5, event.getPlayer().getWorld(), 3, 0, 1, event.getPlayer().getLocation().getDirection());
                        missile.FireMissileAtMissile(lockedmissile);
                        event.getPlayer().getInventory().remove(manpad.getItem());
                        event.getPlayer().updateInventory();
                        this.cancel();
                    }
                }
            }
        }.runTaskTimer(MissileWarfare.getInstance(), 0, 2);
    }
/*else {
            //DEBUGGING TOOL, IF IN RELEASED BUILD CONTACT ME IMMEDIATELY.
            MissileController missile = new MissileController(true, event.getPlayer().getLocation().toVector(), event.getPlayer().getLocation().toVector(), 5, event.getPlayer().getWorld(), 5, 5, 1);
            MissileWarfare.activemissiles.add(missile);
            event.getPlayer().sendMessage("Spawned debug missile at : "+missile.pos);
        }*/
}
Also used : BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MissileController(me.kaiyan.missilewarfare.Missiles.MissileController) ArrayList(java.util.ArrayList) List(java.util.List) SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem) Vector(org.bukkit.util.Vector) ComponentBuilder(net.md_5.bungee.api.chat.ComponentBuilder) Location(org.bukkit.Location)

Aggregations

MissileController (me.kaiyan.missilewarfare.Missiles.MissileController)6 SlimefunItem (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem)3 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)3 Vector (org.bukkit.util.Vector)3 BlockPlaceHandler (io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler)2 List (java.util.List)2 Config (me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config)2 BlockTicker (me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker)2 NamespacedKey (org.bukkit.NamespacedKey)2 Block (org.bukkit.block.Block)2 TileState (org.bukkit.block.TileState)2 BlockPlaceEvent (org.bukkit.event.block.BlockPlaceEvent)2 PersistentDataContainer (org.bukkit.persistence.PersistentDataContainer)2 SlimefunItemStack (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack)1 BlockDispenseHandler (io.github.thebusybiscuit.slimefun4.core.handlers.BlockDispenseHandler)1 Config (io.github.thebusybiscuit.slimefun4.libraries.dough.config.Config)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 MissileConfig (me.kaiyan.missilewarfare.Missiles.MissileConfig)1 ComponentBuilder (net.md_5.bungee.api.chat.ComponentBuilder)1