Skip to content
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

[1.2.0] New inventory system #46

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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 Jul 12, 2021
6665a1a
Some more progress
TheBusyBiscuit Jul 12, 2021
1fa5b43
Added some unit tests and more progress
TheBusyBiscuit Jul 12, 2021
84621cb
Over 90% coverage :cool:
TheBusyBiscuit Jul 12, 2021
54f23ac
A lot more unit tests and stuff
TheBusyBiscuit Jul 12, 2021
127af54
Finished this part of the module
TheBusyBiscuit Jul 12, 2021
1093e83
Implemented click events and factories
TheBusyBiscuit Jul 13, 2021
d43d1f6
Renamed to `Menu`
TheBusyBiscuit Jul 13, 2021
a18af1b
Detect key and name collisions
TheBusyBiscuit Jul 13, 2021
3164f0f
Allow factories to construct custom menu implementations
TheBusyBiscuit Jul 13, 2021
67b9106
Improved docs and logging
TheBusyBiscuit Jul 13, 2021
0556b86
Implemented layout parsing and added unit tests for it
TheBusyBiscuit Jul 13, 2021
fe9a08a
Added another unit test (just to be sure)
TheBusyBiscuit Jul 13, 2021
9bc5481
Test exception handling
TheBusyBiscuit Jul 13, 2021
b37ecb7
Added item and click event injection
TheBusyBiscuit Jul 14, 2021
6c1dbba
Fixed NullPointerException
TheBusyBiscuit Jul 14, 2021
cd4c6f9
Added some documentation
TheBusyBiscuit Jul 14, 2021
48158d0
Updated package names
TheBusyBiscuit Jul 14, 2021
1fce6ab
Merge branch 'main' into inventory-system
TheBusyBiscuit Jul 14, 2021
b530e01
Removed redundant imports
TheBusyBiscuit Jul 14, 2021
a5a4b1d
Small optimization
TheBusyBiscuit Jul 14, 2021
6053fcd
Merge branch 'main' into inventory-system
TheBusyBiscuit Jul 14, 2021
128c565
Merge branch 'main' of https://github.com/baked-libs/dough into inven…
TheBusyBiscuit Jul 14, 2021
73177c5
Bump version
TheBusyBiscuit Jul 14, 2021
96978d7
Merge branch 'inventory-system' of https://github.com/baked-libs/doug…
TheBusyBiscuit Jul 14, 2021
c5b67a0
Merge branch 'main' of https://github.com/baked-libs/dough into
TheBusyBiscuit Jul 15, 2021
1b6fbd4
Added `SlotGroupBuilder#withSlotRange` and more documentation
TheBusyBiscuit Jul 15, 2021
fea4f35
More documentation
TheBusyBiscuit Jul 15, 2021
e36a8d4
Update dough-inventories/src/main/java/io/github/bakedlibs/dough/inve…
TheBusyBiscuit Jan 1, 2022
44df16a
Merge branch 'main' into inventory-system
TheBusyBiscuit Jan 1, 2022
24874bf
Merge branch 'main' into inventory-system
TheBusyBiscuit Mar 20, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dough-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.baked-libs</groupId>
<artifactId>dough</artifactId>
<version>1.1.3</version>
<version>1.2.0</version>
</parent>

<artifactId>dough-api</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion dough-chat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.baked-libs</groupId>
<artifactId>dough</artifactId>
<version>1.1.3</version>
<version>1.2.0</version>
</parent>

<artifactId>dough-chat</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion dough-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.baked-libs</groupId>
<artifactId>dough</artifactId>
<version>1.1.3</version>
<version>1.2.0</version>
</parent>

<artifactId>dough-common</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion dough-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.baked-libs</groupId>
<artifactId>dough</artifactId>
<version>1.1.3</version>
<version>1.2.0</version>
</parent>

<artifactId>dough-config</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion dough-data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.baked-libs</groupId>
<artifactId>dough</artifactId>
<version>1.1.3</version>
<version>1.2.0</version>
</parent>

<artifactId>dough-data</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion dough-inventories/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.baked-libs</groupId>
<artifactId>dough</artifactId>
<version>1.1.3</version>
<version>1.2.0</version>
</parent>

<artifactId>dough-inventories</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public static boolean fitAll(@Nonnull Inventory inv, @Nonnull ItemStack[] items,
* Whether to replace consumables, e.g. turn potions into glass bottles etc...
* @param predicate
* The Predicate that tests the item
*
* @return Whether the operation was successful
*/
public static boolean removeItem(@Nonnull Inventory inv, int amount, boolean replaceConsumables, @Nonnull Predicate<ItemStack> predicate) {
Expand Down
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);
TheBusyBiscuit marked this conversation as resolved.
Show resolved Hide resolved

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();
}

}
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);

}
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);
}

}
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!");
}

}
Loading