Skip to content

Commit

Permalink
Merge pull request #23 from juliarn/development
Browse files Browse the repository at this point in the history
Update 2.4-SNAPSHOT
  • Loading branch information
juliarn committed Oct 13, 2020
2 parents 20affd3 + 2095627 commit 7e85223
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 16 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# NPC-Lib
Minecraft NPC library for 1.8-1.15 servers.
Asynchronous, high-performance Minecraft NPC library for 1.8-1.16 servers.

This Library does only support the latest patch release of a supported version (for example 1.13.2).
Issues with older patch versions (for example 1.13.1) won't be fixed.

Expand All @@ -20,7 +21,7 @@ Maven
<dependency>
<groupId>com.github.juliarn</groupId>
<artifactId>npc-lib</artifactId>
<version>2.3-RELEASE</version>
<version>2.4-RELEASE</version>
</dependency>
```

Expand All @@ -31,7 +32,7 @@ maven {
url 'https://jitpack.io'
}
compile group: 'com.github.juliarn', name: 'npc-lib', version: '2.3-RELEASE'
compile group: 'com.github.juliarn', name: 'npc-lib', version: '2.4-RELEASE'
```

Add ProtocolLib as dependency to your plugin.yml. It could look like this:
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.juliarn</groupId>
<artifactId>npc-lib</artifactId>
<version>2.3-RELEASE</version>
<version>2.4-RELEASE</version>
<packaging>jar</packaging>

