Jump to content

[Solved]Question About attributes.


Noah_Beech

Recommended Posts

(Look at bottom for solution)

 

I have been using attributes to modify player health / speed. But I have ran into an issue, if the player already has the same attribute instance with a modifier, and I try to apply mine, it will crash. I have been able to dig into the code to figure out if an attribute has already been applied. If it isn't applied, I apply my own new attribute, if not I need to modify the existing one, the problem is I don't know how to do this last part even after looking at the attribute classes / code for the longest time. Hopefully someone here is more knowledgeable than me in this subject and would be willing to help me. here is a segment of code that I am working with.

 

final AttributeModifier speed1 = new AttributeModifier(player.getPersistentID(), "Emblem Speed Boost", 0.25D, 1);

AttributeInstance attributeinstance = player.func_110148_a(SharedMonsterAttributes.field_111263_d);
		if(attributeinstance.func_111127_a(speed1.func_111167_a())!= null){

			//The modifier for the modifier needs to be in this clause!
			System.out.println("Conditions are true");
		}
		else{
			System.out.println("Conditions are NOT true");
		attributeinstance.func_111121_a(speed1);
		}

 

Thanks in advance.

 

~Noah

This is the creator of the Rareores mod! Be sure to check it out at

Link to comment
Share on other sites

Okay, so after tinkering around with the above mentioned function, I found out that it does modify an attribute. After this I quickly got started on using it. But ran into a weird error that I can't seem to figure out. The conditionals determine if a new one should be added, or if it should be modified. But for some reason the player start going about 50x faster than he should and I don;t know why. I use a hashmap to keep data for each player if he should loose the attribute or not.

 

My code

 

PlayerTickhandler
________________________________________________________________
public HashMap<String, Integer> useMap = new HashMap<String, Integer>();

if(useMap.get(player.username) == null){
            useMap.put(player.username, 0);
        }

if(player.inventory.hasItem(mod_rareores.ItemEmblemSpeed.itemID) && useMap.get(player.username) == 0){
		AttributeInstance attributeinstance = player.func_110148_a(SharedMonsterAttributes.field_111263_d);
		if(attributeinstance.func_111127_a(speed1.func_111167_a())!= null){
			attributeinstance.func_111128_a(1.25);
			useMap.put(player.username,1 );
			System.out.println("Part1works 1");
		}
		else if(attributeinstance.func_111127_a(speed1.func_111167_a())== null){
		attributeinstance.func_111121_a(speed1);
		useMap.put(player.username,1 );
		System.out.println("Part1works 2");
		}

		player.jumpMovementFactor *=1.25;

	}
	if(!player.inventory.hasItem(mod_rareores.ItemEmblemSpeed.itemID) && useMap.get(player.username) == 1){
		AttributeInstance attributeinstance = player.func_110148_a(SharedMonsterAttributes.field_111263_d);
		attributeinstance.func_111128_a(0.;
		useMap.put(player.username,0 );
		System.out.println("Part2works");
	}

This is the creator of the Rareores mod! Be sure to check it out at

Link to comment
Share on other sites

All I know is that you can set the player's walk speed with a simple call to

		thePlayer.capabilities.setPlayerWalkSpeed(float)

Default value is 0.1F.

I know that this may have the side effect of a FOV change but I guess that is all right since the player is running... fast... :)

 

Isn't this just client side though?

This is the creator of the Rareores mod! Be sure to check it out at

Link to comment
Share on other sites

  • 3 weeks later...

Okay so I pushed this aside for awhile to get other things done, but this is the last thing I need to do. Can anyone help?

 

What are you trying to do?

If you want a player obj on the server side you will need to specify at which time you need to get that player obj, then we can tell you which method to use. If it's a the time of something happening on the client side (aka. keypress for example) then you will need to send a packet from C to S which then the server can use to do things to the player.

If you guys dont get it.. then well ya.. try harder...

Link to comment
Share on other sites

final AttributeModifier speed1 = new AttributeModifier(player.getPersistentID(), "Emblem Speed Boost", 0.25D, 1);

The last int argument deals with how the modifier is applied. Its range is in [0..2].

Either an added value, a multiplier, or an added multiplier.

Example:

choose 2 to apply a (1+0.25D) multiplier to the speed value.

 

You can use

attributeinstance.func_111124_b(AttributeModifier)

to remove a modifier.

Link to comment
Share on other sites

This is what i meant.

Remove the modifier, then apply it again with a new value.

 

Or apply another modifier.

 

Or calculate the complete modified value with

old = attributeInstance.func_111125_b();

and change it with

attributeInstance.func_111128_a(old*change);

 

There is currently no supported way to modify a part of the modifier in the applied collection.

You'll probably end up with a concurrent modification exception if you force it through reflection.

Link to comment
Share on other sites

