@@ -15,51 +15,53 @@ public class BaseScript {
15
15
private static final Pattern IMPORT_PATTERN = Pattern .compile ("import\\ s(.*?);" );
16
16
17
17
private static final String BASE_SCRIPT_IMPORTS = """
18
- import java.io.StringWriter;
19
- import java.util.Arrays;
20
- import java.util.List;
21
- import java.lang.reflect.*;
18
+ import cpw.mods.fml.common.FMLCommonHandler;
22
19
import net.minecraft.entity.player.EntityPlayerMP;
23
20
import net.minecraft.server.MinecraftServer;
24
- import net.minecraft.util.text.TextComponentString ;
21
+ import net.minecraft.util.ChatComponentText ;
25
22
import net.minecraft.world.World;
26
23
import net.minecraft.world.WorldServer;
27
- import net.minecraftforge.fml.common.FMLCommonHandler;
28
- import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
24
+ import net.minecraftforge.common.DimensionManager;
25
+
26
+ import java.io.StringWriter;
27
+ import java.lang.reflect.Constructor;
28
+ import java.lang.reflect.Field;
29
+ import java.lang.reflect.Method;
30
+ import java.util.Arrays;
31
+ import java.util.List;
29
32
""" ;
30
33
31
34
//language=Java
32
35
private static final String BASE_SCRIPT_TEXT = """
33
36
abstract class BaseScript {
34
-
35
37
/*
36
38
---- MC stuff
37
39
*/
38
-
40
+
39
41
public MinecraftServer getServer() {
40
42
return FMLCommonHandler.instance().getMinecraftServerInstance();
41
43
}
42
44
43
45
public void sendToAllPlayers(String message) {
44
- getServerPlayers().forEach(p -> p.sendMessage (new TextComponentString (message)));
46
+ getServerPlayers().forEach(p -> p.addChatMessage (new ChatComponentText (message)));
45
47
}
46
48
47
49
public World getServerOverworld() {
48
- return getServer() .getWorld(0);
50
+ return DimensionManager .getWorld(0);
49
51
}
50
52
51
53
public List<WorldServer> getServerWorlds() {
52
- return Arrays.asList(getServer().worlds );
54
+ return Arrays.asList(getServer().worldServers );
53
55
}
54
56
55
- public List<EntityPlayerMP> getServerPlayers() {
56
- return getServer().getPlayerList ().getPlayers() ;
57
- }
58
-
57
+ public List<EntityPlayerMP> getServerPlayers() {
58
+ return getServer().getConfigurationManager ().playerEntityList ;
59
+ }
60
+ \s
59
61
/*
60
62
---- Reflection
61
63
*/
62
-
64
+
63
65
public static <T> T createInstance(Class<T> clazz, Object... args) {
64
66
try {
65
67
Constructor<T> ctor = clazz.getDeclaredConstructor(Arrays.stream(args).map(Object::getClass).toArray(Class[]::new));
@@ -69,7 +71,7 @@ public static <T> T createInstance(Class<T> clazz, Object... args) {
69
71
throw new RuntimeException(e);
70
72
}
71
73
}
72
-
74
+
73
75
public static <T> T createInstance(Class<T> clazz, Class<?>[] argClasses, Object... args) {
74
76
try {
75
77
Constructor<T> ctor = clazz.getDeclaredConstructor(argClasses);
@@ -79,11 +81,11 @@ public static <T> T createInstance(Class<T> clazz, Class<?>[] argClasses, Object
79
81
throw new RuntimeException(e);
80
82
}
81
83
}
82
-
84
+
83
85
public static <T> T invokeMethod(Object o, String methodName, Object... args) {
84
86
return invokeMethod(o, methodName, Arrays.stream(args).map(Object::getClass).toArray(Class[]::new), args);
85
87
}
86
-
88
+
87
89
public static <T> T invokeMethod(Object o, String methodName, Class<?>[] argClasses, Object... args) {
88
90
try {
89
91
Method method = o.getClass().getDeclaredMethod(methodName, argClasses);
@@ -93,11 +95,11 @@ public static <T> T invokeMethod(Object o, String methodName, Class<?>[] argClas
93
95
throw new RuntimeException(e);
94
96
}
95
97
}
96
-
98
+
97
99
public static <T> T invokeStaticMethod(Class<?> c, String methodName, Object... args) {
98
100
return invokeStaticMethod(c, methodName, Arrays.stream(args).map(Object::getClass).toArray(Class[]::new), args);
99
101
}
100
-
102
+
101
103
public static <T> T invokeStaticMethod(Class<?> c, String methodName, Class<?>[] argClasses, Object... args) {
102
104
try {
103
105
Method method = c.getDeclaredMethod(methodName, argClasses);
@@ -107,7 +109,7 @@ public static <T> T invokeStaticMethod(Class<?> c, String methodName, Class<?>[]
107
109
throw new RuntimeException(e);
108
110
}
109
111
}
110
-
112
+
111
113
public static <T> T getFieldValue(Object o, String fieldName) {
112
114
Field f = findField(o.getClass(), fieldName);
113
115
f.setAccessible(true);
@@ -117,11 +119,11 @@ public static <T> T getFieldValue(Object o, String fieldName) {
117
119
throw new RuntimeException(e);
118
120
}
119
121
}
120
-
122
+
121
123
public static void setField(Object o, String fieldName, Object value) {
122
124
setField(o.getClass(), o, fieldName, value);
123
125
}
124
-
126
+
125
127
public static void setField(Class<?> clazz, Object o, String fieldName, Object value) {
126
128
try {
127
129
Field field = findField(clazz, fieldName);
@@ -131,7 +133,11 @@ public static void setField(Class<?> clazz, Object o, String fieldName, Object v
131
133
throw new RuntimeException(e);
132
134
}
133
135
}
134
-
136
+
137
+ public static Field findField(Object o, String fieldName) {
138
+ return findField(o.getClass(), fieldName);
139
+ }
140
+
135
141
public static Field findField(Class<?> clazz, String fieldName) {
136
142
Class<?> current = clazz;
137
143
while (current != null) {
@@ -141,10 +147,10 @@ public static Field findField(Class<?> clazz, String fieldName) {
141
147
current = current.getSuperclass();
142
148
}
143
149
}
144
-
150
+
145
151
throw new RuntimeException("Field not found: " + fieldName);
146
152
}
147
-
153
+
148
154
public static void setStaticField(Class<?> c, String fieldName, Object value) {
149
155
try {
150
156
Field field = c.getDeclaredField(fieldName);
@@ -154,7 +160,7 @@ public static void setStaticField(Class<?> c, String fieldName, Object value) {
154
160
throw new RuntimeException(e);
155
161
}
156
162
}
157
-
163
+ \s
158
164
/*
159
165
---- Logging
160
166
*/
@@ -170,8 +176,7 @@ public void log(Object s) {
170
176
}
171
177
172
178
public abstract void run() throws Throwable;
173
- }
174
- """ .replace (" " , "\t " );
179
+ }""" .replace (" " , "\t " );
175
180
176
181
private static final String BASE_SCRIPT = BASE_SCRIPT_IMPORTS + BASE_SCRIPT_TEXT ;
177
182
private static final Path PATH = CompanionApp .getRootPath ().resolve ("scripts" ).resolve ("BaseScript.java" );
0 commit comments