7
7
import com .comphenix .protocol .reflect .StructureModifier ;
8
8
import com .comphenix .protocol .reflect .accessors .Accessors ;
9
9
10
+ import com .comphenix .protocol .reflect .accessors .FieldAccessor ;
10
11
import com .comphenix .protocol .reflect .fuzzy .FuzzyFieldContract ;
11
12
import net .minecraft .server .level .ChunkProviderServer ;
12
13
import net .minecraft .server .level .EntityTrackerEntry ;
24
25
25
26
import java .lang .reflect .Field ;
26
27
28
+ import static com .comphenix .protocol .utility .TestUtils .setFinalField ;
29
+
27
30
import static org .junit .Assert .assertEquals ;
28
31
import static org .mockito .Mockito .mock ;
29
32
import static org .mockito .Mockito .when ;
@@ -35,7 +38,7 @@ public static void beforeClass() {
35
38
BukkitInitialization .initializeItemMeta ();
36
39
}
37
40
38
- // @Test
41
+ @ Test
39
42
public void testReflection () throws ReflectiveOperationException {
40
43
CraftWorld bukkit = mock (CraftWorld .class );
41
44
WorldServer world = mock (WorldServer .class );
@@ -44,29 +47,25 @@ public void testReflection() throws ReflectiveOperationException {
44
47
ChunkProviderServer provider = mock (ChunkProviderServer .class );
45
48
when (world .getChunkProvider ()).thenReturn (provider );
46
49
47
- // TODO unsetting final doesn't work anymore
48
50
PlayerChunkMap chunkMap = mock (PlayerChunkMap .class );
49
51
Field chunkMapField = FuzzyReflection .fromClass (ChunkProviderServer .class , true )
50
52
.getField (FuzzyFieldContract .newBuilder ().typeExact (PlayerChunkMap .class ).build ());
51
- chunkMapField .setAccessible (true );
52
- chunkMapField .set (provider , chunkMap );
53
+ setFinalField (provider , chunkMapField , chunkMap );
53
54
54
55
CraftEntity bukkitEntity = mock (CraftEntity .class );
55
56
Entity fakeEntity = mock (Entity .class );
56
57
when (fakeEntity .getBukkitEntity ()).thenReturn (bukkitEntity );
57
58
58
- PlayerChunkMap . EntityTracker tracker = mock (PlayerChunkMap . EntityTracker .class );
59
- FuzzyReflection .fromClass (EntityTracker .class , true )
60
- .getField (FuzzyFieldContract .newBuilder ().typeExact (EntityTrackerEntry .class ).build ())
61
- . set (tracker , fakeEntity );
59
+ EntityTracker tracker = mock (EntityTracker .class );
60
+ Field trackerField = FuzzyReflection .fromClass (EntityTracker .class , true )
61
+ .getField (FuzzyFieldContract .newBuilder ().typeExact (Entity .class ).build ());
62
+ setFinalField (tracker , trackerField , fakeEntity );
62
63
63
- Int2ObjectMap <PlayerChunkMap . EntityTracker > trackerMap = new Int2ObjectOpenHashMap <>();
64
+ Int2ObjectMap <EntityTracker > trackerMap = new Int2ObjectOpenHashMap <>();
64
65
trackerMap .put (1 , tracker );
65
-
66
- new StructureModifier <>(PlayerChunkMap .class , true )
67
- .withTarget (chunkMap )
68
- .withParamType (Int2ObjectMap .class , null , EntityTracker .class )
69
- .write (0 , trackerMap );
66
+ Field trackedEntitiesField = FuzzyReflection .fromClass (PlayerChunkMap .class , true )
67
+ .getField (FuzzyFieldContract .newBuilder ().typeExact (Int2ObjectMap .class ).build ());
68
+ setFinalField (chunkMap , trackedEntitiesField , trackerMap );
70
69
71
70
assertEquals (bukkitEntity , EntityUtilities .getInstance ().getEntityFromID (bukkit , 1 ));
72
71
}
0 commit comments