So essentially the attribute system sucks balls? Back in 1.5 I just needed ONE LINE OF CODE do do what I wanted, and now 60 lines only does it crudely. Would there be ANY way to modify player speed dynamically? Like if a different mod has the player speed at 1.5 times and I want to multiply it by 1.3 lets say, it will go upto 1.5 * 1.3 = 1.95. But doing it my previous way makes it so it removes the old modifier and replaces it with my new one so say 1.5 (/) to 1.3. Would it be possible if I could make my own specific mod attribute and not use vanilla ones, or is there some code elsewhere that modifies player speed? Because What Im doing now and what I was doing earlier is NOT going to work!

This is the creator of the Rareores mod! Be sure to check it out at

Link to comment
Share on other sites

Keep in mind that the system is not only new, it's also a work in progress.

This is one of many steps needed towards the API they are working on.

 

Okay I understand that, but how Am I going to modify player speed then, I just can't remove the item, and IM not going to use potioneffects.

This is the creator of the Rareores mod! Be sure to check it out at

Link to comment
Share on other sites

found a much better way (At least IMHO)

if(player.inventory.hasItem(mod_rareores.ItemEmblemFlight.itemID) && useMap2.get(player.username) == 0){

		PlayerCapabilities cap = ObfuscationReflectionHelper.getPrivateValue(EntityPlayer.class, player, "capabilities", "field_71075_bZ");
		float speed = player.capabilities.getWalkSpeed();
		speed *=1.5;
		ObfuscationReflectionHelper.setPrivateValue(PlayerCapabilities.class, cap, speed ,"walkSpeed", "field_73357_f");
		useMap2.put(player.username,1 );

	}
	if(!player.inventory.hasItem(mod_rareores.ItemEmblemFlight.itemID) && useMap2.get(player.username) == 1){
		PlayerCapabilities cap = ObfuscationReflectionHelper.getPrivateValue(EntityPlayer.class, player, "capabilities", "field_71075_bZ");
		float speed = player.capabilities.getWalkSpeed();
		speed /=1.5;
		ObfuscationReflectionHelper.setPrivateValue(PlayerCapabilities.class, cap, speed ,"walkSpeed", "field_73357_f");
		useMap2.put(player.username,0 );
	}

This is the creator of the Rareores mod! Be sure to check it out at

Link to comment
Share on other sites

Why anyone would change code and turn it into "work in progress" state, and thus increasing the amount of code used, is completely over my head. You're not supposed to change a running system...and you're specifically not supposed to change a running system wihtout proper documentation.

 

And don't even get me started about how you guys see new code as opportunity to make everything "easier and better".

 

Why would you change player.landMovementFactor to private and don't replace it with something proper and easy to use in the first place? That's just rubbish.

Link to comment
Share on other sites

There's plenty of documentation for the Minecraft code, as well as proper names for everything and a lot of other things. All of this is provided by Mojang for the programmers working on the Minecraft project :P

 

Why anyone would change code and turn it into "work in progress" state, and thus increasing the amount of code used, is completely over my head.

Because the "wip" state is not noticeable for the end users.

The users will never see or know of how the code looks between 1.5 and 1.6 all they know are new features.

Therefore I see no problem in doing things this way :P

If you guys dont get it.. then well ya.. try harder...

Link to comment
Share on other sites

New features are good, but why remove code that's already properly working?

To make it better, and minecraft's code needs A LOT of work in that regard.

 

Without changing over to the attribute system, the modding API would not be possible.

These changes are towards a bigger goal, and to be honest the Attribute system isn't that bad once you are used to it.

 

One at a time these changes may seem weird and just made to make a modders job tedious for no good reason, but if you look at it from a different standpoint the code is slowly being made into something which is easily expandable and therefore opens up a lot of possibilities for us as modders ;)

