Jump to content

[1.14.4] Unable to set Y motion


-fr0st-

Recommended Posts

Hello mod-makers.

I am trying to create a pair of boots that make the player bounce upon falling while wearing them. So I have this item class that does nothing special apart from extending ArmorItem, and a PlayerEvent class that does the following:

 

@Mod.EventBusSubscriber(modid = PegasusVanguard.ID)
public class ModPlayerEvents {

	@SubscribeEvent
	public static void onPlayerFall(LivingFallEvent event) {
		LivingEntity entity = event.getEntityLiving();

		if (entity instanceof PlayerEntity && !entity.getEntityWorld().isRemote) {
			Iterator<ItemStack> armorSlots = entity.getArmorInventoryList().iterator();
			entity = (PlayerEntity) entity;

			while (armorSlots.hasNext()) {
				if (armorSlots.next().getItem() instanceof ItemAirForceBoots) {
					event.setDamageMultiplier(0);
					entity.setFire(1); // why not
					
					Vec3d v = entity.getMotion();
					entity.setMotion(v.x, -v.y * 5.0D, v.z);
					entity.setJumping(true);
					entity.isAirBorne = true;
				}
			}
		}
	}
}

 

The only problem is setting the Y motion yields no result. First thing I tried is copying vanilla SlimeBlock code, but that didn't work. Then I played around with setMotion, setVelocity and travel, but I only managed to modify the X and Z components of the motion vector (and it works). I also tried, as you can see, to set some boolean flags to no avail. I am out of ideas at this point, the event fires just fine and code above setMotion is perfectly working. I don't see why just the Y axis is problematic...

Hoping someone can help out! Cheers!

Link to comment
Share on other sites

4 hours ago, -fr0st- said:

Hello mod-makers.

I am trying to create a pair of boots that make the player bounce upon falling while wearing them. So I have this item class that does nothing special apart from extending ArmorItem, and a PlayerEvent class that does the following:

 


@Mod.EventBusSubscriber(modid = PegasusVanguard.ID)
public class ModPlayerEvents {

	@SubscribeEvent
	public static void onPlayerFall(LivingFallEvent event) {
		LivingEntity entity = event.getEntityLiving();

		if (entity instanceof PlayerEntity && !entity.getEntityWorld().isRemote) {
			Iterator<ItemStack> armorSlots = entity.getArmorInventoryList().iterator();
			entity = (PlayerEntity) entity;

			while (armorSlots.hasNext()) {
				if (armorSlots.next().getItem() instanceof ItemAirForceBoots) {
					event.setDamageMultiplier(0);
					entity.setFire(1); // why not
					
					Vec3d v = entity.getMotion();
					entity.setMotion(v.x, -v.y * 5.0D, v.z);
					entity.setJumping(true);
					entity.isAirBorne = true;
				}
			}
		}
	}
}

 

The only problem is setting the Y motion yields no result. First thing I tried is copying vanilla SlimeBlock code, but that didn't work. Then I played around with setMotion, setVelocity and travel, but I only managed to modify the X and Z components of the motion vector (and it works). I also tried, as you can see, to set some boolean flags to no avail. I am out of ideas at this point, the event fires just fine and code above setMotion is perfectly working. I don't see why just the Y axis is problematic...

Hoping someone can help out! Cheers!

Okay, motions and velocity have to be handled on the client side as well. The reason why nothing happens is because you have !World#isRemote check; only the server is notified of the velocity change, the client is not, so you won't witness anything happening.

 

Also, use PlayerEntity###getItemStackFromSlot to check if the boots are equipped.

 

Lastly, you don't need to include your Mod ID in the EventBusSubscriber annotation.

 

P.S. was the Entity#world field removed? If not, just use PlayerEntity#world instead of PlayerEntity###getEntityWorld.

Edited by Differentiation
Link to comment
Share on other sites

Hey! Thanks for the reply.

I have removed the check for client side and now it... sort of works.

The problem is that it works as long as i remove event.setDamageMultiplier(0), otherwise it just behaves as before.

This is certainly not good, because I also want to be able to avoid fall damage entirely. There might be some workaround (like using potion effects or something?) but I want it to be as elegant as possible. I just don't get why it interferes like that. Thanks for everything else, though.

 

For some reason it works if i set the damage multiplier to something really small, like 0.0001F. Another thing i noticed is that if I press the spacebar in midair, then the bounce effect will not trigger, otherwise it works (90%). Might be due to some boolean flags being set?

Edited by -fr0st-
Link to comment
Share on other sites

1 hour ago, -fr0st- said:

it works as long as i remove event.setDamageMultiplier(0), otherwise it just behaves as before.

The damage multiplier is used directly on the fall distance, not the damage that you took from falling. Therefore, multiplying it by zero reduces your fall distance to 0. Which, I assume, is setting your Y motion to 0. Afraid I can't help with the space bar issue.

Link to comment
Share on other sites

2 hours ago, -fr0st- said:

Hey! Thanks for the reply.

I have removed the check for client side and now it... sort of works.

The problem is that it works as long as i remove event.setDamageMultiplier(0), otherwise it just behaves as before.

This is certainly not good, because I also want to be able to avoid fall damage entirely. There might be some workaround (like using potion effects or something?) but I want it to be as elegant as possible. I just don't get why it interferes like that. Thanks for everything else, though.

 

For some reason it works if i set the damage multiplier to something really small, like 0.0001F. Another thing i noticed is that if I press the spacebar in midair, then the bounce effect will not trigger, otherwise it works (90%). Might be due to some boolean flags being set?

Simply cancel the event after running all the code will cancel damaging the player. HOWEVER, make sure to set event priority to low or lowest not to conflict with aspects/effects of other mods.

Link to comment
Share on other sites

13 hours ago, Differentiation said:

Simply cancel the event after running all the code will cancel damaging the player. HOWEVER, make sure to set event priority to low or lowest not to conflict with aspects/effects of other mods.

Hey!

I did as you instructed me, and I set the event priority to lowest.

I tried canceling the event but even doing so after running the rest of the code just yields the same result (player being set on fire and no movement).

Link to comment
Share on other sites

8 hours ago, -fr0st- said:

Hey!

Here is what you should do instead. Set the multiplier to 0. Check if the world is on the server (isRemote is false). Then modify the velocity/motion. Then do living.velocityChanged = true.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

15 hours ago, Animefan8888 said:

Here is what you should do instead. Set the multiplier to 0. Check if the world is on the server (isRemote is false). Then modify the velocity/motion. Then do living.velocityChanged = true.

Ahh yes, this is exactly what I was looking for! Thanks :)

Link to comment
Share on other sites

On 10/19/2019 at 2:22 AM, Differentiation said:

Lastly, you don't need to include your Mod ID in the EventBusSubscriber annotation.

You do have to include it unless the annotation is attached to a class inside the main mod class (which would have the mod ID passed in as a parameter in the @Mod annotation), which is not the case here.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

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.