Skip to content

Commit

Permalink
Merge pull request #15 from juliarn/development
Browse files Browse the repository at this point in the history
Update 2.0-RELEASE
  • Loading branch information
juliarn authored May 3, 2020
2 parents 4d97d1c + 3a7a12e commit 09a2bdf
Show file tree
Hide file tree
Showing 11 changed files with 428 additions and 123 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# NPC-Lib
Minecraft NPC library for Paper 1.12.2+ servers.
Minecraft NPC library for 1.8-1.15 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.

## Requirements
This library can only be used on PaperSpigot servers higher or on version 1.12.2.
This library can only be used on spigot servers higher or on version 1.8.8.
The plugin [ProtocolLib](https://www.spigotmc.org/resources/protocollib.1997/) is required on your server.

## How to use
Expand All @@ -18,7 +20,7 @@ Maven
<dependency>
<groupId>com.github.juliarn</groupId>
<artifactId>npc-lib</artifactId>
<version>1.1-RELEASE</version>
<version>2.0-RELEASE</version>
</dependency>
```

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

Add ProtocolLib as dependency to your plugin.yml. It could look like this:
Expand All @@ -38,8 +40,8 @@ name: Hub
version: 1.0-SNAPSHOT
api-version: "1.13"
depend: [ProtocolLib]
author: Panamo
main: de.panamo.server.hub.ServerHub
author: juliarn
main: com.github.juliarn.server.hub.ServerHub
```
Now you're all set! You can start by creating an instance of the
[NPCPool](https://github.com/juliarn/NPC-Lib/blob/master/src/main/java/com/github/juliarn/npc/NPCPool.java) and the
Expand Down
40 changes: 31 additions & 9 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>1.1-RELEASE</version>
<version>2.0-RELEASE</version>
<packaging>jar</packaging>

<properties>
Expand All @@ -15,26 +15,32 @@

<repositories>
<repository>
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>

<repository>
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/nexus/repository/public/</url>
</repository>

<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>16.0.1</version>
<version>19.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -43,6 +49,16 @@
<version>4.5.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.github.derklaro</groupId>
<artifactId>requestbuilder</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>

<build>
Expand All @@ -59,6 +75,14 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<relocations>
<relocation>
<pattern>com.google.gson</pattern>
<shadedPattern>com.github.juliarn.relocation.gson</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
Expand Down Expand Up @@ -99,6 +123,4 @@
</plugin>
</plugins>
</build>


</project>
72 changes: 32 additions & 40 deletions src/main/java/com/github/juliarn/npc/NPC.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package com.github.juliarn.npc;


import com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.comphenix.protocol.wrappers.WrappedSignedProperty;
import com.destroystokyo.paper.profile.PlayerProfile;
import com.destroystokyo.paper.profile.ProfileProperty;
import com.github.juliarn.npc.modifier.*;
import com.github.juliarn.npc.profile.Profile;
import com.github.juliarn.npc.profile.ProfileBuilder;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

public class NPC {
Expand All @@ -33,56 +36,35 @@ public class NPC {

private final SpawnCustomizer spawnCustomizer;

private NPC(Set<ProfileProperty> profileProperties, WrappedGameProfile gameProfile, Location location, boolean lookAtPlayer, boolean imitatePlayer, SpawnCustomizer spawnCustomizer) {
private NPC(Collection<WrappedSignedProperty> profileProperties, WrappedGameProfile gameProfile, Location location, boolean lookAtPlayer, boolean imitatePlayer, SpawnCustomizer spawnCustomizer) {
this.gameProfile = gameProfile;

this.appendProperties(profileProperties);
profileProperties.forEach(property -> this.gameProfile.getProperties().put(property.getName(), property));

this.location = location;
this.lookAtPlayer = lookAtPlayer;
this.imitatePlayer = imitatePlayer;
this.spawnCustomizer = spawnCustomizer;
}

private NPC(UUID textureUUID, WrappedGameProfile gameProfile, Location location, boolean lookAtPlayer, boolean imitatePlayer, SpawnCustomizer spawnCustomizer) {
this.gameProfile = gameProfile;

PlayerProfile profile = Bukkit.createProfile(textureUUID);
profile.complete();
this.appendProperties(profile.getProperties());

this.location = location;
this.lookAtPlayer = lookAtPlayer;
this.imitatePlayer = imitatePlayer;
this.spawnCustomizer = spawnCustomizer;
}

private void appendProperties(Set<ProfileProperty> profileProperties) {
profileProperties.stream()
.map(property -> new WrappedSignedProperty(property.getName(), property.getValue(), property.getSignature()))
.forEach(property -> this.gameProfile.getProperties().put(property.getName(), property));
}

protected void show(@NotNull Player player, @NotNull JavaPlugin javaPlugin) {
VisibilityModifier visibilityModifier = new VisibilityModifier(this);
this.seeingPlayers.add(player);

visibilityModifier.queueAddToPlayerList().send(player);
VisibilityModifier visibilityModifier = new VisibilityModifier(this);
visibilityModifier.queuePlayerListChange(EnumWrappers.PlayerInfoAction.ADD_PLAYER).send(player);

Bukkit.getScheduler().runTaskLater(javaPlugin, () -> {
visibilityModifier.queueSpawn().send(player);

this.seeingPlayers.add(player);

this.spawnCustomizer.handleSpawn(this, player);

// keeping the NPC longer in the player list, otherwise the skin might not be shown sometimes.
Bukkit.getScheduler().runTaskLater(javaPlugin, () -> visibilityModifier.queueRemoveFromPlayerList().send(player), 10L);
}, 5L);
Bukkit.getScheduler().runTaskLater(javaPlugin, () -> visibilityModifier.queuePlayerListChange(EnumWrappers.PlayerInfoAction.REMOVE_PLAYER).send(player), 60L);
}, 10L);
}

protected void hide(@NotNull Player player) {
new VisibilityModifier(this)
.queueRemoveFromPlayerList()
.queuePlayerListChange(EnumWrappers.PlayerInfoAction.REMOVE_PLAYER)
.queueDestroy()
.send(player);

Expand Down Expand Up @@ -172,7 +154,7 @@ public void setImitatePlayer(boolean imitatePlayer) {

public static class Builder {

private Set<ProfileProperty> profileProperties;
private Collection<WrappedSignedProperty> profileProperties;

private final String name;

Expand Down Expand Up @@ -203,10 +185,10 @@ public Builder(@NotNull UUID textureUUID, @NotNull String name) {
/**
* Creates a new instance of the NPC builder
*
* @param profileProperties a set of Paper profile properties, including textures
* @param profileProperties a collection of profile properties, including textures
* @param name the name the NPC should have
*/
public Builder(@NotNull Set<ProfileProperty> profileProperties, @NotNull String name) {
public Builder(@NotNull Collection<WrappedSignedProperty> profileProperties, @NotNull String name) {
this.profileProperties = profileProperties;
this.name = name;
}
Expand Down Expand Up @@ -275,10 +257,20 @@ public Builder spawnCustomizer(@NotNull SpawnCustomizer spawnCustomizer) {
*/
@NotNull
public NPC build(@NotNull NPCPool pool) {
NPC npc = this.profileProperties != null
? new NPC(this.profileProperties, new WrappedGameProfile(this.uuid, this.name), this.location, this.lookAtPlayer, this.imitatePlayer, this.spawnCustomizer)
: new NPC(this.textureUUID, new WrappedGameProfile(this.uuid, this.name), this.location, this.lookAtPlayer, this.imitatePlayer, this.spawnCustomizer);

if (this.profileProperties == null) {
Profile profile = new ProfileBuilder(this.textureUUID).complete(true).build();

this.profileProperties = profile.getWrappedProperties();
}

NPC npc = new NPC(
this.profileProperties,
new WrappedGameProfile(this.uuid, this.name),
this.location,
this.lookAtPlayer,
this.imitatePlayer,
this.spawnCustomizer
);
pool.takeCareOf(npc);

return npc;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/github/juliarn/npc/NPCPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.comphenix.protocol.wrappers.EnumWrappers;
import com.github.juliarn.npc.event.PlayerNPCInteractEvent;
import com.github.juliarn.npc.modifier.AnimationModifier;
import com.github.juliarn.npc.modifier.MetadataModifier;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -144,7 +145,7 @@ public void handleSneak(PlayerToggleSneakEvent event) {

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

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ public AnimationModifier(@NotNull NPC npc) {
}

public AnimationModifier queue(@NotNull EntityAnimation entityAnimation) {
super.newContainer(PacketType.Play.Server.ANIMATION).getIntegers().write(1, entityAnimation.id);
return this.queue(entityAnimation.id);
}

public AnimationModifier queue(int animationId) {
super.newContainer(PacketType.Play.Server.ANIMATION).getIntegers().write(1, animationId);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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(0, itemSlot);
packetContainer.getItemSlots().write(MINECRAFT_VERSION < 9 ? 1 : 0, itemSlot);
packetContainer.getItemModifier().write(0, equipment);

return this;
Expand Down
Loading

0 comments on commit 09a2bdf

Please sign in to comment.