Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public abstract class Npc {
private static final NpcAttribute INVISIBLE_ATTRIBUTE = FancyNpcsPlugin.get().getAttributeManager().getAttributeByName(EntityType.PLAYER, "invisible");
private static final char[] localNameChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'k', 'l', 'm', 'n', 'o', 'r'};
protected final Map<UUID, Boolean> isTeamCreated = new ConcurrentHashMap<>();
protected final Map<UUID, Boolean> isForcedHidden = new ConcurrentHashMap<>();
protected final Map<UUID, Boolean> isVisibleForPlayer = new ConcurrentHashMap<>();
protected final Map<UUID, Boolean> isLookingAtPlayer = new ConcurrentHashMap<>();
protected final Map<UUID, Long> lastPlayerInteraction = new ConcurrentHashMap<>();
Expand All @@ -39,14 +40,12 @@ public Npc(NpcData data) {
}

protected String generateLocalName() {
String localName = "";
StringBuilder localName = new StringBuilder();
for (int i = 0; i < 8; i++) {
localName += "&" + localNameChars[(int) RandomUtils.randomInRange(0, localNameChars.length)];
localName.append('&').append(localNameChars[(int) RandomUtils.randomInRange(0, localNameChars.length)]);
}

localName = ChatColor.translateAlternateColorCodes('&', localName);

return localName;
return ChatColor.translateAlternateColorCodes('&', localName.toString());
}

public abstract void create();
Expand Down Expand Up @@ -98,16 +97,14 @@ protected boolean shouldBeVisible(Player player) {
return false;
}

if (FancyNpcsPlugin.get().getFancyNpcConfig().isSkipInvisibleNpcs() && data.getAttributes().getOrDefault(INVISIBLE_ATTRIBUTE, "false").equalsIgnoreCase("true") && !data.isGlowing() && data.getEquipment().isEmpty()) {
return false;
}

return true;
return !FancyNpcsPlugin.get().getFancyNpcConfig().isSkipInvisibleNpcs()
|| !data.getAttributes().getOrDefault(INVISIBLE_ATTRIBUTE, "false").equalsIgnoreCase("true")
|| data.isGlowing() || !data.getEquipment().isEmpty();
}

