Paste #39439: Create TNT weapon

Date: 2017/01/29 14:53:58 UTC-08:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


public class Demoman implements Kit, Listener {
    private static ItemStack FIREWORK = new ItemManager(Material.FIREWORK).name(SimpleUtils.color("&a&lRocket")).build();
    private static ItemStack MIRV = new ItemManager(Material.FIREBALL).amount(2).name(SimpleUtils.color("&a&lMIRV")).build();
    private HashMap<UUID, String> mirvs = new HashMap<>();
    private ArrayList<UUID> do_no_explode = new ArrayList<>();
    private ConfigurationSection classConfiguration;

    public Demoman() {
        classConfiguration = Shift.plugin.getConfig().getConfigurationSection("demoman");
    }

    @EventHandler
    public void onExplode(EntityExplodeEvent e) {
        Entity e1 = e.getEntity();
        e.blockList().clear();
        if (do_no_explode.contains(e1.getUniqueId())) {
            e.setYield((float) classConfiguration.getDouble("tnt-power", 4));
            do_no_explode.remove(e.getEntity().getUniqueId());
        }
    }

    @EventHandler
    public void onFireballHit(ProjectileHitEvent e) {
        Entity e1 = e.getEntity();
        if (Shift.gs != GameState.STARTED) {
            e1.remove();
        }
        if (!(e1 instanceof Fireball)) {
            return;
        }
        Fireball fb = (Fireball) e1;
        System.out.println("MIRVS: " + mirvs);
        if (mirvs.containsKey(e1.getUniqueId())) {
            Player p = Bukkit.getPlayer(mirvs.remove(e1.getUniqueId()));
            if (p != null) {
                MIRV(fb.getLocation(), p);
                e.getEntity().remove();
            }
        }
    }

    @EventHandler
    public void onRightClick(PlayerInteractEvent e) {
        if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
            Player p = e.getPlayer();
            ItemStack is = p.getItemInHand();
            if (is.hasItemMeta() && is.getItemMeta().hasDisplayName()) {
                if (is.getItemMeta().getDisplayName().equals(FIREWORK.getItemMeta().getDisplayName())) {
                    e.setCancelled(true);
                    return;
                }
            }
        }

        if (e.getAction() == Action.RIGHT_CLICK_AIR) {
            Player p = e.getPlayer();
            ItemStack is = p.getItemInHand();
            if (is.hasItemMeta() && is.getItemMeta().hasDisplayName()) {
                if (is.getItemMeta().getDisplayName().equals(FIREWORK.getItemMeta().getDisplayName())) {
                    e.setCancelled(true);
                    if (Shift.gs != GameState.STARTED) {
                        return;
                    }
                    is.setAmount(is.getAmount() - 1);
                    p.getInventory().setItemInMainHand(is);
                    Vector v = p.getLocation().getDirection().multiply(1.3);
                    Entity e2 = p.getWorld().spawnEntity(p.getLocation(), EntityType.PRIMED_TNT);
                    e2.setVelocity(v);
                    e2.setMetadata("thrower", new FixedMetadataValue(Shift.plugin, p.getName()));
                    ((TNTPrimed) e2).setFuseTicks(20);
                    do_no_explode.add(e2.getUniqueId());

                } else if (is.getItemMeta().getDisplayName().equals(MIRV.getItemMeta().getDisplayName())) {
                    e.setCancelled(true);
                    if (Shift.gs != GameState.STARTED) {
                        return;
                    }
                    is.setAmount(is.getAmount() - 1);
                    p.getInventory().setItemInMainHand(is);
                    Location l = p.getLocation();
                    l.add(0, 3, 0);
                    Entity fb1 = l.getWorld().spawnEntity(l, EntityType.FIREBALL);
                    Fireball fireball = (Fireball) fb1;

                    fireball.setIsIncendiary(false);
                    fireball.setBounce(false);
                    fireball.setDirection(new Vector().zero());
                    fireball.setCustomNameVisible(true);
                    fireball.setCustomName(SimpleUtils.color("&eMIRV -- &a" + p.getName()));
                    mirvs.put(fireball.getUniqueId(), p.getName());
                    fireball.setDirection(p.getLocation().getDirection());

                }
            }
        }
    }

    private void MIRV(Location l, Player p) {
        Location location = l;

        List<Vector> vectorList = new ArrayList<>();

        if (classConfiguration != null && classConfiguration.contains("tnt-velocities")) {
            for (String configVector : classConfiguration.getStringList("tnt-velocities")) {
                if (configVector.contains(":")) {
                    try {
                        double x, y, z;

                        x = Double.parseDouble(configVector.split(":")[0]);
                        y = Double.parseDouble(configVector.split(":")[1]);
                        z = Double.parseDouble(configVector.split(":")[2]);

                        vectorList.add(new Vector(x, y, z));
                    } catch (NumberFormatException ignore) {
                        ignore.printStackTrace();
                        System.out.println(configVector);
                    }
                }
            }
        }

        Vector[] vs = new Vector[]{
                new Vector(0, 0, -1),
                new Vector(1, 0, 0),
                new Vector(-1, 0, 0),
                new Vector(0, 0, 1),
                new Vector(1, 0, -1),
                new Vector(-1, 0, -1),
                new Vector(1, 0, 1),
                new Vector(-1, 0, 1)
        };

        if (vectorList.isEmpty()) {
            Collections.addAll(vectorList, vs);
            System.out.println("Demoman had no configurable vectors, using default values.");
        }

        int fuse = classConfiguration.getInt("tnt-tick-fuse", 20);

        for (Vector v : vectorList) {
            Entity entity = l.getWorld().spawnEntity(location, EntityType.PRIMED_TNT);
            entity.setVelocity(v);
            TNTPrimed tntPrimed = (TNTPrimed) entity;
            tntPrimed.setFuseTicks(fuse);
            tntPrimed.setMetadata("thrower", new FixedMetadataValue(Shift.plugin, p.getName()));
            do_no_explode.add(tntPrimed.getUniqueId());
        }
    }

    @Override
    public HashMap<Integer, ItemStack> getSlottedItems(Integer level) {
        return null;
    }

    @Override
    public ArrayList<ItemStack> getNormalItems(Integer level) {
        ArrayList<ItemStack> items = new ArrayList<>();

        ItemStack woodPickaxe = new ItemStack(Material.WOOD_PICKAXE);
        woodPickaxe.setAmount(classConfiguration.getInt(level + ".woodPickaxe", 0));

        ItemStack firework = FIREWORK.clone();
        firework.setAmount(classConfiguration.getInt(level + ".rocket", 0));

        ItemStack mirv = MIRV.clone();
        mirv.setAmount(classConfiguration.getInt(level + ".mirv", 0));

        items.add(woodPickaxe);
        items.add(firework);
        items.add(mirv);

        return items;
    }

    @Override
    public Integer getMaxLevel() {
        return 3;
    }

    @Override
    public Integer getPrice(Integer level) {
        if (level == 1) {
            return 15000;
        } else if (level == 2) {
            return 40000;
        } else if (level == 3) {
            return 100000;
        } else {
            return null;
        }
    }

    @Override
    public String getName() {
        return "DEMOMAN";
    }

    @Override
    public String getDisplayName(Integer level) {
        if (level == 0) {
            return SimpleUtils.color("&9Demoman");
        }
        return SimpleUtils.color("&9Demoman &a" + level);
    }

    @Override
    public Material getGUIItem() {
        return Material.TNT;
    }

    @Override
    public String getDisplayLore() {
        return SimpleUtils.color("&aAre explosions your thing? Get a wooden pick, and enough explosives to be a master creeper in no time.");
    }
}