This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0401b92
commit 0c031f1
Showing
4 changed files
with
73 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/io/github/darkkronicle/advancedchatcore/chat/MessageSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/io/github/darkkronicle/advancedchatcore/interfaces/IStringFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |