All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.hendrixshen.magiclib.carpet.impl.Validators Maven / Gradle / Ivy

There is a newer version: 0.7.398+fe2125a-beta
Show newest version
package top.hendrixshen.magiclib.carpet.impl;

import carpet.CarpetServer;
import com.google.common.collect.ImmutableList;
import org.jetbrains.annotations.NotNull;
import top.hendrixshen.magiclib.carpet.api.Validator;
import top.hendrixshen.magiclib.compat.minecraft.api.network.chat.ComponentCompatApi;
import top.hendrixshen.magiclib.impl.carpet.CarpetEntrypoint;
import top.hendrixshen.magiclib.util.MessageUtil;

import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import net.minecraft.class_124;
import net.minecraft.class_2168;

//#if MC >= 11901
//$$ @SuppressWarnings("removal")
//#endif
public class Validators {
    public static class Strict extends Validator {
        @Override
        public T getValidValue(class_2168 source, @NotNull RuleOption ruleOption, T newValue, String userInput) {
            if (ruleOption.getOptions().contains(userInput)) {
                return newValue;
            }

            MessageUtil.sendMessage(source, ComponentCompatApi.literal(CarpetEntrypoint.getSettingManager()
                    .trValidator("strict.validValue", ruleOption.getOptions().toString())).method_10859(style -> style.method_10977(class_124.field_1061)));
            return null;
        }
    }

    public static class StrictIgnoreCase extends Validator {
        @Override
        public T getValidValue(class_2168 source, @NotNull RuleOption ruleOption, T newValue, @NotNull String userInput) {
            if (ruleOption.getOptions().stream().map(s -> s.toLowerCase(Locale.ROOT)).collect(Collectors.toList()).contains(userInput.toLowerCase(Locale.ROOT))) {
                return newValue;
            }

            MessageUtil.sendMessage(source, ComponentCompatApi.literal(CarpetEntrypoint.getSettingManager()
                    .trValidator("strictIgnoreCase.validValue", ruleOption.getOptions().toString())).method_10859(style -> style.method_10977(class_124.field_1061)));
            return null;
        }
    }

    public static class Command extends Validator {
        public static final List FULL_OPTIONS = ImmutableList.of("true", "false", "ops", "0", "1", "2", "3", "4");
        public static final List MINIMAL_OPTIONS = ImmutableList.of("true", "false", "ops");

        @Override
        public String getValidValue(class_2168 source, RuleOption ruleOption, @NotNull String newValue, String userInput) {
            if (FULL_OPTIONS.contains(newValue.toLowerCase(Locale.ROOT))) {
                if (CarpetServer.settingsManager != null) {
                    CarpetServer.settingsManager.notifyPlayersCommandsChanged();
                }

                return newValue.toLowerCase(Locale.ROOT);
            }

            MessageUtil.sendMessage(source, ComponentCompatApi.literal(CarpetEntrypoint.getSettingManager()
                    .trValidator("command.validValue", ruleOption.getOptions().toString())).method_10859(style -> style.method_10977(class_124.field_1061)));
            return null;
        }
    }

    public static class Numeric extends Validator {
        private final double max;
        private final double min;
        private final boolean canMaxEquals;
        private final boolean canMinEquals;

        public Numeric(double max, double min, boolean canMaxEquals, boolean canMinEquals) {
            this.max = Math.max(min, max);
            this.min = Math.min(min, max);
            this.canMaxEquals = canMaxEquals;
            this.canMinEquals = canMinEquals;
        }

        @Override
        public T getValidValue(class_2168 source, RuleOption ruleOption, T newValue, String userInput) {
            if (!(newValue instanceof Number)) {
                throw new IllegalArgumentException("Illegal validator call.");
            }

            Number number = (Number) newValue;

            if ((this.canMaxEquals && number.doubleValue() > this.max) ||
                    (!this.canMaxEquals && number.doubleValue() >= this.max) ||
                    (this.canMinEquals && number.doubleValue() < this.min) ||
                    (!this.canMinEquals && number.doubleValue() <= this.min)) {
                MessageUtil.sendMessage(source, ComponentCompatApi.literal(CarpetEntrypoint.getSettingManager()
                        .trValidator("numeric.validValue", String.format("%s%s, %s%s",
                                this.canMinEquals ? "[" : "(", this.min, this.max, this.canMaxEquals ? "]" : ")")))
                        .method_10859(style -> style.method_10977(class_124.field_1061)));
                return null;
            }

            return newValue;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy