Skip to content

Commit

Permalink
Refactor PlayerControllerLook into RotationTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Apr 24, 2022
1 parent 324d94a commit 8694603
Show file tree
Hide file tree
Showing 34 changed files with 105 additions and 1,459 deletions.
6 changes: 6 additions & 0 deletions main/src/main/java/net/citizensnpcs/commands/NPCCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -911,29 +911,35 @@ public void hologram(CommandContext args, CommandSender sender, NPC npc) throws
if (args.argsLength() == 2) {
throw new CommandException(Messages.HOLOGRAM_INVALID_LINE);
}

int idx = Math.max(0, args.getInteger(2));
if (idx >= trait.getLines().size()) {
throw new CommandException(Messages.HOLOGRAM_INVALID_LINE);
}

if (args.argsLength() == 3) {
throw new CommandException(Messages.HOLOGRAM_TEXT_MISSING);
}

trait.setLine(idx, args.getJoinedStrings(3));
Messaging.sendTr(sender, Messages.HOLOGRAM_LINE_SET, idx, args.getJoinedStrings(3));
} else if (args.getString(1).equalsIgnoreCase("add")) {
if (args.argsLength() == 2) {
throw new CommandException(Messages.HOLOGRAM_TEXT_MISSING);
}

trait.addLine(args.getJoinedStrings(2));
Messaging.sendTr(sender, Messages.HOLOGRAM_LINE_ADD, args.getJoinedStrings(2));
} else if (args.getString(1).equalsIgnoreCase("remove")) {
if (args.argsLength() == 2) {
throw new CommandException(Messages.HOLOGRAM_INVALID_LINE);
}

int idx = Math.max(0, args.getInteger(2));
if (idx >= trait.getLines().size()) {
throw new CommandException(Messages.HOLOGRAM_INVALID_LINE);
}

trait.removeLine(idx);
Messaging.sendTr(sender, Messages.HOLOGRAM_LINE_REMOVED, idx);
} else if (args.getString(1).equalsIgnoreCase("clear")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import net.citizensnpcs.trait.Poses;
import net.citizensnpcs.trait.Powered;
import net.citizensnpcs.trait.RabbitType;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.Saddle;
import net.citizensnpcs.trait.ScoreboardTrait;
import net.citizensnpcs.trait.ScriptTrait;
Expand Down Expand Up @@ -88,6 +89,7 @@ public CitizensTraitFactory() {
registerTrait(TraitInfo.create(Poses.class));
registerTrait(TraitInfo.create(Powered.class));
registerTrait(TraitInfo.create(RabbitType.class));
registerTrait(TraitInfo.create(RotationTrait.class));
registerTrait(TraitInfo.create(Saddle.class));
registerTrait(TraitInfo.create(ScoreboardTrait.class));
registerTrait(TraitInfo.create(ScriptTrait.class));
Expand Down
56 changes: 37 additions & 19 deletions main/src/main/java/net/citizensnpcs/trait/HologramTrait.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.citizensnpcs.trait;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -59,16 +60,15 @@ public HologramTrait() {
*/
public void addLine(String text) {
lines.add(text);
onDespawn();
onSpawn();
reloadLineHolograms();
}

/**
* Clears all hologram lines
*/
public void clear() {
onDespawn();
lines.clear();
reloadLineHolograms();
}

private NPC createHologram(String line, double heightOffset) {
Expand Down Expand Up @@ -132,7 +132,7 @@ public double getLineHeight() {
* @return the hologram lines, in bottom-up order
*/
public List<String> getLines() {
return lines;
return Collections.unmodifiableList(lines);
}

private double getMaxHeight() {
Expand Down Expand Up @@ -174,6 +174,21 @@ public void onSpawn() {
if (npc.requiresNameHologram() && lastNameplateVisible) {
nameNPC = createHologram(npc.getFullName(), 0);
}

for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
lineHolograms.add(createHologram(Placeholders.replace(line, null, npc), getHeight(i)));
}
}

private void reloadLineHolograms() {
for (NPC npc : lineHolograms) {
npc.destroy();
}
lineHolograms.clear();

if (!npc.isSpawned())
return;
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
lineHolograms.add(createHologram(Placeholders.replace(line, null, npc), getHeight(i)));
Expand All @@ -186,9 +201,12 @@ public void onSpawn() {
* @param idx
*/
public void removeLine(int idx) {
if (idx < 0 || idx >= lines.size())
return;

lines.remove(idx);
onDespawn();
onSpawn();

reloadLineHolograms();
}

@Override
Expand Down Expand Up @@ -267,8 +285,8 @@ public void run() {
*/
public void setDirection(HologramDirection direction) {
this.direction = direction;
onDespawn();
onSpawn();

reloadLineHolograms();
}

/**
Expand All @@ -281,17 +299,17 @@ public void setDirection(HologramDirection direction) {
*/
public void setLine(int idx, String text) {
if (idx == lines.size()) {
lines.add(text);
} else {
lines.set(idx, text);
if (idx < lineHolograms.size()) {
lineHolograms.get(idx).setName(Placeholders.replace(text, null, npc));
return;
}
addLine(text);
return;
}

onDespawn();
onSpawn();
lines.set(idx, text);
if (idx < lineHolograms.size()) {
lineHolograms.get(idx).setName(Placeholders.replace(text, null, npc));
return;
}

reloadLineHolograms();
}

/**
Expand All @@ -303,8 +321,8 @@ public void setLine(int idx, String text) {
*/
public void setLineHeight(double height) {
lineHeight = height;
onDespawn();
onSpawn();

reloadLineHolograms();
}

public enum HologramDirection {
Expand Down
44 changes: 30 additions & 14 deletions main/src/main/java/net/citizensnpcs/trait/RotationTrait.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package net.citizensnpcs.trait;

import org.bukkit.Location;
import org.bukkit.entity.Entity;

import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;

@TraitName("rotationtrait")
public class RotationTrait extends Trait {
protected float maxPitchRotationPerTick = 10;
protected float maxYawRotationPerTick = 40;
protected boolean rotating;
protected double tx;
protected double ty;
protected double tz;
protected float xMaxRotAngle = 10;
protected float yMaxRotSpeed = 40;

public RotationTrait() {
super("rotationtrait");
Expand All @@ -22,10 +25,6 @@ private double getEyeY() {
return NMS.getHeight(npc.getEntity());
}

private double getX() {
return npc.getStoredLocation().getX();
}

protected float getTargetPitchDifference() {
double dx = tx - getX();
double dy = ty - (getY() + getEyeY());
Expand All @@ -34,18 +33,35 @@ protected float getTargetPitchDifference() {
return (float) -Math.toDegrees(Math.atan2(dy, diag));
}

private double getY() {
return npc.getStoredLocation().getY();
}

protected float getTargetYawDifference() {
return (float) Math.toDegrees(Math.atan2(tz - getZ(), tx - getX())) - 90.0F;
}

private double getX() {
return npc.getStoredLocation().getX();
}

private double getY() {
return npc.getStoredLocation().getY();
}

private double getZ() {
return npc.getStoredLocation().getZ();
}

public void rotateToFace(Entity target) {
Location loc = target.getLocation();
loc.setY(loc.getY() + NMS.getHeight(target));
rotateToFace(loc);
}

public void rotateToFace(Location target) {
this.tx = target.getX();
this.ty = target.getY();
this.tz = target.getZ();
this.rotating = true;
}

protected float rotateTowards(float target, float current, float maxRotPerTick) {
float diff = Util.clamp(current - target);
return target + clamp(diff, -maxRotPerTick, maxRotPerTick);
Expand All @@ -62,8 +78,8 @@ public void run() {
}
if (this.rotating) {
this.rotating = false;
NMS.setHeadYaw(npc.getEntity(),
Util.clamp(rotateTowards(NMS.getHeadYaw(npc.getEntity()), getTargetYawDifference(), this.yMaxRotSpeed)));
NMS.setHeadYaw(npc.getEntity(), Util.clamp(rotateTowards(NMS.getHeadYaw(npc.getEntity()),
getTargetYawDifference(), this.maxYawRotationPerTick)));
float d = Util.clamp(NMS.getHeadYaw(npc.getEntity()) - 40);
if (d > NMS.getYaw(npc.getEntity())) {
NMS.setBodyYaw(npc.getEntity(), d);
Expand All @@ -80,8 +96,8 @@ public void run() {
NMS.setBodyYaw(npc.getEntity(), d);
}
}
NMS.setPitch(npc.getEntity(),
rotateTowards(npc.getStoredLocation().getPitch(), getTargetPitchDifference(), this.xMaxRotAngle));
NMS.setPitch(npc.getEntity(), rotateTowards(npc.getStoredLocation().getPitch(), getTargetPitchDifference(),
this.maxPitchRotationPerTick));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import net.citizensnpcs.nms.v1_10_R1.network.EmptySocket;
import net.citizensnpcs.nms.v1_10_R1.util.NMSImpl;
import net.citizensnpcs.nms.v1_10_R1.util.PlayerControllerJump;
import net.citizensnpcs.nms.v1_10_R1.util.PlayerControllerLook;
import net.citizensnpcs.nms.v1_10_R1.util.PlayerControllerMove;
import net.citizensnpcs.nms.v1_10_R1.util.PlayerNavigation;
import net.citizensnpcs.npc.CitizensNPC;
Expand All @@ -41,7 +40,6 @@
import net.minecraft.server.v1_10_R1.AttributeInstance;
import net.minecraft.server.v1_10_R1.BlockPosition;
import net.minecraft.server.v1_10_R1.DamageSource;
import net.minecraft.server.v1_10_R1.Entity;
import net.minecraft.server.v1_10_R1.EntityHuman;
import net.minecraft.server.v1_10_R1.EntityPlayer;
import net.minecraft.server.v1_10_R1.EnumGamemode;
Expand All @@ -62,7 +60,6 @@
public class EntityHumanNPC extends EntityPlayer implements NPCHolder, SkinnableEntity {
private final Map<PathType, Float> bz = Maps.newEnumMap(PathType.class);
private PlayerControllerJump controllerJump;
private PlayerControllerLook controllerLook;
private PlayerControllerMove controllerMove;
private final Map<EnumItemSlot, ItemStack> equipmentCache = Maps.newEnumMap(EnumItemSlot.class);
private boolean isTracked = false;
Expand Down Expand Up @@ -255,7 +252,6 @@ private void initialise(MinecraftServer minecraftServer) {
range.setValue(Setting.DEFAULT_PATHFINDING_RANGE.asDouble());

controllerJump = new PlayerControllerJump(this);
controllerLook = new PlayerControllerLook(this);
controllerMove = new PlayerControllerMove(this);
navigation = new PlayerNavigation(this, world);
invulnerableTicks = 0;
Expand Down Expand Up @@ -391,21 +387,12 @@ public void setSkinPersistent(String skinName, String signature, String data) {
npc.getOrAddTrait(SkinTrait.class).setSkinPersistent(skinName, signature, data);
}

public void setTargetLook(Entity target, float yawOffset, float renderOffset) {
controllerLook.a(target, yawOffset, renderOffset);
}

public void setTargetLook(Location target) {
controllerLook.a(target.getX(), target.getY(), target.getZ(), 10, 40);
}

public void setTracked() {
isTracked = true;
}

public void updateAI() {
controllerMove.c();
controllerLook.a();
controllerJump.b();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
import net.citizensnpcs.npc.ai.MCTargetStrategy.TargetNavigator;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.npc.skin.SkinnableEntity;
import net.citizensnpcs.trait.RotationTrait;
import net.citizensnpcs.trait.versioned.BossBarTrait;
import net.citizensnpcs.trait.versioned.PolarBearTrait;
import net.citizensnpcs.trait.versioned.ShulkerTrait;
Expand Down Expand Up @@ -745,7 +746,7 @@ public void look(org.bukkit.entity.Entity entity, Location to, boolean headOnly,
((EntityInsentient) handle).aQ += 360F;
}
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).setTargetLook(to);
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).rotateToFace(to);
}
}

Expand All @@ -769,7 +770,7 @@ public void look(org.bukkit.entity.Entity from, org.bukkit.entity.Entity to) {
((EntityLiving) handle).aQ += 360F;
}
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).setTargetLook(target, 10F, 40F);
((EntityHumanNPC) handle).getNPC().getOrAddTrait(RotationTrait.class).rotateToFace(to);
}
}

Expand Down
Loading

0 comments on commit 8694603

Please sign in to comment.