From 07217c6f5c0fac8738408f940306773af0f4e589 Mon Sep 17 00:00:00 2001 From: VeritasDL <121319869+VeritasDL@users.noreply.github.com> Date: Sat, 6 Apr 2024 07:06:49 -0400 Subject: Update ItemCooldowns.java add legendary monkey pet wood cutting cooldown attempt --- .../skyblocker/skyblock/item/ItemCooldowns.java | 93 +++++++++++++++++++--- 1 file changed, 80 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java index b28e5da1..2c76cfcf 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java @@ -1,8 +1,14 @@ package de.hysky.skyblocker.skyblock.item; - +import com.google.gson.JsonParser; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import com.google.common.collect.ImmutableList; import de.hysky.skyblocker.config.SkyblockerConfigManager; +import de.hysky.skyblocker.events.SkyblockEvents; +import de.hysky.skyblocker.utils.ApiUtils; +import de.hysky.skyblocker.utils.Http; import de.hysky.skyblocker.utils.ItemUtils; +import de.hysky.skyblocker.utils.Utils; import net.fabricmc.fabric.api.event.client.player.ClientPlayerBlockBreakEvents; import net.fabricmc.fabric.api.event.player.UseItemCallback; import net.minecraft.block.BlockState; @@ -13,10 +19,14 @@ import net.minecraft.util.Hand; import net.minecraft.util.TypedActionResult; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; - import java.util.HashMap; import java.util.Map; - +import com.mojang.util.UndashedUuid; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.session.Session; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.stream.StreamSupport; public class ItemCooldowns { private static final String JUNGLE_AXE_ID = "JUNGLE_AXE"; private static final String TREECAPITATOR_ID = "TREECAPITATOR_AXE"; @@ -24,6 +34,22 @@ public class ItemCooldowns { private static final ImmutableList BAT_ARMOR_IDS = ImmutableList.of("BAT_PERSON_HELMET", "BAT_PERSON_CHESTPLATE", "BAT_PERSON_LEGGINGS", "BAT_PERSON_BOOTS"); private static final Map ITEM_COOLDOWNS = new HashMap<>(); + private static final int[] EXPERIENCE_LEVELS = { + 0, 660, 730, 800, 880, 960, 1050, 1150, 1260, 1380, 1510, 1650, 1800, 1960, 2130, + 2310, 2500, 2700, 2920, 3160, 3420, 3700, 4000, 4350, 4750, 5200, 5700, 6300, 7000, + 7800, 8700, 9700, 10800, 12000, 13300, 14700, 16200, 17800, 19500, 21300, 23200, + 25200, 27400, 29800, 32400, 35200, 38200, 41400, 44800, 48400, 52200, 56200, 60400, + 64800, 69400, 74200, 79200, 84700, 90700, 97200, 104200, 111700, 119700, 128200, + 137200, 147700, 156700, 167700, 179700, 192700, 206700, 221700, 237700, 254700, + 272700, 291700, 311700, 333700, 357700, 383700, 411700, 441700, 476700, 516700, + 561700, 611700, 666700, 726700, 791700, 861700, 936700, 1016700, 1101700, 1191700, + 1286700, 1386700, 1496700, 1616700, 1746700, 1886700 + }; + + public static String MonkeyExp = ""; + public static int MonkeyLevel = 1; + public static double CalcMonkeyExp = 0; + public static int CalcMonkeyLevel = 1; public static void init() { ClientPlayerBlockBreakEvents.AFTER.register(ItemCooldowns::afterBlockBreak); @@ -32,23 +58,64 @@ public class ItemCooldowns { public static void afterBlockBreak(World world, PlayerEntity player, BlockPos pos, BlockState state) { if (!SkyblockerConfigManager.get().general.itemCooldown.enableItemCooldowns) return; - + String name = MinecraftClient.getInstance().getSession().getUsername(); + String playeruuid = ApiUtils.name2Uuid(name); + try (Http.ApiResponse response = Http.sendHypixelRequest("skyblock/profiles", "?uuid=" + playeruuid)) { + if (!response.ok()) + throw new IllegalStateException("Failed to get profile uuid for player " + name + "! Response: " + response.content()); + JsonObject responseJson = JsonParser.parseString(response.content()).getAsJsonObject(); + + JsonObject players = StreamSupport.stream(responseJson.getAsJsonArray("profiles").spliterator(), false) + .map(JsonElement::getAsJsonObject) + .filter(profile -> profile.getAsJsonPrimitive("selected").getAsBoolean()) + .findFirst() + .orElseThrow(() -> new IllegalStateException("No selected profile found!?")) + .getAsJsonObject("members").entrySet().stream() + .filter(entry -> entry.getKey().equals(playeruuid)) + .map(Map.Entry::getValue) + .map(JsonElement::getAsJsonObject) + .findFirst() + .orElseThrow(() -> new IllegalStateException("Player somehow not found inside their own profile!")); + + for (JsonElement element : players.getAsJsonObject("pets_data").getAsJsonArray("pets")) { + if (!element.getAsJsonObject().get("type").getAsString().equals("MONKEY")) continue; + if (!element.getAsJsonObject().get("active").getAsString().equals("true")) continue; + if (element.getAsJsonObject().get("tier").getAsString().equals("LEGENDARY")) { + CalcMonkeyExp = Double.parseDouble(element.getAsJsonObject().get("exp").getAsString()); + // = Double.parseDouble(MonkeyExp); + MonkeyLevel = 0; + //CalcMonkeyExp = 16000000; + for (int xpLevel : EXPERIENCE_LEVELS) { + if (CalcMonkeyExp < xpLevel) { + break; + } else { + CalcMonkeyExp -= xpLevel; + MonkeyLevel++; + } + } + //CalcMonkeyLevel = MonkeyLevel; + //} + } + } + } catch (Exception e) { + System.out.println("Pet Level Error or something Idk"); + //LOGGER.error("[Skyblocker] Spirit pet lookup by name failed! Name: {}", name, e); + } + double BaseCooldown = 2000; String usedItemId = ItemUtils.getItemId(player.getMainHandStack()); if (usedItemId.isEmpty()) return; - if (state.isIn(BlockTags.LOGS)) { - if (usedItemId.equals(JUNGLE_AXE_ID)) { - if (!isOnCooldown(JUNGLE_AXE_ID)) { - ITEM_COOLDOWNS.put(JUNGLE_AXE_ID, new CooldownEntry(2000)); - } - } else if (usedItemId.equals(TREECAPITATOR_ID)) { - if (!isOnCooldown(TREECAPITATOR_ID)) { - ITEM_COOLDOWNS.put(TREECAPITATOR_ID, new CooldownEntry(2000)); + double EvolvedAxesCooldownReductionPercentage = MonkeyLevel * 0.5; + //double ProcessedCooldown = EvolvedAxesCooldownReductionPercentage * 10000; + double MonkeyPetCDRReduction = (BaseCooldown * EvolvedAxesCooldownReductionPercentage) / 100; + double currentCooldown = BaseCooldown - MonkeyPetCDRReduction; + if (usedItemId.equals(JUNGLE_AXE_ID) || usedItemId.equals(TREECAPITATOR_ID)) { + if (!isOnCooldown(JUNGLE_AXE_ID) || !isOnCooldown(TREECAPITATOR_ID)) { + ITEM_COOLDOWNS.put(usedItemId, new CooldownEntry((int)currentCooldown)); } } } } - private static TypedActionResult onItemInteract(PlayerEntity player, World world, Hand hand) { if (!SkyblockerConfigManager.get().general.itemCooldown.enableItemCooldowns) return TypedActionResult.pass(ItemStack.EMPTY); -- cgit From 28272732df33e6154557e6622a659c6f4023c443 Mon Sep 17 00:00:00 2001 From: VeritasDL <121319869+VeritasDL@users.noreply.github.com> Date: Sat, 6 Apr 2024 17:05:14 -0400 Subject: Update ItemCooldowns.java MonkeyPet treecapitator cooldown --- src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src') diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java index 2c76cfcf..f1dadf3b 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java @@ -82,9 +82,7 @@ public class ItemCooldowns { if (!element.getAsJsonObject().get("active").getAsString().equals("true")) continue; if (element.getAsJsonObject().get("tier").getAsString().equals("LEGENDARY")) { CalcMonkeyExp = Double.parseDouble(element.getAsJsonObject().get("exp").getAsString()); - // = Double.parseDouble(MonkeyExp); MonkeyLevel = 0; - //CalcMonkeyExp = 16000000; for (int xpLevel : EXPERIENCE_LEVELS) { if (CalcMonkeyExp < xpLevel) { break; @@ -93,20 +91,16 @@ public class ItemCooldowns { MonkeyLevel++; } } - //CalcMonkeyLevel = MonkeyLevel; - //} } } } catch (Exception e) { System.out.println("Pet Level Error or something Idk"); - //LOGGER.error("[Skyblocker] Spirit pet lookup by name failed! Name: {}", name, e); } double BaseCooldown = 2000; String usedItemId = ItemUtils.getItemId(player.getMainHandStack()); if (usedItemId.isEmpty()) return; if (state.isIn(BlockTags.LOGS)) { double EvolvedAxesCooldownReductionPercentage = MonkeyLevel * 0.5; - //double ProcessedCooldown = EvolvedAxesCooldownReductionPercentage * 10000; double MonkeyPetCDRReduction = (BaseCooldown * EvolvedAxesCooldownReductionPercentage) / 100; double currentCooldown = BaseCooldown - MonkeyPetCDRReduction; if (usedItemId.equals(JUNGLE_AXE_ID) || usedItemId.equals(TREECAPITATOR_ID)) { -- cgit From 1d01689f1849b28bd5e47baa20eb8ab8f35864cb Mon Sep 17 00:00:00 2001 From: VeritasDL <121319869+VeritasDL@users.noreply.github.com> Date: Sat, 6 Apr 2024 17:39:53 -0400 Subject: Update ItemCooldowns.java Changed to not request api every block break? i think idk havent tested --- .../skyblocker/skyblock/item/ItemCooldowns.java | 88 ++++++++++++---------- 1 file changed, 50 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java index f1dadf3b..655dac53 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java @@ -50,62 +50,74 @@ public class ItemCooldowns { public static int MonkeyLevel = 1; public static double CalcMonkeyExp = 0; public static int CalcMonkeyLevel = 1; + public static int PetLevelApiDelay = 0; //3min 180000 + public static double currentCooldown = 0; public static void init() { ClientPlayerBlockBreakEvents.AFTER.register(ItemCooldowns::afterBlockBreak); UseItemCallback.EVENT.register(ItemCooldowns::onItemInteract); } - public static void afterBlockBreak(World world, PlayerEntity player, BlockPos pos, BlockState state) { - if (!SkyblockerConfigManager.get().general.itemCooldown.enableItemCooldowns) return; + public static void currentCooldown() { String name = MinecraftClient.getInstance().getSession().getUsername(); String playeruuid = ApiUtils.name2Uuid(name); - try (Http.ApiResponse response = Http.sendHypixelRequest("skyblock/profiles", "?uuid=" + playeruuid)) { - if (!response.ok()) - throw new IllegalStateException("Failed to get profile uuid for player " + name + "! Response: " + response.content()); - JsonObject responseJson = JsonParser.parseString(response.content()).getAsJsonObject(); - - JsonObject players = StreamSupport.stream(responseJson.getAsJsonArray("profiles").spliterator(), false) - .map(JsonElement::getAsJsonObject) - .filter(profile -> profile.getAsJsonPrimitive("selected").getAsBoolean()) - .findFirst() - .orElseThrow(() -> new IllegalStateException("No selected profile found!?")) - .getAsJsonObject("members").entrySet().stream() - .filter(entry -> entry.getKey().equals(playeruuid)) - .map(Map.Entry::getValue) - .map(JsonElement::getAsJsonObject) - .findFirst() - .orElseThrow(() -> new IllegalStateException("Player somehow not found inside their own profile!")); - - for (JsonElement element : players.getAsJsonObject("pets_data").getAsJsonArray("pets")) { - if (!element.getAsJsonObject().get("type").getAsString().equals("MONKEY")) continue; - if (!element.getAsJsonObject().get("active").getAsString().equals("true")) continue; - if (element.getAsJsonObject().get("tier").getAsString().equals("LEGENDARY")) { - CalcMonkeyExp = Double.parseDouble(element.getAsJsonObject().get("exp").getAsString()); - MonkeyLevel = 0; - for (int xpLevel : EXPERIENCE_LEVELS) { - if (CalcMonkeyExp < xpLevel) { - break; - } else { - CalcMonkeyExp -= xpLevel; - MonkeyLevel++; + if (PetLevelApiDelay == 0) + try (Http.ApiResponse response = Http.sendHypixelRequest("skyblock/profiles", "?uuid=" + playeruuid)) { + if (!response.ok()) + throw new IllegalStateException("Failed to get profile uuid for player " + name + "! Response: " + response.content()); + JsonObject responseJson = JsonParser.parseString(response.content()).getAsJsonObject(); + JsonObject players = StreamSupport.stream(responseJson.getAsJsonArray("profiles").spliterator(), false) + .map(JsonElement::getAsJsonObject) + .filter(profile -> profile.getAsJsonPrimitive("selected").getAsBoolean()) + .findFirst() + .orElseThrow(() -> new IllegalStateException("No selected profile found!?")) + .getAsJsonObject("members").entrySet().stream() + .filter(entry -> entry.getKey().equals(playeruuid)) + .map(Map.Entry::getValue) + .map(JsonElement::getAsJsonObject) + .findFirst() + .orElseThrow(() -> new IllegalStateException("Player somehow not found inside their own profile!")); + + for (JsonElement element : players.getAsJsonObject("pets_data").getAsJsonArray("pets")) { + if (!element.getAsJsonObject().get("type").getAsString().equals("MONKEY")) continue; + if (!element.getAsJsonObject().get("active").getAsString().equals("true")) continue; + if (element.getAsJsonObject().get("tier").getAsString().equals("LEGENDARY")) { + CalcMonkeyExp = Double.parseDouble(element.getAsJsonObject().get("exp").getAsString()); + MonkeyLevel = 0; + for (int xpLevel : EXPERIENCE_LEVELS) { + if (CalcMonkeyExp < xpLevel) { + break; + } else { + CalcMonkeyExp -= xpLevel; + MonkeyLevel++; + } } } } + } catch (Exception e) { + System.out.println("Pet Level Error or something Idk"); } - } catch (Exception e) { - System.out.println("Pet Level Error or something Idk"); - } double BaseCooldown = 2000; + double EvolvedAxesCooldownReductionPercentage = MonkeyLevel * 0.5; + double MonkeyPetCDRReduction = (BaseCooldown * EvolvedAxesCooldownReductionPercentage) / 100; + currentCooldown = BaseCooldown - MonkeyPetCDRReduction; + PetLevelApiDelay = 180000; + } + public static void afterBlockBreak(World world, PlayerEntity player, BlockPos pos, BlockState state) { + if (!SkyblockerConfigManager.get().general.itemCooldown.enableItemCooldowns) return; String usedItemId = ItemUtils.getItemId(player.getMainHandStack()); if (usedItemId.isEmpty()) return; if (state.isIn(BlockTags.LOGS)) { - double EvolvedAxesCooldownReductionPercentage = MonkeyLevel * 0.5; - double MonkeyPetCDRReduction = (BaseCooldown * EvolvedAxesCooldownReductionPercentage) / 100; - double currentCooldown = BaseCooldown - MonkeyPetCDRReduction; + if (PetLevelApiDelay == 0) { + currentCooldown(); + PetLevelApiDelay = 180000; + } + else { + PetLevelApiDelay--; + } if (usedItemId.equals(JUNGLE_AXE_ID) || usedItemId.equals(TREECAPITATOR_ID)) { if (!isOnCooldown(JUNGLE_AXE_ID) || !isOnCooldown(TREECAPITATOR_ID)) { - ITEM_COOLDOWNS.put(usedItemId, new CooldownEntry((int)currentCooldown)); + ITEM_COOLDOWNS.put(usedItemId, new CooldownEntry((int) currentCooldown)); } } } -- cgit From 774a0f7af1cca495c65292f1e6012229ea10f5f9 Mon Sep 17 00:00:00 2001 From: VeritasDL Date: Sat, 6 Apr 2024 23:38:46 -0400 Subject: added CompletableFuture.runAsync for the monkey cooldown and added a 3 min api cooldown --- .../skyblocker/skyblock/item/ItemCooldowns.java | 55 ++++++++++++---------- 1 file changed, 29 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java index 655dac53..d1868550 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java @@ -1,4 +1,5 @@ package de.hysky.skyblocker.skyblock.item; + import com.google.gson.JsonParser; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -19,20 +20,23 @@ import net.minecraft.util.Hand; import net.minecraft.util.TypedActionResult; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; + import java.util.HashMap; import java.util.Map; + import com.mojang.util.UndashedUuid; import net.minecraft.client.MinecraftClient; import net.minecraft.client.session.Session; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + import java.util.stream.StreamSupport; +import java.util.concurrent.CompletableFuture; + public class ItemCooldowns { private static final String JUNGLE_AXE_ID = "JUNGLE_AXE"; private static final String TREECAPITATOR_ID = "TREECAPITATOR_AXE"; private static final String GRAPPLING_HOOK_ID = "GRAPPLING_HOOK"; private static final ImmutableList BAT_ARMOR_IDS = ImmutableList.of("BAT_PERSON_HELMET", "BAT_PERSON_CHESTPLATE", "BAT_PERSON_LEGGINGS", "BAT_PERSON_BOOTS"); - + public static final long HypixelApiCooldown = 180000; //3min 180000 private static final Map ITEM_COOLDOWNS = new HashMap<>(); private static final int[] EXPERIENCE_LEVELS = { 0, 660, 730, 800, 880, 960, 1050, 1150, 1260, 1380, 1510, 1650, 1800, 1960, 2130, @@ -45,23 +49,26 @@ public class ItemCooldowns { 561700, 611700, 666700, 726700, 791700, 861700, 936700, 1016700, 1101700, 1191700, 1286700, 1386700, 1496700, 1616700, 1746700, 1886700 }; - - public static String MonkeyExp = ""; public static int MonkeyLevel = 1; public static double CalcMonkeyExp = 0; - public static int CalcMonkeyLevel = 1; - public static int PetLevelApiDelay = 0; //3min 180000 public static double currentCooldown = 0; + public static long unixTimeStamp = 0; public static void init() { ClientPlayerBlockBreakEvents.AFTER.register(ItemCooldowns::afterBlockBreak); UseItemCallback.EVENT.register(ItemCooldowns::onItemInteract); + unixTimeStamp = System.currentTimeMillis() - HypixelApiCooldown; } public static void currentCooldown() { - String name = MinecraftClient.getInstance().getSession().getUsername(); - String playeruuid = ApiUtils.name2Uuid(name); - if (PetLevelApiDelay == 0) + long DeltaTime = System.currentTimeMillis() - unixTimeStamp; + if (DeltaTime < HypixelApiCooldown) { + return; + } + unixTimeStamp = System.currentTimeMillis(); + CompletableFuture.runAsync(() -> { + String name = MinecraftClient.getInstance().getSession().getUsername(); + String playeruuid = ApiUtils.name2Uuid(name); try (Http.ApiResponse response = Http.sendHypixelRequest("skyblock/profiles", "?uuid=" + playeruuid)) { if (!response.ok()) throw new IllegalStateException("Failed to get profile uuid for player " + name + "! Response: " + response.content()); @@ -77,7 +84,6 @@ public class ItemCooldowns { .map(JsonElement::getAsJsonObject) .findFirst() .orElseThrow(() -> new IllegalStateException("Player somehow not found inside their own profile!")); - for (JsonElement element : players.getAsJsonObject("pets_data").getAsJsonArray("pets")) { if (!element.getAsJsonObject().get("type").getAsString().equals("MONKEY")) continue; if (!element.getAsJsonObject().get("active").getAsString().equals("true")) continue; @@ -95,26 +101,21 @@ public class ItemCooldowns { } } } catch (Exception e) { - System.out.println("Pet Level Error or something Idk"); + System.out.println("[Skyblocker] Failed to get Player Pet Data, is the API Down/Limited?"); } - double BaseCooldown = 2000; - double EvolvedAxesCooldownReductionPercentage = MonkeyLevel * 0.5; - double MonkeyPetCDRReduction = (BaseCooldown * EvolvedAxesCooldownReductionPercentage) / 100; - currentCooldown = BaseCooldown - MonkeyPetCDRReduction; - PetLevelApiDelay = 180000; + double BaseCooldown = 2000; + double EvolvedAxesCooldownReductionPercentage = MonkeyLevel * 0.5; + double MonkeyPetCDRReduction = (BaseCooldown * EvolvedAxesCooldownReductionPercentage) / 100; + currentCooldown = BaseCooldown - MonkeyPetCDRReduction; + }); } + public static void afterBlockBreak(World world, PlayerEntity player, BlockPos pos, BlockState state) { if (!SkyblockerConfigManager.get().general.itemCooldown.enableItemCooldowns) return; String usedItemId = ItemUtils.getItemId(player.getMainHandStack()); if (usedItemId.isEmpty()) return; if (state.isIn(BlockTags.LOGS)) { - if (PetLevelApiDelay == 0) { - currentCooldown(); - PetLevelApiDelay = 180000; - } - else { - PetLevelApiDelay--; - } + currentCooldown(); if (usedItemId.equals(JUNGLE_AXE_ID) || usedItemId.equals(TREECAPITATOR_ID)) { if (!isOnCooldown(JUNGLE_AXE_ID) || !isOnCooldown(TREECAPITATOR_ID)) { ITEM_COOLDOWNS.put(usedItemId, new CooldownEntry((int) currentCooldown)); @@ -122,8 +123,10 @@ public class ItemCooldowns { } } } + private static TypedActionResult onItemInteract(PlayerEntity player, World world, Hand hand) { - if (!SkyblockerConfigManager.get().general.itemCooldown.enableItemCooldowns) return TypedActionResult.pass(ItemStack.EMPTY); + if (!SkyblockerConfigManager.get().general.itemCooldown.enableItemCooldowns) + return TypedActionResult.pass(ItemStack.EMPTY); String usedItemId = ItemUtils.getItemId(player.getMainHandStack()); if (usedItemId.equals(GRAPPLING_HOOK_ID) && player.fishHook != null) { @@ -185,4 +188,4 @@ public class ItemCooldowns { return this.isOnCooldown() ? (float) this.getRemainingCooldown() / cooldown : 0.0f; } } -} +} \ No newline at end of file -- cgit From e6c0ca1ec23dfcd6e0137628a1df9d15cb65ab0d Mon Sep 17 00:00:00 2001 From: VeritasDL <121319869+VeritasDL@users.noreply.github.com> Date: Mon, 8 Apr 2024 15:53:55 -0400 Subject: Update ItemCooldowns.java github, intellij or I suck --- src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java index d1868550..86765499 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java @@ -53,7 +53,6 @@ public class ItemCooldowns { public static double CalcMonkeyExp = 0; public static double currentCooldown = 0; public static long unixTimeStamp = 0; - public static void init() { ClientPlayerBlockBreakEvents.AFTER.register(ItemCooldowns::afterBlockBreak); UseItemCallback.EVENT.register(ItemCooldowns::onItemInteract); @@ -115,8 +114,8 @@ public class ItemCooldowns { String usedItemId = ItemUtils.getItemId(player.getMainHandStack()); if (usedItemId.isEmpty()) return; if (state.isIn(BlockTags.LOGS)) { - currentCooldown(); if (usedItemId.equals(JUNGLE_AXE_ID) || usedItemId.equals(TREECAPITATOR_ID)) { + currentCooldown(); if (!isOnCooldown(JUNGLE_AXE_ID) || !isOnCooldown(TREECAPITATOR_ID)) { ITEM_COOLDOWNS.put(usedItemId, new CooldownEntry((int) currentCooldown)); } @@ -127,7 +126,6 @@ public class ItemCooldowns { private static TypedActionResult onItemInteract(PlayerEntity player, World world, Hand hand) { if (!SkyblockerConfigManager.get().general.itemCooldown.enableItemCooldowns) return TypedActionResult.pass(ItemStack.EMPTY); - String usedItemId = ItemUtils.getItemId(player.getMainHandStack()); if (usedItemId.equals(GRAPPLING_HOOK_ID) && player.fishHook != null) { if (!isOnCooldown(GRAPPLING_HOOK_ID) && !isWearingBatArmor(player)) { @@ -188,4 +186,4 @@ public class ItemCooldowns { return this.isOnCooldown() ? (float) this.getRemainingCooldown() / cooldown : 0.0f; } } -} \ No newline at end of file +} -- cgit From e049ed1d7ebfc265283d8ef0205d0826a1bec5de Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Wed, 17 Apr 2024 15:55:18 -0400 Subject: Add ProfileUtils and clean up code --- .../java/de/hysky/skyblocker/SkyblockerMod.java | 2 + .../skyblocker/skyblock/dungeon/DungeonScore.java | 58 ++++--------- .../skyblocker/skyblock/item/ItemCooldowns.java | 97 +++++++--------------- .../de/hysky/skyblocker/utils/ProfileUtils.java | 58 +++++++++++++ 4 files changed, 106 insertions(+), 109 deletions(-) create mode 100644 src/main/java/de/hysky/skyblocker/utils/ProfileUtils.java (limited to 'src') diff --git a/src/main/java/de/hysky/skyblocker/SkyblockerMod.java b/src/main/java/de/hysky/skyblocker/SkyblockerMod.java index 494b16e2..947c3fd3 100644 --- a/src/main/java/de/hysky/skyblocker/SkyblockerMod.java +++ b/src/main/java/de/hysky/skyblocker/SkyblockerMod.java @@ -46,6 +46,7 @@ import de.hysky.skyblocker.skyblock.waypoint.OrderedWaypoints; import de.hysky.skyblocker.skyblock.waypoint.Relics; import de.hysky.skyblocker.utils.ApiUtils; import de.hysky.skyblocker.utils.NEURepoManager; +import de.hysky.skyblocker.utils.ProfileUtils; import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.chat.ChatMessageListener; import de.hysky.skyblocker.utils.discord.DiscordRPCManager; @@ -166,6 +167,7 @@ public class SkyblockerMod implements ClientModInitializer { MuseumItemCache.init(); SecretsTracker.init(); ApiUtils.init(); + ProfileUtils.init(); Debug.init(); Kuudra.init(); RenderHelper.init(); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/DungeonScore.java b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/DungeonScore.java index 9d610ccd..f9e33410 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/DungeonScore.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/DungeonScore.java @@ -3,15 +3,12 @@ package de.hysky.skyblocker.skyblock.dungeon; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import de.hysky.skyblocker.config.SkyblockerConfig; import de.hysky.skyblocker.config.SkyblockerConfigManager; -import de.hysky.skyblocker.events.SkyblockEvents; import de.hysky.skyblocker.skyblock.dungeon.secrets.DungeonManager; import de.hysky.skyblocker.skyblock.tabhud.util.PlayerListMgr; -import de.hysky.skyblocker.utils.ApiUtils; import de.hysky.skyblocker.utils.Constants; -import de.hysky.skyblocker.utils.Http; +import de.hysky.skyblocker.utils.ProfileUtils; import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.scheduler.MessageScheduler; import de.hysky.skyblocker.utils.scheduler.Scheduler; @@ -26,12 +23,8 @@ import net.minecraft.util.collection.DefaultedList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.CompletableFuture; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.stream.StreamSupport; public class DungeonScore { private static final SkyblockerConfig.DungeonScore SCORE_CONFIG = SkyblockerConfigManager.get().locations.dungeons.dungeonScore; @@ -68,11 +61,9 @@ public class DungeonScore { private static int puzzleCount; private static int deathCount; private static int score; - private static final Map SpiritPetCache = new HashMap<>(); - public static void init() { + public static void init() { Scheduler.INSTANCE.scheduleCyclic(DungeonScore::tick, 20); - SkyblockEvents.LEAVE.register(SpiritPetCache::clear); ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> reset()); ClientReceiveMessageEvents.GAME.register((message, overlay) -> { if (overlay || !Utils.isInDungeons()) return; @@ -288,36 +279,18 @@ public class DungeonScore { return matcher != null ? Integer.parseInt(matcher.group("crypts")) : 0; } - public static boolean hasSpiritPet(String name) { - return SpiritPetCache.computeIfAbsent(name, k -> { - String playeruuid = ApiUtils.name2Uuid(name); - try (Http.ApiResponse response = Http.sendHypixelRequest("skyblock/profiles", "?uuid=" + playeruuid)) { - if (!response.ok()) throw new IllegalStateException("Failed to get profile uuid for player " + name + "! Response: " + response.content()); - JsonObject responseJson = JsonParser.parseString(response.content()).getAsJsonObject(); - - JsonObject player = StreamSupport.stream(responseJson.getAsJsonArray("profiles").spliterator(), false) - .map(JsonElement::getAsJsonObject) - .filter(profile -> profile.getAsJsonPrimitive("selected").getAsBoolean()) - .findFirst() - .orElseThrow(() -> new IllegalStateException("No selected profile found!?")) - .getAsJsonObject("members").entrySet().stream() - .filter(entry -> entry.getKey().equals(playeruuid)) - .map(Map.Entry::getValue) - .map(JsonElement::getAsJsonObject) - .findFirst() - .orElseThrow(() -> new IllegalStateException("Player somehow not found inside their own profile!")); - - for (JsonElement element : player.getAsJsonObject("pets_data").getAsJsonArray("pets")) { - if (!element.getAsJsonObject().get("type").getAsString().equals("SPIRIT")) continue; - if (!element.getAsJsonObject().get("tier").getAsString().equals("LEGENDARY")) continue; - - return true; - } - } catch (Exception e) { - LOGGER.error("[Skyblocker] Spirit pet lookup by name failed! Name: {}", name, e); + public static boolean hasSpiritPet(JsonObject player, String name) { + try { + for (JsonElement pet : player.getAsJsonObject("pets_data").getAsJsonArray("pets")) { + if (!pet.getAsJsonObject().get("type").getAsString().equals("SPIRIT")) continue; + if (!pet.getAsJsonObject().get("tier").getAsString().equals("LEGENDARY")) continue; + + return true; } - return false; - }); + } catch (Exception e) { + LOGGER.error("[Skyblocker] Spirit pet lookup by name failed! Name: {}", name, e); + } + return false; } private static void checkMessageForDeaths(String message) { @@ -327,11 +300,10 @@ public class DungeonScore { deathCount++; if (deathCount > 1) return; final String whoDied = matcher.group("whodied").transform(s -> { - if (s.equals("You")) return MinecraftClient.getInstance().player.getName().getString(); //This will be wrong if the dead player is called 'You' but that's unlikely + if (s.equals("You")) return MinecraftClient.getInstance().getSession().getUsername(); //This will be wrong if the dead player is called 'You' but that's unlikely else return s; }); - CompletableFuture.supplyAsync(() -> hasSpiritPet(whoDied)) - .thenAccept(hasSpiritPet -> firstDeathHasSpiritPet = hasSpiritPet); + ProfileUtils.updateProfile(whoDied).thenAccept(player -> firstDeathHasSpiritPet = hasSpiritPet(player, whoDied)); } private static void checkMessageForWatcher(String message) { diff --git a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java index 86765499..be934810 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/item/ItemCooldowns.java @@ -1,15 +1,9 @@ package de.hysky.skyblocker.skyblock.item; -import com.google.gson.JsonParser; import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.common.collect.ImmutableList; import de.hysky.skyblocker.config.SkyblockerConfigManager; -import de.hysky.skyblocker.events.SkyblockEvents; -import de.hysky.skyblocker.utils.ApiUtils; -import de.hysky.skyblocker.utils.Http; import de.hysky.skyblocker.utils.ItemUtils; -import de.hysky.skyblocker.utils.Utils; +import de.hysky.skyblocker.utils.ProfileUtils; import net.fabricmc.fabric.api.event.client.player.ClientPlayerBlockBreakEvents; import net.fabricmc.fabric.api.event.player.UseItemCallback; import net.minecraft.block.BlockState; @@ -22,21 +16,14 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import java.util.HashMap; +import java.util.List; import java.util.Map; -import com.mojang.util.UndashedUuid; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.session.Session; - -import java.util.stream.StreamSupport; -import java.util.concurrent.CompletableFuture; - public class ItemCooldowns { private static final String JUNGLE_AXE_ID = "JUNGLE_AXE"; private static final String TREECAPITATOR_ID = "TREECAPITATOR_AXE"; private static final String GRAPPLING_HOOK_ID = "GRAPPLING_HOOK"; - private static final ImmutableList BAT_ARMOR_IDS = ImmutableList.of("BAT_PERSON_HELMET", "BAT_PERSON_CHESTPLATE", "BAT_PERSON_LEGGINGS", "BAT_PERSON_BOOTS"); - public static final long HypixelApiCooldown = 180000; //3min 180000 + private static final List BAT_ARMOR_IDS = List.of("BAT_PERSON_HELMET", "BAT_PERSON_CHESTPLATE", "BAT_PERSON_LEGGINGS", "BAT_PERSON_BOOTS"); private static final Map ITEM_COOLDOWNS = new HashMap<>(); private static final int[] EXPERIENCE_LEVELS = { 0, 660, 730, 800, 880, 960, 1050, 1150, 1260, 1380, 1510, 1650, 1800, 1960, 2130, @@ -49,75 +36,53 @@ public class ItemCooldowns { 561700, 611700, 666700, 726700, 791700, 861700, 936700, 1016700, 1101700, 1191700, 1286700, 1386700, 1496700, 1616700, 1746700, 1886700 }; - public static int MonkeyLevel = 1; - public static double CalcMonkeyExp = 0; - public static double currentCooldown = 0; - public static long unixTimeStamp = 0; + public static int monkeyLevel = 1; + public static double monkeyExp = 0; + public static void init() { ClientPlayerBlockBreakEvents.AFTER.register(ItemCooldowns::afterBlockBreak); UseItemCallback.EVENT.register(ItemCooldowns::onItemInteract); - unixTimeStamp = System.currentTimeMillis() - HypixelApiCooldown; } - public static void currentCooldown() { - long DeltaTime = System.currentTimeMillis() - unixTimeStamp; - if (DeltaTime < HypixelApiCooldown) { - return; - } - unixTimeStamp = System.currentTimeMillis(); - CompletableFuture.runAsync(() -> { - String name = MinecraftClient.getInstance().getSession().getUsername(); - String playeruuid = ApiUtils.name2Uuid(name); - try (Http.ApiResponse response = Http.sendHypixelRequest("skyblock/profiles", "?uuid=" + playeruuid)) { - if (!response.ok()) - throw new IllegalStateException("Failed to get profile uuid for player " + name + "! Response: " + response.content()); - JsonObject responseJson = JsonParser.parseString(response.content()).getAsJsonObject(); - JsonObject players = StreamSupport.stream(responseJson.getAsJsonArray("profiles").spliterator(), false) - .map(JsonElement::getAsJsonObject) - .filter(profile -> profile.getAsJsonPrimitive("selected").getAsBoolean()) - .findFirst() - .orElseThrow(() -> new IllegalStateException("No selected profile found!?")) - .getAsJsonObject("members").entrySet().stream() - .filter(entry -> entry.getKey().equals(playeruuid)) - .map(Map.Entry::getValue) - .map(JsonElement::getAsJsonObject) - .findFirst() - .orElseThrow(() -> new IllegalStateException("Player somehow not found inside their own profile!")); - for (JsonElement element : players.getAsJsonObject("pets_data").getAsJsonArray("pets")) { - if (!element.getAsJsonObject().get("type").getAsString().equals("MONKEY")) continue; - if (!element.getAsJsonObject().get("active").getAsString().equals("true")) continue; - if (element.getAsJsonObject().get("tier").getAsString().equals("LEGENDARY")) { - CalcMonkeyExp = Double.parseDouble(element.getAsJsonObject().get("exp").getAsString()); - MonkeyLevel = 0; - for (int xpLevel : EXPERIENCE_LEVELS) { - if (CalcMonkeyExp < xpLevel) { - break; - } else { - CalcMonkeyExp -= xpLevel; - MonkeyLevel++; - } + public static void updateCooldown() { + ProfileUtils.updateProfile().thenAccept(player -> { + for (JsonElement pet : player.getAsJsonObject("pets_data").getAsJsonArray("pets")) { + if (!pet.getAsJsonObject().get("type").getAsString().equals("MONKEY")) continue; + if (!pet.getAsJsonObject().get("active").getAsString().equals("true")) continue; + if (pet.getAsJsonObject().get("tier").getAsString().equals("LEGENDARY")) { + monkeyExp = Double.parseDouble(pet.getAsJsonObject().get("exp").getAsString()); + monkeyLevel = 0; + for (int xpLevel : EXPERIENCE_LEVELS) { + if (monkeyExp < xpLevel) { + break; + } else { + monkeyExp -= xpLevel; + monkeyLevel++; } } } - } catch (Exception e) { - System.out.println("[Skyblocker] Failed to get Player Pet Data, is the API Down/Limited?"); } - double BaseCooldown = 2000; - double EvolvedAxesCooldownReductionPercentage = MonkeyLevel * 0.5; - double MonkeyPetCDRReduction = (BaseCooldown * EvolvedAxesCooldownReductionPercentage) / 100; - currentCooldown = BaseCooldown - MonkeyPetCDRReduction; + }).exceptionally(e -> { + ProfileUtils.LOGGER.error("[Skyblocker Item Cooldown] Failed to get Player Pet Data, is the API Down/Limited?", e); + return null; }); } + private static int getCooldown() { + int baseCooldown = 2000; + int monkeyPetCooldownReduction = baseCooldown * monkeyLevel / 200; + return baseCooldown - monkeyPetCooldownReduction; + } + public static void afterBlockBreak(World world, PlayerEntity player, BlockPos pos, BlockState state) { if (!SkyblockerConfigManager.get().general.itemCooldown.enableItemCooldowns) return; String usedItemId = ItemUtils.getItemId(player.getMainHandStack()); if (usedItemId.isEmpty()) return; if (state.isIn(BlockTags.LOGS)) { if (usedItemId.equals(JUNGLE_AXE_ID) || usedItemId.equals(TREECAPITATOR_ID)) { - currentCooldown(); + updateCooldown(); if (!isOnCooldown(JUNGLE_AXE_ID) || !isOnCooldown(TREECAPITATOR_ID)) { - ITEM_COOLDOWNS.put(usedItemId, new CooldownEntry((int) currentCooldown)); + ITEM_COOLDOWNS.put(usedItemId, new CooldownEntry(getCooldown())); } } } diff --git a/src/main/java/de/hysky/skyblocker/utils/ProfileUtils.java b/src/main/java/de/hysky/skyblocker/utils/ProfileUtils.java new file mode 100644 index 00000000..0a1f238a --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/utils/ProfileUtils.java @@ -0,0 +1,58 @@ +package de.hysky.skyblocker.utils; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import de.hysky.skyblocker.SkyblockerMod; +import it.unimi.dsi.fastutil.objects.ObjectLongPair; +import net.minecraft.client.MinecraftClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.CompletableFuture; + +public class ProfileUtils { + public static final Logger LOGGER = LoggerFactory.getLogger(ProfileUtils.class); + private static final long HYPIXEL_API_COOLDOWN = 300000; // 5min = 300000 + + public static Map> players = new HashMap<>(); + + public static void init() { + updateProfile(); + } + + public static CompletableFuture updateProfile() { + return updateProfile(MinecraftClient.getInstance().getSession().getUsername()); + } + + public static CompletableFuture updateProfile(String name) { + ObjectLongPair playerCache = players.get(name); + if (playerCache != null && playerCache.rightLong() + HYPIXEL_API_COOLDOWN > System.currentTimeMillis()) { + return CompletableFuture.completedFuture(playerCache.left()); + } + + return CompletableFuture.supplyAsync(() -> { + String uuid = ApiUtils.name2Uuid(name); + try (Http.ApiResponse response = Http.sendHypixelRequest("skyblock/profiles", "?uuid=" + uuid)) { + if (!response.ok()) { + throw new IllegalStateException("Failed to get profile uuid for players " + name + "! Response: " + response.content()); + } + JsonObject responseJson = SkyblockerMod.GSON.fromJson(response.content(), JsonObject.class); + + JsonObject player = responseJson.getAsJsonArray("profiles").asList().stream() + .map(JsonElement::getAsJsonObject) + .filter(profile -> profile.getAsJsonPrimitive("selected").getAsBoolean()) + .findFirst() + .orElseThrow(() -> new IllegalStateException("No selected profile found!?")) + .getAsJsonObject("members").get(uuid).getAsJsonObject(); + + players.put(name, ObjectLongPair.of(player, System.currentTimeMillis())); + return player; + } catch (Exception e) { + LOGGER.error("[Skyblocker Profile Utils] Failed to get Player Profile Data for players {}, is the API Down/Limited?", name, e); + } + return null; + }); + } +} -- cgit