I am making a plugin where people can toggle the chat for themselves on the server.
What I have right now works pretty well:
#EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerChat(#NotNull AsyncPlayerChatEvent e) {
if(TOGGLED_USERS_BY_UUID.contains(e.getPlayer().getUniqueId().toString())) {
e.setCancelled(true);
e.getPlayer().sendRawMessage(ToggleChat.LANG.get("cannotChat"));
return;
}
String message = e.getMessage();
getLogger().info(String.format("<%s>: %s", e.getPlayer().getName(), message));
e.setCancelled(true);
for(Player p : getServer().getOnlinePlayers()) {
if(!TOGGLED_USERS_BY_UUID.contains(p.getUniqueId().toString()))
p.sendRawMessage(String.format("<%s> %s", e.getPlayer().getName(), message));
}
}
But the problem comes when users have other things for their chat, such as a specific level for their chat message like (lvl) [username] - message or something.
How do I make it so that I don't have to re-send the message to users or just cancel the event for a specific user?
Thanks!
#EventHandler
public void onPlayerChat(AsyncPlayerChatEvent event) {
event.getRecipients().remove(/*player who shouldn't see chat*/);
}
If you want to filter players by their xp levels, you can use
getRecipients().removeIf(p -> p.getLevel() < minimumLevel)
Related
General Info
I'm working on a chat plugin for Spigot (Minecraft server). The idea is to create a chatsystem that allows the use of working chat tabs:
In order to accomplish this, I have to use NMS (ProtocolLib) because Minecraft uses 3 types of messages in chat:
Chat messages
System messages
Game info messages
Chat messages are easily accessible, but the other 2 are not. If I don't catch those messages, they push the tabs menu up instead of ending up in their own chat channel.
The problem
The code (below) I'm using to catch these messages is causing the following exception in connected Minecraft clients and disconnects them upon an ingame plugin reload through /reload:
DecoderException: java.lang.IndexOutOfBoundsException: readerIndex(0)
+ length(1) exceeds writerIndex(0) UnpooledSlicedByteBuf(ridx: 0, widx: 0, cap: 0/0, unwrapped: PooledUnsafeDirectByteBuf(ridx: 2, widx:
6, cap: 16384)) # io.netty.handler.codec.MessageToMessageDecoder:98
Relevant code
private void initPacketListener(){
protocolManager.addPacketListener(new PacketAdapter(this, ListenerPriority.NORMAL, new PacketType[] { PacketType.Play.Server.CHAT })
{
#SuppressWarnings("unused")
#Override
public void onPacketSending(PacketEvent event) {
if ((event.getPacketType() == PacketType.Play.Server.CHAT)){
PacketContainer packet = event.getPacket();
Player player = event.getPlayer();
String message = "";
try {
String jsonMessage = event.getPacket().getChatComponents().getValues().get(0).getJson();
if (jsonMessage!=null&&!jsonMessage.isEmpty()) {
message = jsonToString(jsonMessage);
if (message.isEmpty()) return;
Subscriber subscriber = UMM.plugin.getSubscribers().get(player.getUniqueId());
String uuid = player.getUniqueId().toString();
Channel messageChannel = UMM.serverChannels.get(uuid);
messageChannel.addMessage(message);
ChatListener cl = new ChatListener(UMM.plugin);
cl.notifyChanges(messageChannel);
event.setCancelled(true);
} else {
System.out.println("Not a system msg");
}
} catch (Throwable e){
System.out.println("UMM Packet Error:" + e.getMessage());
}
}
}
});
}
What I've tried myself to solve the issue
I asked some other devs about the problem and was told:
Make sure that the size/length of event.getPacket().getChatComponents().getValues() is greater than 0 before making the #get call.
So I tried this:
List<WrappedChatComponent> wrap = event.getPacket().getChatComponents().getValues();
if(wrap.size() > 0) {
if ((event.getPacketType() == PacketType.Play.Server.CHAT)){
...
}
}
But it had no effect at all. They were unable to help me any further so I hope someone else might know what's going on and how I can solve the problem.
Update
Even after reducing the code to just this, the problem still persists:
private void initPacketListener(){
protocolManager.addPacketListener(new PacketAdapter(this, ListenerPriority.NORMAL, new PacketType[] { PacketType.Play.Server.CHAT })
{
#SuppressWarnings("unused")
#Override
public void onPacketSending(PacketEvent event) {
}
});
}
A "temporary" solution to the problem is using a BukkitRunnable to call the method 1 tick after the server is done reloading. The only down side to this is that any messages appearing before the first tick are pushing the tabs menu up. The plugin fixes this upon the next message event.
You should never use /reload command, see more here for example : https://madelinemiller.dev/blog/problem-with-reload/.
You can implement a reload command in your plugin that does what you want if you need it. Maybe your issue could be gone with an internal reload command because it might be a issue with /reload command.
I'm building a chat app that pushes notification like facebook, Instagram.
when a user sends message notification will show automatically. The problem is I don't know how to handle background service.
For example, when the same user sends the message, a notification will append the message like this:
But when FCM send notification this is what happened:
I need to be able to handle background service
Here is my code
public class FirebaseInstaceService extends FirebaseMessagingService {
FirebaseToken firebaseToken = new FirebaseToken();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
#Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d("NewToken","New token: " +s);
if (user != null){
firebaseToken.sendToken(s, new OncompleteCallback() {
#Override
public void callback() {
Log.d("NewToken","Send complete");
}
});
}
}
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
try {
Log.d("CloudMessage",remoteMessage.getNotification().getBody());
}catch (Exception e){
Log.d("CloudMessage",e.toString());
throw e;
}
}
}
If I understand your question correctly, you need to do work on the non UI thread when a push notification is received. In that case, you're in luck - as explained here, onReceive() is called on a non-UI thread.
You just create and show Notifications as you see fit, storing the notification ID uniquely to allow user->notification mapping. That way, you can access the notification shown when the same user sends another message.
I have been reading for hours about different multi-threading techniques for JavaFX and cannot seem to find what I'm looking for. The application being worked on is the "Messenger" which part of a bigger application to provide a trading marketplace for a game.
A breakdown of the process I am having trouble with:
A window with a 'Contact Seller' button is displayed
The user clicks 'Contact Seller', and the Messenger window should be displayed
Using the seller's name from the main window, the Messenger should check if a chat already exists with that name
If the chat already exists, get the index of that chat in the Messenger's ListView, and select the chat so the Messenger's text area is populated with the corresponding messages
If the chat doesn't exist, create one
The issue:
Chats are stored server-side
Messages between clients are stored in a message 'cache' on the server when being processed from one user to the other (they are sorted to their respective chats, inserted into the chats, and pushed to the database upon client disconnection)
Messages received on the client are stored locally in the respective chat
When the Messenger is opened, it requests a list of chats for the signed-in user
The server sends an ArrayList of chats, and upon receiving the client builds the Messenger's ListView with these objects
Now when I need to open the Messenger in order to 'Contact Seller', I need to make sure the sync with the server is complete. Without doing so, I won't be able to properly check if a chat already exists with that name since I won't have the most up-to-date list.
The 'RequestWorker' thread that handles incoming server messages is NOT on the JavaFX thread.
The RequestWorker 'gets' the Messenger instance if it is currently open, and populates the ListView with the newly received chat list. (This needs to happen on the JavaFX thread, since I am working in the Messenger GUI)
What I am trying to do is set a static AtomicBoolean syncInProgress to true when the Messenger initiates a sync upon being displayed. When the RequestWorker receives the latest list back from the server and finishes populating the Messengers ListView, it sets syncInProgress to false.
The sync takes longer than opening the Messenger and making it check if a chat exists. Doing it this way it has no items populated yet in the ListView and the method is ineffective.
Calling a while loop to wait until the boolean is changed, blocks the JavaFX thread which means the RequestWorker cannot do what it needs to in the JavaFX thread.
How can I continuously check for this variable to be set false, then continue to 'Contact Seller' once the ListView has been properly populated?
Contact Seller method: The while loop in here causes a block on the JavaFX thread, thus not enabling RequestWorker to properly populate the ListView.
public static void contactSeller(Messenger messenger, String destination, String itemName)
{
while (TarkovTrader.syncInProgress.get())
{
; // Wait until sync is complete to check the latest chat list for an existing chat
}
if (messenger.chatExists(destination))
{
// Chat exists, select the chat for the user
for (Chat openChat : messenger.chatListView.getItems())
{
if (openChat.getName(TarkovTrader.username).equals(destination))
{
messenger.chatListView.getSelectionModel().select(openChat);
messenger.unpackChatMessages(openChat.getMessages());
break;
}
}
}
else
{
messenger.buildNewChat(destination);
}
messenger.chatInput.setText("Hey " + destination + ". Interested in your '" + itemName + "'.");
messenger.chatInput.setOnMouseClicked(e -> messenger.chatInput.clear());
}
RequestWorker process chat block:
switch(receivedFromServer)
case "chatlist":
// Client requested a chat list, results were returned from the server, and now we need to populate the messenger list
ChatListForm chatlistform = (ChatListForm)processedRequest;
if (Messenger.isOpen)
{
FutureTask<Void> updateChatList = new FutureTask(() -> {
Messenger tempMessenger = trader.getMessenger();
int currentIndex = tempMessenger.chatListView.getSelectionModel().getSelectedIndex();
tempMessenger.populate(chatlistform.getChatList());
tempMessenger.chatListView.getSelectionModel().select(currentIndex);
}, null);
Platform.runLater(updateChatList); // RequestWorker needs access to the JavaFX application thread
try {
updateChatList.get(); // Wait until the ListView has been populated before setting 'syncInProgress' to false again
}
catch (InterruptedException e) {
Alert.display(null, "Sync interrupted.");
}
catch (ExecutionException e) {
Alert.display(null, "Sync failed.");
}
TarkovTrader.syncInProgress.compareAndSet(true, false); // The value of syncInProgress should be true, change to false. Sync complete
}
else
{
Platform.runLater(() -> Alert.display(null, "New chat received."));
TarkovTrader.syncInProgress.compareAndSet(true, false);
}
break;
Contact Seller button logic: If a messenger is not open, create it and pass to static contactSeller method to use.
contactButton.setOnAction(e -> {
Messenger messenger;
if (Messenger.isOpen)
{
// Get messenger
messenger = trader.getMessenger();
}
else
{
messenger = new Messenger(worker);
messenger.display();
trader.setMessenger(messenger);
}
Messenger.contactSeller(messenger, item.getUsername(), item.getName());
itemdisplay.close();
});
EDIT:
Partially using Slaw's idea (AtomicBoolean is still being used since I'm not sure how to do this without it), this is what I came up with...
public static void contactSeller(Messenger messenger, String destination, String itemName)
{
Task<Void> waitForSync = new Task<Void>() {
#Override
public Void call()
{
while (TarkovTrader.syncInProgress.get())
{
; // Wait until sync is complete
}
return null;
}
};
waitForSync.setOnSucceeded(e -> {
while (TarkovTrader.syncInProgress.get())
{
; // Wait until sync is complete to check the latest chat list for an existing chat
}
if (messenger.chatExists(destination))
{
// Chat exists, select the chat for the user
for (Chat openChat : messenger.chatListView.getItems())
{
if (openChat.getName(TarkovTrader.username).equals(destination))
{
messenger.chatListView.getSelectionModel().select(openChat);
messenger.unpackChatMessages(openChat.getMessages());
break;
}
}
}
else
{
messenger.buildNewChat(destination);
}
messenger.chatInput.setText("Hey " + destination + ". Interested in your '" + itemName + "'.");
messenger.chatInput.setOnMouseClicked(me -> messenger.chatInput.clear());
});
Thread t = new Thread(waitForSync);
t.setDaemon(true);
t.start();
}
Which does work, but this doesn't seem like a great solution. Is it fine to do something like this or is there a preferred method over this? I feel like the while loop and using a triggered boolean is sloppy..but is this common practice?
Don't try to handle the opened chat in the same method that schedules the chat retrieval. Instead create a queue of handlers that is executed as soon as the chat is ready.
Simplified Example
public class ChatManager {
private final Map<String, Chat> openChats = new HashMap<>();
// only call from application thread
public void openChat(String user, Consumer<Chat> chatReadyHandler) {
Chat chat = openChats.computeIfAbsent(user, this::createNewChat);
chat.addReadyHandler(chatReadyHandler);
}
private Chat createNewChat(String user) {
return new Chat(user);
}
public class Chat {
// list keeping track of handlers any used for synchronisation
private final ArrayList<Consumer<Chat>> readyHandlers = new ArrayList<>(1);
private boolean ready = false;
private final String user;
public String getUser() {
return user;
}
private void addReadyHandler(Consumer<Chat> chatReadyHandler) {
synchronized (readyHandlers) {
// if already ready, immediately execute, otherwise enqueue
if (ready) {
chatReadyHandler.accept(this);
} else {
readyHandlers.add(chatReadyHandler);
}
}
}
private void chatReady() {
synchronized (readyHandlers) {
ready = true;
}
// execute all handlers on the application thread
Platform.runLater(() -> {
synchronized (readyHandlers) {
for (Consumer<Chat> readyHandler : readyHandlers) {
readyHandler.accept(this);
}
readyHandlers.clear();
readyHandlers.trimToSize();
}
});
}
private Chat(String user) {
this.user = user;
new Thread(() -> {
try {
Thread.sleep(10000); // simulate time required to acquire chat
} catch (InterruptedException ex) {
}
chatReady();
}).start();
}
}
}
The following code creates a Chat with a user when Enter is pressed after typing the user name in the TextField and prints a message to the TextArea when the chat is ready.
#Override
public void start(Stage primaryStage) throws Exception {
ChatManager worker = new ChatManager();
TextField userName = new TextField();
TextArea textArea = new TextArea();
textArea.setEditable(false);
userName.setOnAction(evt -> {
String user = userName.getText();
userName.clear();
textArea.appendText("opening chat for " + user + "\n");
worker.openChat(user, chat -> textArea.appendText("chat for " + chat.getUser() + " ready\n"));
});
Scene scene = new Scene(new VBox(10, userName, textArea));
primaryStage.setScene(scene);
primaryStage.show();
}
in my current Discord (java) bot im trying to apply a command to a user name. how can i make sure this is an actual existing user ?
in psuedo code:
if User "A" exists {
User "A" types something at all
send message "hello"+ user "A"
}
else
{
this is no valid user;
}
i can't figure out how to write the 'check if exist code'.
This is from JDA-Utilities which is a really useful tool when building discord bots.
import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;
public class Example extends Command {
public Example() {
this.name = "'isBot";
this.help = "Tells you if the user is a bot!";
}
#Override
protected void execute(CommandEvent e) {
if (e.getAuthor().isBot()) {
e.reply("Hey you're not a person!!");
} else {
e.reply("Hey " + e.getAuthor().getName() + ", you're not a bot!");
}
}
}
I'd like to broadcast a message to everyone that listen to this room, not only who join the room.
I do receive another call from server but I didn't get any call for this 'room' listener.
Server: Node.js
io.on('connection', function(socket){
console.log('connected');
socket.on('register', function(match) {
console.log(match);
var resultJSON = JSON.parse(match);
socket.join(resultJSON['roomId'], function() {
socket.broadcast.emit(resultJSON['roomId'], match);
});
});
});
Client: Java
mSocket.on(roomSelected.getId()), onSelectedRoom);
private Emitter.Listener onSelectedRoom = new Emitter.Listener() {
#Override
public void call(final Object... args) {
Platform.runLater(new Runnable() {
#Override
public void run() {
try {
JsonObject obj = new JsonParser().parse(args[0].toString()).getAsJsonObject();
System.err.println(obj);
Match match = new Gson().fromJson(obj, Match.class);
updateRoomWith(match);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
As best I know, socket.io does not have a concept of listen to a room, but don't join the room. If you want to receive messages sent to those in a room, you join the room. That's the only purpose of joining so there is no subset of joining that is just listening.
If you are trying to accomplish something that works differently than joining a room, then please describe exactly what you're trying to accomplish so we can help you solve that particular problem.