Various bugfixes including for /npc sitting, item action, event listening to push/knockback events

This commit is contained in:
fullwall 2023-06-19 20:39:50 +08:00
parent c001b65988
commit ad023c3bd5
5 changed files with 18 additions and 7 deletions

View File

@ -629,7 +629,6 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
}
}
saves.loadInto(npcRegistry);
shops.load();

View File

@ -721,6 +721,8 @@ public class EventListen implements Listener {
handlers.register(new RegisteredListener(new Listener() {
}, (listener, event) -> {
try {
if (event.getClass() != kbc)
return;
Entity entity = (Entity) getEntity.invoke(event);
if (!(entity instanceof NPCHolder))
return;
@ -751,6 +753,8 @@ public class EventListen implements Listener {
}, (listener, event) -> {
if (NPCPushEvent.getHandlerList().getRegisteredListeners().length == 0)
return;
if (event.getClass() != clazz)
return;
try {
Entity entity = (Entity) getEntity.invoke(event);
if (!(entity instanceof NPCHolder))

View File

@ -443,7 +443,7 @@ public class CitizensNPC extends AbstractNPC {
public void teleport(Location location, TeleportCause reason) {
if (!isSpawned())
return;
if (hasTrait(SitTrait.class)) {
if (hasTrait(SitTrait.class) && getOrAddTrait(SitTrait.class).isSitting()) {
getOrAddTrait(SitTrait.class).setSitting(location);
}
Location npcLoc = getEntity().getLocation(CACHE_LOCATION);

View File

@ -38,6 +38,7 @@ public class SitTrait extends Trait {
if (chair != null) {
if (chair.getEntity() != null) {
chair.getEntity().eject();
npc.getEntity().teleport(npc.getEntity().getLocation().clone().add(0, 0.3, 0));
}
chair.destroy();
chair = null;
@ -56,9 +57,10 @@ public class SitTrait extends Trait {
if (SUPPORT_SITTABLE && npc.getEntity() instanceof Sittable) {
((Sittable) npc.getEntity()).setSitting(true);
if (npc.getEntity().getLocation().distance(sittingAt) > 0.05) {
if (npc.getEntity().getLocation().distance(sittingAt) >= 0.03) {
npc.teleport(sittingAt, TeleportCause.PLUGIN);
}
return;
}
if (chair == null) {
@ -78,14 +80,14 @@ public class SitTrait extends Trait {
NMS.mount(chair.getEntity(), npc.getEntity());
}
if (chair.getStoredLocation() != null && chair.getStoredLocation().distance(sittingAt) > 0.05) {
if (chair.getStoredLocation() != null && chair.getStoredLocation().distance(sittingAt) >= 0.03) {
chair.teleport(sittingAt.clone(), TeleportCause.PLUGIN);
}
}
public void setSitting(Location at) {
this.sittingAt = at != null ? at.clone() : null;
if (!isSitting()) {
this.sittingAt = at != null ? at.clone().add(0, -0.3, 0) : null;
if (at == null) {
onDespawn();
}
}

View File

@ -122,10 +122,16 @@ public class ItemAction extends NPCShopAction {
continue;
if (tooDamaged(toMatch))
continue;
toMatch = toMatch.clone();
for (int j = 0; j < items.size(); j++) {
if (!matches(items.get(j), toMatch))
continue;
has.set(j, has.get(j) + toMatch.getAmount());
int remaining = req.get(j);
int taken = toMatch.getAmount() > remaining ? remaining : toMatch.getAmount();
has.set(j, has.get(j) + taken);
if (toMatch.getAmount() - taken <= 0)
break;
toMatch.setAmount(toMatch.getAmount() - taken);
}
}
return IntStream.range(0, req.size()).map(i -> req.get(i) == 0 ? 0 : has.get(i) / req.get(i)).reduce(Math::min)