IndexOutOfBoundsException on locked block code

Published by hawkster97 on
Status
Can not reproduce
Issue description

I locked around 100 blocks in my mod. Each of the locked items I then copy/pasted the same code into each. Automatic builds was on, so builds were successful throughout. When I tested the mod in the test client, Minecraft crashed on load. 

 

The issue I see is:

19:13.11 [main/ERROR] [FML]: Exception caught during firing event net.minecraftforge.event.RegistryEvent$Register@26382141:
java.lang.ArrayIndexOutOfBoundsException: -1
   at net.minecraft.block.Block.setHarvestLevel(Block.java:2285) ~[Block.class:?]
   at net.minecraft.block.Block.setHarvestLevel(Block.java:2266) ~[Block.class:?]
   at mod.mcreator.mcreator_fLBG8$BlockCustom.(mcreator_fLBG8.java:51) ~[mcreator_fLBG8$BlockCustom.class:?]
-> Jump to the exception line
   at mod.mcreator.mcreator_fLBG8.lambda$new$0(mcreator_fLBG8.java:32) ~[mcreator_fLBG8.class:?]
-> Jump to the exception line
   at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[?:1.8.0_202]
   at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382) ~[?:1.8.0_202]
   at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[?:1.8.0_202]
   at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[?:1.8.0_202]
   at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:545) ~[?:1.8.0_202]
   at java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:260) ~[?:1.8.0_202]
   at java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:438) ~[?:1.8.0_202]
   at mod.mcreator.themclamod.registerBlocks(themclamod.java:532) ~[themclamod.class:?]
   at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_13_themclamod_registerBlocks_Register.invoke(.dynamic) ~[?:?]
   at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) ~[ASMEventHandler.class:?]
   at net.minecraftforge.fml.common.eventhandler.EventBus$1.invoke(EventBus.java:144) ~[EventBus$1.class:?]
   at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182) [EventBus.class:?]
   at net.minecraftforge.registries.GameData.fireRegistryEvents(GameData.java:777) [GameData.class:?]
   at net.minecraftforge.fml.common.Loader.preinitializeMods(Loader.java:628) [Loader.class:?]
   at net.minecraftforge.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:252) [FMLClientHandler.class:?]
   at net.minecraft.client.Minecraft.init(Minecraft.java:513) [Minecraft.class:?]
   at net.minecraft.client.Minecraft.run(Minecraft.java:421) [Minecraft.class:?]
   at net.minecraft.client.main.Main.main(Main.java:118) [Main.class:?]
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_202]
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_202]
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_202]
   at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_202]
   at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?]
   at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?]
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_202]

 

This issue only is happening on around 10% of the blocks I locked. I tried removing the setHarvestLevel line in each of the affected files, and while it did show a difference, it was just another similar error called from a different location.

 

All of the files were treated equally, so I'm not sure why it would happen to one file and not the next, etc. The code I was copy/pasting was the code for four methods relating to the rotation issue seen here: https://mcreator.net/tracker/issue/46790 

 

This never happened in 1.8.2, but it is now in 1.8.3. I've been working on moving my mod to 1.8.3 due to 1.8.3's better bound boxes with rotation. I keep everything under a private version control repo and I branch as needed, so this isn't affecting my live mod, but it breaks 1.8.3 for me.

 

 

Issue comments

If you lock the code, we can't offer you much support as once the code is locked, it is no more under the control of MCreator. If you modify your code, you can easily introduce new bugs and issues.

This most likely happens in 1.8.3 and not in 1.8.2 because we have switched to new block registration code. You need to adapt your code to this too.

Hi Klemen,

 

I did some more testing, and I figured out that this is caused by rotation code. Since MCreator Y-Axis rotation code generated is broken in later versions of Forge, I have been locking my code and putting the correct code in, and for some reason, some blocks are ok with it, some not. I haven't found any reason yet.

It could be, yes. If you find a working code compatible with later versions and are ready to share it, you can paste it here :)

The fix here is to 

1. While unlocked, ensure block rotation mode is set to Y-Axis (I used Player Side usually

2. Wait 10-15 seconds for MCreator to generate code and blockstates, etc.

3. Lock the block.

4. Replace the four rotation methods, createBlockState, getStateFromMeta, getMetaFromState and getStateForPlacement , with the following code:

 

@Override
		protected net.minecraft.block.state.BlockStateContainer createBlockState() {
			return new net.minecraft.block.state.BlockStateContainer(this, FACING);
		}

		@Override
		public IBlockState getStateFromMeta(int meta) {
			return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta));
		}

		@Override
		public int getMetaFromState(IBlockState state) {
			return ((EnumFacing) state.getValue(FACING)).getHorizontalIndex();
		}

		@Override
		public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta,
				EntityLivingBase placer) {
			return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
		}

5. Compile. And Test. Y-Axis rotation work. If that error happens, unlock block again, set to DUNSEW rotation and compile, then repeat process again.

 

This appears related to my other former issue: https://mcreator.net/tracker/issue/46790