Check lookclose new target from event

This commit is contained in:
fullwall 2021-01-23 10:52:53 +08:00
parent 351cee2cc6
commit 80187501d0
2 changed files with 62 additions and 15 deletions

View File

@ -0,0 +1,33 @@
package net.citizensnpcs.editor;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import net.citizensnpcs.api.gui.InventoryMenuPage;
import net.citizensnpcs.api.gui.InventoryMenuSlot;
import net.citizensnpcs.api.gui.Menu;
import net.citizensnpcs.api.gui.MenuContext;
import net.citizensnpcs.api.gui.MenuSlot;
import net.citizensnpcs.api.util.Messaging;
@Menu(title = "NPC Equipment", type = InventoryType.CHEST, dimensions = { 3, 3 })
public class EquipmentGUI extends InventoryMenuPage {
@MenuSlot(value = { 0, 1 }, material = Material.DIAMOND_SWORD, amount = 1)
private InventoryMenuSlot slot;
@Override
public void create(MenuContext ctx) {
}
@Override
public void onClick(InventoryMenuSlot slot, InventoryClickEvent event) {
Messaging.log(event);
}
@Override
public void onClose(HumanEntity player) {
Messaging.log("CLOSED", player);
}
}

View File

@ -87,6 +87,9 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
if (old != lookingAt) { if (old != lookingAt) {
NPCLookCloseChangeTargetEvent event = new NPCLookCloseChangeTargetEvent(npc, old, lookingAt); NPCLookCloseChangeTargetEvent event = new NPCLookCloseChangeTargetEvent(npc, old, lookingAt);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
if (lookingAt != event.getNewTarget() && event.getNewTarget() != null && !isValid(event.getNewTarget())) {
return;
}
lookingAt = event.getNewTarget(); lookingAt = event.getNewTarget();
} }
} }
@ -111,19 +114,6 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
return lookingAt; return lookingAt;
} }
private boolean tryInvalidateTarget() {
if (lookingAt == null)
return true;
if (!lookingAt.isOnline() || !lookingAt.isValid() || lookingAt.getWorld() != npc.getEntity().getWorld()
|| isInvisible(lookingAt)
|| lookingAt.getLocation(PLAYER_LOCATION).distanceSquared(NPC_LOCATION) > range * range) {
NPCLookCloseChangeTargetEvent event = new NPCLookCloseChangeTargetEvent(npc, lookingAt, null);
Bukkit.getPluginManager().callEvent(event);
lookingAt = event.getNewTarget();
}
return lookingAt == null;
}
private boolean isEqual(float[] array) { private boolean isEqual(float[] array) {
return Math.abs(array[0] - array[1]) < 0.001; return Math.abs(array[0] - array[1]) < 0.001;
} }
@ -146,6 +136,12 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
return enableRandomLook; return enableRandomLook;
} }
private boolean isValid(Player entity) {
return entity.isOnline() && entity.isValid() && entity.getWorld() == npc.getEntity().getWorld()
&& !isInvisible(entity)
&& entity.getLocation(PLAYER_LOCATION).distanceSquared(NPC_LOCATION) < range * range;
}
@Override @Override
public void load(DataKey key) { public void load(DataKey key) {
range = key.getDouble("range"); range = key.getDouble("range");
@ -162,7 +158,11 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
public void onDespawn() { public void onDespawn() {
NPCLookCloseChangeTargetEvent event = new NPCLookCloseChangeTargetEvent(npc, lookingAt, null); NPCLookCloseChangeTargetEvent event = new NPCLookCloseChangeTargetEvent(npc, lookingAt, null);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
lookingAt = event.getNewTarget(); if (event.getNewTarget() != null && isValid(event.getNewTarget())) {
lookingAt = event.getNewTarget();
} else {
lookingAt = null;
}
} }
private void randomLook() { private void randomLook() {
@ -254,12 +254,26 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable
return "LookClose{" + enabled + "}"; return "LookClose{" + enabled + "}";
} }
private boolean tryInvalidateTarget() {
if (lookingAt == null)
return true;
if (!isValid(lookingAt)) {
NPCLookCloseChangeTargetEvent event = new NPCLookCloseChangeTargetEvent(npc, lookingAt, null);
Bukkit.getPluginManager().callEvent(event);
if (event.getNewTarget() != null && isValid(event.getNewTarget())) {
lookingAt = event.getNewTarget();
} else {
lookingAt = null;
}
}
return lookingAt == null;
}
public boolean useRealisticLooking() { public boolean useRealisticLooking() {
return realisticLooking; return realisticLooking;
} }
private static final Location CACHE_LOCATION = new Location(null, 0, 0, 0); private static final Location CACHE_LOCATION = new Location(null, 0, 0, 0);
private static final Location CACHE_LOCATION2 = new Location(null, 0, 0, 0);
private static final Location NPC_LOCATION = new Location(null, 0, 0, 0); private static final Location NPC_LOCATION = new Location(null, 0, 0, 0);
private static final Location PLAYER_LOCATION = new Location(null, 0, 0, 0); private static final Location PLAYER_LOCATION = new Location(null, 0, 0, 0);
} }