HyperSMP / EggHuntPlugin

Minecraft plugin adding the ability to track and steal the Dragon Egg from other players
6 stars 3 forks source link

[BUG] Egg holded on second hand does not drop when player logs off #14

Open DrRek opened 2 months ago

DrRek commented 2 months ago

Describe the bug Any player can put the eng in the second (left hand) and disconnect from the game. The result is that the egg is not dropped and stays in the inventory

To Reproduce Log in Put the egg in the second hand selecting it and pressing F Log out

Expected behavior The egg is removed from the second hand and dropped on the ground

Actual behavior The egg stays in the invenotry of the logged out user

Version Information:

If applicable, also attach the following information:

Configuration and logs: If applicable, attach the config.yml and data.yml files inside the /plugins/EggHunt/ directory, which is generated when the plugin is first run.

#If enabled, logs extra debugging information to the console. This information will appear in logs regardless of debug setting.
debug: false

#Name of your end world, usually to "world_the_end" on spigot
end: world_the_end

#Whether the egg teleporting causes the owner to no longer count as the owner of the egg
reset_owner: true

#Whether the egg should be invulnerable to damage
egg_inv: true
#Whether the egg should respawn after it is destroyed
resp_egg: true
#Whether the egg respawns immediately (true) or after a new dragon is spawned and killed (false) after the egg is destroyed
resp_imm: false

#Whether to drop any eggs that players have in their ender chest onto the ground.
#Setting this to false will prevent the player from removing that egg from their ender chest.
drop_enderchested_egg: false

#Whether to apply an entity tag to the egg owner (works like `/tag <playerName> add <tagName>`)
#Tag is updated when egg ownership changes and on player login
tag_owner: true
#The name of the tag to apply. Selected like: `/tell @a[tag=<tagName>] <message>`
#If changing this while the egg is claimed, remove the old tag by running `/tag <playerName> remove <oldTagName>`
owner_tag_name: eggOwner

#Whether to create and update scoreboards for time holding the dragon egg
#Scoreboards are updated when the egg changes ownership and when the world saves
#When editing the scores, modify the board tracking seconds. Other times are calculated based on that time
keep_score: true
#Whether entities with custom names are counted on the scoreboard instead of the owner when holding the egg
named_entities_keep_score: false

Also attach any relevant log files. Do not upload zipped log files (ending in .gz), first unzip the log file(s) inside with a utility like (7-zip)[https://www.7-zip.org/].

Screenshots If applicable, attach screenshots to help explain the issue.

Additional context Add any other relevant context about the problem here.

DrRek commented 2 months ago

Hotfix I've implemented in my server:

public static void dropEgg(Player player, Data data, Configuration config) {

        Boolean hadToRemoveFromInventory = false;

        // If player has dragon egg in the inventory it gets removed
        if (player.getInventory().contains(Material.DRAGON_EGG)) {
            // Set owner and remove
            data.setEggOwner(player, config); //TODO is this necessary? player will likely already be owner
            player.getInventory().remove(Material.DRAGON_EGG);
            hadToRemoveFromInventory = true;
        }

        // If player has dragon egg in second hand it gets removed
        if(player.getInventory().getItemInOffHand().getType().equals(Material.DRAGON_EGG)){
            // Set owner and remove
            data.setEggOwner(player, config); //TODO is this necessary? player will likely already be owner
            player.getInventory().setItemInOffHand(null);
            hadToRemoveFromInventory = true;
        }

        // If egg has been removed from the player it gets to spawn on the floor
        if(hadToRemoveFromInventory){
            // Drop it on the floor and set its location
            //TODO use drop egg method in EggRespawn
            Item eggDrop = player.getWorld().dropItem(player.getLocation(),
                    new ItemStack(Material.DRAGON_EGG));
            eggDrop.setVelocity(new Vector(0, 0, 0));
            data.updateEggLocation(eggDrop);
        }
    }

Not making a pr because I'm unsure about the impact on other server version