From 2df9bda18ff358e73051db29fc67d5bb8be29043 Mon Sep 17 00:00:00 2001 From: UltraBlackLinux Date: Thu, 6 May 2021 09:26:50 +0200 Subject: [PATCH] - bugfixes regarding skull rendering - code cleanup --- README.md | 2 +- TODO.md | 1 + gradle.properties | 2 +- .../client/MineMenuFabricClient.java | 24 ++++++++--- .../minemenufabric/client/config/Config.java | 9 ++-- .../client/screen/MineMenuSelectScreen.java | 43 +++++++------------ .../client/screen/MineMenuSettingsScreen.java | 34 ++++++--------- .../client/util/RandomUtil.java | 31 +++++++++++-- .../assets/minemenufabric/lang/de_de.json | 5 ++- .../assets/minemenufabric/lang/en_us.json | 5 ++- 10 files changed, 93 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 835e1dd..1604d55 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,6 @@ MineMenuFabric is a customizable menu, that lets you manage commands and chat me Supports enchanted/playerhead icons and copying text, opening links, saying a message in the chat and creating categories to keep your menu sorted! -**Note: You need to reset the config after some major updates, as config sometimes changes and crashes your game, when you open the menu!** +**Note: You may need to reset the config after major updates, as the as it might change and crash your game, as you open the menu!** Thanks to Girafi for the original mod [❤️](https://www.curseforge.com/minecraft/mc-mods/minemenu) diff --git a/TODO.md b/TODO.md index c2119e9..39780d9 100644 --- a/TODO.md +++ b/TODO.md @@ -2,6 +2,7 @@ - Wrong user input in the config - working? #Not working stuff +- enchant button sometimes requires two presses to get toggled #TODO ~~- toggle keybind~~ You can't press a key, while in a gui diff --git a/gradle.properties b/gradle.properties index 16d7ecf..f53c3c2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.16.5 yarn_mappings=1.16.5+build.8 loader_version=0.11.3 # Mod Properties -mod_version=2.1 +mod_version=2.2 maven_group=me.UltraBlackLinux archives_base_name=MineMenuFabric # Dependencies diff --git a/src/main/java/me/ultrablacklinux/minemenufabric/client/MineMenuFabricClient.java b/src/main/java/me/ultrablacklinux/minemenufabric/client/MineMenuFabricClient.java index 7a0089d..e9b9544 100644 --- a/src/main/java/me/ultrablacklinux/minemenufabric/client/MineMenuFabricClient.java +++ b/src/main/java/me/ultrablacklinux/minemenufabric/client/MineMenuFabricClient.java @@ -4,7 +4,9 @@ import me.shedaniel.autoconfig.AutoConfig; import me.ultrablacklinux.minemenufabric.client.config.Config; import me.ultrablacklinux.minemenufabric.client.screen.MineMenuSelectScreen; +import me.ultrablacklinux.minemenufabric.client.screen.MineMenuSettingsScreen; import me.ultrablacklinux.minemenufabric.client.util.GsonUtil; +import me.ultrablacklinux.minemenufabric.client.util.RandomUtil; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -13,6 +15,7 @@ import net.minecraft.client.options.KeyBinding; import net.minecraft.client.util.InputUtil; import net.minecraft.item.ItemStack; +import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import org.lwjgl.glfw.GLFW; import java.util.ArrayList; @@ -24,8 +27,7 @@ public class MineMenuFabricClient implements ClientModInitializer { public static KeyBinding keyBinding; public static JsonObject minemenuData; public static ArrayList datapath; - public static HashMap playerHeadData = new HashMap<>(); - private static boolean buttonPressed; + public static HashMap playerHeadCache = new HashMap<>(); @Override public void onInitializeClient() { @@ -42,15 +44,27 @@ public void onInitializeClient() { minemenuData = new JsonObject(); AutoConfig.getConfigHolder(Config.class).save(); } + + if (Config.get().minemenuFabric.resetHeadCache) { + Config.get().minemenuFabric.resetHeadCache = false; + playerHeadCache.clear(); + } + if (mineMenuSelectScreen != null) this.mineMenuSelectScreen.tick(); }); ClientTickEvents.END_CLIENT_TICK.register(client -> { if (keyBinding.wasPressed()) { if (!(client.currentScreen instanceof MineMenuSelectScreen)) { - minemenuData = GsonUtil.fixEntryAmount(minemenuData); - client.openScreen(new MineMenuSelectScreen(minemenuData, - new TranslatableText("minemenu.default.title").getString(), null)); + minemenuData = GsonUtil.fixEntryAmount(minemenuData); //it is not already assigned dumb intelli + try { + client.openScreen(new MineMenuSelectScreen(MineMenuFabricClient.minemenuData, + new TranslatableText("minemenu.default.title").getString(), null)); + } catch (NullPointerException e) { + client.openScreen(null); + client.player.sendMessage(new TranslatableText("minemenu.error.config"), false); + } + } } }); diff --git a/src/main/java/me/ultrablacklinux/minemenufabric/client/config/Config.java b/src/main/java/me/ultrablacklinux/minemenufabric/client/config/Config.java index aac96d2..9449bd4 100644 --- a/src/main/java/me/ultrablacklinux/minemenufabric/client/config/Config.java +++ b/src/main/java/me/ultrablacklinux/minemenufabric/client/config/Config.java @@ -6,6 +6,9 @@ import me.shedaniel.autoconfig.annotation.ConfigEntry; import me.shedaniel.autoconfig.serializer.GsonConfigSerializer; import me.shedaniel.autoconfig.serializer.PartitioningSerializer; +import net.minecraft.item.ItemStack; + +import java.util.HashMap; @me.shedaniel.autoconfig.annotation.Config(name = "MinemenuFabric") @me.shedaniel.autoconfig.annotation.Config.Gui.Background("minecraft:textures/block/gray_concrete.png") @@ -50,9 +53,9 @@ public static class MinemenuFabric implements ConfigData { public String emptyItemIcon = "minecraft:air"; @ConfigEntry.Gui.Tooltip - public boolean resetConfig = false; + public boolean resetHeadCache = false; + @ConfigEntry.Gui.Tooltip + public boolean resetConfig = false; } - - } diff --git a/src/main/java/me/ultrablacklinux/minemenufabric/client/screen/MineMenuSelectScreen.java b/src/main/java/me/ultrablacklinux/minemenufabric/client/screen/MineMenuSelectScreen.java index 8b5a7f8..327298f 100644 --- a/src/main/java/me/ultrablacklinux/minemenufabric/client/screen/MineMenuSelectScreen.java +++ b/src/main/java/me/ultrablacklinux/minemenufabric/client/screen/MineMenuSelectScreen.java @@ -98,14 +98,15 @@ private void renderGui(MatrixStack matrixStack, int mouseX, int mouseY) { JsonObject iconData = value.get("icon").getAsJsonObject(); this.skullowner = iconData.get("skullOwner").getAsString(); ItemStack i; - if (MineMenuFabricClient.playerHeadData.containsKey(iconData.get("skullOwner").getAsString()) && !iconData.get("skullOwner").getAsString().isEmpty()) { - client.getItemRenderer().renderInGui(playerHeadData.get(iconData.get("skullOwner").getAsString()), drawX, drawY); + if (MineMenuFabricClient.playerHeadCache.containsKey(iconData.get("skullOwner").getAsString()) && + !iconData.get("skullOwner").getAsString().trim().isEmpty()) { + client.getItemRenderer().renderInGui(playerHeadCache.get(iconData.get("skullOwner").getAsString()), drawX, drawY); } else { - i = RandomUtil.iconify(this::setSkullMap, iconData.get("iconItem").getAsString(), + i = RandomUtil.iconify(iconData.get("iconItem").getAsString(), iconData.get("enchanted").getAsBoolean(), iconData.get("skullOwner").getAsString()); if (i == null) try { - client.getItemRenderer().renderInGui(playerHeadData.get(iconData.get("skullOwner").getAsString()), drawX, drawY); + client.getItemRenderer().renderInGui(playerHeadCache.get(iconData.get("skullOwner").getAsString()), drawX, drawY); } catch (Exception e) {} else client.getItemRenderer().renderInGui(i, drawX, drawY); } @@ -153,22 +154,17 @@ private void renderGui(MatrixStack matrixStack, int mouseX, int mouseY) { } } - private void setSkullMap(ItemStack itemStack) { - MineMenuFabricClient.playerHeadData.put(skullowner, itemStack); - } - @Override public void tick() { - if (true){ //keybinding mode - hold or pressed - if (keyBinding.wasPressed()) { //check for keybinding pressed - final double mouseX = this.client.mouse.getX() * ((double) this.client.getWindow().getScaledWidth() / - this.client.getWindow().getWidth()); - final double mouseY = this.client.mouse.getY() * ((double) this.client.getWindow().getScaledHeight() / - this.client.getWindow().getHeight()); - - this.mouseClicked(mouseX, mouseY, 0); - } + if (keyBinding.wasPressed()) { + final double mouseX = this.client.mouse.getX() * ((double) this.client.getWindow().getScaledWidth() / + this.client.getWindow().getWidth()); + final double mouseY = this.client.mouse.getY() * ((double) this.client.getWindow().getScaledHeight() / + this.client.getWindow().getHeight()); + + //this.mouseClicked(mouseX, mouseY, 0); } + } @Override @@ -200,7 +196,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) { switch (value.get("type").getAsString()) { case "empty": dontClose = true; - this.openConfigScreen(); + RandomUtil.openConfigScreen(this); break; case "print": @@ -220,7 +216,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) { } } else if (button == 1) { - this.openConfigScreen(); + RandomUtil.openConfigScreen(this); } } @@ -234,15 +230,6 @@ else if (button == 1) { return false; } - private void openConfigScreen() { - try { - client.openScreen(new MineMenuSettingsScreen(this, datapath)); - } catch (NullPointerException e) { - client.openScreen(null); - client.player.sendMessage(Text.of("§l§c Corrupt config! Reset it via the config menu!"), false); - } - } - @Override public boolean isPauseScreen() { return false; diff --git a/src/main/java/me/ultrablacklinux/minemenufabric/client/screen/MineMenuSettingsScreen.java b/src/main/java/me/ultrablacklinux/minemenufabric/client/screen/MineMenuSettingsScreen.java index 604c28e..a4909a6 100644 --- a/src/main/java/me/ultrablacklinux/minemenufabric/client/screen/MineMenuSettingsScreen.java +++ b/src/main/java/me/ultrablacklinux/minemenufabric/client/screen/MineMenuSettingsScreen.java @@ -17,14 +17,13 @@ import net.minecraft.client.gui.widget.TextFieldWidget; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import java.util.ArrayList; import static me.ultrablacklinux.minemenufabric.client.MineMenuFabricClient.*; -import static me.ultrablacklinux.minemenufabric.client.MineMenuFabricClient.playerHeadData; +import static me.ultrablacklinux.minemenufabric.client.config.Config.*; public class MineMenuSettingsScreen extends Screen { private TypeCycle typeCycle; @@ -105,6 +104,8 @@ protected void init() { this.itemData = new TextFieldWidget(this.textRenderer, this.width / 2 - 100, 141, 200, 20, //TEXT INPUT new TranslatableText("minemenu.settings.data")); + + if (typeCycle == TypeCycle.PRINT || typeCycle == TypeCycle.CLIPBOARD) { this.itemData.setText(data.get("data").getAsJsonObject().get("message").getAsString()); } @@ -120,7 +121,7 @@ else if (typeCycle == TypeCycle.LINK) { typeCycle = typeCycle.next(); this.type.setMessage(typeCycle.getName()); this.updateStates(); - this.updateStates(); + this.updateIconInput(); })); //---------------------------- @@ -131,7 +132,6 @@ else if (typeCycle == TypeCycle.LINK) { this.done = this.addButton(new ButtonWidget(this.width / 2, 220, 100, 20, ScreenTexts.DONE, (buttonWidget) -> close(false))); - this.updateStates(); this.updateStates(); this.updateIconInput(); } @@ -155,15 +155,13 @@ private void saveIconVariable() { private void updateIconInput() { this.iconDataYesNo.active = itemConfigCycle == ItemConfigCycle.ENCHANTED; - this.iconDataText.setEditable(itemConfigCycle != ItemConfigCycle.ENCHANTED); + this.iconDataText.setEditable(itemConfigCycle != ItemConfigCycle.ENCHANTED && typeCycle != TypeCycle.EMPTY); + this.itemSettingType.active = typeCycle != TypeCycle.EMPTY; if (itemConfigCycle != ItemConfigCycle.ENCHANTED) this.iconDataYesNo.setMessage(Text.of("")); - if (itemConfigCycle == ItemConfigCycle.SKULLOWNER) this.iconDataText.setEditable(iconItem.endsWith("player_head")); + if (itemConfigCycle == ItemConfigCycle.SKULLOWNER) this.iconItem = "player_head"; + this.iconDataYesNo.setMessage(iconDataBoolean ? ScreenTexts.YES : ScreenTexts.NO); switch (itemConfigCycle) { - case ENCHANTED: - this.iconDataYesNo.setMessage(enchanted ? ScreenTexts.YES : ScreenTexts.NO); - break; - case ICON: this.iconDataText.setText(this.iconItem); break; @@ -176,6 +174,7 @@ private void updateIconInput() { private void updateStates() { this.itemName.setEditable(typeCycle != TypeCycle.EMPTY); + this.itemData.setEditable(typeCycle == TypeCycle.PRINT || typeCycle == TypeCycle.LINK || typeCycle == TypeCycle.CLIPBOARD); this.done.active = typeCycle == TypeCycle.EMPTY || !this.itemName.getText().isEmpty(); } @@ -211,25 +210,20 @@ public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { this.itemData.render(matrices, mouseX, mouseY, delta); ItemStack i; - if (MineMenuFabricClient.playerHeadData.containsKey(skullowner)) { - client.getItemRenderer().renderInGui(playerHeadData.get(skullowner), this.width / 2 - 120, 82); + if (playerHeadCache.containsKey(skullowner) && !skullowner.trim().isEmpty()) { + client.getItemRenderer().renderInGui(playerHeadCache.get(skullowner), this.width / 2 - 120, 82); } else { - i = RandomUtil.iconify(this::setSkullMap, iconItem, enchanted, skullowner); + i = RandomUtil.iconify(iconItem, enchanted, skullowner); if (i == null) try { - client.getItemRenderer().renderInGui(MineMenuFabricClient.playerHeadData.get(skullowner), this.width / 2 - 120, 82); + client.getItemRenderer().renderInGui(playerHeadCache.get(skullowner), this.width / 2 - 120, 82); } catch (Exception e) {} else client.getItemRenderer().renderInGui(i, this.width / 2 - 120, 82); } - super.render(matrices, mouseX, mouseY, delta); } - private void setSkullMap(ItemStack itemStack) { - MineMenuFabricClient.playerHeadData.put(skullowner, itemStack); - } - private void close(boolean cancel) { if (!cancel) applySettings(); @@ -267,7 +261,7 @@ private void applySettings() { } GsonUtil.saveJson(GsonUtil.template(name, typeCycle.name().toLowerCase(), subData, iconItem, enchanted, skullowner)); - Config.get().minemenuFabric.minemenuData = minemenuData; + get().minemenuFabric.minemenuData = minemenuData; AutoConfig.getConfigHolder(Config.class).save(); } } diff --git a/src/main/java/me/ultrablacklinux/minemenufabric/client/util/RandomUtil.java b/src/main/java/me/ultrablacklinux/minemenufabric/client/util/RandomUtil.java index 01ba059..1c64b9f 100644 --- a/src/main/java/me/ultrablacklinux/minemenufabric/client/util/RandomUtil.java +++ b/src/main/java/me/ultrablacklinux/minemenufabric/client/util/RandomUtil.java @@ -1,9 +1,16 @@ package me.ultrablacklinux.minemenufabric.client.util; import com.mojang.authlib.GameProfile; +import me.shedaniel.autoconfig.AutoConfig; import me.shedaniel.math.Color; +import me.ultrablacklinux.minemenufabric.client.MineMenuFabricClient; +import me.ultrablacklinux.minemenufabric.client.config.Config; +import me.ultrablacklinux.minemenufabric.client.screen.MineMenuSelectScreen; +import me.ultrablacklinux.minemenufabric.client.screen.MineMenuSettingsScreen; import net.minecraft.block.AbstractSkullBlock; import net.minecraft.block.entity.SkullBlockEntity; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.Screen; import net.minecraft.enchantment.Enchantment; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.item.BlockItem; @@ -11,13 +18,18 @@ import net.minecraft.item.Items; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtHelper; +import net.minecraft.text.Text; +import net.minecraft.text.TranslatableText; import net.minecraft.util.Identifier; import net.minecraft.util.InvalidIdentifierException; import net.minecraft.util.registry.Registry; + +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.UUID; -import java.util.function.Consumer; + +import static me.ultrablacklinux.minemenufabric.client.MineMenuFabricClient.datapath; public class RandomUtil { public static me.shedaniel.math.Color getColor(String inp) { @@ -29,7 +41,7 @@ public static me.shedaniel.math.Color getColor(String inp) { return Color.ofRGBA(f, f1, f2, f3); } - public static ItemStack iconify(Consumer consumer, String iconItem, boolean enchanted, String skullowner) { + public static ItemStack iconify(String iconItem, boolean enchanted, String skullowner) { ItemStack out; try { out = itemStackFromString(iconItem); @@ -47,7 +59,9 @@ public static ItemStack iconify(Consumer consumer, String iconItem, b GameProfile gameProfile = new GameProfile((UUID)null, skullowner); gameProfile = SkullBlockEntity.loadProperties(gameProfile); tag.put("SkullOwner", NbtHelper.fromGameProfile(new CompoundTag(), gameProfile)); - consumer.accept(finalOut); + MineMenuFabricClient.playerHeadCache.putIfAbsent(skullowner, finalOut); + //Config.get().minemenuFabric.playerHeadCache = MineMenuFabricClient.playerHeadCache; + //AutoConfig.getConfigHolder(Config.class).save(); }); nbTater.start(); out = null; @@ -69,4 +83,15 @@ public static boolean isSkullItem(ItemStack stack) { return stack.getItem() instanceof BlockItem && ((net.minecraft.item.BlockItem) stack.getItem()).getBlock() instanceof AbstractSkullBlock; } + + + public static void openConfigScreen(Screen parent) { + MinecraftClient client = MinecraftClient.getInstance(); + try { + client.openScreen(new MineMenuSettingsScreen(parent, datapath)); + } catch (NullPointerException e) { + client.openScreen(null); + client.player.sendMessage(new TranslatableText("minemenu.error.config"), false); + } + } } diff --git a/src/main/resources/assets/minemenufabric/lang/de_de.json b/src/main/resources/assets/minemenufabric/lang/de_de.json index e13af5a..f7c4a32 100644 --- a/src/main/resources/assets/minemenufabric/lang/de_de.json +++ b/src/main/resources/assets/minemenufabric/lang/de_de.json @@ -17,6 +17,8 @@ "text.autoconfig.MinemenuFabric.option.minemenuFabric.secondaryColor.@Tooltip": "Hex Farbe - Unterstützt \"#\", \"0x\" und mehr", "text.autoconfig.MinemenuFabric.option.minemenuFabric.emptyItemIcon": "Leeres Icon", "text.autoconfig.MinemenuFabric.option.minemenuFabric.emptyItemIcon.@Tooltip": "Icon für leere Einträge", + "text.autoconfig.MinemenuFabric.option.minemenuFabric.resetHeadCache": "Spielerkopf-cache leeren", + "text.autoconfig.MinemenuFabric.option.minemenuFabric.resetHeadCache.@Tooltip": "Der Spielerkopf-cache wird geleert sobald du Fertig drückst", "text.autoconfig.MinemenuFabric.option.minemenuFabric.resetConfig": "Config zurücksetzen", "text.autoconfig.MinemenuFabric.option.minemenuFabric.resetConfig.@Tooltip": "Die Config wird zurückgesetzt sobald du Fertig drückst", "minemenu.setting.itemconfigcycle.Icon": "Icon", @@ -29,5 +31,6 @@ "minemenu.setting.typecycle.Empty": "Platzhalter", "minemenu.setting.input.name": "Name:", "minemenu.setting.input.icon": "Icon:", - "minemenu.setting.input.text": "Text:" + "minemenu.setting.input.text": "Text:", + "minemenu.error.config": "§l§c Unbrauchbare Config! Setze sie über das Konfigurationsmenü zurück! \n(Dies kann nach einem Update passieren)" } diff --git a/src/main/resources/assets/minemenufabric/lang/en_us.json b/src/main/resources/assets/minemenufabric/lang/en_us.json index 5e82a1b..a399d43 100644 --- a/src/main/resources/assets/minemenufabric/lang/en_us.json +++ b/src/main/resources/assets/minemenufabric/lang/en_us.json @@ -17,6 +17,8 @@ "text.autoconfig.MinemenuFabric.option.minemenuFabric.secondaryColor.@Tooltip": "Hex color - Supports \"#\", \"0x\" and more", "text.autoconfig.MinemenuFabric.option.minemenuFabric.emptyItemIcon": "Empty Icon", "text.autoconfig.MinemenuFabric.option.minemenuFabric.emptyItemIcon.@Tooltip": "Icon for empty menu entries", + "text.autoconfig.MinemenuFabric.option.minemenuFabric.resetHeadCache": "Clear the PlayerHead-cache", + "text.autoconfig.MinemenuFabric.option.minemenuFabric.resetHeadCache.@Tooltip": "The PlayerHead-cache will Clear, once you press done", "text.autoconfig.MinemenuFabric.option.minemenuFabric.resetConfig": "Reset the Config", "text.autoconfig.MinemenuFabric.option.minemenuFabric.resetConfig.@Tooltip": "The Config will reset, once you press done", "minemenu.setting.itemconfigcycle.Icon": "Icon", @@ -29,5 +31,6 @@ "minemenu.setting.typecycle.Empty": "Placeholder", "minemenu.setting.input.name": "Name:", "minemenu.setting.input.icon": "Icon:", - "minemenu.setting.input.text": "Text:" + "minemenu.setting.input.text": "Text:", + "minemenu.error.config": "§l§c Invalid config! Reset it via the config menu! \n(This may happen after an update)" } \ No newline at end of file