If you guys dont get it.. then well ya.. try harder...

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I'm developing a dimension, but it's kinda resource intensive so some times during player teleporting it lags behind making the player phase down into the void, so im trying to implement some kind of pregeneration to force the game loading a small set of chunks in the are the player will teleport to. Some of the things i've tried like using ServerLevel and ServerChunkCache methods like getChunk() dont actually trigger chunk generation if the chunk isn't already on persistent storage (already generated) or placing tickets, but that doesn't work either. Ideally i should be able to check when the task has ended too. I've peeked around some pregen engines, but they're too complex for my current understanding of the system of which I have just a basic understanding (how ServerLevel ,ServerChunkCache  and ChunkMap work) of. Any tips or other classes I should be looking into to understand how to do this correctly?
    • https://mclo.gs/4UC49Ao
    • Way back in the Forge 1.17 days, work started for adding JPMS (Java Platform Module Support) to ModLauncher and ForgeModLoader. This has been used internally by Forge and some libraries for a while now, but mods (those with mods.toml specifically) have not been able to take advantage of it. As of Forge 1.21.1 and 1.21.3, this is now possible!   What is JPMS and what does it mean for modders? JPMS is the Java Platform Module System, introduced in Java 9. It allows you to define modules, which are collections of packages and resources that can be exported or hidden from other modules. This allows for much more fine-tuned control over visibility, cleaner syntax for service declarations and support for sealed types across packages. For example, you might have a mod with a module called `com.example.mod` that exports `com.example.mod.api` and `com.example.mod.impl` to other mods, but hides `com.example.mod.internal` from them. This would allow you to have a clean API for other mods to use, while keeping your internal implementation details hidden from IDE hints, helping prevent accidental usage of internals that might break without prior notice. This is particularly useful if you'd like to use public records with module-private constructors or partially module-private record components, as you can create a sealed interface that only your record implements, having the interface be exported and the record hidden. It's also nice for declaring and using services, as you'll get compile-time errors from the Java compiler for typos and the like, rather than deferring to runtime errors. In more advanced cases, you can also have public methods that are only accessible to specific other modules -- handy if you want internal interactions between multiple of your own mods.   How do I bypass it? We understand there may be drama in implementing a system that prevents mods from accessing each other's internals when necessary (like when a mod is abandoned or you need to fix a compat issue) -- after all, we are already modding a game that doesn't have explicit support for Java mods yet. We have already thought of this and are offering APIs from day one to selectively bypass module restrictions. Let me be clear: Forge mods are not required to use JPMS. If you don't want to use it, you don't have to. The default behaviour is to have fully open, fully exported automatic modules. In Java, you can use the `Add-Opens` and `Add-Exports` manifest attributes to selectively bypass module restrictions of other mods at launch time, and we've added explicit support for these when loading your Forge mods. At compile-time, you can use existing solutions such as the extra-java-module-info Gradle plugin to deal with non-modular dependencies and add extra opens and exports to other modules. Here's an example on how to make the internal package `com.example.examplemod.internal` open to your mod in your build.gradle: tasks.named('jar', Jar) { manifest { attributes([ 'Add-Opens' : 'com.example.examplemod/com.example.examplemod.internal' 'Specification-Title' : mod_id, 'Specification-Vendor' : mod_authors // (...) ]) } } With the above in your mod's jar manifest, you can now reflectively access the classes inside that internal package. Multiple entries are separated with a space, as per Java's official spec. You can also use Add-Exports to directly call without reflection, however you'd need to use the Gradle plugin mentioned earlier to be able to compile. The syntax for Add-Exports is the same as Add-Opens, and instructions for the compile-time step with the Gradle plugin are detailed later in this post. Remember to prefer the opens and exports keywords inside module-info.java for sources you control. The Add-Opens/Add-Exports attributes are only intended for forcing open other mods.   What else is new with module support? Previously, the runtime module name was always forced to the first mod ID in your `mods.toml` file and all packages were forced fully open and exported. Module names are now distinguished from mod IDs, meaning the module name in your module-info.java can be different from the mod ID in your `mods.toml`. This allows you to have a more descriptive module name that doesn't have to be the same as your mod ID, however we strongly recommend including your mod ID as part of your module name to aid troubleshooting. The `Automatic-Module-Name` manifest attribute is now also honoured, allowing you to specify a module name for your mod without needing to create a `module-info.java` file. This is particularly useful for mods that don't care about JPMS features but want to have a more descriptive module name and easier integration with other mods that do use JPMS.   How do I use it? The first step is to create a `module-info.java` file in your mod's source directory. This file should be in the same package as your main mod class, and should look something like this: open module com.example.examplemod { requires net.minecraftforge.eventbus; requires net.minecraftforge.fmlcore; requires net.minecraftforge.forge; requires net.minecraftforge.javafmlmod; requires net.minecraftforge.mergetool.api; requires org.slf4j; requires logging; } For now, we're leaving the whole module open to reflection, which is a good starting point. When we know we want to close something off, we can remove the open modifier from the module and open or export individual packages instead. Remember that you need to be open to Forge (module name net.minecraftforge.forge), otherwise it can't call your mod's constructor. Next is fixing modules in Gradle. While Forge and Java support modules properly, Gradle does not put automatic modules on the module path by default, meaning that the logging module (from com.mojang:logging) is not found. To fix this, add the Gradle plugin and add a compile-time module definition for that Mojang library: plugins { // (...) id 'org.gradlex.extra-java-module-info' version "1.9" } // (...) extraJavaModuleInfo { failOnMissingModuleInfo = false automaticModule("com.mojang:logging", "logging") } The automatic module override specified in your build.gradle should match the runtime one to avoid errors. You can do the same for any library or mod dependency that is missing either a module-info or explicit Automatic-Module-Name, however be aware that you may need to update your mod once said library adds one. That's all you need to get started with module support in your mods. You can learn more about modules and how to use them at dev.java.
    • Faire la mise à jour grâce à ce lien m'a aider personnellement, merci à @Paint_Ninja. https://www.amd.com/en/support 
  • Topics

×
×
  • Create New...

Important Information

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