diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 325bd7a3..74a8c6da 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -11,7 +11,7 @@ on: - '**/build.gradle' - '**/gradle.properties' - '**/settings.gradle' - - .github/workflows/build.yml + - .github/workflows/gradle.yml - LICENCE pull_request: @@ -21,7 +21,7 @@ on: - '**/build.gradle' - '**/gradle.properties' - '**/settings.gradle' - - .github/workflows/build.yml + - .github/workflows/gradle.yml - LICENCE jobs: @@ -53,7 +53,7 @@ jobs: restore-keys: | ${{ runner.os }}-gradle- - name: 🔨 Build artifacts - run: gradle -b build-github-action.gradle clean build + run: gradle -b build-github-action.gradle -c settings-github-action.gradle clean build - name: 📦 Upload artifacts uses: actions/upload-artifact@v2 diff --git a/build.gradle b/build.gradle index 7e9435f8..ffd044d8 100644 --- a/build.gradle +++ b/build.gradle @@ -41,7 +41,7 @@ allprojects { include "me.lucko:fabric-permissions-api:${project.permissions_api_version}" modApi 'eu.pb4:placeholder-api-ec:1.0.1+1.16' include 'eu.pb4:placeholder-api-ec:1.0.1+1.16' - modImplementation group: 'net.devtech', name: 'grossfabrichacks', version: project.gfh_version + modApi group: 'net.devtech', name: 'grossfabrichacks', version: project.gfh_version include group: 'net.devtech', name: 'grossfabrichacks', version: project.gfh_version modImplementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.12.2' include 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.12.2' diff --git a/ec-core/README.md b/ec-core/README.md new file mode 100644 index 00000000..e69de29b diff --git a/ec-core/build.gradle b/ec-core/build.gradle new file mode 100644 index 00000000..abe6f334 --- /dev/null +++ b/ec-core/build.gradle @@ -0,0 +1,2 @@ +// Declare dependencies +dependencies {} diff --git a/ec-core/gradle.properties b/ec-core/gradle.properties new file mode 100644 index 00000000..afc87ca3 --- /dev/null +++ b/ec-core/gradle.properties @@ -0,0 +1,4 @@ +# Mod +mod_name = Essential Commands Core +mod_id = ec-core +mod_version = 1.0.0 diff --git a/ec-core/src/main/java/dev/jpcode/eccore/ECCore.java b/ec-core/src/main/java/dev/jpcode/eccore/ECCore.java new file mode 100644 index 00000000..f2449d6e --- /dev/null +++ b/ec-core/src/main/java/dev/jpcode/eccore/ECCore.java @@ -0,0 +1,9 @@ +package dev.jpcode.eccore; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class ECCore { + public static final Logger LOGGER = LogManager.getLogger("ec-core"); + +} diff --git a/ec-core/src/main/java/dev/jpcode/eccore/config/Config.java b/ec-core/src/main/java/dev/jpcode/eccore/config/Config.java new file mode 100644 index 00000000..4eb44d7b --- /dev/null +++ b/ec-core/src/main/java/dev/jpcode/eccore/config/Config.java @@ -0,0 +1,152 @@ +package dev.jpcode.eccore.config; + +import net.minecraft.text.LiteralText; +import net.minecraft.text.MutableText; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +public abstract class Config { + static final Logger LOGGER = LogManager.getLogger("ec-core-config"); + + protected SortedProperties props; + private final Path configPath; + private final String displayName; + private final String documentationLink; + + public Config(Path savePath, String displayName, String documentationLink) { + this.configPath = savePath; + this.displayName = displayName; + this.documentationLink = documentationLink; + initFieldStorage(); + } + + public void loadOrCreateProperties() { + props = new SortedProperties(); + File inFile = configPath.toFile(); + + try { + boolean fileAlreadyExisted = !inFile.createNewFile(); + if (fileAlreadyExisted) { + props.load(new FileReader(inFile)); + } + } catch (IOException e) { + LOGGER.warn("Failed to load preferences."); + } + initProperties(); + storeProperties(); + } + + private void initProperties() { + Class cls = this.getClass(); + // Cursed reflection reloading of all properties. + Arrays.stream(cls.getDeclaredFields()) + .filter(field -> field.isAnnotationPresent(ConfigOption.class)) + .forEach(field -> { + try { + ((Option) field.get(this)).loadAndSave(props); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + }); + } + + public void storeProperties() { + try { + File outFile = configPath.toFile(); + FileWriter writer = new FileWriter(outFile); + + props.storeSorted(writer, new StringBuilder(80) + .append(displayName) + .append("\n") + .append("Config Documentation: ") + .append(documentationLink) + .toString() + ); + } catch (IOException e) { + LOGGER.warn("Failed to store preferences to disk."); + } + + } + + static final Style DEFAULT_STYLE = Style.EMPTY.withFormatting(Formatting.GOLD); + static final Style ACCENT_STYLE = Style.EMPTY.withFormatting(Formatting.GREEN); + + public @NotNull Text stateAsText() { + LiteralText result = new LiteralText(""); + String newLine = "\n";//System.getProperty("line.separator"); + + result.append(new LiteralText(displayName + " {").setStyle(DEFAULT_STYLE)); + result.append(newLine); + LiteralText propsText = new LiteralText(""); + result.append(propsText); + + //print field names paired with their values + for (Field field : publicFields) { + try { + if (Modifier.isPublic(field.getModifiers())) { + propsText.append(fieldAsText(field).append(newLine)); + } + } catch (IllegalAccessException ex) { + ex.printStackTrace(); + } + } + result.append(new LiteralText("}").setStyle(ACCENT_STYLE)); + + return result; + + } + + private List publicFieldNames; + private List publicFields; + + private void initFieldStorage() { + Class cls = this.getClass(); + publicFieldNames = Arrays.stream(cls.getDeclaredFields()) + .filter(field -> Modifier.isPublic(field.getModifiers())) + .map(Field::getName) + .sorted() + .collect(Collectors.toList()); + publicFields = Arrays.stream(cls.getDeclaredFields()) + .filter(field -> Modifier.isPublic(field.getModifiers())) + .sorted(Comparator.comparing(Field::getName)) + .collect(Collectors.toList()); + + } + + public List getPublicFieldNames() { + return publicFieldNames; + } + + private MutableText fieldAsText(Field field) throws IllegalAccessException { + return new LiteralText("") + .append(new LiteralText(field.getName() + ": ").setStyle(DEFAULT_STYLE)) + .append(new LiteralText(field.get(this.getClass()).toString())); + } + + public @Nullable MutableText getFieldValueAsText(String fieldName) throws NoSuchFieldException { + try { + return fieldAsText(this.getClass().getField(fieldName)); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/ec-core/src/main/java/dev/jpcode/eccore/config/ConfigOption.java b/ec-core/src/main/java/dev/jpcode/eccore/config/ConfigOption.java new file mode 100644 index 00000000..1b30e6a0 --- /dev/null +++ b/ec-core/src/main/java/dev/jpcode/eccore/config/ConfigOption.java @@ -0,0 +1,11 @@ +package dev.jpcode.eccore.config; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface ConfigOption { +} diff --git a/ec-core/src/main/java/dev/jpcode/eccore/config/ConfigUtil.java b/ec-core/src/main/java/dev/jpcode/eccore/config/ConfigUtil.java new file mode 100644 index 00000000..8c4fe949 --- /dev/null +++ b/ec-core/src/main/java/dev/jpcode/eccore/config/ConfigUtil.java @@ -0,0 +1,137 @@ +package dev.jpcode.eccore.config; + +import com.google.gson.JsonParser; +import com.google.gson.JsonSyntaxException; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; +import org.apache.logging.log4j.Level; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import static dev.jpcode.eccore.config.Config.LOGGER; +import static dev.jpcode.eccore.util.TextUtil.parseText; + +/** + * Various parsers, etc. + */ +public final class ConfigUtil { + + private ConfigUtil() {} + + // TODO do not delclair serializer objects out here. Pretty sure is bad for concurrent parsing. + private static final Style.Serializer styleJsonDeserializer = new Style.Serializer(); + private static final JsonParser jsonParser = new JsonParser(); + + public static Style parseStyleOrDefault(String styleStr, String defaultStyleStr) { + Style outStyle = null; + if (styleStr != null) { + outStyle = parseStyle(styleStr); + } + + if (outStyle == null) { + outStyle = parseStyle(defaultStyleStr); + LOGGER.log( + Level.WARN, + String.format("Could not load malformed style: '%s'. Using default, '%s'.", styleStr, defaultStyleStr) + ); + } + return outStyle; + } + + public static Style parseStyle(String styleStr) { + Style outStyle = null; + Formatting formatting = Formatting.byName(styleStr); + if (formatting != null) { + outStyle = Style.EMPTY.withFormatting(formatting); + } + + if (outStyle == null) { + try { + outStyle = styleJsonDeserializer.deserialize( + jsonParser.parse(styleStr), + null, null + ); + } catch (JsonSyntaxException e) { + LOGGER.log(Level.ERROR, String.format( + "Malformed Style JSON in config: %s", styleStr + )); +// e.printStackTrace(); + } + + } + + return outStyle; + } + + public static Text parseTextOrDefault(String textStr, String defaultTextStr) { + Text outText = null; + if (textStr != null) { + outText = parseText(textStr); + } + + if (outText == null) { + outText = parseText(defaultTextStr); + LOGGER.log( + Level.WARN, + String.format("Could not load malformed Text: '%s'. Using default, '%s'.", textStr, defaultTextStr) + ); + } + return outText; + } + + public static int parseInt(String s) { + try { + return Integer.parseInt(s); + } catch (NumberFormatException e) { + logNumberParseError(s, "int"); + } + return -1; + } + + public static double parseDouble(String s) { + try { + return Double.parseDouble(s); + } catch (NumberFormatException e) { + logNumberParseError(s, "double"); + } + return -1; + } + + @Contract(pure = true) + public static @NotNull ValueParser> csvParser(ValueParser valueParser) { + return (String value) -> parseCsv(value, valueParser); + } + + public static List parseCsv(@NotNull String csvString, @NotNull ValueParser valueParser) { + return Arrays.stream(csvString.split(",")).sequential().map(String::trim) + .map(valueParser::parseValue).collect(Collectors.toList()); + } + + @Contract(pure = true) + public static @NotNull ValueParser> arrayParser(ValueParser valueParser) { + return (String value) -> parseArray(value, valueParser); + } + + public static List parseArray(@NotNull String arrayString, @NotNull ValueParser valueParser) { + int endIdx = arrayString.indexOf(']'); + return parseCsv( + arrayString.substring(arrayString.indexOf('[') + 1, endIdx == -1 ? arrayString.length() : endIdx), + valueParser + ); + } + + private static void logNumberParseError(String num, String type) { + Config.LOGGER.log(Level.WARN, String.format( + "Invalid number format for type '%s' in config. Value provided: '%s'", type, num + )); + } + + public static String serializeStyle(Style style) { + return String.valueOf(styleJsonDeserializer.serialize(style, null, null)); + } +} diff --git a/src/main/java/com/fibermc/essentialcommands/config/Option.java b/ec-core/src/main/java/dev/jpcode/eccore/config/Option.java similarity index 58% rename from src/main/java/com/fibermc/essentialcommands/config/Option.java rename to ec-core/src/main/java/dev/jpcode/eccore/config/Option.java index 3ecfa499..62eda646 100644 --- a/src/main/java/com/fibermc/essentialcommands/config/Option.java +++ b/ec-core/src/main/java/dev/jpcode/eccore/config/Option.java @@ -1,30 +1,36 @@ -package com.fibermc.essentialcommands.config; +package dev.jpcode.eccore.config; -import com.fibermc.essentialcommands.events.OptionChangedCallback; import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; import java.util.Properties; +import java.util.function.Consumer; public class Option { private final String key; private final T defaultValue; private final ValueParser parser; + private final StringSerializer serializer; private T value; - public final Event> CHANGE_EVENT = EventFactory.createArrayBacked(OptionChangedCallback.class, + public final Event> changeEvent = EventFactory.createArrayBacked(Consumer.class, (listeners) -> (newValue) -> { - for (OptionChangedCallback event : listeners) { - event.onOptionChanged(newValue); + for (Consumer event : listeners) { + event.accept(newValue); } } ); - public Option(String key, T defaultValue, ValueParser parser) { + public Option(String key, T defaultValue, ValueParser parser, StringSerializer serializer) { this.key = key; this.defaultValue = defaultValue; this.parser = parser; + this.serializer = serializer; + } + + public Option(String key, T defaultValue, ValueParser parser) { + this(key, defaultValue, parser, String::valueOf); } public Option loadAndSave(Properties props) { @@ -35,15 +41,15 @@ public Option loadAndSave(Properties props) { public Option loadFrom(Properties props) { T prevValue = this.value; - this.value = parser.parseValue(String.valueOf(props.getOrDefault(this.key, String.valueOf(this.defaultValue)))); - if (!(prevValue == null || prevValue.equals(this.value))) { - CHANGE_EVENT.invoker().onOptionChanged(this.value); + this.value = parser.parseValue(String.valueOf(props.getOrDefault(this.key, serializer.serialize(this.defaultValue)))); + if (!this.value.equals(prevValue)) { + changeEvent.invoker().accept(this.value); } return this; } public void saveIfAbsent(Properties props) { - props.putIfAbsent(this.key, String.valueOf(this.value)); + props.putIfAbsent(this.key, serializer.serialize(this.value)); } public T getValue() { diff --git a/src/main/java/com/fibermc/essentialcommands/config/SortedProperties.java b/ec-core/src/main/java/dev/jpcode/eccore/config/SortedProperties.java similarity index 97% rename from src/main/java/com/fibermc/essentialcommands/config/SortedProperties.java rename to ec-core/src/main/java/dev/jpcode/eccore/config/SortedProperties.java index 909cc3d8..84c76839 100644 --- a/src/main/java/com/fibermc/essentialcommands/config/SortedProperties.java +++ b/ec-core/src/main/java/dev/jpcode/eccore/config/SortedProperties.java @@ -1,4 +1,4 @@ -package com.fibermc.essentialcommands.config; +package dev.jpcode.eccore.config; import org.jetbrains.annotations.NotNull; diff --git a/ec-core/src/main/java/dev/jpcode/eccore/config/StringSerializer.java b/ec-core/src/main/java/dev/jpcode/eccore/config/StringSerializer.java new file mode 100644 index 00000000..672a105b --- /dev/null +++ b/ec-core/src/main/java/dev/jpcode/eccore/config/StringSerializer.java @@ -0,0 +1,6 @@ +package dev.jpcode.eccore.config; + +public interface StringSerializer { + + String serialize(T value); +} diff --git a/src/main/java/com/fibermc/essentialcommands/config/ValueParser.java b/ec-core/src/main/java/dev/jpcode/eccore/config/ValueParser.java similarity index 66% rename from src/main/java/com/fibermc/essentialcommands/config/ValueParser.java rename to ec-core/src/main/java/dev/jpcode/eccore/config/ValueParser.java index 95c51955..a7eb4839 100644 --- a/src/main/java/com/fibermc/essentialcommands/config/ValueParser.java +++ b/ec-core/src/main/java/dev/jpcode/eccore/config/ValueParser.java @@ -1,4 +1,4 @@ -package com.fibermc.essentialcommands.config; +package dev.jpcode.eccore.config; @FunctionalInterface public interface ValueParser { diff --git a/ec-core/src/main/java/dev/jpcode/eccore/util/NbtUtil.java b/ec-core/src/main/java/dev/jpcode/eccore/util/NbtUtil.java new file mode 100644 index 00000000..df7b8b84 --- /dev/null +++ b/ec-core/src/main/java/dev/jpcode/eccore/util/NbtUtil.java @@ -0,0 +1,5 @@ +package dev.jpcode.eccore.util; + +public class NbtUtil { + +} diff --git a/ec-core/src/main/java/dev/jpcode/eccore/util/StringBuilderPlus.java b/ec-core/src/main/java/dev/jpcode/eccore/util/StringBuilderPlus.java new file mode 100644 index 00000000..faa57440 --- /dev/null +++ b/ec-core/src/main/java/dev/jpcode/eccore/util/StringBuilderPlus.java @@ -0,0 +1,27 @@ +package dev.jpcode.eccore.util; + +public class StringBuilderPlus { + + private final StringBuilder sb; + + public StringBuilderPlus(){ + sb = new StringBuilder(); + } + + public StringBuilderPlus append(String str) + { + sb.append(str != null ? str : ""); + return this; + } + + public StringBuilderPlus appendLine(String str) + { + sb.append(str != null ? str : "").append(System.getProperty("line.separator")); + return this; + } + + public String toString() + { + return sb.toString(); + } +} \ No newline at end of file diff --git a/ec-core/src/main/java/dev/jpcode/eccore/util/StringToTextParser.java b/ec-core/src/main/java/dev/jpcode/eccore/util/StringToTextParser.java new file mode 100644 index 00000000..282191e6 --- /dev/null +++ b/ec-core/src/main/java/dev/jpcode/eccore/util/StringToTextParser.java @@ -0,0 +1,9 @@ +package dev.jpcode.eccore.util; + +import net.minecraft.text.Text; + +@FunctionalInterface +public interface StringToTextParser { + + Text parseText(String str); +} diff --git a/ec-core/src/main/java/dev/jpcode/eccore/util/TextUtil.java b/ec-core/src/main/java/dev/jpcode/eccore/util/TextUtil.java new file mode 100644 index 00000000..eb7b616b --- /dev/null +++ b/ec-core/src/main/java/dev/jpcode/eccore/util/TextUtil.java @@ -0,0 +1,198 @@ +package dev.jpcode.eccore.util; + +import com.google.gson.JsonParseException; +import dev.jpcode.eccore.ECCore; +import eu.pb4.placeholders.TextParser; +import net.minecraft.text.*; +import org.apache.logging.log4j.Level; + +import java.util.ArrayList; +import java.util.Collection; + +public class TextUtil { + + + public static MutableText concat(Text... arr) { + MutableText out = new LiteralText(""); + for (Text text : arr) { + out.append(text); + } + return out; + } +/** + *

Joins the elements of the provided array into a single Text + * containing the provided list of elements.

+ * + *

No delimiter is added before or after the list. + * Null objects or empty strings within the array are represented by + * empty strings.

+ * + *
+  * StringUtils.join(null, *)               = null
+  * StringUtils.join([], *)                 = ""
+  * StringUtils.join([null], *)             = ""
+  * StringUtils.join(["a", "b", "c"], ';')  = "a;b;c"
+  * StringUtils.join(["a", "b", "c"], null) = "abc"
+  * StringUtils.join([null, "", "a"], ';')  = ";;a"
+  * 
+ * + * @param array the array of values to join together, may be null + * @param separator the separator character to use + * @return the joined String, null if null array input + * @since 2.0 + */ + public static Text join(Text[] array, Text separator) { + if (array == null) { + return null; + } + return join(array, separator, 0, array.length); + } + public static Text join(Collection textCollection, Text separator) { + if (textCollection == null) { + return null; + } + return join(textCollection.toArray(new Text[0]), separator, 0, textCollection.size()); + } + public static Text join(Collection stringCollection, Text separator, Style stringsFormatting) { + if (stringCollection == null) { + return null; + } + return join( + stringCollection.stream().map(str -> new LiteralText(str).setStyle(stringsFormatting)).toArray(Text[]::new), + separator, 0, stringCollection.size() + ); + } + + public static String joinStrings(Collection stringCollection, String separator) { + if (stringCollection == null) { + return null; + } + return joinStrings( + stringCollection.toArray(String[]::new), + separator, 0, stringCollection.size() + ); + } + + public static String joinStrings(String[] array, String separator, int startIndex, int endIndex) { + if (array == null) { + return null; + } + int bufSize = (endIndex - startIndex); + if (bufSize <= 0) { + return null; + } + StringBuilder buf = new StringBuilder(); + for (int i = startIndex; i < endIndex; i++) { + if (i > startIndex) { + buf.append(separator); + } + if (array[i] != null) { + buf.append(array[i]); + } + } + return buf.toString(); + } + + + public static Text join(Text[] array, Text separator, int startIndex, int endIndex) { + if (array == null) { + return null; + } + int bufSize = (endIndex - startIndex); + if (bufSize <= 0) { + return null; + } + MutableText buf = new LiteralText(""); + for (int i = startIndex; i < endIndex; i++) { + if (i > startIndex) { + buf.append(separator); + } + if (array[i] != null) { + buf.append(array[i]); + } + } + return buf; + } + + public static Text spaceBetween(Text[] array, int totalWidth, int padding) { + int totalTextSize = 0; + ArrayList strings = new ArrayList<>(array.length); + for (Text txt : array) { + String str = txt.getString(); + strings.add(str); + totalTextSize += str.length(); + } + + // No room for spacing + if (totalTextSize > totalWidth) { + return concat(array); + } + + MutableText outText = new LiteralText("");//new ArrayList<>(strings.size() * 2 - 1); + String lrPadStr = " ".repeat(padding); + String spaceStr = " ".repeat((totalWidth - padding * 2 - totalTextSize) / (array.length - 1)); + outText.append(new LiteralText(lrPadStr)); + + for (int i = 0; i < array.length; i++) { + outText.append(array[i]); + if (i != array.length - 1) + outText.append(new LiteralText(spaceStr)); + } + + outText.append(new LiteralText(lrPadStr)); + + return outText; + } + + public static Text clickableTeleport(MutableText originalText, String destinationName, String commandBaseString) { + String teleportCommand = String.format("%s %s", commandBaseString, destinationName); + + Style outStyle = originalText.getStyle() + .withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, teleportCommand)) + .withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new LiteralText("Click to teleport to " + destinationName +"."))); + + return originalText.setStyle(outStyle); + } + + + private static final Collection textParsers = new ArrayList<>(); + /** + * Parsers should be registered in order of most-restrictive to least restrictive. + */ + public static void registerTextParser(StringToTextParser parser) { + textParsers.add(parser); + } + + static { + registerTextParser(Text.Serializer::fromJson); + int javaVersion = Util.getJavaVersion(); + if (javaVersion >= 16) { + ECCore.LOGGER.log(Level.INFO, String.format( + "Detected Java version %d. Enabling Java %d features.", + javaVersion, + 16 + )); + registerTextParser(TextParser::parse); + } else { + ECCore.LOGGER.log(Level.WARN, String.format( + "Detected Java version %d. Some features require Java %d. Some text formatting features will be disabled.", + javaVersion, + 16 + )); + } + } + public static Text parseText(String textStr) { + Text outText = null; + for (StringToTextParser parser : textParsers) { + try { + outText = parser.parseText(textStr); + } catch (JsonParseException e) { + ECCore.LOGGER.log(Level.INFO, String.format("Failed to parse string '%s' as MinecraftText, trying Fabric Placeholder API...", textStr)); + } + + if (outText != null) + break; + } + return outText; + } +} diff --git a/ec-core/src/main/java/dev/jpcode/eccore/util/Util.java b/ec-core/src/main/java/dev/jpcode/eccore/util/Util.java new file mode 100644 index 00000000..332e3823 --- /dev/null +++ b/ec-core/src/main/java/dev/jpcode/eccore/util/Util.java @@ -0,0 +1,21 @@ +package dev.jpcode.eccore.util; + +public class Util { + static int getJavaVersion() { + String version = System.getProperty("java.version"); + if(version.startsWith("1.")) { + version = version.substring(2, 3); + } else { + int dot = version.indexOf("."); + if(dot != -1) { version = version.substring(0, dot); } + } + int versionNum = 0; + try { + versionNum = Integer.parseInt(version.replaceAll("[^0-9]", "")); + } catch (NumberFormatException e) { + e.printStackTrace(); + } + return versionNum; + } + +} diff --git a/ec-core/src/main/resources/assets/eccore/icon.jpg b/ec-core/src/main/resources/assets/eccore/icon.jpg new file mode 100644 index 00000000..ec6f5f01 Binary files /dev/null and b/ec-core/src/main/resources/assets/eccore/icon.jpg differ diff --git a/ec-core/src/main/resources/eccore.mixins.json b/ec-core/src/main/resources/eccore.mixins.json new file mode 100644 index 00000000..684a550f --- /dev/null +++ b/ec-core/src/main/resources/eccore.mixins.json @@ -0,0 +1,12 @@ +{ + "required": true, + "package": "com.fibermc.essentialcommands.mixin", + "compatibilityLevel": "JAVA_16", + "mixins": [ + ], + "client": [ + ], + "injectors": { + "defaultRequire": 1 + } +} diff --git a/ec-core/src/main/resources/fabric.mod.json b/ec-core/src/main/resources/fabric.mod.json new file mode 100644 index 00000000..675eefbb --- /dev/null +++ b/ec-core/src/main/resources/fabric.mod.json @@ -0,0 +1,45 @@ +{ + "schemaVersion": 1, + "id": "${id}", + "version": "${version}", + + "name": "${name}", + "description": "The core lib for Essential Commands and some other mods by John-Paul-R (JP79194).", + "authors": [ + { + "name": "John Paul R. (JP79194)", + "contact": { + "homepage": "https://www.jpcode.dev", + "discord": "https://discord.jpcode.dev" + } + } + ], + "contact": { + "homepage": "https://www.jpcode.dev/minecraft/fabric-mods", + "issues": "https://github.com/John-Paul-R/Essential-Commands/issues", + "sources": "https://github.com/John-Paul-R/Essential-Commands", + "discord": "https://discord.jpcode.dev" + }, + + "license": "MIT", + "icon": "assets/ec-core/icon.jpg", + + "environment": "*", + "entrypoints": { + "main": [ + ] + }, + "mixins": [ + "eccore.mixins.json" + ], + + "depends": { + "fabricloader": ">=0.11.3", + "fabric": "*", + "fabric-permissions-api-v0": "*" + }, + "suggests": { + "flamingo": "*" + }, + "jars": [] +} diff --git a/gradle.properties b/gradle.properties index d08406a0..30e71dc1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,13 +12,15 @@ org.gradle.jvmargs=-Xmx2048M mod_name = Essential Commands mod_id = essential_commands - mod_version = 0.15.0-4 + + mod_version = 0.16.0 + maven_group = com.fibermc archives_base_name = essential_commands # Dependencies permissions_api_version=0.1-SNAPSHOT - placeholder_api_version=1.0.1+1.17 + placeholder_api_version=1.1.0+1.16 #mod_menu_version = 2.0.2 gfh_version = 6.1-JP7-4 diff --git a/settings-github-action.gradle b/settings-github-action.gradle new file mode 100644 index 00000000..5109f8b4 --- /dev/null +++ b/settings-github-action.gradle @@ -0,0 +1,12 @@ +pluginManagement { + repositories { + maven { + name = 'Fabric' + url = 'https://maven.fabricmc.net/' + } + gradlePluginPortal() + } +} +rootProject.name = 'essential_commands' +rootProject.buildFileName = "build-github-action.gradle" +include 'ec-core' diff --git a/settings.gradle b/settings.gradle index 9fc08af7..14c32a64 100644 --- a/settings.gradle +++ b/settings.gradle @@ -8,3 +8,4 @@ pluginManagement { } } rootProject.name = 'essential_commands' +include 'ec-core' diff --git a/src/main/java/com/fibermc/essentialcommands/ECPerms.java b/src/main/java/com/fibermc/essentialcommands/ECPerms.java index 9878328d..8cbc4bc9 100644 --- a/src/main/java/com/fibermc/essentialcommands/ECPerms.java +++ b/src/main/java/com/fibermc/essentialcommands/ECPerms.java @@ -1,6 +1,5 @@ package com.fibermc.essentialcommands; -import com.fibermc.essentialcommands.config.Config; import me.lucko.fabric.api.permissions.v0.PermissionCheckEvent; import me.lucko.fabric.api.permissions.v0.Permissions; import net.fabricmc.fabric.api.util.TriState; @@ -12,12 +11,15 @@ import java.util.Collection; import java.util.function.Predicate; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class ECPerms { //`essentialcommands..` // `essentialcommands..*` public static final class Registry { public static final String tpa = "essentialcommands.tpa"; + public static final String tpahere = "essentialcommands.tpahere"; public static final String tpaccept = "essentialcommands.tpaccept"; public static final String tpdeny = "essentialcommands.tpdeny"; public static final String home_set = "essentialcommands.home.set"; @@ -41,12 +43,13 @@ public static final class Registry { public static final String fly_others = "essentialcommands.fly.others"; public static final String workbench = "essentialcommands.workbench"; public static final String enderchest = "essentialcommands.enderchest"; + public static final String top = "essentialcommands.top"; public static final String config_reload = "essentialcommands.config.reload"; public static final String bypass_teleport_delay = "essentialcommands.bypass.teleport_delay"; public static final String bypass_allow_teleport_between_dimensions = "essentialcommands.bypass.allow_teleport_between_dimensions"; public static final String bypass_teleport_interrupt_on_damaged = "essentialcommands.bypass.teleport_interrupt_on_damaged"; public static final class Group { - public static final String[] tpa_group = {tpa, tpaccept, tpdeny}; + public static final String[] tpa_group = {tpa, tpahere, tpaccept, tpdeny}; public static final String[] home_group = {home_set, home_tp, home_delete}; public static final String[] warp_group = {warp_set, warp_tp, warp_delete}; public static final String[] spawn_group = {spawn_tp, spawn_set}; @@ -58,7 +61,7 @@ public static final class Group { } static void init() { - if (Config.USE_PERMISSIONS_API) { + if (CONFIG.USE_PERMISSIONS_API.getValue()) { PermissionCheckEvent.EVENT.register((source, permission) -> { if (isSuperAdmin(source)) { return TriState.TRUE; @@ -82,7 +85,7 @@ private static boolean isSuperAdmin(CommandSource source) { } static boolean check(@NotNull CommandSource source, @NotNull String permission, int defaultRequireLevel) { - if (Config.USE_PERMISSIONS_API) { + if (CONFIG.USE_PERMISSIONS_API.getValue()) { return Permissions.getPermissionValue(source, permission).orElse(false); } else { return (source.hasPermissionLevel(defaultRequireLevel) ? TriState.TRUE:TriState.FALSE).orElse(false); @@ -118,14 +121,14 @@ static int getHighestNumericPermission(@NotNull CommandSource source, @NotNull S } // If permissions API is disabled, min int value in permission group is used for all non-op players. - if (!Config.USE_PERMISSIONS_API) { + if (!CONFIG.USE_PERMISSIONS_API.getValue()) { return Arrays.stream(permissionGroup).mapToInt(ECPerms::getNumericValue).min().getAsInt(); } // If permissions api is enabled, find the highest numeric permission node that the user has & return its // numeric value. int highestValue; - if (Config.GRANT_LOWEST_NUMERIC_BY_DEFAULT) { + if (CONFIG.GRANT_LOWEST_NUMERIC_BY_DEFAULT.getValue()) { // Grant min perm value in group by default, if none are set. highestValue = Arrays.stream(permissionGroup).mapToInt(ECPerms::getNumericValue).min().getAsInt(); } else { diff --git a/src/main/java/com/fibermc/essentialcommands/EssentialCommandRegistry.java b/src/main/java/com/fibermc/essentialcommands/EssentialCommandRegistry.java index 05a80c7f..172ede9a 100644 --- a/src/main/java/com/fibermc/essentialcommands/EssentialCommandRegistry.java +++ b/src/main/java/com/fibermc/essentialcommands/EssentialCommandRegistry.java @@ -1,14 +1,11 @@ package com.fibermc.essentialcommands; import com.fibermc.essentialcommands.commands.*; -import com.fibermc.essentialcommands.commands.suggestions.HomeSuggestion; -import com.fibermc.essentialcommands.commands.suggestions.NicknamePlayersSuggestion; -import com.fibermc.essentialcommands.commands.suggestions.TeleportResponseSuggestion; -import com.fibermc.essentialcommands.commands.suggestions.WarpSuggestion; -import com.fibermc.essentialcommands.config.Config; +import com.fibermc.essentialcommands.commands.suggestions.*; import com.fibermc.essentialcommands.util.EssentialsXParser; import com.fibermc.essentialcommands.util.TextUtil; import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.BoolArgumentType; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.tree.LiteralCommandNode; @@ -25,6 +22,7 @@ import java.nio.file.Path; import java.util.function.Predicate; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; import static net.minecraft.server.command.CommandManager.argument; /** @@ -46,7 +44,7 @@ public static void register() { //Make some new nodes //Tpa - if (Config.ENABLE_TPA) { + if (CONFIG.ENABLE_TPA.getValue()) { LiteralCommandNode tpAskNode = CommandManager.literal("tpa") .requires(ECPerms.require(ECPerms.Registry.tpa, 0)) .then(argument("target", EntityArgumentType.player()) @@ -70,7 +68,7 @@ public static void register() { ).build(); LiteralCommandNode tpAskHereNode = CommandManager.literal("tpahere") - .requires(ECPerms.require(ECPerms.Registry.tpa, 0)) + .requires(ECPerms.require(ECPerms.Registry.tpahere, 0)) .then(argument("target", EntityArgumentType.player()) .executes(new TeleportAskHereCommand()) ).build(); @@ -90,7 +88,7 @@ public static void register() { //Homes - if (Config.ENABLE_HOME) { + if (CONFIG.ENABLE_HOME.getValue()) { LiteralArgumentBuilder homeBuilder = CommandManager.literal("home"); LiteralArgumentBuilder homeSetBuilder = CommandManager.literal("set"); LiteralArgumentBuilder homeTpBuilder = CommandManager.literal("tp"); @@ -137,7 +135,7 @@ public static void register() { //Back - if (Config.ENABLE_BACK) { + if (CONFIG.ENABLE_BACK.getValue()) { LiteralArgumentBuilder backBuilder = CommandManager.literal("back"); backBuilder .requires(ECPerms.require(ECPerms.Registry.back, 0)) @@ -150,7 +148,7 @@ public static void register() { } //Warp - if (Config.ENABLE_WARP) { + if (CONFIG.ENABLE_WARP.getValue()) { LiteralArgumentBuilder warpBuilder = CommandManager.literal("warp"); LiteralArgumentBuilder warpSetBuilder = CommandManager.literal("set"); LiteralArgumentBuilder warpTpBuilder = CommandManager.literal("tp"); @@ -197,7 +195,7 @@ public static void register() { } //Spawn - if (Config.ENABLE_SPAWN) { + if (CONFIG.ENABLE_SPAWN.getValue()) { LiteralArgumentBuilder spawnBuilder = CommandManager.literal("spawn"); LiteralArgumentBuilder spawnSetBuilder = CommandManager.literal("set"); LiteralArgumentBuilder spawnTpBuilder = CommandManager.literal("tp"); @@ -223,7 +221,7 @@ public static void register() { } //Nickname - if (Config.ENABLE_NICK) { + if (CONFIG.ENABLE_NICK.getValue()) { LiteralArgumentBuilder nickBuilder = CommandManager.literal("nickname"); LiteralArgumentBuilder nickSetBuilder = CommandManager.literal("set"); LiteralArgumentBuilder nickClearBuilder = CommandManager.literal("clear"); @@ -270,7 +268,7 @@ public static void register() { essentialCommandsRootNode.addChild(nickNode); } - if (Config.ENABLE_RTP) { + if (CONFIG.ENABLE_RTP.getValue()) { LiteralCommandNode rtpNode = dispatcher.register( CommandManager.literal("randomteleport") .requires(ECPerms.require(ECPerms.Registry.randomteleport, 2)) @@ -285,7 +283,7 @@ public static void register() { } - if (Config.ENABLE_FLY) { + if (CONFIG.ENABLE_FLY.getValue()) { LiteralCommandNode flyNode = dispatcher.register( CommandManager.literal("fly") .requires(ECPerms.require(ECPerms.Registry.fly_self, 2)) @@ -293,13 +291,16 @@ public static void register() { .then(argument("target_player", EntityArgumentType.player()) .requires(ECPerms.require(ECPerms.Registry.fly_others, 4)) .executes(new FlyCommand()) + .then(argument("permanent", BoolArgumentType.bool()) + .executes(new FlyCommand()) + ) ) ); essentialCommandsRootNode.addChild(flyNode); } - if (Config.ENABLE_WORKBENCH) { + if (CONFIG.ENABLE_WORKBENCH.getValue()) { LiteralCommandNode workbenchNode = dispatcher.register( CommandManager.literal("workbench") .requires(ECPerms.require(ECPerms.Registry.workbench, 0)) @@ -309,7 +310,7 @@ public static void register() { essentialCommandsRootNode.addChild(workbenchNode); } - if (Config.ENABLE_ENDERCHEST) { + if (CONFIG.ENABLE_ENDERCHEST.getValue()) { LiteralCommandNode enderchestNode = dispatcher.register( CommandManager.literal("enderchest") .requires(ECPerms.require(ECPerms.Registry.enderchest, 0)) @@ -319,29 +320,62 @@ public static void register() { essentialCommandsRootNode.addChild(enderchestNode); } + if (CONFIG.ENABLE_TOP.getValue()) { + LiteralCommandNode topNode = dispatcher.register( + CommandManager.literal("top") + .requires(ECPerms.require(ECPerms.Registry.top, 2)) + .executes(new TopCommand()) + ); + + essentialCommandsRootNode.addChild(topNode); + } + LiteralCommandNode configNode = CommandManager.literal("config") .requires(ECPerms.requireAny(ECPerms.Registry.Group.config_group, 4)) - .build(); - - LiteralCommandNode configReloadNode = CommandManager.literal("reload") - .executes((context) -> { - Config.loadOrCreateProperties(); - context.getSource().sendFeedback( - TextUtil.concat( - ECText.getInstance().getText("essentialcommands.fullprefix"), - ECText.getInstance().getText("cmd.config.reload") - ), - true - ); - return 1; - }).requires( - ECPerms.require(ECPerms.Registry.config_reload, 4) - ).build(); - configNode.addChild(configReloadNode); + .then(CommandManager.literal("reload") + .executes((context) -> { + CONFIG.loadOrCreateProperties(); + context.getSource().sendFeedback( + TextUtil.concat( + ECText.getInstance().getText("essentialcommands.fullprefix"), + ECText.getInstance().getText("cmd.config.reload") + ), + true + ); + return 1; + }).requires( + ECPerms.require(ECPerms.Registry.config_reload, 4) + ).build()) + .then(CommandManager.literal("display") + .requires(ECPerms.require(ECPerms.Registry.config_reload, 4)) + .executes((context) -> { + CONFIG.loadOrCreateProperties(); + context.getSource().sendFeedback( + CONFIG.stateAsText(), + false + ); + return 1; + }) + .then(CommandManager.argument("config_property", StringArgumentType.word()) + .suggests(ListSuggestion.of(CONFIG::getPublicFieldNames)) + .executes(context -> { + try { + context.getSource().sendFeedback(CONFIG.getFieldValueAsText( + StringArgumentType.getString(context, "config_property") + ), false); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + return 1; + }) + ) + ).build(); + essentialCommandsRootNode.addChild(configNode); - if (Config.ENABLE_ESSENTIALSX_CONVERT) { + if (CONFIG.ENABLE_ESSENTIALSX_CONVERT.getValue()) { essentialCommandsRootNode.addChild(CommandManager.literal("convertEssentialsXPlayerHomes") .requires(source -> source.hasPermissionLevel(4)) .executes((source) -> { @@ -352,7 +386,7 @@ public static void register() { mcDir.resolve("world/modplayerdata").toFile(), source.getSource().getMinecraftServer() ); - source.getSource().sendFeedback(new LiteralText("Successfully converted data dirs."), Config.BROADCAST_TO_OPS); + source.getSource().sendFeedback(new LiteralText("Successfully converted data dirs."), CONFIG.BROADCAST_TO_OPS.getValue()); } catch (NotDirectoryException | FileNotFoundException e) { e.printStackTrace(); } diff --git a/src/main/java/com/fibermc/essentialcommands/EssentialCommands.java b/src/main/java/com/fibermc/essentialcommands/EssentialCommands.java index 9a2cc467..8f2616a7 100644 --- a/src/main/java/com/fibermc/essentialcommands/EssentialCommands.java +++ b/src/main/java/com/fibermc/essentialcommands/EssentialCommands.java @@ -1,7 +1,8 @@ package com.fibermc.essentialcommands; //import net.fabricmc.api.DedicatedServerModInitializer; -import com.fibermc.essentialcommands.config.Config; +import com.fibermc.essentialcommands.config.EssentialCommandsConfig; +import dev.jpcode.eccore.config.Config; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.api.ModInitializer; import net.minecraft.server.MinecraftServer; @@ -9,10 +10,16 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.nio.file.Path; + public final class EssentialCommands implements ModInitializer { public static Logger LOGGER = LogManager.getLogger("EssentialCommands"); - + public static final EssentialCommandsConfig CONFIG = new EssentialCommandsConfig( + Path.of("./config/EssentialCommands.properties"), + "Essential Commands Config", + "https://github.com/John-Paul-R/Essential-Commands/wiki/Config-Documentation" + ); public static void log(Level level, String message) { final String logPrefix = "[EssentialCommands]: "; LOGGER.log(level, logPrefix.concat(message)); @@ -24,13 +31,15 @@ public static void log(Level level, String message) { log(Level.INFO, "Mod Load Initiated."); //Load Preferences - Config.loadOrCreateProperties(); + CONFIG.loadOrCreateProperties(); //init mod stuff ManagerLocator managers = new ManagerLocator(); managers.init(); ServerLifecycleEvents.SERVER_STARTING.register((MinecraftServer server) -> { managers.onServerStart(server); +// I just do "./config" If you want toe( be more proper, you can do +//// server.getRunDirectory().toPath().resolv"config") }); ECPerms.init(); @@ -38,7 +47,7 @@ public static void log(Level level, String message) { //Register Mod EssentialCommandRegistry.register(); - if (Config.CHECK_FOR_UPDATES) { + if (CONFIG.CHECK_FOR_UPDATES.getValue()) { Updater.checkForUpdates(); } diff --git a/src/main/java/com/fibermc/essentialcommands/ManagerLocator.java b/src/main/java/com/fibermc/essentialcommands/ManagerLocator.java index ee23e10c..4d2e730e 100644 --- a/src/main/java/com/fibermc/essentialcommands/ManagerLocator.java +++ b/src/main/java/com/fibermc/essentialcommands/ManagerLocator.java @@ -1,8 +1,9 @@ package com.fibermc.essentialcommands; -import com.fibermc.essentialcommands.config.Config; import net.minecraft.server.MinecraftServer; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class ManagerLocator { private PlayerDataManager playerDataManager; @@ -17,19 +18,20 @@ public ManagerLocator() { static boolean playerDataEnabled() { return ( - Config.ENABLE_HOME || - Config.ENABLE_TPA || - Config.ENABLE_BACK || - Config.ENABLE_WARP || - Config.ENABLE_SPAWN + CONFIG.ENABLE_HOME.getValue() || + CONFIG.ENABLE_TPA.getValue() || + CONFIG.ENABLE_BACK.getValue() || + CONFIG.ENABLE_WARP.getValue() || + CONFIG.ENABLE_SPAWN.getValue() || + CONFIG.ENABLE_NICK.getValue() ); } static boolean teleportRequestEnabled() { - return (Config.ENABLE_TPA); + return (CONFIG.ENABLE_TPA.getValue()); } static boolean warpEnabled() { - return (Config.ENABLE_TPA || Config.ENABLE_SPAWN); + return (CONFIG.ENABLE_TPA.getValue() || CONFIG.ENABLE_SPAWN.getValue()); } public void init() { diff --git a/src/main/java/com/fibermc/essentialcommands/PlayerData.java b/src/main/java/com/fibermc/essentialcommands/PlayerData.java index dd602670..6a99e9d0 100644 --- a/src/main/java/com/fibermc/essentialcommands/PlayerData.java +++ b/src/main/java/com/fibermc/essentialcommands/PlayerData.java @@ -1,10 +1,9 @@ package com.fibermc.essentialcommands; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.types.MinecraftLocation; import com.fibermc.essentialcommands.util.TextUtil; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import me.lucko.fabric.api.permissions.v0.Permissions; +import net.minecraft.entity.player.PlayerAbilities; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtElement; import net.minecraft.server.network.ServerPlayerEntity; @@ -19,6 +18,8 @@ import java.io.File; import java.util.*; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class PlayerData extends PersistentState { // ServerPlayerEntity @@ -48,6 +49,8 @@ public class PlayerData extends PersistentState { // RTP Cooldown private int rtpNextUsableTime; + private boolean persistFlight; + public PlayerData(ServerPlayerEntity player, File saveFile) { super(player.getUuid().toString()); this.player = player; @@ -62,13 +65,16 @@ public PlayerData(ServerPlayerEntity player, File saveFile) { * DO NOT USE FOR LOGGED-IN PLAYERS. * This constructor should ONLY be used for temporarily * handling data of offline players. - * @param playerUuid - * @param homes - * @param saveFile + * + * getPlayer() will always return null on an instance created in this fashion, + * and any operations that would require a ServerPlayerEntity will fail. + * + * @param playerUuid UUID of the player whose data we want to grab or modify. + * @param homes NamedLocationStorage of the player's homes (fills field) + * @param saveFile The save file for this PlayerData instance. */ public PlayerData(UUID playerUuid, NamedLocationStorage homes, File saveFile) { super(playerUuid.toString()); -// this.player = player; this.pUuid = playerUuid; this.saveFile = saveFile; tpTimer = -1; @@ -101,10 +107,6 @@ public LinkedHashMap getIncomingTeleportRequests() { return incomingTeleportRequests; } - public TeleportRequest getIncomingTeleportRequest(PlayerData tpAsker) { - return incomingTeleportRequests.get(tpAsker.getPlayer().getUuid()); - } - public TeleportRequest getIncomingTeleportRequest(UUID tpAsker) { return incomingTeleportRequests.get(tpAsker); } @@ -113,8 +115,8 @@ public void addIncomingTeleportRequest(TeleportRequest teleportRequest) { this.incomingTeleportRequests.put(teleportRequest.getSenderPlayer().getUuid(), teleportRequest); } - public void removeTpAsker(PlayerData tpAsker) { - this.incomingTeleportRequests.remove(tpAsker.getPlayer().getUuid()); + public void removeIncomingTeleportRequest(UUID tpAsker) { + this.incomingTeleportRequests.remove(tpAsker); } public ServerPlayerEntity getPlayer() { @@ -131,11 +133,11 @@ public int addHome(String homeName, MinecraftLocation minecraftLocation) throws outCode = 1; } else { this.sendError(TextUtil.concat( - ECText.getInstance().getText("cmd.home.feedback.1").setStyle(Config.FORMATTING_ERROR), - new LiteralText(homeName).setStyle(Config.FORMATTING_ACCENT), - ECText.getInstance().getText("cmd.home.set.error.limit.2").setStyle(Config.FORMATTING_ERROR), - new LiteralText(String.valueOf(playerMaxHomes)).setStyle(Config.FORMATTING_ACCENT), - ECText.getInstance().getText("cmd.home.set.error.limit.3").setStyle(Config.FORMATTING_ERROR) + ECText.getInstance().getText("cmd.home.feedback.1").setStyle(CONFIG.FORMATTING_ERROR.getValue()), + new LiteralText(homeName).setStyle(CONFIG.FORMATTING_ACCENT.getValue()), + ECText.getInstance().getText("cmd.home.set.error.limit.2").setStyle(CONFIG.FORMATTING_ERROR.getValue()), + new LiteralText(String.valueOf(playerMaxHomes)).setStyle(CONFIG.FORMATTING_ACCENT.getValue()), + ECText.getInstance().getText("cmd.home.set.error.limit.3").setStyle(CONFIG.FORMATTING_ERROR.getValue()) )); } @@ -183,6 +185,15 @@ public void fromNbt(NbtCompound tag) { EssentialCommands.LOGGER.warn("Could not refresh player full nickanme, as ServerPlayerEntity was null in PlayerData."); } } + + if (dataTag.contains("persistFlight")) { + this.persistFlight = dataTag.getBoolean("persistFlight"); + } + + if (this.player != null) { + updatePlayer(this.player); + } + } @Override @@ -195,6 +206,8 @@ public NbtCompound writeNbt(NbtCompound tag) { tag.putString("nickname", Text.Serializer.toJson(nickname)); + tag.putBoolean("persistFlight", persistFlight); + return tag; } @@ -219,6 +232,17 @@ public boolean removeHome(String homeName) { public void updatePlayer(ServerPlayerEntity serverPlayerEntity) { this.player = serverPlayerEntity; + + if (this.persistFlight) { + PlayerDataManager.scheduleTask(this::updateFlight); + } + } + + public void updateFlight() { + PlayerAbilities abilities = this.player.abilities; + abilities.allowFlying = true; + abilities.flying = true; + this.player.sendAbilitiesUpdate(); } public void tickTpCooldown() { @@ -252,7 +276,7 @@ public int setNickname(Text nickname) { )); } else { // Ensure nickname does not exceed max length - if (nickname.getString().length() > Config.NICKNAME_MAX_LENGTH) { + if (nickname.getString().length() > CONFIG.NICKNAME_MAX_LENGTH.getValue()) { return -2; } // Ensure player has permissions required to set the specified nickname @@ -303,14 +327,14 @@ public void reloadFullNickname() { // because our mixin to getDisplayName does a null check on getNickname if (this.nickname != null) { tempFullNickname - .append(Config.NICKNAME_PREFIX) + .append(CONFIG.NICKNAME_PREFIX.getValue()) .append(this.nickname ); } else { tempFullNickname .append(baseName); } - if (Config.NICK_REVEAL_ON_HOVER) { + if (CONFIG.NICK_REVEAL_ON_HOVER.getValue()) { tempFullNickname.setStyle(tempFullNickname.getStyle().withHoverEvent( HoverEvent.Action.SHOW_TEXT.buildHoverEvent(baseName) )); @@ -318,4 +342,13 @@ public void reloadFullNickname() { this.fullNickname = tempFullNickname; } + + public boolean isPersistFlight() { + return persistFlight; + } + + public void setPersistFlight(boolean persistFlight) { + this.persistFlight = persistFlight; + this.markDirty(); + } } diff --git a/src/main/java/com/fibermc/essentialcommands/PlayerDataManager.java b/src/main/java/com/fibermc/essentialcommands/PlayerDataManager.java index 718c6fc2..a747a908 100644 --- a/src/main/java/com/fibermc/essentialcommands/PlayerDataManager.java +++ b/src/main/java/com/fibermc/essentialcommands/PlayerDataManager.java @@ -1,7 +1,6 @@ package com.fibermc.essentialcommands; import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.events.PlayerConnectCallback; import com.fibermc.essentialcommands.events.PlayerDeathCallback; import com.fibermc.essentialcommands.events.PlayerLeaveCallback; @@ -20,17 +19,21 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class PlayerDataManager { private final ConcurrentHashMap dataMap; private final List changedNicknames; private final List changedTeams; + private final List nextTickTasks; private static PlayerDataManager INSTANCE; public PlayerDataManager() { INSTANCE = this; this.changedNicknames = new LinkedList<>(); this.changedTeams = new LinkedList<>(); + this.nextTickTasks = new LinkedList<>(); this.dataMap = new ConcurrentHashMap<>(); } @@ -55,7 +58,7 @@ public void markNicknameDirty(String playerName) { } public void tick(MinecraftServer server) { - if (Config.NICKNAMES_IN_PLAYER_LIST && server.getTicks() % (20*5) == 0) { + if (CONFIG.NICKNAMES_IN_PLAYER_LIST.getValue() && server.getTicks() % (20*5) == 0) { if (this.changedNicknames.size() + this.changedTeams.size() > 0) { PlayerManager serverPlayerManager = server.getPlayerManager(); @@ -75,6 +78,19 @@ public void tick(MinecraftServer server) { this.changedTeams.clear(); } } + + if (!nextTickTasks.isEmpty()) { + Iterator tasks = nextTickTasks.listIterator(); + while (tasks.hasNext()) { + tasks.next().run(); + tasks.remove(); + } + } + + } + + public static void scheduleTask(Runnable task) { + INSTANCE.nextTickTasks.add(task); } // EVENTS @@ -97,7 +113,7 @@ private static void onPlayerRespawn(ServerPlayerEntity oldPlayerEntity, ServerPl private static void onPlayerDeath(ServerPlayerEntity playerEntity, DamageSource damageSource) { PlayerData pData = ((ServerPlayerEntityAccess) playerEntity).getEcPlayerData(); - if (Config.ALLOW_BACK_ON_DEATH) + if (CONFIG.ALLOW_BACK_ON_DEATH.getValue()) pData.setPreviousLocation(new MinecraftLocation(pData.getPlayer())); } diff --git a/src/main/java/com/fibermc/essentialcommands/PlayerTeleporter.java b/src/main/java/com/fibermc/essentialcommands/PlayerTeleporter.java index 78012ae9..07b61cb6 100644 --- a/src/main/java/com/fibermc/essentialcommands/PlayerTeleporter.java +++ b/src/main/java/com/fibermc/essentialcommands/PlayerTeleporter.java @@ -1,15 +1,14 @@ package com.fibermc.essentialcommands; import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.types.MinecraftLocation; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; import net.minecraft.text.MutableText; -import net.minecraft.text.Text; - import net.minecraft.util.Util; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + /** * Teleporter */ @@ -23,17 +22,17 @@ public static void requestTeleport(QueuedTeleport queuedTeleport) { // if (pData.getTpCooldown() < 0 || player.getServer().getPlayerManager().isOperator(player.getGameProfile())) { // //send TP request to tpManager // } - if (playerHasTpRulesBypass(player, ECPerms.Registry.bypass_teleport_delay) || Config.TELEPORT_DELAY <= 0) { + if (playerHasTpRulesBypass(player, ECPerms.Registry.bypass_teleport_delay) || CONFIG.TELEPORT_DELAY.getValue() <= 0) { teleport(queuedTeleport.getPlayerData(), queuedTeleport.getDest()); } else { ((ServerPlayerEntityAccess) player).setEcQueuedTeleport(queuedTeleport); TeleportRequestManager.getInstance().queueTeleport(queuedTeleport); player.sendSystemMessage( - ECText.getInstance().getText("teleport.queued.1").setStyle(Config.FORMATTING_DEFAULT) - .append(queuedTeleport.getDestName().setStyle(Config.FORMATTING_ACCENT)) - .append(ECText.getInstance().getText("teleport.queued.2").setStyle(Config.FORMATTING_DEFAULT)) - .append(new LiteralText(String.format("%.1f", Config.TELEPORT_DELAY)).setStyle(Config.FORMATTING_ACCENT)) - .append(ECText.getInstance().getText("teleport.queued.3")).setStyle(Config.FORMATTING_DEFAULT), + ECText.getInstance().getText("teleport.queued.1").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) + .append(queuedTeleport.getDestName().setStyle(CONFIG.FORMATTING_ACCENT.getValue())) + .append(ECText.getInstance().getText("teleport.queued.2").setStyle(CONFIG.FORMATTING_DEFAULT.getValue())) + .append(new LiteralText(String.format("%.1f", CONFIG.TELEPORT_DELAY.getValue())).setStyle(CONFIG.FORMATTING_ACCENT.getValue())) + .append(ECText.getInstance().getText("teleport.queued.3")).setStyle(CONFIG.FORMATTING_DEFAULT.getValue()), Util.NIL_UUID ); } @@ -49,11 +48,11 @@ public static void teleport(PlayerData pData, MinecraftLocation dest) {//forceTe ServerPlayerEntity player = pData.getPlayer(); // If teleporting between dimensions is disabled and player doesn't have TP rules override - if (!Config.ALLOW_TELEPORT_BETWEEN_DIMENSIONS && !playerHasTpRulesBypass(player, ECPerms.Registry.bypass_allow_teleport_between_dimensions)) { + if (!CONFIG.ALLOW_TELEPORT_BETWEEN_DIMENSIONS.getValue() && !playerHasTpRulesBypass(player, ECPerms.Registry.bypass_allow_teleport_between_dimensions)) { // If this teleport is between dimensions if (dest.dim != player.getServerWorld().getRegistryKey()) { player.sendSystemMessage( - ECText.getInstance().getText("teleport.error.interdimensional_teleport_disabled").setStyle(Config.FORMATTING_ERROR), + ECText.getInstance().getText("teleport.error.interdimensional_teleport_disabled").setStyle(CONFIG.FORMATTING_ERROR.getValue()), Util.NIL_UUID ); return; @@ -76,16 +75,16 @@ private static void execTeleport(ServerPlayerEntity playerEntity, MinecraftLocat dest.headYaw, dest.pitch ); playerEntity.sendSystemMessage( - ECText.getInstance().getText("teleport.done.1").setStyle(Config.FORMATTING_DEFAULT) - .append(dest.toLiteralTextSimple().setStyle(Config.FORMATTING_ACCENT)) - .append(new LiteralText(".").setStyle(Config.FORMATTING_DEFAULT)), + ECText.getInstance().getText("teleport.done.1").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) + .append(dest.toLiteralTextSimple().setStyle(CONFIG.FORMATTING_ACCENT.getValue())) + .append(new LiteralText(".").setStyle(CONFIG.FORMATTING_DEFAULT.getValue())), Util.NIL_UUID ); } public static boolean playerHasTpRulesBypass(ServerPlayerEntity player, String permission) { return ( - (player.hasPermissionLevel(4) && Config.OPS_BYPASS_TELEPORT_RULES) + (player.hasPermissionLevel(4) && CONFIG.OPS_BYPASS_TELEPORT_RULES.getValue()) || ECPerms.check(player.getCommandSource(), permission, 5) ); diff --git a/src/main/java/com/fibermc/essentialcommands/QueuedTeleport.java b/src/main/java/com/fibermc/essentialcommands/QueuedTeleport.java index 859ae25a..24d7cd2f 100644 --- a/src/main/java/com/fibermc/essentialcommands/QueuedTeleport.java +++ b/src/main/java/com/fibermc/essentialcommands/QueuedTeleport.java @@ -1,12 +1,13 @@ package com.fibermc.essentialcommands; import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.types.MinecraftLocation; import net.minecraft.server.MinecraftServer; import net.minecraft.text.MutableText; import net.minecraft.text.Text; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public abstract class QueuedTeleport { private int ticksRemaining; @@ -17,7 +18,7 @@ public abstract class QueuedTeleport { public QueuedTeleport(PlayerData playerData, Text destName) { this.playerData = playerData; this.destName = destName; - this.ticksRemaining = (int)(Config.TELEPORT_DELAY*20); + this.ticksRemaining = (int)(CONFIG.TELEPORT_DELAY.getValue()*20); } public QueuedTeleport(PlayerData playerData, Text destName, int delay) { diff --git a/src/main/java/com/fibermc/essentialcommands/TeleportRequestManager.java b/src/main/java/com/fibermc/essentialcommands/TeleportRequestManager.java index b60d5116..8e1439f5 100644 --- a/src/main/java/com/fibermc/essentialcommands/TeleportRequestManager.java +++ b/src/main/java/com/fibermc/essentialcommands/TeleportRequestManager.java @@ -1,7 +1,6 @@ package com.fibermc.essentialcommands; import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.events.PlayerDamageCallback; import com.fibermc.essentialcommands.types.MinecraftLocation; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; @@ -15,6 +14,8 @@ import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + /** * TeleportRequestManager */ @@ -52,7 +53,7 @@ public void endTpRequest(TeleportRequest teleportRequest) { private void endTpRequestFinal(TeleportRequest teleportRequest) { PlayerData target = teleportRequest.getTargetPlayerData(); if (target != null) { - target.removeTpAsker(teleportRequest.getSenderPlayerData()); + target.removeIncomingTeleportRequest(teleportRequest.getSenderPlayer().getUuid()); teleportRequest.getSenderPlayerData().setSentTeleportRequest(null); } } @@ -92,13 +93,13 @@ public void tick(MinecraftServer server) { } public void onPlayerDamaged(ServerPlayerEntity playerEntity, DamageSource damageSource) { - if (Config.TELEPORT_INTERRUPT_ON_DAMAGED && !PlayerTeleporter.playerHasTpRulesBypass(playerEntity, ECPerms.Registry.bypass_teleport_interrupt_on_damaged)) { + if (CONFIG.TELEPORT_INTERRUPT_ON_DAMAGED.getValue() && !PlayerTeleporter.playerHasTpRulesBypass(playerEntity, ECPerms.Registry.bypass_teleport_interrupt_on_damaged)) { try { Objects.requireNonNull( ((ServerPlayerEntityAccess)playerEntity).endEcQueuedTeleport()); delayedQueuedTeleportMap.remove(playerEntity.getUuid()); playerEntity.sendSystemMessage( - new LiteralText("Teleport interrupted. Reason: Damage Taken").setStyle(Config.FORMATTING_ERROR), + new LiteralText("Teleport interrupted. Reason: Damage Taken").setStyle(CONFIG.FORMATTING_ERROR.getValue()), Util.NIL_UUID ); } catch (NullPointerException ignored) {} @@ -109,7 +110,7 @@ public void startTpRequest(ServerPlayerEntity requestSender, ServerPlayerEntity PlayerData requestSenderData = ((ServerPlayerEntityAccess)requestSender).getEcPlayerData(); PlayerData targetPlayerData = ((ServerPlayerEntityAccess)targetPlayer).getEcPlayerData(); - final int TRD = Config.TELEPORT_REQUEST_DURATION * TPS;//sec * ticks per sec + final int TRD = CONFIG.TELEPORT_REQUEST_DURATION.getValue() * TPS;//sec * ticks per sec requestSenderData.setTpTimer(TRD); TeleportRequest teleportRequest = new TeleportRequest(requestSender, targetPlayer, requestType); requestSenderData.setSentTeleportRequest(teleportRequest); @@ -120,7 +121,7 @@ public void startTpRequest(ServerPlayerEntity requestSender, ServerPlayerEntity public void startTpCooldown(ServerPlayerEntity player) { PlayerData pData = ((ServerPlayerEntityAccess)player).getEcPlayerData(); - final int TC = (int)(Config.TELEPORT_COOLDOWN * TPS); + final int TC = (int)(CONFIG.TELEPORT_COOLDOWN.getValue() * TPS); pData.setTpCooldown(TC); tpCooldownList.add(pData); } @@ -138,7 +139,7 @@ public void queueTeleport(QueuedTeleport queuedTeleport) { if (Objects.nonNull(prevValue)) { prevValue.getPlayerData().getPlayer().sendSystemMessage( new LiteralText("Teleport request canceled. Reason: New teleport started!") - .setStyle(Config.FORMATTING_DEFAULT), + .setStyle(CONFIG.FORMATTING_DEFAULT.getValue()), Util.NIL_UUID ); } diff --git a/src/main/java/com/fibermc/essentialcommands/commands/BackCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/BackCommand.java index 2182aab8..d3100de1 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/BackCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/BackCommand.java @@ -4,7 +4,6 @@ import com.fibermc.essentialcommands.PlayerData; import com.fibermc.essentialcommands.PlayerTeleporter; import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.types.MinecraftLocation; import com.mojang.brigadier.Command; import com.mojang.brigadier.context.CommandContext; @@ -12,6 +11,8 @@ import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class BackCommand implements Command { @@ -35,7 +36,7 @@ public int run(CommandContext context) throws CommandSyntax out=1; } else { context.getSource().sendError( - ECText.getInstance().getText("cmd.back.error.no_prev_location").setStyle(Config.FORMATTING_ERROR) + ECText.getInstance().getText("cmd.back.error.no_prev_location").setStyle(CONFIG.FORMATTING_ERROR.getValue()) ); } diff --git a/src/main/java/com/fibermc/essentialcommands/commands/EnderchestCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/EnderchestCommand.java index 897d4200..d7646bef 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/EnderchestCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/EnderchestCommand.java @@ -1,7 +1,6 @@ package com.fibermc.essentialcommands.commands; import com.fibermc.essentialcommands.ECText; -import com.fibermc.essentialcommands.config.Config; import com.mojang.brigadier.Command; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; @@ -14,6 +13,8 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class EnderchestCommand implements Command { public EnderchestCommand() { @@ -28,8 +29,8 @@ public int run(CommandContext context) throws CommandSyntax senderPlayer.incrementStat(Stats.OPEN_ENDERCHEST); source.sendFeedback( - ECText.getInstance().getText("cmd.enderchest.feedback").setStyle(Config.FORMATTING_DEFAULT), - Config.BROADCAST_TO_OPS + ECText.getInstance().getText("cmd.enderchest.feedback").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()), + CONFIG.BROADCAST_TO_OPS.getValue() ); return 0; diff --git a/src/main/java/com/fibermc/essentialcommands/commands/FlyCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/FlyCommand.java index c9e15c02..857753b7 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/FlyCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/FlyCommand.java @@ -1,9 +1,10 @@ package com.fibermc.essentialcommands.commands; import com.fibermc.essentialcommands.ECText; -import com.fibermc.essentialcommands.config.Config; +import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess; import com.fibermc.essentialcommands.util.TextUtil; import com.mojang.brigadier.Command; +import com.mojang.brigadier.arguments.BoolArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.minecraft.command.argument.EntityArgumentType; @@ -12,6 +13,8 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class FlyCommand implements Command { @@ -30,28 +33,41 @@ public int run(CommandContext context) throws CommandSyntax targetPlayer = senderPlayer; } - exec(source, targetPlayer); + boolean permanent; + try { + permanent = BoolArgumentType.getBool(context, "permanent"); + } catch (IllegalArgumentException e) { + permanent = false; + } + + exec(source, targetPlayer, permanent); return 0; } - public void exec(ServerCommandSource source, ServerPlayerEntity target) { + public void exec(ServerCommandSource source, ServerPlayerEntity target, boolean permanent) { PlayerAbilities playerAbilities = target.abilities; playerAbilities.allowFlying = !playerAbilities.allowFlying; if (!playerAbilities.allowFlying) { playerAbilities.flying = false; } + ((ServerPlayerEntityAccess) target).getEcPlayerData().setPersistFlight(permanent); target.sendAbilitiesUpdate(); + // ToDo mixins for handling "persist" option. Also requires saving in PlayerData. + // Lots of the necessary groundwork is already laid out in PlayerData. Should probably add a "persistant data" + // template thing for PlayerData to streamline this process in the future. Or maybe just "tags" + // Perhaps create a list of all fields in PlayerData that can and should be serialized && saved. + source.sendFeedback( TextUtil.concat( - ECText.getInstance().getText("cmd.fly.feedback.1").setStyle(Config.FORMATTING_DEFAULT), - new LiteralText(playerAbilities.allowFlying ? "enabled" : "disabled").setStyle(Config.FORMATTING_ACCENT), - ECText.getInstance().getText("cmd.fly.feedback.2").setStyle(Config.FORMATTING_DEFAULT), + ECText.getInstance().getText("cmd.fly.feedback.1").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()), + new LiteralText(playerAbilities.allowFlying ? "enabled" : "disabled").setStyle(CONFIG.FORMATTING_ACCENT.getValue()), + ECText.getInstance().getText("cmd.fly.feedback.2").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()), target.getDisplayName(), - new LiteralText(".").setStyle(Config.FORMATTING_DEFAULT) + new LiteralText(".").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) ), - Config.BROADCAST_TO_OPS + CONFIG.BROADCAST_TO_OPS.getValue() ); } } \ No newline at end of file diff --git a/src/main/java/com/fibermc/essentialcommands/commands/HomeDeleteCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/HomeDeleteCommand.java index 9fa134c2..ae935af3 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/HomeDeleteCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/HomeDeleteCommand.java @@ -3,7 +3,6 @@ import com.fibermc.essentialcommands.ECText; import com.fibermc.essentialcommands.PlayerData; import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.util.TextUtil; import com.mojang.brigadier.Command; import com.mojang.brigadier.arguments.StringArgumentType; @@ -13,6 +12,8 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class HomeDeleteCommand implements Command { @@ -35,15 +36,15 @@ public int run(CommandContext context) throws CommandSyntax if (wasSuccessful) { source.sendFeedback( TextUtil.concat( - ECText.getInstance().getText("cmd.home.feedback.1").setStyle(Config.FORMATTING_DEFAULT), - new LiteralText(homeName).setStyle(Config.FORMATTING_ACCENT), - ECText.getInstance().getText("cmd.home.delete.feedback.2").setStyle(Config.FORMATTING_DEFAULT) - ), Config.BROADCAST_TO_OPS); + ECText.getInstance().getText("cmd.home.feedback.1").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()), + new LiteralText(homeName).setStyle(CONFIG.FORMATTING_ACCENT.getValue()), + ECText.getInstance().getText("cmd.home.delete.feedback.2").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) + ), CONFIG.BROADCAST_TO_OPS.getValue()); } else { source.sendError(TextUtil.concat( - ECText.getInstance().getText("cmd.home.feedback.1").setStyle(Config.FORMATTING_ERROR), - new LiteralText(homeName).setStyle(Config.FORMATTING_ACCENT), - ECText.getInstance().getText("cmd.home.delete.error.2").setStyle(Config.FORMATTING_ERROR) + ECText.getInstance().getText("cmd.home.feedback.1").setStyle(CONFIG.FORMATTING_ERROR.getValue()), + new LiteralText(homeName).setStyle(CONFIG.FORMATTING_ACCENT.getValue()), + ECText.getInstance().getText("cmd.home.delete.error.2").setStyle(CONFIG.FORMATTING_ERROR.getValue()) )); out = 0; } diff --git a/src/main/java/com/fibermc/essentialcommands/commands/HomeSetCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/HomeSetCommand.java index 60392ea6..6347df73 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/HomeSetCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/HomeSetCommand.java @@ -3,7 +3,6 @@ import com.fibermc.essentialcommands.ECText; import com.fibermc.essentialcommands.PlayerData; import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.types.MinecraftLocation; import com.fibermc.essentialcommands.util.TextUtil; import com.mojang.brigadier.Command; @@ -14,6 +13,8 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class HomeSetCommand implements Command { @@ -35,9 +36,9 @@ public int run(CommandContext context) throws CommandSyntax successCode = pData.addHome(homeName, new MinecraftLocation(senderPlayer)); } catch (CommandSyntaxException e) { source.sendError(TextUtil.concat( - ECText.getInstance().getText("cmd.home.feedback.1").setStyle(Config.FORMATTING_ERROR), - new LiteralText(homeName).setStyle(Config.FORMATTING_ACCENT), - ECText.getInstance().getText("cmd.home.set.error.exists.2").setStyle(Config.FORMATTING_ERROR) + ECText.getInstance().getText("cmd.home.feedback.1").setStyle(CONFIG.FORMATTING_ERROR.getValue()), + new LiteralText(homeName).setStyle(CONFIG.FORMATTING_ACCENT.getValue()), + ECText.getInstance().getText("cmd.home.set.error.exists.2").setStyle(CONFIG.FORMATTING_ERROR.getValue()) )); } @@ -46,10 +47,10 @@ public int run(CommandContext context) throws CommandSyntax //inform command sender that the home has been set if (successCode == 1) { source.sendFeedback( - ECText.getInstance().getText("cmd.home.feedback.1").setStyle(Config.FORMATTING_DEFAULT) - .append(new LiteralText(homeName).setStyle(Config.FORMATTING_ACCENT)) - .append(ECText.getInstance().getText("cmd.home.set.feedback.2").setStyle(Config.FORMATTING_DEFAULT)), - Config.BROADCAST_TO_OPS + ECText.getInstance().getText("cmd.home.feedback.1").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) + .append(new LiteralText(homeName).setStyle(CONFIG.FORMATTING_ACCENT.getValue())) + .append(ECText.getInstance().getText("cmd.home.set.feedback.2").setStyle(CONFIG.FORMATTING_DEFAULT.getValue())), + CONFIG.BROADCAST_TO_OPS.getValue() ); } diff --git a/src/main/java/com/fibermc/essentialcommands/commands/ListCommandFactory.java b/src/main/java/com/fibermc/essentialcommands/commands/ListCommandFactory.java index 937f859f..87605ba7 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/ListCommandFactory.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/ListCommandFactory.java @@ -1,7 +1,6 @@ package com.fibermc.essentialcommands.commands; import com.fibermc.essentialcommands.ECText; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.commands.suggestions.SuggestionListProvider; import com.fibermc.essentialcommands.types.MinecraftLocation; import com.fibermc.essentialcommands.util.TextUtil; @@ -17,6 +16,7 @@ import java.util.Map.Entry; import java.util.stream.Collectors; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; import static com.fibermc.essentialcommands.util.TextUtil.clickableTeleport; public class ListCommandFactory { @@ -24,11 +24,11 @@ public class ListCommandFactory { public static Command create(String responsePreText, String commandExecText, SuggestionListProvider> suggestionsProvider) { return (CommandContext context) -> { MutableText responseText = new LiteralText(""); - responseText.append(new LiteralText(responsePreText).setStyle(Config.FORMATTING_DEFAULT)); + responseText.append(new LiteralText(responsePreText).setStyle(CONFIG.FORMATTING_DEFAULT.getValue())); Collection> suggestionsList = suggestionsProvider.getSuggestionList(context); List suggestionTextList = suggestionsList.stream().map((entry) -> clickableTeleport( - new LiteralText(entry.getKey()).setStyle(Config.FORMATTING_ACCENT), + new LiteralText(entry.getKey()).setStyle(CONFIG.FORMATTING_ACCENT.getValue()), entry.getKey(), String.format("/%s", commandExecText)) ).collect(Collectors.toList()); @@ -36,14 +36,14 @@ public static Command create(String responsePreText, String if (suggestionTextList.size() > 0) { responseText.append(TextUtil.join( suggestionTextList, - new LiteralText(", ").setStyle(Config.FORMATTING_DEFAULT) + new LiteralText(", ").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) )); } else { - responseText.append(ECText.getInstance().getText("cmd.list.feedback.empty").setStyle(Config.FORMATTING_ERROR)); + responseText.append(ECText.getInstance().getText("cmd.list.feedback.empty").setStyle(CONFIG.FORMATTING_ERROR.getValue())); } context.getSource().sendFeedback( responseText, - Config.BROADCAST_TO_OPS + CONFIG.BROADCAST_TO_OPS.getValue() ); return 0; }; diff --git a/src/main/java/com/fibermc/essentialcommands/commands/NicknameClearCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/NicknameClearCommand.java index 585635d1..611a373c 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/NicknameClearCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/NicknameClearCommand.java @@ -1,7 +1,6 @@ package com.fibermc.essentialcommands.commands; import com.fibermc.essentialcommands.ECText; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess; import com.fibermc.essentialcommands.util.TextUtil; import com.mojang.brigadier.Command; @@ -12,6 +11,8 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class NicknameClearCommand implements Command { public NicknameClearCommand() {} @@ -32,10 +33,10 @@ public int run(CommandContext context) throws CommandSyntax //inform command sender that the nickname has been set context.getSource().sendFeedback(TextUtil.concat( - ECText.getInstance().getText("cmd.nickname.set.feedback").setStyle(Config.FORMATTING_DEFAULT), + ECText.getInstance().getText("cmd.nickname.set.feedback").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()), new LiteralText(senderPlayerEntity.getGameProfile().getName()), - ECText.getInstance().getText("generic.quote_fullstop").setStyle(Config.FORMATTING_DEFAULT) - ), Config.BROADCAST_TO_OPS); + ECText.getInstance().getText("generic.quote_fullstop").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) + ), CONFIG.BROADCAST_TO_OPS.getValue()); return 1; } diff --git a/src/main/java/com/fibermc/essentialcommands/commands/NicknameSetCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/NicknameSetCommand.java index ec15e6de..237ab798 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/NicknameSetCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/NicknameSetCommand.java @@ -2,7 +2,6 @@ import com.fibermc.essentialcommands.ECText; import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.util.TextUtil; import com.mojang.brigadier.Command; import com.mojang.brigadier.arguments.StringArgumentType; @@ -16,6 +15,8 @@ import net.minecraft.text.MutableText; import net.minecraft.text.Text; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class NicknameSetCommand implements Command { public NicknameSetCommand() {} @@ -51,10 +52,10 @@ public static int exec(CommandContext context, Text nicknam //inform command sender that the nickname has been set if (successCode >= 0) { source.sendFeedback(TextUtil.concat( - ECText.getInstance().getText("cmd.nickname.set.feedback").setStyle(Config.FORMATTING_DEFAULT), + ECText.getInstance().getText("cmd.nickname.set.feedback").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()), (nicknameText != null) ? nicknameText : new LiteralText(targetPlayer.getGameProfile().getName()), - ECText.getInstance().getText("generic.quote_fullstop").setStyle(Config.FORMATTING_DEFAULT) - ), Config.BROADCAST_TO_OPS); + ECText.getInstance().getText("generic.quote_fullstop").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) + ), CONFIG.BROADCAST_TO_OPS.getValue()); } else { MutableText failReason; switch (successCode) { @@ -65,7 +66,7 @@ public static int exec(CommandContext context, Text nicknam failReason = ECText.getInstance().getText( "cmd.nickname.set.error.length", nicknameText.getString().length(), - Config.NICKNAME_MAX_LENGTH + CONFIG.NICKNAME_MAX_LENGTH.getValue() ); break; default: @@ -74,10 +75,10 @@ public static int exec(CommandContext context, Text nicknam } source.sendError(TextUtil.concat( - ECText.getInstance().getText("cmd.nickname.set.error.1").setStyle(Config.FORMATTING_ERROR), + ECText.getInstance().getText("cmd.nickname.set.error.1").setStyle(CONFIG.FORMATTING_ERROR.getValue()), nicknameText, - ECText.getInstance().getText("cmd.nickname.set.error.2").setStyle(Config.FORMATTING_ERROR), - failReason.setStyle(Config.FORMATTING_ERROR) + ECText.getInstance().getText("cmd.nickname.set.error.2").setStyle(CONFIG.FORMATTING_ERROR.getValue()), + failReason.setStyle(CONFIG.FORMATTING_ERROR.getValue()) )); } diff --git a/src/main/java/com/fibermc/essentialcommands/commands/RandomTeleportCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/RandomTeleportCommand.java index fb205a6b..4321422f 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/RandomTeleportCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/RandomTeleportCommand.java @@ -2,7 +2,6 @@ import com.fibermc.essentialcommands.*; import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.types.MinecraftLocation; import com.fibermc.essentialcommands.util.TextUtil; import com.google.common.base.Stopwatch; @@ -14,7 +13,6 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; import net.minecraft.text.LiteralText; - import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; @@ -22,6 +20,8 @@ import java.util.Random; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + /** * Heavily referenced from * https://github.com/javachaos/randomteleport/blob/master/src/main/java/net.ethermod/commands/RandomTeleportCommand.java @@ -49,20 +49,20 @@ public void run() { }; //TODO Add OP/Permission bypass for RTP cooldown. - if (Config.RTP_COOLDOWN > 0) { + if (CONFIG.RTP_COOLDOWN.getValue() > 0) { ServerCommandSource source = context.getSource(); int ticks = source.getMinecraftServer().getTicks(); PlayerData playerData = ((ServerPlayerEntityAccess)player).getEcPlayerData(); // if cooldown has expired if (playerData.getRtpNextUsableTime() < ticks) { - playerData.setRtpNextUsableTime(ticks + Config.RTP_COOLDOWN*20); + playerData.setRtpNextUsableTime(ticks + CONFIG.RTP_COOLDOWN.getValue()*20); thread.start(); resultCode = 1; } else { source.sendError(TextUtil.concat( - ECText.getInstance().getText("cmd.rtp.error.cooldown.1").setStyle(Config.FORMATTING_ERROR), - new LiteralText(String.format("%.1f", (playerData.getRtpNextUsableTime() - ticks)/20D)).setStyle(Config.FORMATTING_ACCENT), - ECText.getInstance().getText("cmd.rtp.error.cooldown.2").setStyle(Config.FORMATTING_ERROR) + ECText.getInstance().getText("cmd.rtp.error.cooldown.1").setStyle(CONFIG.FORMATTING_ERROR.getValue()), + new LiteralText(String.format("%.1f", (playerData.getRtpNextUsableTime() - ticks)/20D)).setStyle(CONFIG.FORMATTING_ACCENT.getValue()), + ECText.getInstance().getText("cmd.rtp.error.cooldown.2").setStyle(CONFIG.FORMATTING_ERROR.getValue()) )); resultCode = -2; } @@ -95,11 +95,11 @@ private static int exec(ServerCommandSource source, ServerWorld world) throws Co } private static int exec(ServerPlayerEntity player, ServerWorld world, MinecraftLocation center, int timesRun) { - if (timesRun > Config.RTP_MAX_ATTEMPTS) { + if (timesRun > CONFIG.RTP_MAX_ATTEMPTS.getValue()) { return -1; } // Calculate position on circle perimeter - int r = Config.RTP_RADIUS; + int r = CONFIG.RTP_RADIUS.getValue(); double angle = (new Random()).nextDouble()*2*Math.PI; double delta_x = r * Math.cos(angle); double delta_z = r * Math.sin(angle); diff --git a/src/main/java/com/fibermc/essentialcommands/commands/RealNameCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/RealNameCommand.java index 11fcf3a4..c7258c73 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/RealNameCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/RealNameCommand.java @@ -3,7 +3,6 @@ import com.fibermc.essentialcommands.ECText; import com.fibermc.essentialcommands.PlayerData; import com.fibermc.essentialcommands.PlayerDataManager; -import com.fibermc.essentialcommands.config.Config; import com.mojang.brigadier.Command; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; @@ -14,6 +13,8 @@ import java.util.List; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class RealNameCommand implements Command { @Override public int run(CommandContext context) throws CommandSyntaxException { @@ -25,15 +26,15 @@ public int run(CommandContext context) throws CommandSyntax // If no players matched the provided nickname if (nicknamePlayers.size() == 0) { responseText - .append(ECText.getInstance().getText("cmd.realname.feedback.none_match").setStyle(Config.FORMATTING_DEFAULT)) - .append(new LiteralText(nicknameStr).setStyle(Config.FORMATTING_ACCENT)) - .append(ECText.getInstance().getText("generic.quote_fullstop").setStyle(Config.FORMATTING_DEFAULT)); + .append(ECText.getInstance().getText("cmd.realname.feedback.none_match").setStyle(CONFIG.FORMATTING_DEFAULT.getValue())) + .append(new LiteralText(nicknameStr).setStyle(CONFIG.FORMATTING_ACCENT.getValue())) + .append(ECText.getInstance().getText("generic.quote_fullstop").setStyle(CONFIG.FORMATTING_DEFAULT.getValue())); } else { responseText - .append(ECText.getInstance().getText("cmd.realname.feedback.matching.1").setStyle(Config.FORMATTING_DEFAULT)) - .append(new LiteralText(nicknameStr).setStyle(Config.FORMATTING_ACCENT)) - .append(ECText.getInstance().getText("cmd.realname.feedback.matching.2").setStyle(Config.FORMATTING_DEFAULT)); + .append(ECText.getInstance().getText("cmd.realname.feedback.matching.1").setStyle(CONFIG.FORMATTING_DEFAULT.getValue())) + .append(new LiteralText(nicknameStr).setStyle(CONFIG.FORMATTING_ACCENT.getValue())) + .append(ECText.getInstance().getText("cmd.realname.feedback.matching.2").setStyle(CONFIG.FORMATTING_DEFAULT.getValue())); for (PlayerData nicknamePlayer : nicknamePlayers) { responseText.append("\n "); @@ -42,7 +43,7 @@ public int run(CommandContext context) throws CommandSyntax } context.getSource().sendFeedback( - responseText, Config.BROADCAST_TO_OPS + responseText, CONFIG.BROADCAST_TO_OPS.getValue() ); return 0; diff --git a/src/main/java/com/fibermc/essentialcommands/commands/SpawnCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/SpawnCommand.java index 7372b3ac..9a814723 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/SpawnCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/SpawnCommand.java @@ -4,7 +4,6 @@ import com.fibermc.essentialcommands.ManagerLocator; import com.fibermc.essentialcommands.PlayerTeleporter; import com.fibermc.essentialcommands.WorldDataManager; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.types.MinecraftLocation; import com.mojang.brigadier.Command; import com.mojang.brigadier.context.CommandContext; @@ -12,6 +11,8 @@ import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class SpawnCommand implements Command { @@ -34,7 +35,7 @@ public int run(CommandContext context) throws CommandSyntax PlayerTeleporter.requestTeleport(senderPlayer, loc, ECText.getInstance().getText("cmd.spawn.location_name")); out = 1; } else { - context.getSource().sendError(ECText.getInstance().getText("cmd.spawn.tp.error.no_spawn_set").setStyle(Config.FORMATTING_ERROR)); + context.getSource().sendError(ECText.getInstance().getText("cmd.spawn.tp.error.no_spawn_set").setStyle(CONFIG.FORMATTING_ERROR.getValue())); out = -2; } diff --git a/src/main/java/com/fibermc/essentialcommands/commands/SpawnSetCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/SpawnSetCommand.java index 0865eced..3915d13e 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/SpawnSetCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/SpawnSetCommand.java @@ -1,14 +1,16 @@ package com.fibermc.essentialcommands.commands; -import com.fibermc.essentialcommands.*; -import com.fibermc.essentialcommands.config.Config; +import com.fibermc.essentialcommands.ECText; +import com.fibermc.essentialcommands.ManagerLocator; +import com.fibermc.essentialcommands.WorldDataManager; import com.fibermc.essentialcommands.types.MinecraftLocation; import com.mojang.brigadier.Command; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.text.LiteralText; + +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; public class SpawnSetCommand implements Command { @@ -31,9 +33,9 @@ public int run(CommandContext context) throws CommandSyntax //inform command sender that the home has been set source.sendFeedback( - ECText.getInstance().getText("cmd.spawn.set.feedback").setStyle(Config.FORMATTING_DEFAULT) - .append(loc.toLiteralTextSimple().setStyle(Config.FORMATTING_ACCENT)) - , Config.BROADCAST_TO_OPS); + ECText.getInstance().getText("cmd.spawn.set.feedback").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) + .append(loc.toLiteralTextSimple().setStyle(CONFIG.FORMATTING_ACCENT.getValue())) + , CONFIG.BROADCAST_TO_OPS.getValue()); return successCode; } diff --git a/src/main/java/com/fibermc/essentialcommands/commands/TeleportAcceptCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/TeleportAcceptCommand.java index 48819894..e5ea4b9a 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/TeleportAcceptCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/TeleportAcceptCommand.java @@ -1,16 +1,17 @@ package com.fibermc.essentialcommands.commands; -import com.fibermc.essentialcommands.*; +import com.fibermc.essentialcommands.ECText; +import com.fibermc.essentialcommands.PlayerData; +import com.fibermc.essentialcommands.TeleportRequest; +import com.fibermc.essentialcommands.TeleportRequestManager; import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess; -import com.fibermc.essentialcommands.config.Config; -import com.mojang.brigadier.Command; import com.mojang.brigadier.context.CommandContext; -import com.mojang.brigadier.exceptions.CommandSyntaxException; -import net.minecraft.command.argument.EntityArgumentType; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Util; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class TeleportAcceptCommand extends TeleportResponseCommand { public TeleportAcceptCommand() {} @@ -25,7 +26,7 @@ protected int exec(CommandContext context, ServerPlayerEnti //inform target player that teleport has been accepted via chat targetPlayer.sendSystemMessage( - ECText.getInstance().getText("cmd.tpaccept.feedback").setStyle(Config.FORMATTING_DEFAULT) + ECText.getInstance().getText("cmd.tpaccept.feedback").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) , Util.NIL_UUID); //Conduct teleportation @@ -33,8 +34,8 @@ protected int exec(CommandContext context, ServerPlayerEnti //Send message to command sender confirming that request has been accepted source.sendFeedback( - ECText.getInstance().getText("cmd.tpaccept.feedback").setStyle(Config.FORMATTING_DEFAULT) - , Config.BROADCAST_TO_OPS); + ECText.getInstance().getText("cmd.tpaccept.feedback").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) + , CONFIG.BROADCAST_TO_OPS.getValue()); //Clean up TPAsk targetPlayerData.setTpTimer(-1); @@ -44,7 +45,7 @@ protected int exec(CommandContext context, ServerPlayerEnti return 1; } else { source.sendError( - ECText.getInstance().getText("cmd.tpa_reply.error.no_request_from_target").setStyle(Config.FORMATTING_ERROR) + ECText.getInstance().getText("cmd.tpa_reply.error.no_request_from_target").setStyle(CONFIG.FORMATTING_ERROR.getValue()) ); return -1; } diff --git a/src/main/java/com/fibermc/essentialcommands/commands/TeleportAskCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/TeleportAskCommand.java index d8028ff2..aa0a165e 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/TeleportAskCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/TeleportAskCommand.java @@ -1,7 +1,6 @@ package com.fibermc.essentialcommands.commands; import com.fibermc.essentialcommands.*; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.util.TextUtil; import com.mojang.brigadier.Command; import com.mojang.brigadier.context.CommandContext; @@ -12,6 +11,8 @@ import net.minecraft.text.LiteralText; import net.minecraft.util.Util; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class TeleportAskCommand implements Command { public TeleportAskCommand() {} @@ -26,8 +27,8 @@ public int run(CommandContext context) throws CommandSyntax //inform target player of tp request via chat targetPlayer.sendSystemMessage(TextUtil.concat( - new LiteralText(senderPlayer.getEntityName()).setStyle(Config.FORMATTING_ACCENT), - ECText.getInstance().getText("cmd.tpask.receive").setStyle(Config.FORMATTING_DEFAULT) + new LiteralText(senderPlayer.getEntityName()).setStyle(CONFIG.FORMATTING_ACCENT.getValue()), + ECText.getInstance().getText("cmd.tpask.receive").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) ), Util.NIL_UUID); String senderName = context.getSource().getPlayer().getGameProfile().getName(); @@ -35,8 +36,8 @@ public int run(CommandContext context) throws CommandSyntax targetPlayer, "/tpaccept " + senderName, "/tpdeny " + senderName, - new LiteralText("[" + ECText.getInstance().get("generic.accept") + "]").setStyle(Config.FORMATTING_ACCENT), - new LiteralText("[" + ECText.getInstance().get("generic.deny") + "]").setStyle(Config.FORMATTING_ERROR) + new LiteralText("[" + ECText.getInstance().get("generic.accept") + "]").setStyle(CONFIG.FORMATTING_ACCENT.getValue()), + new LiteralText("[" + ECText.getInstance().get("generic.deny") + "]").setStyle(CONFIG.FORMATTING_ERROR.getValue()) ).send(); //Mark TPRequest Sender as having requested a teleport @@ -44,9 +45,9 @@ public int run(CommandContext context) throws CommandSyntax //inform command sender that request has been sent context.getSource().sendFeedback(TextUtil.concat( - new LiteralText("Teleport request has been sent to ").setStyle(Config.FORMATTING_DEFAULT), - new LiteralText(targetPlayer.getEntityName()).setStyle(Config.FORMATTING_ACCENT) - ), Config.BROADCAST_TO_OPS); + new LiteralText("Teleport request has been sent to ").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()), + new LiteralText(targetPlayer.getEntityName()).setStyle(CONFIG.FORMATTING_ACCENT.getValue()) + ), CONFIG.BROADCAST_TO_OPS.getValue()); return 1; } diff --git a/src/main/java/com/fibermc/essentialcommands/commands/TeleportAskHereCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/TeleportAskHereCommand.java index a01d28ca..223977f4 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/TeleportAskHereCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/TeleportAskHereCommand.java @@ -1,7 +1,6 @@ package com.fibermc.essentialcommands.commands; import com.fibermc.essentialcommands.*; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.util.TextUtil; import com.mojang.brigadier.Command; import com.mojang.brigadier.context.CommandContext; @@ -12,6 +11,8 @@ import net.minecraft.text.LiteralText; import net.minecraft.util.Util; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class TeleportAskHereCommand implements Command { public TeleportAskHereCommand() {} @@ -26,8 +27,8 @@ public int run(CommandContext context) throws CommandSyntax //inform target player of tp request via chat targetPlayer.sendSystemMessage(TextUtil.concat( - new LiteralText(senderPlayer.getEntityName()).setStyle(Config.FORMATTING_ACCENT), - ECText.getInstance().getText("cmd.tpaskhere.receive").setStyle(Config.FORMATTING_DEFAULT) + new LiteralText(senderPlayer.getEntityName()).setStyle(CONFIG.FORMATTING_ACCENT.getValue()), + ECText.getInstance().getText("cmd.tpaskhere.receive").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) ), Util.NIL_UUID); String senderName = context.getSource().getPlayer().getGameProfile().getName(); @@ -35,8 +36,8 @@ public int run(CommandContext context) throws CommandSyntax targetPlayer, "/tpaccept " + senderName, "/tpdeny " + senderName, - new LiteralText("[" + ECText.getInstance().get("generic.accept") + "]").setStyle(Config.FORMATTING_ACCENT), - new LiteralText("[" + ECText.getInstance().get("generic.deny") + "]").setStyle(Config.FORMATTING_ERROR) + new LiteralText("[" + ECText.getInstance().get("generic.accept") + "]").setStyle(CONFIG.FORMATTING_ACCENT.getValue()), + new LiteralText("[" + ECText.getInstance().get("generic.deny") + "]").setStyle(CONFIG.FORMATTING_ERROR.getValue()) ).send(); //Mark TPRequest Sender as having requested a teleport @@ -44,9 +45,9 @@ public int run(CommandContext context) throws CommandSyntax //inform command sender that request has been sent context.getSource().sendFeedback(TextUtil.concat( - new LiteralText("Teleport request has been sent to ").setStyle(Config.FORMATTING_DEFAULT), - new LiteralText(targetPlayer.getEntityName()).setStyle(Config.FORMATTING_ACCENT) - ), Config.BROADCAST_TO_OPS); + new LiteralText("Teleport request has been sent to ").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()), + new LiteralText(targetPlayer.getEntityName()).setStyle(CONFIG.FORMATTING_ACCENT.getValue()) + ), CONFIG.BROADCAST_TO_OPS.getValue()); return 1; } diff --git a/src/main/java/com/fibermc/essentialcommands/commands/TeleportDenyCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/TeleportDenyCommand.java index dc97a2ec..107886c7 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/TeleportDenyCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/TeleportDenyCommand.java @@ -5,15 +5,13 @@ import com.fibermc.essentialcommands.TeleportRequest; import com.fibermc.essentialcommands.TeleportRequestManager; import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess; -import com.fibermc.essentialcommands.config.Config; -import com.mojang.brigadier.Command; import com.mojang.brigadier.context.CommandContext; -import com.mojang.brigadier.exceptions.CommandSyntaxException; -import net.minecraft.command.argument.EntityArgumentType; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Util; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class TeleportDenyCommand extends TeleportResponseCommand { public TeleportDenyCommand() {} @@ -27,13 +25,13 @@ protected int exec(CommandContext context, ServerPlayerEnti if (teleportRequest != null && teleportRequest.getTargetPlayer().equals(senderPlayer)) { //inform target player that teleport has been accepted via chat targetPlayer.sendSystemMessage( - ECText.getInstance().getText("cmd.tpdeny.feedback").setStyle(Config.FORMATTING_DEFAULT) + ECText.getInstance().getText("cmd.tpdeny.feedback").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) , Util.NIL_UUID); //Send message to command sender confirming that request has been accepted source.sendFeedback( - ECText.getInstance().getText("cmd.tpdeny.feedback").setStyle(Config.FORMATTING_DEFAULT) - , Config.BROADCAST_TO_OPS + ECText.getInstance().getText("cmd.tpdeny.feedback").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) + , CONFIG.BROADCAST_TO_OPS.getValue() ); //Clean up TPAsk @@ -44,7 +42,7 @@ protected int exec(CommandContext context, ServerPlayerEnti return 1; } else { source.sendError( - ECText.getInstance().getText("cmd.tpa_reply.error.no_request_from_target").setStyle(Config.FORMATTING_ERROR) + ECText.getInstance().getText("cmd.tpa_reply.error.no_request_from_target").setStyle(CONFIG.FORMATTING_ERROR.getValue()) ); return -1; } diff --git a/src/main/java/com/fibermc/essentialcommands/commands/TopCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/TopCommand.java new file mode 100644 index 00000000..71c4453f --- /dev/null +++ b/src/main/java/com/fibermc/essentialcommands/commands/TopCommand.java @@ -0,0 +1,51 @@ +package com.fibermc.essentialcommands.commands; + +import com.fibermc.essentialcommands.ECText; +import com.fibermc.essentialcommands.PlayerTeleporter; +import com.fibermc.essentialcommands.types.MinecraftLocation; +import com.google.common.base.Stopwatch; +import com.mojang.brigadier.Command; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.math.Vec3d; +import net.minecraft.world.RaycastContext; +import net.minecraft.world.World; + +public class TopCommand implements Command { + @Override + public int run(CommandContext context) throws CommandSyntaxException { + ServerCommandSource source = context.getSource(); + ServerPlayerEntity player = source.getPlayer(); + World world = source.getWorld(); + Vec3d playerPos = player.getPos(); + + int new_y; + double new_x = playerPos.x; + double new_z = playerPos.z; + + Stopwatch timer = Stopwatch.createStarted(); + BlockHitResult blockHitResult = world.raycast(new RaycastContext( + new Vec3d(new_x, world.getHeight(), new_z), + new Vec3d(new_x, 1, new_z), + RaycastContext.ShapeType.COLLIDER, + RaycastContext.FluidHandling.SOURCE_ONLY, + player + )); + new_y = blockHitResult.getBlockPos().getY() + 1; + + // Teleport the player + PlayerTeleporter.requestTeleport( + player, + new MinecraftLocation(world.getRegistryKey(), new_x, new_y, new_z, player.getHeadYaw(), player.pitch), + ECText.getInstance().getText("cmd.top.location_name") + ); + + return 0; + + } + + +} diff --git a/src/main/java/com/fibermc/essentialcommands/commands/WarpDeleteCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/WarpDeleteCommand.java index 8c5845c6..3aae2f8e 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/WarpDeleteCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/WarpDeleteCommand.java @@ -3,7 +3,6 @@ import com.fibermc.essentialcommands.ECText; import com.fibermc.essentialcommands.ManagerLocator; import com.fibermc.essentialcommands.WorldDataManager; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.util.TextUtil; import com.mojang.brigadier.Command; import com.mojang.brigadier.arguments.StringArgumentType; @@ -13,6 +12,8 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class WarpDeleteCommand implements Command { @@ -33,17 +34,17 @@ public int run(CommandContext context) throws CommandSyntax //inform command sender that the warp has been removed if (wasSuccessful) { source.sendFeedback(TextUtil.concat( - ECText.getInstance().getText("cmd.warp.feedback.1").setStyle(Config.FORMATTING_DEFAULT), - new LiteralText(warpName).setStyle(Config.FORMATTING_ACCENT), - ECText.getInstance().getText("cmd.warp.delete.feedback.2").setStyle(Config.FORMATTING_DEFAULT) - ), Config.BROADCAST_TO_OPS); + ECText.getInstance().getText("cmd.warp.feedback.1").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()), + new LiteralText(warpName).setStyle(CONFIG.FORMATTING_ACCENT.getValue()), + ECText.getInstance().getText("cmd.warp.delete.feedback.2").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) + ), CONFIG.BROADCAST_TO_OPS.getValue()); out = 1; } else { source.sendFeedback(TextUtil.concat( - ECText.getInstance().getText("cmd.warp.feedback.1").setStyle(Config.FORMATTING_ERROR), - new LiteralText(warpName).setStyle(Config.FORMATTING_ACCENT), - ECText.getInstance().getText("cmd.warp.delete.error.2").setStyle(Config.FORMATTING_ERROR) - ), Config.BROADCAST_TO_OPS); + ECText.getInstance().getText("cmd.warp.feedback.1").setStyle(CONFIG.FORMATTING_ERROR.getValue()), + new LiteralText(warpName).setStyle(CONFIG.FORMATTING_ACCENT.getValue()), + ECText.getInstance().getText("cmd.warp.delete.error.2").setStyle(CONFIG.FORMATTING_ERROR.getValue()) + ), CONFIG.BROADCAST_TO_OPS.getValue()); } diff --git a/src/main/java/com/fibermc/essentialcommands/commands/WarpSetCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/WarpSetCommand.java index 002e75a8..8e8d36e5 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/WarpSetCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/WarpSetCommand.java @@ -3,7 +3,6 @@ import com.fibermc.essentialcommands.ECText; import com.fibermc.essentialcommands.ManagerLocator; import com.fibermc.essentialcommands.WorldDataManager; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.types.MinecraftLocation; import com.fibermc.essentialcommands.util.TextUtil; import com.mojang.brigadier.Command; @@ -14,6 +13,8 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class WarpSetCommand implements Command { @@ -34,15 +35,15 @@ public int run(CommandContext context) throws CommandSyntax worldDataManager.setWarp(warpName, new MinecraftLocation(senderPlayer)); //inform command sender that the home has been set source.sendFeedback(TextUtil.concat( - ECText.getInstance().getText("cmd.warp.feedback.1").setStyle(Config.FORMATTING_DEFAULT), - new LiteralText(warpName).setStyle(Config.FORMATTING_ACCENT), - ECText.getInstance().getText("cmd.warp.set.feedback.2").setStyle(Config.FORMATTING_DEFAULT) - ), Config.BROADCAST_TO_OPS); + ECText.getInstance().getText("cmd.warp.feedback.1").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()), + new LiteralText(warpName).setStyle(CONFIG.FORMATTING_ACCENT.getValue()), + ECText.getInstance().getText("cmd.warp.set.feedback.2").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()) + ), CONFIG.BROADCAST_TO_OPS.getValue()); } catch (CommandSyntaxException e) { source.sendError(TextUtil.concat( - ECText.getInstance().getText("cmd.warp.feedback.1").setStyle(Config.FORMATTING_ERROR), - new LiteralText(warpName).setStyle(Config.FORMATTING_ACCENT), - ECText.getInstance().getText("cmd.warp.set.error.exists.2").setStyle(Config.FORMATTING_ERROR) + ECText.getInstance().getText("cmd.warp.feedback.1").setStyle(CONFIG.FORMATTING_ERROR.getValue()), + new LiteralText(warpName).setStyle(CONFIG.FORMATTING_ACCENT.getValue()), + ECText.getInstance().getText("cmd.warp.set.error.exists.2").setStyle(CONFIG.FORMATTING_ERROR.getValue()) )); } diff --git a/src/main/java/com/fibermc/essentialcommands/commands/WorkbenchCommand.java b/src/main/java/com/fibermc/essentialcommands/commands/WorkbenchCommand.java index 7f94196a..dae62365 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/WorkbenchCommand.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/WorkbenchCommand.java @@ -1,7 +1,6 @@ package com.fibermc.essentialcommands.commands; import com.fibermc.essentialcommands.ECText; -import com.fibermc.essentialcommands.config.Config; import com.fibermc.essentialcommands.screen.CraftingCommandScreenHandler; import com.mojang.brigadier.Command; import com.mojang.brigadier.context.CommandContext; @@ -16,6 +15,8 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import static com.fibermc.essentialcommands.EssentialCommands.CONFIG; + public class WorkbenchCommand implements Command { public WorkbenchCommand() { @@ -30,8 +31,8 @@ public int run(CommandContext context) throws CommandSyntax senderPlayer.incrementStat(Stats.INTERACT_WITH_CRAFTING_TABLE); source.sendFeedback( - new LiteralText("Opened workbench.").setStyle(Config.FORMATTING_DEFAULT), - Config.BROADCAST_TO_OPS + new LiteralText("Opened workbench.").setStyle(CONFIG.FORMATTING_DEFAULT.getValue()), + CONFIG.BROADCAST_TO_OPS.getValue() ); return 0; diff --git a/src/main/java/com/fibermc/essentialcommands/commands/suggestions/ContextFunction.java b/src/main/java/com/fibermc/essentialcommands/commands/suggestions/ContextFunction.java new file mode 100644 index 00000000..550c76e6 --- /dev/null +++ b/src/main/java/com/fibermc/essentialcommands/commands/suggestions/ContextFunction.java @@ -0,0 +1,8 @@ +package com.fibermc.essentialcommands.commands.suggestions; + +import com.mojang.brigadier.exceptions.CommandSyntaxException; + +@FunctionalInterface +public interface ContextFunction { + R apply(T o) throws CommandSyntaxException; +} diff --git a/src/main/java/com/fibermc/essentialcommands/commands/suggestions/HomeSuggestion.java b/src/main/java/com/fibermc/essentialcommands/commands/suggestions/HomeSuggestion.java index eba3757a..11462f3a 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/suggestions/HomeSuggestion.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/suggestions/HomeSuggestion.java @@ -15,8 +15,7 @@ public class HomeSuggestion { //Brigader Suggestions public static SuggestionProvider suggestedStrings() { - return (context, builder) -> ListSuggestion.getSuggestionsBuilder(builder, - getSuggestionsList(context)); + return ListSuggestion.ofContext(HomeSuggestion::getSuggestionsList); } /** diff --git a/src/main/java/com/fibermc/essentialcommands/commands/suggestions/ListSuggestion.java b/src/main/java/com/fibermc/essentialcommands/commands/suggestions/ListSuggestion.java index c8c729c8..25d2e1f4 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/suggestions/ListSuggestion.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/suggestions/ListSuggestion.java @@ -1,25 +1,46 @@ package com.fibermc.essentialcommands.commands.suggestions; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import net.minecraft.server.command.ServerCommandSource; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; -import java.util.List; +import java.util.Collection; import java.util.Locale; import java.util.concurrent.CompletableFuture; +import java.util.function.Supplier; public class ListSuggestion { - public static CompletableFuture getSuggestionsBuilder(SuggestionsBuilder builder, List list) { + public static CompletableFuture buildSuggestions(SuggestionsBuilder builder, Collection suggestionCollection) { String remaining = builder.getRemaining().toLowerCase(Locale.ROOT); - if(list.isEmpty()) { // If the list is empty then return no suggestions + if(suggestionCollection.isEmpty()) { // If the list is empty then return no suggestions return Suggestions.empty(); // No suggestions } - for (String str : list) { // Iterate through the supplied list + for (String str : suggestionCollection) { // Iterate through the supplied list if (str.toLowerCase(Locale.ROOT).startsWith(remaining)) { builder.suggest(str); // Add every single entry to suggestions list. } } return builder.buildFuture(); // Create the CompletableFuture containing all the suggestions } + + @Contract(pure = true) + public static @NotNull SuggestionProvider of(Supplier> suggestionCollection) { + return (CommandContext context, SuggestionsBuilder builder) -> { + return buildSuggestions(builder, suggestionCollection.get()); + }; + } + + @Contract(pure = true) + public static @NotNull SuggestionProvider ofContext(ContextFunction, Collection> suggestionCollection) { + return (CommandContext context, SuggestionsBuilder builder) -> { + return buildSuggestions(builder, suggestionCollection.apply(context)); + }; + } + } diff --git a/src/main/java/com/fibermc/essentialcommands/commands/suggestions/NicknamePlayersSuggestion.java b/src/main/java/com/fibermc/essentialcommands/commands/suggestions/NicknamePlayersSuggestion.java index 0d2ee592..6ca40897 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/suggestions/NicknamePlayersSuggestion.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/suggestions/NicknamePlayersSuggestion.java @@ -12,7 +12,7 @@ public class NicknamePlayersSuggestion { //Brigader Suggestions public static SuggestionProvider suggestedStrings() { - return (context, builder) -> ListSuggestion.getSuggestionsBuilder(builder, + return ListSuggestion.ofContext(context -> PlayerDataManager.getInstance().getAllPlayerData().stream() .map(PlayerData::getNickname) .filter(Objects::nonNull) diff --git a/src/main/java/com/fibermc/essentialcommands/commands/suggestions/TeleportResponseSuggestion.java b/src/main/java/com/fibermc/essentialcommands/commands/suggestions/TeleportResponseSuggestion.java index 56b6286c..45f12462 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/suggestions/TeleportResponseSuggestion.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/suggestions/TeleportResponseSuggestion.java @@ -1,8 +1,10 @@ package com.fibermc.essentialcommands.commands.suggestions; import com.fibermc.essentialcommands.access.ServerPlayerEntityAccess; +import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.suggestion.SuggestionProvider; import net.minecraft.server.command.ServerCommandSource; +import org.apache.logging.log4j.core.jmx.Server; import java.util.stream.Collectors; @@ -10,7 +12,7 @@ public class TeleportResponseSuggestion { //Brigader Suggestions public static SuggestionProvider suggestedStrings() { - return (context, builder) -> ListSuggestion.getSuggestionsBuilder(builder, + return ListSuggestion.ofContext((CommandContext context) -> ((ServerPlayerEntityAccess)context.getSource().getPlayer()).getEcPlayerData().getIncomingTeleportRequests().values() .stream().map((entry) -> entry.getSenderPlayer().getGameProfile().getName()) .collect(Collectors.toList()) diff --git a/src/main/java/com/fibermc/essentialcommands/commands/suggestions/WarpSuggestion.java b/src/main/java/com/fibermc/essentialcommands/commands/suggestions/WarpSuggestion.java index 198a6a51..7736980d 100644 --- a/src/main/java/com/fibermc/essentialcommands/commands/suggestions/WarpSuggestion.java +++ b/src/main/java/com/fibermc/essentialcommands/commands/suggestions/WarpSuggestion.java @@ -8,8 +8,6 @@ public class WarpSuggestion { //Brigader Suggestions public static SuggestionProvider suggestedStrings() { - return (context, builder) -> ListSuggestion.getSuggestionsBuilder(builder, - ManagerLocator.INSTANCE.getWorldDataManager().getWarpNames() - ); + return ListSuggestion.of(() -> ManagerLocator.INSTANCE.getWorldDataManager().getWarpNames()); } } diff --git a/src/main/java/com/fibermc/essentialcommands/config/Config.java b/src/main/java/com/fibermc/essentialcommands/config/Config.java deleted file mode 100644 index 701d2f6b..00000000 --- a/src/main/java/com/fibermc/essentialcommands/config/Config.java +++ /dev/null @@ -1,301 +0,0 @@ -package com.fibermc.essentialcommands.config; - -import com.fibermc.essentialcommands.ECPerms; -import com.fibermc.essentialcommands.EssentialCommands; -import com.fibermc.essentialcommands.util.StringBuilderPlus; -import com.google.gson.JsonParser; -import com.google.gson.JsonSyntaxException; -import net.minecraft.text.Style; -import net.minecraft.text.Text; -import net.minecraft.util.Formatting; -import org.apache.logging.log4j.Level; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; - -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.util.Arrays; -import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; - -import static com.fibermc.essentialcommands.ECPerms.makeNumericPermissionGroup; -import static com.fibermc.essentialcommands.util.TextUtil.parseText; - -public class Config { - - private static SortedProperties props; - private static final String CONFIG_PATH = "./config/EssentialCommands.properties"; - - public static Style FORMATTING_DEFAULT; - public static Style FORMATTING_ACCENT; - public static Style FORMATTING_ERROR; - public static boolean ENABLE_BACK; - public static boolean ENABLE_HOME; - public static boolean ENABLE_SPAWN; - public static boolean ENABLE_TPA; - public static boolean ENABLE_WARP; - public static boolean ENABLE_NICK; - public static boolean ENABLE_RTP; - public static boolean ENABLE_FLY; - public static boolean ENABLE_WORKBENCH; - public static boolean ENABLE_ENDERCHEST; - public static boolean ENABLE_ESSENTIALSX_CONVERT; - public static List HOME_LIMIT; - public static double TELEPORT_COOLDOWN; - public static double TELEPORT_DELAY; - public static boolean ALLOW_BACK_ON_DEATH; - public static int TELEPORT_REQUEST_DURATION; - public static boolean USE_PERMISSIONS_API; - public static boolean CHECK_FOR_UPDATES; - public static boolean TELEPORT_INTERRUPT_ON_DAMAGED; - public static boolean ALLOW_TELEPORT_BETWEEN_DIMENSIONS; - public static boolean OPS_BYPASS_TELEPORT_RULES; - public static boolean NICKNAMES_IN_PLAYER_LIST; - public static Text NICKNAME_PREFIX; - public static int NICKNAME_MAX_LENGTH; - public static int RTP_RADIUS; - public static int RTP_COOLDOWN; - public static int RTP_MAX_ATTEMPTS; - public static boolean BROADCAST_TO_OPS; - public static boolean NICK_REVEAL_ON_HOVER; - public static boolean GRANT_LOWEST_NUMERIC_BY_DEFAULT; - - private static final Option _ENABLE_BACK = new Option<>("enable_back", true, Boolean::parseBoolean); - private static final Option _ENABLE_HOME = new Option<>("enable_home", true, Boolean::parseBoolean); - private static final Option _ENABLE_SPAWN = new Option<>("enable_spawn", true, Boolean::parseBoolean); - private static final Option _ENABLE_TPA = new Option<>("enable_tpa", true, Boolean::parseBoolean); - private static final Option _ENABLE_WARP = new Option<>("enable_warp", true, Boolean::parseBoolean); - private static final Option _ENABLE_NICK = new Option<>("enable_nick", true, Boolean::parseBoolean); - private static final Option _ENABLE_RTP = new Option<>("enable_rtp", true, Boolean::parseBoolean); - private static final Option _ENABLE_FLY = new Option<>("enable_fly", true, Boolean::parseBoolean); - private static final Option _ENABLE_WORKBENCH = new Option<>("enable_workbench", true, Boolean::parseBoolean); - private static final Option _ENABLE_ENDERCHEST = new Option<>("enable_enderchest", true, Boolean::parseBoolean); - private static final Option _ENABLE_ESSENTIALSX_CONVERT = new Option<>("enable_experimental_essentialsx_converter", false, Boolean::parseBoolean); - private static final Option> _HOME_LIMIT = new Option<>("home_limit", List.of(1, 2, 5), arrayParser(Config::parseInt)); - private static final Option _TELEPORT_COOLDOWN = new Option<>("teleport_cooldown", 1.0, Config::parseDouble); - private static final Option _TELEPORT_DELAY = new Option<>("teleport_delay", 0.0, Config::parseDouble); - private static final Option _ALLOW_BACK_ON_DEATH = new Option<>("allow_back_on_death", false, Boolean::parseBoolean); - private static final Option _TELEPORT_REQUEST_DURATION = new Option<>("teleport_request_duration", 60, Config::parseInt); - private static final Option _USE_PERMISSIONS_API = new Option<>("use_permissions_api", false, Boolean::parseBoolean); - private static final Option _CHECK_FOR_UPDATES = new Option<>("check_for_updates", true, Boolean::parseBoolean); - private static final Option _TELEPORT_INTERRUPT_ON_DAMAGED = new Option<>("teleport_interrupt_on_damaged", true, Boolean::parseBoolean); - private static final Option _ALLOW_TELEPORT_BETWEEN_DIMENSIONS = new Option<>("allow_teleport_between_dimensions", true, Boolean::parseBoolean); - private static final Option _OPS_BYPASS_TELEPORT_RULES = new Option<>("ops_bypass_teleport_rules", true, Boolean::parseBoolean); - private static final Option _NICKNAMES_IN_PLAYER_LIST = new Option<>("nicknames_in_player_list", true, Boolean::parseBoolean); - private static final Option _NICKNAME_MAX_LENGTH = new Option<>("nickname_max_length", 32, Config::parseInt); - private static final Option _RTP_RADIUS = new Option<>("rtp_radius", 1000, Config::parseInt); - private static final Option _RTP_COOLDOWN = new Option<>("rtp_cooldown", 30, Config::parseInt); - private static final Option _RTP_MAX_ATTEMPTS = new Option<>("rtp_max_attempts", 15, Config::parseInt); - private static final Option _BROADCAST_TO_OPS = new Option<>("broadcast_to_ops", false, Boolean::parseBoolean); - private static final Option _NICK_REVEAL_ON_HOVER = new Option<>("nick_reveal_on_hover", true, Boolean::parseBoolean); - private static final Option _GRANT_LOWEST_NUMERIC_BY_DEFAULT = new Option<>("grant_lowest_numeric_by_default", true, Boolean::parseBoolean); - - private static final String KEY_FORMATTING_DEFAULT = "formatting_default"; - private static final String KEY_FORMATTING_ACCENT = "formatting_accent"; - private static final String KEY_FORMATTING_ERROR = "formatting_error"; - private static final String KEY_NICKNAME_PREFIX = "nickname_prefix"; - -// static { -// _HOME_LIMIT.CHANGE_EVENT.register((newValue -> -// ECPerms.Registry.Group.home_limit_group = makeNumericPermissionGroup("essentialcommands.home.limit", newValue)) -// ); - // ATM, this does not execute on 1st run, because of default values... -// } - - public static void loadOrCreateProperties() { - props = new SortedProperties(); - File inFile = new File(CONFIG_PATH); - - try{ - boolean fileAlreadyExisted = !inFile.createNewFile(); - if (fileAlreadyExisted) { - props.load(new FileReader(inFile)); - } - } catch (IOException e) { - EssentialCommands.log(Level.WARN,"Failed to load preferences."); - } - initProperties(); - storeProperties(); - } - - private static void initProperties() { - styleJsonDeserializer = new Style.Serializer(); - jsonParser = new JsonParser(); - - FORMATTING_DEFAULT = parseStyleOrDefault((String) props.get(KEY_FORMATTING_DEFAULT), "gold"); - FORMATTING_ACCENT = parseStyleOrDefault((String) props.get(KEY_FORMATTING_ACCENT), "light_purple"); - FORMATTING_ERROR = parseStyleOrDefault((String) props.get(KEY_FORMATTING_ERROR), "red"); - NICKNAME_PREFIX = parseTextOrDefault((String) props.get(KEY_NICKNAME_PREFIX), "{\"text\":\"~\",\"color\":\"red\"}"); - - ENABLE_BACK = _ENABLE_BACK.loadAndSave(props).getValue(); - ENABLE_HOME = _ENABLE_HOME.loadAndSave(props).getValue(); - ENABLE_SPAWN = _ENABLE_SPAWN.loadAndSave(props).getValue(); - ENABLE_TPA = _ENABLE_TPA.loadAndSave(props).getValue(); - ENABLE_WARP = _ENABLE_WARP.loadAndSave(props).getValue(); - ENABLE_NICK = _ENABLE_NICK.loadAndSave(props).getValue(); - ENABLE_RTP = _ENABLE_RTP.loadAndSave(props).getValue(); - ENABLE_FLY = _ENABLE_FLY.loadAndSave(props).getValue(); - ENABLE_WORKBENCH = _ENABLE_WORKBENCH.loadAndSave(props).getValue(); - ENABLE_ENDERCHEST = _ENABLE_ENDERCHEST.loadAndSave(props).getValue(); - ENABLE_ESSENTIALSX_CONVERT = _ENABLE_ESSENTIALSX_CONVERT.loadAndSave(props).getValue(); - HOME_LIMIT = _HOME_LIMIT.loadAndSave(props).getValue(); - ECPerms.Registry.Group.home_limit_group = makeNumericPermissionGroup("essentialcommands.home.limit", HOME_LIMIT); - TELEPORT_COOLDOWN = _TELEPORT_COOLDOWN.loadAndSave(props).getValue(); - TELEPORT_DELAY = _TELEPORT_DELAY.loadAndSave(props).getValue(); - ALLOW_BACK_ON_DEATH = _ALLOW_BACK_ON_DEATH.loadAndSave(props).getValue(); - TELEPORT_REQUEST_DURATION = _TELEPORT_REQUEST_DURATION.loadAndSave(props).getValue(); - USE_PERMISSIONS_API = _USE_PERMISSIONS_API.loadAndSave(props).getValue(); - CHECK_FOR_UPDATES = _CHECK_FOR_UPDATES.loadAndSave(props).getValue(); - TELEPORT_INTERRUPT_ON_DAMAGED = _TELEPORT_INTERRUPT_ON_DAMAGED.loadAndSave(props).getValue(); - ALLOW_TELEPORT_BETWEEN_DIMENSIONS = _ALLOW_TELEPORT_BETWEEN_DIMENSIONS.loadAndSave(props).getValue(); - OPS_BYPASS_TELEPORT_RULES = _OPS_BYPASS_TELEPORT_RULES.loadAndSave(props).getValue(); - NICKNAMES_IN_PLAYER_LIST = _NICKNAMES_IN_PLAYER_LIST.loadAndSave(props).getValue(); - NICKNAME_MAX_LENGTH = _NICKNAME_MAX_LENGTH.loadAndSave(props).getValue(); - RTP_RADIUS = _RTP_RADIUS.loadAndSave(props).getValue(); - RTP_COOLDOWN = _RTP_COOLDOWN.loadAndSave(props).getValue(); - RTP_MAX_ATTEMPTS = _RTP_MAX_ATTEMPTS.loadAndSave(props).getValue(); - BROADCAST_TO_OPS = _BROADCAST_TO_OPS.loadAndSave(props).getValue(); - NICK_REVEAL_ON_HOVER= _NICK_REVEAL_ON_HOVER.loadAndSave(props).getValue(); - GRANT_LOWEST_NUMERIC_BY_DEFAULT = _GRANT_LOWEST_NUMERIC_BY_DEFAULT.loadAndSave(props).getValue(); - - try { - Objects.requireNonNull(FORMATTING_DEFAULT); - Objects.requireNonNull(FORMATTING_ACCENT); - Objects.requireNonNull(FORMATTING_ERROR); - } catch (NullPointerException e) { - EssentialCommands.log(Level.ERROR, "Something went wrong while loading chat styles from EssentialCommands.properties. Additionally, default values could not be loaded."); - e.printStackTrace(); - } - - props.putIfAbsent(KEY_FORMATTING_DEFAULT, String.valueOf(styleJsonDeserializer.serialize(FORMATTING_DEFAULT, null, null))); - props.putIfAbsent(KEY_FORMATTING_ACCENT, String.valueOf(styleJsonDeserializer.serialize(FORMATTING_ACCENT, null, null))); - props.putIfAbsent(KEY_FORMATTING_ERROR, String.valueOf(styleJsonDeserializer.serialize(FORMATTING_ERROR, null, null))); - props.putIfAbsent(KEY_NICKNAME_PREFIX, Text.Serializer.toJson(NICKNAME_PREFIX)); - - } - - private static Style.Serializer styleJsonDeserializer; - private static JsonParser jsonParser; - private static Style parseStyleOrDefault(String styleStr, String defaultStyleStr) { - Style outStyle = null; - if (Objects.nonNull(styleStr)) { - outStyle = parseStyle(styleStr); - } - - if (Objects.isNull(outStyle)) { - outStyle = parseStyle(defaultStyleStr); - EssentialCommands.log( - Level.WARN, - String.format("Could not load malformed style: '%s'. Using default, '%s'.", styleStr, defaultStyleStr) - ); - } - return outStyle; - } - - private static Style parseStyle(String styleStr) { - Style outStyle = null; - Formatting formatting = Formatting.byName(styleStr); - if (Objects.nonNull(formatting)) { - outStyle = Style.EMPTY.withFormatting(formatting); - } - - if (Objects.isNull(outStyle)) { - try { - outStyle = styleJsonDeserializer.deserialize( - jsonParser.parse(styleStr), - null, null - ); - } catch (JsonSyntaxException e) { - EssentialCommands.log(Level.ERROR, String.format( - "Malformed Style JSON in config: %s", styleStr - )); -// e.printStackTrace(); - } - - } - - return outStyle; - } - - private static Text parseTextOrDefault(String textStr, String defaultTextStr) { - Text outText = null; - if (textStr != null) { - outText = parseText(textStr); - } - - if (outText == null) { - outText = parseText(defaultTextStr); - EssentialCommands.log( - Level.WARN, - String.format("Could not load malformed Text: '%s'. Using default, '%s'.", textStr, defaultTextStr) - ); - } - return outText; - } - - public static void storeProperties() { - try{ - File outFile = new File(CONFIG_PATH); - FileWriter writer = new FileWriter(outFile); - - props.storeSorted(writer, new StringBuilderPlus() - .appendLine("Essential Commands Properties") - .append("Config Documentation: https://github.com/John-Paul-R/Essential-Commands/wiki/Config-Documentation") - .toString() - ); - } catch (IOException e) { - EssentialCommands.log(Level.WARN,"Failed to store preferences to disk."); - } - - } - - private static int parseInt(String s) { - try { - return Integer.parseInt(s); - } catch (NumberFormatException e) { - logNumberParseError(s, "int"); - } - return -1; - } - - private static double parseDouble(String s) { - try { - return Double.parseDouble(s); - } catch (NumberFormatException e) { - logNumberParseError(s, "double"); - } - return -1; - } - - @Contract(pure = true) - private static @NotNull ValueParser> csvParser(ValueParser valueParser) { - return (String value) -> parseCsv(value, valueParser); - } - - private static List parseCsv(@NotNull String csvString, @NotNull ValueParser valueParser) { - return Arrays.stream(csvString.split(",")).sequential().map(String::trim) - .map(valueParser::parseValue).collect(Collectors.toList()); - } - - @Contract(pure = true) - private static @NotNull ValueParser> arrayParser(ValueParser valueParser) { - return (String value) -> parseArray(value, valueParser); - } - - private static List parseArray(@NotNull String arrayString, @NotNull ValueParser valueParser) { - int endIdx = arrayString.indexOf(']'); - return parseCsv( - arrayString.substring(arrayString.indexOf('[') + 1, endIdx == -1 ? arrayString.length() : endIdx), - valueParser - ); - } - - private static void logNumberParseError(String num, String type) { - EssentialCommands.log(Level.WARN, String.format( - "Invalid number format for type '%s' in config. Value provided: '%s'", type, num - )); - } -} diff --git a/src/main/java/com/fibermc/essentialcommands/config/EssentialCommandsConfig.java b/src/main/java/com/fibermc/essentialcommands/config/EssentialCommandsConfig.java new file mode 100644 index 00000000..19325a4d --- /dev/null +++ b/src/main/java/com/fibermc/essentialcommands/config/EssentialCommandsConfig.java @@ -0,0 +1,63 @@ +package com.fibermc.essentialcommands.config; + +import com.fibermc.essentialcommands.ECPerms; +import dev.jpcode.eccore.config.Config; +import dev.jpcode.eccore.config.ConfigOption; +import dev.jpcode.eccore.config.ConfigUtil; +import dev.jpcode.eccore.config.Option; +import dev.jpcode.eccore.util.TextUtil; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import dev.jpcode.eccore.config.Option; +import java.nio.file.Path; +import java.util.List; + +import static dev.jpcode.eccore.config.ConfigUtil.arrayParser; +import static dev.jpcode.eccore.config.ConfigUtil.parseStyle; +import static dev.jpcode.eccore.util.TextUtil.parseText; + +public final class EssentialCommandsConfig extends Config { + + @ConfigOption public final Option