Update VoidBank: It actually works now

This commit is contained in:
Julius 2019-08-25 16:00:58 +02:00
parent 1d0204a648
commit d654ae5e88
9 changed files with 345 additions and 0 deletions

View file

@ -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<out String>): 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
}
}

View file

@ -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<out String>): 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
}
}

View file

@ -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<out String>): Boolean {
if (!sender.hasPermission("bank.reload")) return false
bank.send("VoidPlugin", MessageType.GET_CONFIG)
bank.send("VoidPlugin", MessageType.POST_CONFIG, moshi.adapter<BankConfig>().toJson(VoidBank.config))
Collector.reloadConfig()
bank.iconMenu()
return true
}
}

View file

@ -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<out String>
): List<String> {
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<out String>): 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
}
}
}

View file

@ -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<out String>): 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
}
}

View file

@ -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<ProjectEBorrow>()
.fromJson(Collector::class.java.getResourceAsStream("/default.json").bufferedReader().readText())
if (borrow != null) {
prices.putAll(borrow.values.asMaterialList)
}
// let the pain commence
val simple = mutableListOf<SimpleRecipe>()
val recipes = Bukkit.recipeIterator().asSequence().filter { it.result.type !in prices.keys }.toMutableList()
val shapeless = recipes.filterIsInstance<ShapelessRecipe>()
val shaped = recipes.filterIsInstance<ShapedRecipe>()
val furnace = recipes.filterIsInstance<FurnaceRecipe>()
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<Material, Int>()
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<Material, Int>) {
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<Material, Int>, val output: Material, val count: Int)
class meme(val recipes: List<SimpleRecipe>)
}

View file

@ -0,0 +1,9 @@
package nl.voidcorp.bankplugin.costcalc
import org.bukkit.Material
data class ProjectEBorrow(val comment: String, val groups: Map<String, Any>, val values: Values)
data class Values(val before: Map<String, Int>) {
val asMaterialList: Map<Material, Int>
get() = before.filterKeys { Material.matchMaterial(it) != null }.map { Material.matchMaterial(it.key)!! to it.value }.toMap()
}

View file

@ -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<Int>) : IntEntity(id) {
companion object : IntEntityClass<Bank>(Banks)
var player by Banks.player
var money by Banks.money
}

View file

@ -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)
}