SoLegendary Posted September 11, 2022 Share Posted September 11, 2022 I have a feature whereby a user can set a predefined structure on and it will place the blocks both serverside and clientside once every few ticks (currently every 6 ticks). The general logic flow I have is: User selects and places a Building (array of BlockPos/BlockState pairs) Client -> Server packet to initialise a Building object containing this block data On every 6th tick: Server places a block from the array Server -> Client packet to place the same block Video of this in action: https://imgur.com/a/gb9rpJ3 However, sometimes when I do this I get an error in the LongOpenHashSet java class after some of the blocks are placed. java.lang.ArrayIndexOutOfBoundsException: Index 126 out of bounds for length 65 (note the index numbers are always different on each crash) It seems random to me how many blocks it places before crashing or whether it even crashes at all. Full crash log: https://pastebin.com/6JC9nm5B Link to the code that seems to be causing the error (When I comment these lines out the issue stops, but of course, then I don't get the blocks placed clientside): https://github.com/SoLegendary/reignofnether/blob/1.19/src/main/java/com/solegendary/reignofnether/building/BuildingClientEvents.java#L358-L360 I tried wrapping the above lines in a try-catch block but it doesn't seem to actually catch anything. Quote Link to comment Share on other sites More sharing options...
warjort Posted September 11, 2022 Share Posted September 11, 2022 Your code looks very broken. This is just what I found from a quick glance at your code. 1) Fundamentally, you are sending block place/remove events to all clients and then trying to place blocks regardless of whether the client knows about the location. At no point do you check level.isLoaded(BlockPos) so you will be writing blocks (or at least trying to) into unloaded chunks. Which is probably the cause of your crash? 2) Your use of statics with a non-thread safe data type (ArrayList) is asking for trouble unless you can guarantee everything happens on the same thread. This could also be the cause? 3) Using statics like that is just wrong anyway. You want to store this data in a proper object like a Level or Chunk capability where you can save the data, otherwise all your data will be lost when the server reboots. 4) You are not checking the event.phase in your tick event, so your code will run twice per tick. Finally on point (1) why are you doing all this extra work at all? If you pass 3 to setBlock() - or use setBlockAndUpdate() - minecraft will handle all this processing for you. i.e. You can do all the processing on the server and not worry about the client. Minecraft will probably do it a lot more efficiently than you, e.g. sending block updates in groups, using smaller packets and not sending updates to clients it knows don't need them. Quote Boilerplate: If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one. If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install Large files should be posted to a file sharing site like https://gist.github.com You should also read the support forum sticky post. Link to comment Share on other sites More sharing options...
SoLegendary Posted September 11, 2022 Author Share Posted September 11, 2022 Thanks for the pointers... I probably will need to do quite a bit of refactoring - I also didn't know about a lot of these methods lie setBlockAndUpdate Quote Link to comment Share on other sites More sharing options...
SoLegendary Posted September 12, 2022 Author Share Posted September 12, 2022 @warjort I've removed the call from Server -> Client to update blocks and its seemed to have stopped the crashes for the time being. However, when I use setBlockAndUpdate() it doesn't seem to trigger a BlockEvent.EntityPlaceEvent on the client side. I want to set and destroy a dummy block on the spot with each block creation so that we get the particle and sound effects on each block being built. This is the code I added to BuildingClientEvents: @SubscribeEvent public static void onBlockPlace(BlockEvent.EntityPlaceEvent evt) { BlockPos bp = evt.getPos(); BlockState bs = evt.getState(); if (MC.level != null && MC.level.isLoaded(bp)) { MC.level.setBlock(bp.east(), bs, 1); MC.level.destroyBlock(bp, false); } } Quote Link to comment Share on other sites More sharing options...
warjort Posted September 12, 2022 Share Posted September 12, 2022 You don't need to do this. You should be able to trigger these effects from the server without messing around with actually destroying blocks. serverLevel.levelEvent(LevelEvent.PARTICLES_DESTROY_BLOCK, blockPos, Block.getId(blockState)) Quote Boilerplate: If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one. If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install Large files should be posted to a file sharing site like https://gist.github.com You should also read the support forum sticky post. Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.