Ramixin / VisibleTraders

MIT License
1 stars 0 forks source link

[Bug] Crash when oppening the trade screen after breaking and placing the workstation #2

Closed RAK-XTECH closed 2 months ago

RAK-XTECH commented 3 months ago

Some times I get a crash when opening a villager trade screen right after breaking and placing the workstation, it only happened when the villager had that job previously.

For example the librarian had bad trades so I broke the lectern making the villager lose it's job, then placed the lectern making the same villager become a librarian again, then when opening the trade I got a crash. If any other villager got the job instead I didn't experience the crash, only when it was the same villager as before.

After crashing, the world can't be entered again, it disapears from the single player world list, needing to close and reopen minecraft to join the world once again. Good part is that it doesn't save the corrupted info so there's no risk of losing the world.

Below is the error logs when it crashes.

[14:34:06] [Netty Server IO #1/ERROR]: Error sending packet clientbound/minecraft:merchant_offers io.netty.handler.codec.EncoderException: Failed to encode packet 'clientbound/minecraft:merchant_offers' at net.minecraft.class_9136.method_56426(class_9136.java:55) ~[client-intermediary.jar:?] at net.minecraft.class_9136.encode(class_9136.java:14) ~[client-intermediary.jar:?] at net.minecraft.class_2545.method_10838(class_2545.java:26) ~[client-intermediary.jar:?] at net.minecraft.class_2545.encode(class_2545.java:12) ~[client-intermediary.jar:?] at io.netty.handler.codec.MessageToByteEncoder.write(MessageToByteEncoder.java:107) ~[netty-codec-4.1.97.Final.jar:?] at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:881) ~[netty-transport-4.1.97.Final.jar:?] at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:863) ~[netty-transport-4.1.97.Final.jar:?] at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:968) ~[netty-transport-4.1.97.Final.jar:?] at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:856) ~[netty-transport-4.1.97.Final.jar:?] at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:113) ~[netty-codec-4.1.97.Final.jar:?] at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:881) ~[netty-transport-4.1.97.Final.jar:?] at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:863) ~[netty-transport-4.1.97.Final.jar:?] at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:968) ~[netty-transport-4.1.97.Final.jar:?] at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:856) ~[netty-transport-4.1.97.Final.jar:?] at io.netty.channel.ChannelOutboundHandlerAdapter.write(ChannelOutboundHandlerAdapter.java:113) ~[netty-transport-4.1.97.Final.jar:?] at net.minecraft.class_2535$2.write(class_2535.java:530) ~[client-intermediary.jar:?] at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:881) ~[netty-transport-4.1.97.Final.jar:?] at io.netty.channel.AbstractChannelHandlerContext.invokeWriteAndFlush(AbstractChannelHandlerContext.java:940) ~[netty-transport-4.1.97.Final.jar:?] at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:966) ~[netty-transport-4.1.97.Final.jar:?] at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:934) ~[netty-transport-4.1.97.Final.jar:?] at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:984) ~[netty-transport-4.1.97.Final.jar:?] at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:1025) ~[netty-transport-4.1.97.Final.jar:?] at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:306) ~[netty-transport-4.1.97.Final.jar:?] at net.minecraft.class_2535.method_36942(class_2535.java:350) ~[client-intermediary.jar:?] at net.minecraft.class_2535.method_52917(class_2535.java:345) ~[client-intermediary.jar:?] at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:174) ~[netty-common-4.1.97.Final.jar:?] at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:167) ~[netty-common-4.1.97.Final.jar:?] at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470) ~[netty-common-4.1.97.Final.jar:?] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:569) ~[netty-transport-4.1.97.Final.jar:?] at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) ~[netty-common-4.1.97.Final.jar:?] at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.97.Final.jar:?] at java.base/java.lang.Thread.run(Unknown Source) [?:?] Caused by: io.netty.handler.codec.EncoderException: Empty ItemStack not allowed at net.minecraft.class_1799$2.method_57387(class_1799.java:173) ~[client-intermediary.jar:?] at net.minecraft.class_1799$2.encode(class_1799.java:160) ~[client-intermediary.jar:?] at net.minecraft.class_1914.method_56111(class_1914.java:215) ~[client-intermediary.jar:?] at net.minecraft.class_9139$1.encode(class_9139.java:25) ~[client-intermediary.jar:?] at net.minecraft.class_9135$11.method_58006(class_9135.java:378) ~[client-intermediary.jar:?] at net.minecraft.class_9135$11.encode(class_9135.java:363) ~[client-intermediary.jar:?] at net.minecraft.class_3943.method_55889(class_3943.java:39) ~[client-intermediary.jar:?] at net.minecraft.class_9139$6.encode(class_9139.java:42) ~[client-intermediary.jar:?] at net.minecraft.class_9139$9.method_56442(class_9139.java:97) ~[client-intermediary.jar:?] at net.minecraft.class_9139$9.encode(class_9139.java:87) ~[client-intermediary.jar:?] at net.minecraft.class_9136.method_56426(class_9136.java:53) ~[client-intermediary.jar:?] ... 31 more

[14:34:08] [Server thread/ERROR]: Exception stopping the server net.minecraft.class_148: Saving entity NBT at net.minecraft.class_1297.method_5647(class_1297.java:1940) ~[client-intermediary.jar:?] at net.minecraft.class_1297.method_5786(class_1297.java:1858) ~[client-intermediary.jar:?] at net.minecraft.class_1297.method_5662(class_1297.java:1866) ~[client-intermediary.jar:?] at net.minecraft.class_5565.method_31734(class_5565.java:109) ~[client-intermediary.jar:?] at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(Unknown Source) ~[?:?] at java.base/java.util.stream.ReferencePipeline$Head.forEach(Unknown Source) ~[?:?] at net.minecraft.class_5565.method_31760(class_5565.java:107) ~[client-intermediary.jar:?] at net.minecraft.class_5579.method_31812(class_5579.java:299) ~[client-intermediary.jar:?] at net.minecraft.class_5579.method_31837(class_5579.java:316) ~[client-intermediary.jar:?] at net.minecraft.class_5579.md0e7fbd$c2me-opts-scheduling$lambda$c2me$tryFlush$1$0(class_5579.java:3061) ~[client-intermediary.jar:?] at it.unimi.dsi.fastutil.longs.LongCollection.removeIf(LongCollection.java:274) ~[fastutil-8.5.12.jar:?] at it.unimi.dsi.fastutil.longs.LongCollection.removeIf(LongCollection.java:306) ~[fastutil-8.5.12.jar:?] at it.unimi.dsi.fastutil.longs.AbstractLongCollection.removeIf(AbstractLongCollection.java:163) ~[fastutil-8.5.12.jar:?] at net.minecraft.class_5579.c2me$tryFlush(class_5579.java:3059) ~[client-intermediary.jar:?] at net.minecraft.class_3218.handler$bcf000$c2me-opts-scheduling$replaceEntityFlushLogic(class_3218.java:13775) ~[client-intermediary.jar:?] at net.minecraft.class_3218.method_14176(class_3218.java:823) ~[client-intermediary.jar:?] at net.minecraft.server.MinecraftServer.method_3723(MinecraftServer.java:556) ~[client-intermediary.jar:?] at net.minecraft.server.MinecraftServer.method_3782(MinecraftServer.java:620) ~[client-intermediary.jar:?] at net.minecraft.class_1132.method_3782(class_1132.java:228) ~[client-intermediary.jar:?] at net.minecraft.server.MinecraftServer.method_29741(MinecraftServer.java:735) ~[client-intermediary.jar:?] at net.minecraft.server.MinecraftServer.method_29739(MinecraftServer.java:281) ~[client-intermediary.jar:?] at java.base/java.lang.Thread.run(Unknown Source) [?:?] Caused by: java.lang.IllegalStateException: Value must be positive: 0; Value must be within range [1;99]: 0; Item must not be minecraft:air at com.mojang.serialization.DataResult$Error.getOrThrow(DataResult.java:287) ~[datafixerupper-8.0.16.jar:?] at com.mojang.serialization.DataResult.getOrThrow(DataResult.java:81) ~[datafixerupper-8.0.16.jar:?] at net.minecraft.class_1646.handler$fip000$visibletraders$writeOfferingLevel(class_1646.java:2641) ~[client-intermediary.jar:?] at net.minecraft.class_1646.method_5652(class_1646.java) ~[client-intermediary.jar:?] at net.minecraft.class_1297.method_5647(class_1297.java:1922) ~[client-intermediary.jar:?] ... 21 more [14:34:08] [Render thread/WARN]: Client disconnected with reason: Internal Exception: io.netty.handler.codec.EncoderException: Failed to encode packet 'clientbound/minecraft:merchant_offers'

Ramixin commented 3 months ago

Thank you @RAK-XTECH for the report. Please reproduce the issue and share a new crash report without the following mods:

I doubt those are the issue, but it would help clean up the report to better find the exact cause

RAK-XTECH commented 3 months ago

There's no mod with those names, I believe they are normal libraries that minecraft uses during the saving process.

I didn't really try to peek deeper after I experienced the crash, after getting mending there was no reason to do any rerolls, so I just ran a quick test and was able to understand a bit more about the crashing.

First I made a new instance, installed only Visible Traders and started breaking replacing the lectern, there was several times where the locked trades didn't appear, specially if you open the villager trade screen very fast after the villager gets the job, some times it would generate by the second time you open the trade screen, but there was rare ocassions where it didn't generate any locked trade, indicating that the script failed somehow, even so there was no crashes.

Then I installed a single mod Scriptor Magicae, it adds two trades to the librarian.

Then very rarely I was getting the crash. I can confirm that both trades added by Scriptor are working since they appeared on locked trades several times and worked without problems after leveling the villager, even when the locked trades got interrupted and after reopening they still showed as options.

Then I removed Scriptor and added a different mod that just adds more enchantments, got no crashes, I believe that's because the new enchantments don't really add new items, just different nbt tags for the existing enchanted book.

My theory is that when the code for generating the locked trades gets interrupted by opening the trade window there's a small chance of something going wrong when dealing with modded items, making it save some bad data and that's what causes the crash when the game tries to encode the mechant_offers packet.

I don't know other mods that add custom trades to villager so can't really test much further than what I already did.

Ramixin commented 3 months ago

Thanks for looking into it. All the trades sometimes not appearing immediately when the menu is opened just after the villager recieves a job was for performance reasons. However, it is interesting though that sometimes the trades will not generate. I will do some tests to try and find why custom trades are not working too well...

Ramixin commented 2 months ago

EDIT: everything that was this post was incorrect. Here is the real reason:

The issue was, as you stated in your initial post, the packet encoder and the trade generator running at the same time, which has a slight chance of causing an issue. Not only have I made it so the mod fails gracefully when it can't save the locked trades, but now it will also prevent the locked trades from being encoded if a faulty trade is detected. Thank you again @RAK-XTECH