pull/6/head
parent
b6fd6812e9
commit
eb9b40aa61
11 changed files with 449 additions and 34 deletions
@ -0,0 +1,15 @@ |
|||||||
|
package xyz.soukup.ecoCraftCore.events; |
||||||
|
|
||||||
|
import org.bukkit.event.EventHandler; |
||||||
|
import org.bukkit.event.Listener; |
||||||
|
import org.bukkit.event.player.PlayerJoinEvent; |
||||||
|
import xyz.soukup.ecoCraftCore.objects.Account; |
||||||
|
|
||||||
|
public class PreparePlayer implements Listener { |
||||||
|
|
||||||
|
@EventHandler |
||||||
|
public void preparePlayer(PlayerJoinEvent event){ |
||||||
|
Account account = Account.getOrCreate(event.getPlayer()); |
||||||
|
account.save(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,162 @@ |
|||||||
|
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 net.kyori.adventure.text.Component; |
||||||
|
import org.bukkit.Material; |
||||||
|
import org.bukkit.block.Block; |
||||||
|
import org.bukkit.block.Sign; |
||||||
|
import org.bukkit.entity.Player; |
||||||
|
import org.bukkit.event.EventHandler; |
||||||
|
import org.bukkit.event.Listener; |
||||||
|
import org.bukkit.event.block.Action; |
||||||
|
import org.bukkit.event.player.PlayerInteractEvent; |
||||||
|
import org.bukkit.inventory.Inventory; |
||||||
|
import org.bukkit.inventory.ItemStack; |
||||||
|
import xyz.soukup.ecoCraftCore.objects.Account; |
||||||
|
import xyz.soukup.ecoCraftCore.objects.Shop; |
||||||
|
import xyz.soukup.ecoCraftCore.objects.Transaction; |
||||||
|
import xyz.soukup.ecoCraftCore.objects.VirtualChest; |
||||||
|
import xyz.soukup.ecoCraftCore.utilities.Converter; |
||||||
|
import xyz.soukup.ecoCraftCore.utilities.InventoryUtils; |
||||||
|
import xyz.soukup.ecoCraftCore.utilities.PDC; |
||||||
|
|
||||||
|
public class ShopLogic implements Listener { |
||||||
|
|
||||||
|
@EventHandler |
||||||
|
public void openShop(PlayerInteractEvent event){ |
||||||
|
|
||||||
|
if (event.getAction() != Action.RIGHT_CLICK_BLOCK){ |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
Block block = event.getClickedBlock(); |
||||||
|
|
||||||
|
assert block != null; |
||||||
|
if (!(block.getState() instanceof Sign sign)){ |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
Integer id = PDC.getInteger(sign, "shop"); |
||||||
|
|
||||||
|
if (id == null){ |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
Shop shop = Shop.findById(id); |
||||||
|
|
||||||
|
if (shop == null){ |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
event.setCancelled(true); |
||||||
|
|
||||||
|
Player player = event.getPlayer(); |
||||||
|
|
||||||
|
Gui gui = buildShopGui(player, shop); |
||||||
|
|
||||||
|
gui.open(player); |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public static Gui buildShopGui(Player player, Shop shop){ |
||||||
|
|
||||||
|
Gui gui = Gui.gui() |
||||||
|
.title(Component.text("Obchod")) |
||||||
|
.rows(1) |
||||||
|
.create(); |
||||||
|
|
||||||
|
Float buyPrice = shop.getPriceBuy(); |
||||||
|
Float sellPrice = shop.getPriceSell(); |
||||||
|
int amount = shop.getAmount(); |
||||||
|
|
||||||
|
if (buyPrice != 0){ |
||||||
|
GuiItem one = ItemBuilder |
||||||
|
.from(Material.LIME_WOOL) |
||||||
|
.name(Converter.toComponent("&aKoupit " + amount + "ks za " + String.format("%.2f", buyPrice))) |
||||||
|
.asGuiItem(); |
||||||
|
one.setAction(event -> { |
||||||
|
event.setCancelled(true); |
||||||
|
ShopLogic.buy(shop, player, 1); |
||||||
|
}); |
||||||
|
|
||||||
|
GuiItem eight = ItemBuilder |
||||||
|
.from(Material.LIME_WOOL) |
||||||
|
.name(Converter.toComponent("&aKoupit " + amount*8 + "ks za " + String.format("%.2f", buyPrice*8))) |
||||||
|
.asGuiItem(); |
||||||
|
eight.setAction(event -> { |
||||||
|
event.setCancelled(true); |
||||||
|
ShopLogic.buy(shop, player, 8); |
||||||
|
}); |
||||||
|
gui.setItem(0, one); |
||||||
|
gui.setItem(1, eight); |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
if (sellPrice != 0){ |
||||||
|
GuiItem one = ItemBuilder |
||||||
|
.from(Material.YELLOW_WOOL) |
||||||
|
.name(Converter.toComponent("&eProdat " + amount + "ks za " + String.format("%.2f", sellPrice))) |
||||||
|
.asGuiItem(); |
||||||
|
|
||||||
|
GuiItem eight = ItemBuilder |
||||||
|
.from(Material.YELLOW_WOOL) |
||||||
|
.name(Converter.toComponent("&eProdat " + amount*8 + "ks za " + String.format("%.2f", sellPrice*8))) |
||||||
|
.asGuiItem(); |
||||||
|
|
||||||
|
gui.setItem(7, one); |
||||||
|
gui.setItem(8, eight); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
GuiItem item = new GuiItem(shop.getItemStack()); |
||||||
|
|
||||||
|
gui.setItem(4, item); |
||||||
|
|
||||||
|
gui.setDefaultClickAction(event -> { |
||||||
|
event.setCancelled(true); |
||||||
|
}); |
||||||
|
return gui; |
||||||
|
} |
||||||
|
|
||||||
|
public static void buy(Shop shop, Player player, int multiplier){ |
||||||
|
int amount = shop.getAmount() * multiplier; |
||||||
|
float price = shop.getPriceBuy() * multiplier; |
||||||
|
ItemStack itemStack = shop.getItemStack(); |
||||||
|
itemStack.setAmount(amount); |
||||||
|
|
||||||
|
Account account = Account.getOrCreate(player); |
||||||
|
|
||||||
|
if (price > account.getBalance()){ |
||||||
|
player.sendPlainMessage("nemaš dost mony"); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
VirtualChest virtualChest = VirtualChest.findById(shop.getVirtualChestID()); |
||||||
|
|
||||||
|
if (virtualChest == null){ |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
Inventory playerInventory = player.getInventory(); |
||||||
|
|
||||||
|
if (InventoryUtils.getSpaceLeft(playerInventory, itemStack) < amount){ |
||||||
|
player.sendPlainMessage("Nemáš dost místa v invu"); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
if (!virtualChest.removeItem(itemStack)){ |
||||||
|
player.sendPlainMessage("není dost věcí v chestce"); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
playerInventory.removeItem(itemStack); |
||||||
|
|
||||||
|
new Transaction(price, "player", player.getName(), "player", shop.getOwner(), "playerShop", shop.getItemName() ,Integer.toString(amount)); |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,109 @@ |
|||||||
|
package xyz.soukup.ecoCraftCore.objects; |
||||||
|
|
||||||
|
import com.j256.ormlite.field.DatabaseField; |
||||||
|
import com.j256.ormlite.stmt.QueryBuilder; |
||||||
|
import com.j256.ormlite.table.DatabaseTable; |
||||||
|
|
||||||
|
import org.bukkit.entity.Player; |
||||||
|
import xyz.soukup.ecoCraftCore.EcoCraftCore; |
||||||
|
import xyz.soukup.ecoCraftCore.utilities.DaoRegistry; |
||||||
|
|
||||||
|
import java.sql.SQLException; |
||||||
|
|
||||||
|
@DatabaseTable(tableName = "accounts") |
||||||
|
public class Account { |
||||||
|
@DatabaseField(generatedId = true) |
||||||
|
private int id; |
||||||
|
|
||||||
|
@DatabaseField(canBeNull = false) |
||||||
|
private String owner; |
||||||
|
|
||||||
|
@DatabaseField(canBeNull = false) |
||||||
|
private String type; |
||||||
|
|
||||||
|
@DatabaseField(canBeNull = false, defaultValue = "0") |
||||||
|
private float balance; |
||||||
|
|
||||||
|
public Account(){ |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public Account(String owner, String type){ |
||||||
|
this.owner = owner; |
||||||
|
this.type = type; |
||||||
|
} |
||||||
|
|
||||||
|
public String getOwner() { |
||||||
|
return owner; |
||||||
|
} |
||||||
|
|
||||||
|
public String getType() { |
||||||
|
return type; |
||||||
|
} |
||||||
|
|
||||||
|
public float getBalance() { |
||||||
|
return balance; |
||||||
|
} |
||||||
|
|
||||||
|
public int getId() { |
||||||
|
return id; |
||||||
|
} |
||||||
|
|
||||||
|
public void setBalance(float balance) { |
||||||
|
this.balance = balance; |
||||||
|
} |
||||||
|
|
||||||
|
public void deposit(float amount){ |
||||||
|
this.balance += amount; |
||||||
|
} |
||||||
|
|
||||||
|
public void withdraw(float amount){ |
||||||
|
this.balance -= amount; |
||||||
|
} |
||||||
|
|
||||||
|
public static Account getOrCreate(String type, String owner){ |
||||||
|
QueryBuilder<Account, Integer> queryBuilder = DaoRegistry.getAccountDao().queryBuilder(); |
||||||
|
try { |
||||||
|
queryBuilder.where() |
||||||
|
.eq("type", type) |
||||||
|
.and() |
||||||
|
.eq("owner", owner); |
||||||
|
Account account = DaoRegistry.getAccountDao().queryForFirst(queryBuilder.prepare()); |
||||||
|
|
||||||
|
if (account == null){ |
||||||
|
account = new Account(owner, type); |
||||||
|
} |
||||||
|
return account; |
||||||
|
} catch (SQLException e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static Account getOrCreate(Player player){ |
||||||
|
QueryBuilder<Account, Integer> queryBuilder = DaoRegistry.getAccountDao().queryBuilder(); |
||||||
|
try { |
||||||
|
queryBuilder.where() |
||||||
|
.eq("type", "player") |
||||||
|
.and() |
||||||
|
.eq("owner", player.getName()); |
||||||
|
Account account = DaoRegistry.getAccountDao().queryForFirst(queryBuilder.prepare()); |
||||||
|
|
||||||
|
if (account == null){ |
||||||
|
account = new Account(player.getName(), "player"); |
||||||
|
} |
||||||
|
return account; |
||||||
|
} catch (SQLException e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void save(){ |
||||||
|
try { |
||||||
|
DaoRegistry.getAccountDao().createOrUpdate(this); |
||||||
|
} catch (SQLException e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue