mirror of
https://github.com/EngineHub/WorldGuard.git
synced 2025-01-03 15:08:02 +01:00
Extract NumberFlag for suggestions, make FlagHelper prettier in console.
This commit is contained in:
parent
bbed280f62
commit
e59d61e1cf
@ -38,11 +38,10 @@
|
||||
import com.sk89q.worldguard.WorldGuard;
|
||||
import com.sk89q.worldguard.internal.permission.RegionPermissionModel;
|
||||
import com.sk89q.worldguard.protection.flags.BooleanFlag;
|
||||
import com.sk89q.worldguard.protection.flags.DoubleFlag;
|
||||
import com.sk89q.worldguard.protection.flags.Flag;
|
||||
import com.sk89q.worldguard.protection.flags.Flags;
|
||||
import com.sk89q.worldguard.protection.flags.IntegerFlag;
|
||||
import com.sk89q.worldguard.protection.flags.LocationFlag;
|
||||
import com.sk89q.worldguard.protection.flags.NumberFlag;
|
||||
import com.sk89q.worldguard.protection.flags.RegistryFlag;
|
||||
import com.sk89q.worldguard.protection.flags.SetFlag;
|
||||
import com.sk89q.worldguard.protection.flags.StateFlag;
|
||||
@ -81,6 +80,7 @@ class FlagHelperBox extends PaginationBox {
|
||||
private final World world;
|
||||
private final ProtectedRegion region;
|
||||
private final RegionPermissionModel perms;
|
||||
private boolean monoSpace;
|
||||
|
||||
FlagHelperBox(World world, ProtectedRegion region, RegionPermissionModel perms) {
|
||||
super("Flags for " + region.getId(), "/rg flags -w " + world.getName() + " -p %page% " + region.getId());
|
||||
@ -115,25 +115,25 @@ private Component createLine(Flag<?> flag, boolean thirdParty) {
|
||||
|
||||
private void appendFlagName(TextComponent.Builder builder, Flag<?> flag, TextColor color) {
|
||||
final String name = flag.getName();
|
||||
int length = FlagFontInfo.getPxLength(name);
|
||||
int length = monoSpace ? name.length() : FlagFontInfo.getPxLength(name);
|
||||
builder.append(TextComponent.of(name, color));
|
||||
if (flag.usesMembershipAsDefault()) {
|
||||
builder.append(TextComponent.empty().append(TextComponent.of("*", TextColor.AQUA))
|
||||
.hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT,
|
||||
TextComponent.of("This is a special flag which defaults to allow for members, and deny for non-members"))));
|
||||
length += FlagFontInfo.getPxLength('*');
|
||||
length += monoSpace ? 1 : FlagFontInfo.getPxLength('*');
|
||||
}
|
||||
if (flag == Flags.PASSTHROUGH) {
|
||||
builder.append(TextComponent.empty().append(TextComponent.of("*", TextColor.AQUA))
|
||||
.hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT,
|
||||
TextComponent.of("This is a special flag which overrides build checks. (Not movement related!)"))));
|
||||
length += FlagFontInfo.getPxLength('*');
|
||||
length += monoSpace ? 1 : FlagFontInfo.getPxLength('*');
|
||||
}
|
||||
int leftover = PAD_PX_SIZE - length;
|
||||
int leftover = (monoSpace ? PAD_PX_SIZE / 3 : PAD_PX_SIZE) - length;
|
||||
builder.append(TextComponent.space());
|
||||
leftover -= 4;
|
||||
if (leftover > 0) {
|
||||
builder.append(TextComponent.of(Strings.repeat(".", leftover / 2), TextColor.DARK_GRAY));
|
||||
builder.append(TextComponent.of(Strings.repeat(".", monoSpace ? leftover : leftover / 2), TextColor.DARK_GRAY));
|
||||
}
|
||||
}
|
||||
|
||||
@ -150,10 +150,8 @@ private void appendFlagValue(TextComponent.Builder builder, Flag<?> flag) {
|
||||
appendStringFlagValue(builder, ((StringFlag) flag));
|
||||
} else if (flag instanceof LocationFlag) {
|
||||
appendLocationFlagValue(builder, ((LocationFlag) flag));
|
||||
} else if (flag instanceof IntegerFlag) {
|
||||
appendNumericFlagValue(builder, (IntegerFlag) flag);
|
||||
} else if (flag instanceof DoubleFlag) {
|
||||
appendNumericFlagValue(builder, (DoubleFlag) flag);
|
||||
} else if (flag instanceof NumberFlag) {
|
||||
appendNumericFlagValue(builder, (NumberFlag<? extends Number>) flag);
|
||||
} else {
|
||||
String display = String.valueOf(region.getFlag(flag));
|
||||
if (display.length() > 23) {
|
||||
@ -404,13 +402,13 @@ private void appendLocationFlagValue(TextComponent.Builder builder, LocationFlag
|
||||
}
|
||||
}
|
||||
|
||||
private <V extends Number> void appendNumericFlagValue(TextComponent.Builder builder, Flag<V> flag) {
|
||||
private <V extends Number> void appendNumericFlagValue(TextComponent.Builder builder, NumberFlag<V> flag) {
|
||||
Number currVal = region.getFlag(flag);
|
||||
if (currVal == null) {
|
||||
currVal = getInheritedValue(region, flag);
|
||||
}
|
||||
Number defVal = flag.getDefault();
|
||||
Number[] suggested = getSuggestedNumbers(flag);
|
||||
Number[] suggested = flag.getSuggestedValues();
|
||||
SortedSet<Number> choices = new TreeSet<>(Comparator.comparing(Number::doubleValue));
|
||||
if (currVal != null) {
|
||||
choices.add(currVal);
|
||||
@ -425,19 +423,6 @@ private <V extends Number> void appendNumericFlagValue(TextComponent.Builder bui
|
||||
appendValueChoices(builder, flag, (Iterator<V>) choices.iterator(), choices.isEmpty() ? "unset number" : "[custom]");
|
||||
}
|
||||
|
||||
private Number[] getSuggestedNumbers(Flag<? extends Number> flag) {
|
||||
if (flag == Flags.HEAL_AMOUNT || flag == Flags.FEED_AMOUNT) {
|
||||
return new Number[]{0, 5, 10, 20};
|
||||
} else if (flag == Flags.MIN_FOOD || flag == Flags.MIN_HEAL) {
|
||||
return new Number[]{0, 10};
|
||||
} else if (flag == Flags.MAX_FOOD || flag == Flags.MAX_HEAL) {
|
||||
return new Number[]{10, 20};
|
||||
} else if (flag == Flags.HEAL_DELAY || flag == Flags.FEED_DELAY) {
|
||||
return new Number[]{0, 1, 5};
|
||||
}
|
||||
return new Number[0];
|
||||
}
|
||||
|
||||
private void appendStringFlagValue(TextComponent.Builder builder, StringFlag flag) {
|
||||
String currVal = region.getFlag(flag);
|
||||
if (currVal == null) {
|
||||
@ -486,6 +471,10 @@ private static void appendTextTo(StringBuilder builder, Component component) {
|
||||
}
|
||||
}
|
||||
|
||||
void tryMonoSpacing() {
|
||||
this.monoSpace = true;
|
||||
}
|
||||
|
||||
private static final class FlagFontInfo {
|
||||
static int getPxLength(char c) {
|
||||
switch (c) {
|
||||
|
@ -676,6 +676,9 @@ public void flagHelper(CommandContext args, Actor sender) throws CommandExceptio
|
||||
private static void sendFlagHelper(Actor sender, World world, ProtectedRegion region, RegionPermissionModel perms, int page) {
|
||||
final FlagHelperBox flagHelperBox = new FlagHelperBox(world, region, perms);
|
||||
flagHelperBox.setComponentsPerPage(18);
|
||||
if (!sender.isPlayer()) {
|
||||
flagHelperBox.tryMonoSpacing();
|
||||
}
|
||||
AsyncCommandBuilder.wrap(() -> flagHelperBox.create(page), sender)
|
||||
.onSuccess((Component) null, sender::print)
|
||||
.onFailure("Failed to get region flags", WorldGuard.getInstance().getExceptionConverter())
|
||||
|
@ -230,7 +230,7 @@ protected static Region checkSelection(LocalPlayer player) throws CommandExcepti
|
||||
throw new CommandException(
|
||||
"Please select an area first. " +
|
||||
"Use WorldEdit to make a selection! " +
|
||||
"(wiki: http://wiki.sk89q.com/wiki/WorldEdit).");
|
||||
"(see: https://worldedit.enginehub.org/en/latest/usage/regions/selections/).");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@
|
||||
/**
|
||||
* Stores doubles.
|
||||
*/
|
||||
public class DoubleFlag extends Flag<Double> {
|
||||
public class DoubleFlag extends NumberFlag<Double> {
|
||||
|
||||
public DoubleFlag(String name, RegionGroup defaultGroup) {
|
||||
super(name, defaultGroup);
|
||||
|
@ -34,6 +34,7 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* The flags that are used in WorldGuard.
|
||||
@ -156,15 +157,20 @@ public final class Flags {
|
||||
|
||||
public static final RegistryFlag<GameMode> GAME_MODE = register(new RegistryFlag<>("game-mode", GameMode.REGISTRY));
|
||||
|
||||
public static final IntegerFlag HEAL_DELAY = register(new IntegerFlag("heal-delay"));
|
||||
public static final IntegerFlag HEAL_AMOUNT = register(new IntegerFlag("heal-amount"));
|
||||
public static final DoubleFlag MIN_HEAL = register(new DoubleFlag("heal-min-health"));
|
||||
public static final DoubleFlag MAX_HEAL = register(new DoubleFlag("heal-max-health"));
|
||||
private static final Number[] DELAY_VALUES = {0, 1, 5};
|
||||
private static final Number[] VITALS_VALUES = {0, 5, 10, 20};
|
||||
private static final Number[] VITALS_MINS = {0, 10};
|
||||
private static final Number[] VITALS_MAXS = {10, 20};
|
||||
|
||||
public static final IntegerFlag FEED_DELAY = register(new IntegerFlag("feed-delay"));
|
||||
public static final IntegerFlag FEED_AMOUNT = register(new IntegerFlag("feed-amount"));
|
||||
public static final IntegerFlag MIN_FOOD = register(new IntegerFlag("feed-min-hunger"));
|
||||
public static final IntegerFlag MAX_FOOD = register(new IntegerFlag("feed-max-hunger"));
|
||||
public static final IntegerFlag HEAL_DELAY = register(new IntegerFlag("heal-delay"), f -> f.setSuggestedValues(DELAY_VALUES));
|
||||
public static final IntegerFlag HEAL_AMOUNT = register(new IntegerFlag("heal-amount"), f -> f.setSuggestedValues(VITALS_VALUES));
|
||||
public static final DoubleFlag MIN_HEAL = register(new DoubleFlag("heal-min-health"), f -> f.setSuggestedValues(VITALS_MINS));
|
||||
public static final DoubleFlag MAX_HEAL = register(new DoubleFlag("heal-max-health"), f -> f.setSuggestedValues(VITALS_MAXS));
|
||||
|
||||
public static final IntegerFlag FEED_DELAY = register(new IntegerFlag("feed-delay"), f -> f.setSuggestedValues(DELAY_VALUES));
|
||||
public static final IntegerFlag FEED_AMOUNT = register(new IntegerFlag("feed-amount"), f -> f.setSuggestedValues(VITALS_VALUES));
|
||||
public static final IntegerFlag MIN_FOOD = register(new IntegerFlag("feed-min-hunger"), f -> f.setSuggestedValues(VITALS_MINS));
|
||||
public static final IntegerFlag MAX_FOOD = register(new IntegerFlag("feed-max-hunger"), f -> f.setSuggestedValues(VITALS_MAXS));
|
||||
|
||||
// deny messages
|
||||
public static final StringFlag DENY_MESSAGE = register(new StringFlag("deny-message",
|
||||
@ -189,6 +195,12 @@ private static <T extends Flag<?>> T register(final T flag) throws FlagConflictE
|
||||
return flag;
|
||||
}
|
||||
|
||||
private static <T extends Flag<?>> T register(final T flag, Consumer<T> cfg) throws FlagConflictException {
|
||||
T f = register(flag);
|
||||
cfg.accept(f);
|
||||
return f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to match the flag with the given ID using a fuzzy name match.
|
||||
*
|
||||
|
@ -22,7 +22,7 @@
|
||||
/**
|
||||
* Stores an integer.
|
||||
*/
|
||||
public class IntegerFlag extends Flag<Integer> {
|
||||
public class IntegerFlag extends NumberFlag<Integer> {
|
||||
|
||||
public IntegerFlag(String name, RegionGroup defaultGroup) {
|
||||
super(name, defaultGroup);
|
||||
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* WorldGuard, a suite of tools for Minecraft
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldGuard team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldguard.protection.flags;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
/**
|
||||
* Stores an Number.
|
||||
*/
|
||||
public abstract class NumberFlag<T extends Number> extends Flag<T> {
|
||||
|
||||
private static final Number[] EMPTY_NUMBER_ARRAY = new Number[0];
|
||||
private Number[] suggestions = EMPTY_NUMBER_ARRAY;
|
||||
|
||||
protected NumberFlag(String name, RegionGroup defaultGroup) {
|
||||
super(name, defaultGroup);
|
||||
}
|
||||
|
||||
protected NumberFlag(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public void setSuggestedValues(Number[] values) {
|
||||
this.suggestions = checkNotNull(values);
|
||||
}
|
||||
|
||||
public Number[] getSuggestedValues() {
|
||||
return suggestions;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user