diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-06-04 12:40:50 +0200 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-06-04 12:40:50 +0200 |
commit | 27ca631ac727377f0514954dd179ef145a8feba4 (patch) | |
tree | ab5ba44d4cd5f47d995df6f81a67846e200c2753 /src/main/java/at/hannibal2/skyhanni/data | |
parent | 1a6dc6ad26494c6769159a9b404ccfb837065bcf (diff) | |
download | skyhanni-27ca631ac727377f0514954dd179ef145a8feba4.tar.gz skyhanni-27ca631ac727377f0514954dd179ef145a8feba4.tar.bz2 skyhanni-27ca631ac727377f0514954dd179ef145a8feba4.zip |
Added Command Autocomplete
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/data')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/data/FriendAPI.kt | 144 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/data/PartyAPI.kt | 67 |
2 files changed, 211 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/FriendAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/FriendAPI.kt new file mode 100644 index 000000000..5cc815437 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/FriendAPI.kt @@ -0,0 +1,144 @@ +package at.hannibal2.skyhanni.data + +import at.hannibal2.skyhanni.config.ConfigManager +import at.hannibal2.skyhanni.events.HypixelJoinEvent +import at.hannibal2.skyhanni.events.LorenzChatEvent +import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.StringUtils.cleanPlayerName +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher +import at.hannibal2.skyhanni.utils.jsonobjects.FriendsJson +import at.hannibal2.skyhanni.utils.jsonobjects.FriendsJson.PlayerFriends.Friend +import net.minecraft.util.ChatStyle +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import java.io.File +import java.io.FileReader +import java.util.* + +class FriendAPI { + private val file = File("config/skyhanni/friends.json") + private val removedFriendPattern = + ".*\n§r§eYou removed §r(?<name>.*)§e from your friends list!§r§9§m\n.*".toPattern() + private val addedFriendPattern = "§aYou are now friends with (?<name>.*)".toPattern() + private val noBestFriendPattern = ".*\n§r(?<name>.*)§e is no longer a best friend!§r§9§m\n.*".toPattern() + private val bestFriendPattern = ".*\n(?<name>.*)§a is now a best friend!§r§9§m\n.*".toPattern() + + companion object { + + private var friendsJson: FriendsJson? = null + + private fun getFriends(): MutableMap<UUID, Friend> { + val friendsJson = friendsJson ?: error("savedFriends not loaded yet!") + return friendsJson.players.getOrPut(LorenzUtils.getRawPlayerUuid()) { + FriendsJson.PlayerFriends().also { it.friends = mutableMapOf() } + }.friends + } + + private val tempFriends = mutableListOf<Friend>() + + fun getAllFriends(): List<Friend> { + val list = mutableListOf<Friend>() + list.addAll(getFriends().values) + list.addAll(tempFriends) + return list + } + } + + @SubscribeEvent + fun onHypixelJoin(event: HypixelJoinEvent) { + if (file.isFile) { + friendsJson = ConfigManager.gson.fromJson(FileReader(file), FriendsJson::class.java) + } + if (friendsJson == null) { + file.parentFile.mkdirs() + file.createNewFile() + friendsJson = FriendsJson().also { it.players = mutableMapOf() } + saveConfig() + } + } + + fun saveConfig() { + file.writeText(ConfigManager.gson.toJson(friendsJson)) + } + + @SubscribeEvent + fun onChat(event: LorenzChatEvent) { + readFriendsList(event) + + removedFriendPattern.matchMatcher(event.message) { + val name = group("name").cleanPlayerName() + println("removed friend: '$name'") + removedFriend(name) + } + addedFriendPattern.matchMatcher(event.message) { + val name = group("name").cleanPlayerName() + println("added friend: '$name'") + addFriend(name) + } + + noBestFriendPattern.matchMatcher(event.message) { + val name = group("name").cleanPlayerName() + println("no best friend: '$name'") + setBestFriend(name, false) + } + bestFriendPattern.matchMatcher(event.message) { + val name = group("name").cleanPlayerName() + println("best friend: '$name'") + setBestFriend(name, true) + } + } + + private fun setBestFriend(name: String, bestFriend: Boolean) { + getFriends().entries.firstOrNull { it.value.name == name }?.let { + it.value.bestFriend = bestFriend + saveConfig() + } + } + + private fun addFriend(name: String) { + tempFriends.add(Friend().also { it.name = name }) + } + + private fun removedFriend(name: String) { + tempFriends.removeIf { it.name == name } + getFriends().entries.removeIf { it.value.name == name } + saveConfig() + } + + private fun readFriendsList(event: LorenzChatEvent) { + if (!event.message.contains("Friends")) return + + for (sibling in event.chatComponent.siblings) { + val chatStyle = sibling.chatStyle ?: continue + val value = chatStyle.chatClickEvent?.value ?: continue + if (!value.startsWith("/viewprofile")) continue + + val uuid = "/viewprofile (?<uuid>.*)".toPattern().matchMatcher(value) { + group("uuid")?.let { + UUID.fromString(it) + } + } + val bestFriend = sibling.unformattedText.contains("§l") + val name = readName(chatStyle) + if (uuid != null && name != null) { + getFriends()[uuid] = Friend().also { + it.name = name + it.bestFriend = bestFriend + } + } + } + + saveConfig() + } + + private fun readName(chatStyle: ChatStyle): String? { + for (component in chatStyle.chatHoverEvent.value.siblings) { + val rawName = component.unformattedText + val rawNamePattern = "\\n§eClick to view §.(?<name>.*)§e's profile".toPattern() + rawNamePattern.matchMatcher(rawName) { + return group("name") + } + } + + return null + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/data/PartyAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/PartyAPI.kt new file mode 100644 index 000000000..21f76998c --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/PartyAPI.kt @@ -0,0 +1,67 @@ +package at.hannibal2.skyhanni.data + +import at.hannibal2.skyhanni.events.LorenzChatEvent +import at.hannibal2.skyhanni.utils.StringUtils.cleanPlayerName +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +class PartyAPI { + companion object { + val partyMembers = mutableListOf<String>() + } + + @SubscribeEvent + fun onChat(event: LorenzChatEvent) { + val message = event.message + // new member joined + "§eYou have joined §r(?<name>.*)'s §r§eparty!".toPattern().matchMatcher(message) { + val name = group("name").cleanPlayerName() + partyMembers.add(name) + println("partyMembers: $partyMembers") + } + "(?<name>.*) §r§ejoined the party.".toPattern().matchMatcher(message) { + val name = group("name").cleanPlayerName() + partyMembers.add(name) + println("partyMembers: $partyMembers") + } + "§eYou'll be partying with: §r(?<names>.*)".toPattern().matchMatcher(message) { + for (name in group("names").split(", ")) { + partyMembers.add(name.cleanPlayerName()) + } + println("partyMembers: $partyMembers") + } + + // one member got removed + "(?<name>.*) §r§ehas left the party.".toPattern().matchMatcher(message) { + val name = group("name").cleanPlayerName() + partyMembers.remove(name) + println("partyMembers: $partyMembers") + } + "(?<name>.*) §r§ehas been removed from the party.".toPattern().matchMatcher(message) { + val name = group("name").cleanPlayerName() + partyMembers.remove(name) + println("partyMembers: $partyMembers") + } + "(?<name>.*) neuberddo§r§e because they were offline.".toPattern().matchMatcher(message) { + val name = group("name").cleanPlayerName() + partyMembers.remove(name) + println("partyMembers: $partyMembers") + } + + // party disbanded + ".* §r§ehas disbanded the party!".toPattern().matchMatcher(message) { + partyMembers.clear() + println("partyMembers: $partyMembers") + } + "§eYou have been kicked from the party by §r.* §r§e".toPattern().matchMatcher(message) { + partyMembers.clear() + println("partyMembers: $partyMembers") + } + if (message == "§eYou left the party." || + message == "§cThe party was disbanded because all invites expired and the party was empty." + ) { + partyMembers.clear() + println("partyMembers: $partyMembers") + } + } +} |