Jump to content

[1.7.10] Problems with Attributes, collections and arrays


Recommended Posts

Posted

So the problem is:

When i hit entity with my item, before hurt is performed (using AttackEntityEvent) if player is holding my item, it scans 9 items "inside"n this item and chooses one with the biggest amount of damage. The problem is:

AttributeModifier attribute = (AttributeModifier) i.getItem().getAttributeModifiers(i).get(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName()).toArray()[0];

Crashes minecraft on:

java.lang.ArrayIndexOutOfBoundsException: 0

 

What i did in my class:

                              ItemStack i = item.getSword();
				System.out.println("collection: " + i.getItem().getAttributeModifiers(i).get(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName()));
				System.out.println("array of attributes: " + i.getItem().getAttributeModifiers(i).get(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName()).toArray());
				System.out.println("attribute: " + i.getItem().getAttributeModifiers(i).get(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName()).toArray()[0]);
				Object[] array = i.getItem().getAttributeModifiers(i).get(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName()).toArray();
				System.out.println("lenght of array: " + array.length);
				Object object = i.getItem().getAttributeModifiers(i).get(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName()).toArray()[array.length-1];
				System.out.println("is object attribute: " + (object instanceof AttributeModifier));
				AttributeModifier attribute = (AttributeModifier) object;
				meta = attribute.getAmount() > damage ? 1 : meta;
				damage = attribute.getAmount() > damage ? attribute.getAmount() : damage;

What i get in console:

