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

Commit

Permalink
Adding pre-send filters
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkKronicle committed Jan 9, 2022
1 parent 0401b92 commit 0c031f1
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 8 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
minecraft_version=1.18
yarn_mappings=1.18+build.1
loader_version=0.12.5
mod_version=1.3.3
mod_version=1.3.4
maven_group=io.github.darkkronicle
archives_base_name=AdvancedChatCore
fabric_api_version=0.43.1+1.18
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 DarkKronicle
* Copyright (C) 2021-2022 DarkKronicle
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -168,12 +168,7 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (keyCode == KeyCodes.KEY_ENTER || keyCode == KeyCodes.KEY_KP_ENTER) {
String string = this.chatField.getText().trim();
// Strip message and send
if (!string.isEmpty()) {
if (string.length() > 256) {
string = string.substring(0, 256);
}
this.sendMessage(string);
}
MessageSender.getInstance().sendMessage(string);
this.chatField.setText("");
last = "";
// Exit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2022 DarkKronicle
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package io.github.darkkronicle.advancedchatcore.chat;

import io.github.darkkronicle.advancedchatcore.interfaces.IStringFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import net.minecraft.client.MinecraftClient;

public class MessageSender {

private static final MessageSender INSTANCE = new MessageSender();
private final MinecraftClient client = MinecraftClient.getInstance();

public static MessageSender getInstance() {
return INSTANCE;
}

private MessageSender() {}

private final List<IStringFilter> filters = new ArrayList<>();

public void addFilter(IStringFilter filter) {
filters.add(filter);
}

public void addFilter(IStringFilter filter, int index) {
filters.add(index, filter);
}

public void sendMessage(String string) {
String unfiltered = string;
for (IStringFilter filter : filters) {
Optional<String> filtered = filter.filter(string);
if (filtered.isPresent()) {
string = filtered.get();
}
}
if (string.length() > 256) {
string = string.substring(0, 256);
}
this.client.inGameHud.getChatHud().addToMessageHistory(unfiltered);

if (client.player != null) {
this.client.player.sendChatMessage(string);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (C) 2022 DarkKronicle
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package io.github.darkkronicle.advancedchatcore.interfaces;

import java.util.Optional;

/** An interface to modify raw string */
public interface IStringFilter {

Optional<String> filter(String input);
}

0 comments on commit 0c031f1

Please sign in to comment.