ProtocolLib

3M Downloads

WrappedBlockData.createData() Not working properly on Spigot1.12.2

MiniDay opened this issue ยท 3 comments

commented

Describe the question
WrappedBlockData.createData() Not working properly

API method(s) used
WrappedBlockData.createData

Expected behavior
Let the player's client show a block as sign

Code

        PacketContainer blockChange = protocolManager.createPacket(PacketType.Play.Server.BLOCK_CHANGE);
        blockChange.getBlockPositionModifier().write(0, blockPosition);
        blockChange.getBlockData().write(0,
                WrappedBlockData.createData(Material.WALL_SIGN)
        );
        protocolManager.sendServerPacket(player, blockChange);

Additional context
I've tried many tests, whether it's changing the box position or changing the material(Material.SIGN_POST or Material.SLIME_BLOCK), this method can't be performed correctly. It always throws an NPE.

My minecraft server version is spigot-1.12.2. The version just built using buildtools.

I tried to change the protocollib version. I found that this bug also appeared in 4.5.0 and 4.4.0, but it can be used normally on 4.3.0.

[22:03:25] [Server thread/WARN]: java.lang.NullPointerException
[22:03:25] [Server thread/WARN]: 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[22:03:25] [Server thread/WARN]: 	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[22:03:25] [Server thread/WARN]: 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[22:03:25] [Server thread/WARN]: 	at java.lang.reflect.Method.invoke(Unknown Source)
[22:03:25] [Server thread/WARN]: 	at com.comphenix.protocol.reflect.accessors.DefaultMethodAccessor.invoke(DefaultMethodAccessor.java:16)
[22:03:25] [Server thread/WARN]: 	at com.comphenix.protocol.wrappers.WrappedBlockData$OldBlockData.createOldData(WrappedBlockData.java:238)
[22:03:25] [Server thread/WARN]: 	at com.comphenix.protocol.wrappers.WrappedBlockData$OldBlockData.access$400(WrappedBlockData.java:164)
[22:03:25] [Server thread/WARN]: 	at com.comphenix.protocol.wrappers.WrappedBlockData.createData(WrappedBlockData.java:300)
[22:03:25] [Server thread/WARN]: 	at cn.hamster3.work.utils.WorkUtils.openPlayerSign(WorkUtils.java:76)
[22:03:25] [Server thread/WARN]: 	at cn.hamster3.work.guihandler.ReleaseWorkHandler.click(ReleaseWorkHandler.java:46)
[22:03:25] [Server thread/WARN]: 	at cn.hamster3.api.gui.listener.GuiClickListener.onInventoryClick(GuiClickListener.java:20)
[22:03:25] [Server thread/WARN]: 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[22:03:25] [Server thread/WARN]: 	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[22:03:25] [Server thread/WARN]: 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[22:03:25] [Server thread/WARN]: 	at java.lang.reflect.Method.invoke(Unknown Source)
[22:03:25] [Server thread/WARN]: 	at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302)
[22:03:25] [Server thread/WARN]: 	at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
[22:03:25] [Server thread/WARN]: 	at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500)
[22:03:25] [Server thread/WARN]: 	at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485)
[22:03:25] [Server thread/WARN]: 	at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1893)
[22:03:25] [Server thread/WARN]: 	at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:33)
[22:03:25] [Server thread/WARN]: 	at net.minecraft.server.v1_12_R1.PacketPlayInWindowClick.a(SourceFile:10)
[22:03:25] [Server thread/WARN]: 	at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13)
[22:03:25] [Server thread/WARN]: 	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
[22:03:25] [Server thread/WARN]: 	at java.util.concurrent.FutureTask.run(Unknown Source)
[22:03:25] [Server thread/WARN]: 	at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46)
[22:03:25] [Server thread/WARN]: 	at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:748)
[22:03:25] [Server thread/WARN]: 	at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406)
[22:03:25] [Server thread/WARN]: 	at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679)
[22:03:25] [Server thread/WARN]: 	at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577)
[22:03:25] [Server thread/WARN]: 	at java.lang.Thread.run(Unknown Source)

In addition, my English level is not very good, so these words are produced by translation software. If it makes you angry, I'm very sorry.

commented

Update to 4.5.1

commented

Update to 4.5.1

Thank you for your reply!

I just tried to use the 4.5.1 version of protocollib, and then I found that WrappedBlockData.createData It's ready to use.

however PacketType.Play.Server .OPEN_ SIGN_ Editor has become unusable.

It doesn't report any errors, it just doesn't work.

(but 4.3.0, 4.4.0, and 4.5.0 can be used PacketType.Play.Server .OPEN_ SIGN_ Editor opens a sign editor for players)

The complete code is as follows:

        Location location = new Location(player.getWorld(), player.getLocation().getBlockX(), 0, player.getLocation().getBlockZ());

        BlockPosition blockPosition = new BlockPosition(location.getBlockX(), 0, location.getBlockZ());
        ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();

        PacketContainer blockChange = protocolManager.createPacket(PacketType.Play.Server.BLOCK_CHANGE);
        blockChange.getBlockPositionModifier().write(0, blockPosition);
        blockChange.getBlockData().write(0,
                WrappedBlockData.createData(Material.WALL_SIGN)
        );
        try {
            protocolManager.sendServerPacket(player, blockChange);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        PacketContainer updateSign = protocolManager.createPacket(PacketType.Play.Server.TILE_ENTITY_DATA);
        updateSign.getBlockPositionModifier().write(0, blockPosition);
        updateSign.getIntegers().write(0, 9);
        ArrayList<NbtBase<?>> list = new ArrayList<>();
        list.add(NbtFactory.of("x", blockPosition.getX()));
        list.add(NbtFactory.of("y", blockPosition.getY()));
        list.add(NbtFactory.of("z", blockPosition.getZ()));
        list.add(NbtFactory.of("id", "minecraft:sign"));
        list.add(NbtFactory.of("Text1", "{\"extra\":[{\"text\":\"" + lines[0] + "\"}],\"text\":\"\"}"));
        list.add(NbtFactory.of("Text2", "{\"extra\":[{\"text\":\"" + lines[1] + "\"}],\"text\":\"\"}"));
        list.add(NbtFactory.of("Text3", "{\"extra\":[{\"text\":\"" + lines[2] + "\"}],\"text\":\"\"}"));
        list.add(NbtFactory.of("Text4", "{\"extra\":[{\"text\":\"" + lines[3] + "\"}],\"text\":\"\"}"));
        NbtCompound compound = NbtFactory.ofCompound("", list);
        updateSign.getNbtModifier().write(0, compound);
        try {
            protocolManager.sendServerPacket(player, updateSign);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        PacketContainer openSignEditor = protocolManager.createPacket(PacketType.Play.Server.OPEN_SIGN_EDITOR);
        openSignEditor.getBlockPositionModifier().write(0, blockPosition);
        try {
            protocolManager.sendServerPacket(player, openSignEditor);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

In addition, I'm a little busy, so I didn't test this further. Sorry.

commented

Tracking that issue in #743