Minecraft static accessor doesn't really want to work so back to reflection we are

master
Wynd 2024-09-11 00:53:22 +03:00
parent 76c7f45d78
commit 43026a9f60
7 changed files with 70 additions and 39 deletions

View File

@ -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;
} }

View File

@ -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) {

View File

@ -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();
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -44,8 +44,7 @@
"SlimeMixin", "SlimeMixin",
"DimensionMixin", "DimensionMixin",
"TranslationStorageMixin", "TranslationStorageMixin",
"RecipeRegistryAccessorMixin", "RecipeRegistryAccessor"
"MinecraftAccessorMixin"
], ],
"injectors": { "injectors": {
"defaultRequire": -1 "defaultRequire": -1