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
9b138a4
commit aec0402
Showing
17 changed files
with
616 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ build/ | |
.gradle/ | ||
classes/ | ||
lombok.config | ||
extra.properties | ||
extra.properties | ||
profanity.txt |
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
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
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
168 changes: 168 additions & 0 deletions
168
src/main/java/io/github/darkkronicle/advancedchatcore/finder/CustomFinder.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,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); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/io/github/darkkronicle/advancedchatcore/finder/LiteralFinder.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,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)); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/io/github/darkkronicle/advancedchatcore/finder/PatternFinder.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,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; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/io/github/darkkronicle/advancedchatcore/finder/RegexFinder.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,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); | ||
} | ||
} |
Oops, something went wrong.