Solved EntityItem spawns at random direction

Discussion in 'Spigot Plugin Development' started by KarmaConfigs, Jun 19, 2021.

  1. Hi, I know I could just spawn the entity using spigot API without reflection, and setGravity to false, but I can't because I need this to be this per-player, and not server globally, the problem is that when I spawn the EntityItem ( using reflection ), the entity will spawn being thrown at random direction, let me show you:



    The item should be showing like if it was floating on ground

    Code:


    Code (Java):
    /**
    * Summon an item
    *
    * @param item the item to add
    * @param location the armor stand location
    * @param players the player to show to
    */

    private void summonItem(final ItemStack item, final Location location, final Player...players) {
        VersionUtils utils = new ServerVersion();

        Object itemEntity = utils.createEntity(entityItem, location, item);
        if (itemEntity != null) {
            utils.invoke(itemEntity, "setNoGravity", boolean.class, true);
            utils.invoke(itemEntity, "setPickupDelay", int.class, Integer.MAX_VALUE);

            Object temp_id = utils.invoke(itemEntity, "getId", null);
            if (temp_id != null) {
                int id = (int) temp_id;
                entities.add(id);

                Object packetPlayOutEntityMetadata = utils.createPacket("PacketPlayOutEntityMetadata", new Class[] {
                    int.class, utils.getMinecraftClass("DataWatcher"), boolean.class
                }, id, utils.invoke(itemEntity, "getDataWatcher", null), true);
                Object packetPlayOutSpawnEntity = utils.createPacket("PacketPlayOutSpawnEntity", utils.getMinecraftClass("Entity"), itemEntity);
                for (Player player: players) {
                    hidden.remove(player.getUniqueId());

                    shown.add(player.getUniqueId());
                    utils.invokePacket(player, packetPlayOutSpawnEntity);
                    utils.invokePacket(player, packetPlayOutEntityMetadata);
                }

                location.setY(location.getY() - separation);
            }
        }
    }
     
  2. try setting the items velocity to 0.
    not too sure how your utilities work but maybe something like this

    Code (Java):
    utils.invoke(itemEntity, "setVelocity", Vector.class, new Vector(0, 0, 0));
     
  3. I don't think it will work, as the EntityItem is a minecraft class, and Vector is a bukkit class

    Btw, this is my VersionUtils class ( I made it abstract to make it compatible with old versions of my API )
    md_5 paste - ipezidimuh (md-5.net)
     
    • Winner Winner x 1

  4. Works perfectly, thanks!


    Code (Java):

    /**
     * Summon an item
     *
     * @param item the item to add
     * @param location the item location
     * @param players the player to show to
     */

    private void summonItem(final ItemStack item, final Location location, final Player...players) {
        VersionUtils utils = new ServerVersion();

        Object itemEntity = utils.createEntity(entityItem, location, item);
        if (itemEntity != null) {
            utils.invoke(itemEntity, "setNoGravity", boolean.class, true);
            utils.invoke(itemEntity, "setPickupDelay", int.class, Integer.MAX_VALUE);

            Object temp_id = utils.invoke(itemEntity, "getId", null);
            if (temp_id != null) {
                int id = (int) temp_id;
                entities.add(id);

                Object packetPlayOutSpawnEntity = utils.createPacket("PacketPlayOutSpawnEntity", utils.getMinecraftClass("Entity"), itemEntity);
                Object packetPlayOutEntityVelocity = utils.createPacket("PacketPlayOutEntityVelocity", new Class[] {
                    int.class, utils.getMinecraftClass("Vec3D")
                }, id, utils.createVector3D(0 d, 0 d, 0 d));
                Object packetPlayOutEntityMetadata = utils.createPacket("PacketPlayOutEntityMetadata", new Class[] {
                    int.class, utils.getMinecraftClass("DataWatcher"), boolean.class
                }, id, utils.invoke(itemEntity, "getDataWatcher", null), true);
                for (Player player: players) {
                    hidden.remove(player.getUniqueId());

                    shown.add(player.getUniqueId());
                    utils.invokePacket(player, packetPlayOutSpawnEntity);
                    utils.invokePacket(player, packetPlayOutEntityVelocity);
                    utils.invokePacket(player, packetPlayOutEntityMetadata);
                }

                location.setY(location.getY() - separation);
            }
        }
    }
     
     
    • Like Like x 1