public void checkAndUpdateVisibility(Player player) {
FancyNpcsPlugin.get().getNpcThread().submit(() -> {
boolean shouldBeVisible = shouldBeVisible(player);
boolean shouldBeVisible = !isForcedHidden.getOrDefault(player.getUniqueId(), false) && shouldBeVisible(player);
boolean wasVisible = isVisibleForPlayer.getOrDefault(player.getUniqueId(), false);

if (shouldBeVisible && !wasVisible) {
Expand Down Expand Up @@ -182,10 +179,7 @@ public void interact(Player player, ActionTrigger actionTrigger) {
}

List<NpcAction.NpcActionData> actions = data.getActions(actionTrigger);
NpcInteractEvent npcInteractEvent = new NpcInteractEvent(this, data.getOnClick(), actions, player, actionTrigger);
npcInteractEvent.callEvent();

if (npcInteractEvent.isCancelled()) {
if (!new NpcInteractEvent(this, data.getOnClick(), actions, player, actionTrigger).callEvent()) {
return;
}

Expand Down Expand Up @@ -220,6 +214,10 @@ public Map<UUID, Boolean> getIsVisibleForPlayer() {
return isVisibleForPlayer;
}

public Map<UUID, Boolean> getIsForcedHidden() {
return isForcedHidden;
}

public Map<UUID, Boolean> getIsLookingAtPlayer() {
return isLookingAtPlayer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ public void spawn(Player player) {
);
}

NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player);
spawnEvent.callEvent();
if (spawnEvent.isCancelled()) {
if (!new NpcSpawnEvent(this, player).callEvent()) {
return;
}

Expand Down Expand Up @@ -250,7 +248,7 @@ public void update(Player player, boolean swingArm) {

npc.setGlowingTag(data.isGlowing());

if (data.getEquipment() != null && data.getEquipment().size() > 0) {
if (data.getEquipment() != null && !data.getEquipment().isEmpty()) {
List<Pair<EquipmentSlot, ItemStack>> equipmentList = new ArrayList<>();

for (NpcEquipmentSlot slot : data.getEquipment().keySet()) {
Expand Down Expand Up @@ -307,6 +305,7 @@ protected void refreshEntityData(Player player) {
serverPlayer.connection.send(setEntityDataPacket);
}

@Override
public void move(Player player, boolean swingArm) {
if (npc == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ public void spawn(Player player) {
);
}

NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player);
spawnEvent.callEvent();
if (spawnEvent.isCancelled()) {
if (!new NpcSpawnEvent(this, player).callEvent()) {
return;
}

Expand Down Expand Up @@ -245,7 +243,7 @@ public void update(Player player, boolean swingArm) {

npc.setGlowingTag(data.isGlowing());

if (data.getEquipment() != null && data.getEquipment().size() > 0) {
if (data.getEquipment() != null && !data.getEquipment().isEmpty()) {
List<Pair<EquipmentSlot, ItemStack>> equipmentList = new ArrayList<>();

for (NpcEquipmentSlot slot : data.getEquipment().keySet()) {
Expand Down Expand Up @@ -302,6 +300,7 @@ protected void refreshEntityData(Player player) {
serverPlayer.connection.send(setEntityDataPacket);
}

@Override
public void move(Player player, boolean swingArm) {
if (npc == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ public void spawn(Player player) {
);
}

NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player);
spawnEvent.callEvent();
if (spawnEvent.isCancelled()) {
if (!new NpcSpawnEvent(this, player).callEvent()) {
return;
}

Expand Down Expand Up @@ -244,7 +242,7 @@ public void update(Player player, boolean swingArm) {

npc.setGlowingTag(data.isGlowing());

if (data.getEquipment() != null && data.getEquipment().size() > 0) {
if (data.getEquipment() != null && !data.getEquipment().isEmpty()) {
List<Pair<EquipmentSlot, ItemStack>> equipmentList = new ArrayList<>();

for (NpcEquipmentSlot slot : data.getEquipment().keySet()) {
Expand Down Expand Up @@ -301,6 +299,7 @@ protected void refreshEntityData(Player player) {
serverPlayer.connection.send(setEntityDataPacket);
}

@Override
public void move(Player player, boolean swingArm) {
if (npc == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ public void spawn(Player player) {
);
}

NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player);
spawnEvent.callEvent();
if (spawnEvent.isCancelled()) {
if (!new NpcSpawnEvent(this, player).callEvent()) {
return;
}

Expand Down Expand Up @@ -249,7 +247,7 @@ public void update(Player player, boolean swingArm) {

npc.setGlowingTag(data.isGlowing());

if (data.getEquipment() != null && data.getEquipment().size() > 0) {
if (data.getEquipment() != null && !data.getEquipment().isEmpty()) {
List<Pair<EquipmentSlot, ItemStack>> equipmentList = new ArrayList<>();

for (NpcEquipmentSlot slot : data.getEquipment().keySet()) {
Expand Down Expand Up @@ -316,6 +314,7 @@ protected void refreshEntityData(Player player) {
serverPlayer.connection.send(setEntityDataPacket);
}

@Override
public void move(Player player, boolean swingArm) {
if (npc == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ public void spawn(Player player) {
);
}

NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player);
spawnEvent.callEvent();
if (spawnEvent.isCancelled()) {
if (!new NpcSpawnEvent(this, player).callEvent()) {
return;
}

Expand Down Expand Up @@ -261,7 +259,7 @@ public void update(Player player, boolean swingArm) {

npc.setGlowingTag(data.isGlowing());

if (data.getEquipment() != null && data.getEquipment().size() > 0) {
if (data.getEquipment() != null && !data.getEquipment().isEmpty()) {
List<Pair<EquipmentSlot, ItemStack>> equipmentList = new ArrayList<>();

for (NpcEquipmentSlot slot : data.getEquipment().keySet()) {
Expand Down Expand Up @@ -329,6 +327,7 @@ protected void refreshEntityData(Player player) {
serverPlayer.connection.send(setEntityDataPacket);
}

@Override
public void move(Player player, boolean swingArm) {
if (npc == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ public void spawn(Player player) {
((ServerPlayer) npc).gameProfile = new GameProfile(uuid, localName, propertyMap);
}

NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player);
spawnEvent.callEvent();
if (spawnEvent.isCancelled()) {
if (!new NpcSpawnEvent(this, player).callEvent()) {
return;
}

Expand Down Expand Up @@ -359,6 +357,7 @@ protected void refreshEntityData(Player player) {
serverPlayer.connection.send(setEntityDataPacket);
}

@Override
public void move(Player player, boolean swingArm) {
if (npc == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ public void spawn(Player player) {
);
}

NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player);
spawnEvent.callEvent();
if (spawnEvent.isCancelled()) {
if (!new NpcSpawnEvent(this, player).callEvent()) {
return;
}

Expand Down Expand Up @@ -272,7 +270,7 @@ public void update(Player player, boolean swingArm) {

npc.setGlowingTag(data.isGlowing());

if (data.getEquipment() != null && data.getEquipment().size() > 0) {
if (data.getEquipment() != null && !data.getEquipment().isEmpty()) {
List<Pair<EquipmentSlot, ItemStack>> equipmentList = new ArrayList<>();

for (NpcEquipmentSlot slot : data.getEquipment().keySet()) {
Expand Down Expand Up @@ -340,6 +338,7 @@ protected void refreshEntityData(Player player) {
serverPlayer.connection.send(setEntityDataPacket);
}

@Override
public void move(Player player, boolean swingArm) {
if (npc == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ public void spawn(Player player) {
);
}

NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player);
spawnEvent.callEvent();
if (spawnEvent.isCancelled()) {
if (!new NpcSpawnEvent(this, player).callEvent()) {
return;
}

Expand Down Expand Up @@ -272,7 +270,7 @@ public void update(Player player, boolean swingArm) {

npc.setGlowingTag(data.isGlowing());

if (data.getEquipment() != null && data.getEquipment().size() > 0) {
if (data.getEquipment() != null && !data.getEquipment().isEmpty()) {
List<Pair<EquipmentSlot, ItemStack>> equipmentList = new ArrayList<>();

for (NpcEquipmentSlot slot : data.getEquipment().keySet()) {
Expand Down Expand Up @@ -340,6 +338,7 @@ protected void refreshEntityData(Player player) {
serverPlayer.connection.send(setEntityDataPacket);
}

@Override
public void move(Player player, boolean swingArm) {
if (npc == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ public void spawn(Player player) {
);
}

NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player);
spawnEvent.callEvent();
if (spawnEvent.isCancelled()) {
if (!new NpcSpawnEvent(this, player).callEvent()) {
return;
}

Expand Down Expand Up @@ -272,7 +270,7 @@ public void update(Player player, boolean swingArm) {

npc.setGlowingTag(data.isGlowing());

if (data.getEquipment() != null && data.getEquipment().size() > 0) {
if (data.getEquipment() != null && !data.getEquipment().isEmpty()) {
List<Pair<EquipmentSlot, ItemStack>> equipmentList = new ArrayList<>();

for (NpcEquipmentSlot slot : data.getEquipment().keySet()) {
Expand Down Expand Up @@ -340,6 +338,7 @@ protected void refreshEntityData(Player player) {
serverPlayer.connection.send(setEntityDataPacket);
}

@Override
public void move(Player player, boolean swingArm) {
if (npc == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ public void spawn(Player player) {
);
}

NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player);
spawnEvent.callEvent();
if (spawnEvent.isCancelled()) {
if (!new NpcSpawnEvent(this, player).callEvent()) {
return;
}

Expand Down Expand Up @@ -350,6 +348,7 @@ protected void refreshEntityData(Player player) {
serverPlayer.connection.send(setEntityDataPacket);
}

@Override
public void move(Player player, boolean swingArm) {
if (npc == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ public void spawn(Player player) {
((ServerPlayer) npc).gameProfile = new GameProfile(uuid, localName, propertyMap);
}

NpcSpawnEvent spawnEvent = new NpcSpawnEvent(this, player);
spawnEvent.callEvent();
if (spawnEvent.isCancelled()) {
if (!new NpcSpawnEvent(this, player).callEvent()) {
return;
}

Expand Down Expand Up @@ -359,6 +357,7 @@ protected void refreshEntityData(Player player) {
serverPlayer.connection.send(setEntityDataPacket);
}

@Override
public void move(Player player, boolean swingArm) {
if (npc == null) {
return;
Expand Down
Loading