com.github.shynixn.ball.bukkit.logic.persistence.entity.ItemContainer.kt Maven / Gradle / Ivy
package com.github.shynixn.ball.bukkit.logic.persistence.entity
import com.github.shynixn.ball.bukkit.BallPlugin
import com.github.shynixn.ball.bukkit.core.logic.business.helper.NBTTagHelper
import com.github.shynixn.ball.bukkit.core.logic.business.helper.SkinHelper
import com.github.shynixn.ball.bukkit.logic.business.Permission
import com.github.shynixn.ball.bukkit.logic.persistence.configuration.Config
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import java.util.*
import java.util.logging.Level
import kotlin.collections.ArrayList
/**
* Created by Shynixn 2018.
*
* Version 1.2
*
* MIT License
*
* Copyright (c) 2018 by Shynixn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
class ItemContainer(var position : Int, data : Map)
{
private var cache : ItemStack? = null
var id : Int = 0
var damage : Int = 0
lateinit var skin : String
var name : String? = null
var lore: Array? = null
var unbreakable: Boolean = false
init {
if (data.containsKey("position"))
this.position = data["position"] as Int
if (data.containsKey("id"))
this.id = data["id"] as Int
if (data.containsKey("damage"))
this.damage = data["damage"] as Int
if (data.containsKey("skin") && data["skin"] != "none")
this.skin = data["skin"] as String
if (data.containsKey("name")) {
when {
data["name"] == "default" -> this.name = null
data["name"] == "none" -> this.name = " "
else -> this.name = ChatColor.translateAlternateColorCodes('&', data["name"] as String)
}
}
if (data.containsKey("unbreakable"))
this.unbreakable = data["unbreakable"] as Boolean
if (data.containsKey("lore")) {
val m = data["lore"] as List<*>
val lore = m
.filter { it != "none" }
.map { ChatColor.translateAlternateColorCodes('&', it.toString()) }
this.lore = lore.toTypedArray()
}
}
/**
* Generates a new itemStack for the player and his permissions
*
* @param player player
* @param permissions permission
* @return itemStack
*/
fun generate(player: Player, vararg permissions: Permission): ItemStack {
if (this.cache != null) {
return this.cache!!.clone()
}
try {
var itemStack: ItemStack? = ItemStack(Material.getMaterial(this.id), 1, this.damage.toShort())
if (itemStack!!.type == Material.SKULL_ITEM) {
SkinHelper.setItemStackSkin(itemStack, skin)
}
val data = HashMap()
data.put("Unbreakable", this.unbreakable)
itemStack = NBTTagHelper.setItemStackNBTTag(itemStack, data)
val itemMeta = itemStack!!.itemMeta
itemMeta.displayName = this.name
if(this.lore != null)
{
var dataValues = ArrayList();
dataValues.addAll(this.lore!!);
itemMeta.lore = dataValues;
}
itemStack.itemMeta = itemMeta
this.cache = itemStack
return itemStack
} catch (ex: Exception) {
BallPlugin.logger().log(Level.WARNING, "Failed to generate itemStack.", ex)
}
return ItemStack(Material.AIR)
}
private fun hasPermission(player: Player, vararg permissions: String): Boolean {
for (permission in permissions) {
if (permission.endsWith(".all")) {
val subPermission = permission.substring(0, permission.indexOf("all")) + this.position
if (player.hasPermission(subPermission)) {
return true
}
}
if (player.hasPermission(permission))
return true
}
return false
}
}