Switch the reflection wrapper to java 17-compatible gtnhlib (#15)

* Update buildscript and dependencies

* Switch the reflection wrapper to java 17-compatible gtnhlib

* Fix gtnhlib version range
This commit is contained in:
Raven Szewczyk authored and GitHub committed 2023-01-25 21:10:55 +01:00
1 parent 27145a63a5
commit 1dd49f8a2e
6 files changed
+43 -87

No files matched your search

+8 -3
View File
@@ -1,4 +1,4 @@
//version: 1673027205
//version: 1674409054
/*
DO NOT CHANGE THIS FILE!
Also, you may replace this file at any time if there is an update available.
@@ -144,6 +144,7 @@ propertyDefaultIfUnset("modrinthProjectId", "")
propertyDefaultIfUnset("modrinthRelations", "")
propertyDefaultIfUnset("curseForgeProjectId", "")
propertyDefaultIfUnset("curseForgeRelations", "")
propertyDefaultIfUnset("minimizeShadowedDependencies", true)
String javaSourceDir = "src/main/java/"
String scalaSourceDir = "src/main/scala/"
@@ -411,7 +412,9 @@ shadowJar {
attributes(getManifestAttributes())
}
minimize() // This will only allow shading for actually used classes
if (minimizeShadowedDependencies.toBoolean()) {
minimize() // This will only allow shading for actually used classes
}
configurations = [
project.configurations.shadowImplementation,
project.configurations.shadowCompile
@@ -554,7 +557,9 @@ task shadowDevJar(type: ShadowJar) {
attributes(getManifestAttributes())
}
minimize() // This will only allow shading for actually used classes
if (minimizeShadowedDependencies.toBoolean()) {
minimize() // This will only allow shading for actually used classes
}
configurations = [
project.configurations.shadowImplementation,
project.configurations.shadowCompile
+2 -1
View File
@@ -1,7 +1,8 @@
// Add your dependencies here
dependencies {
compile('com.github.GTNewHorizons:NotEnoughItems:2.2.6-GTNH:dev')
implementation('com.github.GTNewHorizons:GTNHLib:0.0.10:dev')
implementation('com.github.GTNewHorizons:NotEnoughItems:2.3.20-GTNH:dev')
compileOnly('thaumcraft:Thaumcraft:1.7.10-4.2.3.5:dev')
}
@@ -54,7 +54,7 @@ import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
@Mod(modid = "HardcoreEnderExpansion", name = "Hardcore Ender Expansion", version = "GRADLETOKEN_VERSION", useMetadata = true, guiFactory = "chylex.hee.gui.core.ModGuiFactory")
@Mod(modid = "HardcoreEnderExpansion", name = "Hardcore Ender Expansion", version = "GRADLETOKEN_VERSION", useMetadata = true, guiFactory = "chylex.hee.gui.core.ModGuiFactory", dependencies = "required-after:gtnhlib@[0.0.10,)")
public class HardcoreEnderExpansion{
@Instance("HardcoreEnderExpansion")
public static HardcoreEnderExpansion instance;
@@ -3,6 +3,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import com.gtnewhorizon.gtnhlib.reflect.Fields;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
@@ -11,7 +12,6 @@ import net.minecraft.util.ObjectIntIdentityMap;
import chylex.hee.system.logging.Log;
import chylex.hee.system.logging.Stopwatch;
import chylex.hee.system.util.ReflectionUtils;
import chylex.hee.system.util.Unfinalizer;
import cpw.mods.fml.common.registry.FMLControlledNamespacedRegistry;
import cpw.mods.fml.common.registry.GameData;
@@ -23,11 +23,13 @@ public class BlockReplaceHelper{
Exception exception = null;
try{
final Fields.ClassFields blocksFields = Fields.ofClass(Blocks.class);
for(Field blockField:Blocks.class.getFields()){
if (Block.class.isAssignableFrom(blockField.getType())){
Block block = (Block)blockField.get(null);
if (block == toReplace){
final Fields.ClassFields.Field fieldAccessor = blocksFields.getUntypedField(Fields.LookupType.PUBLIC, blockField.getName());
String registryName = Block.blockRegistry.getNameForObject(block);
int id = Block.getIdFromBlock(block);
@@ -50,10 +52,9 @@ public class BlockReplaceHelper{
Log.debug("Set new value for registryObjects: registryObjects. For object registryBlocks.");
ReflectionUtils.setFieldValue(registryBlocks, "underlyingIntegerMap", underlyingIntegerMap);
Log.debug("Set new value for underlyingIntegerMap: underlyingIntegerMap. For object registryBlocks.");
ReflectionUtils.makeFieldAccessible(blockField);
Log.debug("Made "+blockField.getName()+" accessible.");
blockField.set(null,replacement);
fieldAccessor.setValue(null,replacement);
Log.debug("Set "+blockField.getName()+" to "+replacement.getUnlocalizedName()+".");
Method delegateNameMethod = replacement.delegate.getClass().getDeclaredMethod("setName",String.class);
@@ -8,10 +8,12 @@ import java.util.LinkedHashMap;
import java.util.Map;
import chylex.hee.system.logging.Log;
import com.gtnewhorizon.gtnhlib.reflect.Fields;
import cpw.mods.fml.relauncher.CoreModManager;
import cpw.mods.fml.relauncher.ReflectionHelper;
import net.minecraft.launchwrapper.Launch;
@SuppressWarnings({"unchecked", "rawtypes"})
public class ReflectionUtils {
public static Boolean obf;
@@ -78,25 +80,25 @@ public class ReflectionUtils {
private static class CachedField {
private final Field FIELD;
private final Fields.ClassFields.Field FIELD;
public CachedField(Field aField) {
public CachedField(Fields.ClassFields.Field aField) {
FIELD = aField;
}
public Field get() {
public Fields.ClassFields.Field get() {
return FIELD;
}
}
private static boolean cacheField(Class<?> aClass, Field aField) {
private static boolean cacheField(Class<?> aClass, Fields.ClassFields.Field aField) {
if (aField == null) {
return false;
}
CachedField y = mCachedFields.get(aClass.getName()+"."+aField.getName());
CachedField y = mCachedFields.get(aClass.getName()+"."+aField.javaField.getName());
if (y == null) {
Log.debug("Caching Field: "+aClass.getName()+"."+aField.getName());
mCachedFields.put(aClass.getName()+"."+aField.getName(), new CachedField(aField));
Log.debug("Caching Field: "+aClass.getName()+"."+aField.javaField.getName());
mCachedFields.put(aClass.getName()+"."+aField.javaField.getName(), new CachedField(aField));
return true;
}
return false;
@@ -108,7 +110,7 @@ public class ReflectionUtils {
* @param aFieldName - Field name in {@link String} form.
* @return - Valid, non-final, {@link Field} object, or {@link null}.
*/
public static Field getField(final Class<?> aClass, String aFieldName) {
public static Fields.ClassFields.Field getField(final Class<?> aClass, String aFieldName) {
if (aClass == null || aFieldName == null || aFieldName.length() <= 0) {
return null;
}
@@ -116,14 +118,11 @@ public class ReflectionUtils {
Log.debug("Getting Field: "+aFieldName);
CachedField y = mCachedFields.get(aClass.getName()+"."+aFieldName);
if (y == null) {
Field u;
try {
u = getField_Internal(aClass, aFieldName);
if (u != null) {
cacheField(aClass, u);
return u;
}
} catch (NoSuchFieldException e) {
Fields.ClassFields.Field u;
u = getField_Internal(aClass, aFieldName);
if (u != null) {
cacheField(aClass, u);
return u;
}
return null;
@@ -138,74 +137,46 @@ public class ReflectionUtils {
* @param aFieldName - Field name in {@link String} form.
* @return - Valid, non-final, {@link Field} object, or {@link null}.
*/
public static Field getField(final Object aInstance, String aFieldName) {
public static Fields.ClassFields.Field getField(final Object aInstance, String aFieldName) {
return getField(aInstance.getClass(), aFieldName);
}
@SuppressWarnings("unchecked")
public static <T> T getFieldValue(final Object aInstance, String aFieldName) {
aFieldName = getCorrectFieldName(aFieldName);
Log.debug("Getting Field Value: "+aFieldName);
try {
return (T) getField(aInstance, aFieldName).get(aInstance);
} catch (IllegalArgumentException | IllegalAccessException e) {
return (T) getField(aInstance, aFieldName).getValue(aInstance);
} catch (ClassCastException e) {
return null;
}
}
public static void setFieldValue(final Object aInstance, String aFieldName, Object aNewValue) {
setFieldValue(aInstance, aFieldName, aInstance instanceof Class ? true : false, aNewValue);
setFieldValue(aInstance, aFieldName, aInstance instanceof Class, aNewValue);
}
public static void setFieldValue(final Object aInstance, String aFieldName, boolean aStatic, Object aNewValue) {
Field f;
public static void setFieldValue(final Object aInstance, String aFieldName, boolean aStatic, Object aNewValue) {
Fields.ClassFields.Field f;
aFieldName = getCorrectFieldName(aFieldName);
Log.debug("Setting Field: "+aFieldName);
try {
if (aInstance instanceof Class && aStatic) {
f = getField((Class) aInstance, aFieldName);
makeFieldAccessible(f);
f.set(null, aNewValue);
f.setValue(null, aNewValue);
}
else {
f = getField(aInstance, aFieldName);
makeFieldAccessible(f);
f.set(aInstance, aNewValue);
f.setValue(aInstance, aNewValue);
}
} catch (IllegalArgumentException | IllegalAccessException e) {
}
} catch (ClassCastException e) {
e.printStackTrace();
}
}
private static Field getField_Internal(final Class<?> clazz, final String fieldName) throws NoSuchFieldException {
try {
Field k = clazz.getDeclaredField(fieldName);
makeFieldAccessible(k);
return k;
} catch (final NoSuchFieldException e) {
final Class<?> superClass = clazz.getSuperclass();
if (superClass == null) {
throw e;
}
Log.debug("Checking Super: "+superClass.getCanonicalName());
return getField_Internal(superClass, fieldName);
}
}
public static void makeFieldAccessible(final Field field) {
field.setAccessible(true);
if (Modifier.isFinal(field.getModifiers())){
try {
Unfinalizer.unfinalizeField(field);
}
catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
private static Fields.ClassFields.Field getField_Internal(final Class<?> clazz, final String fieldName) {
return Fields.ofClass(clazz).getUntypedField(Fields.LookupType.DECLARED_IN_HIERARCHY, fieldName);
}
}
@@ -1,22 +0,0 @@
package chylex.hee.system.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public final class Unfinalizer{
private static Field modifiersField;
public static void unfinalizeField(Field field) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
try{
if (modifiersField == null){
modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
}
modifiersField.setInt(field,field.getModifiers() & ~Modifier.FINAL);
}catch(NoSuchFieldException | IllegalArgumentException | IllegalAccessException e){
throw e;
}
}
private Unfinalizer(){}
}