Skip to content

Commit cab15b4

Browse files
committed
Block NPC compatible with Nukkit PM1E
1 parent 6dea023 commit cab15b4

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>idk.plugin.npc</groupId>
77
<artifactId>NPC</artifactId>
8-
<version>2.7.1</version>
8+
<version>2.7.2</version>
99
<build>
1010
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
1111
<plugins>

src/main/java/idk/plugin/npc/entities/NPC_Block.java

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
11
package idk.plugin.npc.entities;
22

3+
import cn.nukkit.Player;
4+
import cn.nukkit.Server;
35
import cn.nukkit.entity.data.IntEntityData;
46
import cn.nukkit.level.GlobalBlockPalette;
7+
import cn.nukkit.level.Level;
58
import cn.nukkit.level.format.FullChunk;
69
import cn.nukkit.nbt.tag.CompoundTag;
10+
import cn.nukkit.network.protocol.AddEntityPacket;
11+
12+
import java.lang.reflect.Field;
13+
import java.lang.reflect.Method;
714

815
public class NPC_Block extends NPC_Entity {
916

1017
public static final int NID = 66;
1118

19+
private static Field f_protocol;
20+
private static Method f_getOrCreateRuntimeId;
21+
22+
static {
23+
if ("Nukkit PetteriM1 Edition".equals(Server.getInstance().getName())) {
24+
try {
25+
f_protocol = Player.class.getDeclaredField("protocol");
26+
f_getOrCreateRuntimeId = GlobalBlockPalette.class.getDeclaredMethod("getOrCreateRuntimeId", int.class, int.class, int.class);
27+
} catch (Exception e) {
28+
throw new RuntimeException(e);
29+
}
30+
}
31+
}
32+
1233
public NPC_Block(FullChunk chunk, CompoundTag nbt) {
1334
super(chunk, nbt);
1435
this.setDataFlag(DATA_FLAGS, DATA_FLAG_IMMOBILE, true);
15-
if (!this.getServer().getName().equals("Nukkit PetteriM1 Edition")) {
36+
if (f_getOrCreateRuntimeId == null) {
1637
this.setDataProperty(new IntEntityData(DATA_TYPE_INT, GlobalBlockPalette.getOrCreateRuntimeId(this.namedTag.getInt("Tile"), this.namedTag.getByte("Data"))));
1738
}
1839
}
@@ -22,9 +43,44 @@ public int getNetworkId() {
2243
return NID;
2344
}
2445

46+
@Override
47+
public void spawnTo(Player player) {
48+
if (f_getOrCreateRuntimeId == null) {
49+
super.spawnTo(player);
50+
return;
51+
}
52+
53+
if (!this.hasSpawned.containsKey(player.getLoaderId())) {
54+
Boolean hasChunk = player.usedChunks.get(Level.chunkHash(this.chunk.getX(), this.chunk.getZ()));
55+
if (hasChunk != null && hasChunk) {
56+
AddEntityPacket addEntity = new AddEntityPacket();
57+
addEntity.type = this.getNetworkId();
58+
addEntity.entityUniqueId = this.id;
59+
addEntity.entityRuntimeId = this.id;
60+
addEntity.yaw = (float) this.yaw;
61+
addEntity.headYaw = (float) this.yaw;
62+
addEntity.pitch = (float) this.pitch;
63+
addEntity.x = (float) this.x;
64+
addEntity.y = (float) this.y;
65+
addEntity.z = (float) this.z;
66+
addEntity.speedX = (float) this.motionX;
67+
addEntity.speedY = (float) this.motionY;
68+
addEntity.speedZ = (float) this.motionZ;
69+
try {
70+
int protocol = (int) f_protocol.get(player);
71+
addEntity.metadata = this.dataProperties.clone().put(new IntEntityData(DATA_VARIANT, protocol > 201 ? (int) f_getOrCreateRuntimeId.invoke(null, protocol, this.namedTag.getInt("Tile"), this.namedTag.getByte("Data")) : this.namedTag.getInt("Tile")));
72+
} catch (Exception e) {
73+
throw new RuntimeException(e);
74+
}
75+
player.dataPacket(addEntity);
76+
this.hasSpawned.put(player.getLoaderId(), player);
77+
}
78+
}
79+
}
80+
2581
@Override
2682
public void respawnToAll() {
27-
if (!this.getServer().getName().equals("Nukkit PetteriM1 Edition")) {
83+
if (f_getOrCreateRuntimeId == null) {
2884
this.setDataProperty(new IntEntityData(DATA_TYPE_INT, GlobalBlockPalette.getOrCreateRuntimeId(this.namedTag.getInt("Tile"), this.namedTag.getByte("Data"))));
2985
}
3086
super.respawnToAll();

0 commit comments

Comments
 (0)