-
Notifications
You must be signed in to change notification settings - Fork 38
[1.2.0] New inventory system #46
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
Open
TheBusyBiscuit
wants to merge
31
commits into
main
Choose a base branch
from
inventory-system
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
7d8e95f
Initial work on the new inventory system
TheBusyBiscuit 6665a1a
Some more progress
TheBusyBiscuit 1fa5b43
Added some unit tests and more progress
TheBusyBiscuit 84621cb
Over 90% coverage :cool:
TheBusyBiscuit 54f23ac
A lot more unit tests and stuff
TheBusyBiscuit 127af54
Finished this part of the module
TheBusyBiscuit 1093e83
Implemented click events and factories
TheBusyBiscuit d43d1f6
Renamed to `Menu`
TheBusyBiscuit a18af1b
Detect key and name collisions
TheBusyBiscuit 3164f0f
Allow factories to construct custom menu implementations
TheBusyBiscuit 67b9106
Improved docs and logging
TheBusyBiscuit 0556b86
Implemented layout parsing and added unit tests for it
TheBusyBiscuit fe9a08a
Added another unit test (just to be sure)
TheBusyBiscuit 9bc5481
Test exception handling
TheBusyBiscuit b37ecb7
Added item and click event injection
TheBusyBiscuit 6c1dbba
Fixed NullPointerException
TheBusyBiscuit cd4c6f9
Added some documentation
TheBusyBiscuit 48158d0
Updated package names
TheBusyBiscuit 1fce6ab
Merge branch 'main' into inventory-system
TheBusyBiscuit b530e01
Removed redundant imports
TheBusyBiscuit a5a4b1d
Small optimization
TheBusyBiscuit 6053fcd
Merge branch 'main' into inventory-system
TheBusyBiscuit 128c565
Merge branch 'main' of https://github.com/baked-libs/dough into inven…
TheBusyBiscuit 73177c5
Bump version
TheBusyBiscuit 96978d7
Merge branch 'inventory-system' of https://github.com/baked-libs/doug…
TheBusyBiscuit c5b67a0
Merge branch 'main' of https://github.com/baked-libs/dough into
TheBusyBiscuit 1b6fbd4
Added `SlotGroupBuilder#withSlotRange` and more documentation
TheBusyBiscuit fea4f35
More documentation
TheBusyBiscuit e36a8d4
Update dough-inventories/src/main/java/io/github/bakedlibs/dough/inve…
TheBusyBiscuit 44df16a
Merge branch 'main' into inventory-system
TheBusyBiscuit 24874bf
Merge branch 'main' into inventory-system
TheBusyBiscuit 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
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
86 changes: 86 additions & 0 deletions
86
dough-inventories/src/main/java/io/github/bakedlibs/dough/inventory/Menu.java
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,86 @@ | ||
package io.github.bakedlibs.dough.inventory; | ||
|
||
import java.util.Iterator; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
import javax.annotation.ParametersAreNonnullByDefault; | ||
|
||
import org.apache.commons.lang.Validate; | ||
import org.bukkit.entity.HumanEntity; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.Inventory; | ||
import org.bukkit.inventory.InventoryHolder; | ||
import org.bukkit.inventory.InventoryView; | ||
import org.bukkit.inventory.ItemStack; | ||
|
||
import io.github.bakedlibs.dough.inventory.factory.MenuFactory; | ||
|
||
public interface Menu extends InventoryHolder { | ||
|
||
/** | ||
* This method returns the {@link MenuFactory} which was used | ||
* to create this {@link Menu}. | ||
* | ||
* @return The original {@link MenuFactory} | ||
*/ | ||
@Nonnull | ||
MenuFactory getFactory(); | ||
|
||
/** | ||
* This returns the {@link MenuLayout} which was used to create | ||
* this {@link Menu}. | ||
* | ||
* @return The {@link MenuLayout} | ||
*/ | ||
@Nonnull | ||
MenuLayout getLayout(); | ||
|
||
/** | ||
* This returns the title of this {@link Menu}. | ||
* If no title was set, null will be returned. | ||
* | ||
* @return The title of this {@link Menu} or null | ||
*/ | ||
@Nullable | ||
String getTitle(); | ||
|
||
void setAll(@Nonnull SlotGroup group, @Nullable ItemStack item); | ||
|
||
default void clear(@Nonnull SlotGroup group) { | ||
setAll(group, null); | ||
} | ||
|
||
@ParametersAreNonnullByDefault | ||
@Nullable | ||
ItemStack addItem(SlotGroup group, ItemStack item); | ||
|
||
void setItem(int slot, @Nullable ItemStack item); | ||
|
||
@Nullable | ||
ItemStack getItem(int slot); | ||
|
||
default @Nonnull InventoryView open(@Nonnull Player player) { | ||
Validate.notNull(player, "The Player must not be null"); | ||
return player.openInventory(getInventory()); | ||
} | ||
|
||
default void closeAllViews() { | ||
Inventory inv = getInventory(); | ||
Iterator<HumanEntity> iterator = inv.getViewers().iterator(); | ||
|
||
while (iterator.hasNext()) { | ||
iterator.next().closeInventory(); | ||
} | ||
} | ||
|
||
/** | ||
* This returns the size of this {@link Menu}. | ||
* | ||
* @return The size of the {@link Menu}. | ||
*/ | ||
default int getSize() { | ||
return getInventory().getSize(); | ||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
dough-inventories/src/main/java/io/github/bakedlibs/dough/inventory/MenuLayout.java
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,92 @@ | ||
package io.github.bakedlibs.dough.inventory; | ||
|
||
import java.util.Set; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
import org.bukkit.inventory.Inventory; | ||
|
||
import io.github.bakedlibs.dough.inventory.builders.MenuLayoutBuilder; | ||
|
||
/** | ||
* The {@link MenuLayout} covers the different {@link SlotGroup}s and basic characteristics | ||
* of a {@link Menu}. | ||
* | ||
* @author TheBusyBiscuit | ||
* | ||
* @see Menu | ||
* @see SlotGroup | ||
* @see MenuLayoutBuilder | ||
* | ||
*/ | ||
public interface MenuLayout { | ||
|
||
/** | ||
* This returns all defined {@link SlotGroup}s for this | ||
* {@link MenuLayout}. | ||
* | ||
* @return A {@link Set} containing every {@link SlotGroup} | ||
*/ | ||
@Nonnull | ||
Set<SlotGroup> getSlotGroups(); | ||
|
||
/** | ||
* This returns the size of the resulting {@link Inventory}. | ||
* | ||
* @return The {@link Inventory} size | ||
*/ | ||
int getSize(); | ||
|
||
/** | ||
* This returns the title to be set for the resulting {@link Menu}. | ||
* If no title was set, this will return null. | ||
* | ||
* @return The title or null | ||
*/ | ||
@Nullable | ||
String getTitle(); | ||
|
||
/** | ||
* This returns the {@link SlotGroup} with the given identifier. | ||
* <p> | ||
* If no corresponding {@link SlotGroup} was found, it will throw an | ||
* {@link IllegalArgumentException}. | ||
* | ||
* @param identifier | ||
* The unique identifier for this {@link SlotGroup}. | ||
* | ||
* @return The corresponding {@link SlotGroup} | ||
*/ | ||
@Nonnull | ||
SlotGroup getGroup(char identifier); | ||
|
||
/** | ||
* This returns the {@link SlotGroup} present at the given slot. | ||
* <p> | ||
* If no corresponding {@link SlotGroup} was found, it will throw an | ||
* {@link IllegalArgumentException}. | ||
* | ||
* @param slot | ||
* The slot | ||
* | ||
* @return The corresponding {@link SlotGroup} | ||
*/ | ||
@Nonnull | ||
SlotGroup getGroup(int slot); | ||
|
||
/** | ||
* This returns the {@link SlotGroup} with the given name. | ||
* <p> | ||
* If no corresponding {@link SlotGroup} was found, it will throw an | ||
* {@link IllegalArgumentException}. | ||
* | ||
* @param name | ||
* The unique name of this {@link SlotGroup}. | ||
* | ||
* @return The corresponding {@link SlotGroup} | ||
*/ | ||
@Nonnull | ||
SlotGroup getGroup(@Nonnull String name); | ||
|
||
} |
102 changes: 102 additions & 0 deletions
102
dough-inventories/src/main/java/io/github/bakedlibs/dough/inventory/SlotGroup.java
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,102 @@ | ||
package io.github.bakedlibs.dough.inventory; | ||
|
||
import java.util.Iterator; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.Inventory; | ||
import org.bukkit.inventory.ItemStack; | ||
|
||
import io.github.bakedlibs.dough.inventory.builders.SlotGroupBuilder; | ||
import io.github.bakedlibs.dough.inventory.handlers.MenuClickHandler; | ||
|
||
/** | ||
* A {@link SlotGroup} groups slots together and divides an {@link Inventory} | ||
* into distinct regions which can be used for easy access. | ||
* | ||
* @author TheBusyBiscuit | ||
* | ||
* @see MenuLayout | ||
* @see SlotGroupBuilder | ||
* | ||
*/ | ||
public interface SlotGroup extends Iterable<Integer> { | ||
|
||
/** | ||
* This returns the unique identifier for this {@link SlotGroup}, | ||
* we use a {@link Character} for this. | ||
* | ||
* @return The unique identifier of this {@link SlotGroup} | ||
*/ | ||
@Nonnull | ||
char getIdentifier(); | ||
|
||
/** | ||
* This method returns whether this {@link SlotGroup} is interactable. | ||
* An interactable {@link SlotGroup} allows the {@link Player} to take | ||
* and store items from/in slots of this group. | ||
* <p> | ||
* If {@link #isInteractable()} returns <code>false</code>, the click event will | ||
* be cancelled. | ||
* | ||
* @return Whether this {@link SlotGroup} is interactable | ||
*/ | ||
boolean isInteractable(); | ||
|
||
/** | ||
* This returns the name or label of this {@link SlotGroup}, names help | ||
* to make a {@link SlotGroup} easier to identify instead of relying on the | ||
* {@link Character} identifier all the time. | ||
* | ||
* @return The name of this {@link SlotGroup} | ||
*/ | ||
@Nonnull | ||
String getName(); | ||
|
||
/** | ||
* This method returns an array containing all the individual slots of | ||
* this {@link SlotGroup}. | ||
* | ||
* @return An array with all slots of this {@link SlotGroup} | ||
*/ | ||
@Nonnull | ||
int[] getSlots(); | ||
|
||
/** | ||
* This returns the size of this {@link SlotGroup}. | ||
* | ||
* @return The size of this {@link SlotGroup} | ||
*/ | ||
default int size() { | ||
return getSlots().length; | ||
} | ||
|
||
/** | ||
* This returns the default {@link ItemStack} for this {@link SlotGroup}. | ||
* | ||
* @return The default {@link ItemStack}, can be null | ||
*/ | ||
@Nullable | ||
ItemStack getDefaultItemStack(); | ||
|
||
/** | ||
* This method returns the {@link MenuClickHandler} belonging to this {@link SlotGroup}. | ||
* If no {@link MenuClickHandler} was added, this will return null. | ||
* | ||
* @return The {@link MenuClickHandler} or null | ||
*/ | ||
@Nullable | ||
MenuClickHandler getClickHandler(); | ||
|
||
/** | ||
* This returns an {@link Iterator} allowing you to iterate through | ||
* all slots within this {@link SlotGroup}. | ||
*/ | ||
@Override | ||
default @Nonnull Iterator<Integer> iterator() { | ||
return new SlotGroupIterator(this); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
dough-inventories/src/main/java/io/github/bakedlibs/dough/inventory/SlotGroupIterator.java
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,51 @@ | ||
package io.github.bakedlibs.dough.inventory; | ||
|
||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* This {@link Iterator} implementation iterates through all slots within a {@link SlotGroup}. | ||
* | ||
* @author TheBusyBiscuit | ||
* | ||
*/ | ||
class SlotGroupIterator implements Iterator<Integer> { | ||
|
||
private final int[] slots; | ||
private int index = 0; | ||
|
||
/** | ||
* This creates a new {@link SlotGroupIterator} for the given | ||
* {@link SlotGroup}. | ||
* | ||
* @param slotGroup | ||
* The {@link SlotGroup} | ||
*/ | ||
SlotGroupIterator(@Nonnull SlotGroup slotGroup) { | ||
this.slots = slotGroup.getSlots(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return index < slots.length; | ||
} | ||
|
||
@Override | ||
public Integer next() { | ||
if (index < slots.length) { | ||
int slot = slots[index]; | ||
md5sha256 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
index++; | ||
return slot; | ||
} else { | ||
throw new NoSuchElementException("No more slots available."); | ||
} | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
throw new UnsupportedOperationException("Iterator#remove() is not supported!"); | ||
} | ||
|
||
} |
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.