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

Commit

Permalink
Custom findtypes... wish me luck
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkKronicle committed Nov 13, 2021
1 parent 9b138a4 commit aec0402
Show file tree
Hide file tree
Showing 17 changed files with 616 additions and 105 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ build/
.gradle/
classes/
lombok.config
extra.properties
extra.properties
profanity.txt
11 changes: 11 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ jar {
from "LICENSE.txt"
}

tasks.register('downloadExtra') {
def f = new File('./src/main/resources/profanity.txt')
if (!f.exists()) {
new URL('https://raw.githubusercontent.com/surge-ai/profanity/main/profanity_en-us.csv').withInputStream{ i -> f.withOutputStream{ it << i }}
}
}

build {
dependsOn 'downloadExtra'
}

spotless {
ratchetFrom 'origin/main'
format 'misc', {
Expand Down
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.17.1
yarn_mappings=1.17.1+build.40
loader_version=0.12.3
mod_version=1.2.0
mod_version=1.3.0
maven_group=io.github.darkkronicle
archives_base_name=AdvancedChatCore
fabric_api_version=0.41.0+1.17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lwjgl.glfw.GLFW;

@Environment(EnvType.CLIENT)
Expand All @@ -45,6 +47,8 @@ public class AdvancedChatCore implements ClientModInitializer {
*/
public static boolean CREATE_SUGGESTOR = true;

public static final Logger LOGGER = LogManager.getLogger();

private static final Random RANDOM = new Random();

private static final String[] RANDOM_STRINGS = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@
import io.github.darkkronicle.advancedchatcore.chat.MessageDispatcher;
import io.github.darkkronicle.advancedchatcore.config.ConfigStorage;
import io.github.darkkronicle.advancedchatcore.config.gui.GuiConfigHandler;
import io.github.darkkronicle.advancedchatcore.finder.CustomFinder;
import io.github.darkkronicle.advancedchatcore.finder.custom.ProfanityFinder;
import io.github.darkkronicle.advancedchatcore.util.FluidText;
import io.github.darkkronicle.advancedchatcore.util.ProfanityUtil;
import io.github.darkkronicle.advancedchatcore.util.StringMatch;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

Expand Down Expand Up @@ -48,6 +56,36 @@ public void registerModHandlers() {
"advancedchat.config.tab.chatscreen",
ConfigStorage.ChatScreen.OPTIONS));

ProfanityUtil.getInstance().loadConfigs();
MessageDispatcher.getInstance()
.registerPreFilter(
text -> {
if (ConfigStorage.General.FILTER_PROFANITY.config.getBooleanValue()) {
List<StringMatch> profanity =
ProfanityUtil.getInstance().getBadWords(text.getString());
if (profanity.size() == 0) {
return Optional.empty();
}
Map<StringMatch, FluidText.StringInsert> insertions =
new HashMap<>();
for (StringMatch bad : profanity) {
insertions.put(
bad,
(current, match) ->
new FluidText(
current.withMessage(
"*"
.repeat(
bad.end
- bad.start))));
}
text.replaceStrings(insertions);
return Optional.of(text);
}
return Optional.empty();
},
-1);

// This constructs the default chat suggestor
ChatScreenSectionHolder.getInstance()
.addSectionSupplier(
Expand All @@ -57,5 +95,12 @@ public void registerModHandlers() {
}
return null;
}));

CustomFinder.getInstance()
.register(
ProfanityFinder::new,
"profanity",
"advancedchatcore.findtype.custom.profanity",
"advancedchatcore.findtype.custom.info.profanity");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ public static String translate(String key) {
"[A-Za-z0-9_§]{3,16}",
translate("info.messageownerregex")));

public static final SaveableConfig<ConfigBoolean> FILTER_PROFANITY =
SaveableConfig.fromConfig(
"filterProfanity",
new ConfigBoolean(
translate("filterprofanity"),
false,
translate("info.filterprofanity")));

public static final ImmutableList<SaveableConfig<? extends IConfigBase>> OPTIONS =
ImmutableList.of(
TIME_FORMAT,
Expand All @@ -137,7 +145,8 @@ public static String translate(String key) {
CLEAR_ON_DISCONNECT,
CHAT_STACK,
CHAT_STACK_UPDATE,
MESSAGE_OWNER_REGEX);
MESSAGE_OWNER_REGEX,
FILTER_PROFANITY);
}

