diff options
author | Alkalus <3060479+draknyte1@users.noreply.github.com> | 2018-07-17 18:09:25 +1000 |
---|---|---|
committer | Alkalus <3060479+draknyte1@users.noreply.github.com> | 2018-07-17 18:09:25 +1000 |
commit | 91485c3ddbf57b9698d339576a410c51e58d7e16 (patch) | |
tree | 91ca0b5cc0cff2973c7ef2041653ef429d8152e3 /src/Java | |
parent | 8a6ecc74bcdf930d22ca51af745c31183d982c7f (diff) | |
download | GT5-Unofficial-91485c3ddbf57b9698d339576a410c51e58d7e16.tar.gz GT5-Unofficial-91485c3ddbf57b9698d339576a410c51e58d7e16.tar.bz2 GT5-Unofficial-91485c3ddbf57b9698d339576a410c51e58d7e16.zip |
+ Added a fix for beds when sleeping above Y >= 127.
% More functionality to Forge Srg handling.
Diffstat (limited to 'src/Java')
5 files changed, 212 insertions, 6 deletions
diff --git a/src/Java/gtPlusPlus/api/interfaces/IPlugin.java b/src/Java/gtPlusPlus/api/interfaces/IPlugin.java index 6a8e8c085d..c3840476e9 100644 --- a/src/Java/gtPlusPlus/api/interfaces/IPlugin.java +++ b/src/Java/gtPlusPlus/api/interfaces/IPlugin.java @@ -22,6 +22,13 @@ public interface IPlugin { default void log(String message) { Logger.INFO("["+getPluginAbbreviation()+"] "+message); } + + /** + * @param message - A {@link String} object which holds a warning/error message to be logged to console. + */ + default void logDebug(String message) { + Logger.WARNING("["+getPluginAbbreviation()+"] "+message); + } public boolean preInit(); public boolean init(); diff --git a/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java b/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java index fc8923b21a..039cddb6d6 100644 --- a/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java +++ b/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java @@ -275,6 +275,7 @@ public class ReflectionUtils { } @SuppressWarnings("rawtypes") + @Deprecated public static Method getMethodViaReflection(final Class<?> lookupClass, final String methodName, final boolean invoke) throws Exception { final Class<? extends Class> lookup = lookupClass.getClass(); @@ -286,6 +287,31 @@ public class ReflectionUtils { return m; } + /** + * Removes final modifier & returns a {@link Method} object. + * @param aClass - Class containing the Method. + * @param aMethodName - Method's name in {@link String} form. + * @param aTypes - Varags Class Types for {@link Method}'s constructor. + * @return - Valid, non-final, {@link Method} object. + */ + public static Method getMethod(Class aClass, String aMethodName, Class... aTypes) { + Method m = null; + try { + m = aClass.getDeclaredMethod(aMethodName, aTypes); + if (m != null) { + m.setAccessible(true); + int modifiers = m.getModifiers(); + Field modifierField = m.getClass().getDeclaredField("modifiers"); + modifiers = modifiers & ~Modifier.FINAL; + modifierField.setAccessible(true); + modifierField.setInt(m, modifiers); + } + } + catch (Throwable t) { + } + return m; + } + public static Class<?> getNonPublicClass(final String className) { Class<?> c = null; try { diff --git a/src/Java/gtPlusPlus/plugin/fixes/vanilla/Core_VanillaFixes.java b/src/Java/gtPlusPlus/plugin/fixes/vanilla/Core_VanillaFixes.java new file mode 100644 index 0000000000..49cdc26c38 --- /dev/null +++ b/src/Java/gtPlusPlus/plugin/fixes/vanilla/Core_VanillaFixes.java @@ -0,0 +1,44 @@ +package gtPlusPlus.plugin.fixes.vanilla; + +import gtPlusPlus.api.interfaces.IPlugin; +import gtPlusPlus.plugin.manager.Core_Manager; + +public class Core_VanillaFixes implements IPlugin { + + final static Core_VanillaFixes mInstance; + + static { + mInstance = new Core_VanillaFixes(); + mInstance.log("Preparing "+mInstance.getPluginName()+" for use."); + } + + Core_VanillaFixes() { + Core_Manager.registerPlugin(this); + } + + @Override + public boolean preInit() { + return false; + } + + @Override + public boolean init() { + return false; + } + + @Override + public boolean postInit() { + return false; + } + + @Override + public String getPluginName() { + return "GT++ Vanilla Fixes Module"; + } + + @Override + public String getPluginAbbreviation() { + return "VFIX"; + } + +} diff --git a/src/Java/gtPlusPlus/plugin/fixes/vanilla/VanillaBedHeightFix.java b/src/Java/gtPlusPlus/plugin/fixes/vanilla/VanillaBedHeightFix.java new file mode 100644 index 0000000000..a6811b5a15 --- /dev/null +++ b/src/Java/gtPlusPlus/plugin/fixes/vanilla/VanillaBedHeightFix.java @@ -0,0 +1,67 @@ +package gtPlusPlus.plugin.fixes.vanilla; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import cpw.mods.fml.common.eventhandler.EventPriority; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.relauncher.ReflectionHelper; +import gtPlusPlus.api.objects.Logger; +import gtPlusPlus.core.util.Utils; +import gtPlusPlus.preloader.DevHelper; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraftforge.event.entity.player.PlayerSleepInBedEvent; + +public class VanillaBedHeightFix { + + private static final VanillaBedHeightFix mInstance; + private final Method mSleepInBedAt; + + static { + mInstance = new VanillaBedHeightFix(); + } + + public VanillaBedHeightFix() { + if (DevHelper.isValidHelperObject()) { + Method m = DevHelper.getInstance().getForgeMethod(EntityPlayer.class, "sleepInBedAt", int.class, int.class, int.class); + if (m != null) { + mSleepInBedAt = m; + Utils.registerEvent(this); + } + else { + mSleepInBedAt = null; + } + } + else { + mSleepInBedAt = null; + } + } + + /** + * Fix created by deNULL - https://github.com/deNULL/BugPatch/blob/master/src/main/java/ru/denull/BugPatch/mod/ClientEvents.java#L45 + * @param evt - The event where a player sleeps + */ + + @SubscribeEvent(priority = EventPriority.HIGHEST) + public void playerSleepInBed(PlayerSleepInBedEvent evt) { + if (evt.y <= 0) { + int correctY = 256 + evt.y; + if (correctY <= 0) { + Logger.INFO("You're trying to sleep at y=" + evt.y + ", which is impossibly low. However, fixed y value is " + correctY + ", which is still below 0. Falling back to default behavior."); + } else { + Logger.INFO("You're trying to sleep at y=" + evt.y + ". This is probably caused by overflow, stopping original event; retrying with y=" + correctY + "."); + evt.result = EntityPlayer.EnumStatus.OTHER_PROBLEM; + if (mSleepInBedAt != null) { + try { + mSleepInBedAt.invoke(evt.entityPlayer, evt.x, correctY, evt.z); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + Logger.INFO("Encountered an error trying to sleep."); + } + } else { + Logger.INFO("Method sleepInBedAt was not found in EntityPlayer (wrong MC and/or Forge version?), unable to fix"); + } + } + } + } + +} diff --git a/src/Java/gtPlusPlus/preloader/DevHelper.java b/src/Java/gtPlusPlus/preloader/DevHelper.java index 729e1ddbaa..80830e869b 100644 --- a/src/Java/gtPlusPlus/preloader/DevHelper.java +++ b/src/Java/gtPlusPlus/preloader/DevHelper.java @@ -1,13 +1,15 @@ package gtPlusPlus.preloader; +import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; -import gtPlusPlus.api.objects.data.AutoMap; import gtPlusPlus.api.objects.data.Pair; +import gtPlusPlus.api.objects.data.weakref.WeakAutoMap; +import gtPlusPlus.core.util.reflect.ReflectionUtils; public class DevHelper { - + private static final DevHelper mInstance; private static final boolean mIsValidHelper; @@ -20,24 +22,84 @@ public class DevHelper { mIsValidHelper = false; } } - + public DevHelper() { - + + } + + public Method getForgeMethod(Class c, String s, Class... varags) { + String s1, s2; + Method a, b; + s1 = s; + s2 = getSRG(s); + + try { + a = ReflectionUtils.getMethod(c, s1, varags); + if (a != null) { + return a; + } + else { + b = ReflectionUtils.getMethod(c, s2, varags); + if (b != null) { + return b; + } + } + } + catch (Exception e) {} + return null; } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + public static synchronized final DevHelper getInstance() { return mInstance; } - + public static synchronized final boolean isValidHelperObject() { return mIsValidHelper; } + public String getSRG(String mForgeName) { + return DevHelperInternals.forgeToSrg.get(mForgeName); + } + + public String getForge(String mSrgName) { + return DevHelperInternals.srgToForge.get(mSrgName); + } + public static class DevHelperInternals { public static Map<String, String> srgToForge = new HashMap<String, String>(); public static Map<String, String> forgeToSrg = new HashMap<String, String>(); - private static AutoMap<Pair<String, String>> mInitMap = new AutoMap<Pair<String, String>>(); + private static WeakAutoMap<Pair<String, String>> mInitMap = new WeakAutoMap<Pair<String, String>>(); private static boolean init() { init1(); |