Skip to content

Commit 3e08630

Browse files
Jitse BoonstraJitse Boonstra
authored andcommitted
pre-release for 2.12
1 parent 7a8580a commit 3e08630

File tree

35 files changed

+521
-786
lines changed

35 files changed

+521
-786
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ NPCLib – Basic non-player character library.<br>
77
[![Discord](https://img.shields.io/badge/Support-Discord-blue.svg)](https://discord.gg/pvJGhEq)
88
=
99

10-
`This project is starting 5 Oct. 2020 no longer actively maintained. I Thank you all for riding along!`
11-
12-
1310
This is an API made specifically for spigot servers (Minecraft). Current supported versions: **1.8.8 - latest**. Lightweight replacement for Citizens. NPCLib only uses packets instead of registering the entity in the actual Minecraft server.
1411

1512
### Preview (click to play video)

api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<artifactId>npclib</artifactId>
1010
<groupId>net.jitse</groupId>
11-
<version>2.11.1-SNAPSHOT</version>
11+
<version>2.12-SNAPSHOT</version>
1212
</parent>
1313

1414
<artifactId>npclib-api</artifactId>

api/src/main/java/com/comphenix/tinyprotocol/TinyProtocol.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.util.concurrent.atomic.AtomicInteger;
2626

2727
/**
28-
* Minimized version of TinyProtocol by Kristian suited for NPCLib.
28+
* Minimized version of TinyProtocol by Kristian suited for NPCLib, modifications and refactoring by Jitse.
2929
*/
3030
public abstract class TinyProtocol {
3131
private static final AtomicInteger ID = new AtomicInteger(0);
@@ -50,7 +50,8 @@ public abstract class TinyProtocol {
5050
private static final Reflection.FieldAccessor<GameProfile> getGameProfile = Reflection.getField(PACKET_LOGIN_IN_START, GameProfile.class, 0);
5151

5252
// Speedup channel lookup
53-
private Map<String, Channel> channelLookup = new MapMaker().weakValues().makeMap();
53+
// Made channelLookup UUID-based (JMB - 23rd Jan. 2021)
54+
private Map<UUID, Channel> channelLookup = new MapMaker().weakValues().makeMap();
5455
private Listener listener;
5556

5657
// Channels that have already been removed
@@ -89,6 +90,7 @@ protected TinyProtocol(NPCLib instance) {
8990
registerChannelHandler();
9091
registerPlayers(plugin);
9192
injected = true;
93+
instance.getLogger().info("Injection complete");
9294
} catch (IllegalArgumentException ex) {
9395
// Damn you, late bind
9496
instance.getLogger().warning("Attempting to delay injection");
@@ -271,14 +273,14 @@ private PacketInterceptor injectChannelInternal(Channel channel) {
271273
}
272274

273275
private Channel getChannel(Player player) {
274-
Channel channel = channelLookup.get(player.getName());
276+
Channel channel = channelLookup.get(player.getUniqueId());
275277

276278
// Lookup channel again
277279
if (channel == null) {
278280
Object connection = getConnection.get(getPlayerHandle.invoke(player));
279281
Object manager = getManager.get(connection);
280282

281-
channelLookup.put(player.getName(), channel = getChannel.get(manager));
283+
channelLookup.put(player.getUniqueId(), channel = getChannel.get(manager));
282284
}
283285

284286
return channel;
@@ -292,9 +294,7 @@ private void close() {
292294
for (Player player : plugin.getServer().getOnlinePlayers()) {
293295
// No need to guard against this if we're closing
294296
Channel channel = getChannel(player);
295-
if (!closed) {
296-
uninjectedChannels.add(channel);
297-
}
297+
if (!closed) uninjectedChannels.add(channel);
298298

299299
// See ChannelInjector in ProtocolLib, line 590
300300
channel.eventLoop().execute(() -> channel.pipeline().remove(handlerName));
@@ -324,12 +324,10 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
324324
try {
325325
msg = onPacketInAsync(player, msg);
326326
} catch (Exception e) {
327-
instance.getLogger().severe("Error in onPacketInAsync().", e);
327+
instance.getLogger().severe("Error in onPacketInAsync()", e);
328328
}
329329

330-
if (msg != null) {
331-
super.channelRead(ctx, msg);
332-
}
330+
if (msg != null) super.channelRead(ctx, msg);
333331
}
334332

335333
// Keeping this here for testing purposes:
@@ -338,7 +336,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
338336
// try {
339337
// msg = onPacketOutAsync(player, ctx.channel(), msg);
340338
// } catch (Exception e) {
341-
// instance.getLogger().severe("Error in onPacketOutAsync().", e);
339+
// instance.getLogger().severe("Error in onPacketOutAsync()", e);
342340
// }
343341
//
344342
// if (msg != null) {
@@ -349,7 +347,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
349347
private void handleLoginStart(Channel channel, Object packet) {
350348
if (PACKET_LOGIN_IN_START.isInstance(packet)) {
351349
GameProfile profile = getGameProfile.get(packet);
352-
channelLookup.put(profile.getName(), channel);
350+
channelLookup.put(profile.getId(), channel);
353351
}
354352
}
355353
}

api/src/main/java/net/jitse/npclib/api/NPC.java

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import net.jitse.npclib.api.state.NPCAnimation;
99
import net.jitse.npclib.api.state.NPCSlot;
1010
import net.jitse.npclib.api.state.NPCState;
11-
import net.jitse.npclib.hologram.Hologram;
1211
import org.bukkit.Location;
1312
import org.bukkit.World;
1413
import org.bukkit.entity.Player;
@@ -20,51 +19,29 @@
2019
public interface NPC {
2120

2221
/**
23-
* @param player
24-
* @return unique hologram for that user
25-
*/
26-
Hologram getPlayerHologram(Player player);
27-
28-
/**
29-
*
30-
* @param targetPlayer The target player
31-
* @return object instance
32-
* @author Gatt
33-
*/
34-
NPC removePlayerLines(Player targetPlayer);
35-
/**
36-
*
37-
* @param targetPlayer The target player
38-
* @param update whether or not to update the hologram
22+
* @param player The target player
3923
* @return object instance
40-
* @author Gatt
4124
*/
42-
NPC removePlayerLines(Player targetPlayer, boolean update);
25+
NPC removeText(Player player);
4326

4427
/**
45-
*
46-
* @param uniqueLines The text that the targetPlayer will see. Null to remove
47-
* @param targetPlayer The target player
28+
* @param text The text that the player will see. Null to remove
29+
* @param player The target player
4830
* @return object instance
49-
* @author Gatt
5031
*/
51-
NPC setPlayerLines(List<String> uniqueLines, Player targetPlayer);
32+
NPC setText(Player player, List<String> text);
5233

5334
/**
54-
* @param uniqueLines The text that the targetPlayer will see
55-
* @param targetPlayer The target player
56-
* @param update whether or not to send the update packets
35+
* @param text The text that all players will see. Null to remove
5736
* @return object instance
58-
* @author Gatt
5937
*/
60-
NPC setPlayerLines(List<String> uniqueLines, Player targetPlayer, boolean update);
38+
NPC setText(List<String> text);
6139

6240
/**
63-
* @param targetPlayer The target player
64-
* @return the lines that the targetPlayer will see, if null; default lines.
65-
* @author Gatt
41+
* @param player The target player
42+
* @return the lines that the player will see, if null; default lines.
6643
*/
67-
List<String> getPlayerLines(Player targetPlayer);
44+
List<String> getText(Player player);
6845

6946
/**
7047
* Set the NPC's location.
@@ -183,8 +160,6 @@ public interface NPC {
183160
*/
184161
NPC setItem(NPCSlot slot, ItemStack item);
185162

186-
NPC setText(List<String> text);
187-
188163
/**
189164
* Get the text of an NPC
190165
*

api/src/main/java/net/jitse/npclib/internal/NPCBase.java

Lines changed: 43 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public abstract class NPCBase implements NPC, NPCPacketHandler {
5353
protected final Map<NPCSlot, ItemStack> items = new EnumMap<>(NPCSlot.class);
5454

5555
// Storage for per-player text;
56-
protected final Map<UUID, List<String>> uniqueText = new HashMap<>();
57-
protected final Map<UUID, Hologram> textDisplayHolograms = new HashMap<>();
56+
protected final Map<UUID, List<String>> playerText = new HashMap<>();
57+
protected final Map<UUID, Hologram> playerHologram = new HashMap<>();
5858

5959
public NPCBase(NPCLib instance, List<String> text) {
6060
this.instance = instance;
61-
this.text = text == null ? Collections.emptyList() : text;
61+
this.text = text != null ? text : Collections.emptyList();
6262

6363
NPCManager.add(this);
6464
}
@@ -67,64 +67,59 @@ public NPCLib getInstance() {
6767
return instance;
6868
}
6969

70-
@Override
71-
public Hologram getPlayerHologram(Player player) {
70+
// Unique per-player hologram by Gatt, modifications and refactoring done by Jitse (JMB - 23rd Jan. 2021).
71+
protected Hologram getHologram(Player player) {
7272
Validate.notNull(player, "Player cannot be null.");
73-
Hologram playerHologram = textDisplayHolograms.getOrDefault(player.getUniqueId(), null);
74-
return playerHologram;
73+
return playerHologram.getOrDefault(player.getUniqueId(), null); // If null, NMS version will return new hologram instance.
7574
}
7675

77-
7876
@Override
79-
public NPC removePlayerLines(Player targetPlayer) {
80-
Validate.notNull(targetPlayer, "Player cannot be null.");
81-
setPlayerLines(null, targetPlayer);
77+
public NPC removeText(Player player) {
78+
Validate.notNull(player, "Player cannot be null.");
79+
setText(player, null);
8280
return this;
8381
}
8482

8583
@Override
86-
public NPC removePlayerLines(Player targetPlayer, boolean update) {
87-
Validate.notNull(targetPlayer, "Player cannot be null.");
88-
setPlayerLines(null, targetPlayer, update);
89-
return this;
90-
}
84+
public NPC setText(Player player, List<String> text) {
85+
Validate.notNull(player, "Player cannot be null.");
86+
List<String> originalText = getText(player);
9187

92-
@Override
93-
public NPC setPlayerLines(List<String> uniqueLines, Player targetPlayer) {
94-
Validate.notNull(targetPlayer, "Player cannot be null.");
95-
if (uniqueLines == null) uniqueText.remove(targetPlayer.getUniqueId());
96-
else uniqueText.put(targetPlayer.getUniqueId(), uniqueLines);
88+
if (text == null) {
89+
playerText.remove(player.getUniqueId());
90+
} else playerText.put(player.getUniqueId(), text);
91+
92+
if (originalText.size() != text.size()) { // recreate the entire hologram
93+
Hologram originalHologram = getHologram(player);
94+
originalHologram.hide(player); // essentially destroy the hologram
95+
playerHologram.remove(player.getUniqueId()); // remove the old object
96+
}
97+
if (isShown(player)) { // only show hologram if the player is in range
98+
if (originalText.size() != text.size()) {
99+
getHologram(player).show(player);
100+
} else {
101+
Hologram hologram = getHologram(player);
102+
List<Object> updatePackets = hologram.getUpdatePackets(text);
103+
hologram.update(player, updatePackets);
104+
}
105+
}
97106
return this;
98107
}
99108

100109
@Override
101-
public NPC setPlayerLines(List<String> uniqueLines, Player targetPlayer, boolean update) {
102-
Validate.notNull(targetPlayer, "Player cannot be null.");
103-
List<String> originalLines = getPlayerLines(targetPlayer);
104-
setPlayerLines(uniqueLines, targetPlayer);
105-
if (update) {
106-
107-
uniqueLines = getPlayerLines(targetPlayer); // retrieve the player lines from this function, incase it's been removed.
108-
109-
if (originalLines.size() != uniqueLines.size()) { // recreate the entire hologram
110-
Hologram originalhologram = getPlayerHologram(targetPlayer);
111-
originalhologram.hide(targetPlayer); // essentially destroy the hologram
112-
textDisplayHolograms.remove(targetPlayer.getUniqueId()); // remove the old obj
113-
}
114-
115-
if (isShown(targetPlayer)) { //only show hologram if the player is in range
116-
Hologram hologram = getPlayerHologram(targetPlayer);
117-
List<Object> updatePackets = hologram.getUpdatePackets(getPlayerLines(targetPlayer));
118-
hologram.update(targetPlayer, updatePackets);
119-
}
110+
public NPC setText(List<String> text) {
111+
for (UUID uuid : shown) {
112+
Player player = Bukkit.getPlayer(uuid);
113+
if (player == null) continue;
114+
setText(player, text);
120115
}
121116
return this;
122117
}
123118

124119
@Override
125-
public List<String> getPlayerLines(Player targetPlayer) {
126-
Validate.notNull(targetPlayer, "Player cannot be null.");
127-
return uniqueText.getOrDefault(targetPlayer.getUniqueId(), text);
120+
public List<String> getText(Player player) {
121+
Validate.notNull(player, "Player cannot be null.");
122+
return playerText.getOrDefault(player.getUniqueId(), text);
128123
}
129124

130125
@Override
@@ -154,14 +149,9 @@ public void destroy() {
154149

155150
// Destroy NPC for every player that is still seeing it.
156151
for (UUID uuid : shown) {
157-
if (autoHidden.contains(uuid)) {
158-
continue;
159-
}
160-
Player plyr = Bukkit.getPlayer(uuid); // destroy the per player holograms
161-
if (plyr != null) {
162-
getPlayerHologram(plyr).hide(plyr);
163-
hide(plyr, true);
164-
}
152+
if (autoHidden.contains(uuid)) continue;
153+
Player player = Bukkit.getPlayer(uuid);
154+
if (player != null) hide(player, true); // which will also destroy the holograms
165155
}
166156
}
167157

@@ -379,33 +369,13 @@ public NPC setItem(NPCSlot slot, ItemStack item) {
379369
return this;
380370
}
381371

382-
@Override
383-
public NPC setText(List<String> text) {
384-
uniqueText.clear();
385-
386-
for (UUID shownUuid : shown) {
387-
Player player = Bukkit.getPlayer(shownUuid);
388-
if (player != null && isShown(player)) {
389-
Hologram originalHologram = getPlayerHologram(player);
390-
originalHologram.hide(player); // essentially destroy the hologram
391-
textDisplayHolograms.remove(player.getUniqueId()); // remove the old obj
392-
Hologram hologram = getPlayerHologram(player); // let it regenerate
393-
List<Object> updatePackets = hologram.getUpdatePackets(getPlayerLines(player));
394-
hologram.update(player, updatePackets);
395-
}
396-
}
397-
398-
this.text = text;
399-
return this;
400-
}
401-
402372
@Override
403373
public List<String> getText() {
404374
return text;
405375
}
406-
376+
407377
@Override
408378
public void lookAt(Location location) {
409-
sendHeadRotationPackets(location);
379+
sendHeadRotationPackets(location);
410380
}
411381
}

nms/pom.xml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88
<parent>
99
<groupId>net.jitse</groupId>
1010
<artifactId>npclib</artifactId>
11-
<version>2.11.1-SNAPSHOT</version>
11+
<version>2.12-SNAPSHOT</version>
1212
</parent>
1313

1414
<artifactId>npclib-nms</artifactId>
1515

1616
<modules>
17-
<module>v1_8_R3</module>
18-
<module>v1_9_R1</module>
19-
<module>v1_9_R2</module>
20-
<module>v1_10_R1</module>
21-
<module>v1_11_R1</module>
22-
<module>v1_12_R1</module>
23-
<module>v1_13_R1</module>
24-
<module>v1_13_R2</module>
25-
<module>v1_14_R1</module>
26-
<module>v1_15_R1</module>
27-
<module>v1_16_R1</module>
17+
<!-- <module>v1_8_R3</module>-->
18+
<!-- <module>v1_9_R1</module>-->
19+
<!-- <module>v1_9_R2</module>-->
20+
<!-- <module>v1_10_R1</module>-->
21+
<!-- <module>v1_11_R1</module>-->
22+
<!-- <module>v1_12_R1</module>-->
23+
<!-- <module>v1_13_R1</module>-->
24+
<!-- <module>v1_13_R2</module>-->
25+
<!-- <module>v1_14_R1</module>-->
26+
<!-- <module>v1_15_R1</module>-->
27+
<!-- <module>v1_16_R1</module>-->
2828
<module>v1_16_R2</module>
2929
<module>v1_16_R3</module>
3030
</modules>

0 commit comments

Comments
 (0)