Update VoidUtils: Fix weather skip to only run if thunder

This commit is contained in:
Julius 2019-08-25 16:00:36 +02:00
parent 99749ee92c
commit 1d0204a648
8 changed files with 96 additions and 243 deletions

View file

@ -15,10 +15,28 @@ bukkit {
depend = ['VoidPlugin'] depend = ['VoidPlugin']
commands { commands {
buymenu { buymenu {
description = "Help me" description = "Buy or sell stuff!"
} }
woodstrip{ woodstrip {
description = "Use this to transform stripped wood back into it's normal form" description = "Use this to transform stripped wood back into it's normal form"
} }
checkcash {
description = "Check how many ECs you have in you account"
}
transfer {
description = "Send cash to another player"
}
reload {
description = "Reloads the fileconfig"
permission = "bank.reload"
permissionMessage = "You are not allowed to reload the config..."
}
}
permissions {
'bank.reload' {
setDefault("OP")
description = "Allows reloading the config"
}
} }
} }

View file

@ -1,19 +0,0 @@
package nl.voidcorp.bankplugin
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

@ -1,118 +0,0 @@
package nl.voidcorp.bankplugin
import nl.voidcorp.mainplugin.adapter
import nl.voidcorp.mainplugin.messaging.MessageType
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 {
val config = VoidBank.config.bankMap
if (config.isNotEmpty() && !VoidBank.config.rerun) {
prices.putAll(config.map { Material.getMaterial(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 { it.key.name }.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

@ -35,8 +35,7 @@ class IconMenu(
} }
fun close(p: Player): IconMenu { fun close(p: Player): IconMenu {
if (p.openInventory.title == name) viewing.remove(p.uniqueId)
p.closeInventory()
return this return this
} }
@ -44,10 +43,17 @@ class IconMenu(
fun onInventoryClick(event: InventoryClickEvent) { fun onInventoryClick(event: InventoryClickEvent) {
val p = event.whoClicked as Player val p = event.whoClicked as Player
if (p.uniqueId in viewing) { if (p.uniqueId in viewing) {
event.isCancelled = true if (event.clickedInventory == event.whoClicked.inventory) {
val row = getRowFromSlot(event.slot) if (event.isShiftClick) {
if (!click(p, this, row, event.slot % 9, event.currentItem, event)) event.isCancelled = true
close(p) }
} else {
event.isCancelled = true
val row = getRowFromSlot(event.slot)
if (!click(p, this, row, event.slot % 9, event.currentItem, event))
close(p)
}
} }
} }

View file

@ -1,9 +0,0 @@
package nl.voidcorp.bankplugin
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

@ -1,5 +1,9 @@
package nl.voidcorp.bankplugin package nl.voidcorp.bankplugin
import nl.voidcorp.bankplugin.commands.*
import nl.voidcorp.bankplugin.costcalc.Collector
import nl.voidcorp.bankplugin.models.Bank
import nl.voidcorp.bankplugin.models.Banks
import nl.voidcorp.mainplugin.CommandHandler import nl.voidcorp.mainplugin.CommandHandler
import nl.voidcorp.mainplugin.VoidPluginBase import nl.voidcorp.mainplugin.VoidPluginBase
import nl.voidcorp.mainplugin.adapter import nl.voidcorp.mainplugin.adapter
@ -12,9 +16,8 @@ import org.bukkit.entity.Player
import org.bukkit.event.inventory.ClickType import org.bukkit.event.inventory.ClickType
import org.bukkit.event.inventory.InventoryClickEvent import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.inventory.ItemStack import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.ShapedRecipe import org.jetbrains.exposed.sql.SchemaUtils
import org.bukkit.inventory.ShapelessRecipe import org.jetbrains.exposed.sql.transactions.transaction
import java.util.*
import java.util.logging.Logger import java.util.logging.Logger
import kotlin.math.min import kotlin.math.min
@ -27,23 +30,23 @@ class VoidBank(override val comment: String = "Sell all the diamonds!") : VoidPl
send("VoidPlugin", MessageType.POST_CONFIG, moshi.adapter<BankConfig>().toJson(VoidBank.config)) send("VoidPlugin", MessageType.POST_CONFIG, moshi.adapter<BankConfig>().toJson(VoidBank.config))
Collector.rangeTo(0) Collector.rangeTo(0)
send("VoidPlugin", MessageType.POST_CONFIG, moshi.adapter<BankConfig>().toJson(VoidBank.config)) send("VoidPlugin", MessageType.POST_CONFIG, moshi.adapter<BankConfig>().toJson(VoidBank.config))
transaction {
SchemaUtils.createMissingTablesAndColumns(Banks)
}
val buy = BuyCommand(this) val buy = BuyCommand(this)
CommandHandler(this).registerCommand("woodstrip", WoodStripCommand()) CommandHandler(this).registerCommand("woodstrip", WoodStripCommand())
.registerCommand("buymenu", buy) .registerCommand("buymenu", buy)
.registerCommand("checkcash", CheckCashCommand())
.registerCommand("transfer", TransferCashCommand())
.registerCommand("reload", ReloadCommand(this))
iconMenu() iconMenu()
for (iconMenu in iconMenus) { for (iconMenu in iconMenus) {
server.pluginManager.registerEvents(iconMenu, this) server.pluginManager.registerEvents(iconMenu, this)
} }
val recipes = server.recipeIterator().asSequence().toList()
recipes.forEach {
if (it is ShapedRecipe) {
}
if (it is ShapelessRecipe) {
it.ingredientList
}
}
} }
override fun recieve(message: Message) { override fun recieve(message: Message) {
@ -56,71 +59,69 @@ class VoidBank(override val comment: String = "Sell all the diamonds!") : VoidPl
} }
val map = mutableMapOf<UUID, Int>()
val iconfun = val iconfun =
{ clicker: Player, menu: IconMenu, rowNum: Int, slot: Int, item: ItemStack?, event: InventoryClickEvent -> { clicker: Player, menu: IconMenu, rowNum: Int, slot: Int, item: ItemStack?, event: InventoryClickEvent ->
if (event.clickedInventory == event.whoClicked.inventory) { if (item != null) {
if (event.isShiftClick) {
event.isCancelled = true
}
} else if (item != null) {
if (rowNum == 5) { if (rowNum == 5) {
if (slot != 4) { if (slot == 0 || slot == 8) {
val s = item.itemMeta.lore?.get(0)?.toInt() ?: 0 val s = item.itemMeta.lore?.get(0)?.toInt() ?: 0
val next = iconMenus[s] val next = iconMenus[s]
if (next != menu) { if (next != menu) {
Bukkit.getScheduler().runTask(this) { _ -> Bukkit.getScheduler().runTask(this) { _ ->
menu.close(clicker)
next.open(clicker) next.open(clicker)
menu.close(clicker)
} }
} }
} }
} else { } else {
val price = item.itemMeta.lore?.get(0)?.toInt() ?: 0 transaction {
val cash = map.getOrDefault(clicker.uniqueId, 0) val bank = Bank.find { Banks.player eq clicker.uniqueId }.firstOrNull() ?: Bank.new {
when (event.click) { player = clicker.uniqueId
ClickType.LEFT -> {
if (cash >= price) {
clicker.inventory.addItem(ItemStack(item.type))
map[clicker.uniqueId] = cash - price
}
} }
ClickType.RIGHT -> { val price = item.itemMeta.lore?.get(0)?.toInt() ?: 0
val cash = bank.money
when (event.click) {
ClickType.LEFT -> {
if (cash >= price) {
clicker.inventory.addItem(ItemStack(item.type))
bank.money = cash - price
}
}
ClickType.RIGHT -> {
if (clicker.inventory.contains(item.type)) { if (clicker.inventory.contains(item.type)) {
val cl = clicker.inventory.removeItem(ItemStack(item.type)) val cl = clicker.inventory.removeItem(ItemStack(item.type))
if (cl.isEmpty()) if (cl.isEmpty())
map[clicker.uniqueId] = cash + price bank.money = cash + price
}
} }
ClickType.SHIFT_LEFT -> {
// max (0/1536, 64
val amt = min((cash / price).toInt(), item.type.maxStackSize)
clicker.inventory.addItem(ItemStack(item.type, amt))
bank.money = cash - amt * price
} }
ClickType.SHIFT_LEFT -> { ClickType.SHIFT_RIGHT -> {
// max (0/1536, 64 if (clicker.inventory.contains(item.type)) {
val amt = min(cash / price, item.type.maxStackSize) val amt =
clicker.inventory.addItem(ItemStack(item.type, amt)) min(clicker.inventory.contents.filterNotNull().filter { it.type == item.type }.map { it.amount }.sum(),
map[clicker.uniqueId] = cash - amt * price item.type.maxStackSize
)
} val rm = ItemStack(item.type, amt)
ClickType.SHIFT_RIGHT -> { clicker.inventory.removeItem(rm)
if (clicker.inventory.contains(item.type)) { bank.money = cash + price * amt
val amt = }
min(clicker.inventory.contents.filterNotNull().filter { it.type == item.type }.map { it.amount }.sum(), }
item.type.maxStackSize else -> {
) Unit
val rm = ItemStack(item.type, amt)
clicker.inventory.removeItem(rm)
map[clicker.uniqueId] = cash + price * amt
} }
} }
else -> { clicker.sendMessage("You have ${bank.money} ECs left!")
clicker.sendMessage("hmm?")
}
} }
clicker.sendMessage("You have ${map[clicker.uniqueId] ?: 0} EC's left!")
} }
} }
true true
@ -147,6 +148,9 @@ class VoidBank(override val comment: String = "Sell all the diamonds!") : VoidPl
} }
val prev = key - 1 val prev = key - 1
val next = key + 1 val next = key + 1
for (z in 0..8) {
iconMenus[key].addButton(5, z, ItemStack(Material.BARRIER), "Bad, don't click here!")
}
if (prev >= 0) if (prev >= 0)
iconMenus[key].addButton( iconMenus[key].addButton(
5, 5,

View file

@ -1,31 +0,0 @@
package nl.voidcorp.bankplugin
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

@ -31,7 +31,9 @@ class Events(private val utils: VoidUtils) : Listener {
val req = (utils.conf.neededPlayers * evt.player.server.onlinePlayers.size).roundToInt() val req = (utils.conf.neededPlayers * evt.player.server.onlinePlayers.size).roundToInt()
if (bedList.size >= req) { if (bedList.size >= req) {
utils.server.worlds.forEach { it.time = 0 } utils.server.worlds.forEach { it.time = 0 }
evt.player.world.weatherDuration = 1 if (evt.player.world.isThundering) {
evt.player.world.weatherDuration = 1
}
utils.server.broadcastMessage("Enough players were sleeping, skipping the night!") utils.server.broadcastMessage("Enough players were sleeping, skipping the night!")
} else } else
utils.server.broadcastMessage("There are now ${ChatColor.GOLD}${bedList.size}/$req${ChatColor.RESET} players sleeping to skip the night!") utils.server.broadcastMessage("There are now ${ChatColor.GOLD}${bedList.size}/$req${ChatColor.RESET} players sleeping to skip the night!")