Merge pull request #5

Converters
This commit is contained in:
Will FP
2022-03-22 11:02:58 +00:00
committed by GitHub
11 changed files with 515 additions and 0 deletions

View File

@@ -3,4 +3,5 @@ version rootProject.version
dependencies {
compileOnly 'org.spigotmc:spigot-api:1.17.1-R0.1-SNAPSHOT'
compileOnly fileTree(dir: '../../lib', include: ['*.jar'])
}

View File

@@ -3,9 +3,12 @@ package com.willfp.ecocrates
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.command.impl.PluginCommand
import com.willfp.eco.core.display.DisplayModule
import com.willfp.eco.core.integrations.IntegrationLoader
import com.willfp.ecocrates.commands.CommandEcoCrates
import com.willfp.ecocrates.config.CratesYml
import com.willfp.ecocrates.config.RewardsYml
import com.willfp.ecocrates.converters.Converters
import com.willfp.ecocrates.converters.cratereloaded.CrateReloadedConverter
import com.willfp.ecocrates.crate.CrateKeyListener
import com.willfp.ecocrates.crate.placed.CrateDisplay
import com.willfp.ecocrates.crate.placed.PlacedCrates
@@ -46,6 +49,13 @@ class EcoCratesPlugin : EcoPlugin() {
return KeyDisplay(this)
}
override fun loadIntegrationLoaders(): MutableList<IntegrationLoader> {
return mutableListOf(
IntegrationLoader("CrateReloaded") {Converters.registerConverter("CrateReloaded",
CrateReloadedConverter())}
)
}
companion object {
/**
* Instance of the plugin.

View File

@@ -0,0 +1,44 @@
package com.willfp.ecocrates.commands
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.command.impl.Subcommand
import com.willfp.ecocrates.converters.Converters
import org.bukkit.command.CommandSender
import org.bukkit.util.StringUtil
class CommandConvert(plugin: EcoPlugin) : Subcommand(
plugin,
"convert",
"ecocrates.command.convert",
false
) {
override fun onExecute(sender: CommandSender, args: List<String>) {
if (args.isEmpty()) {
sender.sendMessage(plugin.langYml.getMessage("must-specify-converter"))
return
}
val converter = Converters.getById(args[0])
if (converter == null) {
sender.sendMessage(plugin.langYml.getMessage("invalid-converter"))
return
}
converter.convert()
sender.sendMessage(plugin.langYml.getMessage("converted"))
}
override fun tabComplete(sender: CommandSender, args: MutableList<String>): MutableList<String> {
val values = mutableListOf<String>()
if (args.size == 1) {
StringUtil.copyPartialMatches(
args[0],
Converters.BY_ID.keys,
values
)
}
return values
}
}

View File

@@ -19,6 +19,7 @@ class CommandEcoCrates(plugin: EcoPlugin) : PluginCommand(
.addSubcommand(CommandSet(plugin))
.addSubcommand(CommandForceOpen(plugin))
.addSubcommand(CommandResetWins(plugin))
.addSubcommand(CommandConvert(plugin))
}
override fun onExecute(sender: CommandSender, args: List<String>) {

View File

@@ -0,0 +1,5 @@
package com.willfp.ecocrates.converters
interface Converter {
fun convert()
}

View File

@@ -0,0 +1,28 @@
package com.willfp.ecocrates.converters
object Converters {
@JvmStatic
val BY_ID = mutableMapOf<String, Converter>()
@JvmStatic
fun values(): List<Converter> {
return BY_ID.values.toList()
}
@JvmStatic
fun registerConverter(id: String, converter: Converter) {
BY_ID[id] = converter
}
@JvmStatic
fun unregisterConverter(id: String) {
BY_ID.remove(id)
}
@JvmStatic
fun getById(id: String): Converter? {
return BY_ID.filter { it.key.equals(id, true) }.values.firstOrNull()
}
}

View File

@@ -0,0 +1,185 @@
package com.willfp.ecocrates.converters.cratereloaded
import com.hazebyte.crate.api.CrateAPI
import com.hazebyte.crate.api.crate.reward.Reward
import com.willfp.eco.core.Eco
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.ecocrates.EcoCratesPlugin
import com.willfp.ecocrates.converters.Converter
import com.willfp.ecocrates.converters.util.ConverterUtils
import com.willfp.ecocrates.crate.Crates
import com.willfp.ecocrates.crate.placed.PlacedCrate
import com.willfp.ecocrates.crate.placed.PlacedCrates
import com.willfp.ecocrates.crate.roll.Rolls
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.configuration.file.YamlConfiguration
@Suppress("UNCHECKED_CAST")
class CrateReloadedConverter: Converter {
override fun convert() {
val crateConfig = YamlConfiguration.loadConfiguration(CrateAPI.getInstance()
.dataFolder.resolve("crates").resolve("crate.yml"))
val crates = crateConfig.getKeys(false).map { ACrate(it, crateConfig.getConfigurationSection(it)!!) }
.map { convertCrate(it) }
val preCrates = EcoCratesPlugin.instance.cratesYml.bukkitHandle!!.get("crates")
as MutableList<YamlConfiguration>
preCrates.addAll(crates)
EcoCratesPlugin.instance.cratesYml.set("crates", preCrates)
EcoCratesPlugin.instance.cratesYml.save()
EcoCratesPlugin.instance.rewardsYml.save()
EcoCratesPlugin.instance.reload()
CrateAPI.getBlockCrateRegistrar().locations.forEach {
val id = CrateAPI.getBlockCrateRegistrar().getFirstCrate(it).uuid.split(":")[0]
val crate = Crates.getByID(id)
if (crate != null) {
CrateAPI.getBlockCrateRegistrar().getCrates(it)
.forEach { i1 -> CrateAPI.getBlockCrateRegistrar().removeCrate(it, i1)}
PlacedCrates.setAsCrate(it, crate)
}
}
}
class ACrate(val id: String, val config: ConfigurationSection)
private fun convertCrate(aCrate: ACrate): YamlConfiguration {
val id = aCrate.id
val result = ConverterUtils.createDefaultCrate()
result.set("id", id)
result.set("name", aCrate.config.getString("display-name"))
val roll = Rolls.getByID(aCrate.config.getString("animation")!!.lowercase())?: Rolls.CSGO
result.set("roll", roll.id)
val frame = ConverterUtils.emptyBukkit().apply {
this.set("tick", 0)
this.set("lines", aCrate.config.getStringList("holographic"))
}
result.set("placed.hologram.frames", mutableListOf(frame))
val handle = CrateAPI.getCrateRegistrar().getCrate(id)
result.set("pay-to-open.enabled", handle.isBuyable)
result.set("pay-to-open.price", handle.cost)
var row = 2
var col = 2
var counter = 1
val rewards = mutableListOf<YamlConfiguration>()
handle.rewards.forEach {
val salt = id+"_"+counter
rewards.add(convertReward(it, salt, row, col))
col++
if (col >= 8) {
col = 2
row++
}
counter++
}
result.set("rewards", rewards.map { it.getString("id") })
result.set("key", ConverterUtils.emptyBukkit().apply {
this.set("item", "tripwire_hook unbreaking:1 hide_enchants name:\"${result.getString("name")} Key\"")
this.set("lore",
mutableListOf(
"&fUse this key to open",
"&fthe ${result.getString("name")}"
)
)
})
result.set("keygui", ConverterUtils.emptyBukkit().apply {
this.set("enabled", true)
this.set("item", "tripwire_hook unbreaking:1 hide_enchants name:\"${result.getString("name")}\"")
this.set("lore", mutableListOf(
"${result.getString("name")}",
"&fYou have %keys% keys",
"&fGet more at &astore.example.net"
))
this.set("row", 2)
this.set("column", 3)
this.set("right-click-previews", true)
this.set("left-click-opens", true)
this.set("shift-left-click-messsage",
mutableListOf("Buy a ${result.getString("name")} key here! &astore.example.net"))
})
result.set("open", ConverterUtils.emptyBukkit().apply {
this.set("messages", mutableListOf("Good luck!"))
this.set("broadcasts", mutableListOf("%player%&f is opening the ${result.getString("name")}!"))
this.set("commands", mutableListOf<String>())
val sound = ConverterUtils.emptyBukkit().apply {
this.set("sound", "entity_villager_yes")
this.set("volume", 10)
this.set("pitch", 1)
}
this.set("sounds", mutableListOf(ConverterUtils.convertToBukkit(sound)))
})
result.set("finish", ConverterUtils.emptyBukkit().apply {
this.set("messages", mutableListOf("You won %reward%&f!"))
this.set("broadcasts", mutableListOf("%player%&f won %reward%&f from the ${result.getString("name")}!"))
this.set("commands", mutableListOf<String>())
val firework = ConverterUtils.emptyBukkit().apply {
this.set("power", 2)
this.set("type", "ball_large")
this.set("colors", mutableListOf("00ffff", "00ff00"))
this.set("fade-colors", mutableListOf("ffffff", "999999"))
this.set("trail", true)
this.set("flicker", true)
}
this.set("fireworks", mutableListOf(ConverterUtils.convertToBukkit(firework)))
val sound = ConverterUtils.emptyBukkit().apply {
this.set("sound", "entity_generic_explode")
this.set("volume", 10)
this.set("pitch", 1)
}
this.set("sounds", mutableListOf(ConverterUtils.convertToBukkit(sound)))
})
val preRewards = EcoCratesPlugin.instance.rewardsYml.bukkitHandle!!.get("rewards")
as MutableList<YamlConfiguration>
preRewards.addAll(rewards)
EcoCratesPlugin.instance.rewardsYml.set("rewards", preRewards)
return result
}
private fun convertReward(reward: Reward, salt: String, row: Int, col: Int): YamlConfiguration {
val result = ConverterUtils.createDefaultReward()
result.set("id", salt)
result.set("commands", reward.commands.map { it.replace("/", "")
.replace("{player}", "%player%") })
result.set("items", reward.items.map { ConverterUtils.itemToStringSimplified(it) })
val messages = mutableListOf<String>()
reward.messages.values.forEach {
messages.addAll(it)
}
result.set("messages", messages.map {it.replace("{player}", "%player%") })
result.set("weight.display", reward.chance)
result.set("weight.actual", reward.chance)
result.set("display.name", reward.displayItem.itemMeta?.displayName)
result.set("display.item", ConverterUtils.itemToStringSimplified(reward.displayItem))
result.set("display.lore", reward.displayItem.itemMeta?.lore)
result.set("display.row", row)
result.set("display.column", col)
return result
}
}

View File

@@ -0,0 +1,236 @@
package com.willfp.ecocrates.converters.util
import com.willfp.eco.core.Eco
import com.willfp.eco.core.config.BaseConfig
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.items.Items
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.inventory.ItemStack
import org.yaml.snakeyaml.Yaml
class ConverterUtils {
companion object {
@JvmStatic
fun createDefaultReward(): YamlConfiguration {
val result = emptyBukkit()
result.set("id", "default")
result.set("commands", mutableListOf<String>())
result.set("items", mutableListOf("diamond_sword sharpness:5 unbreaking:3"))
result.set("messages", mutableListOf<String>())
result.set("weight",
emptyBukkit().apply {
this.set("permission-multipliers", true)
this.set("actual", 1)
this.set("display", 25)
}
)
result.set("max-wins", -1)
result.set("display",
emptyBukkit().apply {
this.set("name", "&bDiamond Sword")
this.set("item", "diamond_sword sharpness:5 unbreaking:3")
this.set("lore", mutableListOf(
"&fDisplay Chance: &a%chance%%",
"&fActual Chance: &a%actual_chance%%"
))
this.set("row", 3)
this.set("column", 2)
}
)
return result
}
@JvmStatic
fun createDefaultCrate(): YamlConfiguration {
val result = emptyBukkit()
result.set("id", "default")
result.set("name", "default")
result.set("roll", "csgo")
result.set("can-reroll", true)
val preview = emptyBukkit().apply {
this.set("title", "Default Crate")
this.set("rows", 6)
val mask = emptyBukkit().apply {
this.set("items", mutableListOf("black_stained_glass_pane"))
this.set("pattern", mutableListOf(
"222222222",
"200000002",
"200000002",
"200000002",
"200000002",
"222222222"
))
}
this.set("mask", convertToBukkit(mask))
}
result.set("preview", convertToBukkit(preview))
val key = emptyBukkit().apply {
this.set("item", "tripwire_hook unbreaking:1 hide_enchants name:\"&aDefault Crate Key\"")
this.set("lore", mutableListOf(
"&fUse this key to open",
"&fthe <g:#56ab2f>Default Crate</g:#a8e063>"
))
}
result.set("key", convertToBukkit(key))
val keygui = emptyBukkit().apply {
this.set("enabled", true)
this.set("item", "tripwire_hook unbreaking:1 hide_enchants name:\"Default Crate\"")
this.set("lore", mutableListOf(
"<g:#56ab2f>Default Crate</g:#a8e063>",
"&fYou have %keys% keys",
"&fGet more at &astore.example.net"
))
this.set("row", 2)
this.set("column", 3)
this.set("right-click-previews", true)
this.set("left-click-opens", true)
this.set("shift-left-click-messsage", mutableListOf("Buy a Demo Crate key here! &astore.example.net"))
}
result.set("keygui", convertToBukkit(keygui))
val payToOpen = emptyBukkit().apply {
this.set("enabled", false)
this.set("price", 5000)
}
result.set("pay-to-open", convertToBukkit(payToOpen))
val placed = emptyBukkit().apply {
val randomReward = emptyBukkit().apply {
this.set("enabled", true)
this.set("height", 1.5)
this.set("delay", 30)
this.set("name", "&fYou could win:")
}
this.set("random-reward", convertToBukkit(randomReward))
val particle = emptyBukkit().apply {
this.set("particle", "flame")
this.set("animation", "spiral")
}
this.set("particles", mutableListOf(convertToBukkit(particle)))
val hologram = emptyBukkit().apply {
this.set("height", 1.5)
this.set("ticks", 200)
val tick0 = emptyBukkit().apply {
this.set("tick", 0)
this.set("lines", mutableListOf(
"<g:#56ab2f>&lDEFAULT CRATE</g:#a8e063>",
"&b&lLeft Click to Preview",
"&a&lRight click to Open"
))
}
val tick100 = emptyBukkit().apply {
this.set("tick", 100)
this.set("lines", mutableListOf(
"<g:#56ab2f>&lDEFAULT CRATE</g:#a8e063>",
"&a&lLeft Click to Preview",
"&b&lRight click to Open"
))
}
this.set("frames", mutableListOf(convertToBukkit(tick0), convertToBukkit(tick100)))
}
this.set("hologram", hologram)
}
result.set("placed", convertToBukkit(placed))
val open = emptyBukkit().apply {
this.set("messages", mutableListOf("Good luck!"))
this.set("broadcasts", mutableListOf("%player%&f is opening the Default Crate!"))
this.set("commands", mutableListOf<String>())
val sound = emptyBukkit().apply {
this.set("sound", "entity_villager_yes")
this.set("volume", 10)
this.set("pitch", 1)
}
this.set("sounds", mutableListOf(convertToBukkit(sound)))
}
result.set("open", convertToBukkit(open))
val finish = emptyBukkit().apply {
this.set("messages", mutableListOf("You won %reward%&f!"))
this.set("broadcasts", mutableListOf("%player%&f won %reward%&f from the Default Crate!"))
this.set("commands", mutableListOf<String>())
val firework = emptyBukkit().apply {
this.set("power", 2)
this.set("type", "ball_large")
this.set("colors", mutableListOf("00ffff", "00ff00"))
this.set("fade-colors", mutableListOf("ffffff", "999999"))
this.set("trail", true)
this.set("flicker", true)
}
this.set("fireworks", mutableListOf(convertToBukkit(firework)))
val sound = emptyBukkit().apply {
this.set("sound", "entity_generic_explode")
this.set("volume", 10)
this.set("pitch", 1)
}
this.set("sounds", mutableListOf(convertToBukkit(sound)))
}
result.set("finish", convertToBukkit(finish))
result.set("rewards", mutableListOf("diamond_sword", "bedrock"))
return convertToBukkit(result)
}
@JvmStatic
fun emptyBukkit(): YamlConfiguration {
return YamlConfiguration()
}
@JvmStatic
fun convertToBukkit(config: YamlConfiguration): YamlConfiguration {
return config
}
@JvmStatic
fun itemToStringSimplified(itemStack: ItemStack): String {
if (Items.isCustomItem(itemStack)) {
val custom = Items.getCustomItem(itemStack)!!
return "${custom.key.namespace}:${custom.key.key}"
}
var result = "${itemStack.type.name.lowercase()} ${itemStack.amount}"
val meta = itemStack.itemMeta?: return result
meta.enchants.forEach {
result += " ${it.key.key.key}:${it.value}"
}
if (meta.hasDisplayName()) {
result += " name:\"${meta.displayName}\""
}
if (meta.hasCustomModelData()) {
result += " custom-model-data:${meta.customModelData}"
}
meta.itemFlags.forEach {
result += " ${it.name.lowercase()}"
}
return result
}
}
}

View File

@@ -18,3 +18,6 @@ messages:
resetting-wins: "&fResetting wins for %user%&f..."
reset-wins: "&fReset wins for %user%&f!"
no-crate-permission: "&cYou don't have permission to open this crate!"
must-specify-converter: "&cYou must specify a valid converter!"
invalid-converter: "&cUnknown converter!"
converted: "&aConverted successfully!"

View File

@@ -8,6 +8,8 @@ load: STARTUP
depend:
- eco
- ProtocolLib
softdepend:
- CrateReloaded
commands:
ecocrates: