Skip to content

Commit

Permalink
GH-404 Add currency example with multi file command. (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi authored Jun 19, 2024
1 parent 7b25787 commit 6d1c02e
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import dev.rollczi.example.bukkit.command.NumberCommand;
import dev.rollczi.example.bukkit.command.RandomItemCommand;
import dev.rollczi.example.bukkit.command.TeleportCommand;
import dev.rollczi.example.bukkit.command.currency.CurrencyBalanceCommand;
import dev.rollczi.example.bukkit.command.currency.CurrencyCommand;
import dev.rollczi.example.bukkit.command.currency.CurrencyService;
import dev.rollczi.example.bukkit.validator.IsNotOpValidator;
import dev.rollczi.example.bukkit.validator.IsNotOp;
import dev.rollczi.litecommands.bukkit.LiteBukkitMessages;
Expand All @@ -33,6 +36,9 @@ public class ExamplePlugin extends JavaPlugin {

@Override
public void onEnable() {
// [!] you don't have to use this service, it's just for demonstration [!]
CurrencyService currencyService = new CurrencyService();

this.liteCommands = LiteCommandsBukkit.builder()
// configure bukkit platform
.settings(settings -> settings
Expand All @@ -51,7 +57,9 @@ public void onEnable() {
new FlyCommand(),
new GiveCommand(),
new RandomItemCommand(),
new NumberCommand()
new NumberCommand(),
new CurrencyCommand(currencyService),
new CurrencyBalanceCommand(currencyService)
)

// Custom annotation validators
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.rollczi.example.bukkit.command.currency;

import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

@Command(name = "currency balance")
public class CurrencyBalanceCommand {

private final CurrencyService currencyService;

public CurrencyBalanceCommand(CurrencyService currencyService) {
this.currencyService = currencyService;
}

@Execute
void balance(@Context Player sender) {
sender.sendMessage("Your balance is " + currencyService.getCurrency(sender.getUniqueId()));
}

@Execute
void balance(@Context CommandSender sender, @Arg Player player) {
sender.sendMessage(player.getName() + "'s balance is " + currencyService.getCurrency(player.getUniqueId()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package dev.rollczi.example.bukkit.command.currency;

import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

@Command(name = "currency")
public class CurrencyCommand {

private final CurrencyService currencyService;

public CurrencyCommand(CurrencyService currencyService) {
this.currencyService = currencyService;
}

@Execute(name = "give")
void give(@Context CommandSender sender, @Arg Player player, @Arg int amount) {
currencyService.addCurrency(player.getUniqueId(), amount);

sender.sendMessage("Given " + amount + " to " + player.getName());
player.sendMessage("You have received " + amount + " from " + sender.getName());
}

@Execute(name = "take")
void take(@Context CommandSender sender, @Arg Player player, @Arg int amount) {
currencyService.removeCurrency(player.getUniqueId(), amount);

sender.sendMessage("Taken " + amount + " from " + player.getName());
player.sendMessage("Your balance has been reduced by " + amount + " by " + sender.getName());
}

@Execute(name = "set")
void set(@Context CommandSender sender, @Arg Player player, @Arg int amount) {
currencyService.setCurrency(player.getUniqueId(), amount);

sender.sendMessage("Set " + amount + " to " + player.getName());
player.sendMessage("Your balance has been set to " + amount + " by " + sender.getName());
}

@Execute(name = "take")
void reset(@Context CommandSender sender, @Arg Player player) {
currencyService.setCurrency(player.getUniqueId(), 0);

sender.sendMessage("Reset " + player.getName());
player.sendMessage("Your balance has been reset by " + sender.getName());
}

@Execute(name = "pay")
void pay(@Context Player sender, @Arg Player player, @Arg int amount) {
currencyService.addCurrency(player.getUniqueId(), amount);
currencyService.removeCurrency(sender.getUniqueId(), amount);

sender.sendMessage("Paid " + amount + " to " + player.getName());
player.sendMessage("You have received " + amount + " from " + sender.getName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dev.rollczi.example.bukkit.command.currency;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
* This class is a simple service that stores currency for players. (Used for demonstration /currency command)
* {@link CurrencyCommand} and {@link CurrencyBalanceCommand} are using this service.
*/
public class CurrencyService {

private final Map<UUID, Integer> currency = new HashMap<>();

public void setCurrency(UUID uuid, int amount) {
currency.put(uuid, amount);
}

public void addCurrency(UUID uuid, int amount) {
currency.put(uuid, currency.getOrDefault(uuid, 0) + amount);
}

public void removeCurrency(UUID uuid, int amount) {
currency.put(uuid, currency.getOrDefault(uuid, 0) - amount);
}

public int getCurrency(UUID uuid) {
return currency.getOrDefault(uuid, 0);
}

}

0 comments on commit 6d1c02e

Please sign in to comment.