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); saves.loadInto(npcRegistry);
shops.load(); shops.load();

View File

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

View File

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

View File

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

View File

@ -122,10 +122,16 @@ public class ItemAction extends NPCShopAction {
continue; continue;
if (tooDamaged(toMatch)) if (tooDamaged(toMatch))
continue; continue;
toMatch = toMatch.clone();
for (int j = 0; j < items.size(); j++) { for (int j = 0; j < items.size(); j++) {
if (!matches(items.get(j), toMatch)) if (!matches(items.get(j), toMatch))
continue; 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) return IntStream.range(0, req.size()).map(i -> req.get(i) == 0 ? 0 : has.get(i) / req.get(i)).reduce(Math::min)