diff options
author | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2023-09-12 20:16:10 -0400 |
---|---|---|
committer | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2023-09-12 20:16:10 -0400 |
commit | e4757b16aca9f67621a5beaad3a55aeb15401d9c (patch) | |
tree | ae23b09ef49fd3b373b72d68ead676f6a4516c42 | |
parent | 942d3a47c6fea5c5dcd2db8e2e30ada628afbc9f (diff) | |
download | Skyblocker-e4757b16aca9f67621a5beaad3a55aeb15401d9c.tar.gz Skyblocker-e4757b16aca9f67621a5beaad3a55aeb15401d9c.tar.bz2 Skyblocker-e4757b16aca9f67621a5beaad3a55aeb15401d9c.zip |
Refactor DungeonChestProfit
5 files changed, 76 insertions, 79 deletions
diff --git a/src/main/java/me/xmrvizzy/skyblocker/SkyblockerMod.java b/src/main/java/me/xmrvizzy/skyblocker/SkyblockerMod.java index cdf78d73..bb08b99a 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/SkyblockerMod.java +++ b/src/main/java/me/xmrvizzy/skyblocker/SkyblockerMod.java @@ -4,10 +4,7 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import me.xmrvizzy.skyblocker.config.SkyblockerConfig; import me.xmrvizzy.skyblocker.skyblock.*; -import me.xmrvizzy.skyblocker.skyblock.dungeon.DungeonBlaze; -import me.xmrvizzy.skyblocker.skyblock.dungeon.DungeonMap; -import me.xmrvizzy.skyblocker.skyblock.dungeon.LividColor; -import me.xmrvizzy.skyblocker.skyblock.dungeon.TicTacToe; +import me.xmrvizzy.skyblocker.skyblock.dungeon.*; import me.xmrvizzy.skyblocker.skyblock.dungeon.secrets.DungeonSecrets; import me.xmrvizzy.skyblocker.skyblock.dwarven.DwarvenHud; import me.xmrvizzy.skyblocker.skyblock.item.*; @@ -92,6 +89,7 @@ public class SkyblockerMod implements ClientModInitializer { DungeonMap.init(); DungeonSecrets.init(); DungeonBlaze.init(); + DungeonChestProfit.init(); TheRift.init(); TitleContainer.init(); ScreenMaster.init(); diff --git a/src/main/java/me/xmrvizzy/skyblocker/mixin/HandledScreenMixin.java b/src/main/java/me/xmrvizzy/skyblocker/mixin/HandledScreenMixin.java index 916fab03..eccd63e6 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/mixin/HandledScreenMixin.java +++ b/src/main/java/me/xmrvizzy/skyblocker/mixin/HandledScreenMixin.java @@ -110,17 +110,4 @@ public abstract class HandledScreenMixin extends Screen { } } } - - @WrapOperation(method = "drawForeground", at = @At(value = "FIELD", target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;title:Lnet/minecraft/text/Text;", opcode = Opcodes.GETFIELD)) - private Text skyblocker$modifyScreenTitle(HandledScreen<?> handledScreen, Operation<Text> operation) { - Text title = handledScreen.getTitle(); - - if (Utils.isOnSkyblock() && handledScreen.getScreenHandler().getType().equals(ScreenHandlerType.GENERIC_9X6)) { - GenericContainerScreenHandler gcsHandler = (GenericContainerScreenHandler) handledScreen.getScreenHandler(); - - return DungeonChestProfit.getChestProfit(gcsHandler, title, this.client); - } - - return title; - } } diff --git a/src/main/java/me/xmrvizzy/skyblocker/mixin/accessor/ScreenAccessor.java b/src/main/java/me/xmrvizzy/skyblocker/mixin/accessor/ScreenAccessor.java new file mode 100644 index 00000000..6a671601 --- /dev/null +++ b/src/main/java/me/xmrvizzy/skyblocker/mixin/accessor/ScreenAccessor.java @@ -0,0 +1,14 @@ +package me.xmrvizzy.skyblocker.mixin.accessor; + +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.Text; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(Screen.class) +public interface ScreenAccessor { + @Accessor + @Mutable + void setTitle(Text title); +} diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/dungeon/DungeonChestProfit.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/dungeon/DungeonChestProfit.java index 84d88ecd..beed0cd8 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/dungeon/DungeonChestProfit.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/dungeon/DungeonChestProfit.java @@ -1,28 +1,30 @@ package me.xmrvizzy.skyblocker.skyblock.dungeon; -import java.text.DecimalFormat; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.google.gson.JsonObject; - import it.unimi.dsi.fastutil.ints.IntBooleanPair; import me.xmrvizzy.skyblocker.config.SkyblockerConfig; +import me.xmrvizzy.skyblocker.mixin.accessor.ScreenAccessor; import me.xmrvizzy.skyblocker.skyblock.item.PriceInfoTooltip; +import me.xmrvizzy.skyblocker.utils.Utils; +import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.ingame.GenericContainerScreen; import net.minecraft.client.item.TooltipContext; import net.minecraft.item.ItemStack; import net.minecraft.screen.GenericContainerScreenHandler; +import net.minecraft.screen.ScreenHandlerType; import net.minecraft.screen.slot.Slot; -import net.minecraft.text.MutableText; import net.minecraft.text.Style; import net.minecraft.text.Text; import net.minecraft.util.Formatting; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.text.DecimalFormat; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class DungeonChestProfit { private static final Logger LOGGER = LoggerFactory.getLogger(DungeonChestProfit.class); @@ -32,139 +34,134 @@ public class DungeonChestProfit { private static final Style DARK_BLUE = Style.EMPTY.withColor(Formatting.DARK_BLUE); private static final Style DARK_GREEN = Style.EMPTY.withColor(Formatting.DARK_GREEN); private static final Style DARK_RED = Style.EMPTY.withColor(Formatting.DARK_RED); - + + public static void init() { + ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> ScreenEvents.afterTick(screen).register(screen1 -> { + if (Utils.isOnSkyblock() && screen instanceof GenericContainerScreen genericContainerScreen && genericContainerScreen.getScreenHandler().getType() == ScreenHandlerType.GENERIC_9X6) { + ((ScreenAccessor) screen).setTitle(getChestProfit(genericContainerScreen.getScreenHandler(), screen.getTitle(), client)); + } + })); + } + public static Text getChestProfit(GenericContainerScreenHandler handler, Text title, MinecraftClient client) { try { - if (SkyblockerConfig.get().locations.dungeons.dungeonChestProfit.enableProfitCalculator && isDungeonChest(title.getString())) { + if (SkyblockerConfig.get().locations.dungeons.dungeonChestProfit.enableProfitCalculator && isDungeonChest(title.getString())) { int profit = 0; boolean hasIncompleteData = false, usedKismet = false; List<Slot> slots = handler.slots.subList(0, handler.getRows() * 9); - + //If the item stack for the "Open Reward Chest" button or the kismet button hasn't been sent to the client yet if (slots.get(31).getStack().isEmpty() || slots.get(50).getStack().isEmpty()) return title; - + for (Slot slot : slots) { ItemStack stack = slot.getStack(); - + if (!stack.isEmpty()) { String name = stack.getName().getString(); String id = PriceInfoTooltip.getInternalNameFromNBT(stack, false); - + //Regular item price - if (id != null) { + if (id != null) { IntBooleanPair priceData = getItemPrice(id); - - if (priceData.rightBoolean() == false) hasIncompleteData = true; - + + if (!priceData.rightBoolean()) hasIncompleteData = true; + //Add the item price to the profit profit += priceData.leftInt(); continue; } - + //Essence price if (name.contains("Essence") && SkyblockerConfig.get().locations.dungeons.dungeonChestProfit.includeEssence) { Matcher matcher = ESSENCE_PATTERN.matcher(name); - + if (matcher.matches()) { String type = matcher.group("type"); int amount = Integer.parseInt(matcher.group("amount")); - + IntBooleanPair priceData = getItemPrice(("ESSENCE_" + type).toUpperCase()); - - if (priceData.rightBoolean() == false) hasIncompleteData = true; - + + if (!priceData.rightBoolean()) hasIncompleteData = true; + //Add the price of the essence to the profit profit += priceData.leftInt() * amount; - + continue; } } - + //Determine the cost of the chest if (name.contains("Open Reward Chest")) { String foundString = searchLoreFor(stack, client, "Coins"); - + //Incase we're searching the free chest if (!StringUtils.isBlank(foundString)) { profit -= Integer.parseInt(foundString.replaceAll("[^0-9]", "")); } - + continue; } - + //Determine if a kismet was used or not if (name.contains("Reroll Chest")) { usedKismet = !StringUtils.isBlank(searchLoreFor(stack, client, "You already rerolled a chest!")); - - continue; } } } - + if (SkyblockerConfig.get().locations.dungeons.dungeonChestProfit.includeKismet && usedKismet) { IntBooleanPair kismetPriceData = getItemPrice("KISMET_FEATHER"); - - if (kismetPriceData.rightBoolean() == false) hasIncompleteData = true; - + + if (!kismetPriceData.rightBoolean()) hasIncompleteData = true; + profit -= kismetPriceData.leftInt(); } - - return ((MutableText) Text.literal(title.getString())).append(getProfitText(profit, hasIncompleteData)); + + return Text.literal(title.getString()).append(getProfitText(profit, hasIncompleteData)); } } catch (Exception e) { LOGGER.error("[Skyblocker Profit Calculator] Failed to calculate dungeon chest profit! ", e); } - + return title; } - + /** - * * @return An {@link IntBooleanPair} with the {@code left int} representing the item's price, and the {@code right boolean} indicating if the price * was based on complete data. */ private static IntBooleanPair getItemPrice(String id) { JsonObject bazaarPrices = PriceInfoTooltip.getBazaarPrices(); JsonObject lbinPrices = PriceInfoTooltip.getLBINPrices(); - + if (bazaarPrices == null || lbinPrices == null) return IntBooleanPair.of(0, false); - + if (bazaarPrices.has(id)) { JsonObject item = bazaarPrices.get(id).getAsJsonObject(); boolean isPriceNull = item.get("sellPrice").isJsonNull(); - + return IntBooleanPair.of(isPriceNull ? 0 : (int) item.get("sellPrice").getAsDouble(), !isPriceNull); } - + if (lbinPrices.has(id)) { return IntBooleanPair.of((int) lbinPrices.get(id).getAsDouble(), true); } - + return IntBooleanPair.of(0, false); } - + /** * Searches for a specific string of characters in the name and lore of an item */ private static String searchLoreFor(ItemStack stack, MinecraftClient client, String searchString) { - List<Text> lore = stack.getTooltip(client.player, TooltipContext.BASIC); - - for (int i = 0; i < lore.size(); i++) { - String line = lore.get(i).getString(); - - if (line.contains(searchString)) { - return line; - } - } - - return null; + return stack.getTooltip(client.player, TooltipContext.BASIC).stream().map(Text::getString).filter(line -> line.contains(searchString)).findAny().orElse(null); } - + private static boolean isDungeonChest(String name) { return name.equals("Wood Chest") || name.equals("Gold Chest") || name.equals("Diamond Chest") || name.equals("Emerald Chest") || name.equals("Obsidian Chest") || name.equals("Bedrock Chest"); } - + private static Text getProfitText(int profit, boolean hasIncompleteData) { return (profit == 0) ? Text.literal(" " + FORMATTER.format(profit)).setStyle(hasIncompleteData ? DARK_BLUE : GRAY) : (profit > 0) ? Text.literal(" +" + FORMATTER.format(profit)).setStyle(hasIncompleteData ? DARK_BLUE : DARK_GREEN) : Text.literal(" " + FORMATTER.format(profit)).setStyle(hasIncompleteData ? DARK_BLUE : DARK_RED); } diff --git a/src/main/resources/skyblocker.mixins.json b/src/main/resources/skyblocker.mixins.json index 98759aac..9d2ce7c2 100644 --- a/src/main/resources/skyblocker.mixins.json +++ b/src/main/resources/skyblocker.mixins.json @@ -28,6 +28,7 @@ "accessor.HandledScreenAccessor", "accessor.PlayerListHudAccessor", "accessor.RecipeBookWidgetAccessor", + "accessor.ScreenAccessor", "accessor.WorldRendererAccessor" ], "injectors": { |