[1.18.2] Item Frame Packet Problems

Discussion in 'Plugin Development' started by BaddCamden, Aug 3, 2024.

Thread Status:
Not open for further replies.
  1. Offline

    BaddCamden

    So, I am trying to send a packet containing an Item Frame with a map on it, however two issues have occured.
    1. The itemframe won't face anywhere but down. I have tried setting it's block face, rotation (floats), and at several other indexes however nothing works.

    2. The map on the itemframe does not appear immediately, i have to middle click it in creative for it to appear.


    This is my code:

    Code:
            private void sendFramePacket(Location location, int[] rgb) {
                try {
                    UUID uniqueId = entityIds.computeIfAbsent(location, loc -> {
                        UUID newId = UUID.randomUUID();
                        Bukkit.getLogger().info("Generating new UUID for location " + loc + ": " + newId);
    
                        // Spawn the item frame without specifying the direction here
                        PacketContainer spawnPacket = protocolManager.createPacket(PacketType.Play.Server.SPAWN_ENTITY);
                        spawnPacket.getIntegers().write(0, newId.hashCode()); // Entity ID
                        spawnPacket.getDoubles().write(0, loc.getX()).write(1, loc.getY()).write(2, loc.getZ());
                       
                        spawnPacket.getEntityTypeModifier().write(0, EntityType.ITEM_FRAME); // Entity type
                        spawnPacket.getUUIDs().write(0, newId); // Entity UUID
    
    
                        Bukkit.getOnlinePlayers().forEach(player -> {
                            try {
                                protocolManager.sendServerPacket(player, spawnPacket);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        });
                        Bukkit.getLogger().info("Spawned item frame at " + loc + " with UUID: " + newId);
                        return newId;
                    });
    
                    // Metadata packet to update the item frame with the map
                    PacketContainer metadataPacket = protocolManager.createPacket(PacketType.Play.Server.ENTITY_METADATA);
                    metadataPacket.getIntegers().write(0, uniqueId.hashCode());
    
                    WrappedDataWatcher watcher = new WrappedDataWatcher();
                    WrappedDataWatcher.Serializer itemSerializer = WrappedDataWatcher.Registry.getItemStackSerializer(false);
    
                    // Add map to item frame
                    ItemStack mapItem = createColoredMap(rgb);
                    WrappedDataWatcher.WrappedDataWatcherObject itemFrameIndex = new WrappedDataWatcher.WrappedDataWatcherObject(8, itemSerializer);
                    watcher.setObject(itemFrameIndex, mapItem);
    
                    metadataPacket.getWatchableCollectionModifier().write(0, watcher.getWatchableObjects());
    
                    Bukkit.getOnlinePlayers().forEach(player -> {
                        try {
                            protocolManager.sendServerPacket(player, metadataPacket);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });
                    Bukkit.getLogger().info("Updated item frame metadata at " + location);
                } catch (Exception e) {
                    Bukkit.getLogger().severe("Failed to send packet at " + location + ": " + e.getMessage());
                    e.printStackTrace();
                }
            }
    
     
Thread Status:
Not open for further replies.

Share This Page