Skip to content

Commit

Permalink
Merge properties into builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeltumn committed Jun 3, 2024
1 parent 6c59fb5 commit 2cab50c
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 115 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import net.kyori.adventure.text.Component

/** Assists in building a [ChestInterface]. */
public class ChestInterfaceBuilder :
AbstractInterfaceBuilder<Pane, ChestInterface>() {
InterfaceBuilder<Pane, ChestInterface>() {

/** Sets the amount of rows for this interface to use. */
public var rows: Int = 0
Expand All @@ -16,6 +16,6 @@ public class ChestInterfaceBuilder :
override fun build(): ChestInterface = ChestInterface(
rows,
initialTitle,
properties
this
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import net.kyori.adventure.text.Component

/** Assists in building a [CombinedInterface]. */
public class CombinedInterfaceBuilder :
AbstractInterfaceBuilder<CombinedPane, CombinedInterface>() {
InterfaceBuilder<CombinedPane, CombinedInterface>() {

/** Sets the amount of rows for this interface to use. */
public var rows: Int = 0
Expand All @@ -16,6 +16,6 @@ public class CombinedInterfaceBuilder :
override fun build(): CombinedInterface = CombinedInterface(
rows,
initialTitle,
properties
this
)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
package com.noxcrew.interfaces.interfaces

import com.noxcrew.interfaces.pane.Pane
import com.noxcrew.interfaces.properties.Trigger
import com.noxcrew.interfaces.transform.AppliedTransform
import com.noxcrew.interfaces.transform.ReactiveTransform
import com.noxcrew.interfaces.transform.Transform
import com.noxcrew.interfaces.utilities.IncrementingInteger

/** A generic builder for creating an [Interface]. */
public abstract class InterfaceBuilder<P : Pane, T : Interface<P>> {
/** Assists in creating a new interface. */
public abstract class InterfaceBuilder<P : Pane, I : Interface<P>> : InterfaceProperties<P>() {

private val transformCounter by IncrementingInteger()
private val _transforms: MutableCollection<AppliedTransform<P>> = mutableListOf()

/** All transforms in this builder. */
public val transforms: Collection<AppliedTransform<P>>
get() = _transforms

/** Creates the interface. */
public abstract fun build(): T
public abstract fun build(): I

/** Adds a new transform to the interface that updates whenever [triggers] change. */
public fun withTransform(vararg triggers: Trigger, transform: Transform<P>) {
_transforms += AppliedTransform(transformCounter, triggers.toSet(), transform)
}

/** Adds a new reactive transform to the interface. */
public fun addTransform(reactiveTransform: ReactiveTransform<P>) {
_transforms += AppliedTransform(transformCounter, reactiveTransform.triggers.toSet(), reactiveTransform)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,73 @@ package com.noxcrew.interfaces.interfaces

import com.noxcrew.interfaces.click.ClickHandler
import com.noxcrew.interfaces.pane.Pane
import com.noxcrew.interfaces.transform.AppliedTransform
import org.bukkit.event.block.Action
import org.bukkit.event.inventory.InventoryCloseEvent
import org.bukkit.inventory.ItemStack

/** Stores all shared properties of an interface. */
public data class InterfaceProperties<P : Pane>(
public open class InterfaceProperties<P : Pane> {

private companion object {
/** All default reasons used for a new close handler. */
private val DEFAULT_REASONS = InventoryCloseEvent.Reason.entries.minus(InventoryCloseEvent.Reason.PLUGIN)
}

private val _closeHandlers: MutableMap<InventoryCloseEvent.Reason, CloseHandler> = mutableMapOf()
private val _clickPreprocessors: MutableCollection<ClickHandler> = mutableListOf()
private val _preventedInteractions: MutableCollection<Action> = mutableListOf()

/** All close handlers on this interface mapped by closing reason. */
public val closeHandlers: Map<InventoryCloseEvent.Reason, CloseHandler> = emptyMap(),
/** All transforms that make up this interface. */
public val transforms: Collection<AppliedTransform<P>> = emptySet(),
public val closeHandlers: Map<InventoryCloseEvent.Reason, CloseHandler>
get() = _closeHandlers

/** A collection of click handlers that will be run before each click without blocking. */
public val clickPreprocessors: Collection<ClickHandler> = emptySet(),
public val clickPreprocessors: Collection<ClickHandler>
get() = _clickPreprocessors

/** All interactions that will be ignored on this view and cancelled on pane items without calling the handler. */
public val preventedInteractions: Collection<Action>
get() = _preventedInteractions

/** A post-processor applied to all items placed in the inventory. */
public val itemPostProcessor: ((ItemStack) -> Unit)? = {},
public var itemPostProcessor: ((ItemStack) -> Unit)? = {}

/** Whether clicking on empty slots should be cancelled. */
public val preventClickingEmptySlots: Boolean = true,
/** All interactions that will be ignored on this view and cancelled on pane items without calling the handler. */
public val preventedInteractions: Collection<Action> = emptySet(),
/** Persists items added to this pane in a previous instance. */
public val persistAddedItems: Boolean = false,
public var preventClickingEmptySlots: Boolean = true

/**
* Persists items added to this pane in a previous instance.
* Particularly useful for player inventories, this allows the non-interface items
* to function as normal inventory items and be normally added/removed.
*/
public var persistAddedItems: Boolean = false

/** Keeps items that were previously in the inventory before opening this. */
public val inheritExistingItems: Boolean = false,
public var inheritExistingItems: Boolean = false

/** Whether close handlers should be called when switching to a different view. */
public val callCloseHandlerOnViewSwitch: Boolean = true
)
public var callCloseHandlerOnViewSwitch: Boolean = true

/** Whether to place empty air elements in all background slots. */
public var fillMenuWithAir: Boolean = false

/** Adds a new close handler [closeHandler] that triggers whenever the inventory is closed for any of the given [reasons]. */
public fun addCloseHandler(
reasons: Collection<InventoryCloseEvent.Reason> = DEFAULT_REASONS,
closeHandler: CloseHandler
) {
reasons.forEach {
_closeHandlers[it] = closeHandler
}
}

/** Adds a new pre-processor to this menu which will run [handler] before every click without blocking. */
public fun addPreprocessor(handler: ClickHandler) {
_clickPreprocessors += handler
}

/** Adds [action] to be cancelled without triggering any click handlers on valid items in this pane. */
public fun addPreventedAction(action: Action) {
_preventedInteractions += action
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.noxcrew.interfaces.interfaces
import com.noxcrew.interfaces.pane.PlayerPane

/** Assists in building a [PlayerInterface]. */
public class PlayerInterfaceBuilder : AbstractInterfaceBuilder<PlayerPane, PlayerInterface>() {
public class PlayerInterfaceBuilder : InterfaceBuilder<PlayerPane, PlayerInterface>() {

override fun build(): PlayerInterface = PlayerInterface(properties)
override fun build(): PlayerInterface = PlayerInterface(this)
}

0 comments on commit 2cab50c

Please sign in to comment.