Skip to content

Commit

Permalink
dedicated InventorySize for implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Mar 16, 2024
1 parent 4e84971 commit 321aeb9
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.hsgamer.hscore.bukkit.gui;

import me.hsgamer.hscore.bukkit.gui.object.BukkitInventorySize;
import me.hsgamer.hscore.bukkit.gui.object.BukkitItem;
import me.hsgamer.hscore.minecraft.gui.InventoryGUIDisplay;
import me.hsgamer.hscore.minecraft.gui.object.InventorySize;
Expand Down Expand Up @@ -34,7 +35,7 @@ public BukkitGUIDisplay(UUID uuid, BukkitGUIHolder holder) {
@Override
protected void initInventory() {
this.inventory = holder.getInventoryFunction().apply(this);
this.inventorySize = BukkitGUIUtils.getInventorySize(inventory);
this.inventorySize = new BukkitInventorySize(inventory);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import me.hsgamer.hscore.bukkit.gui.event.BukkitClickEvent;
import me.hsgamer.hscore.bukkit.gui.event.BukkitDragEvent;
import me.hsgamer.hscore.minecraft.gui.object.InventorySize;
import org.bukkit.Bukkit;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
Expand Down Expand Up @@ -117,36 +116,4 @@ public static Function<BukkitGUIDisplay, Inventory> getInventoryFunctionFromTitl
: Bukkit.createInventory(display, type, title);
};
}

/**
* Get the inventory size from the inventory
*
* @param inventory the inventory
*
* @return the inventory size
*/
public static InventorySize getInventorySize(Inventory inventory) {
return new InventorySize() {
@Override
public int getSize() {
return inventory.getSize();
}

@Override
public int getSlotPerRow() {
switch (inventory.getType()) {
case CHEST:
case ENDER_CHEST:
case SHULKER_BOX:
return 9;
case DISPENSER:
case DROPPER:
case HOPPER:
return 3;
default:
return getSize();
}
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package me.hsgamer.hscore.bukkit.gui.object;

import me.hsgamer.hscore.minecraft.gui.object.InventorySize;
import org.bukkit.inventory.Inventory;

/**
* The {@link InventorySize} of {@link Inventory}
*/
public class BukkitInventorySize implements InventorySize {
private final Inventory inventory;

/**
* Create a new instance
*
* @param inventory the inventory
*/
public BukkitInventorySize(Inventory inventory) {
this.inventory = inventory;
}

@Override
public int getSize() {
return inventory.getSize();
}

@Override
public int getSlotPerRow() {
switch (inventory.getType()) {
case CHEST:
case ENDER_CHEST:
case SHULKER_BOX:
return 9;
case DISPENSER:
case DROPPER:
case HOPPER:
return 3;
default:
return getSize();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface InventorySize {
* @return the slot per row
*/
default int getSlotPerRow() {
return 9;
return getSize();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.hsgamer.hscore.minecraft.gui.object.InventorySize;
import me.hsgamer.hscore.minecraft.gui.object.Item;
import me.hsgamer.hscore.minestom.gui.inventory.DelegatingInventory;
import me.hsgamer.hscore.minestom.gui.object.MinestomInventorySize;
import me.hsgamer.hscore.minestom.gui.object.MinestomItem;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player;
Expand Down Expand Up @@ -34,17 +35,7 @@ public MinestomGUIDisplay(@NotNull UUID uuid, @NotNull MinestomGUIHolder holder)
protected void initInventory() {
this.inventory = new DelegatingInventory(holder.getInventoryType(), holder.getTitleFunction().apply(uuid), this);
this.inventory.init();
this.inventorySize = new InventorySize() {
@Override
public int getSize() {
return inventory.getSize();
}

@Override
public int getSlotPerRow() {
return inventory.getSlotPerRow();
}
};
this.inventorySize = new MinestomInventorySize(inventory);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,4 @@ public EventNode<InventoryEvent> getEventNode() {
public MinestomGUIDisplay getDisplay() {
return display;
}

/**
* Get the slot per row of the inventory
*
* @return the slot per row
*/
public int getSlotPerRow() {
switch (getInventoryType()) {
case CHEST_1_ROW, CHEST_2_ROW, CHEST_3_ROW, CHEST_4_ROW, CHEST_5_ROW, CHEST_6_ROW, SHULKER_BOX -> {
return 9;
}
case WINDOW_3X3, CRAFTER_3X3 -> {
return 3;
}
default -> {
return getSize();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.hsgamer.hscore.minestom.gui.object;

import me.hsgamer.hscore.minecraft.gui.object.InventorySize;
import net.minestom.server.inventory.Inventory;

/**
* The {@link InventorySize} of {@link Inventory}
*/
public class MinestomInventorySize implements InventorySize {
private final Inventory inventory;

/**
* Create a new instance
*
* @param inventory the inventory
*/
public MinestomInventorySize(Inventory inventory) {
this.inventory = inventory;
}

@Override
public int getSize() {
return inventory.getSize();
}

@Override
public int getSlotPerRow() {
switch (inventory.getInventoryType()) {
case CHEST_1_ROW, CHEST_2_ROW, CHEST_3_ROW, CHEST_4_ROW, CHEST_5_ROW, CHEST_6_ROW, SHULKER_BOX -> {
return 9;
}
case WINDOW_3X3, CRAFTER_3X3 -> {
return 3;
}
default -> {
return getSize();
}
}
}
}

0 comments on commit 321aeb9

Please sign in to comment.