Skip to content

Commit

Permalink
fix: forge mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
IMB11 committed Aug 16, 2024
1 parent d470b3f commit 65eb8d1
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 44 deletions.
30 changes: 27 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ dependencies {
})

modImplementation(include("dev.architectury:architectury-${loader}:${property("deps.arch_api")}"))
modImplementation "dev.imb11:mru:${property("deps.mru")}+${mcVersion}+${loader}"
modImplementation ("dev.imb11:mru:${property("deps.mru")}+${mcVersion}+${loader}", {
transitive = false
})

modCompileOnly "dev.emi:emi-${loader}:${property("compile.emi")}"
modCompileOnly "maven.modrinth:inventorio:${property("compile.inventorio")}"
Expand All @@ -75,7 +77,9 @@ dependencies {
modLocalRuntime modCompileOnly("com.terraformersmc:modmenu:${property("runtime.modmenu")}")
include(implementation(annotationProcessor("io.github.llamalad7:mixinextras-fabric:0.5.0-beta.2")))
} else {
modImplementation(include("dev.isxander:yet-another-config-lib:${property("deps.yacl")}-${loader}"))
modImplementation(include("dev.isxander:yet-another-config-lib:${property("deps.yacl")}-${loader}"), {
transitive = false
})

"forgeRuntimeLibrary"(implementation include("com.twelvemonkeys.imageio:imageio-core:3.10.0"))
"forgeRuntimeLibrary"(implementation include("com.twelvemonkeys.imageio:imageio-webp:3.10.0"))
Expand Down Expand Up @@ -138,9 +142,10 @@ loom {
if (loader == "forge") {
forge {
convertAccessWideners = true
mixinConfig "mru.mixins.json"
mixinConfig "sounds.mixins.json"
}
}
accessWidenerPath = getRootProject().file("src/main/resources/sounds.accesswidener")
}

java {
Expand Down Expand Up @@ -192,6 +197,25 @@ processResources {
exclude("META-INF/neoforge.mods.toml")
}
}

// expand ${pack_format} in pack.mcmeta


int packFormat;
switch (mcVersion) {
case "1.20.1":
packFormat = 15;
break;
case "1.21":
packFormat = 34;
break;
default:
throw new IllegalArgumentException("Unsupported Minecraft version: " + mcVersion);
}

filesMatching("pack.mcmeta") {
expand("pack_format": packFormat)
}
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/imb11/sounds/gui/DynamicGridWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void calculateLayout() {
/*? >=1.21 {*/
child.widget().setHeight(thisCellHeight - padding * 2);
/*?} else {*/
/*((dev.imb11.sounds.mixin.accessors.ClickableWidgetAccessor) child.widget()).setHeight_1_20_1(thisCellHeight - padding * 2);
/*child.widget().height = (thisCellHeight - padding * 2);
*//*?}*/


Expand Down
36 changes: 25 additions & 11 deletions src/main/java/dev/imb11/sounds/gui/ImageButtonWidget.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.imb11.sounds.gui;

import dev.imb11.sounds.mixin.accessors.AnimatedDynamicTextureImageAccessor;
import dev.isxander.yacl3.gui.image.ImageRendererManager;
import dev.isxander.yacl3.gui.image.impl.AnimatedDynamicTextureImage;
import net.minecraft.client.MinecraftClient;
Expand All @@ -13,6 +12,7 @@
import net.minecraft.util.math.ColorHelper;
import net.minecraft.util.math.MathHelper;

import java.lang.reflect.Field;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;
Expand Down Expand Up @@ -65,16 +65,30 @@ protected void renderWidget(DrawContext context, int mouseX, int mouseY, float d
var contentImage = image.get();
if (contentImage != null) {

// Scale the image so that the image height is the same as the button height.
float neededWidth = ((AnimatedDynamicTextureImageAccessor) contentImage).getFrameWidth() * ((float) this.height / ((AnimatedDynamicTextureImageAccessor) contentImage).getFrameHeight());

// Scale the image to fit within the width and height of the button.
context.getMatrices().push();
// gl bilinear scaling.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
contentImage.render(context, getX(), getY(), (int) Math.max(neededWidth, this.width), delta);
context.getMatrices().pop();
// Using reflection, get value of contentImage.frameWidth and frameHeight
try {
Field frameWidthField = contentImage.getClass().getDeclaredField("frameWidth");
frameWidthField.setAccessible(true);
int frameWidth = frameWidthField.getInt(contentImage);

Field frameHeightField = contentImage.getClass().getDeclaredField("frameHeight");
frameHeightField.setAccessible(true);
int frameHeight = frameHeightField.getInt(contentImage);

// Use frameWidth and frameHeight as needed
// Scale the image so that the image height is the same as the button height.
float neededWidth = frameWidth * ((float) this.height / frameHeight);

// Scale the image to fit within the width and height of the button.
context.getMatrices().push();
// gl bilinear scaling.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
contentImage.render(context, getX(), getY(), (int) Math.max(neededWidth, this.width), delta);
context.getMatrices().pop();
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
} catch (InterruptedException | ExecutionException ignored) {
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dev/imb11/sounds/loaders/forge/SoundsForge.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
//? if forge {
/*package dev.imb11.sounds.loaders.forge;
import dev.architectury.platform.forge.EventBuses;
import dev.imb11.sounds.SoundsClient;
import dev.imb11.sounds.gui.SoundsConfigScreen;
import net.minecraftforge.client.ConfigScreenHandler;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
@Mod("sounds")
public class SoundsForge {
public SoundsForge() {
EventBuses.registerModEventBus("sounds", FMLJavaModLoadingContext.get().getModEventBus());
SoundsClient.init();
ModLoadingContext.get().registerExtensionPoint(
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/dev/imb11/sounds/mixin/BlockSoundMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Debug;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -27,6 +28,7 @@
@Mixin(AbstractBlock.class)
/*?} else {*/
/*@Mixin(Block.class)
@Debug(export = true)
*//*?}*/
public abstract class BlockSoundMixin implements BlockAccessor {
@Unique
Expand Down Expand Up @@ -57,6 +59,7 @@ public abstract class BlockSoundMixin implements BlockAccessor {
@Inject(method = "getSoundGroup", at = @At("HEAD"), cancellable = true)
public void $manageCustomSounds(BlockState state, CallbackInfoReturnable<BlockSoundGroup> cir) {
// if(MinecraftClient.getInstance().world == null) return;
SoundsClient.LOGGER.info("Getting sound group for " + state.getBlock().getTranslationKey());
try {
if(!hasFetched) {
sounds$prepareTagPair(Registries.BLOCK.getId(state.getBlock()));
Expand Down

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"license": "ARR",
"icon": "assets/sounds/icon.png",
"accessWidener": "sounds.accesswidener",
"environment": "client",
"entrypoints": {
"client": [
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/pack.mcmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"description": "Library mod providing compatability utilities and other stuff.",
"pack_format": ${pack_format}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/sounds.accesswidener
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
accessWidener v2 named
accessible field net/minecraft/client/gui/widget/ClickableWidget height I
2 changes: 0 additions & 2 deletions src/main/resources/sounds.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
"BlockSoundMixin",
"SoundsOptionsScreenMixin",
"TitleScreenWidget",
"accessors.AnimatedDynamicTextureImageAccessor",
"accessors.ClickableWidgetAccessor",
"accessors.BlockAccessor",
"compat.EmiSoundEffects",
"compat.InventiveInventoryCompatMixin",
Expand Down

0 comments on commit 65eb8d1

Please sign in to comment.