Make SlottedContainerBuilder be embedded inside SlottedContainer

This commit is contained in:
DBotThePony 2025-02-27 20:41:56 +07:00
parent b39c82e79a
commit fc1d9d6448
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 46 additions and 49 deletions

View File

@ -17,7 +17,7 @@ import ru.dbotthepony.mc.otm.data.codec.minRange
import java.util.function.Predicate
class SlottedContainer(
slots: Collection<SlottedContainerBuilder.SlotProvider>,
slots: Collection<SlotProvider>,
private val stillValid: Predicate<Player>,
private val globalChangeListeners: Array<Runnable>
) : ISlottedContainer, INBTSerializable<Tag> {
@ -171,6 +171,51 @@ class SlottedContainer(
notifyChanged()
}
fun interface SlotProvider {
fun create(container: SlottedContainer, index: Int): ContainerSlot
}
class Builder {
private val slots = ArrayList<SlotProvider>()
private var stillValid = Predicate<Player> { true }
private val globalChangeListeners = ArrayList<Runnable>()
fun add(slot: SlotProvider): Builder {
slots.add(slot)
return this
}
fun add(amount: Int, provider: SlotProvider): Builder {
for (i in 0 until amount)
slots.add(provider)
return this
}
fun stillValid(predicate: Predicate<Player>): Builder {
this.stillValid = predicate
return this
}
fun onChanged(listener: Runnable): Builder {
globalChangeListeners.add(listener)
return this
}
fun copy(): Builder {
val copy = Builder()
copy.slots.addAll(slots)
copy.globalChangeListeners.addAll(globalChangeListeners)
copy.stillValid = stillValid
return copy
}
fun build(): SlottedContainer {
return SlottedContainer(slots, stillValid, globalChangeListeners.toTypedArray())
}
}
companion object {
private val LOGGER = LogManager.getLogger()
}

View File

@ -1,48 +0,0 @@
package ru.dbotthepony.mc.otm.container
import net.minecraft.world.entity.player.Player
import java.util.function.Predicate
class SlottedContainerBuilder {
fun interface SlotProvider {
fun create(container: SlottedContainer, index: Int): ContainerSlot
}
private val slots = ArrayList<SlotProvider>()
private var stillValid = Predicate<Player> { true }
private val globalChangeListeners = ArrayList<Runnable>()
fun add(slot: SlotProvider): SlottedContainerBuilder {
slots.add(slot)
return this
}
fun add(amount: Int, provider: SlotProvider): SlottedContainerBuilder {
for (i in 0 until amount)
slots.add(provider)
return this
}
fun stillValid(predicate: Predicate<Player>): SlottedContainerBuilder {
this.stillValid = predicate
return this
}
fun onChanged(listener: Runnable): SlottedContainerBuilder {
globalChangeListeners.add(listener)
return this
}
fun copy(): SlottedContainerBuilder {
val copy = SlottedContainerBuilder()
copy.slots.addAll(slots)
copy.globalChangeListeners.addAll(globalChangeListeners)
copy.stillValid = stillValid
return copy
}
fun build(): SlottedContainer {
return SlottedContainer(slots, stillValid, globalChangeListeners.toTypedArray())
}
}