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! DO NOT CHANGE THIS FILE!
Also, you may replace this file at any time if there is an update available. Also, you may replace this file at any time if there is an update available.
@@ -144,6 +144,7 @@ propertyDefaultIfUnset("modrinthProjectId", "")
propertyDefaultIfUnset("modrinthRelations", "") propertyDefaultIfUnset("modrinthRelations", "")
propertyDefaultIfUnset("curseForgeProjectId", "") propertyDefaultIfUnset("curseForgeProjectId", "")
propertyDefaultIfUnset("curseForgeRelations", "") propertyDefaultIfUnset("curseForgeRelations", "")
propertyDefaultIfUnset("minimizeShadowedDependencies", true)
String javaSourceDir = "src/main/java/" String javaSourceDir = "src/main/java/"
String scalaSourceDir = "src/main/scala/" String scalaSourceDir = "src/main/scala/"
@@ -411,7 +412,9 @@ shadowJar {
attributes(getManifestAttributes()) 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 = [ configurations = [
project.configurations.shadowImplementation, project.configurations.shadowImplementation,
project.configurations.shadowCompile project.configurations.shadowCompile
@@ -554,7 +557,9 @@ task shadowDevJar(type: ShadowJar) {
attributes(getManifestAttributes()) 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 = [ configurations = [
project.configurations.shadowImplementation, project.configurations.shadowImplementation,
project.configurations.shadowCompile project.configurations.shadowCompile
+2 -1
View File
@@ -1,7 +1,8 @@
// Add your dependencies here // Add your dependencies here
dependencies { 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') 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.event.FMLServerStartingEvent;
import cpw.mods.fml.common.network.NetworkRegistry; 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{ public class HardcoreEnderExpansion{
@Instance("HardcoreEnderExpansion") @Instance("HardcoreEnderExpansion")
public static HardcoreEnderExpansion instance; public static HardcoreEnderExpansion instance;
@@ -3,6 +3,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Map; import java.util.Map;
import com.gtnewhorizon.gtnhlib.reflect.Fields;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.init.Blocks; import net.minecraft.init.Blocks;
import net.minecraft.item.Item; 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.Log;
import chylex.hee.system.logging.Stopwatch; import chylex.hee.system.logging.Stopwatch;
import chylex.hee.system.util.ReflectionUtils; 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.FMLControlledNamespacedRegistry;
import cpw.mods.fml.common.registry.GameData; import cpw.mods.fml.common.registry.GameData;
@@ -23,11 +23,13 @@ public class BlockReplaceHelper{
Exception exception = null; Exception exception = null;
try{ try{
final Fields.ClassFields blocksFields = Fields.ofClass(Blocks.class);
for(Field blockField:Blocks.class.getFields()){ for(Field blockField:Blocks.class.getFields()){
if (Block.class.isAssignableFrom(blockField.getType())){ if (Block.class.isAssignableFrom(blockField.getType())){
Block block = (Block)blockField.get(null); Block block = (Block)blockField.get(null);
if (block == toReplace){ if (block == toReplace){
final Fields.ClassFields.Field fieldAccessor = blocksFields.getUntypedField(Fields.LookupType.PUBLIC, blockField.getName());
String registryName = Block.blockRegistry.getNameForObject(block); String registryName = Block.blockRegistry.getNameForObject(block);
int id = Block.getIdFromBlock(block); int id = Block.getIdFromBlock(block);
@@ -50,10 +52,9 @@ public class BlockReplaceHelper{
Log.debug("Set new value for registryObjects: registryObjects. For object registryBlocks."); Log.debug("Set new value for registryObjects: registryObjects. For object registryBlocks.");
ReflectionUtils.setFieldValue(registryBlocks, "underlyingIntegerMap", underlyingIntegerMap); ReflectionUtils.setFieldValue(registryBlocks, "underlyingIntegerMap", underlyingIntegerMap);
Log.debug("Set new value for underlyingIntegerMap: underlyingIntegerMap. For object registryBlocks."); Log.debug("Set new value for underlyingIntegerMap: underlyingIntegerMap. For object registryBlocks.");
ReflectionUtils.makeFieldAccessible(blockField);
Log.debug("Made "+blockField.getName()+" accessible."); Log.debug("Made "+blockField.getName()+" accessible.");
blockField.set(null,replacement); fieldAccessor.setValue(null,replacement);
Log.debug("Set "+blockField.getName()+" to "+replacement.getUnlocalizedName()+"."); Log.debug("Set "+blockField.getName()+" to "+replacement.getUnlocalizedName()+".");
Method delegateNameMethod = replacement.delegate.getClass().getDeclaredMethod("setName",String.class); Method delegateNameMethod = replacement.delegate.getClass().getDeclaredMethod("setName",String.class);
@@ -8,10 +8,12 @@ import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import chylex.hee.system.logging.Log; import chylex.hee.system.logging.Log;
import com.gtnewhorizon.gtnhlib.reflect.Fields;
import cpw.mods.fml.relauncher.CoreModManager; import cpw.mods.fml.relauncher.CoreModManager;
import cpw.mods.fml.relauncher.ReflectionHelper; import cpw.mods.fml.relauncher.ReflectionHelper;
import net.minecraft.launchwrapper.Launch; import net.minecraft.launchwrapper.Launch;
@SuppressWarnings({"unchecked", "rawtypes"})
public class ReflectionUtils { public class ReflectionUtils {
public static Boolean obf; public static Boolean obf;
@@ -78,25 +80,25 @@ public class ReflectionUtils {
private static class CachedField { 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; FIELD = aField;
} }
public Field get() { public Fields.ClassFields.Field get() {
return FIELD; return FIELD;
} }
} }
private static boolean cacheField(Class<?> aClass, Field aField) { private static boolean cacheField(Class<?> aClass, Fields.ClassFields.Field aField) {
if (aField == null) { if (aField == null) {
return false; return false;
} }
CachedField y = mCachedFields.get(aClass.getName()+"."+aField.getName()); CachedField y = mCachedFields.get(aClass.getName()+"."+aField.javaField.getName());
if (y == null) { if (y == null) {
Log.debug("Caching Field: "+aClass.getName()+"."+aField.getName()); Log.debug("Caching Field: "+aClass.getName()+"."+aField.javaField.getName());
mCachedFields.put(aClass.getName()+"."+aField.getName(), new CachedField(aField)); mCachedFields.put(aClass.getName()+"."+aField.javaField.getName(), new CachedField(aField));
return true; return true;
} }
return false; return false;
@@ -108,7 +110,7 @@ public class ReflectionUtils {
* @param aFieldName - Field name in {@link String} form. * @param aFieldName - Field name in {@link String} form.
* @return - Valid, non-final, {@link Field} object, or {@link null}. * @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) { if (aClass == null || aFieldName == null || aFieldName.length() <= 0) {
return null; return null;
} }
@@ -116,14 +118,11 @@ public class ReflectionUtils {
Log.debug("Getting Field: "+aFieldName); Log.debug("Getting Field: "+aFieldName);
CachedField y = mCachedFields.get(aClass.getName()+"."+aFieldName); CachedField y = mCachedFields.get(aClass.getName()+"."+aFieldName);
if (y == null) { if (y == null) {
Field u; Fields.ClassFields.Field u;
try { u = getField_Internal(aClass, aFieldName);
u = getField_Internal(aClass, aFieldName); if (u != null) {
if (u != null) { cacheField(aClass, u);
cacheField(aClass, u); return u;
return u;
}
} catch (NoSuchFieldException e) {
} }
return null; return null;
@@ -138,74 +137,46 @@ public class ReflectionUtils {
* @param aFieldName - Field name in {@link String} form. * @param aFieldName - Field name in {@link String} form.
* @return - Valid, non-final, {@link Field} object, or {@link null}. * @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); return getField(aInstance.getClass(), aFieldName);
} }
@SuppressWarnings("unchecked")
public static <T> T getFieldValue(final Object aInstance, String aFieldName) { public static <T> T getFieldValue(final Object aInstance, String aFieldName) {
aFieldName = getCorrectFieldName(aFieldName); aFieldName = getCorrectFieldName(aFieldName);
Log.debug("Getting Field Value: "+aFieldName); Log.debug("Getting Field Value: "+aFieldName);
try { try {
return (T) getField(aInstance, aFieldName).get(aInstance); return (T) getField(aInstance, aFieldName).getValue(aInstance);
} catch (IllegalArgumentException | IllegalAccessException e) { } catch (ClassCastException e) {
return null; return null;
} }
} }
public static void setFieldValue(final Object aInstance, String aFieldName, Object aNewValue) { 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) { public static void setFieldValue(final Object aInstance, String aFieldName, boolean aStatic, Object aNewValue) {
Field f; Fields.ClassFields.Field f;
aFieldName = getCorrectFieldName(aFieldName); aFieldName = getCorrectFieldName(aFieldName);
Log.debug("Setting Field: "+aFieldName); Log.debug("Setting Field: "+aFieldName);
try { try {
if (aInstance instanceof Class && aStatic) { if (aInstance instanceof Class && aStatic) {
f = getField((Class) aInstance, aFieldName); f = getField((Class) aInstance, aFieldName);
makeFieldAccessible(f); f.setValue(null, aNewValue);
f.set(null, aNewValue);
} }
else { else {
f = getField(aInstance, aFieldName); f = getField(aInstance, aFieldName);
makeFieldAccessible(f); f.setValue(aInstance, aNewValue);
f.set(aInstance, aNewValue);
} }
} catch (IllegalArgumentException | IllegalAccessException e) { } catch (ClassCastException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private static Fields.ClassFields.Field getField_Internal(final Class<?> clazz, final String fieldName) {
private static Field getField_Internal(final Class<?> clazz, final String fieldName) throws NoSuchFieldException { return Fields.ofClass(clazz).getUntypedField(Fields.LookupType.DECLARED_IN_HIERARCHY, fieldName);
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();
}
}
} }
} }
@@ -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(){}
}