dešpagetizace příkozových class shop a money

dev_gioth
jakub 9 months ago
parent d881cade87
commit bd8b88cdae
  1. 72
      src/main/java/xyz/soukup/ecoCraftCore/commands/MoneyCommand.java
  2. 32
      src/main/java/xyz/soukup/ecoCraftCore/commands/ShopCommand.java

@ -1,8 +1,10 @@
package xyz.soukup.ecoCraftCore.commands; package xyz.soukup.ecoCraftCore.commands;
import com.mojang.brigadier.LiteralMessage;
import com.mojang.brigadier.arguments.FloatArgumentType; import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.papermc.paper.command.brigadier.CommandSourceStack; import io.papermc.paper.command.brigadier.CommandSourceStack;
@ -16,36 +18,56 @@ import xyz.soukup.ecoCraftCore.objects.Transaction;
import xyz.soukup.ecoCraftCore.utilities.Messages; import xyz.soukup.ecoCraftCore.utilities.Messages;
import xyz.soukup.ecoCraftCore.utilities.PHHM; import xyz.soukup.ecoCraftCore.utilities.PHHM;
@SuppressWarnings("UnstableApiUsage")
public class MoneyCommand { public class MoneyCommand {
public static LiteralArgumentBuilder<CommandSourceStack> createCommand() { public static LiteralArgumentBuilder<CommandSourceStack> createCommand() {
return Commands.literal("money")
.then(Commands.literal("send") RequiredArgumentBuilder<CommandSourceStack, PlayerSelectorArgumentResolver> playerArgument = Commands.argument("player", ArgumentTypes.player());
RequiredArgumentBuilder<CommandSourceStack, Float> amountArgument = Commands.argument("amount", FloatArgumentType.floatArg(0.1F));
RequiredArgumentBuilder<CommandSourceStack, String> ownerArgument = Commands.argument("owner", StringArgumentType.word());
RequiredArgumentBuilder<CommandSourceStack, String> typeArgument = Commands.argument("type", StringArgumentType.word());
LiteralArgumentBuilder<CommandSourceStack> send = Commands.literal("send")
.requires(source -> source.getSender() instanceof Player) .requires(source -> source.getSender() instanceof Player)
.then(Commands.argument("player", ArgumentTypes.player()) .then(playerArgument.then(amountArgument.executes(MoneyCommand::sendMoney)));
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F))
.executes(MoneyCommand::sendMoney))))
.then(Commands.literal("give") RequiredArgumentBuilder<CommandSourceStack, Float> selfGive = amountArgument
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F))
.requires(source -> source.getSender() instanceof Player) .requires(source -> source.getSender() instanceof Player)
.executes(MoneyCommand::giveMoneySelf)) .executes(MoneyCommand::giveMoneySelf);
.then(Commands.argument("player", ArgumentTypes.player())
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F)) RequiredArgumentBuilder<CommandSourceStack, PlayerSelectorArgumentResolver> playerGive = playerArgument
.executes(MoneyCommand::giveMoneyPlayer))) .then(amountArgument.executes(MoneyCommand::giveMoneyPlayer));
.then(Commands.argument("type", StringArgumentType.word())
.then(Commands.argument("owner", StringArgumentType.word()) RequiredArgumentBuilder<CommandSourceStack, String> giveOther = typeArgument.then(ownerArgument.then(amountArgument.executes(MoneyCommand::giveMoneyOther)));
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F))
.executes(MoneyCommand::giveMoneyOther))))) LiteralArgumentBuilder<CommandSourceStack> give = Commands.literal("give")
.then(Commands.literal("remove") .then(selfGive)
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F)) .then(playerGive)
.then(giveOther);
RequiredArgumentBuilder<CommandSourceStack, Float> removeSelf = amountArgument
.requires(source -> source.getSender() instanceof Player) .requires(source -> source.getSender() instanceof Player)
.executes(MoneyCommand::removeMoneySelf)) .executes(MoneyCommand::removeMoneySelf);
.then(Commands.argument("player", ArgumentTypes.player())
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F)) RequiredArgumentBuilder<CommandSourceStack, PlayerSelectorArgumentResolver> removePlayer = playerArgument
.executes(MoneyCommand::removeMoneyPlayer))) .then(amountArgument.executes(MoneyCommand::removeMoneyPlayer));
.then(Commands.argument("type", StringArgumentType.word())
.then(Commands.argument("owner", StringArgumentType.word()) RequiredArgumentBuilder<CommandSourceStack, String> removeOther = typeArgument
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F)) .then(ownerArgument
.executes(MoneyCommand::removeMoneyOther))))) .then(amountArgument.executes(MoneyCommand::removeMoneyOther)));
LiteralArgumentBuilder<CommandSourceStack> remove = Commands.literal("remove")
.then(removeSelf)
.then(removePlayer)
.then(removeOther);
return Commands.literal("money")
.then(send)
.then(give)
.then(remove)
.executes(MoneyCommand::getBalance); .executes(MoneyCommand::getBalance);
} }

