mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Add optimisations.
This commit is contained in:
parent
4d4facf0ab
commit
760cdef6ad
7 changed files with 78 additions and 55 deletions
36
src/main/kotlin/xyz/alexcrea/group/AbstractMaterialGroup.kt
Normal file
36
src/main/kotlin/xyz/alexcrea/group/AbstractMaterialGroup.kt
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package xyz.alexcrea.group
|
||||
|
||||
import org.bukkit.Material
|
||||
import java.util.EnumSet
|
||||
|
||||
abstract class AbstractMaterialGroup(private val name: String) {
|
||||
protected val includedMaterial by lazy {createDefaultSet()}
|
||||
|
||||
// Get the group as a set
|
||||
protected abstract fun createDefaultSet(): EnumSet<Material>
|
||||
|
||||
// Get if a material is allowed following the group policy
|
||||
fun contain(mat : Material): Boolean {
|
||||
return mat in includedMaterial
|
||||
}
|
||||
|
||||
// Get if a group is referenced by this
|
||||
abstract fun isReferencing(other : AbstractMaterialGroup): Boolean
|
||||
|
||||
// Push a material to this group to follow this group policy
|
||||
abstract fun addToPolicy(mat : Material)
|
||||
|
||||
// Push a group to this group to follow this group policy
|
||||
abstract fun addToPolicy(other : AbstractMaterialGroup)
|
||||
|
||||
// Get the group name in case something is wrong
|
||||
fun getName(): String {
|
||||
return name
|
||||
}
|
||||
|
||||
// Get the group as a set
|
||||
fun getSet(): Set<Material> {
|
||||
return includedMaterial
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@ import io.delilaheve.util.ItemUtil.findEnchantments
|
|||
import org.bukkit.enchantments.Enchantment
|
||||
import org.bukkit.inventory.ItemStack
|
||||
|
||||
class EnchantConflictGroup(val cantConflict: MaterialGroup, val minBeforeBlock: Int){
|
||||
class EnchantConflictGroup(val cantConflict: AbstractMaterialGroup, val minBeforeBlock: Int){
|
||||
|
||||
private val enchantments = HashSet<Enchantment>()
|
||||
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ class EnchantConflictManager {
|
|||
}
|
||||
// Find or create the selected group for the conflict
|
||||
val groupList = section.getStringList(CONFLICT_GROUP_PATH)
|
||||
val finalGroup: MaterialGroup
|
||||
val finalGroup: AbstractMaterialGroup
|
||||
if(groupList.size < 1){
|
||||
finalGroup = DEFAULT_EMPTY_GROUP
|
||||
}else if(groupList.size == 1){
|
||||
|
|
@ -101,7 +101,7 @@ class EnchantConflictManager {
|
|||
return EnchantConflictGroup(finalGroup, minBeforeBlock)
|
||||
}
|
||||
|
||||
private fun findGroup(groupName: String,itemManager: ItemGroupManager, conflictName: String): MaterialGroup {
|
||||
private fun findGroup(groupName: String,itemManager: ItemGroupManager, conflictName: String): AbstractMaterialGroup {
|
||||
val group = itemManager.get(groupName)
|
||||
if(group == null){
|
||||
UnsafeEnchants.instance.logger.warning("Group $groupName do not exist but is ask by conflict $conflictName")
|
||||
|
|
|
|||
|
|
@ -1,11 +1,33 @@
|
|||
package xyz.alexcrea.group
|
||||
|
||||
import org.bukkit.Material
|
||||
import java.util.*
|
||||
import kotlin.collections.HashSet
|
||||
|
||||
class ExcludeGroup(name: String): IncludeGroup(name) {
|
||||
|
||||
override fun contain(mat: Material): Boolean {
|
||||
return !super.contain(mat)
|
||||
class ExcludeGroup(name: String): AbstractMaterialGroup(name) {
|
||||
override fun createDefaultSet(): EnumSet<Material> {
|
||||
return EnumSet.allOf(Material::class.java)
|
||||
}
|
||||
|
||||
private val includedGroup = HashSet<AbstractMaterialGroup>()
|
||||
|
||||
override fun isReferencing(other: AbstractMaterialGroup): Boolean {
|
||||
for (materialGroup in includedGroup.iterator()) {
|
||||
if((materialGroup == other) || (materialGroup.isReferencing(other))){
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
override fun addToPolicy(mat: Material) {
|
||||
includedMaterial.remove(mat)
|
||||
}
|
||||
|
||||
override fun addToPolicy(other: AbstractMaterialGroup) {
|
||||
includedGroup.add(other)
|
||||
includedMaterial.removeAll(other.getSet())
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -3,24 +3,14 @@ package xyz.alexcrea.group
|
|||
import org.bukkit.Material
|
||||
import java.util.EnumSet
|
||||
|
||||
open class IncludeGroup(private val name: String): MaterialGroup{
|
||||
private val includedMaterial = EnumSet.noneOf(Material::class.java)
|
||||
private val includedGroup = HashSet<MaterialGroup>()
|
||||
|
||||
override fun contain(mat: Material): Boolean {
|
||||
if(mat in includedMaterial){
|
||||
return true
|
||||
}
|
||||
|
||||
for (materialGroup in includedGroup.iterator()) {
|
||||
if(materialGroup.contain(mat)){
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
class IncludeGroup(name: String): AbstractMaterialGroup(name) {
|
||||
override fun createDefaultSet(): EnumSet<Material> {
|
||||
return EnumSet.noneOf(Material::class.java)
|
||||
}
|
||||
|
||||
override fun isReferencing(other: MaterialGroup): Boolean {
|
||||
private val includedGroup = HashSet<AbstractMaterialGroup>()
|
||||
|
||||
override fun isReferencing(other: AbstractMaterialGroup): Boolean {
|
||||
for (materialGroup in includedGroup.iterator()) {
|
||||
if((materialGroup == other) || (materialGroup.isReferencing(other))){
|
||||
return true
|
||||
|
|
@ -33,12 +23,9 @@ open class IncludeGroup(private val name: String): MaterialGroup{
|
|||
includedMaterial.add(mat)
|
||||
}
|
||||
|
||||
override fun addToPolicy(other: MaterialGroup) {
|
||||
override fun addToPolicy(other: AbstractMaterialGroup) {
|
||||
includedGroup.add(other)
|
||||
}
|
||||
|
||||
override fun getName(): String {
|
||||
return name
|
||||
includedMaterial.addAll(other.getSet())
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -20,7 +20,7 @@ class ItemGroupManager {
|
|||
private val FUTURE_MATERIAL = setOf("PIGLIN_HEAD","BRUSH")
|
||||
}
|
||||
|
||||
private lateinit var groupMap : HashMap<String,MaterialGroup>
|
||||
private lateinit var groupMap : HashMap<String,AbstractMaterialGroup>
|
||||
|
||||
// Read and create material groups
|
||||
fun prepareGroups(config: YamlConfiguration){
|
||||
|
|
@ -37,12 +37,12 @@ class ItemGroupManager {
|
|||
// Create group by key
|
||||
private fun createGroup(config: YamlConfiguration,
|
||||
keys: Set<String>,
|
||||
key: String): MaterialGroup{
|
||||
key: String): AbstractMaterialGroup{
|
||||
val groupSection = config.getConfigurationSection(key)!!
|
||||
val groupType = groupSection.getString(GROUP_TYPE_PATH,null)
|
||||
|
||||
// Create Material group according to the group type
|
||||
val group: MaterialGroup
|
||||
val group: AbstractMaterialGroup
|
||||
if(GroupType.EXCLUDE.equal(groupType)){
|
||||
group = ExcludeGroup(key)
|
||||
}else {
|
||||
|
|
@ -58,7 +58,7 @@ class ItemGroupManager {
|
|||
}
|
||||
|
||||
// Read Group elements
|
||||
private fun readGroup(group: MaterialGroup,
|
||||
private fun readGroup(group: AbstractMaterialGroup,
|
||||
groupSection: ConfigurationSection,
|
||||
config: YamlConfiguration,
|
||||
keys: Set<String>){
|
||||
|
|
@ -109,7 +109,7 @@ class ItemGroupManager {
|
|||
}
|
||||
|
||||
// Get the selected group or return null if it doesn't exist
|
||||
fun get(groupName: String): MaterialGroup? {
|
||||
fun get(groupName: String): AbstractMaterialGroup? {
|
||||
return groupMap[groupName]
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,22 +0,0 @@
|
|||
package xyz.alexcrea.group
|
||||
|
||||
import org.bukkit.Material
|
||||
|
||||
interface MaterialGroup {
|
||||
|
||||
// Get if a material is allowed following the group policy
|
||||
fun contain(mat : Material): Boolean
|
||||
|
||||
// Get if a group is referenced by this
|
||||
fun isReferencing(other : MaterialGroup): Boolean
|
||||
|
||||
// Push a material to this group to follow this group policy
|
||||
fun addToPolicy(mat : Material)
|
||||
|
||||
// Push a group to this group to follow this group policy
|
||||
fun addToPolicy(other : MaterialGroup)
|
||||
|
||||
// Get the group name in case something is wrong
|
||||
fun getName(): String
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue