Skip to content

Instantly share code, notes, and snippets.

@tyhdefu
Last active May 1, 2020 15:29
Show Gist options
  • Save tyhdefu/f2ea4cc721d571d0e7f3104163071dd7 to your computer and use it in GitHub Desktop.
Save tyhdefu/f2ea4cc721d571d0e7f3104163071dd7 to your computer and use it in GitHub Desktop.
private static Field mapDataBridgeMixin$dimensionId;
private static Method getTypeMethod;
@Shadow public abstract void updateMapData(int x, int y);
static {
Class<?> clazz = null;
try {
if (SpongeImplHooks.isDeobfuscatedEnvironment()) {
mapDataBridgeMixin$dimensionId = MapData.class.getDeclaredField("dimension");
} else {
mapDataBridgeMixin$dimensionId = MapData.class.getDeclaredField("field_76200_c");
}
clazz = mapDataBridgeMixin$dimensionId.getType();
// See java's Number class. all primitive number types are able to
// be converted to from any number via .xxxValue()
// its not the best solution, but due to primitive types you cant use
// Class.cast() since it auto-boxes into the wrapper class, which can't be cast.
String methodName = clazz.getName() + "Value";
// Integer is what we currently use until we meet with the field
getTypeMethod = Integer.class.getMethod(methodName);
SpongeImpl.getLogger().warn("Got type method from Integer.class. Method name: " + methodName + ". class: " + clazz.getName());
} catch (NoSuchMethodException e) {
SpongeImpl.getLogger().fatal("Field dimensionId in MapData was not an instance of a valid type (byte, int etc). SpongeForge patches to int and normally it is a byte. Actual type was " + clazz.getTypeName());
}
catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
@Override
public int bridge$getDimensionId() {
try {
return ((Number)mapDataBridgeMixin$dimensionId.get(this)).intValue();
} catch (IllegalAccessException e) {
e.printStackTrace();
return 0;
}
}
@Override
public void bridge$setDimensionId(int dimensionId) {
try {
mapDataBridgeMixin$dimensionId.set(this, getTypeMethod.invoke(dimensionId));
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment