[Tutorial] Riding two players in one entity.

Started by dkrdjdi on

Topic category: User side tutorials

Joined May 2013
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
[Tutorial] Riding two players in one entity.
Sat, 08/10/2024 - 14:34 (edited)

This tutorial is a way to enable two player riding on an entity.

 

The link below shows you what you're done with.

https://imgur.com/Z7Xo9ou

 

STEP1

  1. Create a custom entity.
  2. Check the ride function.
  3. Save.

 

STEP2

  1. Open the Code Editor.
  2. Add an import.

 


import java.util.ArrayList;
import java.util.List;


  1. Modify the code.

 

The original code 1

*Change the 'test' part of the code to the 'name of the Workspace AND Entity name.'


public class TestEntity extends Monster {
public TestEntity(PlayMessages.SpawnEntity packet, Level world) {
 this(TestModEntities.TEST.get(), world);
 }


 

Changed code 1


public class TestEntity extends Monster {
private List<Entity> customPassengers = new ArrayList<>(); // Add Code
public TestEntity(PlayMessages.SpawnEntity packet, Level world) {
this(TestModEntities.TEST.get(), world);
}


 

The original code 2


@Override
 public InteractionResult mobInteract(Player sourceentity, InteractionHand hand) {
 ItemStack itemstack = sourceentity.getItemInHand(hand);
 InteractionResult retval = InteractionResult.sidedSuccess(this.level().isClientSide());
 super.mobInteract(sourceentity, hand);
 sourceentity.startRiding(this);
 return retval;
  }


 

Changed code 2


@Override
   public InteractionResult mobInteract(Player sourceentity, InteractionHand hand) {
       ItemStack itemstack = sourceentity.getItemInHand(hand);
       InteractionResult retval = super.mobInteract(sourceentity, hand);
       if (!this.customPassengers.contains(sourceentity) && this.customPassengers.size() < 2) { // Add Riding 2 players.
           sourceentity.startRiding(this, true); // Add Code
           customPassengers.add(sourceentity); // Add Code
       }
       return retval;
   }


 

The original code 3


@Override
    public void travel(Vec3 dir) {
        Entity entity = this.getPassengers().isEmpty() ? null : (Entity) this.getPassengers().get(0);
        if (this.isVehicle()) {
            this.setYRot(entity.getYRot());
            this.yRotO = this.getYRot();
            this.setXRot(entity.getXRot() * 0.5F);
            this.setRot(this.getYRot(), this.getXRot());
            this.yBodyRot = entity.getYRot();
            this.yHeadRot = entity.getYRot();
            if (entity instanceof LivingEntity passenger) {
                this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED));
                float forward = passenger.zza;
                float strafe = passenger.xxa;
                super.travel(new Vec3(strafe, 0, forward));
            }
            double d1 = this.getX() - this.xo;
            double d0 = this.getZ() - this.zo;
            float f1 = (float) Math.sqrt(d1 * d1 + d0 * d0) * 4;
            if (f1 > 1.0F)
                f1 = 1.0F;
            this.walkAnimation.setSpeed(this.walkAnimation.speed() + (f1 - this.walkAnimation.speed()) * 0.4F);
            this.walkAnimation.position(this.walkAnimation.position() + this.walkAnimation.speed());
            this.calculateEntityAnimation(true);
            return;
        }
        super.travel(dir);
    }


 

Changed code 3


@Override
   public void travel(Vec3 dir) {
       if (this.isVehicle() && !this.customPassengers.isEmpty()) { // Add Code
           Entity entity = this.customPassengers.get(0); // Add Code
           this.setYRot(entity.getYRot());
           this.yRotO = this.getYRot();
           this.setXRot(entity.getXRot() * 0.5F);
           this.setRot(this.getYRot(), this.getXRot());
           this.yBodyRot = entity.getYRot();
           this.yHeadRot = entity.getYRot();
           if (entity instanceof LivingEntity passenger) {
               this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED));
               float forward = passenger.zza;
               float strafe = passenger.xxa;
               super.travel(new Vec3(strafe, 0, forward));
           }
           double d1 = this.getX() - this.xo;
           double d0 = this.getZ() - this.zo;
           float f1 = (float) Math.sqrt(d1 * d1 + d0 * d0) * 4;
           if (f1 > 1.0F)
               f1 = 1.0F;
           this.walkAnimation.setSpeed(this.walkAnimation.speed() + (f1 - this.walkAnimation.speed()) * 0.4F);
           this.walkAnimation.position(this.walkAnimation.position() + this.walkAnimation.speed());
           this.calculateEntityAnimation(true);
           this.setMaxUpStep(1.0f); // Makes you climb the hill when you have a rider in the entity.
       return;
       }
       super.travel(dir);
   }


 

Lastly, please add the code.


    public static AttributeSupplier.Builder createAttributes() {
       AttributeSupplier.Builder builder = Mob.createMobAttributes();
       builder = builder.add(Attributes.MOVEMENT_SPEED, 0.3);
       builder = builder.add(Attributes.MAX_HEALTH, 10);
       builder = builder.add(Attributes.ARMOR, 0);
       builder = builder.add(Attributes.ATTACK_DAMAGE, 3);
       builder = builder.add(Attributes.FOLLOW_RANGE, 16);
       return builder;
   }

 

