diff options
Diffstat (limited to 'src/main/java/com/thatgravyboat/skyblockhud/handlers')
-rw-r--r-- | src/main/java/com/thatgravyboat/skyblockhud/handlers/CooldownHandler.java | 98 | ||||
-rw-r--r-- | src/main/java/com/thatgravyboat/skyblockhud/handlers/WarpHandler.java | 172 |
2 files changed, 270 insertions, 0 deletions
diff --git a/src/main/java/com/thatgravyboat/skyblockhud/handlers/CooldownHandler.java b/src/main/java/com/thatgravyboat/skyblockhud/handlers/CooldownHandler.java new file mode 100644 index 0000000..d7d3a41 --- /dev/null +++ b/src/main/java/com/thatgravyboat/skyblockhud/handlers/CooldownHandler.java @@ -0,0 +1,98 @@ +package com.thatgravyboat.skyblockhud.handlers; + +import com.thatgravyboat.skyblockhud.SkyblockHud; +import com.thatgravyboat.skyblockhud.api.item.IAbility; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraftforge.event.entity.player.PlayerInteractEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.TickEvent; + +public class CooldownHandler { + + private static final Pattern ABILITY_REGEX = Pattern.compile("\u00A76Ability: (.*) \u00A7e\u00A7lRIGHT CLICK .* \u00A78Cooldown: \u00A7a(\\d*)s"); + + private static final Map<String, Cooldown> COOLDOWNS = new HashMap<>(); + + public static Matcher getAbility(NBTTagCompound nbt) { + if (nbt != null && nbt.hasKey("ExtraAttributes") && nbt.getCompoundTag("ExtraAttributes").hasKey("uuid") && nbt.hasKey("display")){ + NBTTagCompound display = nbt.getCompoundTag("display"); + if (display != null && display.hasKey("Lore")){ + NBTTagList lore = display.getTagList("Lore", 8); + List<String> loreList = new ArrayList<>(); + for (int i = 0; i < lore.tagCount(); i++) { + String loreLine = lore.getStringTagAt(i).trim(); + if (!loreLine.isEmpty()) loreList.add(loreLine); + } + Matcher abilityMatcher = ABILITY_REGEX.matcher(String.join(" ", loreList)); + if (abilityMatcher.find()){ + return abilityMatcher; + } + } + } + return null; + } + + private static void addCooldown(IAbility ability) { + COOLDOWNS.putIfAbsent(ability.getAbility(), new Cooldown(ability.getAbilityTime()*20)); + } + + @SubscribeEvent + public void tick(TickEvent.ClientTickEvent event){ + if (SkyblockHud.config.misc.hideItemCooldowns) return; + if (event.phase.equals(TickEvent.Phase.END)) { + COOLDOWNS.values().forEach(Cooldown::tick); + COOLDOWNS.entrySet().removeIf(entry -> entry.getValue().isOver()); + } + } + + @SubscribeEvent + public void onPlayerInteract(PlayerInteractEvent event) { + if (SkyblockHud.config.misc.hideItemCooldowns) return; + if (event.action.equals(PlayerInteractEvent.Action.RIGHT_CLICK_AIR) || event.action.equals(PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK)){ + if (event.entityPlayer.getHeldItem() != null) { + IAbility ability = (IAbility)((Object) event.entityPlayer.getHeldItem()); + if (ability.getAbility() != null) { + addCooldown(ability); + } + } + } + } + + public static float getAbilityTime(ItemStack stack){ + IAbility ability = (IAbility)((Object) stack); + if (ability.getAbility() != null) { + return COOLDOWNS.containsKey(ability.getAbility()) ? COOLDOWNS.get(ability.getAbility()).getTime() : -1f; + } + return -1f; + } + + private static class Cooldown { + public int current; + public final int end; + + Cooldown(int end){ + this.end = end; + } + + public boolean isOver() { + return current >= end; + } + + public void tick() { + current++; + } + + public float getTime() { + return current/(float)end; + } + } + +} diff --git a/src/main/java/com/thatgravyboat/skyblockhud/handlers/WarpHandler.java b/src/main/java/com/thatgravyboat/skyblockhud/handlers/WarpHandler.java new file mode 100644 index 0000000..71079b6 --- /dev/null +++ b/src/main/java/com/thatgravyboat/skyblockhud/handlers/WarpHandler.java @@ -0,0 +1,172 @@ +package com.thatgravyboat.skyblockhud.handlers; + +import com.google.common.collect.HashMultimap; +import com.google.common.collect.SetMultimap; +import com.google.gson.*; +import com.thatgravyboat.skyblockhud.SkyblockHud; +import com.thatgravyboat.skyblockhud.api.events.ProfileJoinedEvent; +import com.thatgravyboat.skyblockhud.api.events.ProfileSwitchedEvent; +import com.thatgravyboat.skyblockhud.mixins.GuiChestAccessor; +import com.thatgravyboat.skyblockhud.utils.Utils; +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.inventory.GuiChest; +import net.minecraft.init.Items; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagList; +import net.minecraftforge.client.event.GuiOpenEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +public class WarpHandler { + + private static String profile = null; + private static File warpConfig; + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create(); + private static final SetMultimap<String, Warp> PLAYER_WARPS = HashMultimap.create(); + + public static Collection<Warp> getWarps() { + return PLAYER_WARPS.get(profile); + } + + @SubscribeEvent + public void profileChange(ProfileSwitchedEvent event){ + if (profile != null && !profile.equals(event.profile)){ + load(); + } + profile = event.profile; + } + + @SubscribeEvent + public void profileJoined(ProfileJoinedEvent event){ + if (profile != null && !profile.equals(event.profile)){ + load(); + } + profile = event.profile; + } + + @SubscribeEvent + public void onGuiClosed(GuiOpenEvent event) { + boolean changed = false; + GuiScreen currentScreen = Minecraft.getMinecraft().currentScreen; + if (currentScreen instanceof GuiChest){ + GuiChestAccessor accessor = (GuiChestAccessor) currentScreen; + if (accessor.getLowerChestInventory().getDisplayName().getUnformattedText().contains("Fast Travel")){ + for (int i = 9; i < Math.min(36, accessor.getLowerChestInventory().getSizeInventory()); i++) { + ItemStack stack = accessor.getLowerChestInventory().getStackInSlot(i); + if (stack != null && stack.getItem().equals(Items.skull) && stack.getTagCompound().hasKey("display")){ + NBTTagList lore = stack.getTagCompound().getCompoundTag("display").getTagList("Lore", 8); + + String warpLine = Utils.removeColor(lore.getStringTagAt(0)).trim(); + + if (warpLine.equals("Unknown island!")) continue; + + String disabledLine = Utils.removeColor(lore.getStringTagAt(lore.tagCount()-1)).trim(); + + Warp warp = Warp.fromId(warpLine.replace("/warp", "").trim()); + + if (warp != null && !disabledLine.equals("Warp not unlocked!")) { + if (PLAYER_WARPS.put(profile, warp)){ + changed = true; + } + } + } + } + } + } + if (changed) save(); + } + + public static void save() { + JsonObject json = new JsonObject(); + JsonArray array = new JsonArray(); + PLAYER_WARPS.asMap().forEach((profile, warps) -> { + JsonObject profileObject = new JsonObject(); + profileObject.addProperty("profile", profile); + JsonArray warpArray = new JsonArray(); + warps.forEach(warp -> warpArray.add(new JsonPrimitive(warp.name()))); + profileObject.add("warps", warpArray); + array.add(profileObject); + }); + json.add("profileWarps", array); + + warpConfig = new File(SkyblockHud.configDirectory, "sbh-warps.json"); + + try { + warpConfig.createNewFile(); + try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(warpConfig), StandardCharsets.UTF_8))) { + writer.write(GSON.toJson(json)); + } + } catch (IOException ignored) {} + } + + public static boolean load() { + warpConfig = new File(SkyblockHud.configDirectory, "sbh-warps.json"); + + try { + if (warpConfig.createNewFile()) return true; + try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(warpConfig), StandardCharsets.UTF_8))) { + JsonObject json = GSON.fromJson(reader, JsonObject.class); + json.get("profileWarps").getAsJsonArray().forEach(jsonElement -> { + JsonObject profileObject = jsonElement.getAsJsonObject(); + List<Warp> warps = new ArrayList<>(); + profileObject.get("warps").getAsJsonArray().forEach(warpId -> { + Warp warp = Warp.safeValueOf(warpId.getAsString()); + if (warp != null) warps.add(warp); + }); + PLAYER_WARPS.putAll(profileObject.get("profile").getAsString(), warps); + }); + } + } catch (Exception ignored) {} + return false; + } + + public enum Warp { + HUB("hub"), + PRIVATE("home"), + SPIDERSDEN("spider"), + BLAZINGFORTRESS("nether"), + THEEND("end"), + THEPARK("park"), + GOLDMINE("gold"), + DEEPCAVERNS("deep"), + DWARVENMINES("mines"), + THEBARN("barn"), + MUSHROOMDESERT("desert"), + THECASTLE("castle"), + SIRIUSSHACK("da"), + GRAVEYARDCAVES("crypt"), + SPIDERSNEST("nest"), + MAGMACUBE("magma"), + DRAGONNEST("drag"), + JUNGLE("jungle"), + HOWLINGCAVE("howl"), + DUNGEONHUB("dungeon_hub"); + + public String warpId; + + Warp(String warpId){ + this.warpId = warpId; + } + + public static Warp fromId(String id){ + for (Warp value : Warp.values()) { + if (value.warpId.equals(id)) return value; + } + return null; + } + + public static Warp safeValueOf(String value){ + try { + return Warp.valueOf(value); + }catch (Exception e) { + return null; + } + } + } + +} |