Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixmeta2 1.20.1 #18

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ loom {
sourceSet sourceSets.client
}
}

accessWidenerPath.set(file("src/main/resources/dynamicpack.accesswidener"))
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ public static void addAllowedHosts(String host, Object requester) throws Excepti
Mod.addAllowedHosts(host, requester);
}

public boolean isNameIsDynamic(String name) {
return getDynamicPackByMinecraftName(name) != null;
}

public Pack getDynamicPackByMinecraftName(String name) {
for (Pack pack : packs) {
if (("file/" + pack.getName()).equals(name)) {
return pack;
}
}
return null;
}

public boolean isResourcePackActive(Pack pack) throws IOException {
for (String readLine : Files.readAllLines(new File(getGameDir(), "options.txt").toPath(), StandardCharsets.UTF_8)) {
if (readLine.startsWith("resourcePacks:")) {
Expand Down Expand Up @@ -124,5 +137,5 @@ public boolean isMinecraftInitialized() {

public abstract String getCurrentGameVersion();

public abstract boolean checkResourcePackMetaValid(String s);
public abstract boolean checkResourcePackMetaValid(String s) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.adamcalculator.dynamicpack;

import com.adamcalculator.dynamicpack.pack.Pack;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text;

public class DynamicPackScreen extends Screen {
private final Screen parent;

public DynamicPackScreen(Screen parent, Pack pack) {
super(Text.literal(pack.getName()));
this.client = MinecraftClient.getInstance();
this.parent = parent;
}

@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
renderBackground(context);
super.render(context, mouseX, mouseY, delta);
}

@Override
protected void init() {
addDrawableChild(ButtonWidget.builder(Text.of("Manually sync"), button -> DynamicPackModBase.INSTANCE.startSyncThread()).size(120, 20).position(this.width / 2, this.height / 2).build());
}

@Override
public void close() {
this.client.setScreen(parent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,8 @@ public String getCurrentGameVersion() {

@Override
public boolean checkResourcePackMetaValid(String s) {
try {
new PackResourceMetadataReader().fromJson(JsonHelper.deserialize(s));
return true;
} catch (Exception e) {
return false;
}
new PackResourceMetadataReader().fromJson(JsonHelper.deserialize(s).getAsJsonObject("pack"));
return true;
}

private void tryToReloadResources() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.adamcalculator.dynamicpack;

import com.adamcalculator.dynamicpack.pack.Pack;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.pack.PackListWidget;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

public class PackMixinHelper {
public static void renderResourcePackEntry(Object resourcePackEntryMixin, DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci) {
PackListWidget.ResourcePackEntry entry = (PackListWidget.ResourcePackEntry) resourcePackEntryMixin;
if (DynamicPackModBase.INSTANCE.isNameIsDynamic(entry.getName())) {
int i = mouseX - x;
int j = mouseY - y;
context.drawTexture(Identifier.of("dynamicpack", "select_button.png"), x + 174, y+16, 0.0F, ((i >= 174 && j >= 16 && hovered) ? 16f : 0f), 16, 16, 16, 32);
}
}

public static void mouseClicked(Object resourcePackEntryMixin, PackListWidget widget, double mouseX, double mouseY, int button, CallbackInfoReturnable<Boolean> cir) {
PackListWidget.ResourcePackEntry entry = (PackListWidget.ResourcePackEntry) resourcePackEntryMixin;
Pack pack = DynamicPackModBase.INSTANCE.getDynamicPackByMinecraftName(entry.getName());
if (pack != null) {
double d = mouseX - (double)widget.getRowLeft();
double e = mouseY - (double)widget.getRowTop(widget.children().indexOf(entry));

if (d >= 174) {
if (e >= 16) {
openPackScreen(pack);
}
}
}
}

private static void openPackScreen(Pack pack) {
MinecraftClient.getInstance().setScreen(new DynamicPackScreen(MinecraftClient.getInstance().currentScreen, pack));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.adamcalculator.dynamicpack.mixin.client;

import com.adamcalculator.dynamicpack.PackMixinHelper;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.pack.PackListWidget;
import org.spongepowered.asm.mixin.Final;
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 org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(PackListWidget.ResourcePackEntry.class)
public abstract class ResourcePackEntryMixin {
@Shadow @Final private PackListWidget widget;

@Inject(at = @At("RETURN"), method = "render")
private void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci) {
PackMixinHelper.renderResourcePackEntry(this, context, index, y, x, entryWidth, entryHeight, mouseX, mouseY, hovered, tickDelta, ci);
}

@Inject(at = @At("RETURN"), method = "mouseClicked")
private void mouseClicked(double mouseX, double mouseY, int button, CallbackInfoReturnable<Boolean> cir) {
PackMixinHelper.mouseClicked(this, widget, mouseX, mouseY, button, cir);
}
}
3 changes: 2 additions & 1 deletion src/client/resources/dynamicpack.client.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"package": "com.adamcalculator.dynamicpack.mixin.client",
"compatibilityLevel": "JAVA_17",
"client": [
"MinecraftClientMixin"
"MinecraftClientMixin",
"ResourcePackEntryMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
Binary file modified src/main/resources/assets/dynamicpack/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/main/resources/dynamicpack.accesswidener
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
accessWidener v2 named
accessible method net/minecraft/client/gui/widget/EntryListWidget getRowTop (I)I
1 change: 1 addition & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"license": "MIT",
"icon": "assets/dynamicpack/icon.png",
"environment": "client",
"accessWidener": "dynamicpack.accesswidener",
"entrypoints": {
"modmenu": [
"com.adamcalculator.dynamicpack.ModMenu"
Expand Down
Loading