From d654ae5e88b2d86ba5c70616e98434e426e1b66a Mon Sep 17 00:00:00 2001 From: Julius de Jeu Date: Sun, 25 Aug 2019 16:00:58 +0200 Subject: [PATCH] Update VoidBank: It actually works now --- .../bankplugin/commands/BuyCommand.kt | 20 +++ .../bankplugin/commands/CheckCashCommand.kt | 23 +++ .../bankplugin/commands/ReloadCommand.kt | 23 +++ .../commands/TransferCashCommand.kt | 72 +++++++++ .../bankplugin/commands/WoodStripCommand.kt | 31 ++++ .../voidcorp/bankplugin/costcalc/Collector.kt | 146 ++++++++++++++++++ .../bankplugin/costcalc/ProjectEBorrow.kt | 9 ++ .../nl/voidcorp/bankplugin/models/Bank.kt | 12 ++ .../nl/voidcorp/bankplugin/models/Banks.kt | 9 ++ 9 files changed, 345 insertions(+) create mode 100644 VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/BuyCommand.kt create mode 100644 VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/CheckCashCommand.kt create mode 100644 VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/ReloadCommand.kt create mode 100644 VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/TransferCashCommand.kt create mode 100644 VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/WoodStripCommand.kt create mode 100644 VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/costcalc/Collector.kt create mode 100644 VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/costcalc/ProjectEBorrow.kt create mode 100644 VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/models/Bank.kt create mode 100644 VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/models/Banks.kt diff --git a/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/BuyCommand.kt b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/BuyCommand.kt new file mode 100644 index 0000000..74ef603 --- /dev/null +++ b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/BuyCommand.kt @@ -0,0 +1,20 @@ +package nl.voidcorp.bankplugin.commands + +import nl.voidcorp.bankplugin.VoidBank +import nl.voidcorp.mainplugin.commands.VoidCommand +import org.bukkit.command.Command +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player + +class BuyCommand(private val plugin: VoidBank) : VoidCommand() { + private val title = "Exchange" + + override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { + if (sender is Player) { + val page = args.toList().getOrNull(0)?.toInt() ?: 0 + plugin.iconMenus[page.coerceIn(0 until plugin.iconMenus.size)].open(sender) + } + return true + } + +} \ No newline at end of file diff --git a/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/CheckCashCommand.kt b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/CheckCashCommand.kt new file mode 100644 index 0000000..d2fd4bc --- /dev/null +++ b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/CheckCashCommand.kt @@ -0,0 +1,23 @@ +package nl.voidcorp.bankplugin.commands + +import nl.voidcorp.bankplugin.models.Bank +import nl.voidcorp.bankplugin.models.Banks +import nl.voidcorp.mainplugin.commands.VoidCommand +import org.bukkit.command.Command +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player +import org.jetbrains.exposed.sql.transactions.transaction + +class CheckCashCommand : VoidCommand() { + override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { + if (sender is Player) { + transaction { + val bank = Bank.find { Banks.player eq sender.uniqueId }.firstOrNull() ?: Bank.new { + player = sender.uniqueId + } + sender.sendMessage("You have ${bank.money} EC in you account") + } + } + return true + } +} \ No newline at end of file diff --git a/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/ReloadCommand.kt b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/ReloadCommand.kt new file mode 100644 index 0000000..bb453bf --- /dev/null +++ b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/ReloadCommand.kt @@ -0,0 +1,23 @@ +package nl.voidcorp.bankplugin.commands + +import nl.voidcorp.bankplugin.BankConfig +import nl.voidcorp.bankplugin.VoidBank +import nl.voidcorp.bankplugin.costcalc.Collector +import nl.voidcorp.mainplugin.adapter +import nl.voidcorp.mainplugin.commands.VoidCommand +import nl.voidcorp.mainplugin.messaging.MessageType +import nl.voidcorp.mainplugin.moshi +import org.bukkit.command.Command +import org.bukkit.command.CommandSender + +class ReloadCommand(private val bank: VoidBank) : VoidCommand() { + override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { + if (!sender.hasPermission("bank.reload")) return false + + bank.send("VoidPlugin", MessageType.GET_CONFIG) + bank.send("VoidPlugin", MessageType.POST_CONFIG, moshi.adapter().toJson(VoidBank.config)) + Collector.reloadConfig() + bank.iconMenu() + return true + } +} \ No newline at end of file diff --git a/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/TransferCashCommand.kt b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/TransferCashCommand.kt new file mode 100644 index 0000000..6861080 --- /dev/null +++ b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/TransferCashCommand.kt @@ -0,0 +1,72 @@ +package nl.voidcorp.bankplugin.commands + +import nl.voidcorp.bankplugin.models.Bank +import nl.voidcorp.bankplugin.models.Banks +import nl.voidcorp.mainplugin.commands.VoidCommand +import org.bukkit.Bukkit +import org.bukkit.command.Command +import org.bukkit.command.CommandSender +import org.bukkit.command.ConsoleCommandSender +import org.bukkit.entity.Player +import org.jetbrains.exposed.sql.transactions.transaction +import kotlin.math.absoluteValue + +class TransferCashCommand : VoidCommand() { + override fun onTabComplete( + sender: CommandSender, + command: Command, + alias: String, + args: Array + ): List { + return if (args.size <= 1) + sender.server.onlinePlayers.filter { it.name.startsWith(args.getOrNull(0) ?: "") }.map { it.name } + else emptyList() + } + + + override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { + return if (args.size == 2) { + val to = Bukkit.getPlayer(args[0]) + val amt = args[1].toLongOrNull() + if (amt == null) { + sender.sendMessage("The amount needs to be a number, not '${args[1]}'...") + true + } else + if (to != null && amt > 0) { + if (sender is Player) { + transaction { + val from = Bank.find { Banks.player eq sender.uniqueId }.firstOrNull() ?: Bank.new { + player = sender.uniqueId + } + val bank = Bank.find { Banks.player eq to.uniqueId }.firstOrNull() ?: Bank.new { + player = to.uniqueId + } + if (from.money >= amt) { + from.money -= amt + bank.money += amt + sender.sendMessage("You sent $amt to ${to.name}") + to.sendMessage("You received $amt from ${sender.name}!") + } else { + val missing = (from.money - amt).absoluteValue + sender.sendMessage("You are missing $missing ECs to send them to ${to.name}") + } + + } + } else if (sender is ConsoleCommandSender) { + transaction { + val bank = Bank.find { Banks.player eq to.uniqueId }.firstOrNull() ?: Bank.new { + player = to.uniqueId + } + bank.money += amt + to.sendMessage("Hmm... you seem to have magically received $amt from the gods?") + } + } + true + } else { + false + } + } else { + false + } + } +} \ No newline at end of file diff --git a/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/WoodStripCommand.kt b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/WoodStripCommand.kt new file mode 100644 index 0000000..9b5b676 --- /dev/null +++ b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/commands/WoodStripCommand.kt @@ -0,0 +1,31 @@ +package nl.voidcorp.bankplugin.commands + +import nl.voidcorp.mainplugin.commands.VoidCommand +import org.bukkit.Bukkit +import org.bukkit.Material +import org.bukkit.command.Command +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player +import org.bukkit.inventory.ItemStack +import org.bukkit.inventory.MerchantRecipe + +class WoodStripCommand : VoidCommand() { + override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { + if (sender is Player) { + val merchant = Bukkit.createMerchant("Wood Stripper") + + val cursed = "STRIPPED_(.*)_LOG".toRegex() + val logs = Material.values().toList().filter { it.name.matches(cursed) } + .map { cursed.matchEntire(it.name)!!.groupValues[1] } + val recipes = + logs.map { + MerchantRecipe(ItemStack(Material.getMaterial("${it}_LOG")!!), Int.MAX_VALUE).apply { + addIngredient(ItemStack(Material.getMaterial("STRIPPED_${it}_LOG")!!)) + } + }.toMutableList() + merchant.recipes = recipes + sender.openMerchant(merchant, true) + } + return true + } +} \ No newline at end of file diff --git a/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/costcalc/Collector.kt b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/costcalc/Collector.kt new file mode 100644 index 0000000..c69e302 --- /dev/null +++ b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/costcalc/Collector.kt @@ -0,0 +1,146 @@ +package nl.voidcorp.bankplugin.costcalc + +import nl.voidcorp.bankplugin.VoidBank +import nl.voidcorp.mainplugin.adapter +import nl.voidcorp.mainplugin.moshi +import org.bukkit.Bukkit +import org.bukkit.Material +import org.bukkit.Tag +import org.bukkit.inventory.FurnaceRecipe +import org.bukkit.inventory.ShapedRecipe +import org.bukkit.inventory.ShapelessRecipe +import kotlin.math.max + +object Collector { + private val prices = mutableMapOf(Material.IRON_INGOT to 256) + + init { + reloadConfig() + } + + fun reloadConfig() { + val config = VoidBank.config.bankMap + if (config.isNotEmpty() && !VoidBank.config.rerun) { + prices.clear() + prices.putAll(config.map { Material.matchMaterial(it.key)!! to it.value }.toMap()) + } + if (VoidBank.config.rerun) { + prices.putAll(Tag.LOGS.values.map { it to 32 }) + prices.putAll(Tag.PLANKS.values.map { it to 8 }) + prices.putAll(Tag.SAPLINGS.values.map { it to 32 }) + prices.putAll(Tag.LEAVES.values.map { it to 1 }) + prices.putAll(Tag.WOOL.values.map { it to 48 }) + this += Material.DIAMOND to 8192 + this += Material.STICK to 4 + this += Material.REDSTONE to 64 + this += Material.GLOWSTONE_DUST to 384 + this += Material.GOLD_INGOT to prices[Material.IRON_INGOT]!! * 8 + val borrow = moshi.adapter() + .fromJson(Collector::class.java.getResourceAsStream("/default.json").bufferedReader().readText()) + if (borrow != null) { + prices.putAll(borrow.values.asMaterialList) + } + + // let the pain commence + val simple = mutableListOf() + val recipes = Bukkit.recipeIterator().asSequence().filter { it.result.type !in prices.keys }.toMutableList() + val shapeless = recipes.filterIsInstance() + val shaped = recipes.filterIsInstance() + val furnace = recipes.filterIsInstance() + for (r in shapeless) { + + simple += SimpleRecipe( + r.ingredientList.groupingBy { it.type }.eachCount(), + r.result.type, + r.result.amount + ) + } + for (r in shaped) { + val ingredients = r.ingredientMap + val shape = r.shape.joinToString("").toList() + val m = mutableMapOf() + for (c in shape) { + val i = ingredients[c] + if (i != null) { + val curr = m.getOrDefault(i.type, 0) + 1 + m[i.type] = curr + } + } + simple += SimpleRecipe(m, r.result.type, r.result.amount) + } + for (r in furnace) { + val sticks = r.cookingTime / 100 + val inp = r.input + val outp = r.result + val m = mutableMapOf(inp.type to inp.amount, Material.STICK to sticks) + simple += SimpleRecipe(m, outp.type, outp.amount) + } + + simple += SimpleRecipe( + mapOf(Material.BUCKET to 1), + Material.MILK_BUCKET, + 1 + ) + simple += SimpleRecipe( + mapOf(Material.GOLD_BLOCK to 8, Material.APPLE to 1), + Material.ENCHANTED_GOLDEN_APPLE, + 1 + ) + simple += SimpleRecipe( + mapOf(Material.IRON_INGOT to 4), + Material.CREEPER_HEAD, + 1 + ) + simple += SimpleRecipe( + mapOf(Material.WITHER_SKELETON_SKULL to 4), + Material.CREEPER_HEAD, + 1 + ) + simple += SimpleRecipe( + mapOf(Material.COBBLESTONE to 16384 / 12), + Material.MELON_SLICE, + 9 + ) + simple += SimpleRecipe( + mapOf(Material.PUMPKIN to 1), + Material.CARVED_PUMPKIN, + 1 + ) + simple += SimpleRecipe( + mapOf(Material.COBBLESTONE to 2), + Material.BAMBOO, + 1 + ) + + repeat(5) { + simple.removeIf { (it.output in prices.keys) or (it.input.keys.any { it.name.contains("ORE") }) } + VoidBank.logger.info("Count: ${simple.size}, step $it") + val rs = simple.filter { it.input.keys.all { it in prices.keys } } + for (r in rs) { + val cost = r.input.map { it.value * prices.getOrDefault(it.key, 0) }.sum() / r.count + prices[r.output] = max(cost, prices.getOrDefault(r.output, 0)) + } + } + simple.removeIf { (it.output in prices.keys) or (it.input.keys.any { it.name.contains("ORE") }) } + VoidBank.logger.info("Count: ${simple.size}, step end") +// VoidBank.logger.info(moshi.adapter(meme::class.java).toJson(meme(simple))) + prices.remove(Material.ENCHANTED_BOOK) + prices.remove(Material.FILLED_MAP) + prices.remove(Material.WRITTEN_BOOK) + VoidBank.config.bankMap = prices.mapKeys { "minecraft:${it.key.name.toLowerCase()}" }.toMutableMap() + VoidBank.config.rerun = false + + } + } + + operator fun plusAssign(p: Pair) { + prices += p + } + + operator fun get(m: Material) = prices.getOrDefault(m, 0) + + operator fun rangeTo(max: Int) = prices.filterValues { it <= max } + + data class SimpleRecipe(val input: Map, val output: Material, val count: Int) + class meme(val recipes: List) +} \ No newline at end of file diff --git a/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/costcalc/ProjectEBorrow.kt b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/costcalc/ProjectEBorrow.kt new file mode 100644 index 0000000..32c3384 --- /dev/null +++ b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/costcalc/ProjectEBorrow.kt @@ -0,0 +1,9 @@ +package nl.voidcorp.bankplugin.costcalc + +import org.bukkit.Material + +data class ProjectEBorrow(val comment: String, val groups: Map, val values: Values) +data class Values(val before: Map) { + val asMaterialList: Map + get() = before.filterKeys { Material.matchMaterial(it) != null }.map { Material.matchMaterial(it.key)!! to it.value }.toMap() +} \ No newline at end of file diff --git a/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/models/Bank.kt b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/models/Bank.kt new file mode 100644 index 0000000..c5afebd --- /dev/null +++ b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/models/Bank.kt @@ -0,0 +1,12 @@ +package nl.voidcorp.bankplugin.models + +import org.jetbrains.exposed.dao.EntityID +import org.jetbrains.exposed.dao.IntEntity +import org.jetbrains.exposed.dao.IntEntityClass + +class Bank(id: EntityID) : IntEntity(id) { + companion object : IntEntityClass(Banks) + + var player by Banks.player + var money by Banks.money +} \ No newline at end of file diff --git a/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/models/Banks.kt b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/models/Banks.kt new file mode 100644 index 0000000..90bd34b --- /dev/null +++ b/VoidBank/src/main/kotlin/nl/voidcorp/bankplugin/models/Banks.kt @@ -0,0 +1,9 @@ +package nl.voidcorp.bankplugin.models + +import org.jetbrains.exposed.dao.IntIdTable + +object Banks : IntIdTable() { + val player = uuid("player").uniqueIndex() + val money = long("money").default(0) + +} \ No newline at end of file