Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package net.theevilreaper.aves.inventory;

import net.theevilreaper.aves.inventory.holder.InventoryHolderImpl;
import net.kyori.adventure.text.Component;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import org.jetbrains.annotations.NotNull;

import java.util.Locale;

/**
* The {@link GlobalInventoryBuilder} builds an inventory which can be used in a global context.
* That means that the inventory is related to all player's on the server and not bound to a single player.
* That means that the inventory is related to all players on the server and not bound to a single player.
*
* @author Patrick Zdarsky / Rxcki
* @version 1.0.0
Expand All @@ -33,7 +31,6 @@ public class GlobalInventoryBuilder extends BaseInventoryBuilderImpl {
public GlobalInventoryBuilder(@NotNull Component title, @NotNull InventoryType type) {
super(type);
this.titleComponent = title;
this.holder = new InventoryHolderImpl(this);
}

/**
Expand Down Expand Up @@ -86,22 +83,20 @@ protected void updateInventory() {
}

/**
* Applies the data layout to the inventory, if the specific layout is set.
* Applies the data layout to the inventory if the specific layout is set.
*/
@Override
protected void applyDataLayout() {
if (getDataLayout() == null) return;
synchronized (this) {
if (getDataLayout() == null) return;
LOGGER.info("Applying data layouts");
LOGGER.debug("Applying data layouts");
ItemStack[] contents = inventory.getItemStacks();
getDataLayout().applyLayout(contents, null);
for (int i = 0; i < contents.length; i++) {
if (contents[i] == null) {
this.inventory.setItemStack(i, ItemStack.AIR);
continue;
ItemStack stack = contents[i];
if (stack != null && !stack.isAir()) {
this.inventory.setItemStack(i, stack);
}
if (contents[i].material() == Material.AIR) continue;
this.inventory.setItemStack(i, contents[i]);
}
this.dataLayoutValid = true;
updateViewer(inventory);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package net.theevilreaper.aves.inventory;

import net.kyori.adventure.text.Component;
import net.minestom.server.item.ItemStack;
import net.theevilreaper.aves.i18n.TextData;
import net.theevilreaper.aves.inventory.holder.InventoryHolderImpl;
import net.kyori.adventure.translation.GlobalTranslator;
import net.minestom.server.entity.Player;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
import net.minestom.server.item.Material;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -91,18 +91,18 @@ protected void updateInventory() {

@Override
protected void applyDataLayout() {
if (getDataLayout() == null) return;
LOGGER.debug("Applying data layout");
synchronized (this) {
if (getDataLayout() != null) {
LOGGER.info("Applying data layout");
for (var entry : inventoryTranslatedObjectCache.entrySet()) {
var contents = entry.getValue().getItemStacks();
getDataLayout().applyLayout(contents, entry.getKey());
for (int i = 0; i < contents.length; i++) {
if (contents[i].material() == Material.AIR) continue;
entry.getValue().setItemStack(i, contents[i]);
entry.getValue().update();
}
for (var entry : inventoryTranslatedObjectCache.entrySet()) {
var contents = entry.getValue().getItemStacks();
getDataLayout().applyLayout(contents, entry.getKey());
for (int i = 0; i < contents.length; i++) {
ItemStack stack = contents[i];
if (stack.isAir()) continue;
entry.getValue().setItemStack(i, stack);
}
entry.getValue().update();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import net.minestom.server.inventory.click.Click;
import net.minestom.server.inventory.click.ClickType;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.theevilreaper.aves.inventory.click.ClickHolder;
import net.theevilreaper.aves.inventory.function.CloseFunction;
import net.theevilreaper.aves.inventory.function.InventoryClick;
import net.theevilreaper.aves.inventory.function.OpenFunction;
import net.theevilreaper.aves.inventory.layout.InventoryLayout;
import net.theevilreaper.aves.inventory.slot.EmptySlot;
import net.theevilreaper.aves.inventory.slot.ISlot;
import net.theevilreaper.aves.inventory.util.InventoryConstants;
Expand All @@ -31,23 +31,25 @@
* The {@link InventoryBuilder} is the base class which contains the necessary methods to create different context implementations for an inventory.
*
* @author Patrick Zdarsky / Rxcki
* @version 1.3.0
* @version 1.3.1
* @since 1.0.12
*/
@SuppressWarnings("java:S3252")
public abstract class InventoryBuilder {

protected static final Logger LOGGER = LoggerFactory.getLogger(InventoryBuilder.class);
protected final InventoryType type;
private InventoryLayout inventoryLayout;
private InventoryLayout dataLayout;
protected boolean inventoryLayoutValid = true;
protected boolean dataLayoutValid = false;
protected volatile boolean inventoryLayoutValid = true;
protected volatile boolean dataLayoutValid = false;
protected volatile boolean dataLayoutPending = false;
protected OpenFunction openFunction;
protected CloseFunction closeFunction;
protected ThrowingFunction<InventoryLayout, InventoryLayout> dataLayoutFunction;
protected InventoryClick inventoryClick;

private InventoryLayout inventoryLayout;
private InventoryLayout dataLayout;

/**
* Creates a new instance from the inventory builder with the given size.
*
Expand Down Expand Up @@ -94,11 +96,7 @@ private void acceptClick(
@NotNull ItemStack stack,
@NotNull Consumer<ClickHolder> result
) {
if (slot == null) {
result.accept(ClickHolder.noClick());
return;
}
if (slot instanceof EmptySlot) {
if (slot == null || slot instanceof EmptySlot) {
result.accept(ClickHolder.noClick());
return;
}
Expand All @@ -108,7 +106,7 @@ private void acceptClick(
/**
* Set a new reference to the data layout
*
* @param dataLayout The {@link InventoryLayoutImpl} to set
* @param dataLayout The {@link InventoryLayout} to set
*/
public void setDataLayoutFunction(ThrowingFunction<InventoryLayout, InventoryLayout> dataLayout) {
this.dataLayoutFunction = dataLayout;
Expand Down Expand Up @@ -167,9 +165,9 @@ public void invalidateLayout() {
public void invalidateDataLayout() {
if (isOpen()) {
retrieveDataLayout();
LOGGER.info("DataLayout invalidated on open inv, requested data...");
LOGGER.debug("DataLayout invalidated on open inv, requested data...");
} else {
LOGGER.info("DataLayout invalidated on closed inv");
LOGGER.debug("DataLayout invalidated on closed inv");
dataLayoutValid = false;
}
}
Expand Down Expand Up @@ -197,39 +195,27 @@ protected void handleClose(@NotNull InventoryCloseEvent event) {
/**
* Updates the given inventory with the content.
*
* @param inventory the inventory which should receive the update
* @param inventory the inventory that should receive the update
* @param locale the locale for the inventory
* @param applyLayout if the layout should be applied
*/
protected void updateInventory(
@NotNull Inventory inventory,
Locale locale,
boolean applyLayout
) {
protected void updateInventory(@NotNull Inventory inventory, Locale locale, boolean applyLayout){
if (!applyLayout) return;
if (this.inventoryLayout == null) {
throw new IllegalStateException("Can't update content because the layout is null");
}

// Design
if (applyLayout) {
var contents = inventory.getItemStacks();
inventory.clear();
getLayout().applyLayout(contents, locale);
this.setItemsInternal(inventory, contents);
LOGGER.info("UpdateInventory applied the InventoryLayout!");
this.inventoryLayoutValid = true;
}
ItemStack[] contents = inventory.getItemStacks();
inventory.clear();
this.inventoryLayout.applyLayout(contents, locale);
this.setItemsInternal(inventory, contents);
LOGGER.debug("UpdateInventory applied the InventoryLayout!");
this.inventoryLayoutValid = true;

// Values
synchronized (this) {
if (!dataLayoutValid) {
retrieveDataLayout();
} else {
if (getDataLayout() != null) {
var contents = inventory.getItemStacks();
getDataLayout().applyLayout(contents, locale);
this.setItemsInternal(inventory, contents);
}
}
}
}
Expand All @@ -238,28 +224,32 @@ protected void updateInventory(
* Set's the given array with the {@link ItemStack}'s into an inventory.
*
* @param inventory the inventory for the items
* @param contents the array itself which contains all items
* @param contents the array itself that contains all items
*/
private void setItemsInternal(@NotNull Inventory inventory, @NotNull ItemStack[] contents) {
for (int i = 0; i < contents.length; i++) {
var contentSlot = contents[i];
if (contentSlot == null || contentSlot.material() == Material.AIR) continue;
inventory.setItemStack(i, contents[i]);
if (contentSlot == null || contentSlot.isAir()) continue;
inventory.setItemStack(i, contentSlot);
}
}

/**
* Executes the logic to retrieve the {@link InventoryLayoutImpl} which comes from the {@link ThrowingFunction}.
* Executes the logic to retrieve the {@link InventoryLayout} which comes from the {@link ThrowingFunction}.
*/
protected void retrieveDataLayout() {
if (this.dataLayoutFunction == null) return;
if (dataLayoutPending) return;
synchronized (this) {
if (this.dataLayoutFunction == null) return;
this.dataLayoutPending = true;
MinecraftServer.getSchedulerManager().scheduleNextTick(() -> {
try {
this.dataLayout = this.dataLayoutFunction.acceptThrows(this.dataLayout);
applyDataLayout();
} catch (Exception exception) {
MinecraftServer.getExceptionManager().handleException(exception);
} finally {
this.dataLayoutPending = false;
}
});
}
Expand Down Expand Up @@ -328,7 +318,7 @@ public InventoryBuilder setCloseFunction(CloseFunction closeFunction) {
}

/**
* Set a new instance of the {@link InventoryLayoutImpl} to the builder
* Set a new instance of the {@link InventoryLayout} to the builder
*
* @param inventoryLayoutImpl The layout to set
* @return the current instance of the builder
Expand All @@ -339,7 +329,7 @@ public InventoryBuilder setLayout(@NotNull InventoryLayout inventoryLayoutImpl)
}

/**
* Returns the underlying {@link InventoryLayoutImpl}.
* Returns the underlying {@link InventoryLayout}.
*
* @return the given layout
*/
Expand All @@ -348,7 +338,7 @@ public InventoryBuilder setLayout(@NotNull InventoryLayout inventoryLayoutImpl)
}

/**
* Get underlying data {@link InventoryLayoutImpl}.
* Get underlying data {@link InventoryLayout}.
*
* @return the given layout
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package net.theevilreaper.aves.inventory.click;

import net.minestom.server.inventory.click.Click;
import org.jetbrains.annotations.NotNull;

/**
* The {@link ClickHolder} interface represents a holder for click actions in the Aves inventory system.
* It can hold different types of clicks, including a Minestom click, a cancel click, or a no-operation click.
* This allows for flexible handling of click actions within the inventory system.
*
* @author theEvilReaper
* @version 1.0.0
* @version 1.0.1
* @since 1.9.0
*/
public sealed interface ClickHolder permits ClickHolder.MinestomClick, ClickHolder.CancelClick, ClickHolder.NOPClick {
Expand All @@ -19,7 +18,7 @@ public sealed interface ClickHolder permits ClickHolder.MinestomClick, ClickHold
*
* @return the no-click reference
*/
static @NotNull ClickHolder noClick() {
static ClickHolder noClick() {
return InternalClickRegistry.NOP_CLICK;
}

Expand All @@ -28,7 +27,7 @@ public sealed interface ClickHolder permits ClickHolder.MinestomClick, ClickHold
*
* @return the cancel-click reference
*/
static @NotNull ClickHolder cancelClick() {
static ClickHolder cancelClick() {
return InternalClickRegistry.CANCEL_CLICK;
}

Expand All @@ -38,7 +37,7 @@ public sealed interface ClickHolder permits ClickHolder.MinestomClick, ClickHold
* @param click the click to wrap
* @return the wrapped click holder
*/
static @NotNull ClickHolder of(@NotNull Click click) {
static ClickHolder of(Click click) {
return new MinestomClick(click);
}

Expand All @@ -50,7 +49,7 @@ public sealed interface ClickHolder permits ClickHolder.MinestomClick, ClickHold
* @version 1.0.0
* @since 1.9.0
*/
record MinestomClick(@NotNull Click click) implements ClickHolder {
record MinestomClick(Click click) implements ClickHolder {
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@NotNullByDefault
package net.theevilreaper.aves.inventory.click;

import org.jetbrains.annotations.NotNullByDefault;
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package net.theevilreaper.aves.inventory.exception;

import org.jetbrains.annotations.NotNull;

/**
* The {@link ListenerStateException} is a custom exception which is thrown when a listener is in an invalid state.
* Its former used in the in inventory listener handling to indicate that the listener is not in a valid state to process an event.
*
* @author theEvilReaper
* @version 1.0.0
* @version 1.0.1
* @since 1.0.12
*/
public final class ListenerStateException extends IllegalStateException {
Expand All @@ -17,7 +15,7 @@ public final class ListenerStateException extends IllegalStateException {
*
* @param message the message for the exception
*/
public ListenerStateException(@NotNull String message) {
public ListenerStateException(String message) {
super(message);
}

Expand All @@ -27,7 +25,7 @@ public ListenerStateException(@NotNull String message) {
* @param message the message for the exception
* @param cause the cause of the exception
*/
public ListenerStateException(@NotNull String message, @NotNull Throwable cause) {
public ListenerStateException(String message, Throwable cause) {
super(message, cause);
}

Expand All @@ -36,7 +34,7 @@ public ListenerStateException(@NotNull String message, @NotNull Throwable cause)
*
* @param cause the cause of the exception
*/
public ListenerStateException(@NotNull Throwable cause) {
public ListenerStateException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@NotNullByDefault
package net.theevilreaper.aves.inventory.exception;

import org.jetbrains.annotations.NotNullByDefault;
Loading