import java.util.jar.Attributes;

Started by Valirane on

Topic category: Help with modding (Java Edition)

Last seen on 05:05, 30. Oct 2022
Joined Mar 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
import java.util.jar.Attributes;
Sat, 05/07/2022 - 18:12 (edited)

I'm trying to make a procedure that modifies a player's Attributes and have run into a problem.  I'm using DTM's Toolkit, which doesn't support 1.18.2 out of the box.  I copy pasted the relevant files from the plugin's 1.16.5 setup to make it show up in 1.18.2  When I add one of the code blocks from the plugin to a procedure I get errors when it tries to compile.

error: a type with the same simple name is already defined by the single-type-import of Attributes 

error: reference to Attributes is ambiguous 

 

Apparently, the line of code generated by the plugin:

((LivingEntity) entity).getAttribute(Attributes.MAX_HEALTH).setBaseValue(30);

conflicts with:

import java.util.jar.Attributes;

which gets inserted into the header of the procedure's code whenever the "getAttribute" code above gets compiled.

 

I tried manually removing the "import" line and the procedure compiled without issue.  I was able to run the game and everything worked perfectly.  When my condition was triggered, the player's max HP went to 30.

 

So my question:  What is "import java.util.jar.Attributes;" used for?  If it's not important, is there a way to stop it from being inserted into my procedures. If it is important, how can I eliminate the conflict?

Edited by Valirane on Sat, 05/07/2022 - 18:12
Last seen on 05:05, 30. Oct 2022
Joined Mar 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Upon further investigation,…
Mon, 05/16/2022 - 07:39

Upon further investigation, the conflict appears to be between two "imports" that get generated when the attribute modifying procedure is built.

import net.minecraft.world.entity.ai.attributes.Attributes;

import java.util.jar.Attributes;

The first one is needed to properly read the procedure.  The second one doesn't seem to do anything other than conflict with the first.  Deleting it clears the conflict and allows the procedure to run and work in game.

Anyone have any insight on how to prevent that second "Import" from auto generating when the procedure is built?

Last seen on 01:18, 28. Mar 2024
Joined Apr 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
If I had to guess It's…
Tue, 05/17/2022 - 05:30

If I had to guess It's because Mcreator has built-in procedure blocks for getting the max health and setting current health of entities, I and many others wish they would just implement the attributes fully into mcreator. Guess we'll see.

Last seen on 00:46, 5. Jun 2023
Joined Apr 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hey so I've actually been…
Sat, 07/02/2022 - 03:29

Hey so I've actually been trying to get into understanding the java code and writing my own after learning C this semester at uni, and thought that creating plugins for MCreator would be a great place to start - I spent pretty much all of yesterday getting my head around it and decided that accessing and editting attributes of entities would be a good place to start. I of course ran into the same issue you are experiencing here, and actually found somewhat of a solution to it! I havent tested it fully yet however modifying and reading the MAX_HEALTH attribute works like a charm!

So to fix the importting conflict, I modified this parameter of the getAttribute method:

((LivingEntity) entity).getAttribute(Attributes.MAX_HEALTH).setBaseValue(30);

to:

((LivingEntity) entity).getAttribute(net.minecraft.world.entity.ai.attributes.Attributes.MAX_HEALTH).setBaseValue(30);

Im fairly certain that this is not extremely efficient, however it definitely works even as a brute force option.

I had a little look-through on the MCreator git and found the code that handles adding imports. It seems like it should be removing duplicate imports when there are multiple entries with different roots (in this case the 'Attributes' class has two roots being net.minecraft and java.util)... however it just does't seem to be doing that in this case and I cannot figure out why with my own current understandning of the language.

For reference, the section of code I am refering to can be found at: https://github.com/MCreator/MCreator/blob/c205ccfc81c549d5ef09b84eafcea58219cf81f4/src/main/java/net/mcreator/java/ImportFormat.java#L124
From lines 232-245.

If Klemen, Goldorion or anyone else with knowledge of why this would be the case could provide any insight then that would be appreciated greatly!

Last seen on 05:05, 30. Oct 2022
Joined Mar 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Great work on figuring out a…
Sat, 07/02/2022 - 06:21

Great work on figuring out a solution.   I've been working on a plugin as well and Attributes is the one area I still had a snag with.  I managed to get around this import conflict for the most part by switching to using Commands to adjust Attributes.  This worked great for Setting values, but not for Getting them, since the command outputs a query through an in game message.  I resorted to using my original method for these and just code edited out the extra import.

I just applied your fix to my Get Attribute plugin files and it works perfectly, no more conflicts.  One of these days I plan on releasing my plugin, but I keep finding new things to add.

Last seen on 00:46, 5. Jun 2023
Joined Apr 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hey that's great to hear! I…
Sun, 07/03/2022 - 03:08

Hey that's great to hear! I feel like I'm getting a bit too ambitious on my end - attempting to create a new variable type that can be initialised as an Attribute type. It's quite tedious in the way that whenever I am messing with new variable types it always seems to corrupt my workspace and locks me out of the forge-1.18.2 generator until I remove my plugin and revert the changes...