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

Implement notification when LabyConnect friend get's removed #4

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class LabyChatUtilsAddon extends LabyAddon<LabyChatUtilsConfig> {
protected void enable() {
registerSettingCategory();
registerCommand(new LabyChatUtilsCommand());
registerListener(new LabyChatListener());
registerListener(new LabyChatListener(this));
labyAPI().hudWidgetRegistry().register(new UnreadChatCountWidget());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,39 @@
import net.labymod.api.addon.AddonConfig;
import net.labymod.api.client.gui.screen.widget.widgets.input.SwitchWidget.SwitchSetting;
import net.labymod.api.configuration.loader.property.ConfigProperty;
import net.labymod.api.configuration.settings.annotation.SettingRequires;
import net.labymod.api.configuration.settings.annotation.SettingSection;

public class LabyChatUtilsConfig extends AddonConfig {

@SwitchSetting
private final ConfigProperty<Boolean> enabled = new ConfigProperty<>(true);
@SettingSection("friends")
@SwitchSetting
private final ConfigProperty<Boolean> showIncomingRequests = new ConfigProperty<>(true);
@SwitchSetting
private final ConfigProperty<Boolean> showRemovedFriends = new ConfigProperty<>(true);
@SettingSection("messages")
@SwitchSetting
private final ConfigProperty<Boolean> showAnyMessages = new ConfigProperty<>(true);
@SettingRequires("showAnyMessages")
@SwitchSetting
private final ConfigProperty<Boolean> showOwnMessages = new ConfigProperty<>(true);

@Override
public ConfigProperty<Boolean> enabled() {
return enabled;
}
public boolean showIncomingRequests() {
return showIncomingRequests.get();
}
public boolean showRemovedFriends() {
return showRemovedFriends.get();
}
public boolean showAnyMessages() {
return showAnyMessages.get();
}
public boolean showOwnMessages() {
return showOwnMessages.get();
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
package com.rappytv.labychatutils.listeners;

import com.rappytv.labychatutils.LabyChatUtilsAddon;
import com.rappytv.labychatutils.LabyChatUtilsConfig;
import net.labymod.api.Laby;
import net.labymod.api.client.component.Component;
import net.labymod.api.client.component.event.ClickEvent;
import net.labymod.api.client.component.event.HoverEvent;
import net.labymod.api.client.component.format.NamedTextColor;
import net.labymod.api.client.component.format.TextDecoration;
import net.labymod.api.client.gui.icon.Icon;
import net.labymod.api.event.Subscribe;
import net.labymod.api.event.labymod.labyconnect.session.chat.LabyConnectChatMessageEvent;
import net.labymod.api.event.labymod.labyconnect.session.friend.LabyConnectFriendRemoveEvent;
import net.labymod.api.event.labymod.labyconnect.session.request.LabyConnectIncomingFriendRequestAddEvent;
import net.labymod.api.labyconnect.protocol.model.chat.TextChatMessage;
import net.labymod.api.notification.Notification;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class LabyChatListener {

private static final Map<UUID, TextChatMessage> messages = new HashMap<>();
private final LabyChatUtilsConfig config;

public LabyChatListener(LabyChatUtilsAddon addon) {
this.config = addon.configuration();
}

@Subscribe
public void onFriendRequestReceive(LabyConnectIncomingFriendRequestAddEvent event) {
if(!config.showIncomingRequests()) return;
Laby.references().chatExecutor().displayClientMessage(
Component.empty()
.append(LabyChatUtilsAddon.prefix)
Expand Down Expand Up @@ -55,14 +65,32 @@ public void onFriendRequestReceive(LabyConnectIncomingFriendRequestAddEvent even
);
}

@Subscribe
public void onFriendRemove(LabyConnectFriendRemoveEvent event) {
if(!config.showRemovedFriends()) return;
Laby.labyAPI().notificationController().push(
Notification.builder()
.title(Component.translatable("labychatutils.messages.remove.title"))
.text(Component.translatable(
"labychatutils.messages.remove.description",
Component.text(event.friend().getName())
))
.icon(Icon.head(event.friend().getUniqueId(), true))
.duration(10000)
.build()
);
}

@SuppressWarnings("ConstantConditions")
@Subscribe
public void onChatReceive(LabyConnectChatMessageEvent event) {
if(!config.showAnyMessages()) return;
TextChatMessage message = (TextChatMessage) event.message();
if(event.labyConnect().getSession() == null) return;
boolean isSelf = message.sender() == event.labyConnect().getSession().self();
UUID uuid = UUID.randomUUID();
messages.put(uuid, message);
if(isSelf && !config.showOwnMessages()) return;
Laby.references().chatExecutor().displayClientMessage(LabyChatUtilsAddon.chatMessage(
message.sender().getName(),
message.getRawMessage(),
Expand Down
28 changes: 27 additions & 1 deletion core/src/main/resources/assets/labychatutils/i18n/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,30 @@
"labychatutils": {
"settings": {
"name": "LabyChat Utils",
"header": {
"friends": {
"name": "Friends"
},
"messages": {
"name": "LabyChat messages"
}
},
"enabled": {
"name": "Enabled"
},
"showIncomingRequests": {
"name": "Show incoming requests in chat"
},
"showRemovedFriends": {
"name": "Notify you if one of your friends removes you.",
"description": "Note that this will also notify you when you remove them from your friend list as there is no way to check who was performing the action."
},
"showAnyMessages": {
"name": "Show any received messages in chat",
"description": "Disabling this will completely hide received messages in chat."
},
"showOwnMessages": {
"name": "Show your own sent messages in chat"
}
},
"messages": {
Expand All @@ -26,7 +48,11 @@
"reply": "Reply",
"read": "Mark as read",
"markedRead": "The message was marked as read.",
"manual": "This command can't be run manually!"
"manual": "This command can't be run manually!",
"remove": {
"title": "LabyMod friendship ended!",
"description": "Either you removed %s as a friend or they removed you."
}
}
}
}
Loading