-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
HumanController.java
130 lines (109 loc) · 4.75 KB
/
HumanController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package net.citizensnpcs.nms.v1_13_R2.entity;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_13_R2.CraftWorld;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import com.mojang.authlib.GameProfile;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Colorizer;
import net.citizensnpcs.npc.AbstractEntityController;
import net.citizensnpcs.npc.skin.Skin;
import net.citizensnpcs.npc.skin.SkinnableEntity;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;
import net.minecraft.server.v1_13_R2.PlayerInteractManager;
import net.minecraft.server.v1_13_R2.WorldServer;
public class HumanController extends AbstractEntityController {
public HumanController() {
super();
}
@Override
protected Entity createEntity(final Location at, final NPC npc) {
final WorldServer nmsWorld = ((CraftWorld) at.getWorld()).getHandle();
String coloredName = Colorizer.parseColors(npc.getFullName());
String[] nameSplit = Util.splitPlayerName(coloredName);
String name = nameSplit[0];
final String prefixCapture = nameSplit[1], suffixCapture = nameSplit[2];
UUID uuid = npc.getUniqueId();
if (uuid.version() == 4) { // clear version
long msb = uuid.getMostSignificantBits();
msb &= ~0x0000000000004000L;
msb |= 0x0000000000002000L;
uuid = new UUID(msb, uuid.getLeastSignificantBits());
}
final GameProfile profile = new GameProfile(uuid, name);
final EntityHumanNPC handle = new EntityHumanNPC(nmsWorld.getServer().getServer(), nmsWorld, profile,
new PlayerInteractManager(nmsWorld), npc);
Skin skin = handle.getSkinTracker().getSkin();
if (skin != null) {
skin.apply(handle);
}
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
if (getBukkitEntity() == null || !getBukkitEntity().isValid())
return;
boolean removeFromPlayerList = npc.data().get("removefromplayerlist",
Setting.REMOVE_PLAYERS_FROM_PLAYER_LIST.asBoolean());
NMS.addOrRemoveFromPlayerList(getBukkitEntity(), removeFromPlayerList);
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
Scoreboard scoreboard = Util.getDummyScoreboard();
String teamName = profile.getId().toString().substring(0, 16);
Team team = scoreboard.getTeam(teamName);
int mode = 2;
if (team == null) {
team = scoreboard.registerNewTeam(teamName);
mode = 0;
}
if (prefixCapture != null) {
team.setPrefix(prefixCapture);
}
if (suffixCapture != null) {
team.setSuffix(suffixCapture);
}
team.addPlayer(handle.getBukkitEntity());
handle.getNPC().data().set(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, teamName);
Util.sendTeamPacketToOnlinePlayers(team, mode);
}
}
}, 20);
handle.getBukkitEntity().setSleepingIgnored(true);
return handle.getBukkitEntity();
}
@Override
public Player getBukkitEntity() {
return (Player) super.getBukkitEntity();
}
@Override
public void remove() {
Player entity = getBukkitEntity();
if (entity != null) {
if (Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
String teamName = entity.getUniqueId().toString().substring(0, 16);
Scoreboard scoreboard = Util.getDummyScoreboard();
Team team = scoreboard.getTeam(teamName);
if (team != null && team.hasPlayer(entity)) {
if (team.getSize() == 1) {
Util.sendTeamPacketToOnlinePlayers(team, 1);
team.unregister();
}
else {
team.removePlayer(entity);
}
}
}
NMS.removeFromWorld(entity);
SkinnableEntity npc = entity instanceof SkinnableEntity ? (SkinnableEntity) entity : null;
npc.getSkinTracker().onRemoveNPC();
}
NMS.remove(entity);
// Paper decided to break Spigot compatibility.
// super.remove();
}
}