Fix skinlayers trait, add /npc lookclose --linkedbody

This commit is contained in:
fullwall 2023-05-02 00:50:50 +08:00
parent 64bec7eb40
commit 3fe2d189f4
8 changed files with 59 additions and 14 deletions

View File

@ -1433,8 +1433,9 @@ public class NPCCommands {
@Flag({ "randomlook", "rlook" }) Boolean randomlook, @Flag("range") Double range,
@Flag("randomlookdelay") Duration randomLookDelay, @Flag("randomyawrange") String randomYaw,
@Flag("randompitchrange") String randomPitch, @Flag("randomswitchtargets") Boolean randomSwitchTargets,
@Flag("headonly") Boolean headonly, @Flag("disablewhennavigating") Boolean disableWhenNavigating,
@Flag("perplayer") Boolean perPlayer, @Flag("targetnpcs") Boolean targetNPCs) throws CommandException {
@Flag("headonly") Boolean headonly, @Flag("linkedbody") Boolean linkedbody,
@Flag("disablewhennavigating") Boolean disableWhenNavigating, @Flag("perplayer") Boolean perPlayer,
@Flag("targetnpcs") Boolean targetNPCs) throws CommandException {
boolean toggle = true;
LookClose trait = npc.getOrAddTrait(LookClose.class);
if (randomlook != null) {
@ -1454,6 +1455,11 @@ public class NPCCommands {
Messaging.sendTr(sender, headonly ? Messages.HEADONLY_SET : Messages.HEADONLY_UNSET, npc.getName());
toggle = false;
}
if (linkedbody != null) {
trait.setLinkedBody(linkedbody);
Messaging.sendTr(sender, linkedbody ? Messages.LINKEDBODY_SET : Messages.LINKEDBODY_UNSET, npc.getName());
toggle = false;
}
if (randomSwitchTargets != null) {
trait.setRandomlySwitchTargets(randomSwitchTargets);
Messaging.sendTr(sender, randomSwitchTargets ? Messages.LOOKCLOSE_RANDOM_TARGET_SWITCH_ENABLED
@ -2448,7 +2454,7 @@ public class NPCCommands {
if (yaw != null) {
NMS.setBodyYaw(npc.getEntity(), yaw);
if (npc.getEntity().getType() == EntityType.PLAYER) {
NMS.sendPositionUpdate(npc.getEntity(), false, yaw, npc.getStoredLocation().getPitch(), null);
NMS.sendPositionUpdate(npc.getEntity(), true, yaw, npc.getStoredLocation().getPitch(), null);
}
}
if (pitch != null) {

View File

@ -2,6 +2,7 @@ package net.citizensnpcs.npc;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
@ -302,7 +303,7 @@ public class CitizensNPC extends AbstractNPC {
getEntity().setMetadata("NPC", new FixedMetadataValue(CitizensAPI.getPlugin(), true));
if (getEntity() instanceof SkinnableEntity && !hasTrait(SkinLayers.class)) {
((SkinnableEntity) getEntity()).setSkinFlags((byte) 0xFF);
((SkinnableEntity) getEntity()).setSkinFlags(EnumSet.allOf(SkinLayers.Layer.class));
}
Collection<Trait> onPreSpawn = traits.values();

View File

@ -1,10 +1,13 @@
package net.citizensnpcs.npc.skin;
import java.util.Set;
import org.bukkit.entity.Player;
import com.mojang.authlib.GameProfile;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.trait.SkinLayers;
import net.citizensnpcs.trait.SkinTrait;
/**
@ -44,6 +47,10 @@ public interface SkinnableEntity extends NPCHolder {
*/
void setSkinFlags(byte flags);
default void setSkinFlags(Set<SkinLayers.Layer> flags) {
setSkinFlags(SkinLayers.Layer.toByte(flags));
}
/**
* Set the name of the player whose skin the NPC uses.
*

View File

@ -47,6 +47,8 @@ public class LookClose extends Trait implements Toggleable {
private boolean enableRandomLook = Setting.DEFAULT_RANDOM_LOOK_CLOSE.asBoolean();
@Persist("headonly")
private boolean headOnly;
@Persist("linkedbody")
private boolean linkedBody;
private Player lookingAt;
@Persist("perplayer")
private boolean perPlayer;
@ -302,6 +304,7 @@ public class LookClose extends Trait implements Toggleable {
RotationTrait rot = npc.getOrAddTrait(RotationTrait.class);
rot.getGlobalParameters().headOnly(headOnly);
rot.getGlobalParameters().linkedBody(linkedBody);
rot.getPhysicalSession().rotateToFace(lookingAt);
if (npc.getEntity().getType().name().equals("SHULKER")) {
@ -326,6 +329,10 @@ public class LookClose extends Trait implements Toggleable {
this.headOnly = headOnly;
}
public void setLinkedBody(boolean linkedBody) {
this.linkedBody = linkedBody;
}
public void setPerPlayer(boolean perPlayer) {
this.perPlayer = perPlayer;
}

View File

@ -227,6 +227,7 @@ public class RotationTrait extends Trait {
private Function<Player, Boolean> filter;
private boolean headOnly = false;
private boolean immediate = false;
private boolean linkedBody;
private float maxPitchPerTick = 10;
private float maxYawPerTick = 40;
private boolean persist = false;
@ -262,6 +263,11 @@ public class RotationTrait extends Trait {
return this;
}
public RotationParams linkedBody(boolean linked) {
this.linkedBody = linked;
return this;
}
@Override
public void load(DataKey key) {
if (key.keyExists("headOnly")) {
@ -276,6 +282,9 @@ public class RotationTrait extends Trait {
if (key.keyExists("maxYawPerTick")) {
maxYawPerTick = (float) key.getDouble("maxYawPerTick");
}
if (key.keyExists("linkedBody")) {
linkedBody = key.getBoolean("linkedBody");
}
if (key.keyExists("yawRange")) {
String[] parts = key.getString("yawRange").split(",");
yawRange = new float[] { Float.parseFloat(parts[0]), Float.parseFloat(parts[1]) };
@ -481,6 +490,10 @@ public class RotationTrait extends Trait {
rot.pitch = params.immediate ? getTargetPitch() : params.rotatePitchTowards(t, rot.pitch, getTargetPitch());
t++;
if (params.linkedBody) {
rot.bodyYaw = rot.headYaw;
}
if (Math.abs(rot.pitch - getTargetPitch()) + Math.abs(rot.headYaw - getTargetYaw()) < 0.1) {
t = -1;
rot.bodyYaw = rot.headYaw;

View File

@ -1,5 +1,8 @@
package net.citizensnpcs.trait;
import java.util.EnumSet;
import java.util.Set;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
@ -131,21 +134,17 @@ public class SkinLayers extends Trait {
}
private void setFlags() {
if (!npc.isSpawned())
if (!(npc.getEntity() instanceof SkinnableEntity))
return;
SkinnableEntity skinnable = npc.getEntity() instanceof SkinnableEntity ? (SkinnableEntity) npc.getEntity()
: null;
if (skinnable == null)
return;
int flags = 0xFF;
SkinnableEntity skinnable = (SkinnableEntity) npc.getEntity();
Set<Layer> visible = EnumSet.noneOf(Layer.class);
for (Layer layer : Layer.values()) {
if (!isVisible(layer)) {
flags &= ~layer.flag;
if (isVisible(layer)) {
visible.add(layer);
}
}
skinnable.setSkinFlags((byte) flags);
skinnable.setSkinFlags(visible);
}
public SkinLayers setVisible(Layer layer, boolean isVisible) {
@ -264,5 +263,13 @@ public class SkinLayers extends Trait {
Layer(int offset) {
this.flag = 1 << offset;
}
public static byte toByte(Set<Layer> flags) {
byte b = 0;
for (Layer layer : flags) {
b |= layer.flag;
}
return b;
}
}
}

View File

@ -212,6 +212,8 @@ public class Messages {
public static final String LINEAR_WAYPOINT_EDITOR_SELECTED_WAYPOINT = "citizens.editors.waypoints.linear.selected-waypoint";
public static final String LINEAR_WAYPOINT_EDITOR_SHOWING_MARKERS = "citizens.editors.waypoints.linear.showing-markers";
public static final String LINEAR_WAYPOINT_EDITOR_WAYPOINTS_CLEARED = "citizens.editors.waypoints.linear.waypoints-cleared";
public static final String LINKEDBODY_SET = "citizens.commands.npc.lookclose.linkedbody-set";
public static final String LINKEDBODY_UNSET = "citizens.commands.npc.lookclose.linkedbody-unset";
public static final String LLAMA_COLOR_SET = "citizens.commands.npc.llama.color-set";
public static final String LLAMA_STRENGTH_SET = "citizens.commands.npc.llama.strength-set";
public static final String LOAD_NAME_NOT_FOUND = "citizens.notifications.npc-name-not-found";

View File

@ -169,6 +169,8 @@ citizens.commands.npc.lookclose.disable-when-navigating=[[{0}]] will no longer l
citizens.commands.npc.lookclose.random-target-switch-enabled=[[{0}]] will now randomly switch targets depending on the random look delay.
citizens.commands.npc.lookclose.random-target-switch-disabled=[[{0}]] will no longer randomly switch targets.
citizens.commands.npc.lookclose.stopped=[[{0}]] will no longer rotate when players are nearby.
citizens.commands.npc.lookclose.linkedbody-set=[[{0}]] will now set head and body yaw to be the same.
citizens.commands.npc.lookclose.linkedbody-unset=[[{0}]] will no longer set head and body yaw to be the same.
citizens.commands.npc.metadata.set=[[{0}]] set to [[{1}]].
citizens.commands.npc.metadata.unset=Removed [[{0}]] from [[{1}]].
citizens.commands.npc.minecart.set=[[{0}]] now has item [[{1}]]:[[{2}]] with offset [[{3}]].