public static class ChatScreen {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright (C) 2021 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.finder;

import io.github.darkkronicle.advancedchatcore.AdvancedChatCore;
import io.github.darkkronicle.advancedchatcore.interfaces.IFinder;
import io.github.darkkronicle.advancedchatcore.interfaces.RegistryOption;
import io.github.darkkronicle.advancedchatcore.util.AbstractRegistry;
import io.github.darkkronicle.advancedchatcore.util.StringMatch;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import org.apache.logging.log4j.Level;

@Environment(EnvType.CLIENT)
public class CustomFinder extends AbstractRegistry<IFinder, CustomFinder.CustomFinderOption>
implements IFinder {

private static final CustomFinder INSTANCE = new CustomFinder();

public static CustomFinder getInstance() {
return INSTANCE;
}

private CustomFinder() {}

public static final String NAME = "customfind";

@Override
public boolean isMatch(String input, String toMatch) {
Optional<IFinder> option = getFinder(toMatch);
if (option.isEmpty()) {
// Invalid :(
AdvancedChatCore.LOGGER.log(Level.WARN, getHelp(toMatch));
return false;
}
return option.get().isMatch(input, toMatch);
}

private String getHelp(String toMatch) {
StringBuilder builder =
new StringBuilder()
.append("Custom find type was used but the match ")
.append(toMatch)
.append(" does not exist in the registry! Possible correct options: ");
for (CustomFinderOption o : getAll()) {
builder.append(o.saveString).append(", ");
}
return builder.substring(0, builder.length() - 2);
}

@Override
public List<StringMatch> getMatches(String input, String toMatch) {
Optional<IFinder> option = getFinder(toMatch);
if (option.isEmpty()) {
// Invalid :(
AdvancedChatCore.LOGGER.log(Level.WARN, getHelp(toMatch));
return new ArrayList<>();
}
return option.get().getMatches(input, toMatch);
}

public Optional<IFinder> getFinder(String toMatch) {
for (CustomFinderOption o : getAll()) {
if (toMatch.startsWith(o.saveString)) {
return Optional.of(o.getOption());
}
}
return Optional.empty();
}

@Override
public CustomFinder clone() {
CustomFinder finder = new CustomFinder();
for (CustomFinderOption o : getAll()) {
finder.add(o.copy(this));
}
return finder;
}

@Override
public CustomFinderOption constructOption(
Supplier<IFinder> type,
String saveString,
String translation,
String infoTranslation,
boolean active,
boolean setDefault,
boolean hidden) {
return new CustomFinderOption(
type, saveString, translation, infoTranslation, active, hidden, this);
}

public static class CustomFinderOption implements RegistryOption<IFinder> {

private final IFinder finder;
private final String saveString;
private final String translation;
private final CustomFinder registry;
private final String infoTranslation;
private final boolean hidden;
private final boolean active;

private CustomFinderOption(
Supplier<IFinder> finder,
String saveString,
String translation,
String infoTranslation,
boolean active,
boolean hidden,
CustomFinder registry) {
this(finder.get(), saveString, translation, infoTranslation, active, hidden, registry);
}

// Only register
private CustomFinderOption(
IFinder finder,
String saveString,
String translation,
String infoTranslation,
boolean active,
boolean hidden,
CustomFinder registry) {
this.finder = finder;
this.saveString = saveString;
this.translation = translation;
this.registry = registry;
this.infoTranslation = infoTranslation;
this.hidden = hidden;
this.active = active;
}

@Override
public IFinder getOption() {
return finder;
}

@Override
public boolean isActive() {
return active;
}

@Override
public String getSaveString() {
return saveString;
}

@Override
public CustomFinderOption copy(AbstractRegistry<IFinder, ?> registry) {
return new CustomFinderOption(
finder,
saveString,
translation,
infoTranslation,
isActive(),
isHidden(),
registry == null ? this.registry : (CustomFinder) registry);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (C) 2021 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.finder;

import java.util.regex.Pattern;

public class LiteralFinder extends PatternFinder {
@Override
public Pattern getPattern(String toMatch) {
return Pattern.compile(Pattern.quote(toMatch));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2021 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.finder;

import io.github.darkkronicle.advancedchatcore.interfaces.IFinder;
import io.github.darkkronicle.advancedchatcore.util.StringMatch;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class PatternFinder implements IFinder {

public abstract Pattern getPattern(String toMatch);

@Override
public boolean isMatch(String input, String toMatch) {
Pattern pattern = Pattern.compile(Pattern.quote(toMatch));
return pattern.matcher(input).find();
}

@Override
public List<StringMatch> getMatches(String input, String toMatch) {
Pattern pattern = Pattern.compile(Pattern.quote(toMatch));
Matcher matcher = pattern.matcher(input);
List<StringMatch> matches = new ArrayList<>();
while (matcher.find()) {
matches.add(new StringMatch(matcher.group(), matcher.start(), matcher.end()));
}
matcher.reset();
return matches;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (C) 2021 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.finder;

import java.util.regex.Pattern;

public class RegexFinder extends PatternFinder {

@Override
public Pattern getPattern(String toMatch) {
return Pattern.compile(toMatch);
}
}
Loading

0 comments on commit aec0402

Please sign in to comment.