@ -16,22 +16,24 @@ import xyz.soukup.ecoCraftCore.utilities.InventoryUtils;
import xyz.soukup.ecoCraftCore.utilities.Messages; import xyz.soukup.ecoCraftCore.utilities.Messages;
import xyz.soukup.ecoCraftCore.utilities.PDC; import xyz.soukup.ecoCraftCore.utilities.PDC;
@SuppressWarnings("UnstableApiUsage")
public class ShopCommand { public class ShopCommand {
public static LiteralArgumentBuilder<CommandSourceStack> createCommand() { public static LiteralArgumentBuilder<CommandSourceStack> createCommand() {
FloatArgumentType floatArgType = FloatArgumentType.floatArg(0.1F);
LiteralArgumentBuilder<CommandSourceStack> buy = Commands.literal("buy")
.then(Commands.argument("buy_price", floatArgType).executes(ShopCommand::createShop));
LiteralArgumentBuilder<CommandSourceStack> sell = Commands.literal("sell")
.then(Commands.argument("sell_price", floatArgType).executes(ShopCommand::createShop));
return Commands.literal("shop") return Commands.literal("shop")
.then(Commands.argument("amount", IntegerArgumentType.integer(1)) .then(Commands.argument("amount", IntegerArgumentType.integer(1))
.then(Commands.literal("buy") .then(buy.then(sell))
.then(Commands.argument("buy_price", FloatArgumentType.floatArg(0.1F)) .then(sell.then(buy)));
.executes(ShopCommand::createShop)
.then(Commands.literal("sell")
.then(Commands.argument("sell_price", FloatArgumentType.floatArg(0.1F))
.executes(ShopCommand::createShop)))))
.then(Commands.literal("sell")
.then(Commands.argument("sell_price", FloatArgumentType.floatArg(0.1F))
.executes(ShopCommand::createShop)
.then(Commands.literal("buy")
.then(Commands.argument("buy_price", FloatArgumentType.floatArg(0.1F))
.executes(ShopCommand::createShop))))));
} }
@ -51,14 +53,14 @@ public class ShopCommand {
try { try {
buyPrice = ctx.getArgument("buy_price", Float.class); buyPrice = ctx.getArgument("buy_price", Float.class);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
buyPrice = 0F; buyPrice = (Float) 0F;
} }
Float sellPrice; Float sellPrice;
try { try {
sellPrice = ctx.getArgument("sell_price", Float.class); sellPrice = ctx.getArgument("sell_price", Float.class);
}catch (IllegalArgumentException e){ }catch (IllegalArgumentException e){
sellPrice = 0F; sellPrice = (Float) 0F;
} }
@ -85,7 +87,7 @@ public class ShopCommand {
virtualChest.databaseSave(); virtualChest.databaseSave();
PDC.set(chest, "virtual", virtualChest.getId()); PDC.set(chest, "virtual", Integer.valueOf(virtualChest.getId()));
int freeSpace = InventoryUtils.getSpaceLeft(chest.getInventory(), item); int freeSpace = InventoryUtils.getSpaceLeft(chest.getInventory(), item);
int stock = InventoryUtils.getCount(chest.getInventory(), item); int stock = InventoryUtils.getCount(chest.getInventory(), item);

Loading…
Cancel
Save