-
Notifications
You must be signed in to change notification settings - Fork 4
/
EntityFallingBlockClassGenerator.java
386 lines (340 loc) · 18.2 KB
/
EntityFallingBlockClassGenerator.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package nl.pim16aap2.bigDoors.codegeneration;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.scaffold.FieldLocator;
import net.bytebuddy.implementation.FieldAccessor;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.implementation.MethodCall;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.This;
import net.bytebuddy.implementation.bytecode.assign.Assigner;
import net.bytebuddy.matcher.ElementMatchers;
import nl.pim16aap2.bigDoors.NMS.CustomEntityFallingBlock;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Objects;
import static net.bytebuddy.implementation.FixedValue.value;
import static net.bytebuddy.implementation.MethodCall.construct;
import static net.bytebuddy.implementation.MethodCall.invoke;
import static net.bytebuddy.matcher.ElementMatchers.is;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static nl.pim16aap2.bigDoors.codegeneration.ReflectionRepository.*;
import static nl.pim16aap2.bigDoors.reflection.ReflectionBuilder.findMethod;
/**
* Represents an implementation of a {@link ClassGenerator} to generate a subclass of {@link CustomEntityFallingBlock}.
*
* @author Pim
*/
final class EntityFallingBlockClassGenerator extends ClassGenerator
{
private static final @NotNull Class<?>[] CONSTRUCTOR_PARAMETER_TYPES =
new Class<?>[]{classCraftWorld, double.class, double.class, double.class,
classIBlockData, asArrayType(classEnumMoveType)};
public static final String FIELD_BLOCK = "generated$block";
public static final String FIELD_MOVE_TYPE_VALUES = "generated$enumMoveTypeValues";
public static final String FIELD_CRAFT_WORLD = "generated$craftWorld";
public static final String METHOD_MULTIPLY_VEC = "generated$multiplyVec";
public static final String METHOD_NAME_LOAD_DATA_CONDITIONAL = methodLoadData.getName() + "$conditional";
public static final String METHOD_NAME_SAVE_DATA_CONDITIONAL = methodSaveData.getName() + "$conditional";
public static final String METHOD_TICK = "generated$tick";
public static final String METHOD_SPAWN = "generated$spawn";
public static final Method METHOD_LOAD_DATA =
findMethod().inClass(IGeneratedFallingBlockEntity.class).withName("generated$loadTileEntityData").get();
public static final Method METHOD_SAVE_DATA =
findMethod().inClass(IGeneratedFallingBlockEntity.class).withName("generated$saveTileEntityData").get();
public static final Method METHOD_DIE =
findMethod().inClass(IGeneratedFallingBlockEntity.class).withName("generated$die").get();
public static final Method METHOD_IS_AIR =
findMethod().inClass(IGeneratedFallingBlockEntity.class).withName("generated$isAir").get();
public static final Method METHOD_MOVE =
findMethod().inClass(IGeneratedFallingBlockEntity.class).withName("generated$move").get();
public static final Method METHOD_GET_TICKS_LIVED =
findMethod().inClass(IGeneratedFallingBlockEntity.class).withName("generated$getTicksLived").get();
public static final Method METHOD_SET_TICKS_LIVED =
findMethod().inClass(IGeneratedFallingBlockEntity.class).withName("generated$setTicksLived").get();
public static final Method METHOD_UPDATE_MOT =
findMethod().inClass(IGeneratedFallingBlockEntity.class).withName("generated$updateMot").get();
private final Field fieldNoClip;
private final Field fieldHurtEntities;
public EntityFallingBlockClassGenerator(@NotNull String mappingsVersion)
throws Exception
{
super(mappingsVersion);
final EntityFallingBlockClassAnalyzer analyzer = new EntityFallingBlockClassAnalyzer();
fieldHurtEntities = analyzer.getHurtEntitiesField();
fieldNoClip = analyzer.getNoClipField();
generate();
}
@Override
protected @NotNull Class<?>[] getConstructorArgumentTypes()
{
return CONSTRUCTOR_PARAMETER_TYPES;
}
@Override
protected @NotNull String getBaseName()
{
return "EntityFallingBlock";
}
@Override
protected void generateImpl()
{
DynamicType.Builder<?> builder = createBuilder(classEntityFallingBlock)
.implement(CustomEntityFallingBlock.class, IGeneratedFallingBlockEntity.class);
builder = addFields(builder);
builder = addCTor(builder);
builder = addSpawnMethod(builder);
builder = addHurtEntitiesMethod(builder);
builder = addGetBlockMethod(builder);
builder = addLoadDataMethod(builder);
builder = addSaveDataMethod(builder);
builder = addAuxiliaryMethods(builder);
builder = addTickMethod(builder);
builder = addCrashReportMethod(builder);
finishBuilder(builder);
}
private DynamicType.Builder<?> addFields(DynamicType.Builder<?> builder)
{
return builder
.defineField(fieldTicksLived.getName(), fieldTicksLived.getType(), Visibility.PROTECTED)
.defineField(fieldHurtEntities.getName(), fieldHurtEntities.getType(), Visibility.PROTECTED)
.defineField(fieldNoClip.getName(), fieldNoClip.getType(), Visibility.PROTECTED)
.defineField(fieldTileEntityData.getName(), fieldTileEntityData.getType(), Visibility.PROTECTED)
.defineField(FIELD_BLOCK, classIBlockData, Visibility.PRIVATE)
.defineField(FIELD_CRAFT_WORLD, classCraftWorld, Visibility.PRIVATE)
.defineField(FIELD_MOVE_TYPE_VALUES, asArrayType(classEnumMoveType), Visibility.PRIVATE);
}
private DynamicType.Builder<?> addCTor(DynamicType.Builder<?> builder)
{
final MethodCall worldCast = (MethodCall) invoke(methodGetNMSWorld)
.onArgument(0).withAssigner(Assigner.DEFAULT, Assigner.Typing.DYNAMIC);
final FieldLocator.Factory entityTypeLocatorFactory =
new FieldLocator.ForExactType.Factory(new TypeDescription.ForLoadedType(classEntityTypes));
return builder
.defineConstructor(Visibility.PUBLIC)
.withParameters(getConstructorArgumentTypes())
.intercept(invoke(cTorPublicNMSFallingBlockEntity)
.withField(entityTypeLocatorFactory, fieldEntityTypeFallingBlock.getName())
.withMethodCall(worldCast).andThen(
FieldAccessor.ofField(FIELD_BLOCK).setsArgumentAt(4)).andThen(
invoke(methodSetPosition).withArgument(1, 2, 3)).andThen(
FieldAccessor.ofField(FIELD_CRAFT_WORLD).setsArgumentAt(0)).andThen(
FieldAccessor.ofField(FIELD_MOVE_TYPE_VALUES).setsArgumentAt(5)).andThen(
FieldAccessor.of(fieldTicksLived).setsValue(0)).andThen(
FieldAccessor.of(fieldHurtEntities).setsValue(false)).andThen(
FieldAccessor.of(fieldNoClip).setsValue(true)).andThen(
invoke(methodSetNoGravity).with(true)).andThen(
invoke(methodSetMotVec).withMethodCall(construct(cTorVec3D).with(0d, 0d, 0d))).andThen(
invoke(methodSetStartPos).withMethodCall(invoke(methodNewBlockPosition)
.withMethodCall(invoke(methodLocX))
.withMethodCall(invoke(methodLocY))
.withMethodCall(invoke(methodLocZ)))).andThen(
invoke(named(METHOD_SPAWN)))
);
}
private DynamicType.Builder<?> addSpawnMethod(DynamicType.Builder<?> builder)
{
return builder
.defineMethod(METHOD_SPAWN, boolean.class, Visibility.PUBLIC)
.intercept(invoke(methodCraftAddEntityToWorld)
.onField(FIELD_CRAFT_WORLD)
.with(MethodCall.ArgumentLoader.ForThisReference.Factory.INSTANCE)
.with(CreatureSpawnEvent.SpawnReason.CUSTOM)
.withAssigner(Assigner.DEFAULT, Assigner.Typing.DYNAMIC)
);
}
private DynamicType.Builder<?> addHurtEntitiesMethod(DynamicType.Builder<?> builder)
{
return builder
.defineMethod(methodHurtEntities.getName(), boolean.class, Visibility.PUBLIC)
.withParameters(methodHurtEntities.getParameterTypes())
.intercept(value(false));
}
private DynamicType.Builder<?> addCrashReportMethod(DynamicType.Builder<?> builder)
{
return builder
.defineMethod(methodAppendEntityCrashReport.getName(), void.class, Visibility.PUBLIC)
.withParameters(methodAppendEntityCrashReport.getParameterTypes())
.intercept(invoke(methodAppendEntityCrashReport).onSuper().withArgument(0).andThen(
invoke(methodCrashReportAppender).onArgument(0).with("Animated BigDoors block with state: ")
.withField(FIELD_BLOCK)));
}
private DynamicType.Builder<?> addGetBlockMethod(DynamicType.Builder<?> builder)
{
return builder
.defineMethod(methodEntityFallingBlockGetBlock.getName(), classIBlockData, Visibility.PUBLIC)
.intercept(FieldAccessor.ofField(FIELD_BLOCK));
}
private DynamicType.Builder<?> addLoadDataMethod(DynamicType.Builder<?> builder)
{
builder = builder
.define(METHOD_LOAD_DATA)
.intercept(invoke(methodNBTTagCompoundGetCompound)
.onArgument(0).with("TileEntityData").setsField(fieldTileEntityData)
.withAssigner(Assigner.DEFAULT, Assigner.Typing.DYNAMIC));
builder = builder
.defineMethod(METHOD_NAME_LOAD_DATA_CONDITIONAL, void.class, Visibility.PRIVATE)
.withParameters(IGeneratedFallingBlockEntity.class, classNBTTagCompound, boolean.class)
.intercept(MethodDelegation.to((ILoadDataDelegation) (baseObject, compound, hasKey) ->
{
if (!hasKey)
baseObject.generated$loadTileEntityData(compound);
}, ILoadDataDelegation.class));
final MethodCall deserializedInvocation;
if (methodIBlockDataDeserializer.getParameterCount() == 1)
{
deserializedInvocation = invoke(methodIBlockDataDeserializer)
.withMethodCall(invoke(methodNBTTagCompoundGetCompound).onArgument(0).with("BlockState"));
}
else if (methodIBlockDataDeserializer.getParameterCount() == 2)
{
Objects.requireNonNull(methodWorldReaderHolderLookup);
deserializedInvocation = invoke(methodIBlockDataDeserializer)
.withMethodCall(invoke(methodWorldReaderHolderLookup).onField(fieldNMSWorld).with(objectRegistryBlock))
.withMethodCall(invoke(methodNBTTagCompoundGetCompound).onArgument(0).with("BlockState"));
}
else
throw new IllegalStateException("Method IBlockDataDeserializer has unexpected number of parameters!");
builder = builder
.define(methodLoadData)
.intercept(deserializedInvocation.setsField(named(FIELD_BLOCK)).andThen(
invoke(methodNBTTagCompoundGetInt)
.onArgument(0).with("Time").setsField(fieldTicksLived)).andThen(
invoke(named(METHOD_NAME_LOAD_DATA_CONDITIONAL))
.withThis().withArgument(0)
.withMethodCall(invoke(methodNBTTagCompoundHasKeyOfType)
.onArgument(0).with("TileEntityData", 10))));
return builder;
}
private DynamicType.Builder<?> addSaveDataMethod(DynamicType.Builder<?> builder)
{
builder = builder
.define(METHOD_SAVE_DATA)
.intercept(invoke(methodNBTTagCompoundSet)
.onArgument(0).with("TileEntityData").withField(fieldTileEntityData.getName())
.withAssigner(Assigner.DEFAULT, Assigner.Typing.DYNAMIC));
builder = builder
.defineMethod(METHOD_NAME_SAVE_DATA_CONDITIONAL, void.class, Visibility.PRIVATE)
.withParameters(IGeneratedFallingBlockEntity.class, classNBTTagCompound, classNBTTagCompound)
.intercept(MethodDelegation.to((ISaveDataDelegation) (baseObject, compound, block) ->
{
if (block != null)
baseObject.generated$saveTileEntityData(compound);
}, ISaveDataDelegation.class));
return builder
.define(methodSaveData)
.intercept(invoke(methodNBTTagCompoundSet)
.onArgument(0).with("BlockState")
.withMethodCall(invoke(methodIBlockDataSerializer).withField(FIELD_BLOCK)).andThen(
invoke(methodNBTTagCompoundSetInt)
.onArgument(0).with("Time").withField(fieldTicksLived.getName())).andThen(
invoke(methodNBTTagCompoundSetBoolean).onArgument(0).with("DropItem", false)).andThen(
invoke(methodNBTTagCompoundSetBoolean)
.onArgument(0).with("HurtEntities").withField(fieldHurtEntities.getName())).andThen(
invoke(methodNBTTagCompoundSetFloat).onArgument(0).with("FallHurtAmount", 0.0f)).andThen(
invoke(methodNBTTagCompoundSetInt).onArgument(0).with("FallHurtMax", 0)).andThen(
invoke(named(METHOD_NAME_SAVE_DATA_CONDITIONAL)).withThis().withArgument(0)
.withField(fieldTileEntityData.getName())).andThen(
FixedValue.argument(0)));
}
private DynamicType.Builder<?> addAuxiliaryMethods(DynamicType.Builder<?> builder)
{
builder = builder
.defineMethod(METHOD_MULTIPLY_VEC, classVec3D, Visibility.PRIVATE)
.withParameters(classVec3D, double.class, double.class, double.class)
.intercept(MethodDelegation.to((IMultiplyVec3D) (vec, x, y, z) ->
{
try
{
return cTorVec3D.newInstance((double) fieldsVec3D.get(0).get(vec) * x,
(double) fieldsVec3D.get(1).get(vec) * y,
(double) fieldsVec3D.get(2).get(vec) * z);
}
catch (InstantiationException | IllegalAccessException | InvocationTargetException e)
{
e.printStackTrace();
return vec;
}
}, IMultiplyVec3D.class));
builder = builder.method(is(METHOD_DIE)).intercept(invoke(methodDie).onSuper());
builder = builder.method(is(METHOD_IS_AIR)).intercept(invoke(methodIsAir).onField(FIELD_BLOCK));
builder = builder
.method(is(METHOD_MOVE).and(ElementMatchers.takesArguments(Collections.emptyList())))
.intercept(invoke(methodMove)
.withMethodCall(invoke(methodArrayGetIdx).withField(FIELD_MOVE_TYPE_VALUES).with(0))
.withMethodCall(invoke(methodGetMot))
.withAssigner(Assigner.DEFAULT, Assigner.Typing.DYNAMIC));
builder = builder.method(is(METHOD_GET_TICKS_LIVED))
.intercept(FieldAccessor.ofField(fieldTicksLived.getName()));
builder = builder.method(is(METHOD_SET_TICKS_LIVED))
.intercept(FieldAccessor.of(fieldTicksLived).setsArgumentAt(0));
builder = builder
.method(is(METHOD_UPDATE_MOT))
.intercept(invoke(methodSetMotVec)
.withMethodCall(invoke(named(METHOD_MULTIPLY_VEC))
.withMethodCall(invoke(methodGetMot))
.with(0.9800000190734863D, 1.0D, 0.9800000190734863D)));
return builder;
}
private DynamicType.Builder<?> addTickMethod(DynamicType.Builder<?> builder)
{
builder = builder
.defineMethod(METHOD_TICK, void.class, Visibility.PRIVATE)
.withParameters(IGeneratedFallingBlockEntity.class)
.intercept(MethodDelegation.to((ITickMethodDelegate) entity ->
{
if (entity.generated$isAir())
{
entity.generated$die();
return;
}
entity.generated$move();
final int ticks = entity.generated$getTicksLived() + 1;
entity.generated$setTicksLived(ticks);
if (ticks > 12000)
entity.generated$die();
entity.generated$updateMot();
}, ITickMethodDelegate.class));
builder = builder
.define(methodTick).intercept(invoke(named(METHOD_TICK))
.withThis().withAssigner(Assigner.DEFAULT, Assigner.Typing.DYNAMIC));
return builder;
}
public interface ILoadDataDelegation
{
@RuntimeType
void intercept(@This IGeneratedFallingBlockEntity baseObject, @RuntimeType Object compound, boolean hasKey);
}
public interface ISaveDataDelegation
{
@RuntimeType
void intercept(@This IGeneratedFallingBlockEntity baseObject, @RuntimeType Object compound,
@RuntimeType Object block);
}
public interface IGeneratedFallingBlockEntity
{
void generated$die();
boolean generated$isAir();
void generated$move();
int generated$getTicksLived();
void generated$setTicksLived(int val);
void generated$updateMot();
void generated$saveTileEntityData(Object target);
void generated$loadTileEntityData(Object target);
}
public interface IMultiplyVec3D
{
@RuntimeType
Object intercept(@RuntimeType Object vec3d, double x, double y, double z);
}
public interface ITickMethodDelegate
{
@SuppressWarnings("unused") //
void intercept(IGeneratedFallingBlockEntity entity);
}
}