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. 118
      src/main/java/xyz/soukup/ecoCraftCore/commands/MoneyCommand.java
  4. 87
      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> <dependency>
<groupId>io.papermc.paper</groupId> <groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId> <artifactId>paper-api</artifactId>
<version>1.21.3-R0.1-SNAPSHOT</version> <version>1.21.1-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>

@ -73,7 +73,7 @@ public final class EcoCraftCore extends JavaPlugin {
} }
private void prepareDatabase() throws SQLException { 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"); connectionSource = new JdbcConnectionSource(databaseUrl, "ecc", "ecc");
Logger.getLogger("com.j256.ormlite.table.TableUtils").setLevel(Level.OFF); Logger.getLogger("com.j256.ormlite.table.TableUtils").setLevel(Level.OFF);

@ -1,10 +1,8 @@
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;
@ -20,71 +18,66 @@ import xyz.soukup.ecoCraftCore.utilities.PHHM;
@SuppressWarnings("UnstableApiUsage") @SuppressWarnings("UnstableApiUsage")
public class MoneyCommand { 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") LiteralArgumentBuilder<CommandSourceStack> send = Commands.literal("send")
.requires(source -> source.getSender() instanceof Player) .requires(source -> source.getSender() instanceof Player)
.then(playerArgument.then(amountArgument.executes(MoneyCommand::sendMoney))); .then(Commands.argument("player", ArgumentTypes.player())
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F))
.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)));
// 2. Give Branch
LiteralArgumentBuilder<CommandSourceStack> give = Commands.literal("give") LiteralArgumentBuilder<CommandSourceStack> give = Commands.literal("give")
.then(selfGive) // Case: /money give <player> <amount>
.then(playerGive) .then(Commands.argument("player", ArgumentTypes.player())
.then(giveOther); .then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F))
.executes(MoneyCommand::giveMoneyPlayer)))
// Case: /money give <amount> (Self)
RequiredArgumentBuilder<CommandSourceStack, Float> removeSelf = amountArgument .then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F))
.requires(source -> source.getSender() instanceof Player) .requires(source -> source.getSender() instanceof Player)
.executes(MoneyCommand::removeMoneySelf); .executes(MoneyCommand::giveMoneySelf))
// Case: /money give <type> <owner> <amount>
RequiredArgumentBuilder<CommandSourceStack, PlayerSelectorArgumentResolver> removePlayer = playerArgument .then(Commands.argument("type", StringArgumentType.word())
.then(amountArgument.executes(MoneyCommand::removeMoneyPlayer)); .then(Commands.argument("owner", StringArgumentType.word())
.then(Commands.argument("amount", FloatArgumentType.floatArg(0.1F))
RequiredArgumentBuilder<CommandSourceStack, String> removeOther = typeArgument .executes(MoneyCommand::giveMoneyOther))));
.then(ownerArgument
.then(amountArgument.executes(MoneyCommand::removeMoneyOther))); // 3. Remove Branch
LiteralArgumentBuilder<CommandSourceStack> remove = Commands.literal("remove") LiteralArgumentBuilder<CommandSourceStack> remove = Commands.literal("remove")
.then(removeSelf) // Case: /money remove <player> <amount>
.then(removePlayer) .then(Commands.argument("player", ArgumentTypes.player())
.then(removeOther); .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") return Commands.literal("money")
.then(send) .then(send)
.then(give) .then(give)
.then(remove) .then(remove)
.executes(MoneyCommand::getBalance); .executes(MoneyCommand::getBalance);
} }
private static int getBalance(CommandContext<CommandSourceStack> context){ private static int getBalance(CommandContext<CommandSourceStack> context) {
CommandSender commandSender = context.getSource().getSender(); CommandSender commandSender = context.getSource().getSender();
if (!(commandSender instanceof Player player)){ if (!(commandSender instanceof Player player)) {
Messages.send(commandSender, "generic.error.not-player"); Messages.send(commandSender, "generic.error.not-player");
return 0; return 0;
} }
Account account = Account.getOrCreate(player); Account account = Account.getOrCreate(player);
Messages.send(player, "money.balance.self", PHHM.get(account)); Messages.send(player, "money.balance.self", PHHM.get(account));
return 1; return 1;
} }
private static int sendMoney(CommandContext<CommandSourceStack> context) throws CommandSyntaxException { private static int sendMoney(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
PlayerSelectorArgumentResolver receiverResolver = context.getArgument("player", PlayerSelectorArgumentResolver.class); PlayerSelectorArgumentResolver receiverResolver = context.getArgument("player", PlayerSelectorArgumentResolver.class);
Player receiver = receiverResolver.resolve(context.getSource()).getFirst(); Player receiver = receiverResolver.resolve(context.getSource()).getFirst();
@ -94,12 +87,12 @@ public class MoneyCommand {
Account senderAccount = Account.getOrCreate(sender); Account senderAccount = Account.getOrCreate(sender);
if (amount > senderAccount.getBalance()){ if (amount > senderAccount.getBalance()) {
Messages.send(sender, "generic.error.no-funds"); Messages.send(sender, "generic.error.no-funds");
return 0; return 0;
} }
Transaction transaction = new Transaction(amount,"player", sender.getName(), "player", receiver.getName(), "player"); Transaction transaction = new Transaction(amount, "player", sender.getName(), "player", receiver.getName(), "player");
transaction.process(); transaction.process();
Messages.send(sender, "money.send.player", PHHM.get(transaction)); Messages.send(sender, "money.send.player", PHHM.get(transaction));
@ -110,82 +103,75 @@ public class MoneyCommand {
private static int giveMoneyPlayer(CommandContext<CommandSourceStack> context) throws CommandSyntaxException { private static int giveMoneyPlayer(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
CommandSender commandSender = context.getSource().getSender(); CommandSender commandSender = context.getSource().getSender();
PlayerSelectorArgumentResolver receiverResolver = context.getArgument("player", PlayerSelectorArgumentResolver.class); PlayerSelectorArgumentResolver receiverResolver = context.getArgument("player", PlayerSelectorArgumentResolver.class);
Player receiver = receiverResolver.resolve(context.getSource()).getFirst(); Player receiver = receiverResolver.resolve(context.getSource()).getFirst();
Float amount = context.getArgument("amount", Float.class); Float amount = context.getArgument("amount", Float.class);
Transaction transaction = new Transaction(amount,"admin", commandSender.getName(), "player", receiver.getName(), "admin"); Transaction transaction = new Transaction(amount, "admin", commandSender.getName(), "player", receiver.getName(), "admin");
transaction.process(); transaction.process();
Messages.send(commandSender, "money.give.player", PHHM.get(transaction)); Messages.send(commandSender, "money.give.player", PHHM.get(transaction));
return 1; return 1;
} }
private static int giveMoneySelf(CommandContext<CommandSourceStack> context){ private static int giveMoneySelf(CommandContext<CommandSourceStack> context) {
Player player = (Player) context.getSource().getSender(); Player player = (Player) context.getSource().getSender();
Float amount = context.getArgument("amount", Float.class); Float amount = context.getArgument("amount", Float.class);
Transaction transaction = new Transaction(amount,"admin", player.getName(), "player", player.getName(), "admin"); Transaction transaction = new Transaction(amount, "admin", player.getName(), "player", player.getName(), "admin");
transaction.process(); transaction.process();
Messages.send(player, "money.give.self", PHHM.get(transaction)); Messages.send(player, "money.give.self", PHHM.get(transaction));
return 1; return 1;
} }
private static int giveMoneyOther(CommandContext<CommandSourceStack> context){ private static int giveMoneyOther(CommandContext<CommandSourceStack> context) {
CommandSender commandSender = context.getSource().getSender(); CommandSender commandSender = context.getSource().getSender();
Float amount = context.getArgument("amount", Float.class); Float amount = context.getArgument("amount", Float.class);
String owner = context.getArgument("owner", String.class); String owner = context.getArgument("owner", String.class);
String type = context.getArgument("type", String.class); String type = context.getArgument("type", String.class);
Transaction transaction = new Transaction(amount,"admin", commandSender.getName(), type, owner, "admin"); Transaction transaction = new Transaction(amount, "admin", commandSender.getName(), type, owner, "admin");
transaction.process(); transaction.process();
Messages.send(commandSender, "money.give.other", PHHM.get(transaction)); Messages.send(commandSender, "money.give.other", PHHM.get(transaction));
return 1; return 1;
} }
private static int removeMoneyPlayer(CommandContext<CommandSourceStack> context) throws CommandSyntaxException { private static int removeMoneyPlayer(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
CommandSender commandSender = context.getSource().getSender(); CommandSender commandSender = context.getSource().getSender();
PlayerSelectorArgumentResolver senderResolver = context.getArgument("player", PlayerSelectorArgumentResolver.class); PlayerSelectorArgumentResolver senderResolver = context.getArgument("player", PlayerSelectorArgumentResolver.class);
Player sender = senderResolver.resolve(context.getSource()).getFirst(); Player sender = senderResolver.resolve(context.getSource()).getFirst();
Float amount = context.getArgument("amount", Float.class); Float amount = context.getArgument("amount", Float.class);
Transaction transaction = new Transaction(amount, "player", sender.getName(),"admin", commandSender.getName(), "admin"); Transaction transaction = new Transaction(amount, "player", sender.getName(), "admin", commandSender.getName(), "admin");
transaction.process(); transaction.process();
Messages.send(commandSender, "money.remove.player", PHHM.get(transaction)); Messages.send(commandSender, "money.remove.player", PHHM.get(transaction));
return 1; return 1;
} }
private static int removeMoneySelf(CommandContext<CommandSourceStack> context){
private static int removeMoneySelf(CommandContext<CommandSourceStack> context) {
Player player = (Player) context.getSource().getSender(); Player player = (Player) context.getSource().getSender();
Float amount = context.getArgument("amount", Float.class); Float amount = context.getArgument("amount", Float.class);
Transaction transaction = new Transaction(amount,"player", player.getName(),"admin", player.getName(), "admin"); Transaction transaction = new Transaction(amount, "player", player.getName(), "admin", player.getName(), "admin");
transaction.process(); transaction.process();
Messages.send(player, "money.remove.self", PHHM.get(transaction)); Messages.send(player, "money.remove.self", PHHM.get(transaction));
return 1; return 1;
} }
private static int removeMoneyOther(CommandContext<CommandSourceStack> context){
private static int removeMoneyOther(CommandContext<CommandSourceStack> context) {
CommandSender commandSender = context.getSource().getSender(); CommandSender commandSender = context.getSource().getSender();
Float amount = context.getArgument("amount", Float.class); Float amount = context.getArgument("amount", Float.class);
String owner = context.getArgument("owner", String.class); String owner = context.getArgument("owner", String.class);
String type = context.getArgument("type", String.class); String type = context.getArgument("type", String.class);
Transaction transaction = new Transaction(amount, type, owner,"admin", commandSender.getName(), "admin"); Transaction transaction = new Transaction(amount, type, owner, "admin", commandSender.getName(), "admin");
transaction.process(); transaction.process();
Messages.send(commandSender, "money.remove.other", PHHM.get(transaction)); Messages.send(commandSender, "money.remove.other", PHHM.get(transaction));
return 1; return 1;
} }
} }

@ -18,88 +18,89 @@ import xyz.soukup.ecoCraftCore.utilities.PDC;
@SuppressWarnings("UnstableApiUsage") @SuppressWarnings("UnstableApiUsage")
public class ShopCommand { 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") public static LiteralArgumentBuilder<CommandSourceStack> createCommand() {
.then(Commands.argument("sell_price", floatArgType).executes(ShopCommand::createShop)); // 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") return Commands.literal("shop")
.then(Commands.argument("amount", IntegerArgumentType.integer(1)) .then(amountArg
.then(buy.then(sell)) // Branch 1: ... buy <price> [sell <price>]
.then(sell.then(buy))); .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){ private static int createShop(CommandContext<CommandSourceStack> ctx) {
CommandSourceStack source = ctx.getSource(); CommandSourceStack source = ctx.getSource();
if (!(source.getSender() instanceof Player player)){ if (!(source.getSender() instanceof Player player)) {
Messages.send(source.getSender(), "generic.error.not-player"); Messages.send(source.getSender(), "generic.error.not-player");
return 0; return 0;
} }
// 1. Check if blocks are marked
Chest chest = RulerMarking.chests.get(player); Chest chest = RulerMarking.chests.get(player);
Sign sign = RulerMarking.signs.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) {
if (sign == null || chest == null){
Messages.send(player, "shop.error.not-marked"); Messages.send(player, "shop.error.not-marked");
return 0; return 0;
} }
// 2. Validate Item in Hand
ItemStack item = player.getInventory().getItemInMainHand(); ItemStack item = player.getInventory().getItemInMainHand();
if (item.isEmpty()) {
if (item.isEmpty()){
Messages.send(player, "shop.error.empty-hand"); Messages.send(player, "shop.error.empty-hand");
return 0; return 0;
} }
if (PDC.getInteger(sign, "shop") != null){ // 3. Check if already a shop
if (PDC.getInteger(sign, "shop") != null) {
Messages.send(player, "shop.error.already-shop"); Messages.send(player, "shop.error.already-shop");
return 0; return 0;
} }
// 4. Parse Arguments Safely
int amount = ctx.getArgument("amount", Integer.class);
VirtualChest virtualChest = VirtualChest.getOrCreate(chest); float buyPrice = 0.0F;
virtualChest.databaseSave(); 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 freeSpace = InventoryUtils.getSpaceLeft(chest.getInventory(), item);
int stock = InventoryUtils.getCount(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 shop = new Shop(player, item, stock, freeSpace, amount, buyPrice, sellPrice, virtualChest.getId());
shop.save(); shop.save();
shop.writeIntoSign(sign); shop.writeIntoSign(sign);
Messages.send(player, "shop.created"); Messages.send(player, "shop.created");
return 1; return 1;
} }
} }

@ -3,6 +3,9 @@ package xyz.soukup.ecoCraftCore.events;
import dev.triumphteam.gui.builder.item.ItemBuilder; import dev.triumphteam.gui.builder.item.ItemBuilder;
import dev.triumphteam.gui.guis.Gui; import dev.triumphteam.gui.guis.Gui;
import dev.triumphteam.gui.guis.GuiItem; 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 net.kyori.adventure.text.Component;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -25,6 +28,7 @@ import xyz.soukup.ecoCraftCore.utilities.PDC;
import java.util.HashMap; import java.util.HashMap;
@SuppressWarnings("UnstableApiUsage")
public class ShopLogic implements Listener { public class ShopLogic implements Listener {
@EventHandler @EventHandler
@ -64,6 +68,10 @@ public class ShopLogic implements Listener {
//ZDE Přídat otevíraní shop edit gui //ZDE Přídat otevíraní shop edit gui
if (shop.getOwnerType().equals("player") && shop.getOwner().equals(player.getName())){
return;
}
Gui gui = buildShopGui(player, shop); Gui gui = buildShopGui(player, shop);
@ -160,6 +168,13 @@ public class ShopLogic implements Listener {
return gui; 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){ public static void buy(Shop shop, Player player, int multiplier){
if (VirtualChestLogic.openedChests.containsKey(shop.getVirtualChestID())){ if (VirtualChestLogic.openedChests.containsKey(shop.getVirtualChestID())){

@ -81,11 +81,14 @@ public class Transaction {
this.receiverType = receiverType; this.receiverType = receiverType;
this.receiver = receiver; this.receiver = receiver;
this.type = type; this.type = type;
this.primaryInfo = "";
this.secondaryInfo = "";
} }
public Transaction(float amount, String senderType, String sender, String receiverType, String receiver, String type, String primaryInfo) { public Transaction(float amount, String senderType, String sender, String receiverType, String receiver, String type, String primaryInfo) {
this(amount, senderType, sender, receiverType, receiver, type); this(amount, senderType, sender, receiverType, receiver, type);
this.primaryInfo = primaryInfo; this.primaryInfo = primaryInfo;
this.secondaryInfo = "";
} }
public Transaction(float amount, String senderType, String sender, String receiverType, String receiver, String type, String primaryInfo, String 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>$" sell: "<yellow>Prodat <amount>ks za <price>$"
gui: gui:
all: "<green>Seznam guis: <yellow><1>, <2>" all: "<green>Seznam guis: <yellow><1>, <2>"
shop-edit:
title: "<green>Editace Obchodu"
shopadmin: shopadmin:
title: "GUI - <dark_green>Shop Admin" title: "GUI - <dark_green>Shop Admin"
items: items:

Loading…
Cancel
Save