Skip to content

Commit

Permalink
Check lookclose new target from event
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jan 23, 2021
1 parent 351cee2 commit 8018750
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
33 changes: 33 additions & 0 deletions main/src/main/java/net/citizensnpcs/editor/EquipmentGUI.java
@@ -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);
}
}
44 changes: 29 additions & 15 deletions main/src/main/java/net/citizensnpcs/trait/LookClose.java
Expand Up @@ -87,6 +87,9 @@ public void findNewTarget() {
if (old != lookingAt) {
NPCLookCloseChangeTargetEvent event = new NPCLookCloseChangeTargetEvent(npc, old, lookingAt);
Bukkit.getPluginManager().callEvent(event);
if (lookingAt != event.getNewTarget() && event.getNewTarget() != null && !isValid(event.getNewTarget())) {
return;
}
lookingAt = event.getNewTarget();
}
}
Expand All @@ -111,19 +114,6 @@ public Player getTarget() {
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) {
return Math.abs(array[0] - array[1]) < 0.001;
}
Expand All @@ -146,6 +136,12 @@ public boolean isRandomLook() {
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
public void load(DataKey key) {
range = key.getDouble("range");
Expand All @@ -162,7 +158,11 @@ public void lookClose(boolean lookClose) {
public void onDespawn() {
NPCLookCloseChangeTargetEvent event = new NPCLookCloseChangeTargetEvent(npc, lookingAt, null);
Bukkit.getPluginManager().callEvent(event);
lookingAt = event.getNewTarget();
if (event.getNewTarget() != null && isValid(event.getNewTarget())) {
lookingAt = event.getNewTarget();
} else {
lookingAt = null;
}
}

private void randomLook() {
Expand Down Expand Up @@ -254,12 +254,26 @@ public String toString() {
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() {
return realisticLooking;
}

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 PLAYER_LOCATION = new Location(null, 0, 0, 0);
}

0 comments on commit 8018750

Please sign in to comment.