Spawning multiple experience orbs at the same location not working

Discussion in 'Plugin Development' started by Programie, Jan 27, 2024.

  1. Offline

    Programie

    I've a plugin which should transfer the player's current experience into experience orb to allow the player to repair mending tools using their already collected experience.

    The plugin works by placing a sign and right clicking with the mending tool on it. That's also working as expected. For example, 10 XP is removed from the player and added to a spawned experience orb.

    But the right click event is triggered two times when sneaking while right clicking the sign. First I thought that was a bug, but then realized that the event is triggered for both hands (primary hand and off hand).

    Those two events result in also spawning two experience orbs at the exact same location which does not work. The second experience orb destroys the first one for some reason.

    I was also able to reproduce the issue by spawning a single experience orb a few blocks away from my player (to prevent picking it up automatically) in a command. Executing that command multiple times while not moving my player clearly shows that the new experience orb is colliding with the previously spawned one and also destroys the previous one. Each experience orb only has one XP, so executing the command 5 times should give 5 XP to the collecting player. But after spawning it multiple times and collecting the orb it only gives one XP point. So, the experience orbs are not merging but replacing each other.

    Example code to be used inside a plugin class:

    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player player = (Player) sender;
    
            Location location = player.getLocation();
    
            location.add(10, 0, 0);
    
            player.getWorld().spawn(location, ExperienceOrb.class).setExperience(1);
    
            return true;
        }
    
    I also tried to reproduce that issue using the /summon command, but in that case the experience orbs are merging the XP if executed multiple times:

    Code:
    /summon minecraft:experience_orb ~10 ~ ~ {Value:1}
    
    I've tested that with the latest Minecraft release (1.20.4) using Spigot as well as Paper.

    For me, this looks like a bug in Bukkit as the /summon command is not affected.
     
    Last edited by a moderator: Jan 27, 2024
  2. Offline

    EvilWitchdoctor

    It could still be a Spigot/Paper bug, since I assume when the sign is clicked the two orbs spawn in the same gametick, whereas the /summon command is probably getting run with a 1gt+ delay between. Hard to say though - could in fact be some kind of optimization bug.

    However, if you want to do a bug report, you will need to do it over at https://hub.spigotmc.org/jira/
    (The Spigot team maintains Craftbukkit - the forum you posted on here is just plugin dev support)

    If I misinterpreted your post, and you were just asking how to prevent the double-sign-click event, then like you noticed it seems to happen for each hand, so you could just check which hand at the top of the function and ignore the event for non-main-hand clicks.
     
  3. Offline

    Programie

    As mentioned in my first post, this doesn't seem to be related to the ticks. My example command only spawns one experience orb per execution. Executing that command a few times as a player, results in replacing the previously spawned orb. It even happens if I wait some time between each command execution.

    The spawning of the orbs is working fine if I spawn them a few blocks away from each other. Then it's even working if I spawn two orbs in the same tick. A few block means around 5 blocks away from each other. Spawning one orb and spawning a second one just one or two blocks away results in the same issue. Therefore, I think your guess about some kind of optimization bug could be right. I could think about Spigot/Paper is merging them to prevent too many orbs at the same location but for some reason the stored experience points are not added.

    And no, you did not misinterpreted my post. I don't want to prevent the duplicate event. First I thought that was a bug, but then realized it is triggered for the off hand, too. As long as both spawned orbs are collected by the player again, I don't have any issue with that. Right now, this bug causes the player to loose their XP on each click.

    So I guess, I should open a bug report in the Spigot issue tracker.

    EDIT: I've reported the bug in the Spigot issue tracker: SPIGOT-7578
     
    Last edited: Jan 30, 2024

Share This Page