tak jsem na něčem zamakal

ble
jakub 2 months ago
parent cbd2ec8baa
commit 858c366b36
  1. 2
      pom.xml
  2. 2
      src/main/java/xyz/soukup/ecoCraftCore/EcoCraftCore.java
  3. 90
      src/main/java/xyz/soukup/ecoCraftCore/commands/MoneyCommand.java
  4. 77
      src/main/java/xyz/soukup/ecoCraftCore/commands/ShopCommand.java
  5. 15
      src/main/java/xyz/soukup/ecoCraftCore/events/ShopLogic.java
  6. 3
      src/main/java/xyz/soukup/ecoCraftCore/objects/Transaction.java
  7. 2
      src/main/resources/messages.yml

@ -136,7 +136,7 @@
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.21.3-R0.1-SNAPSHOT</version>
<version>1.21.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>

@ -73,7 +73,7 @@ public final class EcoCraftCore extends JavaPlugin {
}
private void prepareDatabase() throws SQLException {
String databaseUrl = "jdbc:mysql://soukup.xyz:3306/ecc";
String databaseUrl = "jdbc:mysql://localhost:3306/ecc";
connectionSource = new JdbcConnectionSource(databaseUrl, "ecc", "ecc");
Logger.getLogger("com.j256.ormlite.table.TableUtils").setLevel(Level.OFF);

@ -1,10 +1,8 @@
package xyz.soukup.ecoCraftCore.commands;
import com.mojang.brigadier.LiteralMessage;
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.papermc.paper.command.brigadier.CommandSourceStack;
@ -20,56 +18,53 @@ import xyz.soukup.ecoCraftCore.utilities.PHHM;
@SuppressWarnings("UnstableApiUsage")
public class MoneyCommand {
public static LiteralArgumentBuilder<CommandSourceStack> createCommand() {
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());
public static LiteralArgumentBuilder<CommandSourceStack> createCommand() {
// 1. Send Branch
LiteralArgumentBuilder<CommandSourceStack> send = Commands.literal("send")
.requires(source -> source.getSender() instanceof Player)
.then(playerArgument.then(amountArgument.executes(MoneyCommand::sendMoney)));
RequiredArgumentBuilder<CommandSourceStack, Float> selfGive = amountArgument
.requires(source -> source.getSender() instanceof Player)
.executes(MoneyCommand::giveMoneySelf);
RequiredArgumentBuilder<CommandSourceStack, PlayerSelectorArgumentResolver> playerGive = playerArgument
.then(amountArgument.executes(MoneyCommand::giveMoneyPlayer));
RequiredArgumentBuilder<CommandSourceStack, String> giveOther = typeArgument.then(ownerArgument.then(amountArgument.executes(MoneyCommand::giveMoneyOther)));
.then(Commands.argument("player", ArgumentTypes.player())
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F))
.executes(MoneyCommand::sendMoney)));
// 2. Give Branch
LiteralArgumentBuilder<CommandSourceStack> give = Commands.literal("give")
.then(selfGive)
.then(playerGive)
.then(giveOther);
RequiredArgumentBuilder<CommandSourceStack, Float> removeSelf = amountArgument
// Case: /money give <player> <amount>
.then(Commands.argument("player", ArgumentTypes.player())
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F))
.executes(MoneyCommand::giveMoneyPlayer)))
// Case: /money give <amount> (Self)
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F))
.requires(source -> source.getSender() instanceof Player)
.executes(MoneyCommand::removeMoneySelf);
RequiredArgumentBuilder<CommandSourceStack, PlayerSelectorArgumentResolver> removePlayer = playerArgument
.then(amountArgument.executes(MoneyCommand::removeMoneyPlayer));
RequiredArgumentBuilder<CommandSourceStack, String> removeOther = typeArgument
.then(ownerArgument
.then(amountArgument.executes(MoneyCommand::removeMoneyOther)));
.executes(MoneyCommand::giveMoneySelf))
// Case: /money give <type> <owner> <amount>
.then(Commands.argument("type", StringArgumentType.word())
.then(Commands.argument("owner", StringArgumentType.word())
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F))
.executes(MoneyCommand::giveMoneyOther))));
// 3. Remove Branch
LiteralArgumentBuilder<CommandSourceStack> remove = Commands.literal("remove")
.then(removeSelf)
.then(removePlayer)
.then(removeOther);
// Case: /money remove <player> <amount>
.then(Commands.argument("player", ArgumentTypes.player())
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F))
.executes(MoneyCommand::removeMoneyPlayer)))
// Case: /money remove <amount> (Self)
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F))
.requires(source -> source.getSender() instanceof Player)
.executes(MoneyCommand::removeMoneySelf))
// Case: /money remove <type> <owner> <amount>
.then(Commands.argument("type", StringArgumentType.word())
.then(Commands.argument("owner", StringArgumentType.word())
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F))
.executes(MoneyCommand::removeMoneyOther))));
// Main command: /money
return Commands.literal("money")
.then(send)
.then(give)
.then(remove)
.executes(MoneyCommand::getBalance);
}
private static int getBalance(CommandContext<CommandSourceStack> context) {
@ -81,10 +76,8 @@ public class MoneyCommand {
Account account = Account.getOrCreate(player);
Messages.send(player, "money.balance.self", PHHM.get(account));
return 1;
}
private static int sendMoney(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
PlayerSelectorArgumentResolver receiverResolver = context.getArgument("player", PlayerSelectorArgumentResolver.class);
Player receiver = receiverResolver.resolve(context.getSource()).getFirst();
@ -110,17 +103,14 @@ public class MoneyCommand {
private static int giveMoneyPlayer(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
CommandSender commandSender = context.getSource().getSender();
PlayerSelectorArgumentResolver receiverResolver = context.getArgument("player", PlayerSelectorArgumentResolver.class);
Player receiver = receiverResolver.resolve(context.getSource()).getFirst();
Float amount = context.getArgument("amount", Float.class);
Transaction transaction = new Transaction(amount, "admin", commandSender.getName(), "player", receiver.getName(), "admin");
transaction.process();
Messages.send(commandSender, "money.give.player", PHHM.get(transaction));
return 1;
}
@ -132,7 +122,6 @@ public class MoneyCommand {
transaction.process();
Messages.send(player, "money.give.self", PHHM.get(transaction));
return 1;
}
@ -146,24 +135,22 @@ public class MoneyCommand {
transaction.process();
Messages.send(commandSender, "money.give.other", PHHM.get(transaction));
return 1;
}
private static int removeMoneyPlayer(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
CommandSender commandSender = context.getSource().getSender();
PlayerSelectorArgumentResolver senderResolver = context.getArgument("player", PlayerSelectorArgumentResolver.class);
Player sender = senderResolver.resolve(context.getSource()).getFirst();
Float amount = context.getArgument("amount", Float.class);
Transaction transaction = new Transaction(amount, "player", sender.getName(), "admin", commandSender.getName(), "admin");
transaction.process();
Messages.send(commandSender, "money.remove.player", PHHM.get(transaction));
return 1;
}
private static int removeMoneySelf(CommandContext<CommandSourceStack> context) {
Player player = (Player) context.getSource().getSender();
Float amount = context.getArgument("amount", Float.class);
@ -172,9 +159,9 @@ public class MoneyCommand {
transaction.process();
Messages.send(player, "money.remove.self", PHHM.get(transaction));
return 1;
}
private static int removeMoneyOther(CommandContext<CommandSourceStack> context) {
CommandSender commandSender = context.getSource().getSender();
Float amount = context.getArgument("amount", Float.class);
@ -185,7 +172,6 @@ public class MoneyCommand {
transaction.process();
Messages.send(commandSender, "money.remove.other", PHHM.get(transaction));
return 1;
}
}

@ -18,23 +18,29 @@ import xyz.soukup.ecoCraftCore.utilities.PDC;
@SuppressWarnings("UnstableApiUsage")
public class ShopCommand {
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));
public static LiteralArgumentBuilder<CommandSourceStack> createCommand() {
// Define the argument types
var amountArg = Commands.argument("amount", IntegerArgumentType.integer(1));
var buyPriceArg = Commands.argument("buy_price", FloatArgumentType.floatArg(0.0F));
var sellPriceArg = Commands.argument("sell_price", FloatArgumentType.floatArg(0.0F));
// Path: /shop <amount> ...
return Commands.literal("shop")
.then(Commands.argument("amount", IntegerArgumentType.integer(1))
.then(buy.then(sell))
.then(sell.then(buy)));
.then(amountArg
// Branch 1: ... buy <price> [sell <price>]
.then(Commands.literal("buy")
.then(buyPriceArg
.executes(ShopCommand::createShop)
.then(Commands.literal("sell")
.then(sellPriceArg.executes(ShopCommand::createShop)))))
// Branch 2: ... sell <price> [buy <price>]
.then(Commands.literal("sell")
.then(sellPriceArg
.executes(ShopCommand::createShop)
.then(Commands.literal("buy")
.then(buyPriceArg.executes(ShopCommand::createShop))))));
}
private static int createShop(CommandContext<CommandSourceStack> ctx) {
@ -45,61 +51,56 @@ public class ShopCommand {
return 0;
}
// 1. Check if blocks are marked
Chest chest = RulerMarking.chests.get(player);
Sign sign = RulerMarking.signs.get(player);
Integer amount = ctx.getArgument("amount", Integer.class);
Float buyPrice;
try {
buyPrice = ctx.getArgument("buy_price", Float.class);
} catch (IllegalArgumentException e) {
buyPrice = (Float) 0F;
}
Float sellPrice;
try {
sellPrice = ctx.getArgument("sell_price", Float.class);
}catch (IllegalArgumentException e){
sellPrice = (Float) 0F;
}
if (sign == null || chest == null) {
Messages.send(player, "shop.error.not-marked");
return 0;
}
// 2. Validate Item in Hand
ItemStack item = player.getInventory().getItemInMainHand();
if (item.isEmpty()) {
Messages.send(player, "shop.error.empty-hand");
return 0;
}
// 3. Check if already a shop
if (PDC.getInteger(sign, "shop") != null) {
Messages.send(player, "shop.error.already-shop");
return 0;
}
// 4. Parse Arguments Safely
int amount = ctx.getArgument("amount", Integer.class);
VirtualChest virtualChest = VirtualChest.getOrCreate(chest);
virtualChest.databaseSave();
float buyPrice = 0.0F;
try {
buyPrice = ctx.getArgument("buy_price", Float.class);
} catch (IllegalArgumentException ignored) {}
float sellPrice = 0.0F;
try {
sellPrice = ctx.getArgument("sell_price", Float.class);
} catch (IllegalArgumentException ignored) {}
PDC.set(chest, "virtual", Integer.valueOf(virtualChest.getId()));
// 5. Database and PDC Logic
VirtualChest virtualChest = VirtualChest.getOrCreate(chest);
virtualChest.databaseSave();
PDC.set(chest, "virtual", virtualChest.getId());
// 6. Inventory Calculations
int freeSpace = InventoryUtils.getSpaceLeft(chest.getInventory(), item);
int stock = InventoryUtils.getCount(chest.getInventory(), item);
// 7. Final Creation
Shop shop = new Shop(player, item, stock, freeSpace, amount, buyPrice, sellPrice, virtualChest.getId());
shop.save();
shop.writeIntoSign(sign);
Messages.send(player, "shop.created");
return 1;
}
}

@ -3,6 +3,9 @@ package xyz.soukup.ecoCraftCore.events;
import dev.triumphteam.gui.builder.item.ItemBuilder;
import dev.triumphteam.gui.guis.Gui;
import dev.triumphteam.gui.guis.GuiItem;
import io.papermc.paper.dialog.Dialog;
import io.papermc.paper.registry.data.dialog.DialogBase;
import io.papermc.paper.registry.data.dialog.type.DialogType;
import net.kyori.adventure.text.Component;
import org.bukkit.Material;
import org.bukkit.block.Block;
@ -25,6 +28,7 @@ import xyz.soukup.ecoCraftCore.utilities.PDC;
import java.util.HashMap;
@SuppressWarnings("UnstableApiUsage")
public class ShopLogic implements Listener {
@EventHandler
@ -64,6 +68,10 @@ public class ShopLogic implements Listener {
//ZDE Přídat otevíraní shop edit gui
if (shop.getOwnerType().equals("player") && shop.getOwner().equals(player.getName())){
return;
}
Gui gui = buildShopGui(player, shop);
@ -160,6 +168,13 @@ public class ShopLogic implements Listener {
return gui;
}
public static Dialog editDialog(){
return Dialog.create(builder -> builder.empty()
.base(DialogBase.builder(Messages.get("gui.shop-edit.title")).build())
.type(DialogType.notice())
);
}
public static void buy(Shop shop, Player player, int multiplier){
if (VirtualChestLogic.openedChests.containsKey(shop.getVirtualChestID())){

@ -81,11 +81,14 @@ public class Transaction {
this.receiverType = receiverType;
this.receiver = receiver;
this.type = type;
this.primaryInfo = "";
this.secondaryInfo = "";
}
public Transaction(float amount, String senderType, String sender, String receiverType, String receiver, String type, String primaryInfo) {
this(amount, senderType, sender, receiverType, receiver, type);
this.primaryInfo = primaryInfo;
this.secondaryInfo = "";
}
public Transaction(float amount, String senderType, String sender, String receiverType, String receiver, String type, String primaryInfo, String secondaryInfo) {

@ -49,6 +49,8 @@ menu:
sell: "<yellow>Prodat <amount>ks za <price>$"
gui:
all: "<green>Seznam guis: <yellow><1>, <2>"
shop-edit:
title: "<green>Editace Obchodu"
shopadmin:
title: "GUI - <dark_green>Shop Admin"
items:

Loading…
Cancel
Save