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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
More documentation
  • Loading branch information
TheBusyBiscuit committed Jul 15, 2021
commit fea4f35be69fbea5f11a672344851663e20ce14d
Original file line number Diff line number Diff line change
@@ -9,15 +9,29 @@

import org.apache.commons.lang.Validate;

import io.github.bakedlibs.dough.inventory.Menu;
import io.github.bakedlibs.dough.inventory.MenuLayout;
import io.github.bakedlibs.dough.inventory.SlotGroup;

/**
* The {@link MenuLayoutBuilder} allows you to construct a {@link MenuLayout}
* easily via the builder pattern.
*
* @author TheBusyBiscuit
*
*/
public class MenuLayoutBuilder {

protected final int size;
protected final Set<SlotGroup> groups = new HashSet<>();
protected String title;

/**
* This creates a new {@link MenuLayoutBuilder} with the given inventory size.
*
* @param size
* The inventory size for this {@link MenuLayout}
*/
public MenuLayoutBuilder(int size) {
Validate.isTrue(size > 0, "The size must be greater than 0.");
Validate.isTrue(size % 9 == 0, "The size must be a multiple of 9.");
@@ -26,17 +40,38 @@ public MenuLayoutBuilder(int size) {
this.size = size;
}

/**
* This sets an optional title for the resulting {@link Menu}.
*
* @param title
* The title or null
*
* @return Our {@link MenuLayoutBuilder} instance
*/
public @Nonnull MenuLayoutBuilder title(@Nullable String title) {
this.title = title;
return this;
}

/**
* This adds the given {@link SlotGroup} to this {@link MenuLayout}.
*
* @param group
* The {@link SlotGroup} to add
*
* @return Our {@link MenuLayoutBuilder} instance
*/
@ParametersAreNonnullByDefault
public @Nonnull MenuLayoutBuilder addSlotGroup(SlotGroup group) {
groups.add(group);
return this;
}

/**
* This creates the final {@link MenuLayout} object from this {@link MenuLayoutBuilder}.
*
* @return The resulting {@link MenuLayout}
*/
public @Nonnull MenuLayout build() {
Validate.notEmpty(groups, "There are no SlotGroups defined.");

Original file line number Diff line number Diff line change
@@ -61,21 +61,33 @@ class MenuLayoutImpl implements MenuLayout {
}
}

/**
* {@inheritDoc}
*/
@Override
public @Nonnull Set<SlotGroup> getSlotGroups() {
return Collections.unmodifiableSet(groups);
}

/**
* {@inheritDoc}
*/
@Override
public int getSize() {
return size;
}

/**
* {@inheritDoc}
*/
@Override
public @Nullable String getTitle() {
return title;
}

/**
* {@inheritDoc}
*/
@Override
public @Nonnull SlotGroup getGroup(char identifier) {
SlotGroup result = findGroup(group -> group.getIdentifier() == identifier);
@@ -87,6 +99,9 @@ public int getSize() {
}
}

/**
* {@inheritDoc}
*/
@Override
public @Nonnull SlotGroup getGroup(int slot) {
Validate.isTrue(slot >= 0, "Slot cannot be a negative number: " + slot);
@@ -100,6 +115,9 @@ public int getSize() {
return groupsBySlot[slot];
}

/**
* {@inheritDoc}
*/
@Override
public @Nonnull SlotGroup getGroup(@Nonnull String name) {
SlotGroup result = findGroup(group -> group.getName().equals(name));
Original file line number Diff line number Diff line change
@@ -28,6 +28,14 @@ class SlotGroupImpl implements SlotGroup {
this.clickHandler = builder.clickHandler;
}

/**
* This method converts our {@link Set} of slots into a sorted array.
*
* @param slots
* The slots
*
* @return A sorted array of our slots
*/
private @Nonnull int[] convertSlots(@Nonnull Set<Integer> slots) {
// @formatter:off
return slots.stream()
@@ -37,31 +45,49 @@ class SlotGroupImpl implements SlotGroup {
// @formatter:on
}

/**
* {@inheritDoc}
*/
@Override
public char getIdentifier() {
return identifier;
}

/**
* {@inheritDoc}
*/
@Override
public boolean isInteractable() {
return interactable;
}

/**
* {@inheritDoc}
*/
@Override
public @Nonnull String getName() {
return name;
}

/**
* {@inheritDoc}
*/
@Override
public @Nonnull int[] getSlots() {
return slots;
}

/**
* {@inheritDoc}
*/
@Override
public @Nullable ItemStack getDefaultItemStack() {
return defaultItem;
}

/**
* {@inheritDoc}
*/
@Override
public @Nonnull MenuClickHandler getClickHandler() {
return clickHandler;
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
import io.github.bakedlibs.dough.inventory.Menu;
import io.github.bakedlibs.dough.inventory.SlotGroup;
import io.github.bakedlibs.dough.inventory.handlers.MenuClickHandler;
import io.github.bakedlibs.dough.inventory.payloads.MenuEventPayloads;
import io.github.bakedlibs.dough.inventory.payloads.MenuPayloads;

/**
* The {@link MenuListener} is responsible for handling any
@@ -86,7 +86,7 @@ public void onClick(InventoryClickEvent e) {
MenuClickHandler clickHandler = slotGroup.getClickHandler();

if (clickHandler != null) {
clickHandler.onClick(MenuEventPayloads.create(inv, e));
clickHandler.onClick(MenuPayloads.create(inv, e));
}
}
} catch (Exception | LinkageError x) {
Original file line number Diff line number Diff line change
@@ -26,6 +26,13 @@
import io.github.bakedlibs.dough.inventory.builders.MenuLayoutBuilder;
import io.github.bakedlibs.dough.inventory.builders.SlotGroupBuilder;

/**
* This class allows you to parse {@link JsonObject}s, {@link String}s or {@link InputStream}s
* into a {@link MenuLayout}, given they follow our format.
*
* @author TheBusyBiscuit
*
*/
public class LayoutParser {

private LayoutParser() {}
Original file line number Diff line number Diff line change
@@ -9,7 +9,17 @@
import javax.annotation.ParametersAreNonnullByDefault;

import org.apache.commons.lang.Validate;
import org.bukkit.inventory.Inventory;

import io.github.bakedlibs.dough.inventory.SlotGroup;

/**
* Little helper class to transition a {@link String} array into
* a {@link SlotGroup}-like map.
*
* @author TheBusyBiscuit
*
*/
class LayoutShape {

private final Map<Character, Set<Integer>> groups = new HashMap<>();
@@ -36,10 +46,22 @@ class LayoutShape {
}
}

/**
* This returns the size of this {@link LayoutShape}, aka the size of the
* corresponding {@link Inventory}.
*
* @return The size of this {@link LayoutShape}
*/
public int getSize() {
return size;
}

/**
* This returns the {@link SlotGroup}-ready representation of this shape.
* Grouped by their unique character keys.
*
* @return The "{@link SlotGroup} map".
*/
public @Nonnull Map<Character, Set<Integer>> getGroups() {
return groups;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.github.bakedlibs.dough.inventory.payloads;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.apache.commons.lang.Validate;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;

import io.github.bakedlibs.dough.inventory.Menu;
import io.github.bakedlibs.dough.inventory.handlers.MenuClickHandler;

/**
* Utility class for constructing an event payload for menu handlers.
*
* @author TheBusyBiscuit
*
*/
public class MenuPayloads {

private MenuPayloads() {}

/**
* This creates our payload for an {@link InventoryClickEvent}.
*
* @param menu
* The {@link Menu} involved in this event.
* @param e
* The {@link InventoryClickEvent}
*
* @return A {@link MenuClickPayload} to pass onto the {@link MenuClickHandler}.
*/
@ParametersAreNonnullByDefault
public static @Nonnull MenuClickPayload create(Menu menu, InventoryClickEvent e) {
Validate.notNull(menu, "The menu cannot be null");
Validate.notNull(e, "Cannot create a payload for an event that is null");

Player player = (Player) e.getWhoClicked();
int slot = e.getSlot();

return new MenuClickPayload(menu, player, slot);
}

}
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ public MockMenuFactory() {
super(MockBukkit.loadWith(MockJavaPlugin.class, new PluginDescriptionFile(
"MockPlugin",
"1.0.0",
"io.github.thebusybiscuit.dough.inventory.MockInventoryFactory.MockJavaPlugin")
MockJavaPlugin.class.getName())
));
// @formatter:on
}