From a55d25285ccd577ad8c0a527b4b31bec939227bc Mon Sep 17 00:00:00 2001 From: Flibio Date: Sun, 3 Apr 2016 12:20:09 -0600 Subject: [PATCH] Added migration command and bumped version --- build.gradle | 2 +- .../flibio/economylite/EconomyLite.java | 4 +- .../economylite/commands/MigrateCommand.java | 109 ++++++++++++++++++ src/main/resources/messages.properties | 4 + 4 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/github/flibio/economylite/commands/MigrateCommand.java diff --git a/build.gradle b/build.gradle index db02b8c..803f3ef 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ sourceCompatibility = '1.8' targetCompatibility = '1.8' group 'io.github.flibio' -version '2.0.0' +version '2.0.1' defaultTasks 'licenseFormat', 'clean', 'build' diff --git a/src/main/java/io/github/flibio/economylite/EconomyLite.java b/src/main/java/io/github/flibio/economylite/EconomyLite.java index d01fbd2..86fd816 100644 --- a/src/main/java/io/github/flibio/economylite/EconomyLite.java +++ b/src/main/java/io/github/flibio/economylite/EconomyLite.java @@ -43,6 +43,7 @@ import io.github.flibio.economylite.api.CurrencyEconService; import io.github.flibio.economylite.api.PlayerEconService; import io.github.flibio.economylite.api.VirtualEconService; +import io.github.flibio.economylite.commands.MigrateCommand; import io.github.flibio.economylite.commands.PayCommand; import io.github.flibio.economylite.commands.admin.AddCommand; import io.github.flibio.economylite.commands.admin.EconCommand; @@ -136,7 +137,8 @@ public void onServerInitialize(GameInitializationEvent event) { new PayCommand(), new BalTopCommand(), new VirtualBalanceCommand(), - new VirtualSetCommand() + new VirtualSetCommand(), + new MigrateCommand() ); // Register currency registry game.getRegistry().registerModule(Currency.class, new CurrencyRegistryModule()); diff --git a/src/main/java/io/github/flibio/economylite/commands/MigrateCommand.java b/src/main/java/io/github/flibio/economylite/commands/MigrateCommand.java new file mode 100644 index 0000000..2f98086 --- /dev/null +++ b/src/main/java/io/github/flibio/economylite/commands/MigrateCommand.java @@ -0,0 +1,109 @@ +/* + * This file is part of EconomyLite, licensed under the MIT License (MIT). + * + * Copyright (c) 2015 - 2016 Flibio + * Copyright (c) Contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package io.github.flibio.economylite.commands; + +import ninja.leaping.configurate.ConfigurationNode; +import ninja.leaping.configurate.hocon.HoconConfigurationLoader; +import ninja.leaping.configurate.loader.ConfigurationLoader; + +import org.slf4j.Logger; +import org.spongepowered.api.command.CommandSource; +import org.spongepowered.api.command.args.CommandContext; +import org.spongepowered.api.command.args.GenericArguments; +import org.spongepowered.api.command.spec.CommandSpec; +import org.spongepowered.api.command.spec.CommandSpec.Builder; +import org.spongepowered.api.text.Text; + +import io.github.flibio.economylite.EconomyLite; +import io.github.flibio.economylite.api.PlayerEconService; +import io.github.flibio.economylite.api.VirtualEconService; +import io.github.flibio.utils.commands.AsyncCommand; +import io.github.flibio.utils.commands.BaseCommandExecutor; +import io.github.flibio.utils.commands.Command; +import io.github.flibio.utils.message.MessageStorage; + +import java.io.File; +import java.math.BigDecimal; +import java.util.UUID; + +@AsyncCommand +@Command(aliases = {"migrate"}, permission = "economylite.admin.migrate") +public class MigrateCommand extends BaseCommandExecutor { + + private MessageStorage messageStorage = EconomyLite.getMessageStorage(); + private PlayerEconService playerService = EconomyLite.getPlayerService(); + private VirtualEconService virtualService = EconomyLite.getVirtualService(); + private Logger logger = EconomyLite.getInstance().getLogger(); + + @Override + public Builder getCommandSpecBuilder() { + return CommandSpec.builder() + .executor(this) + .arguments(GenericArguments.optional(GenericArguments.bool(Text.of("confirm")))); + } + + @Override + public void run(CommandSource src, CommandContext args) { + if (args.getOne("confirm").isPresent()) { + File file = new File("config/economylite/data.conf"); + File bFile = new File("config/economylite/businesses.conf"); + try { + if (file.exists()) { + ConfigurationLoader manager = HoconConfigurationLoader.builder().setFile(file).build(); + ConfigurationNode root = manager.load(); + root.getChildrenMap().keySet().forEach(raw -> { + if (raw instanceof String) { + UUID uuid = UUID.fromString((String) raw); + if (root.getNode(uuid.toString()).getNode("balance") != null) { + int balance = root.getNode(uuid.toString()).getNode("balance").getInt(); + playerService.setBalance(uuid, BigDecimal.valueOf(balance), EconomyLite.getEconomyService().getDefaultCurrency()); + } + } + }); + } + if (bFile.exists()) { + ConfigurationLoader manager = HoconConfigurationLoader.builder().setFile(bFile).build(); + ConfigurationNode root = manager.load(); + root.getChildrenMap().keySet().forEach(raw -> { + if (raw instanceof String) { + String name = (String) raw; + if (root.getNode(name).getNode("balance") != null) { + int balance = root.getNode(name).getNode("balance").getInt(); + virtualService.setBalance(name, BigDecimal.valueOf(balance), EconomyLite.getEconomyService().getDefaultCurrency()); + } + } + }); + } + src.sendMessage(messageStorage.getMessage("command.migrate.completed")); + } catch (Exception e) { + logger.error(e.getMessage()); + src.sendMessage(messageStorage.getMessage("command.migrate.fail")); + } + } else { + src.sendMessage(messageStorage.getMessage("command.migrate.confirm")); + } + } + +} diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index a3ddabe..77dfb5d 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -53,6 +53,10 @@ command.econ.removesuccess=&aSuccessfully removed currency from the balance of & command.econ.setfail=&cFailed to set the balance of {name}! command.econ.setsuccess=&aSuccessfully set the balance of &6{name}! +command.migrate.completed=&aMigration completed! +command.migrate.confirm=&cMigrating will override any existing balances. &6Run &a/migrate yes &6to confirm the migration! +command.migrate.fail=&cMigration failed! + command.pay.notyou=&cYou can not pay yourself! command.pay.failed=&cThe payment to {target} failed! command.pay.success=&aSuccessfully payed &6{target}!