Skip to content

Commit

Permalink
add open in InventoryContext
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Feb 21, 2025
1 parent 945d721 commit fbdb289
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.hsgamer.hscore.bukkit.gui.common.inventory;

import me.hsgamer.hscore.minecraft.gui.common.inventory.InventoryContext;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -69,4 +71,12 @@ public int getSlot(int x, int y) {
public void setItem(int slot, @Nullable Object item) {
inventory.setItem(slot, item instanceof ItemStack ? (ItemStack) item : null);
}

@Override
public void open(UUID uuid) {
Player player = Bukkit.getPlayer(uuid);
if (player != null) {
player.openInventory(inventory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import me.hsgamer.hscore.bukkit.gui.common.inventory.BukkitInventoryContext;
import me.hsgamer.hscore.minecraft.gui.common.event.ClickEvent;
import me.hsgamer.hscore.minecraft.gui.holder.GUIHolder;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;

Expand Down Expand Up @@ -56,19 +54,11 @@ protected BukkitInventoryContext createInventoryContext() {
return new BukkitInventoryContext(getViewerID(), inventory);
}

@Override
public void open(UUID uuid) {
Player player = Bukkit.getPlayer(uuid);
if (player != null) {
player.openInventory(getInventoryContext().getInventory());
}
}

@Override
public void handleClick(ClickEvent event) {
if (moveItemOnBottom && event instanceof BukkitClickEvent) {
InventoryClickEvent clickEvent = ((BukkitClickEvent) event).getEvent();
if (clickEvent.getClickedInventory() != clickEvent.getInventory())
if (clickEvent.getClickedInventory() != clickEvent.getInventory()) {
switch (clickEvent.getAction()) {
case DROP_ALL_SLOT:
case DROP_ONE_SLOT:
Expand All @@ -87,6 +77,8 @@ public void handleClick(ClickEvent event) {
default:
break;
}
return;
}
}
super.handleClick(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public interface InventoryContext {
*/
void setItem(int slot, @Nullable Object object);

/**
* Open the inventory
*
* @param viewerID the unique ID of the viewer
*/
void open(UUID viewerID);

/**
* Get the inventory slot from the position
*
Expand All @@ -59,4 +66,11 @@ default int getSlot(InventoryPosition position) {
default void removeItem(int slot) {
setItem(slot, null);
}

/**
* Open the inventory
*/
default void open() {
open(getViewerID());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.hsgamer.hscore.minecraft.gui.holder;

import me.hsgamer.hscore.minecraft.gui.common.button.ButtonMap;
import me.hsgamer.hscore.minecraft.gui.common.GUIElement;
import me.hsgamer.hscore.minecraft.gui.common.button.ButtonMap;
import me.hsgamer.hscore.minecraft.gui.common.event.ClickEvent;
import me.hsgamer.hscore.minecraft.gui.common.inventory.InventoryContext;
import me.hsgamer.hscore.minecraft.gui.common.item.ActionItem;
Expand Down Expand Up @@ -39,13 +39,6 @@ public GUIHolder(UUID viewerID) {
*/
protected abstract T createInventoryContext();

/**
* Open the inventory
*
* @param uuid the unique ID of the viewer
*/
public abstract void open(UUID uuid);

/**
* Handle the open event. Override this method to add custom behavior.
*
Expand Down Expand Up @@ -100,13 +93,6 @@ public void update() {
});
}

/**
* Open the inventory
*/
public void open() {
open(inventoryContext.getViewerID());
}

@Override
public void init() {
inventoryContext = createInventoryContext();
Expand Down Expand Up @@ -177,4 +163,20 @@ public ButtonMap getButtonMap() {
public void setButtonMap(ButtonMap buttonMap) {
this.buttonMap = buttonMap;
}

/**
* Open the inventory
*
* @param viewerID the unique ID of the player
*/
public void open(UUID viewerID) {
getInventoryContext().open(viewerID);
}

/**
* Open the inventory
*/
public void open() {
getInventoryContext().open();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import me.hsgamer.hscore.minestom.gui.event.MinestomCloseEvent;
import me.hsgamer.hscore.minestom.gui.event.MinestomOpenEvent;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player;
import net.minestom.server.event.EventFilter;
import net.minestom.server.event.EventNode;
import net.minestom.server.event.inventory.InventoryCloseEvent;
Expand Down Expand Up @@ -52,14 +51,6 @@ protected MinestomInventoryContext createInventoryContext() {
return new MinestomInventoryContext(getViewerID(), inventoryFunction.apply(getViewerID()));
}

@Override
public void open(UUID uuid) {
Player player = MinecraftServer.getConnectionManager().getOnlinePlayerByUuid(uuid);
if (player != null) {
player.openInventory(getInventoryContext().getInventory());
}
}

@Override
public void init() {
super.init();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.hsgamer.hscore.minestom.gui;

import me.hsgamer.hscore.minecraft.gui.common.inventory.InventoryContext;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -58,4 +60,12 @@ public int getSlot(int x, int y) {
public void setItem(int slot, @Nullable Object item) {
inventory.setItemStack(slot, item instanceof ItemStack itemStack ? itemStack : ItemStack.AIR);
}

@Override
public void open(UUID uuid) {
Player player = MinecraftServer.getConnectionManager().getOnlinePlayerByUuid(uuid);
if (player != null) {
player.openInventory(inventory);
}
}
}

0 comments on commit fbdb289

Please sign in to comment.