Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Notify when a player join/leave your lobby #2653

Open
wants to merge 7 commits into
base: beta
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import at.hannibal2.skyhanni.utils.LorenzColor;
import com.google.gson.annotations.Expose;
import io.github.notenoughupdates.moulconfig.annotations.Accordion;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorBoolean;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorDropdown;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorText;
import io.github.notenoughupdates.moulconfig.annotations.ConfigOption;
import io.github.notenoughupdates.moulconfig.observer.Property;

Expand Down Expand Up @@ -34,4 +36,36 @@ public class MarkedPlayerConfig {
@ConfigEditorDropdown
public Property<LorenzColor> entityColor = Property.of(LorenzColor.YELLOW);

@Expose
@ConfigOption(name = "Join/Leave Message", desc = "")
@Accordion
public JoinLeaveMessage joinLeaveMessage = new JoinLeaveMessage();

public static class JoinLeaveMessage {
@Expose
@ConfigOption(name = "Enabled", desc = "Enable the Join/Leave message for marked players.")
@ConfigEditorBoolean
public boolean enabled = false;

@Expose
@ConfigOption(name = "Players List", desc = "The list of players you want to be notified for.\n§cCase sensitive, separated by commas.")
@ConfigEditorText
public Property<String> playersList = Property.of("hypixel,Minikloon");

@Expose
@ConfigOption(name = "Use Prefix", desc = "Should the [SkyHanni] prefix should be in the join/leave message ?")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatting around the question mark is incorret

@ConfigEditorBoolean
public boolean usePrefix = true;

@Expose
@ConfigOption(name = "Join Message", desc = "Configure the message when someone joins.\n&& is replaced with the minecraft color code §.\n%s is replaced with the player name.")
@ConfigEditorText
public String joinMessage = "&&b%s &&ajoined your lobby.";

@Expose
@ConfigOption(name = "Left Message", desc = "Configure the message when someone leaves.\n&& is replaced with the minecraft color code §.\n%s is replaced with the player name.")
@ConfigEditorText
public String leftMessage = "&&b%s &&cleft your lobby.";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ package at.hannibal2.skyhanni.features.misc
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.config.enums.OutsideSbFeature
import at.hannibal2.skyhanni.data.model.TabWidget
import at.hannibal2.skyhanni.events.ConfigLoadEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.events.SecondPassedEvent
import at.hannibal2.skyhanni.events.WidgetUpdateEvent
import at.hannibal2.skyhanni.mixins.hooks.RenderLivingEntityHelper
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.ColorUtils.withAlpha
import at.hannibal2.skyhanni.utils.ConditionalUtils.onToggle
import at.hannibal2.skyhanni.utils.EntityUtils
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.client.Minecraft
import net.minecraft.client.entity.EntityOtherPlayerMP
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
Expand All @@ -25,6 +29,24 @@ object MarkedPlayerManager {
private val playerNamesToMark = mutableListOf<String>()
private val markedPlayers = mutableMapOf<String, EntityOtherPlayerMP>()

private val patternGroup = RepoPattern.group("misc.markedplayer")

/**
* REGEX-TEST: §8[§r§6400§r§8] §r§6HiZe_ §r§6▒
* REGEX-TEST: §8[§r§9318§r§8] §r§bwings_wacr §r§b§lᛝ
* REGEX-TEST: §8[§r§d321§r§8] §r§bbotbob21 §r§b§lᛝ
* REGEX-TEST: §8[§r§f42§r§8] §r§aVoidW_
* REGEX-TEST: §8[§r§a151§r§8] §r§bPhoenix_325
*/
private val tabPlayerName by patternGroup.pattern(
"tabplayername",
"§8\\[§r(?<level>.*)§r§8] §r§\\w(?<name>[A-z0-9_]+)(?<symbol>.*)?",
)

private val notifyList = mutableSetOf<String>()
private val currentLobbyPlayers = mutableSetOf<String>()
private var personOfInterest = listOf<String>()

fun command(args: Array<String>) {
if (args.size != 1) {
ChatUtils.userError("Usage: /shmarkplayer <name>")
Expand Down Expand Up @@ -72,7 +94,7 @@ object MarkedPlayerManager {
RenderLivingEntityHelper.setEntityColorWithNoHurtTime(
this,
config.entityColor.get().toColor().withAlpha(127),
::isEnabled
::isEnabled,
)
}

Expand Down Expand Up @@ -105,6 +127,10 @@ object MarkedPlayerManager {
}
}
config.entityColor.onToggle(::refreshColors)
config.joinLeaveMessage.playersList.onToggle {
personOfInterest = config.joinLeaveMessage.playersList.get().split(",").map { it.trim() }
}

}

@SubscribeEvent
Expand All @@ -119,6 +145,8 @@ object MarkedPlayerManager {
if (Minecraft.getMinecraft().thePlayer == null) return

markedPlayers.clear()
notifyList.clear()
currentLobbyPlayers.clear()
if (config.markOwnName.get()) {
val name = LorenzUtils.getPlayerName()
if (!playerNamesToMark.contains(name)) {
Expand All @@ -127,6 +155,42 @@ object MarkedPlayerManager {
}
}

@SubscribeEvent
fun onTablistUpdate(event: WidgetUpdateEvent) {
if (!isEnabled()) return
if (!config.joinLeaveMessage.enabled) return
if (!event.isWidget(TabWidget.PLAYER_LIST)) return

currentLobbyPlayers.clear()

loop@ for (line in event.lines) {
tabPlayerName.matchMatcher(line) {
val name = group("name")
if (name == LorenzUtils.getPlayerName()) continue@loop
currentLobbyPlayers.add(name)
}
}

val playerJoined = currentLobbyPlayers.filter { it in personOfInterest && it !in notifyList }
val playerLeft = personOfInterest.filter { it in notifyList && it !in currentLobbyPlayers }

if (playerJoined.isNotEmpty()) {
ChatUtils.chat(
String.format(config.joinLeaveMessage.joinMessage.replace("&&", "§"), playerJoined.joinToString(", ")),
config.joinLeaveMessage.usePrefix,
)
notifyList.addAll(playerJoined)
}

if (playerLeft.isNotEmpty()) {
ChatUtils.chat(
String.format(config.joinLeaveMessage.leftMessage.replace("&&", "§"), playerLeft.joinToString(", ")),
config.joinLeaveMessage.usePrefix,
)
notifyList.removeAll(playerLeft)
}
}

@SubscribeEvent
fun onConfigFix(event: ConfigUpdaterMigrator.ConfigFixEvent) {
event.move(31, "markedPlayers", "gui.markedPlayers")
Expand Down
Loading