<properties>
Expand Down Expand Up @@ -46,7 +46,7 @@
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>4.5.0</version>
<version>4.6.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/github/juliarn/npc/NPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class NPC {

private final Collection<Player> seeingPlayers = new CopyOnWriteArraySet<>();

private final Collection<Player> excludedPlayers = new CopyOnWriteArraySet<>();

private final int entityId = RANDOM.nextInt(Short.MAX_VALUE);

private final WrappedGameProfile gameProfile;
Expand Down Expand Up @@ -81,6 +83,35 @@ public boolean isShownFor(Player player) {
return this.seeingPlayers.contains(player);
}

/**
* Adds a player which should be explicitly excluded from seeing this NPC
*
* @param player the player to be excluded
*/
public void addExcludedPlayer(Player player) {
this.excludedPlayers.add(player);
}

/**
* Removes a player from being explicitly excluded from seeing this NPC
*
* @param player the player to be included again
*/
public void removeExcludedPlayer(Player player) {
this.excludedPlayers.remove(player);
}

/**
* @return a modifiable collection of all players which are explicitly excluded from seeing this NPC
*/
public Collection<Player> getExcludedPlayers() {
return this.excludedPlayers;
}

public boolean isExcluded(Player player) {
return this.excludedPlayers.contains(player);
}

/**
* Creates a new animation modifier which serves methods to play animations on an NPC
*
Expand Down
32 changes: 25 additions & 7 deletions src/main/java/com/github/juliarn/npc/NPCPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerToggleSneakEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -102,18 +103,21 @@ private void npcTick() {
for (Player player : ImmutableList.copyOf(Bukkit.getOnlinePlayers())) {
for (NPC npc : this.npcMap.values()) {
if (!npc.getLocation().getWorld().equals(player.getLocation().getWorld())) {
if (npc.isShownFor(player)) {
npc.hide(player);
}
continue;
}

double distance = npc.getLocation().distanceSquared(player.getLocation());

if (distance >= this.spawnDistance && npc.isShownFor(player)) {
if ((npc.isExcluded(player) || distance >= this.spawnDistance) && npc.isShownFor(player)) {
npc.hide(player);
} else if (distance <= this.spawnDistance && !npc.isShownFor(player)) {
} else if ((!npc.isExcluded(player) && distance <= this.spawnDistance) && !npc.isShownFor(player)) {
npc.show(player, this.javaPlugin, this.tabListRemoveTicks);
}

if (npc.isLookAtPlayer() && distance <= this.actionDistance) {
if (npc.isShownFor(player) && npc.isLookAtPlayer() && distance <= this.actionDistance) {
npc.rotation().queueLookAt(player.getLocation()).send(player);
}
}
Expand All @@ -140,20 +144,33 @@ public void removeNPC(int entityId) {
}

@EventHandler
public void handleQuit(PlayerQuitEvent event) {
public void handleRespawn(PlayerRespawnEvent event) {
Player player = event.getPlayer();

this.npcMap.values().stream()
.filter(npc -> npc.isShownFor(player))
.forEach(npc -> npc.removeSeeingPlayer(player));
.forEach(npc -> npc.hide(player));
}

@EventHandler
public void handleQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();

this.npcMap.values().stream()
.filter(npc -> npc.isShownFor(player) || npc.isExcluded(player))
.forEach(npc -> {
npc.removeSeeingPlayer(player);
npc.removeExcludedPlayer(player);
});
}

@EventHandler
public void handleSneak(PlayerToggleSneakEvent event) {
Player player = event.getPlayer();

this.npcMap.values().stream()
.filter(npc -> npc.isImitatePlayer() && npc.isShownFor(player) && npc.getLocation().distanceSquared(player.getLocation()) <= this.actionDistance)
.filter(npc -> npc.isImitatePlayer() && npc.isShownFor(player))
.filter(npc -> npc.getLocation().getWorld().equals(player.getWorld()) && npc.getLocation().distanceSquared(player.getLocation()) <= this.actionDistance)
.forEach(npc -> npc.metadata().queue(MetadataModifier.EntityMetadata.SNEAKING, event.isSneaking()).send(player));
}

Expand All @@ -163,7 +180,8 @@ public void handleClick(PlayerInteractEvent event) {

if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
this.npcMap.values().stream()
.filter(npc -> npc.isImitatePlayer() && npc.isShownFor(player) && npc.getLocation().distanceSquared(player.getLocation()) <= this.actionDistance)
.filter(npc -> npc.isImitatePlayer() && npc.isShownFor(player))
.filter(npc -> npc.getLocation().getWorld().equals(player.getWorld()) && npc.getLocation().distanceSquared(player.getLocation()) <= this.actionDistance)
.forEach(npc -> npc.animation().queue(AnimationModifier.EntityAnimation.SWING_MAIN_ARM).send(player));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.Pair;
import com.github.juliarn.npc.NPC;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;

public class EquipmentModifier extends NPCModifier {

public EquipmentModifier(@NotNull NPC npc) {
Expand All @@ -17,17 +20,30 @@ public EquipmentModifier(@NotNull NPC npc) {
public EquipmentModifier queue(@NotNull EnumWrappers.ItemSlot itemSlot, @NotNull ItemStack equipment) {
PacketContainer packetContainer = super.newContainer(PacketType.Play.Server.ENTITY_EQUIPMENT);

packetContainer.getItemSlots().write(MINECRAFT_VERSION < 9 ? 1 : 0, itemSlot);
packetContainer.getItemModifier().write(0, equipment);
if (MINECRAFT_VERSION < 16) {
packetContainer.getItemSlots().write(MINECRAFT_VERSION < 9 ? 1 : 0, itemSlot);
packetContainer.getItemModifier().write(0, equipment);
} else {
packetContainer.getSlotStackPairLists().write(0, Collections.singletonList(new Pair<>(itemSlot, equipment)));
}

return this;
}

public EquipmentModifier queue(int itemSlot, @NotNull ItemStack equipment) {
PacketContainer packetContainer = super.newContainer(PacketType.Play.Server.ENTITY_EQUIPMENT);

packetContainer.getIntegers().write(MINECRAFT_VERSION < 9 ? 1 : 0, itemSlot);
packetContainer.getItemModifier().write(0, equipment);
if (MINECRAFT_VERSION < 16) {
packetContainer.getIntegers().write(MINECRAFT_VERSION < 9 ? 1 : 0, itemSlot);
packetContainer.getItemModifier().write(0, equipment);
} else {
for (EnumWrappers.ItemSlot slot : EnumWrappers.ItemSlot.values()) {
if (slot.ordinal() == itemSlot) {
packetContainer.getSlotStackPairLists().write(0, Collections.singletonList(new Pair<>(slot, equipment)));
break;
}
}
}

return this;
}
Expand Down

0 comments on commit 7e85223

Please sign in to comment.