[12:31:30] [Client thread/INFO] [sTDOUT]: [code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:32]: collection: [AttributeModifier{amount=6.0, operation=0, name='Weapon modifier', id=cb3f55d3-645c-4f38-a497-9c13a33db5cf, serialize=true}]
[12:31:30] [Client thread/INFO] [sTDOUT]: [code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:33]: array of attributes: [Ljava.lang.Object;@5411a529
[12:31:30] [Client thread/INFO] [sTDOUT]: [code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:34]: attribute: AttributeModifier{amount=6.0, operation=0, name='Weapon modifier', id=cb3f55d3-645c-4f38-a497-9c13a33db5cf, serialize=true}
[12:31:30] [Client thread/INFO] [sTDOUT]: [code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:36]: lenght of array: 1
[12:31:30] [Client thread/INFO] [sTDOUT]: [code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:40]: is object attribute: true
[12:31:30] [Client thread/ERROR] [FML]: Exception caught during firing event net.minecraftforge.event.entity.player.AttackEntityEvent@20e43235:
java.lang.ArrayIndexOutOfBoundsException: 0
at code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent.onAttack(AttackWithCompressedToolEvent.java:72) ~[AttackWithCompressedToolEvent.class:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler_11_AttackWithCompressedToolEvent_onAttack_AttackEntityEvent.invoke(.dynamic) ~[?:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:54) ~[ASMEventHandler.class:?]
at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:138) [EventBus.class:?]
at net.minecraft.entity.player.EntityPlayer.attackTargetEntityWithCurrentItem(EntityPlayer.java:1339) [EntityPlayer.class:?]
at net.minecraft.client.multiplayer.PlayerControllerMP.attackEntity(PlayerControllerMP.java:462) [PlayerControllerMP.class:?]
at net.minecraft.client.Minecraft.func_147116_af(Minecraft.java:1464) [Minecraft.class:?]
at net.minecraft.client.Minecraft.runTick(Minecraft.java:2028) [Minecraft.class:?]
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1028) [Minecraft.class:?]
at net.minecraft.client.Minecraft.run(Minecraft.java:951) [Minecraft.class:?]
at net.minecraft.client.main.Main.main(Main.java:164) [Main.class:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.11.jar:?]
at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.11.jar:?]
at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:78) [start/:?]
at GradleStart.main(GradleStart.java:45) [start/:?]
[12:31:30] [Client thread/ERROR] [FML]: Index: 1 Listeners:
[12:31:30] [Client thread/ERROR] [FML]: 0: NORMAL
[12:31:30] [Client thread/ERROR] [FML]: 1: ASM: code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent@5cd5e1b6 onAttack(Lnet/minecraftforge/event/entity/player/AttackEntityEvent;)V
[12:31:31] [server thread/INFO] [sTDOUT]: [code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:32]: collection: [AttributeModifier{amount=6.0, operation=0, name='Weapon modifier', id=cb3f55d3-645c-4f38-a497-9c13a33db5cf, serialize=true}]
[12:31:31] [server thread/INFO] [sTDOUT]: [code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:33]: array of attributes: [Ljava.lang.Object;@78b9bfbd
[12:31:31] [server thread/INFO] [sTDOUT]: [code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:34]: attribute: AttributeModifier{amount=6.0, operation=0, name='Weapon modifier', id=cb3f55d3-645c-4f38-a497-9c13a33db5cf, serialize=true}
[12:31:31] [server thread/INFO] [sTDOUT]: [code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:36]: lenght of array: 1
[12:31:31] [server thread/INFO] [sTDOUT]: [code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:40]: is object attribute: true
[12:31:31] [server thread/ERROR] [FML]: Exception caught during firing event net.minecraftforge.event.entity.player.AttackEntityEvent@26b5a0d7:
java.lang.ArrayIndexOutOfBoundsException: 0
at code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent.onAttack(AttackWithCompressedToolEvent.java:72) ~[AttackWithCompressedToolEvent.class:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler_11_AttackWithCompressedToolEvent_onAttack_AttackEntityEvent.invoke(.dynamic) ~[?:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:54) ~[ASMEventHandler.class:?]
at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:138) [EventBus.class:?]
at net.minecraft.entity.player.EntityPlayer.attackTargetEntityWithCurrentItem(EntityPlayer.java:1339) [EntityPlayer.class:?]
at net.minecraft.network.NetHandlerPlayServer.processUseEntity(NetHandlerPlayServer.java:881) [NetHandlerPlayServer.class:?]
at net.minecraft.network.play.client.C02PacketUseEntity.processPacket(C02PacketUseEntity.java:51) [C02PacketUseEntity.class:?]
at net.minecraft.network.play.client.C02PacketUseEntity.processPacket(C02PacketUseEntity.java:69) [C02PacketUseEntity.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) [NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) [NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) [integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) [MinecraftServer$2.class:?]
[12:31:31] [server thread/ERROR] [FML]: Index: 1 Listeners:
[12:31:31] [server thread/ERROR] [FML]: 0: NORMAL
[12:31:31] [server thread/ERROR] [FML]: 1: ASM: code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent@5cd5e1b6 onAttack(Lnet/minecraftforge/event/entity/player/AttackEntityEvent;)V
[12:31:31] [server thread/ERROR]: Encountered an unexpected exception
net.minecraft.util.ReportedException: Ticking memory connection
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:198) ~[NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) ~[MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) ~[MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) ~[integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) [MinecraftServer$2.class:?]
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
at code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent.onAttack(AttackWithCompressedToolEvent.java:72) ~[AttackWithCompressedToolEvent.class:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler_11_AttackWithCompressedToolEvent_onAttack_AttackEntityEvent.invoke(.dynamic) ~[?:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:54) ~[ASMEventHandler.class:?]
at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:138) ~[EventBus.class:?]
at net.minecraft.entity.player.EntityPlayer.attackTargetEntityWithCurrentItem(EntityPlayer.java:1339) ~[EntityPlayer.class:?]
at net.minecraft.network.NetHandlerPlayServer.processUseEntity(NetHandlerPlayServer.java:881) ~[NetHandlerPlayServer.class:?]
at net.minecraft.network.play.client.C02PacketUseEntity.processPacket(C02PacketUseEntity.java:51) ~[C02PacketUseEntity.class:?]
at net.minecraft.network.play.client.C02PacketUseEntity.processPacket(C02PacketUseEntity.java:69) ~[C02PacketUseEntity.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) ~[NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) ~[NetworkSystem.class:?]
... 5 more
[12:31:31] [server thread/ERROR]: This crash report has been saved to: C:\my\mcmodding\mods\tool_compresser\eclipse\.\crash-reports\crash-2014-12-27_12.31.31-server.txt
[12:31:31] [server thread/INFO]: Stopping server
[12:31:31] [server thread/INFO]: Saving players
[12:31:31] [server thread/INFO]: Saving worlds
[12:31:31] [server thread/INFO]: Saving chunks for level 'Test2'/Overworld
[12:31:31] [server thread/INFO]: Saving chunks for level 'Test2'/Nether
[12:31:31] [server thread/INFO]: Saving chunks for level 'Test2'/The End
[12:31:31] [server thread/INFO] [FML]: Unloading dimension 0
[12:31:31] [server thread/INFO] [FML]: Unloading dimension -1
[12:31:31] [server thread/INFO] [FML]: Unloading dimension 1
[12:31:31] [server thread/INFO] [FML]: Applying holder lookups
[12:31:31] [server thread/INFO] [FML]: Holder lookups applied
[12:31:31] [server thread/INFO] [FML]: The state engine was in incorrect state SERVER_STOPPING and forced into state SERVER_STOPPED. Errors may have been discarded.
[12:31:32] [Client thread/FATAL]: Unreported exception thrown!
java.lang.ArrayIndexOutOfBoundsException: 0
at code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent.onAttack(AttackWithCompressedToolEvent.java:72) ~[AttackWithCompressedToolEvent.class:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler_11_AttackWithCompressedToolEvent_onAttack_AttackEntityEvent.invoke(.dynamic) ~[?:?]
at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:54) ~[ASMEventHandler.class:?]
at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:138) ~[EventBus.class:?]
at net.minecraft.entity.player.EntityPlayer.attackTargetEntityWithCurrentItem(EntityPlayer.java:1339) ~[EntityPlayer.class:?]
at net.minecraft.client.multiplayer.PlayerControllerMP.attackEntity(PlayerControllerMP.java:462) ~[PlayerControllerMP.class:?]
at net.minecraft.client.Minecraft.func_147116_af(Minecraft.java:1464) ~[Minecraft.class:?]
at net.minecraft.client.Minecraft.runTick(Minecraft.java:2028) ~[Minecraft.class:?]
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1028) ~[Minecraft.class:?]
at net.minecraft.client.Minecraft.run(Minecraft.java:951) [Minecraft.class:?]
at net.minecraft.client.main.Main.main(Main.java:164) [Main.class:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.11.jar:?]
at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.11.jar:?]
at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:78) [start/:?]
at GradleStart.main(GradleStart.java:45) [start/:?]
...

