Jump to content

[1.7.2]Increasing movement speed based on a stat[SOLVED]


hugo_the_dwarf

Recommended Posts

Hey so im adding stats that players can have based on items they are wearing and a class that they choose, and one stat is Agility and I want it to increase movement speed.

 

Now looking at the villager mob I see they have a line called: getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.5d);

 

which I think makes them slower, as tests with the player show higher numbers mean moving faster, however putting this into my "setAgility" method just makes the screen seizure as the base value is being set every tick, and my attempts to counter act this fail. So how do I passively increase a players movement speed based on their agility stat, and do so properly?

 

here is my current code snippet in my extended player class:

public void setAgility(int value)
{
	this.agility = MathHelper.clamp_int(value + currentClass.getAgi(), -20, 20);
	if (player.getEntityAttribute(SharedMonsterAttributes.movementSpeed).getBaseValue() != 2)
	{
		player.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(2);
		System.out.println("set speed");			
	}
	//System.out.println(player.getEntityAttribute(SharedMonsterAttributes.movementSpeed).getAttributeValue());
}

 

Now this method is called in the LivingUpdateEvent event so I'm hoping that this effects server and client side, as effects of my other events work when I alter this. Am I just changing the player stat in the wrong place? PS the move speed does work even if the screen is warping in and out I did a test MP and the players zoom around (of course once I fix this the insane test numbers will be handled in a mathHelper.clamp)

Link to comment
Share on other sites

Ok so doing so makes it do it once, however the speed boost doesn't stay.

 

How can I make it set the players movespeed and be done with it untill the next stat change?

 

Nvm was setting the speed in the wrong place, aka client side.

 

EDIT 2:

ok so to have the speed effect applied one has to leave and reload the world, and changing the stat resets it back to default walk speed..

 

if (props.getAgility() != (agiMod - props.getClassModifers()[1]))
			{
				 props.setAgility(agiMod);
				 player.getEntityAttribute(SharedMonsterAttributes.movementSpeed)
				 .setBaseValue(MathHelper.clamp_double(SharedMonsterAttributes.movementSpeed.getDefaultValue() + props.getAgility() / 125, 0.45d, 0.7d));

				 System.out.println(player.getEntityAttribute(SharedMonsterAttributes.movementSpeed).getAttributeValue());
			}

 

Do I have to use a command to refresh the player?

Link to comment
Share on other sites

Walking speed is defined in net.minecraft.entity.player.PlayerCapabilities and it's pretty useful and is normally saved with the player data.

    public void writeCapabilitiesToNBT(NBTTagCompound p_75091_1_)
    {
        NBTTagCompound nbttagcompound1 = new NBTTagCompound();
        nbttagcompound1.setBoolean("invulnerable", this.disableDamage);
        nbttagcompound1.setBoolean("flying", this.isFlying);
        nbttagcompound1.setBoolean("mayfly", this.allowFlying);
        nbttagcompound1.setBoolean("instabuild", this.isCreativeMode);
        nbttagcompound1.setBoolean("mayBuild", this.allowEdit);
        nbttagcompound1.setFloat("flySpeed", this.flySpeed);
        nbttagcompound1.setFloat("walkSpeed", this.walkSpeed);
        p_75091_1_.setTag("abilities", nbttagcompound1);
    }

    public void readCapabilitiesFromNBT(NBTTagCompound p_75095_1_)
    {
        if (p_75095_1_.hasKey("abilities", 10))
        {
            NBTTagCompound nbttagcompound1 = p_75095_1_.getCompoundTag("abilities");
            this.disableDamage = nbttagcompound1.getBoolean("invulnerable");
            this.isFlying = nbttagcompound1.getBoolean("flying");
            this.allowFlying = nbttagcompound1.getBoolean("mayfly");
            this.isCreativeMode = nbttagcompound1.getBoolean("instabuild");

            if (nbttagcompound1.hasKey("flySpeed", 99))
            {
                this.flySpeed = nbttagcompound1.getFloat("flySpeed");
                this.walkSpeed = nbttagcompound1.getFloat("walkSpeed");
            }

            if (nbttagcompound1.hasKey("mayBuild", 1))
            {
                this.allowEdit = nbttagcompound1.getBoolean("mayBuild");
            }
        }
    }

You can use PlayerCapabilities#setPlayerWalkSpeed(float speed), but you have to make sure to do it on both sides (for agreement). OnLivingUpdate will set attributes accordingly. These are saved automagicly.

 

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

    • 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;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
    • Remove Instant Massive Structures Mod from your server     Add new crash-reports with sites like https://paste.ee/  
    • Update your drivers: https://www.amd.com/en/support/graphics/amd-radeon-r9-series/amd-radeon-r9-200-series/amd-radeon-r9-280x
  • Topics

×
×
  • Create New...

Important Information

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