Merge pull request #3109 from Multiverse/ben/mv5/entryfee-currency

Properly implement entryfee for world config
This commit is contained in:
Ben Woo 2024-10-14 10:57:35 +08:00 committed by GitHub
commit 1ba0505c4b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 181 additions and 70 deletions

View File

@ -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));
}
}

View File

@ -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;
}
}

View File

@ -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 : "");
}
}
}

View File

@ -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<Void> setEntryFeeEnabled(boolean entryFeeEnabled) {
return worldConfig.setEntryFeeEnabled(entryFeeEnabled);
}
/**
* Gets the amount of currency it requires to enter this world.
*

View File

@ -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<Material> {
static final String VAULT_ECONOMY_CODE = "@vault-economy";
/**
* {@inheritDoc}
*/
@Override
public Material deserialize(Object object, Class<Material> 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<Material> type) {
return Option.of(object)
.map(material -> material == MVEconomist.VAULT_ECONOMY_MATERIAL ? VAULT_ECONOMY_CODE : material.name())
.getOrElse(VAULT_ECONOMY_CODE);
}
}

View File

@ -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);
}
}
}

View File

@ -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<Void> setEntryFeeEnabled(boolean entryFeeEnabled) {
return configHandle.set(configNodes.ENTRY_FEE_ENABLED, entryFeeEnabled);
}
public double getEntryFeeAmount() {
return configHandle.get(configNodes.ENTRY_FEE_AMOUNT);
}

View File

@ -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<Boolean> ENTRY_FEE_ENABLED = node(ConfigNode.builder("entry-fee.enabled", Boolean.class)
.defaultValue(false)
.name("entryfee-enabled")
.build());
final ConfigNode<Double> ENTRY_FEE_AMOUNT = node(ConfigNode.builder("entry-fee.amount", Double.class)
.defaultValue(0.0)
.name("entryfee-amount")
.build());
final ConfigNode<Material> 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<World.Environment> ENVIRONMENT = node(ConfigNode

View File

@ -136,7 +136,7 @@ public class WorldEntryChecker {
public Result<EntryFeeResult.Success, EntryFeeResult.Failure> 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) {

View File

@ -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

View File

@ -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

View File

@ -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: ''

View File

@ -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: ''

View File

@ -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: ''

View File

@ -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'

View File

@ -71,7 +71,8 @@ worlds:
exceptions: []
entryfee:
==: MVEntryFee
amount: '0.0'
amount: '5.0'
currency: 'DIRT'
hunger: 'true'
autoHeal: 'true'
adjustSpawn: 'true'

View File

@ -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: ''