Skip to content

Commit

Permalink
flying controller
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeTheTech committed Sep 19, 2023
1 parent 888da85 commit d86d266
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
12 changes: 3 additions & 9 deletions src/main/java/lee/code/pets/pets/SheepPet.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lee.code.pets.pets;

import lee.code.pets.pets.logic.ControllerWASD;
import lee.code.pets.pets.controllers.ControllerLookFlying;
import lee.code.pets.pets.logic.FollowOwnerGoal;
import lee.code.pets.utils.CoreUtil;
import net.minecraft.nbt.CompoundTag;
Expand All @@ -17,17 +17,16 @@
public class SheepPet extends Mob {

public SheepPet(Player player, Location loc, String name) {
super(EntityType.SHEEP, ((CraftWorld) loc.getWorld()).getHandle());
super(EntityType.BEE, ((CraftWorld) loc.getWorld()).getHandle());
this.setPos(loc.getX(), loc.getY(), loc.getZ());
this.setInvulnerable(true);
this.setCustomName(Component.Serializer.fromJson(CoreUtil.serializeColorComponentJson(name)));
this.setCustomNameVisible(true);
this.collides = false;
this.setPersistenceRequired(true);
this.setTarget(((CraftPlayer) player).getHandle(), EntityTargetEvent.TargetReason.CUSTOM, false);
this.moveControl = new ControllerWASD(this, player.getUniqueId());
this.moveControl = new ControllerLookFlying(this, player.getUniqueId());
setAttributes();
setNavSettings();
}

@Override
Expand All @@ -41,11 +40,6 @@ private void setAttributes() {
this.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(0.23000000417232513D);
}

private void setNavSettings() {
this.getNavigation().pathFinder.nodeEvaluator.setCanOpenDoors(true);
this.getNavigation().pathFinder.nodeEvaluator.setCanFloat(true);
}

@Override
public boolean save(CompoundTag compoundTag) {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lee.code.pets.pets.controllers;

import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;

import java.util.UUID;

public class ControllerLookFlying extends ControllerWASD {
public ControllerLookFlying(Mob mob, UUID owner) {
super(mob, owner);
}

@Override
public void tick() {
if (mob.getPassengers().isEmpty()) {
super.tick();
return;
}
if (!(mob.getPassengers().get(0) instanceof Player player)) return;
if (!player.getUUID().equals(owner)) return;
this.rider = player;
final Vec3 riddenInput = getRiddenInput(rider);

// Calculate the movement direction based on the player's pitch and yaw angles
final float forward = Math.max(0, (float) riddenInput.z);
final float pitch = rider.getXRot();
final float yaw = rider.getYRot();
final double motionX = forward * -Math.sin(Math.toRadians(yaw)) * Math.cos(Math.toRadians(pitch));
final double motionY = forward * -Math.sin(Math.toRadians(pitch));
final double motionZ = forward * Math.cos(Math.toRadians(yaw)) * Math.cos(Math.toRadians(pitch));

// Set the mob's motion
mob.setDeltaMovement(new Vec3(motionX, motionY, motionZ));

// Adjust mob's rotation based on the player's yaw
mob.setYRot(yaw);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package lee.code.pets.pets.logic;
package lee.code.pets.pets.controllers;

import net.minecraft.util.Mth;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.control.MoveControl;
import net.minecraft.world.entity.player.Player;
Expand All @@ -9,8 +8,8 @@
import java.util.UUID;

public class ControllerWASD extends MoveControl {
private final UUID owner;
private Player rider;
protected final UUID owner;
protected Player rider;

public ControllerWASD(Mob mob, UUID owner) {
super(mob);
Expand Down Expand Up @@ -63,25 +62,16 @@ public void tick() {
if (rider.jumping) jump();
}

private Vec3 getRiddenInput(Player rider) {
protected Vec3 getRiddenInput(Player rider) {
final float f = rider.xxa * 0.5F;
float f1 = rider.zza;

if (f1 <= 0.0F) f1 *= 0.25F;
return new Vec3(f, 0.0D, f1);
}

private void jump() {
protected void jump() {
if (!mob.onGround()) return;
final double jumpHeight = 3;
final Vec3 motion = mob.getDeltaMovement();

mob.setDeltaMovement(motion.x, jumpHeight, motion.z);

float f1 = Mth.sin(mob.getYRot() * 0.017453292F);
float f2 = Mth.cos(mob.getYRot() * 0.017453292F);

mob.setDeltaMovement(motion.add((-0.4F * f1), 0.0D, (0.4F * f2)));
mob.getJumpControl().jump();
}
}

0 comments on commit d86d266

Please sign in to comment.