commit 7c354ccc570440d61d84ecb308e4843193c9a83d Author: jakub Date: Sun Jun 1 20:21:58 2025 +0200 ik diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4788b4b --- /dev/null +++ b/.gitignore @@ -0,0 +1,113 @@ +# User-specific stuff +.idea/ + +*.iml +*.ipr +*.iws + +# IntelliJ +out/ + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +target/ + +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next + +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +.flattened-pom.xml + +# Common working directory +run/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b2759e2 --- /dev/null +++ b/pom.xml @@ -0,0 +1,137 @@ + + + 4.0.0 + + xyz.soukup + ecocraftcore + 1.0-SNAPSHOT + jar + + ecocraftcore + + + 21 + UTF-8 + + + + clean package + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + ${java.version} + ${java.version} + + + + + io.ebean + ebean-maven-plugin + 14.1.0 + + + main + process-classes + + enhance + + + + + xyz.soukup.ecoCraftCore.objects.** + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.5.3 + + + package + + shade + + + + + io.ebean + xyz.soukup.shaded.io.ebean + + + io.ebeaninternal + xyz.soukup.shaded.io.ebeaninternal + + + org.avaje + xyz.soukup.shaded.org.avaje + + + + + + false + false + + + + + + + + + src/main/resources + true + + + + + + + papermc-repo + https://repo.papermc.io/repository/maven-public/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + + + + + + com.j256.ormlite + ormlite-core + 6.1 + + + com.j256.ormlite + ormlite-jdbc + 6.1 + + + + com.mysql + mysql-connector-j + 9.3.0 + + + + io.github.odalita-developments.odalitamenus + core + 0.6.2 + + + + io.papermc.paper + paper-api + 1.21.3-R0.1-SNAPSHOT + provided + + + diff --git a/src/main/java/xyz/soukup/ecoCraftCore/EcoCraftCore.java b/src/main/java/xyz/soukup/ecoCraftCore/EcoCraftCore.java new file mode 100644 index 0000000..66848ca --- /dev/null +++ b/src/main/java/xyz/soukup/ecoCraftCore/EcoCraftCore.java @@ -0,0 +1,68 @@ +package xyz.soukup.ecoCraftCore; + +import com.j256.ormlite.dao.DaoManager; +import com.j256.ormlite.jdbc.JdbcConnectionSource; +import com.j256.ormlite.support.ConnectionSource; +import com.j256.ormlite.table.TableUtils; +import org.bukkit.plugin.java.JavaPlugin; +import xyz.soukup.ecoCraftCore.commands.TransactionCommand; +import xyz.soukup.ecoCraftCore.objects.Shop; +import xyz.soukup.ecoCraftCore.objects.Transaction; +import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; +import xyz.soukup.ecoCraftCore.objects.VirtualChest; +import xyz.soukup.ecoCraftCore.utilities.DaoRegistry; + +import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; + +public final class EcoCraftCore extends JavaPlugin { + + public static ConnectionSource connectionSource; + + @Override + public void onEnable() { + this.getLogger().info("plugin starting out"); + + try { + prepareDatabase(); + registerCommands(); + } catch (SQLException e) { + e.printStackTrace(); + getLogger().severe("Failed to initialize database."); + } + } + + @Override + public void onDisable() { + try { + if (connectionSource != null) { + connectionSource.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void prepareDatabase() throws SQLException { + String databaseUrl = "jdbc:mysql://localhost:3306/ecc"; + connectionSource = new JdbcConnectionSource(databaseUrl, "ecc", ""); + Logger.getLogger("com.j256.ormlite.table.TableUtils").setLevel(Level.OFF); + + + TableUtils.createTableIfNotExists(connectionSource, Transaction.class); + TableUtils.createTableIfNotExists(connectionSource, Shop.class); + TableUtils.createTableIfNotExists(connectionSource, VirtualChest.class); + + DaoRegistry.setTransactionDao(DaoManager.createDao(connectionSource, Transaction.class)); + DaoRegistry.setShopDao(DaoManager.createDao(connectionSource, Shop.class)); + DaoRegistry.setVirtualChestDao(DaoManager.createDao(connectionSource, VirtualChest.class)); + + } + + private void registerCommands() { + this.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, + event -> event.registrar().register(TransactionCommand.createCommand().build()) + ); + } +} diff --git a/src/main/java/xyz/soukup/ecoCraftCore/commands/TransactionCommand.java b/src/main/java/xyz/soukup/ecoCraftCore/commands/TransactionCommand.java new file mode 100644 index 0000000..0ad0d98 --- /dev/null +++ b/src/main/java/xyz/soukup/ecoCraftCore/commands/TransactionCommand.java @@ -0,0 +1,41 @@ +package xyz.soukup.ecoCraftCore.commands; + +import com.j256.ormlite.dao.Dao; +import com.j256.ormlite.dao.DaoManager; +import com.mojang.brigadier.arguments.DoubleArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.command.brigadier.Commands; +import xyz.soukup.ecoCraftCore.EcoCraftCore; +import xyz.soukup.ecoCraftCore.objects.Transaction; + +public class TransactionCommand { + + public static LiteralArgumentBuilder createCommand() { + return Commands.literal("transaction") + .then(Commands.literal("simulate") + .then(Commands.argument("sender", StringArgumentType.word()) + .then(Commands.argument("receiver", StringArgumentType.word()) + .then(Commands.argument("amount", DoubleArgumentType.doubleArg(0.1)) + .executes(TransactionCommand::simulateTransaction))))); + } + + private static int simulateTransaction(CommandContext ctx) { + String sender = StringArgumentType.getString(ctx, "sender"); + String receiver = StringArgumentType.getString(ctx, "receiver"); + double amount = DoubleArgumentType.getDouble(ctx, "amount"); + + try { + Dao dao = DaoManager.createDao(EcoCraftCore.connectionSource, Transaction.class); + Transaction tx = new Transaction(amount, "player", sender, "player", receiver, "command"); + dao.create(tx); + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + + return 1; + } +} diff --git a/src/main/java/xyz/soukup/ecoCraftCore/objects/Shop.java b/src/main/java/xyz/soukup/ecoCraftCore/objects/Shop.java new file mode 100644 index 0000000..f7ee0ac --- /dev/null +++ b/src/main/java/xyz/soukup/ecoCraftCore/objects/Shop.java @@ -0,0 +1,141 @@ +package xyz.soukup.ecoCraftCore.objects; +import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.table.DatabaseTable; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import xyz.soukup.ecoCraftCore.utilities.DaoRegistry; +import xyz.soukup.ecoCraftCore.utilities.JSONConverter; + +import java.sql.SQLException; +import java.sql.Timestamp; + +@DatabaseTable(tableName = "shops") +public class Shop { + + @DatabaseField(generatedId = true) + private long id; + + @DatabaseField(canBeNull = false) + private String owner; + + @DatabaseField(canBeNull = false) + private String ownerType; + + + @DatabaseField(canBeNull = false) + private String itemName; + + @DatabaseField(canBeNull = false, columnName = "itemstack") + private String itemStackJson; + + @DatabaseField(canBeNull = false) + private int amount; + + @DatabaseField + private float priceBuy; + + @DatabaseField + private float priceSell; + + @DatabaseField(canBeNull = false, defaultValue = "0") + private int stock; + + + @DatabaseField(canBeNull = false) + private int virtualChestID; + + @DatabaseField(canBeNull = false) + private Timestamp created = new Timestamp(System.currentTimeMillis()); + + public Shop() { + + } + + public Shop(Player player, ItemStack itemStack, int stock, int amount, float priceBuy, float priceSell, int virtualChestID) { + this.ownerType = "player"; + this.owner = player.getName(); + this.itemStackJson = JSONConverter.itemStackToJson(itemStack); + this.itemName = itemStack.getType().toString(); + this.stock = stock; + this.amount = amount; + this.priceBuy = priceBuy; + this.priceSell = priceSell; + this.virtualChestID = virtualChestID; + } + + public void setItemName(String itemName) { + this.itemName = itemName; + } + + public void setItemStackJson(String itemStackJson) { + this.itemStackJson = itemStackJson; + } + + public void setAmount(int amount) { + this.amount = amount; + } + + public void setPriceBuy(float priceBuy) { + this.priceBuy = priceBuy; + } + + public void setPriceSell(float priceSell) { + this.priceSell = priceSell; + } + + public void setStock(int stock) { + this.stock = stock; + } + + public static Shop findById(int id) { + try { + return DaoRegistry.getShopDao().queryForId(id); + } catch (SQLException e) { + return null; + } + } + + public ItemStack getItemStack() { + return JSONConverter.itemStackFromJson(this.itemStackJson); + } + + public long getId() { + return id; + } + + public String getOwner() { + return owner; + } + + public String getOwnerType() { + return ownerType; + } + + public String getItemName() { + return itemName; + } + + public int getAmount() { + return amount; + } + + public float getPriceBuy() { + return priceBuy; + } + + public float getPriceSell() { + return priceSell; + } + + public int getStock() { + return stock; + } + + public int getVirtualChestID() { + return virtualChestID; + } + + public Timestamp getCreated() { + return created; + } +} diff --git a/src/main/java/xyz/soukup/ecoCraftCore/objects/Transaction.java b/src/main/java/xyz/soukup/ecoCraftCore/objects/Transaction.java new file mode 100644 index 0000000..8a6a135 --- /dev/null +++ b/src/main/java/xyz/soukup/ecoCraftCore/objects/Transaction.java @@ -0,0 +1,76 @@ +package xyz.soukup.ecoCraftCore.objects; + +import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.table.DatabaseTable; + +import java.sql.Timestamp; + +@DatabaseTable(tableName = "transactions") +public class Transaction { + + @DatabaseField(generatedId = true) + private long id; + + @DatabaseField(canBeNull = false) + private double amount; + + @DatabaseField(canBeNull = false, columnName = "sender_type") + private String senderType; + + @DatabaseField(canBeNull = false) + private String sender; + + @DatabaseField(canBeNull = false, columnName = "receiver_type") + private String receiverType; + + @DatabaseField(canBeNull = false) + private String receiver; + + @DatabaseField(canBeNull = false) + private String type; + + @DatabaseField(columnName = "primary_info") + private String primaryInfo; + + @DatabaseField(columnName = "secondary_info") + private String secondaryInfo; + + @DatabaseField(canBeNull = false) + private Timestamp timestamp = new Timestamp(System.currentTimeMillis()); + + + public Transaction() { + // ORMLite requires a no-arg constructor + } + + public Transaction(double amount, String senderType, String sender, String receiverType, String receiver, String type) { + this.amount = amount; + this.senderType = senderType; + this.sender = sender; + this.receiverType = receiverType; + this.receiver = receiver; + this.type = type; + } + + public Transaction(double amount, String senderType, String sender, String receiverType, String receiver, String type, String primaryInfo) { + this(amount, senderType, sender, receiverType, receiver, type); + this.primaryInfo = primaryInfo; + } + + public Transaction(double amount, String senderType, String sender, String receiverType, String receiver, String type, String primaryInfo, String secondaryInfo) { + this(amount, senderType, sender, receiverType, receiver, type, primaryInfo); + this.secondaryInfo = secondaryInfo; + } + + // Getters + public long getId() { return id; } + public Timestamp getTimestamp() { return timestamp; } + public double getAmount() { return amount; } + public String getSenderType() { return senderType; } + public String getSender() { return sender; } + public String getReceiverType() { return receiverType; } + public String getReceiver() { return receiver; } + public String getType() { return type; } + public String getPrimaryInfo() { return primaryInfo; } + public String getSecondaryInfo() { return secondaryInfo; } +} diff --git a/src/main/java/xyz/soukup/ecoCraftCore/objects/VirtualChest.java b/src/main/java/xyz/soukup/ecoCraftCore/objects/VirtualChest.java new file mode 100644 index 0000000..2539c9b --- /dev/null +++ b/src/main/java/xyz/soukup/ecoCraftCore/objects/VirtualChest.java @@ -0,0 +1,20 @@ +package xyz.soukup.ecoCraftCore.objects; + +import com.j256.ormlite.field.DataType; +import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.table.DatabaseTable; +import org.bukkit.inventory.ItemStack; + +@DatabaseTable(tableName = "virtual_chests") +public class VirtualChest { + + @DatabaseField(generatedId = true) + private long id; + + @DatabaseField + private String type; + + @DatabaseField(dataType = DataType.SERIALIZABLE, columnName = "data") + private ItemStack[] itemStacks; + +} diff --git a/src/main/java/xyz/soukup/ecoCraftCore/utilities/DaoRegistry.java b/src/main/java/xyz/soukup/ecoCraftCore/utilities/DaoRegistry.java new file mode 100644 index 0000000..60bac6c --- /dev/null +++ b/src/main/java/xyz/soukup/ecoCraftCore/utilities/DaoRegistry.java @@ -0,0 +1,37 @@ +package xyz.soukup.ecoCraftCore.utilities; + +import com.j256.ormlite.dao.Dao; +import xyz.soukup.ecoCraftCore.objects.Shop; +import xyz.soukup.ecoCraftCore.objects.Transaction; +import xyz.soukup.ecoCraftCore.objects.VirtualChest; + +public class DaoRegistry { + private static Dao shopDao; + private static Dao transactionDao; + private static Dao virtualChestDao; + + + public static Dao getShopDao() { + return shopDao; + } + + public static void setShopDao(Dao shopDao) { + DaoRegistry.shopDao = shopDao; + } + + public static Dao getTransactionDao() { + return transactionDao; + } + + public static void setTransactionDao(Dao transactionDao) { + DaoRegistry.transactionDao = transactionDao; + } + + public static Dao getVirtualChestDao() { + return virtualChestDao; + } + + public static void setVirtualChestDao(Dao virtualChestDao) { + DaoRegistry.virtualChestDao = virtualChestDao; + } +} diff --git a/src/main/java/xyz/soukup/ecoCraftCore/utilities/JSONConverter.java b/src/main/java/xyz/soukup/ecoCraftCore/utilities/JSONConverter.java new file mode 100644 index 0000000..df2fad2 --- /dev/null +++ b/src/main/java/xyz/soukup/ecoCraftCore/utilities/JSONConverter.java @@ -0,0 +1,36 @@ +package xyz.soukup.ecoCraftCore.utilities; + +import com.google.common.reflect.TypeToken; +import com.google.gson.Gson; +import org.bukkit.inventory.ItemStack; + +import java.lang.reflect.Type; +import java.util.Map; + + +public class JSONConverter { + + public static String itemStackToJson(ItemStack item) { + try { + Map map = item.serialize(); + return new Gson().toJson(map); // or use your JSON library of choice + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public static ItemStack itemStackFromJson(String json) { + try { + Type type = new TypeToken>() {}.getType(); + Map map = new Gson().fromJson(json, type); + return ItemStack.deserialize(map); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + + +} diff --git a/src/main/java/xyz/soukup/ecoCraftCore/utilities/PDC.java b/src/main/java/xyz/soukup/ecoCraftCore/utilities/PDC.java new file mode 100644 index 0000000..564b91d --- /dev/null +++ b/src/main/java/xyz/soukup/ecoCraftCore/utilities/PDC.java @@ -0,0 +1,45 @@ +package xyz.soukup.ecoCraftCore.utilities; + +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; + +@SuppressWarnings({"unchecked", "rawtypes"}) +public class PDC { + public static ItemStack setItemStackString(ItemStack itemStack, String key, String value){ + return setItemStack(itemStack, key, value, PersistentDataType.STRING); + } + public static ItemStack setItemStackBoolean(ItemStack itemStack, String key, Boolean value){ + return setItemStack(itemStack, key, value, PersistentDataType.BOOLEAN); + } + + public static Boolean retrieveBooleanFromItemStack(ItemStack itemStack, String key){ + return (Boolean) retrieveFromItemStack(itemStack, key, PersistentDataType.BOOLEAN); + } + + public static String retrieveStringFromItemStack(ItemStack itemStack, String key){ + return (String) retrieveFromItemStack(itemStack, key, PersistentDataType.STRING); + } + + private static Object retrieveFromItemStack(ItemStack itemStack, String key, PersistentDataType persistentDataType){ + ItemMeta itemMeta = itemStack.getItemMeta(); + PersistentDataContainer persistentDataContainer = itemMeta.getPersistentDataContainer(); + + NamespacedKey namespacedKey = new NamespacedKey("ecc", key); + + return persistentDataContainer.get(namespacedKey, persistentDataType); + } + + private static ItemStack setItemStack(ItemStack itemStack, String key, Object value, PersistentDataType persistentDataType){ + ItemMeta itemMeta = itemStack.getItemMeta(); + PersistentDataContainer persistentDataContainer = itemMeta.getPersistentDataContainer(); + + NamespacedKey namespacedKey = new NamespacedKey("ecc", key); + persistentDataContainer.set(namespacedKey, persistentDataType, value); + itemStack.setItemMeta(itemMeta); + + return itemStack; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..5868d64 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,4 @@ +name: EcoCraftCore +version: '1.0-SNAPSHOT' +main: xyz.soukup.ecoCraftCore.EcoCraftCore +api-version: '1.21'