Infinite Firework Rockets

Discussion in 'Spigot Plugin Development' started by jmilthedude, Jul 26, 2018.

  1. Hi. I am trying to make it so that a player always has a Firework Rocket in Slot 1. So that when they use it while flying, it gets replaced. I have tried PlayerInteractEvent which is not working for me. Also tried PlayerItemConsumeEvent. Has anyone tried to do this or know what I should utilize?

    This is what I have right now..
    Code (Java):
        @EventHandler(priority = EventPriority.LOWEST)
        public void onRocket(PlayerInteractEvent event) {
            Material inHand = event.getItem().getType();
            Action action = event.getAction();
            Player player = event.getPlayer();
            if (inHand != null) {
                if (inHand == Material.FIREWORK_ROCKET && action == Action.RIGHT_CLICK_BLOCK) {
                    event.setCancelled(true);
                }

                if (inHand == Material.FIREWORK_ROCKET && action == Action.RIGHT_CLICK_AIR) {
                    player.getInventory().setItemInMainHand(rocket);
                }
            }
        }
     
  2. Essentials has an unlimited function which sounds similar to what you want to achieve, perhaps it's worth looking at their source and seeing how they do it.

    Here is where they seem to modify item stacks:
    https://github.com/EssentialsX/Esse...e/essentials/EssentialsBlockListener.java#L50
    (Should jump to line 50)

    For throwing eggs and using buckets: https://github.com/EssentialsX/Esse...essentials/EssentialsPlayerListener.java#L426
    (Should jump to line 426)

    I'd guess you just need to call updateInventory() after modifying it.
     
  3. Hmm, none of that is working for me. He is using the block placed event, which will not work for fireworks. And the other is using the events relative to eggs thrown and buckets used. They have their own events. I'm wondering if I should just use a runnable check the inventory slot constantly and replace the rocket if it's missing. That is not efficient though and could cause some lag I believe..
     
  4. For your case, I'd imagine that PlayerInteractEvent would work fine. Check if the item stack in hand is a firework and that the event Action was RIGHT_CLICK_BLOCK or RIGHT_CLICK_AIR, then you can either modify the in-hand item stack then update the inventory.

    If I recall, some events may have issues with you modifying the inventory during them, so to circumvent those situations, you can schedule a delayed task with a 1 tick delay, then modify the item stack.

    Keep in mind that if you need to use a delayed task, you may need to store a copy of the item stack in case the item isn't there by the time the delayed task is ran.