Search code examples
javaeclipseminecraftbukkitspigot

Assigning a bukkit runnable task to a Minecraft player using a hashmap


I have the following code.

I am trying to:

  1. Assign a bukkit runnable task to a given ID
  2. Assign a player a given ID
  3. Place these two IDs into a hashmap, where each participant is matched to their respective bukkit runnable task

The repeating task should assign a maximum of 4 objects to a given player's inventory, assigning one object every minute.

This means that for each player, the repeating task should last a maximum of 4 minutes and should be cancelled when the counter exceeds the length of the hashmap.

However, I get the issue 'the local variable task may not have been initialised'.

I know that this means that I should initialise the variable 'task', but I am not sure how to do so, given that the variable task corresponds to the bukkit runnable task?

I would be so grateful for a helping hand!

    Map<UUID, Integer> map = new HashMap<UUID, Integer>();
    List<ItemStack> items = java.util.Arrays.asList(
            new ItemStack(Material.WATER),
            new ItemStack(Material.COBWEB),
            new ItemStack(Material.CAKE),
            new ItemStack(Material.RED_WOOL)
        );

    @EventHandler
    public void on(PlayerQuitEvent event) {
    map.remove(event.getPlayer());
        }
       
    @EventHandler
    public void on(PlayerInteractEvent event) {
            final ItemStack item = event.getItem();
            if (item.getType() == Material.WHITE_WOOL) {
                BukkitTask task = getServer().getScheduler().runTaskTimer(this, () -> {
                    if(this.stopRepeater) {
                        int counter = 0; 
                        while (counter <= 4){
                           Material[] listofitems = {Material.WATER, Material.COBWEB, Material.CAKE, Material.SNOW};
                           int idx = counter; 
                           Material randomItem = listofitems[idx];
                           ItemStack items = new ItemStack(randomItem); 
                           Player thePlayer = event.getPlayer();
                           thePlayer.getInventory().addItem(items);
                           map.put(event.getPlayer().getUniqueId(),task.getTaskId()); 
                           counter ++; 
                           if (counter >= map.size()) {
                               Bukkit.getServer().getScheduler().cancelTask(task.getTaskId());
                           }
                        }
                    } 
             }, 20 * 60, 20 * 60);
            }
        }      

Solution

  • Thank you for your help!

    I have come up with the following solution:

        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
          new BukkitRunnable() {
            int count = 0;
            Material[] listofitems = {Material.WATER_BUCKET, Material.CACTUS, Material.CAKE, Material.SNOW};
    
            public void run() {
              if (count == listofitems.length-1) cancel();
              Material nextItem = listofitems[count];
              ItemStack item = new ItemStack(nextItem);
              event.getPlayer().getInventory().addItem(item);
              count++;
            }
          }.runTaskTimer(this, 1200L, 1200L);
        }
    }
    v