Skip to content

Commit

Permalink
move setItem and open to GUIHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Feb 24, 2025
1 parent fedb067 commit abb28b9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
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;

import java.util.UUID;

Expand Down Expand Up @@ -66,17 +62,4 @@ public int getSlot(int x, int y) {
}
return x + y * slotPerRow;
}

@Override
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 @@ -4,9 +4,14 @@
import me.hsgamer.hscore.bukkit.gui.common.event.BukkitDragEvent;
import me.hsgamer.hscore.bukkit.gui.common.inventory.BukkitInventoryContext;
import me.hsgamer.hscore.minecraft.gui.common.event.ClickEvent;
import me.hsgamer.hscore.minecraft.gui.common.item.ActionItem;
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;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;
import java.util.function.Function;
Expand Down Expand Up @@ -53,6 +58,20 @@ protected BukkitInventoryContext createInventoryContext() {
return new BukkitInventoryContext(getViewerID(), inventoryFunction.apply(this));
}

@Override
protected void setItem(int slot, @Nullable ActionItem item) {
Object i = item != null ? item.getItem() : null;
getInventoryContext().getInventory().setItem(slot, i instanceof ItemStack ? (ItemStack) i : null);
}

@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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package me.hsgamer.hscore.minecraft.gui.common.inventory;

import org.jetbrains.annotations.Nullable;

import java.util.UUID;

/**
Expand Down Expand Up @@ -32,21 +30,6 @@ public interface InventoryContext {
*/
int getSlot(int x, int y);

/**
* Get the item in the slot
*
* @param slot the slot
* @param object the object
*/
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 @@ -57,20 +40,4 @@ public interface InventoryContext {
default int getSlot(InventoryPosition position) {
return getSlot(position.getX(), position.getY());
}

/**
* Remove the item in the slot
*
* @param slot the slot
*/
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
Expand Up @@ -43,6 +43,14 @@ public GUIHolder(UUID viewerID) {
*/
protected abstract T createInventoryContext();

/**
* Set the item in the slot
*
* @param slot the slot
* @param item the item
*/
protected abstract void setItem(int slot, @Nullable ActionItem item);

/**
* Check if the item can be set in the slot
*
Expand All @@ -54,6 +62,13 @@ protected boolean canSetItem(int slot) {
return slot >= 0 && slot < getInventoryContext().getSize();
}

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

/**
* Handle the open event. Override this method to add custom behavior.
*
Expand Down Expand Up @@ -89,15 +104,15 @@ public void update() {
for (int slot : oldMap.keySet()) {
if (newMap != null && newMap.containsKey(slot)) continue;
if (!canSetItem(slot)) continue;
getInventoryContext().removeItem(slot);
this.setItem(slot, null);
}
}

if (newMap != null) {
for (Map.Entry<Integer, ActionItem> entry : newMap.entrySet()) {
int slot = entry.getKey();
if (!canSetItem(slot)) continue;
getInventoryContext().setItem(slot, entry.getValue().getItem());
this.setItem(slot, entry.getValue());
}
}

Expand Down Expand Up @@ -176,19 +191,10 @@ public void setButtonMap(Function<@NotNull InventoryContext, @Nullable Map<Integ
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();
open(viewerID);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package me.hsgamer.hscore.minestom.gui;

import me.hsgamer.hscore.minecraft.gui.common.item.ActionItem;
import me.hsgamer.hscore.minecraft.gui.holder.GUIHolder;
import me.hsgamer.hscore.minestom.gui.event.MinestomClickEvent;
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;
import net.minestom.server.event.inventory.InventoryOpenEvent;
import net.minestom.server.event.inventory.InventoryPreClickEvent;
import net.minestom.server.event.trait.InventoryEvent;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.UUID;
Expand Down Expand Up @@ -51,6 +55,20 @@ protected MinestomInventoryContext createInventoryContext() {
return new MinestomInventoryContext(getViewerID(), inventoryFunction.apply(getViewerID()));
}

@Override
protected void setItem(int slot, @Nullable ActionItem item) {
Object i = item != null ? item.getItem() : null;
getInventoryContext().getInventory().setItemStack(slot, i instanceof ItemStack ? (ItemStack) i : ItemStack.AIR);
}

@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,11 +1,7 @@
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;

import java.util.UUID;

Expand Down Expand Up @@ -55,17 +51,4 @@ public int getSlot(int x, int y) {
};
return x + y * slotPerRow;
}

@Override
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 abb28b9

Please sign in to comment.