93 lines
2.0 KiB
Kotlin
93 lines
2.0 KiB
Kotlin
package club.mcscrims.speedhg.recraft
|
|
|
|
import org.bukkit.Material
|
|
import org.bukkit.entity.Player
|
|
import org.bukkit.inventory.ItemStack
|
|
|
|
|
|
class Recraft {
|
|
|
|
private val recraftMaterials = listOf(
|
|
RecraftMaterial( 1, arrayOf( Material.RED_MUSHROOM, Material.BROWN_MUSHROOM )),
|
|
RecraftMaterial( 1, arrayOf( Material.COCOA_BEANS )),
|
|
RecraftMaterial( 1, arrayOf( Material.CACTUS ))
|
|
)
|
|
|
|
fun calcRecraft(
|
|
vararg items: ItemStack?
|
|
) {
|
|
recraftMaterials.forEach( RecraftMaterial::reset )
|
|
|
|
for ( item in items )
|
|
{
|
|
if ( item == null )
|
|
return
|
|
|
|
for ( recraftMaterial in recraftMaterials )
|
|
{
|
|
val type = item.type
|
|
|
|
if (recraftMaterial.containsKey( type ))
|
|
recraftMaterial[ type ] = recraftMaterial.getOrDefault( type , 0 ) + item.amount
|
|
}
|
|
}
|
|
}
|
|
|
|
fun decrease(
|
|
player: Player,
|
|
amount: Int
|
|
) {
|
|
val lowestMaterials = mutableListOf<Material>()
|
|
|
|
for ( recraftMaterial in recraftMaterials )
|
|
if ( recraftMaterial.getLowestMaterial() != null )
|
|
lowestMaterials.add( recraftMaterial.getLowestMaterial()!! )
|
|
|
|
var highestMaterial: Material? = null
|
|
var i = 0f
|
|
|
|
for ( lowestMaterial in lowestMaterials )
|
|
{
|
|
val recraftMaterial = byMaterial( lowestMaterial )
|
|
|
|
if (recraftMaterial!![ lowestMaterial ]!! * recraftMaterial.getMaterialValue() > i )
|
|
{
|
|
i = recraftMaterial[ lowestMaterial ]!! * recraftMaterial.getMaterialValue()
|
|
highestMaterial = lowestMaterial
|
|
}
|
|
}
|
|
|
|
val recraftMaterial = byMaterial( highestMaterial!! )
|
|
recraftMaterial?.decrease( highestMaterial, amount )
|
|
|
|
for ( item in player.inventory.contents )
|
|
{
|
|
if ( item == null )
|
|
continue
|
|
|
|
if ( item.type == highestMaterial )
|
|
{
|
|
item.amount -= amount
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
fun byMaterial(
|
|
material: Material
|
|
): RecraftMaterial?
|
|
{
|
|
return recraftMaterials.stream().filter { it.containsKey( material ) }.findFirst().orElse( null )
|
|
}
|
|
|
|
fun getRecraftPoints(): Float
|
|
{
|
|
var points = 0f
|
|
|
|
for ( recraftMaterial in recraftMaterials )
|
|
points += recraftMaterial.getPoints()
|
|
|
|
return points
|
|
}
|
|
|
|
} |