Slabs fix and some cleaner reflection helpers

master
Wynd 2024-01-12 15:29:02 +02:00
parent 3889189f0b
commit 95c9317572
4 changed files with 76 additions and 40 deletions

View File

@ -59,6 +59,8 @@ public class ModConfig {
"Drops 3 books when breaking a bookshelf");
public static final Option<Boolean> FIX_DOUBLE_DOORS = make("Fix Double Doors", true,
"Fixes double doors not being in their correct state when pressure plates are used to open them");
public static final Option<Boolean> FIX_SLABS_RECIPE = make("Fix Slabs Recipe", true,
"Givs two slabs for every block, since 1 block = 2 slabs => 3 blocks = 6 slabs (instead of 3)");
private static ModConfig instance = new ModConfig();
public static final ModConfig instance() {

View File

@ -33,38 +33,44 @@ public class WyHelper {
INSTANCE = getInstance();
}
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 Minecraft getInstance() {
if (INSTANCE != null) {
return INSTANCE;
} else {
try {
Field field = null;
try {
field = Minecraft.class.getDeclaredField("instance");
} catch (NoSuchFieldException ex) {
try {
field = Minecraft.class.getDeclaredField("field_2791");
} catch (NoSuchFieldException | SecurityException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
if (field == null) {
return null;
}
field.setAccessible(true);
return (Minecraft) field.get(null);
return (Minecraft) getField(Minecraft.class, "instance", "field_2791").get(null);
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}
}
public static long getRealDaysPlayed() {
long seconds = WyHelper.playTime / 20;
@ -89,24 +95,13 @@ public class WyHelper {
}
public static void registerWhiteWoolRecipe() {
Method method = null;
try {
method = RecipeRegistry.class.getDeclaredMethod("addShapelessRecipe", ItemInstance.class, Object[].class);
} catch (NoSuchMethodException ex) {
try {
method = RecipeRegistry.class.getDeclaredMethod("method_542", ItemInstance.class, Object[].class);
} catch (NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
}
Method method = getMethod(RecipeRegistry.class, new String[] { "addShapelessRecipe", "method_542" }, ItemInstance.class, Object[].class);
if (method == null) {
return;
}
try {
method.setAccessible(true);
// Makes new recipes for all colored wools so they can be dyed back
// white using bone meal
for (int colorId = 0; colorId < 16; ++colorId) {
@ -119,10 +114,13 @@ public class WyHelper {
}
public static void cheatCommand(Player player) {
// player.level.setLevelTime(0);
// player.level.getProperties().setRaining(false);
// player.level.getProperties().setRainTime(0);
// player.level.getProperties().setThundering(false);
// player.level.getProperties().setThunderTime(0);
player.dropItem(new ItemInstance(ItemType.redstone, 64));
player.level.setLevelTime(0);
player.level.getProperties().setRaining(false);
player.level.getProperties().setRainTime(0);
player.level.getProperties().setThundering(false);
player.level.getProperties().setThunderTime(0);
}
}

View File

@ -0,0 +1,35 @@
package xyz.pixelatedw.finalbeta.mixin;
import java.util.List;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import net.minecraft.item.ItemInstance;
import net.minecraft.item.StoneSlabItem;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeRegistry;
import xyz.pixelatedw.finalbeta.ModConfig;
@Mixin(RecipeRegistry.class)
public class RecipeRegistryMixin {
@Shadow
private List<Recipe> recipes;
@Inject(method = "<init>", at = @At("TAIL"))
private void recipesChanges(CallbackInfo ci) {
if(ModConfig.FIX_SLABS_RECIPE.get()) {
for (Recipe recipe : this.recipes) {
ItemInstance itemStack = recipe.getOutput();
if (itemStack.getType() instanceof StoneSlabItem) {
itemStack.count = 6;
}
}
}
}
}

View File

@ -38,7 +38,8 @@
"LevelMixin",
"DoorTileMixin",
"CraftingContainerMixin",
"CraftingInventoryMixin"
"CraftingInventoryMixin",
"RecipeRegistryMixin"
],
"injectors": {
"defaultRequire": -1