Added coal and redstone blocks for better storage and removed some reflection in favor of accessors and invokers

master
Wynd 2024-09-10 00:32:41 +03:00
parent 53b359f751
commit e4d3f10587
13 changed files with 155 additions and 101 deletions

View File

@ -4,6 +4,10 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.fabricmc.api.ModInitializer;
import net.minecraft.item.ItemInstance;
import net.minecraft.item.ItemType;
import net.minecraft.tile.Tile;
import net.minecraft.tile.WoolTile;
public class MainMod implements ModInitializer {
public static final Logger LOGGER = LogManager.getLogger();
@ -11,5 +15,17 @@ public class MainMod implements ModInitializer {
@Override
public void onInitialize() {
ModConfig.instance();
if (ModConfig.ENABLE_WHITE_WOOL_RECIPE.get()) {
for (int colorId = 0; colorId < 16; ++colorId) {
WyHelper.addShapelessRecipe(new ItemInstance(Tile.WOOL, 1, 0), new Object[]{
new ItemInstance(Tile.WOOL, 1, WoolTile.method_2(colorId)), new ItemInstance(ItemType.dyePowder, 1, 15)});
}
}
if (ModConfig.ENABLE_COAL_AND_REDSTONE_BLOCKS_RECIPE.get()) {
WyHelper.addShapedRecipe(new ItemInstance(ModTile.COAL_BLOCK, 1, 0), "###", "###", "###", '#', ItemType.coal);
WyHelper.addShapedRecipe(new ItemInstance(ModTile.REDSTONE_BLOCK, 1, 0), "###", "###", "###", '#', ItemType.redstone);
}
}
}

View File

@ -50,6 +50,8 @@ public class ModConfig {
"Name says it all, furnace minecarts will load chunks as they go on tracks.");
public static final Option<Double> CLOUDS_HEIGHT = make("Clouds Height", 0.0,
"Clouds height modifier, goes from 0.0, meaning the default 108 blocks height, to 1.0, meaning a 324 blocks height.");
public static final Option<Boolean> ENABLE_COAL_AND_REDSTONE_BLOCKS_RECIPE = make("Enable Coal and Redstone Blocks Recipe", false,
"Allows crafting coal and redstone as blocks for better storage");
public static final Option<Boolean> FIX_BOW_MODEL = make("Fix bow model", true,
"Makes the box model held by players and skeletons bigger and facing forward");

View File

@ -0,0 +1,59 @@
package xyz.pixelatedw.finalbeta;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.item.ItemType;
import net.minecraft.item.PlaceableTileItem;
import net.minecraft.level.TileView;
import net.minecraft.tile.Tile;
import net.minecraft.tile.TileSounds;
import net.minecraft.tile.material.Material;
public class ModTile extends Tile {
public static final Tile REDSTONE_BLOCK = (new ModTile(100, 176, Material.STONE)).hardness(3.0F).blastResistance(5.0F).sounds(Tile.STONE_SOUNDS).tint(11149858).name("blockRedstone");
public static final Tile COAL_BLOCK = (new ModTile(101, 103, Material.STONE)).hardness(3.0F).blastResistance(5.0F).sounds(Tile.STONE_SOUNDS).tint(1118481).name("blockCoal");
static {
ItemType.byId[REDSTONE_BLOCK.id] = (new PlaceableTileItem(REDSTONE_BLOCK.id - 256)).setName("redstoneBlock");
ItemType.byId[COAL_BLOCK.id] = (new PlaceableTileItem(COAL_BLOCK.id - 256)).setName("coalBlock");
}
private int tint = 16777215;
public ModTile(int id, int texId, Material mat) {
super(id, texId, mat);
}
@Override
public ModTile hardness(float hardness) {
return (ModTile) super.hardness(hardness);
}
@Override
public ModTile blastResistance(float resistance) {
return (ModTile) super.blastResistance(resistance);
}
@Override
public ModTile sounds(TileSounds sounds) {
return (ModTile) super.sounds(sounds);
}
public ModTile tint(int tint) {
this.tint = tint;
return this;
}
@Override
@Environment(EnvType.CLIENT)
public int method_1589(int i) {
return this.tint;
}
@Override
@Environment(EnvType.CLIENT)
public int getTint(TileView tile, int x, int y, int z) {
return this.tint;
}
}

View File

@ -1,17 +1,17 @@
package xyz.pixelatedw.finalbeta;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.HashMap;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.Player;
import net.minecraft.item.ItemInstance;
import net.minecraft.recipe.RecipeRegistry;
import xyz.pixelatedw.finalbeta.mixin.MinecraftAccessorMixin;
import xyz.pixelatedw.finalbeta.mixin.RecipeRegistryAccessorMixin;
public class WyHelper {
private static final Minecraft INSTANCE;
public static final String SPAWN_TIME_TAG = "SpawnTime";
public static final String PLAY_TIME_TAG = "PlayTime";
public static long playTime;
@ -21,48 +21,13 @@ public class WyHelper {
// doors have ok ? I don't even care about the overhead at this point.
public static final HashMap<Integer, Long> DOOR_UPDATES = new HashMap<>();
public static final HashMap<Integer, Boolean> DOOR_STATES = new HashMap<>();
static {
INSTANCE = getInstance();
public static void addShapedRecipe(ItemInstance result, Object... recipe) {
((RecipeRegistryAccessorMixin) RecipeRegistry.getInstance()).invokeAddShapedRecipe(result, recipe);
}
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 {
return (Minecraft) getField(Minecraft.class, "instance", "field_2791").get(null);
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
public static void addShapelessRecipe(ItemInstance result, Object... ingredients) {
((RecipeRegistryAccessorMixin) RecipeRegistry.getInstance()).invokeAddShapelessRecipe(result, ingredients);
}
public static long getRealDaysPlayed() {
@ -71,7 +36,7 @@ public class WyHelper {
}
public static long getGameDaysPlayed() {
long seconds = WyHelper.getInstance().level.getLevelTime() / 20;
long seconds = MinecraftAccessorMixin.getMinecraft().level.getLevelTime() / 20;
return Duration.ofSeconds(seconds).toMinutes() / 20;
}

View File

@ -24,7 +24,7 @@ public class ClientPlayerMixin {
@Inject(method = "method_136", at = @At("TAIL"))
public void onKeyPressed(int key, boolean state, CallbackInfo ci) {
Minecraft mc = WyHelper.getInstance();
Minecraft mc = MinecraftAccessorMixin.getMinecraft();
Player player = (Player) (Object) this;
if (player.vehicle != null && key == Keyboard.KEY_LSHIFT && state) {

View File

@ -1,52 +0,0 @@
package xyz.pixelatedw.finalbeta.mixin;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.spongepowered.asm.mixin.Mixin;
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.container.Container;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.item.ItemInstance;
import net.minecraft.item.ItemType;
import net.minecraft.recipe.RecipeRegistry;
import net.minecraft.tile.Tile;
import net.minecraft.tile.WoolTile;
import xyz.pixelatedw.finalbeta.ModConfig;
import xyz.pixelatedw.finalbeta.WyHelper;
@Mixin(CraftingInventory.class)
public class CraftingInventoryMixin {
private static boolean hasCustomRecipesRegistered = false;
@Inject(method = "<init>", at = @At("TAIL"))
public void init(Container container, int x, int y, CallbackInfo ci) {
if (ModConfig.ENABLE_WHITE_WOOL_RECIPE.get() && !hasCustomRecipesRegistered) {
registerWhiteWoolRecipe();
hasCustomRecipesRegistered = true;
}
}
private static void registerWhiteWoolRecipe() {
Method method = WyHelper.getMethod(RecipeRegistry.class, new String[] { "addShapelessRecipe", "method_542" }, ItemInstance.class, Object[].class);
if (method == null) {
return;
}
try {
// Makes new recipes for all colored wools so they can be dyed back
// white using bone meal
for (int colorId = 0; colorId < 16; ++colorId) {
method.invoke(RecipeRegistry.getInstance(), new ItemInstance(Tile.WOOL, 1, 0), new Object[]{
new ItemInstance(Tile.WOOL, 1, WoolTile.method_2(colorId)), new ItemInstance(ItemType.dyePowder, 1, 15)});
}
} catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
ex.printStackTrace();
}
}
}

View File

@ -0,0 +1,14 @@
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 RecipeRegistryAccessorMixin {
@Invoker("addShapedRecipe")
void invokeAddShapedRecipe(ItemInstance result, Object... shape);
@Invoker("addShapelessRecipe")
void invokeAddShapelessRecipe(ItemInstance result, Object... materials);
}

View File

@ -13,6 +13,7 @@ import net.minecraft.item.tool.ToolItem;
import net.minecraft.item.tool.ToolMaterial;
import net.minecraft.tile.Tile;
import xyz.pixelatedw.finalbeta.ModConfig;
import xyz.pixelatedw.finalbeta.ModTile;
@Mixin(ToolItem.class)
public class ToolItemMixin {
@ -22,7 +23,8 @@ public class ToolItemMixin {
Tile.DIAMOND_ORE, Tile.BLOCK_DIAMOND, Tile.ICE, Tile.NETHERRACK, Tile.LAPIS_LAZULI_ORE, Tile.LAPIS_LAZULI_BLOCK,
// Here starts the list of blocks that are not affected by default
Tile.STAIRS_STONE, Tile.REDSTONE_ORE, Tile.REDSTONE_ORE_LIT, Tile.DOOR_IRON, Tile.BRICK, Tile.FURNACE, Tile.FURNACE_LIT,
Tile.DISPENSER, Tile.STONE_PRESSURE_PLATE, Tile.RAIL, Tile.DETECTOR_RAIL, Tile.GOLDEN_RAIL, Tile.PISTON, Tile.STICKY_PISTON};
Tile.DISPENSER, Tile.STONE_PRESSURE_PLATE, Tile.RAIL, Tile.DETECTOR_RAIL, Tile.GOLDEN_RAIL, Tile.PISTON, Tile.STICKY_PISTON,
ModTile.COAL_BLOCK, ModTile.REDSTONE_BLOCK};
private static final Tile[] HATCHET_BLOCKS = new Tile[]{Tile.WOOD, Tile.BOOKSHELF, Tile.LOG, Tile.CHEST,
// Here starts the list of blocks that are not affected by default

View File

@ -0,0 +1,27 @@
package xyz.pixelatedw.finalbeta.mixin;
import java.io.IOException;
import java.util.Properties;
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.client.resource.language.TranslationStorage;
@Mixin(TranslationStorage.class)
public class TranslationStorageMixin {
@Shadow
private Properties translations;
@Inject(method = "<init>()V", at = @At("TAIL"))
private void init(CallbackInfo ci) {
try {
this.translations.load(TranslationStorage.class.getResourceAsStream("/assets/finalbeta/lang/en_US.lang"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 453 B

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@ -0,0 +1,3 @@
tile.blockRedstone.name=Redstone Block
tile.blockCoal.name=Coal Block

View File

@ -38,12 +38,14 @@
"LevelMixin",
"DoorTileMixin",
"CraftingContainerMixin",
"CraftingInventoryMixin",
"RecipeRegistryMixin",
"SnowballMixin",
"EntityMixin",
"SlimeMixin",
"DimensionMixin"
"DimensionMixin",
"TranslationStorageMixin",
"RecipeRegistryAccessorMixin",
"MinecraftAccessorMixin"
],
"injectors": {
"defaultRequire": -1