Reign of Nether RTS

Reign of Nether RTS

234k Downloads

Server crash due to Random.nextInt() zero-value parameter

alexdev-at opened this issue ยท 5 comments

commented

We found an exception which leads to a server crash and making the game unplayable after joining again. The cause is a 0 passed to the nextInt function. Here is a section of the log, this error is identical in 3 different runs, so i'm just copying it once:

[21:41:08] [Server thread/ERROR]: Encountered an unexpected exception
net.minecraft.ReportedException: Ticking entity
	at net.minecraft.server.MinecraftServer.m_5703_(MinecraftServer.java:870) ~[client-1.19.2-20220805.130853-srg.jar%23170!/:?]
	at net.minecraft.server.MinecraftServer.m_5705_(MinecraftServer.java:806) ~[client-1.19.2-20220805.130853-srg.jar%23170!/:?]
	at net.minecraft.client.server.IntegratedServer.m_5705_(IntegratedServer.java:84) ~[client-1.19.2-20220805.130853-srg.jar%23170!/:?]
	at net.minecraft.server.MinecraftServer.m_130011_(MinecraftServer.java:654) ~[client-1.19.2-20220805.130853-srg.jar%23170!/:?]
	at net.minecraft.server.MinecraftServer.m_206580_(MinecraftServer.java:244) ~[client-1.19.2-20220805.130853-srg.jar%23170!/:?]
	at java.lang.Thread.run(Thread.java:833) ~[?:?]
Caused by: java.lang.IllegalArgumentException: bound must be positive
	at java.util.Random.nextInt(Random.java:322) ~[?:?]
	at com.solegendary.reignofnether.unit.goals.RangedAttackBuildingGoal.setNextBlockTarget(RangedAttackBuildingGoal.java:42) ~[reignofnether-1.0.8.jar%23169!/:1.0.8]
	at com.solegendary.reignofnether.unit.goals.RangedAttackBuildingGoal.setBuildingTarget(RangedAttackBuildingGoal.java:62) ~[reignofnether-1.0.8.jar%23169!/:1.0.8]
	at com.solegendary.reignofnether.unit.interfaces.AttackerUnit.setAttackBuildingTarget(AttackerUnit.java:54) ~[reignofnether-1.0.8.jar%23169!/:1.0.8]
	at com.solegendary.reignofnether.unit.interfaces.AttackerUnit.attackClosestEnemy(AttackerUnit.java:187) ~[reignofnether-1.0.8.jar%23169!/:1.0.8]
	at com.solegendary.reignofnether.unit.interfaces.AttackerUnit.tick(AttackerUnit.java:154) ~[reignofnether-1.0.8.jar%23169!/:1.0.8]
	at com.solegendary.reignofnether.unit.units.villagers.PillagerUnit.m_8119_(PillagerUnit.java:198) ~[reignofnether-1.0.8.jar%23169!/:1.0.8]
	at net.minecraft.world.entity.Entity.m_6083_(Entity.java:1730) ~[client-1.19.2-20220805.130853-srg.jar%23170!/:?]
	at net.minecraft.world.entity.LivingEntity.m_6083_(LivingEntity.java:2732) ~[client-1.19.2-20220805.130853-srg.jar%23170!/:?]
	at net.minecraft.server.level.ServerLevel.m_8662_(ServerLevel.java:678) ~[client-1.19.2-20220805.130853-srg.jar%23170!/:?]
	at net.minecraft.server.level.ServerLevel.m_8647_(ServerLevel.java:662) ~[client-1.19.2-20220805.130853-srg.jar%23170!/:?]
	at net.minecraft.world.level.Level.m_46653_(Level.java:457) ~[client-1.19.2-20220805.130853-srg.jar%23170!/:?]
	at net.minecraft.server.level.ServerLevel.m_184063_(ServerLevel.java:323) ~[client-1.19.2-20220805.130853-srg.jar%23170!/:?]
	at net.minecraft.world.level.entity.EntityTickList.m_156910_(EntityTickList.java:54) ~[client-1.19.2-20220805.130853-srg.jar%23170!/:?]
	at net.minecraft.server.level.ServerLevel.m_8793_(ServerLevel.java:303) ~[client-1.19.2-20220805.130853-srg.jar%23170!/:?]
	at net.minecraft.server.MinecraftServer.m_5703_(MinecraftServer.java:866) ~[client-1.19.2-20220805.130853-srg.jar%23170!/:?]
	... 5 more
[21:41:08] [Server thread/FATAL]: Preparing crash report with UUID 0990b3c9-8ab4-44e4-bb51-c9be954c2328
[21:41:08] [Server thread/ERROR]: This crash report has been saved to: C:\Users\ANONYMOUS\curseforge\minecraft\Instances\1.19.2 Forge RTS\crash-reports\crash-2024-11-13_21.41.08-server.txt
[21:41:08] [Server thread/INFO]: Stopping server
[21:41:08] [Server thread/INFO]: Saving players
[21:41:08] [Render thread/FATAL]: Preparing crash report with UUID cb1677af-d8c6-4142-93c5-8e14f032c6ff

Full logs are attached:
gameplay_before_crash_one.log
gameplay_before_crash_two.log
join_after_crash.log

We were always at least 3 players, hosted via essential. All 3 factions were played.

commented

will fix

commented

public void setNextBlockTarget() {
if (this.buildingTarget != null && !buildingTarget.getBlocks().isEmpty()) {
Random rand = new Random();
List<BuildingBlock> nonAirBlocks = buildingTarget.getBlocks().stream().filter(b -> b.isPlaced(this.mob.level)).toList();
BuildingBlock block = nonAirBlocks.get(rand.nextInt(nonAirBlocks.size()));
this.blockTarget = block.getBlockPos();
}
}

commented

Fixed the comment to say that 0 is the issue, not a negative number. nextInt doesn't allow 0 to be passed, and since the filtered stream above yields 0 elements, this seems to cause the crash.

commented

should now be fixed

commented

Contributor apparently left the community, so the comments above should be ignored.