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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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