Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly implement entryfee for world config #3109

Merged
merged 1 commit into from
Oct 14, 2024
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
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
@Service
public class MVEconomist {
public static final Material DISABLED_MATERIAL = Material.AIR;
public static final Material VAULT_ECONOMY_MATERIAL = Material.AIR;

Check warning on line 18 in src/main/java/org/mvplugins/multiverse/core/economy/MVEconomist.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/economy/MVEconomist.java:18:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocVariableCheck)

private final VaultHandler vaultHandler;

Expand Down Expand Up @@ -238,7 +238,7 @@
* @return true if currency string matches a valid material.
*/
public static boolean isItemCurrency(Material currency) {
return currency != null;
return currency != VAULT_ECONOMY_MATERIAL;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,25 @@
return worldConfig.setPortalForm(portalForm);
}

/**

Check warning on line 390 in src/main/java/org/mvplugins/multiverse/core/world/MultiverseWorld.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 First sentence should end with a period. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/world/MultiverseWorld.java:390:0: warning: First sentence should end with a period. (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocStyleCheck)

Check warning on line 390 in src/main/java/org/mvplugins/multiverse/core/world/MultiverseWorld.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 First sentence of Javadoc is missing an ending period. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/world/MultiverseWorld.java:390:0: warning: First sentence of Javadoc is missing an ending period. (com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck)
* Gets if entry fee is needed when entering this world
*
* @return True if entry fee is needed
*/
public boolean isEntryFeeEnabled() {
return worldConfig.isEntryFeeEnabled();
}

/**

Check warning on line 399 in src/main/java/org/mvplugins/multiverse/core/world/MultiverseWorld.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 First sentence should end with a period. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/world/MultiverseWorld.java:399:0: warning: First sentence should end with a period. (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocStyleCheck)

Check warning on line 399 in src/main/java/org/mvplugins/multiverse/core/world/MultiverseWorld.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 First sentence of Javadoc is missing an ending period. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/world/MultiverseWorld.java:399:0: warning: First sentence of Javadoc is missing an ending period. (com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck)
* 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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Check warning on line 7 in src/main/java/org/mvplugins/multiverse/core/world/config/CurrencySerializer.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 'org.mvplugins.multiverse.core.configuration.functions.NodeSerializer' should be separated from previous imports. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/world/config/CurrencySerializer.java:7:1: warning: 'org.mvplugins.multiverse.core.configuration.functions.NodeSerializer' should be separated from previous imports. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)
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";

Check warning on line 16 in src/main/java/org/mvplugins/multiverse/core/world/config/CurrencySerializer.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/world/config/CurrencySerializer.java:16:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocVariableCheck)

/**
* {@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);
}
}
Original file line number Diff line number Diff line change
@@ -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;

Check warning on line 5 in src/main/java/org/mvplugins/multiverse/core/world/config/EntryFeeMigrator.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 'org.mvplugins.multiverse.core.configuration.migration.MigratorAction' should be separated from previous imports. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/world/config/EntryFeeMigrator.java:5:1: warning: 'org.mvplugins.multiverse.core.configuration.migration.MigratorAction' should be separated from previous imports. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)

/**
* Migrates the entry fee settings. Assumes entry fee is disabled if currency is not set.
*/
public class EntryFeeMigrator implements MigratorAction {
@Override

Check warning on line 11 in src/main/java/org/mvplugins/multiverse/core/world/config/EntryFeeMigrator.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Class 'EntryFeeMigrator' looks like designed for extension (can be subclassed), but the method 'migrate' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'EntryFeeMigrator' final or making the method 'migrate' static/final/abstract/empty, or adding allowed annotation for the method. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/world/config/EntryFeeMigrator.java:11:5: info: Class 'EntryFeeMigrator' looks like designed for extension (can be subclassed), but the method 'migrate' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'EntryFeeMigrator' final or making the method 'migrate' static/final/abstract/empty, or adding allowed annotation for the method. (com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck)
public void migrate(ConfigurationSection config) {
String currency = config.getString("entry-fee.currency", "");

Check warning on line 13 in src/main/java/org/mvplugins/multiverse/core/world/config/EntryFeeMigrator.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 The String "entry-fee.currency" appears 2 times in the file. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/world/config/EntryFeeMigrator.java:13:44: warning: The String "entry-fee.currency" appears 2 times in the file. (com.puppycrawl.tools.checkstyle.checks.coding.MultipleStringLiteralsCheck)
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@
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;

/**
Expand Down Expand Up @@ -68,6 +62,7 @@ private VersionMigrator initialVersionMigrator() {
.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"))
Expand Down Expand Up @@ -100,6 +95,7 @@ private VersionMigrator initialVersionMigrator() {
.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();
}
Expand Down Expand Up @@ -184,6 +180,14 @@ public Try<Void> setDifficulty(Difficulty difficulty) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;

Expand Down Expand Up @@ -102,15 +103,20 @@ private <N extends Node> N node(N node) {
})
.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public Result<BlacklistResult.Success, BlacklistResult.Failure> isNotBlacklisted
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/test/resources/default_worlds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''
Expand Down Expand Up @@ -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: ''
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/delete_worlds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''
Expand Down
6 changes: 4 additions & 2 deletions src/test/resources/migrated_worlds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''
Expand Down Expand Up @@ -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: ''
Expand Down
Loading
Loading