Problem is that in console it succesfully obtains array and value of array and processes it:

[code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:32]: collection: [AttributeModifier{amount=6.0, operation=0, name='Weapon modifier', id=cb3f55d3-645c-4f38-a497-9c13a33db5cf, serialize=true}]

[12:31:30] [Client thread/INFO] [sTDOUT]: [code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:33]: array of attributes: [Ljava.lang.Object;@5411a529

[12:31:30] [Client thread/INFO] [sTDOUT]: [code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:34]: attribute: AttributeModifier{amount=6.0, operation=0, name='Weapon modifier', id=cb3f55d3-645c-4f38-a497-9c13a33db5cf, serialize=true}

[12:31:30] [Client thread/INFO] [sTDOUT]: [code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:36]: lenght of array: 1

[12:31:30] [Client thread/INFO] [sTDOUT]: [code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:40]: is object attribute: true

How ever, when action starts it crashes on same thing:

[12:31:30] [Client thread/ERROR] [FML]: Exception caught during firing event net.minecraftforge.event.entity.player.AttackEntityEvent@20e43235:

java.lang.ArrayIndexOutOfBoundsException: 0

at code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent.onAttack(AttackWithCompressedToolEvent.java:72) ~[AttackWithCompressedToolEvent.class:?]

at cpw.mods.fml.common.eventhandler.ASMEventHandler_11_AttackWithCompressedToolEvent_onAttack_AttackEntityEvent.invoke(.dynamic) ~[?:?]

at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:54) ~[ASMEventHandler.class:?]

at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:138) [EventBus.class:?]

at net.minecraft.entity.player.EntityPlayer.attackTargetEntityWithCurrentItem(EntityPlayer.java:1339) [EntityPlayer.class:?]

at net.minecraft.client.multiplayer.PlayerControllerMP.attackEntity(PlayerControllerMP.java:462) [PlayerControllerMP.class:?]

at net.minecraft.client.Minecraft.func_147116_af(Minecraft.java:1464) [Minecraft.class:?]

at net.minecraft.client.Minecraft.runTick(Minecraft.java:2028) [Minecraft.class:?]

at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1028) [Minecraft.class:?]

at net.minecraft.client.Minecraft.run(Minecraft.java:951) [Minecraft.class:?]

at net.minecraft.client.main.Main.main(Main.java:164) [Main.class:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_25]

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]

at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]

at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.11.jar:?]

at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.11.jar:?]

at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:78) [start/:?]

at GradleStart.main(GradleStart.java:45) [start/:?]

Can somebody explain me what is going on?

Posted

Well the Exceptions is thrown

at code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent.onAttack(AttackWithCompressedToolEvent.java:72) ~[AttackWithCompressedToolEvent.class:?]

 

So at line 72 in the method AttackWithCompressedToolEvent.onAttack(...)

 

This is your last output to the console:

 

[12:31:30] [Client thread/INFO] [sTDOUT]: [code.elix_x.toolscompressor.events.AttackWithCompressedToolEvent:onAttack:40]: is object attribute: true

 

At line 40...

 

I would recommend you to post the whole method if you want help and advice in this case.

Posted

Uhm... do you actually understand what a stacktrace is and what I have written in my post before?

 

Your posted part of  the method AttackWithCompressedToolEvent.onAttack(...)

ends at line =>        43        <=

 

BUT!!!: The java.lang.ArrayIndexOutOfBoundsException

is thrown at line =>          72        <=

 

Do you know what I mean?

Can you please post the whole method???

 

not only this part:

 

ItemStack i = item.getSword();

System.out.println("collection: " + i.getItem().getAttributeModifiers(i).get(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName()));

System.out.println("array of attributes: " + i.getItem().getAttributeModifiers(i).get(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName()).toArray());

System.out.println("attribute: " + i.getItem().getAttributeModifiers(i).get(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName()).toArray()[0]);

Object[] array = i.getItem().getAttributeModifiers(i).get(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName()).toArray();

System.out.println("lenght of array: " + array.length);

Object object = i.getItem().getAttributeModifiers(i).get(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName()).toArray()[array.length-1];

System.out.println("is object attribute: " + (object instanceof AttributeModifier));

AttributeModifier attribute = (AttributeModifier) object;

meta = attribute.getAmount() > damage ? 1 : meta;

damage = attribute.getAmount() > damage ? attribute.getAmount() : damage;

Posted

I found an error that's fine now: one of items was hoe, that doesn't have attack damage value...

 

ITS MAGIC. 01a474b92e5bb4fd7601508tf1.gif

 

Read:

Don't do shit like that in one line and always check for array size and null pointers before you access them.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.