1
1
package idk .plugin .npc .entities ;
2
2
3
+ import cn .nukkit .Player ;
4
+ import cn .nukkit .Server ;
3
5
import cn .nukkit .entity .data .IntEntityData ;
4
6
import cn .nukkit .level .GlobalBlockPalette ;
7
+ import cn .nukkit .level .Level ;
5
8
import cn .nukkit .level .format .FullChunk ;
6
9
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 ;
7
14
8
15
public class NPC_Block extends NPC_Entity {
9
16
10
17
public static final int NID = 66 ;
11
18
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
+
12
33
public NPC_Block (FullChunk chunk , CompoundTag nbt ) {
13
34
super (chunk , nbt );
14
35
this .setDataFlag (DATA_FLAGS , DATA_FLAG_IMMOBILE , true );
15
- if (! this . getServer (). getName (). equals ( "Nukkit PetteriM1 Edition" ) ) {
36
+ if (f_getOrCreateRuntimeId == null ) {
16
37
this .setDataProperty (new IntEntityData (DATA_TYPE_INT , GlobalBlockPalette .getOrCreateRuntimeId (this .namedTag .getInt ("Tile" ), this .namedTag .getByte ("Data" ))));
17
38
}
18
39
}
@@ -22,9 +43,44 @@ public int getNetworkId() {
22
43
return NID ;
23
44
}
24
45
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
+
25
81
@ Override
26
82
public void respawnToAll () {
27
- if (! this . getServer (). getName (). equals ( "Nukkit PetteriM1 Edition" ) ) {
83
+ if (f_getOrCreateRuntimeId == null ) {
28
84
this .setDataProperty (new IntEntityData (DATA_TYPE_INT , GlobalBlockPalette .getOrCreateRuntimeId (this .namedTag .getInt ("Tile" ), this .namedTag .getByte ("Data" ))));
29
85
}
30
86
super .respawnToAll ();
0 commit comments