/
CustomEntityHelperImpl.java
156 lines (146 loc) · 6.99 KB
/
CustomEntityHelperImpl.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.denizenscript.denizen.nms.v1_18.helpers;
import com.denizenscript.denizen.nms.v1_18.impl.entities.CraftFakePlayerImpl;
import com.denizenscript.denizen.nms.v1_18.impl.entities.EntityFakeArrowImpl;
import com.denizenscript.denizen.nms.v1_18.impl.entities.EntityFakePlayerImpl;
import com.denizenscript.denizen.nms.v1_18.impl.entities.EntityItemProjectileImpl;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.interfaces.CustomEntityHelper;
import com.denizenscript.denizen.nms.interfaces.FakeArrow;
import com.denizenscript.denizen.nms.interfaces.FakePlayer;
import com.denizenscript.denizen.nms.interfaces.ItemProjectile;
import com.denizenscript.denizen.nms.util.PlayerProfile;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.minecraft.server.level.ServerLevel;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_18_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.UUID;
public class CustomEntityHelperImpl implements CustomEntityHelper {
@Override
public FakeArrow spawnFakeArrow(Location location) {
CraftWorld world = (CraftWorld) location.getWorld();
EntityFakeArrowImpl arrow = new EntityFakeArrowImpl(world, location);
return arrow.getBukkitEntity();
}
@Override
public ItemProjectile spawnItemProjectile(Location location, ItemStack itemStack) {
CraftWorld world = (CraftWorld) location.getWorld();
EntityItemProjectileImpl entity = new EntityItemProjectileImpl(world.getHandle(), location, CraftItemStack.asNMSCopy(itemStack));
world.getHandle().addFreshEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM);
return entity.getBukkitEntity();
}
@Override
public FakePlayer spawnFakePlayer(Location location, String name, String skin) throws IllegalArgumentException {
return spawnFakePlayer(location, name, skin, true);
}
public static FakePlayer spawnFakePlayer(Location location, String name, String skin, boolean doAdd) throws IllegalArgumentException {
String fullName = name;
String prefix = null;
String suffix = null;
if (name == null) {
return null;
}
else if (fullName.length() > 16) {
prefix = fullName.substring(0, 16);
if (fullName.length() > 30) {
int len = 30;
name = fullName.substring(16, 30);
if (name.matches(".*[^A-Za-z0-9_].*")) {
if (fullName.length() >= 32) {
len = 32;
name = fullName.substring(16, 32);
}
else if (fullName.length() == 31) {
len = 31;
name = fullName.substring(16, 31);
}
}
else if (name.length() > 46) {
throw new IllegalArgumentException("You must specify a name with no more than 46 characters for FAKE_PLAYER entities!");
}
else {
name = ChatColor.RESET + name;
}
suffix = fullName.substring(len);
}
else {
name = fullName.substring(16);
if (!name.matches(".*[^A-Za-z0-9_].*")) {
name = ChatColor.RESET + name;
}
if (name.length() > 16) {
suffix = name.substring(16);
name = name.substring(0, 16);
}
}
}
if (skin != null && skin.length() > 16) {
throw new IllegalArgumentException("You must specify a name with no more than 16 characters for FAKE_PLAYER entity skins!");
}
CraftWorld world = (CraftWorld) location.getWorld();
ServerLevel worldServer = world.getHandle();
PlayerProfile playerProfile = new PlayerProfile(name, null);
if (skin == null && !name.matches(".*[^A-Za-z0-9_].*")) {
playerProfile = NMSHandler.getInstance().fillPlayerProfile(playerProfile);
}
if (skin != null) {
PlayerProfile skinProfile = new PlayerProfile(skin, null);
skinProfile = NMSHandler.getInstance().fillPlayerProfile(skinProfile);
playerProfile.setTexture(skinProfile.getTexture());
playerProfile.setTextureSignature(skinProfile.getTextureSignature());
}
UUID uuid = UUID.randomUUID();
if (uuid.version() == 4) {
long msb = uuid.getMostSignificantBits();
msb &= ~0x0000000000004000L;
msb |= 0x0000000000002000L;
uuid = new UUID(msb, uuid.getLeastSignificantBits());
}
playerProfile.setUniqueId(uuid);
GameProfile gameProfile = new GameProfile(playerProfile.getUniqueId(), playerProfile.getName());
gameProfile.getProperties().put("textures",
new Property("textures", playerProfile.getTexture(), playerProfile.getTextureSignature()));
final EntityFakePlayerImpl fakePlayer = new EntityFakePlayerImpl(worldServer.getServer(), worldServer, gameProfile, doAdd);
fakePlayer.forceSetPositionRotation(location.getX(), location.getY(), location.getZ(),
location.getYaw(), location.getPitch());
CraftFakePlayerImpl craftFakePlayer = fakePlayer.getBukkitEntity();
craftFakePlayer.fullName = fullName;
if (prefix != null) {
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
String teamName = "FAKE_PLAYER_TEAM_" + fullName;
String hash = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = teamName.getBytes(StandardCharsets.UTF_8);
md.update(bytes, 0, bytes.length);
hash = new BigInteger(1, md.digest()).toString(16).substring(0, 16);
}
catch (Exception e) {
Debug.echoError(e);
}
if (hash != null) {
Team team = scoreboard.getTeam(hash);
if (team == null) {
team = scoreboard.registerNewTeam(hash);
team.setPrefix(prefix);
if (suffix != null) {
team.setSuffix(suffix);
}
}
team.addPlayer(craftFakePlayer);
}
}
return craftFakePlayer;
}
}