1.17.x Elytra counter blocks

Discussion in 'Spigot Plugin Development' started by ppapower, Jul 21, 2021 at 2:03 AM.

?

sdf

  1. sdf

    3 vote(s)
    100.0%
  2. sdf

    0 vote(s)
    0.0%
  1. I created an Elytra item with Lore (Counter blocks: 0) and want to record the number of blocks flown. For this I use statistics. Here is the code:


    @EventHandler
    public void onElytraFlight(EntityToggleGlideEvent event) {
    boolean isGliding = event.isGliding();
    Player player = (Player) event.getEntity();
    ItemStack item = player.getInventory().getChestplate();

    if(item.getType() == Material.ELYTRA && item.getItemMeta().getLocalizedName().equals("counter_elytra")) {

    if(isGliding) {
    firstDist = player.getStatistic(Statistic.AVIATE_ONE_CM);
    player.sendMessage("IS GLIDING: [firstDist = " + firstDist + "]");
    event.getEntity().sendMessage("Gliding on elytra");
    } else {
    lastDist = player.getStatistic(Statistic.AVIATE_ONE_CM);
    player.sendMessage("IS NOT GLIDING: [lastDist = " + lastDist + "]");
    currDist = lastDist - firstDist;
    player.sendMessage("IS NOT GLIDING: [CurrDist = " + currDist + "]");
    ItemMeta itemMeta = item.getItemMeta();
    List<String> itemLore = item.getItemMeta().getLore();
    int currentTree = Integer.parseInt(itemLore.get(0).split(" ")[2]) + currDist;
    player.sendMessage("IS NOT GLIDING: [lore is = " + Integer.parseInt(itemLore.get(0).split(" ")[2]) + "]");
    itemLore.set(0, "§6Blocks passed: " + currentTree);
    player.sendMessage("IS NOT GLIDING: [new lore amount is = " + itemLore.get(0) + "]");
    itemMeta.setLore(itemLore);
    item.setItemMeta(itemMeta);

    event.getEntity().sendMessage("Not gliding on elytra");
    }

    }
    }

    How i can realize the counter?
     

  2. What is the issue with the current code? I can't see exactly what your question is.
     
  3. Firstly, please post code using a bin pr using the code tag as doing it like this is not readable.
    Secondly, the event is throwed when an entity toggles glide, meaning on actovation and deactivation, I suggest using the move event and check if entity is gliding then, figure out of getFrom and getTo are different blocks and of so increment the counter. If you need me to go in more detail just tell me what you dont understand