@ -2,26 +2,43 @@ package pw.hamzantal.shopreborn
import hazae41.minecraft.kutils.bukkit.ConfigFile
import hazae41.minecraft.kutils.bukkit.keys
import hazae41.minecraft.kutils.get
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.ItemStack
import java.io.File
import java.lang.IllegalArgumentException
object Configurations {
var dataFolder = File ( " " )
lateinit var main : MainConfig
val shops = mutableListOf < ConfigFile > ( )
val shops = mutableListOf < ShopConfig > ( )
}
class Buttons ( val pane : ItemStack , val previous : ItemStack , val menu : ItemStack , val forward : ItemStack )
class MainConfig ( file : File ) : ConfigFile ( file ) {
data class Block ( val item : ItemStack , val slot : Int , val commands : List < String > )
class Block ( val item : ItemStack , val slot : Int , val commands : List < String > )
va r shops by section ( " shops " )
va l shops by section ( " shops " )
var size by int ( " shopGUI.size " )
var title by string ( " shopGUI.title " )
var blocksRaw by section ( " shopGUI.blocks " )
val size by int ( " shopGUI.size " )
val title by string ( " shopGUI.title " )
val blocksRaw by section ( " shopGUI.blocks " )
val pane by section ( " buttons.pane " )
val previous by section ( " buttons.previous " )
val forward by section ( " buttons.forward " )
val menu by section ( " buttons.menu " )
val buttons = Buttons (
pane !! . asItem ( ) ,
previous !! . asItem ( ) ,
menu !! . asItem ( ) ,
forward !! . asItem ( )
)
val blocks = blocksRaw ?. keys
?. map { blocksRaw !! . getConfigurationSection ( it ) }
@ -36,19 +53,105 @@ class MainConfig(file: File) : ConfigFile(file) {
val inventory : Inventory = Bukkit . createInventory ( null , size , title . c ) . apply {
blocks . forEach {
println ( it )
setItem ( it . slot , it . item )
}
}
fun addShops ( ) {
Configurations . shops . addAll ( shops !! . keys . map {
ShopConfig (
it ,
Configurations . dataFolder [ shops !! . getString ( it ) ]
)
} )
}
}
class ShopConfig ( val name : String , file : File ) : ConfigFile ( file ) {
open class Block ( val item : ItemStack )
class Item ( item : ItemStack , val buy : Int , val sell : Int ) : Block ( item )
class Command ( item : ItemStack , val asPlayer : Boolean , val commands : List < String > ) : Block ( item )
init {
run {
if ( file . exists ( ) ) return @run
file . createNewFile ( )
set ( " name " , " &a $name " )
set ( " size " , 18 )
set ( " buttons.menuRow " , 9 )
set ( " items.1.type " , " item " )
set ( " items.1.item.material " , " STONE " )
set ( " items.1.buyPrice " , 5 )
set ( " items.2.type " , " item " )
set ( " items.2.item.material " , " GRASS " )
set ( " items.2.buyPrice " , 6 )
set ( " items.2.sellPrice " , 6 )
set ( " items.3.type " , " item " )
set ( " items.3.item.material " , " IRON_ORE " )
set ( " items.3.buyPrice " , 10 )
}
}
val title by string ( " name " , " &a $name " )
val size by int ( " size " , 9 )
val menuRow by int ( " buttons.menuRow " , - 1 )
val blocksRaw by section ( " items " )
val blocks = blocksRaw ?. keys
?. map { blocksRaw !! . getConfigurationSection ( it ) }
?. map < ConfigurationSection , Block > {
val type = it . getString ( " type " )
val buy = it . getInt ( " buyPrice " , - 1 )
val sell = it . getInt ( " sellPrice " , - 1 )
val item = it . getConfigurationSection ( " item " ) . asItem ( buy , sell )
when ( type ) {
" command " -> Command (
item ,
it . getBoolean ( " asplayer " ) ,
it . getStringList ( " commands " )
)
" item " -> Item (
item ,
buy ,
sell
)
else -> throw IllegalArgumentException ( " Item type ( $type ) was not recognised " )
}
}
?: listOf ( )
val inventory : Inventory = Bukkit . createInventory ( null , size , title . c ) . apply {
//Set Single Row Menu
if ( menuRow != - 1 ) {
val buttons = Configurations . main . buttons
for ( i in menuRow until menuRow + 9 ) {
setItem ( i , buttons . pane )
}
setItem ( menuRow , buttons . previous )
setItem ( menuRow + 4 , buttons . menu )
setItem ( menuRow + 8 , buttons . forward )
}
blocks . forEach {
setItem ( firstEmpty ( ) , it . item )
}
}
}
fun ConfigurationSection . asItem ( ) : ItemStack {
fun ConfigurationSection . asItem ( buy : Int = - 1 , sell : Int = - 1 ) : ItemStack {
val material = Material . valueOf ( getString ( " material " ) . toUpperCase ( ) )
val item = ItemStack ( material , getInt ( " quantity " , 1 ) , getInt ( " data " , 0 ) . toShort ( ) )
item . itemMeta = item . itemMeta . apply {
displayName = getString ( " name " , " " ) . c
lore = getStringList ( " lore " ) . map { it . c }
val configLore = getStringList ( " lore " ) . map { it . c } . toMutableList ( )
if ( buy > 0 ) configLore += " &fPurchase for $ $buy " . c
if ( sell > 0 ) configLore += " &fSell for $ $sell " . c
lore = configLore
}
return item
}