mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-03 06:28:49 +00:00
More implementation, timed teleports, regex, etc
This commit is contained in:
1 parent
7ac52d747f
commit
e335a48888
25 files changed
+803
-157
No files matched your search
@@ -0,0 +1,97 @@
|
||||
package net.william278.huskhomes.config;
|
||||
|
||||
import de.themoep.minedown.MineDown;
|
||||
import dev.dejvokep.boostedyaml.YamlDocument;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Loaded locales used by the plugin to display various locales
|
||||
*/
|
||||
public class Locales {
|
||||
|
||||
@NotNull
|
||||
private final HashMap<String, String> rawLocales;
|
||||
|
||||
private Locales(YamlDocument localesConfig) {
|
||||
this.rawLocales = new HashMap<>();
|
||||
for (String localeId : localesConfig.getRoutesAsStrings(false)) {
|
||||
rawLocales.put(localeId, localesConfig.getString(localeId));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an unformatted locale loaded from the locales file
|
||||
*
|
||||
* @param localeId String identifier of the locale, corresponding to a key in the file
|
||||
* @return An {@link Optional} containing the locale corresponding to the id, if it exists
|
||||
*/
|
||||
public Optional<String> getRawLocale(String localeId) {
|
||||
if (rawLocales.containsKey(localeId)) {
|
||||
return Optional.of(rawLocales.get(localeId));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an unformatted locale loaded from the locales file, with replacements applied
|
||||
*
|
||||
* @param localeId String identifier of the locale, corresponding to a key in the file
|
||||
* @param replacements Ordered array of replacement strings to fill in placeholders with
|
||||
* @return An {@link Optional} containing the replacement-applied locale corresponding to the id, if it exists
|
||||
*/
|
||||
public Optional<String> getRawLocale(String localeId, String... replacements) {
|
||||
return getRawLocale(localeId).map(locale -> applyReplacements(locale, replacements));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a MineDown-formatted locale from the locales file
|
||||
*
|
||||
* @param localeId String identifier of the locale, corresponding to a key in the file
|
||||
* @return An {@link Optional} containing the formatted locale corresponding to the id, if it exists
|
||||
*/
|
||||
public Optional<MineDown> getLocale(String localeId) {
|
||||
return getRawLocale(localeId).map(MineDown::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a MineDown-formatted locale from the locales file, with replacements applied
|
||||
*
|
||||
* @param localeId String identifier of the locale, corresponding to a key in the file
|
||||
* @param replacements Ordered array of replacement strings to fill in placeholders with
|
||||
* @return An {@link Optional} containing the replacement-applied, formatted locale corresponding to the id, if it exists
|
||||
*/
|
||||
public Optional<MineDown> getLocale(String localeId, String... replacements) {
|
||||
return getRawLocale(localeId, replacements).map(MineDown::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply placeholder replacements to a raw locale
|
||||
*
|
||||
* @param rawLocale The raw, unparsed locale
|
||||
* @param replacements Ordered array of replacement strings to fill in placeholders with
|
||||
* @return the raw locale, with inserted placeholders
|
||||
*/
|
||||
private String applyReplacements(String rawLocale, String... replacements) {
|
||||
int replacementIndexer = 1;
|
||||
for (String replacement : replacements) {
|
||||
String replacementString = "%" + replacementIndexer + "%";
|
||||
rawLocale = rawLocale.replace(replacementString, replacement);
|
||||
replacementIndexer = replacementIndexer + 1;
|
||||
}
|
||||
return rawLocale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the locales from a BoostedYaml {@link YamlDocument} locales file
|
||||
*
|
||||
* @param localesConfig The loaded {@link YamlDocument} locales.yml file
|
||||
* @return the loaded {@link Locales}
|
||||
*/
|
||||
public static Locales load(YamlDocument localesConfig) {
|
||||
return new Locales(localesConfig);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
package net.william278.huskhomes.config;
|
||||
|
||||
import de.themoep.minedown.MineDown;
|
||||
import dev.dejvokep.boostedyaml.YamlDocument;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Loaded locales used by the plugin to display various messages
|
||||
*/
|
||||
public class Messages {
|
||||
|
||||
@NotNull
|
||||
private final HashMap<String, String> rawMessages;
|
||||
|
||||
private Messages(YamlDocument messagesConfig) {
|
||||
this.rawMessages = new HashMap<>();
|
||||
for (String messageId : messagesConfig.getRoutesAsStrings(false)) {
|
||||
rawMessages.put(messageId, messagesConfig.getString(messageId));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an unformatted message loaded from the messages file
|
||||
*
|
||||
* @param messageId String identifier of the message, corresponding to a key in the file
|
||||
* @return An {@link Optional} containing the message corresponding to the id, if it exists
|
||||
*/
|
||||
public Optional<String> getRawMessage(String messageId) {
|
||||
if (rawMessages.containsKey(messageId)) {
|
||||
return Optional.of(rawMessages.get(messageId));
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an unformatted message loaded from the messages file, with replacements applied
|
||||
*
|
||||
* @param messageId String identifier of the message, corresponding to a key in the file
|
||||
* @param replacements Ordered array of replacement strings to fill in placeholders with
|
||||
* @return An {@link Optional} containing the replacement-applied message corresponding to the id, if it exists
|
||||
*/
|
||||
public Optional<String> getRawMessage(String messageId, String... replacements) {
|
||||
return getRawMessage(messageId).map(message -> applyReplacements(message, replacements));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a MineDown-formatted message from the messages file
|
||||
*
|
||||
* @param messageId String identifier of the message, corresponding to a key in the file
|
||||
* @return An {@link Optional} containing the formatted message corresponding to the id, if it exists
|
||||
*/
|
||||
public Optional<MineDown> getMessage(String messageId) {
|
||||
return getRawMessage(messageId).map(MineDown::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a MineDown-formatted message from the messages file, with replacements applied
|
||||
*
|
||||
* @param messageId String identifier of the message, corresponding to a key in the file
|
||||
* @param replacements Ordered array of replacement strings to fill in placeholders with
|
||||
* @return An {@link Optional} containing the replacement-applied, formatted message corresponding to the id, if it exists
|
||||
*/
|
||||
public Optional<MineDown> getMessage(String messageId, String... replacements) {
|
||||
return getRawMessage(messageId, replacements).map(MineDown::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply placeholder replacements to a raw message
|
||||
*
|
||||
* @param rawMessage The raw, unparsed message
|
||||
* @param replacements Ordered array of replacement strings to fill in placeholders with
|
||||
* @return the raw message, with inserted placeholders
|
||||
*/
|
||||
private String applyReplacements(String rawMessage, String... replacements) {
|
||||
int replacementIndexer = 1;
|
||||
for (String replacement : replacements) {
|
||||
String replacementString = "%" + replacementIndexer + "%";
|
||||
rawMessage = rawMessage.replace(replacementString, replacement);
|
||||
replacementIndexer = replacementIndexer + 1;
|
||||
}
|
||||
return rawMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the messages from a BoostedYaml {@link YamlDocument} messages file
|
||||
*
|
||||
* @param messagesConfig The loaded {@link YamlDocument} messages.yml file
|
||||
* @return the loaded {@link Messages}
|
||||
*/
|
||||
public static Messages load(YamlDocument messagesConfig) {
|
||||
return new Messages(messagesConfig);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user