Minecraft static accessor doesn't really want to work so back to reflection we are
parent
76c7f45d78
commit
43026a9f60
|
@ -1,18 +1,21 @@
|
||||||
package xyz.pixelatedw.finalbeta;
|
package xyz.pixelatedw.finalbeta;
|
||||||
|
|
||||||
import java.lang.management.ManagementFactory;
|
import java.lang.management.ManagementFactory;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.entity.player.Player;
|
import net.minecraft.entity.player.Player;
|
||||||
import net.minecraft.item.ItemInstance;
|
import net.minecraft.item.ItemInstance;
|
||||||
import net.minecraft.item.ItemType;
|
import net.minecraft.item.ItemType;
|
||||||
import net.minecraft.recipe.RecipeRegistry;
|
import net.minecraft.recipe.RecipeRegistry;
|
||||||
import xyz.pixelatedw.finalbeta.mixin.MinecraftAccessorMixin;
|
import xyz.pixelatedw.finalbeta.mixin.RecipeRegistryAccessor;
|
||||||
import xyz.pixelatedw.finalbeta.mixin.RecipeRegistryAccessorMixin;
|
|
||||||
|
|
||||||
public class WyHelper {
|
public class WyHelper {
|
||||||
|
|
||||||
|
private static final Minecraft INSTANCE;
|
||||||
public static final String SPAWN_TIME_TAG = "SpawnTime";
|
public static final String SPAWN_TIME_TAG = "SpawnTime";
|
||||||
public static final String PLAY_TIME_TAG = "PlayTime";
|
public static final String PLAY_TIME_TAG = "PlayTime";
|
||||||
public static long playTime;
|
public static long playTime;
|
||||||
|
@ -23,12 +26,55 @@ public class WyHelper {
|
||||||
public static final HashMap<Integer, Long> DOOR_UPDATES = new HashMap<>();
|
public static final HashMap<Integer, Long> DOOR_UPDATES = new HashMap<>();
|
||||||
public static final HashMap<Integer, Boolean> DOOR_STATES = new HashMap<>();
|
public static final HashMap<Integer, Boolean> DOOR_STATES = new HashMap<>();
|
||||||
|
|
||||||
|
static {
|
||||||
|
INSTANCE = getInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Minecraft getInstance() {
|
||||||
|
if (INSTANCE != null) {
|
||||||
|
return INSTANCE;
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
return (Minecraft) getField(Minecraft.class, "instance", "field_2791").get(null);
|
||||||
|
} catch (IllegalArgumentException | IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Field getField(Class clz, String... names) {
|
||||||
|
for (String name : names) {
|
||||||
|
try {
|
||||||
|
Field field = clz.getDeclaredField(name);
|
||||||
|
field.setAccessible(true);
|
||||||
|
return field;
|
||||||
|
} catch (NoSuchFieldException ex) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Method getMethod(Class clz, String[] names, Class<?>... params) {
|
||||||
|
for (String name : names) {
|
||||||
|
try {
|
||||||
|
Method method = clz.getDeclaredMethod(name, params);
|
||||||
|
method.setAccessible(true);
|
||||||
|
return method;
|
||||||
|
} catch (NoSuchMethodException ex) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public static void addShapedRecipe(ItemInstance result, Object... recipe) {
|
public static void addShapedRecipe(ItemInstance result, Object... recipe) {
|
||||||
((RecipeRegistryAccessorMixin) RecipeRegistry.getInstance()).invokeAddShapedRecipe(result, recipe);
|
((RecipeRegistryAccessor) RecipeRegistry.getInstance()).invokeAddShapedRecipe(result, recipe);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void addShapelessRecipe(ItemInstance result, Object... ingredients) {
|
public static void addShapelessRecipe(ItemInstance result, Object... ingredients) {
|
||||||
((RecipeRegistryAccessorMixin) RecipeRegistry.getInstance()).invokeAddShapelessRecipe(result, ingredients);
|
((RecipeRegistryAccessor) RecipeRegistry.getInstance()).invokeAddShapelessRecipe(result, ingredients);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long getRealDaysPlayed() {
|
public static long getRealDaysPlayed() {
|
||||||
|
@ -37,7 +83,7 @@ public class WyHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long getGameDaysPlayed() {
|
public static long getGameDaysPlayed() {
|
||||||
long seconds = MinecraftAccessorMixin.getMinecraft().level.getLevelTime() / 20;
|
long seconds = WyHelper.getInstance().level.getLevelTime() / 20;
|
||||||
return Duration.ofSeconds(seconds).toMinutes() / 20;
|
return Duration.ofSeconds(seconds).toMinutes() / 20;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class ClientPlayerMixin {
|
||||||
|
|
||||||
@Inject(method = "method_136", at = @At("TAIL"))
|
@Inject(method = "method_136", at = @At("TAIL"))
|
||||||
public void onKeyPressed(int key, boolean state, CallbackInfo ci) {
|
public void onKeyPressed(int key, boolean state, CallbackInfo ci) {
|
||||||
Minecraft mc = MinecraftAccessorMixin.getMinecraft();
|
Minecraft mc = WyHelper.getInstance();
|
||||||
Player player = (Player) (Object) this;
|
Player player = (Player) (Object) this;
|
||||||
|
|
||||||
if (player.vehicle != null && key == Keyboard.KEY_LSHIFT && state) {
|
if (player.vehicle != null && key == Keyboard.KEY_LSHIFT && state) {
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
package xyz.pixelatedw.finalbeta.mixin;
|
|
||||||
|
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
|
||||||
import org.spongepowered.asm.mixin.gen.Accessor;
|
|
||||||
|
|
||||||
import net.minecraft.client.Minecraft;
|
|
||||||
|
|
||||||
@Mixin(Minecraft.class)
|
|
||||||
public interface MinecraftAccessorMixin {
|
|
||||||
@Accessor("instance")
|
|
||||||
public static Minecraft getMinecraft() {
|
|
||||||
throw new AssertionError();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -16,7 +16,7 @@ import xyz.pixelatedw.finalbeta.MainMod;
|
||||||
import xyz.pixelatedw.finalbeta.ModConfig;
|
import xyz.pixelatedw.finalbeta.ModConfig;
|
||||||
|
|
||||||
@Mixin(Minecraft.class)
|
@Mixin(Minecraft.class)
|
||||||
public class MinecraftMixin {
|
public class MinecraftMixin {
|
||||||
@Redirect(method = "init()V", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/Display;create()V"), remap = false)
|
@Redirect(method = "init()V", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/Display;create()V"), remap = false)
|
||||||
public void createDisplay() throws LWJGLException {
|
public void createDisplay() throws LWJGLException {
|
||||||
// Why the fuck is this even a thing ? What was its intended purpose ? I NEED TO KNOW
|
// Why the fuck is this even a thing ? What was its intended purpose ? I NEED TO KNOW
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
package xyz.pixelatedw.finalbeta.mixin;
|
||||||
|
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.gen.Invoker;
|
||||||
|
|
||||||
|
import net.minecraft.item.ItemInstance;
|
||||||
|
import net.minecraft.recipe.RecipeRegistry;
|
||||||
|
|
||||||
|
@Mixin(RecipeRegistry.class)
|
||||||
|
public interface RecipeRegistryAccessor {
|
||||||
|
@Invoker("addShapedRecipe")
|
||||||
|
void invokeAddShapedRecipe(ItemInstance result, Object... shape);
|
||||||
|
|
||||||
|
@Invoker("addShapelessRecipe")
|
||||||
|
void invokeAddShapelessRecipe(ItemInstance result, Object... materials);
|
||||||
|
}
|
|
@ -1,16 +0,0 @@
|
||||||
package xyz.pixelatedw.finalbeta.mixin;
|
|
||||||
|
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
|
||||||
import org.spongepowered.asm.mixin.gen.Invoker;
|
|
||||||
|
|
||||||
import net.minecraft.item.ItemInstance;
|
|
||||||
import net.minecraft.recipe.RecipeRegistry;
|
|
||||||
|
|
||||||
@Mixin(RecipeRegistry.class)
|
|
||||||
public interface RecipeRegistryAccessorMixin {
|
|
||||||
@Invoker("addShapedRecipe")
|
|
||||||
void invokeAddShapedRecipe(ItemInstance result, Object... shape);
|
|
||||||
|
|
||||||
@Invoker("addShapelessRecipe")
|
|
||||||
void invokeAddShapelessRecipe(ItemInstance result, Object... materials);
|
|
||||||
}
|
|
|
@ -44,8 +44,7 @@
|
||||||
"SlimeMixin",
|
"SlimeMixin",
|
||||||
"DimensionMixin",
|
"DimensionMixin",
|
||||||
"TranslationStorageMixin",
|
"TranslationStorageMixin",
|
||||||
"RecipeRegistryAccessorMixin",
|
"RecipeRegistryAccessor"
|
||||||
"MinecraftAccessorMixin"
|
|
||||||
],
|
],
|
||||||
"injectors": {
|
"injectors": {
|
||||||
"defaultRequire": -1
|
"defaultRequire": -1
|
||||||
|
|
Loading…
Reference in New Issue