Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum MVCorei18n implements MessageKeyProvider {
CONFIG_STRING_PARSER_INVALIDDOUBLE,
CONFIG_STRING_PARSER_INVALIDFLOAT,
CONFIG_STRING_PARSER_INVALIDLONG,
CONFIG_STRING_PARSER_INVALIDMATERIAL,

// core config
CORE_CONFIG_CUSTOMPORTALSEARCHRADIUS_NONNEGATIVE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.dumptruckman.minecraft.util.Logging;
import io.vavr.control.Option;
import io.vavr.control.Try;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
Expand All @@ -26,6 +27,9 @@
import org.mvplugins.multiverse.core.config.node.ListConfigNode;
import org.mvplugins.multiverse.core.config.node.NodeGroup;
import org.mvplugins.multiverse.core.economy.MVEconomist;
import org.mvplugins.multiverse.core.exceptions.MultiverseException;
import org.mvplugins.multiverse.core.locale.MVCorei18n;
import org.mvplugins.multiverse.core.locale.message.Message;
import org.mvplugins.multiverse.core.utils.MaterialConverter;
import org.mvplugins.multiverse.core.utils.text.ChatTextFormatter;
import org.mvplugins.multiverse.core.world.helpers.AliasNameConflictChecker;
Expand All @@ -35,6 +39,8 @@
import org.mvplugins.multiverse.core.world.location.SpawnLocation;
import org.mvplugins.multiverse.core.world.entity.EntitySpawnConfig;

import static org.mvplugins.multiverse.core.locale.message.MessageReplacement.replace;

/**
* Represents nodes in a world configuration.
*/
Expand Down Expand Up @@ -169,11 +175,22 @@ private <T> ConfigNode<T> node(ConfigNode.Builder<T, ?> nodeBuilder) {
final ConfigNode<Material> entryFeeCurrency = node(ConfigNode.builder("entry-fee.currency", Material.class)
.defaultValue(MVEconomist.VAULT_ECONOMY_MATERIAL)
.name("entryfee-currency")
.stringParser((value, clazz) -> {
if (value != null && value.equalsIgnoreCase(MVEconomist.VAULT_ECONOMY_CODE)) {
return Try.success(MVEconomist.VAULT_ECONOMY_MATERIAL);
}
return Try.of(() -> MaterialConverter.stringToMaterial(value))
.filter(Objects::nonNull)
.orElse(Try.failure(new MultiverseException(Message.of(
MVCorei18n.CONFIG_STRING_PARSER_INVALIDMATERIAL,
replace("{input}").with(value)))));
})
.serializer(new NodeSerializer<>() {
@Override
public Material deserialize(Object object, Class<Material> type) {
return Option.of(object)
.map(String::valueOf)
.filter(s -> !s.isBlank())
.map(materialStr -> {
if (materialStr.equalsIgnoreCase(MVEconomist.VAULT_ECONOMY_CODE)) {
return MVEconomist.VAULT_ECONOMY_MATERIAL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@
import org.bukkit.entity.EntityType;
import org.bukkit.entity.SpawnCategory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mvplugins.multiverse.core.config.CoreConfig;
import org.mvplugins.multiverse.core.config.handle.MemoryConfigurationHandle;
import org.mvplugins.multiverse.core.config.handle.StringPropertyHandle;
import org.mvplugins.multiverse.core.config.node.ConfigNode;
import org.mvplugins.multiverse.core.config.node.ListConfigNode;
import org.mvplugins.multiverse.core.config.node.Node;
import org.mvplugins.multiverse.core.config.node.NodeGroup;
import org.mvplugins.multiverse.core.config.node.functions.NodeStringParser;
import org.mvplugins.multiverse.core.config.node.serializer.NodeSerializer;
import org.mvplugins.multiverse.core.exceptions.MultiverseException;
import org.mvplugins.multiverse.core.locale.MVCorei18n;
import org.mvplugins.multiverse.core.locale.message.Message;
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
import org.mvplugins.multiverse.core.world.MultiverseWorld;

import java.util.ArrayList;
import java.util.List;

import static org.mvplugins.multiverse.core.locale.message.MessageReplacement.replace;

public final class SpawnCategoryConfig {

private final CoreConfig config;
Expand Down Expand Up @@ -177,6 +184,7 @@
final ConfigNode<Integer> tickRate = node(ConfigNode.builder("tick-rate", Integer.class)
.defaultValue(-2)
.suggester(input -> List.of("@unset", "@bukkit", "10", "100", "400", "1000"))
.stringParser(SpawnValueStringParser.INSTANCE)
.serializer(SpawnValueSerializer.INSTANCE)
.onLoadAndChange((oldValue, newValue) -> applyConfigToWorld())
.onChange((sender, oldValue, newValue) -> {
Expand All @@ -193,6 +201,7 @@
final ConfigNode<Integer> spawnLimit = node(ConfigNode.builder("spawn-limit", Integer.class)
.defaultValue(-2)
.suggester(input -> List.of("@unset", "@bukkit", "10", "100", "400", "1000"))
.stringParser(SpawnValueStringParser.INSTANCE)
.serializer(SpawnValueSerializer.INSTANCE)
.onLoadAndChange((oldValue, newValue) -> applyConfigToWorld())
.onChange((sender, oldValue, newValue) -> {
Expand All @@ -215,6 +224,27 @@
.build());
}

private static class SpawnValueStringParser implements NodeStringParser<Integer> {

Check warning on line 227 in src/main/java/org/mvplugins/multiverse/core/world/entity/SpawnCategoryConfig.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Class SpawnValueStringParser should be declared as final. Raw Output: /github/workspace/src/main/java/org/mvplugins/multiverse/core/world/entity/SpawnCategoryConfig.java:227:5: warning: Class SpawnValueStringParser should be declared as final. (com.puppycrawl.tools.checkstyle.checks.design.FinalClassCheck)
private static final SpawnValueStringParser INSTANCE = new SpawnValueStringParser();

@Override

Check warning on line 230 in src/main/java/org/mvplugins/multiverse/core/world/entity/SpawnCategoryConfig.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Return count is 4 (max allowed for non-void methods/lambdas is 2). Raw Output: /github/workspace/src/main/java/org/mvplugins/multiverse/core/world/entity/SpawnCategoryConfig.java:230:9: info: Return count is 4 (max allowed for non-void methods/lambdas is 2). (com.puppycrawl.tools.checkstyle.checks.coding.ReturnCountCheck)
public @NotNull Try<Integer> parse(@Nullable String input, @NotNull Class<Integer> type) {
String str = String.valueOf(input).trim();
if (str.equalsIgnoreCase("@unset")) {

Check warning on line 233 in src/main/java/org/mvplugins/multiverse/core/world/entity/SpawnCategoryConfig.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 String literal expressions should be on the left side of an equalsIgnoreCase comparison. Raw Output: /github/workspace/src/main/java/org/mvplugins/multiverse/core/world/entity/SpawnCategoryConfig.java:233:37: warning: String literal expressions should be on the left side of an equalsIgnoreCase comparison. (com.puppycrawl.tools.checkstyle.checks.coding.EqualsAvoidNullCheck)
return Try.success(-2);

Check warning on line 234 in src/main/java/org/mvplugins/multiverse/core/world/entity/SpawnCategoryConfig.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 '-2' is a magic number. Raw Output: /github/workspace/src/main/java/org/mvplugins/multiverse/core/world/entity/SpawnCategoryConfig.java:234:36: warning: '-2' is a magic number. (com.puppycrawl.tools.checkstyle.checks.coding.MagicNumberCheck)
}
if (str.equalsIgnoreCase("@bukkit")) {

Check warning on line 236 in src/main/java/org/mvplugins/multiverse/core/world/entity/SpawnCategoryConfig.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 String literal expressions should be on the left side of an equalsIgnoreCase comparison. Raw Output: /github/workspace/src/main/java/org/mvplugins/multiverse/core/world/entity/SpawnCategoryConfig.java:236:37: warning: String literal expressions should be on the left side of an equalsIgnoreCase comparison. (com.puppycrawl.tools.checkstyle.checks.coding.EqualsAvoidNullCheck)
return Try.success(-1);
}
try {
return Try.success(Integer.parseInt(str));
} catch (NumberFormatException e) {
return Try.failure(new MultiverseException(Message.of(MVCorei18n.CONFIG_STRING_PARSER_INVALIDINTEGER,
replace("{input}").with(input))));
}
}
}

private static class SpawnValueSerializer implements NodeSerializer<Integer> {
private static final SpawnValueSerializer INSTANCE = new SpawnValueSerializer();

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/multiverse-core_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mv-core.config.string.parser.invalidinteger=Unable to convert '{input}' to numbe
mv-core.config.string.parser.invaliddouble=Unable to convert '{input}' to number. (double)
mv-core.config.string.parser.invalidfloat=Unable to convert '{input}' to number. (float)
mv-core.config.string.parser.invalidlong=Unable to convert '{input}' to number. (long)
mv-core.config.string.parser.invalidmaterial='{input}' is not a valid material.

# core config
mv-core.core.config.customportalsearchradius.nonnegative=The value must be greater than or equal to 0.
Expand Down
Loading