//Please add the following code below this code.


@Override
protected void positionRider(Entity passenger, MoveFunction moveFunction) {
        if (this.hasPassenger(passenger)) {
            int passengerIndex = this.getPassengers().indexOf(passenger);
            double offsetX = this.getX();
            double offsetY = this.getY() + 1.0D + passenger.getMyRidingOffset(); // Change ride Y height
            double offsetZ = this.getZ();

       if (passengerIndex == 0) {
           // Location of the first passenger
           offsetX = this.getX();
           offsetZ = this.getZ();
       } else if (passengerIndex == 1) {
           // Second passenger's position
           offsetX = this.getX() - Math.sin(Math.toRadians(this.getYRot())) * -1.0D;
           offsetZ = this.getZ() + Math.cos(Math.toRadians(this.getYRot())) * -1.0D;
       }

       passenger.setPos(offsetX, offsetY, offsetZ);
   }
}
}


 

Save and test the code.

It was completed by exchanging many questions and answers with Copilot(AI) to find this method.

Edited by dkrdjdi on Sat, 08/10/2024 - 14:34
Joined May 2013
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
----------------------------…
Sun, 08/04/2024 - 10:04

----------------------------------------------------------------------------------

STEP3

Save and test.

-----------------------------------------------------------------------------------

 

The locations of the two players are affected by the server

This part has not been resolved.
I will write a new tutorial if I solve it.
Thank you.

Joined May 2013
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
The problem of Step3 written…
Thu, 08/08/2024 - 14:16

The problem of Step3 written on August 4th has been solved, thank you.

Joined May 2013
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Sorry... there was a mistake…
Thu, 08/08/2024 - 15:00

Sorry... there was a mistake with some tutorial codes.

The incorrectly entered part of this code should be sorted out.

I can't edit it now, so I'll edit it later.

 

this.calculateEntityAnimation(true);this.setMaxUpStep(1.0f); // Ensure the entity can step up higher blocks while traveling
           this.setMaxUpStep(1.0f); // Makes you climb the hill when you have a rider in the entity.
       return;
       }
       super.travel(dir);
   }

 

edit

this.calculateEntityAnimation(true);
this.setMaxUpStep(1.0f); // Makes you climb the hill when you have a rider in the entity.
       return;
       }
       super.travel(dir);
   }

Joined Apr 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I have checked the…
Mon, 08/26/2024 - 07:05

I have checked the screenshot you sent multiple times. To reduce errors, my mod and entity are also called "tests", just like your example. Then mcreator successfully compiled Java, but crashed when starting mc. The client appeared for about two seconds and then crashed.
 

Joined Apr 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Sorry, I just realized that…
Mon, 08/26/2024 - 07:12

Sorry, I just realized that it seems to be a client issue. Even after deleting the entity, it still cannot start. I need to check first

Joined Apr 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I have successfully created…
Wed, 08/28/2024 - 09:57

I have successfully created a two player ride entity by following your tutorial. Thank you! That's amazing and I want to learn more! 

How can I change only the riding position without increasing the number of passengers? 

How can I make more than two player ride an entity?

I'm not a native English speaker. I'm sorry if I didn't express myself clearly

Joined May 2013
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
 if (!this.customPassengers…
Wed, 08/28/2024 - 10:15

 if (!this.customPassengers.contains(sourceentity) && this.customPassengers.size() < 2) { // Add Riding 2 players.

 

You can adjust the number of passengers by changing the number of people in this part.

 


@Override
protected void positionRider(Entity passenger, MoveFunction moveFunction) {
    if (this.hasPassenger(passenger)) {
        int passengerIndex = this.getPassengers().indexOf(passenger);
        double offsetX = this.getX();
        double offsetY = this.getY() + 1.0D + passenger.getMyRidingOffset(); // Change ride Y height
        double offsetZ = this.getZ();

        if (passengerIndex == 0) {
            // Location of the first passenger
            offsetX = this.getX();
            offsetZ = this.getZ();
        } else if (passengerIndex == 1) {
            // Second passenger's position
            offsetX = this.getX() - Math.sin(Math.toRadians(this.getYRot())) * -1.0D;
            offsetZ = this.getZ() + Math.cos(Math.toRadians(this.getYRot())) * -1.0D;
        } else if (passengerIndex == 2) {
            // Third passenger's position
            offsetX = this.getX() + Math.sin(Math.toRadians(this.getYRot())) * 1.0D;
            offsetZ = this.getZ() - Math.cos(Math.toRadians(this.getYRot())) * 1.0D;
        }

        passenger.setPos(offsetX, offsetY, offsetZ);
    }
}

 

Just add the index number and adjust the position.

You can modify the part that says the position is 1.0D.

Joined May 2013
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
In the latest version (2024…
Wed, 11/13/2024 - 10:52

In the latest version (2024.3)
double offsetY = this.getY() + 1.0D + passenger.getMyRidingOffset();

 

You have to do this to make it work.

Double offsetY = this.getY() + 1.0D;
 

Joined Jul 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
You are a hero! I have been…
Fri, 11/15/2024 - 18:58

You are a hero! I have been looking for a feasible solution to this exact problem for 2 years now. It is really important to the core idea of my mod.
Will try after i backup my workspace