Jump to content

I need some help figuring out this java.lang.ArrayIndexOutOfBoundsException: 4


TheRPGAdventurer

Recommended Posts

I googled this before and it say that happens if you are calling an array in a restricted 

 

EnumDragonLifeStage: https://pastebin.com/RhcFuqw9

 

Crash

java.lang.ArrayIndexOutOfBoundsException: 4
	at com.TheRPGAdventurer.ROTD.server.entity.helper.EnumDragonLifeStage.fromTickCount(EnumDragonLifeStage.java:31)
	at com.TheRPGAdventurer.ROTD.server.entity.helper.DragonLifeStageHelper.getLifeStage(DragonLifeStageHelper.java:128)
	at com.TheRPGAdventurer.ROTD.server.entity.helper.DragonLifeStageHelper.updateLifeStage(DragonLifeStageHelper.java:278)
	at com.TheRPGAdventurer.ROTD.server.entity.helper.DragonLifeStageHelper.onLivingUpdate(DragonLifeStageHelper.java:267)
	at java.util.HashMap$Values.forEach(HashMap.java:972)
	at com.TheRPGAdventurer.ROTD.server.entity.EntityTameableDragon.func_70636_d(EntityTameableDragon.java:923)
	at net.minecraft.entity.EntityLivingBase.func_70071_h_(EntityLivingBase.java:2179)
	at net.minecraft.entity.EntityLiving.func_70071_h_(EntityLiving.java:295)
	at com.TheRPGAdventurer.ROTD.server.entity.EntityTameableDragon.func_70071_h_(EntityTameableDragon.java:847)
	at net.minecraft.world.World.func_72866_a(World.java:1993)
	at net.minecraft.world.WorldServer.func_72866_a(WorldServer.java:832)
	at net.minecraft.world.World.func_72870_g(World.java:1955)
	at net.minecraft.world.World.func_72939_s(World.java:1759)
	at net.minecraft.world.WorldServer.func_72939_s(WorldServer.java:613)
	at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:767)
	at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:668)
	at net.minecraft.server.integrated.IntegratedServer.func_71217_p(IntegratedServer.java:185)
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:526)
	at java.lang.Thread.run(Thread.java:745)

To be fair i didnt write thi code thi wa written years ago im here to revive it.

Edited by TheRPGAdventurer
Link to comment
Share on other sites

    public static int clampTickCount(int ticksSinceCreation) {
        return MathHelper.clamp(ticksSinceCreation, 0, VALUES.length * TICKS_PER_STAGE);
    }
 
    public static EnumDragonLifeStage fromTickCount(int ticksSinceCreation) {
        return VALUES[clampTickCount(ticksSinceCreation) / TICKS_PER_STAGE];
    }

Your enum has only 4 elements; therefore, the maximum value for the index can only be 3.

However, in your clampTickCount, it clamps the value as if there are 5 elements in your enum (hint: off by one). 

 

Note that MathHelper#clamp is inclusive on both the min value and the max value.

Edited by DavidM

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

VALUES.length is equal to 4, which, after multiplying and dividing by the same constant, still equals to 4.

 

Let's say we set the TICKS_PER_STAGE constant to 10, in which case

VALUES.length * TICKS_PER_STAGE

equals to 40.

40 divided by TICKS_PER_STAGE (10 in this case) is 4, which is out of bound for your enum.

 

However, if you change the return statement in your clampTickCount to:

return MathHelper.clamp(ticksSinceCreation, 0, (VALUES.length * TICKS_PER_STAGE) - 1);

, it will clamp everything into the range of [0, 39].

Now, the maximum return value is 39, which, after being divided by TICKS_PER_STAGE (10 in this case), is 3. This matches the highest dragon growth stage in your enum.

 

TL;DR:

Spoiler

Change your return statement in your clampTickCount to:


return MathHelper.clamp(ticksSinceCreation, 0, (VALUES.length * TICKS_PER_STAGE) - 1);

 

 

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.