Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
Adding ChatHistory updates
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkKronicle committed Aug 14, 2021
1 parent 7b9a7f0 commit 80a9cf2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.darkkronicle.advancedchatcore.chat;

import io.github.darkkronicle.advancedchatcore.config.ConfigStorage;
import io.github.darkkronicle.advancedchatcore.interfaces.IChatMessageProcessor;
import lombok.Getter;
import lombok.Setter;
import net.fabricmc.api.EnvType;
Expand All @@ -9,6 +10,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
* A utility class to maintain the storage of the chat.
Expand All @@ -29,10 +31,7 @@ public class ChatHistory {
private final List<Runnable> onClear = new ArrayList<>();

@Getter
private final List<Consumer<ChatMessage>> onMessage = new ArrayList<>();

@Getter
private final List<Consumer<ChatMessage>> onStack = new ArrayList<>();
private final List<IChatMessageProcessor> onUpdate = new ArrayList<>();

public static ChatHistory getInstance() {
return INSTANCE;
Expand All @@ -51,20 +50,14 @@ public void addOnClear(Runnable runnable) {
}

/**
* Add's a consumer that will accept a ChatMessage when it's added. This will not get triggered for stacks.
* @param consumer Consumer to take ChatMessage
* Add's a {@link IChatMessageProcessor} that get's called on new messages, added messages, stacked messages,
* or removed messages.
* @param processor Processor ot add
*/
public void addOnMessage(Consumer<ChatMessage> consumer) {
onMessage.add(consumer);
public void addOnUpdate(IChatMessageProcessor processor) {
onUpdate.add(processor);
}

/**
* Add's a consumer that will accept a ChatMessage when it's been stacked.
* @param consumer Consumer to take ChatMessage
*/
public void addOnStack(Consumer<ChatMessage> consumer) {
onStack.add(consumer);
}

/**
* Goes through and clears all message data from everywhere.
Expand All @@ -83,27 +76,30 @@ public void clear() {
messages.clear();
}

private void sendUpdate(ChatMessage message, IChatMessageProcessor.UpdateType type) {
for (IChatMessageProcessor consumer : onUpdate) {
consumer.onMessageUpdate(message, type);
}
}

/**
* Add's a chat message to the history.
* @param message
*/
public boolean add(ChatMessage message) {
sendUpdate(message, IChatMessageProcessor.UpdateType.NEW);
for (int i = 0; i < ConfigStorage.General.CHAT_STACK.config.getIntegerValue() && i < messages.size(); i++) {
ChatMessage chatLine = messages.get(i);
if (message.isSimilar(chatLine)) {
chatLine.setStacks(chatLine.getStacks() + 1);
for (Consumer<ChatMessage> consumer : onStack) {
consumer.accept(chatLine);
}
sendUpdate(chatLine, IChatMessageProcessor.UpdateType.STACK);
return false;
}
}
sendUpdate(message, IChatMessageProcessor.UpdateType.ADDED);
messages.add(0, message);
while (this.messages.size() > maxLines) {
this.messages.remove(this.messages.size() - 1);
}
for (Consumer<ChatMessage> consumer : onMessage) {
consumer.accept(message);
sendUpdate(this.messages.remove(this.messages.size() - 1), IChatMessageProcessor.UpdateType.REMOVE);
}
return true;
}
Expand All @@ -113,7 +109,11 @@ public boolean add(ChatMessage message) {
* @param messageId Message ID to find and remove
*/
public void removeMessage(int messageId) {
this.messages.removeIf(line -> line.getId() == messageId);
List<ChatMessage> toRemove = this.messages.stream().filter(line -> line.getId() == messageId).collect(Collectors.toList());
this.messages.removeAll(toRemove);
for (ChatMessage m : toRemove) {
sendUpdate(m, IChatMessageProcessor.UpdateType.REMOVE);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.github.darkkronicle.advancedchatcore.interfaces;

import io.github.darkkronicle.advancedchatcore.chat.ChatMessage;

/**
* A processor that will get updated whenever an event in {@link io.github.darkkronicle.advancedchatcore.chat.ChatHistory} happens.
* This gets triggered for specific {@link ChatMessage}.
*/
public interface IChatMessageProcessor {

/**
* Types of chat message events that can be referenced
*/
enum UpdateType {
/**
* A new message is sent. This still gets triggered even if the message is stacked.
*/
NEW,

/**
* A message is added to the history. This does not get called on stack.
*/
ADDED,

/**
* A previous message has been stacked. The stack number has changed.
*/
STACK,

/**
* A message is removed. This does not get called on clear, but only if a specific message was called to remove.
*/
REMOVE
}

/**
* A method to handle a {@link ChatMessage} update.
* @param message Message that was updated
* @param type Type of the update
*/
void onMessageUpdate(ChatMessage message, UpdateType type);

}

0 comments on commit 80a9cf2

Please sign in to comment.