Local code

Discussion in 'Spigot Plugin Development' started by ItsRealGaming, Jul 8, 2019.

  1. Hi, im a newbie at Plugin programming... I need you to help me something about local code and local variable value for entity item (other word, each entity item have different variable value). In this case, my variable value here is a timer. Here is my code:
    Code (Text):
    package tk.realityprogrammer.cataclysm.EventClass;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;

    import org.bukkit.entity.Player;
    import org.bukkit.scheduler.BukkitRunnable;

    import java.util.ArrayList;
    import java.util.List;

    import static org.bukkit.Bukkit.getServer;

    public class EventClass implements Listener {

        List<Item> cataclysm = new ArrayList<>();
        List<Item> handgrenade = new ArrayList<>();

        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();

            /**  Cataclysm */
            if (player.hasPermission("cataclysm.throwBomb.Cataclysm") || player.isOp()) {
                if (event.hasItem() && event.getItem().getType() == Material.TNT && player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.DARK_RED + "Cataclysm") && (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                    event.setCancelled(true);
                    Item item = event.getPlayer().getWorld().dropItem(event.getPlayer().getEyeLocation(), new ItemStack(Material.TNT));
                    item.setVelocity(event.getPlayer().getEyeLocation().getDirection());
                    item.setPickupDelay(10000);
                    cataclysm.add(item);
                    ItemStack itemStack = event.getItem().clone();
                    if (!player.isOp())
                        itemStack.setAmount(itemStack.getAmount() - 1);
                    event.getPlayer().setItemInHand(itemStack);
                }

                if ((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) && event.hasItem() && event.getItem().getType() == Material.BRICK && player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "Hand Detonater")) {
                    for (Item item : cataclysm) {
                        item.getWorld().createExplosion(item.getLocation(), 5);
                        item.remove();
                    }
                    cataclysm.clear();
                }
            }

            /**  Hand Grenade */
            if (player.hasPermission("cataclysm.throwBomb.HandGrenade") || player.isOp()) {
                if (event.hasItem() && event.getItem().getType() == Material.GREEN_WOOL && player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.DARK_GREEN + "Hand Grenade (Safe)") && (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                    event.setCancelled(true);
                    Item item = event.getPlayer().getWorld().dropItem(event.getPlayer().getEyeLocation(), new ItemStack(Material.GREEN_WOOL));
                    item.setVelocity(event.getPlayer().getEyeLocation().getDirection().multiply(2));
                    item.setPickupDelay(10000);
                    item.setCustomName("Exploding percentage: 0%");
                    item.setCustomNameVisible(true);
                    handgrenade.add(item);
                    if (!player.isOp()) {
                        ItemStack itemStack = event.getItem().clone();
                        itemStack.setAmount(itemStack.getAmount() - 1);
                        event.getPlayer().setItemInHand(itemStack);
                    }
                }
            }
        }

        public void runTick() {

            getServer().getScheduler().scheduleSyncRepeatingTask((Plugin) this,new BukkitRunnable() {
                private int timer = 0;

                @Override
                public void run() {
                    timer++;

                    for (Item item : handgrenade) {
                        item.setCustomName("Exploding percentage: " + 100 / 120 * timer);
                        item.setCustomNameVisible(true);
                    }
                    for (Player player : Bukkit.getOnlinePlayers()) {
                        player.sendMessage("Timer: " + timer);
                    }

                    if (timer >= 120) {
                        this.cancel();

                        for (Item item : handgrenade) {
                            item.getWorld().createExplosion(item.getLocation(), 3);
                            item.remove();
                        }
                        handgrenade.clear();
                    }
                }
            },0L,20L);
        }
    }
    What I want to fix is the Hand Grenade item, after throw it, it will explode after 6 second / 120 tick. I don't want all thrown Grenade have the same value timer, that would be broken...

    P/s: Don't mind my English and the Cataclysm TNT part :3
    P/s 2: Don't kill me for my knowledge
     
    #1 ItsRealGaming, Jul 8, 2019
    Last edited: Jul 8, 2019
  2. Or I'm suck at timer...
     
  3. Make a map, put the item as a key and then the timer as the value.
     
  4. What do u mean, I don't understand that much ._.
     
  5. Create a Map<Item, Integer>, then put the item in the map with the value being the timer for that item. Use that instead of a local int.
    Example:
    Code (Java):
    // Use this instead of your int 'timer'
    Map<Item, Integer> timers = new HashMap<>();

    // Do this when you create the grenade
    timers.put(item, 0);

    // Then inside your runnable
    timers.put(item, timers.get(item)+1);
    Note: That's just an example, the rest is up to you.
     
  6. Could you rearrange this for me?
    Code (Text):
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();

            /**  Hand Grenade */
            if (player.hasPermission("cataclysm.throwBomb.HandGrenade") || player.isOp()) {
                if (event.hasItem() && event.getItem().getType() == Material.GREEN_WOOL && player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.DARK_GREEN + "Hand Grenade (Safe)") && (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                    event.setCancelled(true);
                    Item item = event.getPlayer().getWorld().dropItem(event.getPlayer().getEyeLocation(), new ItemStack(Material.GREEN_WOOL));
                    item.setVelocity(event.getPlayer().getEyeLocation().getDirection().multiply(2));
                    item.setPickupDelay(10000);
                    item.setCustomName("Exploding percentage: 0%");
                    item.setCustomNameVisible(true);
                    handgrenade.add(item);
                    timers.put(item, 0);
                    if (!player.isOp()) {
                        ItemStack itemStack = event.getItem().clone();
                        itemStack.setAmount(itemStack.getAmount() - 1);
                        event.getPlayer().setItemInHand(itemStack);
                    }
                }
            }
        }

        public void runTick() {

            getServer().getScheduler().scheduleSyncRepeatingTask((Plugin) this,new BukkitRunnable() {
                Map<Item, Integer> timers = new HashMap<>();

                @Override
                public void run() {
                    for (Item item : handgrenade) {
                        timers.put(item, timers.get(item) + 1);
                    }

                    for (Item item : handgrenade) {
                        item.setCustomName("Exploding percentage: " + 100 / 120 * timers);
                        item.setCustomNameVisible(true);
                    }
                    for (Player player : Bukkit.getOnlinePlayers()) {
                        player.sendMessage("Timer: " + timers);
                    }

                    if (timers >= 120) {
                        this.cancel();

                        for (Item item : handgrenade) {
                            item.getWorld().createExplosion(item.getLocation(), 3);
                            item.remove();
                        }
                        handgrenade.clear();
                    }
                }
            },0L,20L);
        }
    I'm sorry for my dumb brain...
     
  7. Create the map outside of the runnable, otherwise you won't be able to set the timer when you create the grenade.
    Put the timer check inside your loop and then get the timer using 'timers.get(item)'
    Don't cancel the runnable when the time is done, just remove the grenade from the 'timers' map and 'handgrenade' list.
     
  8. Oh... Thank you so much for helping me, and pardon me for my dumbness :3
     
  9. Wait... It didn't run... I just give you all my code here ._.
    Code (Text):
    package tk.realityprogrammer.cataclysm.EventClass;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;

    import org.bukkit.scheduler.BukkitRunnable;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    public class EventClass implements Listener {

        List<Item> cataclysm = new ArrayList<>();
        List<Item> handgrenade = new ArrayList<>();

        Map<Item, Integer> timers = new HashMap<>();

        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();

            /**  Cataclysm */
            if (player.hasPermission("cataclysm.throwBomb.Cataclysm") || player.isOp()) {
                if (event.hasItem() && event.getItem().getType() == Material.TNT && player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.DARK_RED + "Cataclysm") && (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                    event.setCancelled(true);
                    Item item = event.getPlayer().getWorld().dropItem(event.getPlayer().getEyeLocation(), new ItemStack(Material.TNT));
                    item.setVelocity(event.getPlayer().getEyeLocation().getDirection());
                    item.setPickupDelay(10000);
                    cataclysm.add(item);
                    ItemStack itemStack = event.getItem().clone();
                    if (!player.isOp())
                        itemStack.setAmount(itemStack.getAmount() - 1);
                    event.getPlayer().setItemInHand(itemStack);
                }

                if ((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) && event.hasItem() && event.getItem().getType() == Material.BRICK && player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.RED + "Hand Detonater")) {
                    for (Item item : cataclysm) {
                        item.getWorld().createExplosion(item.getLocation(), 5);
                        item.remove();
                    }
                    cataclysm.clear();
                }
            }

            /**  Hand Grenade */
            if (player.hasPermission("cataclysm.throwBomb.HandGrenade") || player.isOp()) {
                if (event.hasItem() && event.getItem().getType() == Material.GREEN_WOOL && player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.DARK_GREEN + "Hand Grenade (Safe)") && (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                    event.setCancelled(true);
                    Item item = event.getPlayer().getWorld().dropItem(event.getPlayer().getEyeLocation(), new ItemStack(Material.GREEN_WOOL));
                    item.setVelocity(event.getPlayer().getEyeLocation().getDirection().multiply(2));
                    item.setPickupDelay(10000);
                    item.setCustomName("Exploding percentage: 0%");
                    item.setCustomNameVisible(true);
                    handgrenade.add(item);
                    timers.put(item, 0);
                    if (!player.isOp()) {
                        ItemStack itemStack = event.getItem().clone();
                        itemStack.setAmount(itemStack.getAmount() - 1);
                        event.getPlayer().setItemInHand(itemStack);
                    }
                }
            }
        }

        public void runTick() {

            new BukkitRunnable() {

                @Override
                public void run() {
                    for (Item item : handgrenade) {
                        timers.replace(item, timers.get(item) + 1);

                        item.setCustomName("Exploding percentage: " + 100 / 120 * timers.get(item));
                        item.setCustomNameVisible(true);

                        if (timers.get(item) >= 120) {
                            item.getWorld().createExplosion(item.getLocation(), 3);
                            item.remove();
                            handgrenade.clear();
                            timers.remove(item);
                        }
                    }
                    cancel();
                }
            }.runTaskTimer((Plugin) this, 1, 1);
        }
    }
    I change because the previous doesn't run or I don't know how to get it to work...

    Ps: That just the EventClass (Listener class), the main class is only contain crafting recipe of the TNT
     
    #9 ItsRealGaming, Jul 8, 2019
    Last edited: Jul 8, 2019
  10. You are cancelling the runnable after the first run.