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"); "Drops 3 books when breaking a bookshelf");
public static final Option<Boolean> FIX_DOUBLE_DOORS = make("Fix Double Doors", true, 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"); "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(); private static ModConfig instance = new ModConfig();
public static final ModConfig instance() { public static final ModConfig instance() {

View File

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