Skip to content

Commit

Permalink
Add OfflinePlayer and UUID support to Economy backend (#7425)
Browse files Browse the repository at this point in the history
* Vault offline player support

* Add deprecated since javadocs.

* Update config-migration version number.

* Flip ternary right way around.

* Remove the legacy debt account conversion.

* Update admin command permission nodes to follow command structure->node
pattern.

Made depositall use the economyexecutor too.

* Remove old comment that no longer applied.

* Make economy provider sealed and fix trailing spaces

* Change default npc uuid version

* Fix TownyServerAccount initialization throwing NPE.

* Add /ta eco info command.

Takes town, nation, resident, and serveraccount as subcommands, and
outputs the name, UUID, and balance of the EconomyHandler's Account.

Fixes an issue with array index out of bounds when /ta eco was run
without a sub command.

Adds a HelpMenu for /ta eco and /ta eco info.

* Add translations for convert command feedback

* Set UUID in deprecated methods when available

* Use economy executor for new command

* Update default npc uuid version again

* Re-modify npc uuids when converting

* Don't endlessly suggest modern

* Add help menu entry

* Skip accounts with no balance during conversion

* Deprecated unused Account#getBukkitWorld

* Add debug messages to eco conversion

* Cache constructed offline players

* Fix config-migration.json

* Add conversion progress messages.

* Add online player warning before conversion.

---------

Co-authored-by: LlmDl <[email protected]>
  • Loading branch information
Warriorrrr and LlmDl authored Oct 23, 2024
1 parent 401aaea commit 8bd09a4
Show file tree
Hide file tree
Showing 30 changed files with 1,080 additions and 369 deletions.
15 changes: 15 additions & 0 deletions Towny/src/main/java/com/palmergames/bukkit/config/ConfigNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -2970,6 +2970,21 @@ public enum ConfigNodes {
"false",
"",
"# Does a conquered town which cannot pay the nation tax get deleted?"),

ECO_ADVANCED_MODERN(
"economy.advanced.modern",
"true",
"",
"# When enabled, Towny will use UUIDs when communicating with your economy plugin.",
"# Most users will never have to touch this, but for existing servers this option will automatically be set to false.",
"# If this option is disabled and you wish to avoid losing data, use the `/townyadmin eco convert modern` command to convert."),

ECO_ADVANCED_NPC_UUID_VERSION(
"economy.advanced.npc_uuid_version",
"-1",
"",
"# The UUID version to use for non-player accounts. This is used so that economy plugins can more easily differentiate between player and NPC accounts.",
"# The default is -1, which disables modifying npc uuids."),

BANKHISTORY(
"bank_history",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.palmergames.bukkit.config.migration;

import com.palmergames.bukkit.config.CommentedConfiguration;
import com.palmergames.bukkit.config.ConfigNodes;
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.TownySettings;
import com.palmergames.bukkit.util.BukkitTools;
import com.palmergames.util.StringMgmt;
Expand All @@ -9,6 +11,8 @@
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -18,17 +22,29 @@
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.logging.Level;

@SuppressWarnings("FieldCanBeLocal")
@SuppressWarnings({"unused", "unchecked"})
public class RunnableMigrations {
private final Map<String, Consumer<CommentedConfiguration>> BY_NAME = new HashMap<>();

public RunnableMigrations() {
BY_NAME.put("migrate_notifications", MIGRATE_NOTIFICATIONS);
BY_NAME.put("add_townblocktype_limits", ADD_TOWNBLOCKTYPE_LIMITS);
BY_NAME.put("convert_entity_class_names", CONVERT_ENTITY_CLASS_NAMES);
BY_NAME.put("add_milkable_animals_to_farm_plot", ADD_MILKABLE_ANIMALS);
BY_NAME.put("update_farm_blocks", UPDATE_FARM_BLOCKS);
try {
for (final Field field : this.getClass().getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers()))
continue;

field.setAccessible(true);
Object value = field.get(null);

if (!(value instanceof Consumer))
continue;

BY_NAME.put(field.getName().toLowerCase(Locale.ROOT), (Consumer<CommentedConfiguration>) value);
}
} catch (ReflectiveOperationException e) {
Towny.getPlugin().getLogger().log(Level.WARNING, "Exception occurred when getting runnable migrations", e);
}
}

@Nullable
Expand All @@ -40,7 +56,7 @@ public boolean addMigration(String name, Consumer<CommentedConfiguration> migrat
return BY_NAME.putIfAbsent(name.toLowerCase(Locale.ROOT), migration) == null;
}

private final Consumer<CommentedConfiguration> MIGRATE_NOTIFICATIONS = config -> {
private static final Consumer<CommentedConfiguration> MIGRATE_NOTIFICATIONS = config -> {
if (Boolean.parseBoolean(config.getString("notification.notifications_appear_in_action_bar", "true")))
config.set("notification.notifications_appear_as", "action_bar");
else if (Boolean.parseBoolean(config.getString("notification.notifications_appear_on_bossbar", "false")))
Expand All @@ -49,13 +65,12 @@ else if (Boolean.parseBoolean(config.getString("notification.notifications_appea
config.set("notification.notifications_appear_as", "chat");
};

@SuppressWarnings("unchecked")
private final Consumer<CommentedConfiguration> ADD_TOWNBLOCKTYPE_LIMITS = config -> {
private static final Consumer<CommentedConfiguration> ADD_TOWNBLOCKTYPE_LIMITS = config -> {
for (Map<?, ?> level : config.getMapList("levels.town_level"))
((Map<String, Object>) level).put("townBlockTypeLimits", new HashMap<>());
};

private final Consumer<CommentedConfiguration> CONVERT_ENTITY_CLASS_NAMES = config -> {
private static final Consumer<CommentedConfiguration> CONVERT_ENTITY_CLASS_NAMES = config -> {
List<String> entities = new ArrayList<>(Arrays.asList(config.getString("new_world_settings.plot_management.wild_revert_on_mob_explosion.entities", "").split(",")));

ListIterator<String> iterator = entities.listIterator();
Expand All @@ -79,8 +94,7 @@ else if (Boolean.parseBoolean(config.getString("notification.notifications_appea
config.set("new_world_settings.plot_management.wild_revert_on_mob_explosion.entities", String.join(",", entities));
};

@SuppressWarnings("unchecked")
private final Consumer<CommentedConfiguration> ADD_MILKABLE_ANIMALS = config -> {
private static final Consumer<CommentedConfiguration> ADD_MILKABLE_ANIMALS_TO_FARM_PLOT = config -> {
for (Map<?, ?> plotType : config.getMapList("townblocktypes.types")) {
if (plotType.get("name").equals("farm")) {
String allowedBlocks = (String) plotType.get("allowedBlocks");
Expand All @@ -93,8 +107,7 @@ else if (Boolean.parseBoolean(config.getString("notification.notifications_appea
* 0.100.2.10 included a change which revamped the ItemLists used to construct the farm blocks, resulting in a more comprehensive list.
* This runnable will add any blocks that older configs may have had which were missing from older configs.
*/
@SuppressWarnings("unchecked")
private final Consumer<CommentedConfiguration> UPDATE_FARM_BLOCKS = config -> {
private static final Consumer<CommentedConfiguration> UPDATE_FARM_BLOCKS = config -> {
for (Map<?, ?> plotType : config.getMapList("townblocktypes.types")) {
if (!plotType.get("name").equals("farm"))
continue;
Expand All @@ -106,4 +119,8 @@ else if (Boolean.parseBoolean(config.getString("notification.notifications_appea
((Map<String, Object>) plotType).replace("allowedBlocks", rawBlocks + "," + StringMgmt.join(missingBlocks, ","));
}
};

private static final Consumer<CommentedConfiguration> DISABLE_MODERN_ECO = config -> {
config.set(ConfigNodes.ECO_ADVANCED_MODERN.getRoot(), "false");
};
}
Loading

0 comments on commit 8bd09a4

Please sign in to comment.