diff --git a/src/main/java/org/mvplugins/multiverse/core/configuration/migration/DoubleMigrationAction.java b/src/main/java/org/mvplugins/multiverse/core/configuration/migration/DoubleMigrationAction.java new file mode 100644 index 00000000..507accec --- /dev/null +++ b/src/main/java/org/mvplugins/multiverse/core/configuration/migration/DoubleMigrationAction.java @@ -0,0 +1,24 @@ +package org.mvplugins.multiverse.core.configuration.migration; + +import co.aikar.commands.ACFUtil; +import com.dumptruckman.minecraft.util.Logging; +import org.bukkit.configuration.ConfigurationSection; + +public class DoubleMigrationAction implements MigratorAction { + + public static DoubleMigrationAction of(String path) { + return new DoubleMigrationAction(path); + } + + private final String path; + + public DoubleMigrationAction(String path) { + this.path = path; + } + + @Override + public void migrate(ConfigurationSection config) { + config.set(path, ACFUtil.parseDouble(config.getString(path))); + Logging.info("Converted %s to double %s", path, config.getDouble(path)); + } +} diff --git a/src/main/java/org/mvplugins/multiverse/core/economy/MVEconomist.java b/src/main/java/org/mvplugins/multiverse/core/economy/MVEconomist.java index 5951c74f..073faa0d 100644 --- a/src/main/java/org/mvplugins/multiverse/core/economy/MVEconomist.java +++ b/src/main/java/org/mvplugins/multiverse/core/economy/MVEconomist.java @@ -15,7 +15,7 @@ import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld; */ @Service public class MVEconomist { - public static final Material DISABLED_MATERIAL = Material.AIR; + public static final Material VAULT_ECONOMY_MATERIAL = Material.AIR; private final VaultHandler vaultHandler; @@ -238,7 +238,7 @@ public class MVEconomist { * @return true if currency string matches a valid material. */ public static boolean isItemCurrency(Material currency) { - return currency != null; + return currency != VAULT_ECONOMY_MATERIAL; } } diff --git a/src/main/java/org/mvplugins/multiverse/core/utils/MaterialConverter.java b/src/main/java/org/mvplugins/multiverse/core/utils/MaterialConverter.java deleted file mode 100644 index 7310d5a4..00000000 --- a/src/main/java/org/mvplugins/multiverse/core/utils/MaterialConverter.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.mvplugins.multiverse.core.utils; - -import de.themoep.idconverter.IdMappings; -import org.bukkit.Material; -import org.bukkit.configuration.ConfigurationSection; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -/** - * A tool for converting values which may be an old type ID to a Material. - */ -public class MaterialConverter { - - /** - * Converts the value in the given config at the given path from a numeric id or flattened material name to a - * Material. - * - * @param config The config with the value to convert. - * @param path The path of the value in the config. - * @return The converted Material type or null if no matching type. - */ - @Nullable - public static Material convertConfigType(@NotNull ConfigurationSection config, @NotNull String path) { - return convertTypeString(config.getString(path)); - } - - /** - * Converts a string representing a numeric id or flattened material name to a Material. - * - * @param value The value to convert. - * @return The converted Material type or null if no matching type. - */ - @Nullable - public static Material convertTypeString(@Nullable String value) { - IdMappings.Mapping mapping = IdMappings.getById(value != null ? value : ""); - if (mapping != null) { - return Material.matchMaterial(mapping.getFlatteningType()); - } else { - return Material.matchMaterial(value != null ? value : ""); - } - } -} diff --git a/src/main/java/org/mvplugins/multiverse/core/world/MultiverseWorld.java b/src/main/java/org/mvplugins/multiverse/core/world/MultiverseWorld.java index 6e8b7ae7..b6656d52 100644 --- a/src/main/java/org/mvplugins/multiverse/core/world/MultiverseWorld.java +++ b/src/main/java/org/mvplugins/multiverse/core/world/MultiverseWorld.java @@ -387,6 +387,25 @@ public class MultiverseWorld { return worldConfig.setPortalForm(portalForm); } + /** + * Gets if entry fee is needed when entering this world + * + * @return True if entry fee is needed + */ + public boolean isEntryFeeEnabled() { + return worldConfig.isEntryFeeEnabled(); + } + + /** + * Sets if entry fee is needed when entering this world + * + * @param entryFeeEnabled True to enable use of entry fee + * @return Result of setting property. + */ + public Try setEntryFeeEnabled(boolean entryFeeEnabled) { + return worldConfig.setEntryFeeEnabled(entryFeeEnabled); + } + /** * Gets the amount of currency it requires to enter this world. * diff --git a/src/main/java/org/mvplugins/multiverse/core/world/config/CurrencySerializer.java b/src/main/java/org/mvplugins/multiverse/core/world/config/CurrencySerializer.java new file mode 100644 index 00000000..620b2e34 --- /dev/null +++ b/src/main/java/org/mvplugins/multiverse/core/world/config/CurrencySerializer.java @@ -0,0 +1,59 @@ +package org.mvplugins.multiverse.core.world.config; + +import de.themoep.idconverter.IdMappings; +import io.vavr.control.Option; +import org.bukkit.Material; +import org.jetbrains.annotations.Nullable; +import org.mvplugins.multiverse.core.configuration.functions.NodeSerializer; +import org.mvplugins.multiverse.core.economy.MVEconomist; + +/** + * Converts the material name to/from a {@link Material} enum, with the special case of "vault-economy" + * for world configuration. + */ +public class CurrencySerializer implements NodeSerializer { + + static final String VAULT_ECONOMY_CODE = "@vault-economy"; + + /** + * {@inheritDoc} + */ + @Override + public Material deserialize(Object object, Class type) { + return Option.of(object) + .map(String::valueOf) + .map(materialStr -> { + if (materialStr.equalsIgnoreCase(VAULT_ECONOMY_CODE)) { + return MVEconomist.VAULT_ECONOMY_MATERIAL; + } + return stringToMaterial(materialStr); + }) + .getOrElse(MVEconomist.VAULT_ECONOMY_MATERIAL); + } + + /** + * Converts a string representing a numeric id or flattened material name to a Material. + * + * @param value The value to convert. + * @return The converted Material type or null if no matching type. + */ + @Nullable + private Material stringToMaterial(@Nullable String value) { + IdMappings.Mapping mapping = IdMappings.getById(value != null ? value : ""); + if (mapping != null) { + return Material.matchMaterial(mapping.getFlatteningType()); + } else { + return Material.matchMaterial(value != null ? value : ""); + } + } + + /** + * {@inheritDoc} + */ + @Override + public Object serialize(Material object, Class type) { + return Option.of(object) + .map(material -> material == MVEconomist.VAULT_ECONOMY_MATERIAL ? VAULT_ECONOMY_CODE : material.name()) + .getOrElse(VAULT_ECONOMY_CODE); + } +} diff --git a/src/main/java/org/mvplugins/multiverse/core/world/config/EntryFeeMigrator.java b/src/main/java/org/mvplugins/multiverse/core/world/config/EntryFeeMigrator.java new file mode 100644 index 00000000..9343b9aa --- /dev/null +++ b/src/main/java/org/mvplugins/multiverse/core/world/config/EntryFeeMigrator.java @@ -0,0 +1,22 @@ +package org.mvplugins.multiverse.core.world.config; + +import com.dumptruckman.minecraft.util.Logging; +import org.bukkit.configuration.ConfigurationSection; +import org.mvplugins.multiverse.core.configuration.migration.MigratorAction; + +/** + * Migrates the entry fee settings. Assumes entry fee is disabled if currency is not set. + */ +public class EntryFeeMigrator implements MigratorAction { + @Override + public void migrate(ConfigurationSection config) { + String currency = config.getString("entry-fee.currency", ""); + Logging.info("Entry fee currency: %s", currency); + if (currency.isEmpty()) { + config.set("entry-fee.enabled", false); + config.set("entry-fee.currency", CurrencySerializer.VAULT_ECONOMY_CODE); + } else { + config.set("entry-fee.enabled", true); + } + } +} diff --git a/src/main/java/org/mvplugins/multiverse/core/world/config/WorldConfig.java b/src/main/java/org/mvplugins/multiverse/core/world/config/WorldConfig.java index fd6870bd..85f306c4 100644 --- a/src/main/java/org/mvplugins/multiverse/core/world/config/WorldConfig.java +++ b/src/main/java/org/mvplugins/multiverse/core/world/config/WorldConfig.java @@ -17,13 +17,7 @@ import org.jetbrains.annotations.Nullable; import org.mvplugins.multiverse.core.MultiverseCore; import org.mvplugins.multiverse.core.configuration.handle.ConfigurationSectionHandle; import org.mvplugins.multiverse.core.configuration.handle.StringPropertyHandle; -import org.mvplugins.multiverse.core.configuration.migration.BooleanMigratorAction; -import org.mvplugins.multiverse.core.configuration.migration.ConfigMigrator; -import org.mvplugins.multiverse.core.configuration.migration.IntegerMigratorAction; -import org.mvplugins.multiverse.core.configuration.migration.LongMigratorAction; -import org.mvplugins.multiverse.core.configuration.migration.MoveMigratorAction; -import org.mvplugins.multiverse.core.configuration.migration.NullStringMigratorAction; -import org.mvplugins.multiverse.core.configuration.migration.VersionMigrator; +import org.mvplugins.multiverse.core.configuration.migration.*; import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld; /** @@ -68,6 +62,7 @@ public final class WorldConfig { .addAction(BooleanMigratorAction.of("bed-respawn")) //.addAction(MoveMigratorAction.of("difficulty", "difficulty")) .addAction(MoveMigratorAction.of("entryfee.amount", "entry-fee.amount")) + .addAction(DoubleMigrationAction.of("entry-fee.amount")) .addAction(MoveMigratorAction.of("entryfee.currency", "entry-fee.currency")) //.addAction(MoveMigratorAction.of("environment", "environment")) .addAction(MoveMigratorAction.of("gameMode", "gamemode")) @@ -100,6 +95,7 @@ public final class WorldConfig { .addAction(IntegerMigratorAction.of("spawning.monsters.tick-rate")) //.addAction(MoveMigratorAction.of("spawning.monsters.exceptions", "spawning.monsters.exceptions")) .addAction(MoveMigratorAction.of("worldBlacklist", "world-blacklist")) + .addAction(new EntryFeeMigrator()) .addAction(new LegacyAliasMigrator()) .build(); } @@ -184,6 +180,14 @@ public final class WorldConfig { return configHandle.set(configNodes.DIFFICULTY, difficulty); } + public boolean isEntryFeeEnabled() { + return configHandle.get(configNodes.ENTRY_FEE_ENABLED); + } + + public Try setEntryFeeEnabled(boolean entryFeeEnabled) { + return configHandle.set(configNodes.ENTRY_FEE_ENABLED, entryFeeEnabled); + } + public double getEntryFeeAmount() { return configHandle.get(configNodes.ENTRY_FEE_AMOUNT); } diff --git a/src/main/java/org/mvplugins/multiverse/core/world/config/WorldConfigNodes.java b/src/main/java/org/mvplugins/multiverse/core/world/config/WorldConfigNodes.java index d1de65a3..ad8ec86d 100644 --- a/src/main/java/org/mvplugins/multiverse/core/world/config/WorldConfigNodes.java +++ b/src/main/java/org/mvplugins/multiverse/core/world/config/WorldConfigNodes.java @@ -14,6 +14,7 @@ import org.mvplugins.multiverse.core.configuration.node.ConfigNode; import org.mvplugins.multiverse.core.configuration.node.ListConfigNode; import org.mvplugins.multiverse.core.configuration.node.Node; import org.mvplugins.multiverse.core.configuration.node.NodeGroup; +import org.mvplugins.multiverse.core.economy.MVEconomist; import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld; import org.mvplugins.multiverse.core.world.helpers.EnforcementHandler; @@ -102,15 +103,20 @@ public class WorldConfigNodes { }) .build()); + final ConfigNode ENTRY_FEE_ENABLED = node(ConfigNode.builder("entry-fee.enabled", Boolean.class) + .defaultValue(false) + .name("entryfee-enabled") + .build()); + final ConfigNode ENTRY_FEE_AMOUNT = node(ConfigNode.builder("entry-fee.amount", Double.class) .defaultValue(0.0) .name("entryfee-amount") .build()); final ConfigNode ENTRY_FEE_CURRENCY = node(ConfigNode.builder("entry-fee.currency", Material.class) - // TODO: Convert from material ID - .defaultValue(Material.AIR) + .defaultValue(MVEconomist.VAULT_ECONOMY_MATERIAL) .name("entryfee-currency") + .serializer(new CurrencySerializer()) .build()); final ConfigNode ENVIRONMENT = node(ConfigNode diff --git a/src/main/java/org/mvplugins/multiverse/core/world/entrycheck/WorldEntryChecker.java b/src/main/java/org/mvplugins/multiverse/core/world/entrycheck/WorldEntryChecker.java index b4e4f9a1..74f865bf 100644 --- a/src/main/java/org/mvplugins/multiverse/core/world/entrycheck/WorldEntryChecker.java +++ b/src/main/java/org/mvplugins/multiverse/core/world/entrycheck/WorldEntryChecker.java @@ -136,7 +136,7 @@ public class WorldEntryChecker { public Result canPayEntryFee(LoadedMultiverseWorld world) { double price = world.getPrice(); Material currency = world.getCurrency(); - if (price == 0D && (currency == null || currency == MVEconomist.DISABLED_MATERIAL)) { + if (!world.isEntryFeeEnabled() || price == 0D) { return Result.success(EntryFeeResult.Success.FREE_ENTRY); } if (sender instanceof ConsoleCommandSender || sender instanceof BlockCommandSender) { diff --git a/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigMangerTest.kt b/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigMangerTest.kt index 407b6342..7d5eded7 100644 --- a/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigMangerTest.kt +++ b/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigMangerTest.kt @@ -1,17 +1,15 @@ package org.mvplugins.multiverse.core.world +import org.bukkit.Material import org.bukkit.World.Environment -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Assertions.assertTrue import org.mvplugins.multiverse.core.TestWithMockBukkit +import org.mvplugins.multiverse.core.economy.MVEconomist import org.mvplugins.multiverse.core.world.config.SpawnLocation import org.mvplugins.multiverse.core.world.config.WorldsConfigManager import java.io.File import java.nio.file.Path import kotlin.io.path.absolutePathString -import kotlin.test.BeforeTest -import kotlin.test.Test -import kotlin.test.assertNotNull +import kotlin.test.* class WorldConfigMangerTest : TestWithMockBukkit() { @@ -46,12 +44,18 @@ class WorldConfigMangerTest : TestWithMockBukkit() { assertEquals("&aworld the end", endWorldConfig.alias) assertEquals(Environment.THE_END, endWorldConfig.environment) + assertFalse(endWorldConfig.isEntryFeeEnabled) + assertEquals(MVEconomist.VAULT_ECONOMY_MATERIAL, endWorldConfig.entryFeeCurrency) + assertEquals(0.0, endWorldConfig.entryFeeAmount) val worldConfig = worldConfigManager.getWorldConfig("world").orNull assertNotNull(worldConfig) assertEquals(-5176596003035866649, worldConfig.seed) assertEquals(listOf("test"), worldConfig.worldBlacklist) + assertTrue(worldConfig.isEntryFeeEnabled) + assertEquals(Material.DIRT, worldConfig.entryFeeCurrency) + assertEquals(5.0, worldConfig.entryFeeAmount) } @Test diff --git a/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigTest.kt b/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigTest.kt index 86c63575..8122808d 100644 --- a/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigTest.kt +++ b/src/test/java/org/mvplugins/multiverse/core/world/WorldConfigTest.kt @@ -1,5 +1,6 @@ package org.mvplugins.multiverse.core.world +import org.bukkit.Material import org.mvplugins.multiverse.core.TestWithMockBukkit import org.mvplugins.multiverse.core.world.config.WorldConfig import org.mvplugins.multiverse.core.world.config.WorldsConfigManager @@ -60,6 +61,9 @@ class WorldConfigTest : TestWithMockBukkit() { val blacklists = listOf("a", "b", "c") assertTrue(worldConfig.stringPropertyHandle.setProperty("world-blacklist", blacklists).isSuccess) assertEquals(blacklists, worldConfig.stringPropertyHandle.getProperty("world-blacklist").get()) + + assertTrue(worldConfig.stringPropertyHandle.setProperty("entryfee-currency", Material.JUNGLE_WOOD).isSuccess) + assertEquals(Material.JUNGLE_WOOD, worldConfig.stringPropertyHandle.getProperty("entryfee-currency").get()) } @Test diff --git a/src/test/resources/default_worlds.yml b/src/test/resources/default_worlds.yml index b9d54e0a..2a436ab0 100644 --- a/src/test/resources/default_worlds.yml +++ b/src/test/resources/default_worlds.yml @@ -8,8 +8,9 @@ world: bed-respawn: true difficulty: NORMAL entry-fee: + enabled: false amount: 0.0 - currency: AIR + currency: '@vault-economy' environment: NORMAL gamemode: SURVIVAL generator: '' @@ -45,8 +46,9 @@ world_nether: bed-respawn: true difficulty: NORMAL entry-fee: + enabled: false amount: 0.0 - currency: AIR + currency: '@vault-economy' environment: NETHER gamemode: SURVIVAL generator: '' diff --git a/src/test/resources/delete_worlds.yml b/src/test/resources/delete_worlds.yml index ff7acd84..90e307ec 100644 --- a/src/test/resources/delete_worlds.yml +++ b/src/test/resources/delete_worlds.yml @@ -8,8 +8,9 @@ world_nether: bed-respawn: true difficulty: NORMAL entry-fee: + enabled: false amount: 0.0 - currency: AIR + currency: '@vault-economy' environment: NETHER gamemode: SURVIVAL generator: '' diff --git a/src/test/resources/migrated_worlds.yml b/src/test/resources/migrated_worlds.yml index 5b014723..abd03db9 100644 --- a/src/test/resources/migrated_worlds.yml +++ b/src/test/resources/migrated_worlds.yml @@ -8,8 +8,9 @@ world_the_end: bed-respawn: true difficulty: NORMAL entry-fee: + enabled: false amount: 0.0 - currency: AIR + currency: '@vault-economy' environment: THE_END gamemode: SURVIVAL generator: '' @@ -50,8 +51,9 @@ world: bed-respawn: true difficulty: NORMAL entry-fee: + enabled: false amount: 0.0 - currency: AIR + currency: '@vault-economy' environment: NORMAL gamemode: SURVIVAL generator: '' diff --git a/src/test/resources/newworld_worlds.yml b/src/test/resources/newworld_worlds.yml index 38dbb138..0049a327 100644 --- a/src/test/resources/newworld_worlds.yml +++ b/src/test/resources/newworld_worlds.yml @@ -8,8 +8,9 @@ world: bed-respawn: true difficulty: NORMAL entry-fee: + enabled: false amount: 0.0 - currency: AIR + currency: '@vault-economy' environment: NORMAL gamemode: SURVIVAL generator: '' @@ -45,8 +46,9 @@ world_nether: bed-respawn: true difficulty: NORMAL entry-fee: + enabled: false amount: 0.0 - currency: AIR + currency: '@vault-economy' environment: NETHER gamemode: SURVIVAL generator: '' @@ -82,8 +84,9 @@ newworld: bed-respawn: true difficulty: NORMAL entry-fee: + enabled: false amount: 0.0 - currency: AIR + currency: '@vault-economy' environment: NORMAL gamemode: SURVIVAL generator: '@error' diff --git a/src/test/resources/old_worlds.yml b/src/test/resources/old_worlds.yml index 48dc67f1..e73aae0a 100644 --- a/src/test/resources/old_worlds.yml +++ b/src/test/resources/old_worlds.yml @@ -71,7 +71,8 @@ worlds: exceptions: [] entryfee: ==: MVEntryFee - amount: '0.0' + amount: '5.0' + currency: 'DIRT' hunger: 'true' autoHeal: 'true' adjustSpawn: 'true' diff --git a/src/test/resources/properties_worlds.yml b/src/test/resources/properties_worlds.yml index 80a8b76c..f1bc5cfa 100644 --- a/src/test/resources/properties_worlds.yml +++ b/src/test/resources/properties_worlds.yml @@ -8,8 +8,9 @@ world: bed-respawn: true difficulty: NORMAL entry-fee: + enabled: false amount: 0.0 - currency: AIR + currency: @vault-economy environment: NORMAL gamemode: SURVIVAL generator: '' @@ -50,8 +51,9 @@ world_nether: bed-respawn: true difficulty: NORMAL entry-fee: + enabled: false amount: 0.0 - currency: AIR + currency: @vault-economy environment: NETHER gamemode: SURVIVAL generator: ''