-
Notifications
You must be signed in to change notification settings - Fork 6
chore: Rework pane mapping #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/mapping/ChestGridMapper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.noxcrew.interfaces.grid.mapping | ||
|
||
import com.noxcrew.interfaces.utilities.gridPointToBukkitIndex | ||
|
||
/** Handles [com.noxcrew.interfaces.grid.GridPoint] mapping for [org.bukkit.event.inventory.InventoryType.CHEST] containers. */ | ||
public open class ChestGridMapper(private val rows: Int) : AbstractGridMapper(), GridMapper.TopInventory { | ||
LichtHund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
override fun toTopInventorySlot(row: Int, column: Int): Int { | ||
return gridPointToBukkitIndex(row, column) | ||
} | ||
|
||
override fun isPlayerInventory(row: Int, column: Int): Boolean = row >= rows | ||
} |
19 changes: 19 additions & 0 deletions
19
interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/mapping/CombinedGridMapper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.noxcrew.interfaces.grid.mapping | ||
|
||
import com.noxcrew.interfaces.grid.mapping.GridMapper.PlayerInventory.Companion.PLAYER_INV_ROWS | ||
import com.noxcrew.interfaces.utilities.gridPointToBukkitIndex | ||
import com.noxcrew.interfaces.view.AbstractInterfaceView.Companion.COLUMNS_IN_CHEST | ||
|
||
/** Handles [com.noxcrew.interfaces.grid.GridPoint] mapping for containers combining Chest and Player inventory. */ | ||
public class CombinedGridMapper(private val rows: Int) : ChestGridMapper(rows), GridMapper.PlayerInventory { | ||
|
||
/** Rows of chest times [COLUMNS_IN_CHEST] minus [COLUMNS_IN_CHEST] because 0-8 is in the hot bar. */ | ||
private val chestSize = rows * COLUMNS_IN_CHEST - COLUMNS_IN_CHEST | ||
|
||
override fun toPlayerInventorySlot(row: Int, column: Int): Int { | ||
// If it's not the last row we remove chest size. | ||
if (row < rows + PLAYER_INV_ROWS) return gridPointToBukkitIndex(row, column) - chestSize | ||
// On the last row we use row 0 to map it from 0-8 for the hot bar. | ||
return gridPointToBukkitIndex(0, column) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/mapping/GridMapper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.noxcrew.interfaces.grid.mapping | ||
|
||
import com.noxcrew.interfaces.grid.GridPoint | ||
import com.noxcrew.interfaces.grid.GridPoint.Companion.OUTSIDE_CHEST_INDEX | ||
|
||
/** | ||
* Responsible for mapping inventory slots into [GridPoint] and vice versa. | ||
* @see AbstractGridMapper for the default [toGridPoint] implementation. | ||
* @see ChestGridMapper for the [TopInventory] only implementation. | ||
* @see CombinedGridMapper for a combined [TopInventory] and [PlayerInventory] implementation. | ||
* @see PlayerInventoryGridMapper for the [PlayerInventory] only implementation. | ||
*/ | ||
public interface GridMapper { | ||
|
||
/** | ||
* This function is called from the listener where the [slot] is the `rawSlot` representation. | ||
* This means that slots will go from 0 to x as opposed to normal slots which are based on the inventory they are in. | ||
*/ | ||
public fun toGridPoint(slot: Int): GridPoint? | ||
|
||
/** Whether the [GridPoint] is in the player's inventory. */ | ||
public fun isPlayerInventory(row: Int, column: Int): Boolean | ||
|
||
/** Responsible for mapping [GridPoint] into top inventory slots. */ | ||
public interface TopInventory : GridMapper { | ||
/** The slot returned should be relative to the inventory it is in. */ | ||
public fun toTopInventorySlot(row: Int, column: Int): Int | ||
} | ||
|
||
/** | ||
* Responsible for mapping [GridPoint] into player inventory slots. | ||
* Which cane be completely different based on the type of the [TopInventory]. | ||
*/ | ||
public interface PlayerInventory : GridMapper { | ||
public companion object { | ||
public const val PLAYER_INV_ROWS: Int = 3 | ||
} | ||
|
||
/** The slot returned is relevant to the PlayerInventory mapping, normally 0-8 is the hot bar. */ | ||
public fun toPlayerInventorySlot(row: Int, column: Int): Int | ||
} | ||
} | ||
|
||
/** Simple implementation for [toGridPoint] that can be shared by multiple [GridMapper] (assumes chest-based containers). */ | ||
public abstract class AbstractGridMapper : GridMapper { | ||
override fun toGridPoint(slot: Int): GridPoint? { | ||
if (slot == OUTSIDE_CHEST_INDEX) return null | ||
return GridPoint(slot / 9, slot % 9) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
interfaces/src/main/kotlin/com/noxcrew/interfaces/grid/mapping/PlayerInventoryGridMapper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.noxcrew.interfaces.grid.mapping | ||
|
||
import com.noxcrew.interfaces.grid.GridPoint | ||
import com.noxcrew.interfaces.grid.mapping.GridMapper.PlayerInventory.Companion.PLAYER_INV_ROWS | ||
import com.noxcrew.interfaces.utilities.gridPointToBukkitIndex | ||
import com.noxcrew.interfaces.view.AbstractInterfaceView.Companion.COLUMNS_IN_CHEST | ||
|
||
/** | ||
* Handles [GridPoint] mapping for the player's own inventory. | ||
* Including armor slots and offhand. | ||
*/ | ||
public object PlayerInventoryGridMapper : AbstractGridMapper(), GridMapper.PlayerInventory { | ||
|
||
private const val PLAYER_INV_SIZE = PLAYER_INV_ROWS * COLUMNS_IN_CHEST | ||
private const val PLAYER_INV_HOT_BAR_SIZE = PLAYER_INV_SIZE + COLUMNS_IN_CHEST | ||
|
||
override fun toGridPoint(slot: Int): GridPoint? { | ||
// Hot bar slot. | ||
if (slot >= PLAYER_INV_HOT_BAR_SIZE + COLUMNS_IN_CHEST) { | ||
return super.toGridPoint(slot) | ||
} | ||
|
||
// "Extra" slots, armor, offhand, crafting grid. | ||
if (slot < COLUMNS_IN_CHEST) { | ||
return super.toGridPoint(slot + PLAYER_INV_HOT_BAR_SIZE) | ||
} | ||
|
||
// Everything else we just shift it up by 9 slots. | ||
return super.toGridPoint(slot - COLUMNS_IN_CHEST) | ||
} | ||
|
||
override fun toPlayerInventorySlot(row: Int, column: Int): Int { | ||
// If it's not the last row we remove chest size. | ||
if (row < PLAYER_INV_ROWS) return gridPointToBukkitIndex(row, column) - COLUMNS_IN_CHEST | ||
// On the last row we use row 0 to map it from 0-8 for the hot bar. | ||
return gridPointToBukkitIndex(0, column) | ||
} | ||
|
||
override fun isPlayerInventory(row: Int, column: Int): Boolean = true | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.