Skip to content

Commit

Permalink
Fix /npc equip for horses on 1.15, add max controllable speed setting
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Feb 14, 2020
1 parent 72180ff commit 3bf5881
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 348 deletions.
1 change: 1 addition & 0 deletions main/src/main/java/net/citizensnpcs/Settings.java
Expand Up @@ -100,6 +100,7 @@ public void loadFromKey(DataKey root) {
HIGHLIGHT_COLOUR("general.color-scheme.message-highlight", "<e>"),
KEEP_CHUNKS_LOADED("npc.chunks.always-keep-loaded", false),
LOCALE("general.translation.locale", ""),
MAX_CONTROLLABLE_GROUND_SPEED("npc.controllable.max-ground-speed", 0.5),
MAX_NPC_LIMIT_CHECKS("npc.limits.max-permission-checks", 100),
MAX_NPC_SKIN_RETRIES("npc.skins.max-retries", -1),
MAX_PACKET_ENTRIES("npc.limits.max-packet-entries", 15),
Expand Down
63 changes: 33 additions & 30 deletions main/src/main/java/net/citizensnpcs/trait/Controllable.java
Expand Up @@ -34,8 +34,8 @@
/**
* Persists the controllable status for /npc controllable
*
* A controllable {@link NPC} can be mounted by a {@link Player} using right
* click or /npc mount and moved around using e.g. arrow keys.
* A controllable {@link NPC} can be mounted by a {@link Player} using right click or /npc mount and moved around using
* e.g. arrow keys.
*/
@TraitName("controllable")
public class Controllable extends Trait implements Toggleable, CommandConfigurable {
Expand Down Expand Up @@ -134,7 +134,8 @@ private void loadController() {
/**
* Attempts to mount the {@link NPC} onto the supplied {@link Player}.
*
* @param toMount the player to mount
* @param toMount
* the player to mount
* @return whether the mount was successful
*/
public boolean mount(Player toMount) {
Expand All @@ -159,16 +160,16 @@ private void onPlayerInteract(PlayerInteractEvent event) {
if (NMS.getPassengers(npc.getEntity()).contains(npc.getEntity()))
return;
switch (performed) {
case RIGHT_CLICK_BLOCK:
case RIGHT_CLICK_AIR:
controller.rightClick(event);
break;
case LEFT_CLICK_BLOCK:
case LEFT_CLICK_AIR:
controller.leftClick(event);
break;
default:
break;
case RIGHT_CLICK_BLOCK:
case RIGHT_CLICK_AIR:
controller.rightClick(event);
break;
case LEFT_CLICK_BLOCK:
case LEFT_CLICK_AIR:
controller.leftClick(event);
break;
default:
break;
}
}

Expand Down Expand Up @@ -209,10 +210,10 @@ public boolean setEnabled(boolean enabled) {
}

/**
* Configures the explicit typei.e. whether the NPC should be controlled as if
* it was a certain {@link EntityType}.
* Configures the explicit typei.e. whether the NPC should be controlled as if it was a certain {@link EntityType}.
*
* @param type the explicit type
* @param type
* the explicit type
*/
public void setExplicitType(EntityType type) {
this.explicitType = type;
Expand All @@ -238,8 +239,8 @@ private void setMountedYaw(Entity entity) {
}

/**
* Sets whether the {@link Player} attempting to mount the {@link NPC} must
* actually own the {@link NPC} to mount it.
* Sets whether the {@link Player} attempting to mount the {@link NPC} must actually own the {@link NPC} to mount
* it.
*
* @see Owner#isOwnedBy(org.bukkit.command.CommandSender)
*/
Expand All @@ -257,6 +258,7 @@ public boolean toggle() {
}

private double updateHorizontalSpeed(Entity handle, Entity passenger, double speed, float speedMod) {
double maxSpeed = Setting.MAX_CONTROLLABLE_GROUND_SPEED.asDouble();
Vector vel = handle.getVelocity();
double oldSpeed = Math.sqrt(vel.getX() * vel.getX() + vel.getZ() * vel.getZ());
double horizontal = NMS.getHorizontalMovement(passenger);
Expand All @@ -271,17 +273,17 @@ private double updateHorizontalSpeed(Entity handle, Entity passenger, double spe
new Vector(passenger.getVelocity().getX() * speedMod, 0D, passenger.getVelocity().getZ() * speedMod));

double newSpeed = Math.sqrt(vel.getX() * vel.getX() + vel.getZ() * vel.getZ());
if (newSpeed > 0.5D) {
double movementFactor = 0.5D / newSpeed;
if (newSpeed > maxSpeed) {
double movementFactor = maxSpeed / newSpeed;
vel = vel.multiply(new Vector(movementFactor, 1, movementFactor));
newSpeed = 0.5D;
newSpeed = maxSpeed;
}
handle.setVelocity(vel);

if (newSpeed > oldSpeed && speed < 0.5D) {
return (float) Math.min(0.5D, (speed + ((0.5D - speed) / 50.0D)));
if (newSpeed > oldSpeed && speed < maxSpeed) {
return (float) Math.min(maxSpeed, (speed + ((maxSpeed - speed) / 50.0D)));
} else {
return (float) Math.max(0.07D, (speed - ((speed - 0.07D) / 50.0D)));
return (float) Math.max(0, (speed - ((speed) / 50.0D)));
}
}

Expand Down Expand Up @@ -404,14 +406,15 @@ public void run(Player rider) {
}

/**
* Register a movement controller for a certain {@link EntityType} to be used
* for {@link NPC}s with that type.
* Register a movement controller for a certain {@link EntityType} to be used for {@link NPC}s with that type.
*
* Default controllers are registered for BAT, BLAZE, ENDER_DRAGON, GHAST,
* WITHER and PARROT using {@link PlayerInputAirController}.
* Default controllers are registered for BAT, BLAZE, ENDER_DRAGON, GHAST, WITHER and PARROT using
* {@link PlayerInputAirController}.
*
* @param type the entity type
* @param clazz the controller class
* @param type
* the entity type
* @param clazz
* the controller class
*/
public static void registerControllerType(EntityType type, Class<? extends MovementController> clazz) {
try {
Expand Down

0 comments on commit 3bf5881

Please sign in to comment.