Skip to content

Commit

Permalink
Implemented LiteCommands and change base version to 1.8.9. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurr0 authored Mar 10, 2022
1 parent 2453574 commit cfd78c6
Show file tree
Hide file tree
Showing 17 changed files with 456 additions and 269 deletions.
33 changes: 14 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>mineEconomy</groupId>
<artifactId>mineEconomy</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
<packaging>jar</packaging>

<name>mineEconomy</name>
Expand Down Expand Up @@ -69,19 +69,19 @@
</resources>
</build>


<repositories>
<repository>
<id>minecodes-repo</id>
<url>https://repo.minecodes.pl/</url>
</repository>
<repository>
<id>aikar</id>
<url>https://repo.aikar.co/content/groups/aikar/</url>
<url>https://repository.minecodes.pl/releases</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>dev.rollczi.litecommands</groupId>
<artifactId>bukkit</artifactId>
<version>1.8.4</version>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
Expand All @@ -90,12 +90,12 @@
<dependency>
<groupId>eu.okaeri</groupId>
<artifactId>okaeri-configs-serdes-bukkit</artifactId>
<version>3.2.0</version>
<version>4.0.0-beta4</version>
</dependency>
<dependency>
<groupId>eu.okaeri</groupId>
<artifactId>okaeri-configs-yaml-bukkit</artifactId>
<version>3.3.6</version>
<version>4.0.0-beta4</version>
</dependency>
<dependency>
<groupId>eu.okaeri</groupId>
Expand All @@ -108,26 +108,21 @@
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.16.4-R0.1-SNAPSHOT</version>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>co.aikar</groupId>
<artifactId>acf-bukkit</artifactId>
<version>0.5.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<version>1.7.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.9.2</version>
<version>2.10.9</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
46 changes: 30 additions & 16 deletions src/main/java/pl/minecodes/mineeconomy/EconomyPlugin.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package pl.minecodes.mineeconomy;

import co.aikar.commands.BukkitCommandManager;
import co.aikar.commands.BukkitLocales;
import dev.rollczi.litecommands.LiteCommands;
import dev.rollczi.litecommands.bukkit.LiteBukkitFactory;
import dev.rollczi.litecommands.valid.ValidationInfo;
import eu.okaeri.injector.Injector;
import eu.okaeri.injector.OkaeriInjector;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import pl.minecodes.mineeconomy.command.admin.EconomyCommand;
import pl.minecodes.mineeconomy.command.admin.sub.EconomyCheckCommand;
import pl.minecodes.mineeconomy.command.admin.sub.EconomyClearCommand;
import pl.minecodes.mineeconomy.command.admin.sub.EconomyDepositCommand;
import pl.minecodes.mineeconomy.command.admin.sub.EconomySetCommand;
import pl.minecodes.mineeconomy.command.admin.sub.EconomyWithdrawCommand;
import pl.minecodes.mineeconomy.command.argument.DoubleArgument;
import pl.minecodes.mineeconomy.command.argument.OfflinePlayerArgument;
import pl.minecodes.mineeconomy.command.argument.PlayerArgument;
import pl.minecodes.mineeconomy.command.player.BalanceCommand;
import pl.minecodes.mineeconomy.command.player.RankingCommand;
import pl.minecodes.mineeconomy.command.player.TransferCommand;
Expand All @@ -23,7 +34,6 @@
import pl.minecodes.mineeconomy.runnable.ProfileSaveTask;

import java.text.DecimalFormat;
import java.util.Locale;

