Apply nogravity immediately on spawn and on usage

This commit is contained in:
fullwall 2022-05-26 15:58:54 +08:00
parent 8a68b575a2
commit 487d587675
5 changed files with 29 additions and 15 deletions

View File

@ -877,8 +877,8 @@ public class NPCCommands {
max = 1,
permission = "citizens.npc.gravity")
public void gravity(CommandContext args, CommandSender sender, NPC npc) {
boolean enabled = npc.getOrAddTrait(Gravity.class).toggle();
String key = !enabled ? Messages.GRAVITY_ENABLED : Messages.GRAVITY_DISABLED;
boolean nogravity = npc.getOrAddTrait(Gravity.class).toggle();
String key = !nogravity ? Messages.GRAVITY_ENABLED : Messages.GRAVITY_DISABLED;
Messaging.sendTr(sender, key, npc.getName());
}

View File

@ -10,13 +10,20 @@ import net.citizensnpcs.util.NMS;
*/
@TraitName("gravity")
public class Gravity extends Trait implements Toggleable {
@Persist
private boolean enabled;
@Persist("enabled")
private boolean nogravity;
public Gravity() {
super("gravity");
}
private void applyImmediately() {
if (nogravity && npc.getEntity() != null) {
npc.getEntity().setVelocity(npc.getEntity().getVelocity().setY(0));
NMS.setNoGravity(npc.getEntity(), nogravity);
}
}
/**
* Set whether to disable gravity or not
*
@ -24,26 +31,33 @@ public class Gravity extends Trait implements Toggleable {
* true = disable gravity, false = enable gravity
*/
public void gravitate(boolean gravitate) {
enabled = gravitate;
nogravity = gravitate;
}
public boolean hasGravity() {
return !enabled;
return !nogravity;
}
@Override
public void onSpawn() {
applyImmediately();
}
@Override
public void run() {
if (!npc.isSpawned())
return;
NMS.setNoGravity(npc.getEntity(), enabled);
NMS.setNoGravity(npc.getEntity(), nogravity);
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
this.nogravity = enabled;
}
@Override
public boolean toggle() {
return enabled = !enabled;
nogravity = !nogravity;
applyImmediately();
return nogravity;
}
}

View File

@ -538,8 +538,8 @@ public class NMS {
BRIDGE.setNavigationTarget(handle, target, speed);
}
public static void setNoGravity(Entity entity, boolean enabled) {
BRIDGE.setNoGravity(entity, enabled);
public static void setNoGravity(Entity entity, boolean nogravity) {
BRIDGE.setNoGravity(entity, nogravity);
}
public static void setPandaSitting(Entity entity, boolean sitting) {

View File

@ -149,7 +149,7 @@ public interface NMSBridge {
public void setNavigationTarget(Entity handle, Entity target, float speed);
public void setNoGravity(Entity entity, boolean enabled);
public void setNoGravity(Entity entity, boolean nogravity);
public void setPandaSitting(Entity entity, boolean sitting);

View File

@ -1200,9 +1200,9 @@ public class NMSImpl implements NMSBridge {
}
@Override
public void setNoGravity(org.bukkit.entity.Entity entity, boolean enabled) {
public void setNoGravity(org.bukkit.entity.Entity entity, boolean nogravity) {
Entity handle = getHandle(entity);
handle.setNoGravity(enabled);
handle.setNoGravity(nogravity);
if (!(handle instanceof Mob) || !(entity instanceof NPCHolder))
return;
Mob mob = (Mob) handle;
@ -1210,7 +1210,7 @@ public class NMSImpl implements NMSBridge {
if (!(mob.getMoveControl() instanceof FlyingMoveControl) || npc.data().has("flying-nogravity-float"))
return;
try {
if (enabled) {
if (nogravity) {
boolean old = (boolean) FLYING_MOVECONTROL_FLOAT_GETTER.invoke(mob.getMoveControl());
FLYING_MOVECONTROL_FLOAT_SETTER.invoke(mob.getMoveControl(), true);
npc.data().set("flying-nogravity-float", old);