public class EconomyPlugin extends JavaPlugin {

Expand All @@ -35,6 +45,8 @@ public class EconomyPlugin extends JavaPlugin {
private ProfileService profileService;
private VaultHook vaultHook;

private LiteCommands liteCommands;

@Override
public void onEnable() {
this.injector = OkaeriInjector.create()
Expand Down Expand Up @@ -89,18 +101,20 @@ private void loadConfiguration() {
}

private void registerCommands() {
BukkitCommandManager commandManager = new BukkitCommandManager(this);
commandManager.registerCommand(this.injector.createInstance(BalanceCommand.class));
commandManager.registerCommand(this.injector.createInstance(EconomyCommand.class));
commandManager.registerCommand(this.injector.createInstance(TransferCommand.class));
commandManager.registerCommand(this.injector.createInstance(RankingCommand.class));

commandManager.getLocales().setDefaultLocale(Locale.ENGLISH);
BukkitLocales locales = commandManager.getLocales();
try {
locales.loadYamlLanguageFile(messages.getBindFile().toFile(), Locale.ENGLISH);
} catch (Exception e) {
e.printStackTrace();
}
this.liteCommands = LiteBukkitFactory.builder(this.getServer(), "mineEconomy")
.argument(OfflinePlayer.class, this.injector.createInstance(OfflinePlayerArgument.class))
.argument(Player.class, this.injector.createInstance(PlayerArgument.class))
.argument(Double.class, this.injector.createInstance(DoubleArgument.class))
.commandInstance(this.injector.createInstance(EconomyCommand.class))
.commandInstance(this.injector.createInstance(EconomySetCommand.class))
.commandInstance(this.injector.createInstance(EconomyDepositCommand.class))
.commandInstance(this.injector.createInstance(EconomyWithdrawCommand.class))
.commandInstance(this.injector.createInstance(EconomyClearCommand.class))
.commandInstance(this.injector.createInstance(EconomyCheckCommand.class))
.commandInstance(this.injector.createInstance(BalanceCommand.class))
.commandInstance(this.injector.createInstance(RankingCommand.class))
.commandInstance(this.injector.createInstance(TransferCommand.class))
.message(ValidationInfo.INVALID_USE, messageInfoContext -> this.messages.getCommandUsage().replace("{usage}", messageInfoContext.getUseScheme()))
.register();
}
}
190 changes: 11 additions & 179 deletions src/main/java/pl/minecodes/mineeconomy/command/admin/EconomyCommand.java
Original file line number Diff line number Diff line change
@@ -1,193 +1,25 @@
package pl.minecodes.mineeconomy.command.admin;

import co.aikar.commands.BaseCommand;
import co.aikar.commands.annotation.*;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.AtomicDouble;
import dev.rollczi.litecommands.annotations.Execute;
import dev.rollczi.litecommands.annotations.Permission;
import dev.rollczi.litecommands.annotations.Section;
import eu.okaeri.injector.annotation.Inject;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import pl.minecodes.mineeconomy.EconomyPlugin;
import pl.minecodes.mineeconomy.data.configuration.Configuration;
import pl.minecodes.mineeconomy.data.configuration.Messages;
import pl.minecodes.mineeconomy.profile.Profile;
import pl.minecodes.mineeconomy.profile.ProfileService;
import pl.minecodes.mineeconomy.profile.helper.BalanceOperationCallback;
import pl.minecodes.mineeconomy.util.MessageUtil;
import pl.minecodes.mineeconomy.util.Placeholders;

import java.util.Collections;
import java.util.Objects;
@Section(route = "economy", aliases = {"eco", "ecoadmin"}, priority = 0)
@Permission("economy.admin")
public class EconomyCommand {

@CommandAlias("economy|eco|ecoadmin")
@CommandPermission("economy.admin")
public class EconomyCommand extends BaseCommand {
@Inject private ProfileService profileService;
@Inject private Configuration configuration;
@Inject private Messages messages;

@Inject
private ProfileService profileService;
@Inject
private Configuration configuration;
@Inject
private Messages messages;


@Default
@HelpCommand
public void help(CommandSender sender) {
@Execute
public void executeHelpCommand(CommandSender sender) {
this.messages.getEconomyAdminCommands().forEach(string -> MessageUtil.sendMessage(sender, string));
}


@Syntax("<username> <balance>")
@Subcommand("set")
@Description("Setup player balance.")
@CommandCompletion("@players 10|100|1000|10000")
public void onAdministratorSetBalance(CommandSender sender, String username, double balance) {
AtomicDouble atomicBalance = new AtomicDouble(Double.parseDouble(EconomyPlugin.FORMAT.format(balance)));

OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayerIfCached(username);
if (offlinePlayer == null) {
MessageUtil.sendMessage(sender, this.messages.getPlayerIsNotExistsInCache());
return;
}

Profile profile = this.profileService.getProfile(offlinePlayer.getUniqueId());
profile.setupBalance(atomicBalance.get(), new BalanceOperationCallback() {
@Override
public void done() {
MessageUtil.sendMessage(sender, Placeholders.replace(messages.getBalanceSuccessfullySet(),
ImmutableMap.of(
"player", Objects.requireNonNull(offlinePlayer.getName(), "OfflinePlayer name is null."),
"balance", atomicBalance.get(),
"currency", configuration.getCurrency(atomicBalance.get()))));
}

@Override
public void cancel(CancelReason reason) {
MessageUtil.sendMessage(sender, messages.getBalanceOperationParameterIsNegative());
}
});
}


@Syntax("<username> <value>")
@Subcommand("deposit")
@Description("Deposit money to player balance.")
@CommandCompletion("@players 10|100|1000|10000")
public void onAdministratorDepositMoney(CommandSender sender, String username, double value) {
AtomicDouble atomicValue = new AtomicDouble(Double.parseDouble(EconomyPlugin.FORMAT.format(value)));

OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayerIfCached(username);
if (offlinePlayer == null) {
MessageUtil.sendMessage(sender, this.messages.getPlayerIsNotExistsInCache());
return;
}

Profile profile = this.profileService.getProfile(offlinePlayer.getUniqueId());
profile.deposit(atomicValue.get(), new BalanceOperationCallback() {
@Override
public void done() {
MessageUtil.sendMessage(sender, Placeholders.replace(messages.getBalanceSuccessfullyDeposit(),
ImmutableMap.of(
"player", Objects.requireNonNull(offlinePlayer.getName(), "OfflinePlayer name is null."),
"value", atomicValue.get(),
"currency", configuration.getCurrency(atomicValue.get()))));
}

@Override
public void cancel(CancelReason reason) {
MessageUtil.sendMessage(sender, messages.getBalanceOperationParameterIsNegative());
}
});
}


@Syntax("<username> <value>")
@Subcommand("withdraw")
@Description("Withdraw money from player balance.")
@CommandCompletion("@players 10|100|1000|10000")
public void onAdministratorWithdrawMoney(CommandSender sender, String username, double value) {
AtomicDouble atomicValue = new AtomicDouble(Double.parseDouble(EconomyPlugin.FORMAT.format(value)));

OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayerIfCached(username);
if (offlinePlayer == null) {
MessageUtil.sendMessage(sender, this.messages.getPlayerIsNotExistsInCache());
return;
}

Profile profile = this.profileService.getProfile(offlinePlayer.getUniqueId());
profile.withdraw(atomicValue.get(), new BalanceOperationCallback() {
@Override
public void done() {
MessageUtil.sendMessage(sender, Placeholders.replace(messages.getBalanceSuccessfullyWithdraw(),
ImmutableMap.of(
"player", Objects.requireNonNull(offlinePlayer.getName(), "OfflinePlayer name is null."),
"value", atomicValue.get(),
"currency", configuration.getCurrency(atomicValue.get()))));
}

@Override
public void cancel(CancelReason reason) {
switch (reason) {
case NO_FOUNDS:
MessageUtil.sendMessage(sender, messages.getBalanceWithdrawNoFounds());
break;
case NEGATIVE_BALANCE:
MessageUtil.sendMessage(sender, messages.getBalanceIsNegative());
break;
case NEGATIVE_PARAMETER:
MessageUtil.sendMessage(sender, messages.getBalanceOperationParameterIsNegative());
break;
}
}
});
}


@Syntax("<username>")
@Subcommand("clear")
@Description("Clear player balance.")
@CommandCompletion("@players")
public void onAdministratorClearBalance(CommandSender sender, String username) {
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayerIfCached(username);
if (offlinePlayer == null) {
MessageUtil.sendMessage(sender, this.messages.getPlayerIsNotExistsInCache());
return;
}

Profile profile = this.profileService.getProfile(offlinePlayer.getUniqueId());
profile.setupBalance(0, new BalanceOperationCallback() {
@Override
public void done() {
MessageUtil.sendMessage(sender, Placeholders.replace(messages.getBalanceSuccessfullyClear(),
Collections.singletonMap("player", Objects.requireNonNull(offlinePlayer.getName(), "OfflinePlayer name is null."))));
}

@Override
public void cancel(CancelReason reason) {
MessageUtil.sendMessage(sender, messages.getBalanceOperationParameterIsNegative());
}
});
}


@Syntax("<username>")
@Subcommand("check")
@Description("Check player balance.")
@CommandCompletion("@players")
public void onAdministratorCheckBalance(CommandSender sender, String username) {
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayerIfCached(username);
if (offlinePlayer == null) {
MessageUtil.sendMessage(sender, this.messages.getPlayerIsNotExistsInCache());
return;
}

Profile profile = this.profileService.getProfile(offlinePlayer.getUniqueId());
MessageUtil.sendMessage(sender, Placeholders.replace(this.messages.getBalanceAdministratorCheck(),
ImmutableMap.of(
"player", Objects.requireNonNull(offlinePlayer.getName(), "OfflinePlayer name is null."),
"balance", profile.getBalance(),
"currency", this.configuration.getCurrency(profile.getBalance()))));
}
}
Loading

0 comments on commit cfd78c6

Please sign in to comment.