Skip to content

Commit

Permalink
修复BlockMenuPreset 设置大小后最后一个物品丢失的bug 整改迁移大小的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
JWJUN233233 committed Apr 20, 2024
1 parent d549937 commit e178bf3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class ChestMenu extends SlimefunInventoryHolder {
private boolean emptyClickable;
private String title;
private List<ItemStack> items;
private int size = -1;
private Map<Integer, MenuClickHandler> handlers;
private MenuOpeningHandler open;
private MenuCloseHandler close;
Expand All @@ -52,6 +53,11 @@ public ChestMenu(String title) {
this.playerclick = (p, slot, item, action) -> isPlayerInventoryClickable();
}

public ChestMenu(String title, int size) {
this(title);
setSize(size);
}

/**
* Toggles whether Players can access there
* Inventory while viewing this Menu
Expand Down Expand Up @@ -117,6 +123,9 @@ public ChestMenu addPlayerInventoryClickHandler(MenuClickHandler handler) {
*/
public ChestMenu addItem(int slot, ItemStack item) {
final int size = this.items.size();
if (this.size != -1 && this.size < size) {
setSize((int) (Math.ceil(size / 9d) * 9));
}
if (size > slot) this.items.set(slot, item);
else {
for (int i = 0; i < slot - size; i++) {
Expand Down Expand Up @@ -225,7 +234,7 @@ public boolean contains(@Nonnull Player viewer) {

private void setup() {
if (this.inventory != null) return;
this.inventory = Bukkit.createInventory(this, ((int) Math.ceil(this.items.size() / 9F)) * 9, title);
this.inventory = Bukkit.createInventory(this, getSize(), title);
for (int i = 0; i < this.items.size(); i++) {
this.inventory.setItem(i, this.items.get(i));
}
Expand All @@ -236,7 +245,7 @@ private void setup() {
*/
public void reset(boolean update) {
if (update) this.inventory.clear();
else this.inventory = Bukkit.createInventory(this, ((int) Math.ceil(this.items.size() / 9F)) * 9, title);
else this.inventory = Bukkit.createInventory(this, getSize(), title);
for (int i = 0; i < this.items.size(); i++) {
this.inventory.setItem(i, this.items.get(i));
}
Expand Down Expand Up @@ -315,6 +324,30 @@ public Inventory toInventory() {
return this.inventory;
}

public int getSize() {
return isSizeAutomaticallyInferred() ? ((int) Math.ceil(this.items.size() / 9F)) * 9 : size;
}

public ChestMenu setSize(int size) {
if (size % 9 == 0 && size >= 0 && size < 55) {
for (int i = 0; i < size - items.size(); i++) {
this.items.add(null);
}
this.size = size;
reset(true);
return this;
} else {
throw new IllegalArgumentException(
"The size of a ChestMenu must be a multiple of 9 and within the bounds 0-54,"
+ " received: "
+ size);
}
}

public boolean isSizeAutomaticallyInferred() {
return size == -1;
}

@FunctionalInterface
public interface MenuClickHandler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public abstract class BlockMenuPreset extends ChestMenu {
private final String inventoryTitle;
private final String id;

// -1 means "automatically update according to the contents"
private int size = -1;

private boolean locked;

protected BlockMenuPreset(@Nonnull String id, @Nonnull String title) {
Expand Down Expand Up @@ -146,15 +143,7 @@ public ChestMenu addMenuClickHandler(int slot, MenuClickHandler handler) {
public ChestMenu setSize(int size) {
checkIfLocked();

if (size % 9 == 0 && size >= 0 && size < 55) {
this.size = size;
return this;
} else {
throw new IllegalArgumentException(
"The size of a BlockMenuPreset must be a multiple of 9 and within the bounds 0-54,"
+ " received: "
+ size);
}
return super.setSize(size);
}

/**
Expand All @@ -164,11 +153,7 @@ public ChestMenu setSize(int size) {
* @return The size of this {@link BlockMenuPreset}
*/
public int getSize() {
return size;
}

private boolean isSizeAutomaticallyInferred() {
return size == -1;
return super.getSize();
}

/**
Expand Down Expand Up @@ -197,7 +182,7 @@ public Set<Integer> getInventorySlots() {
}
}
} else {
for (int i = 0; i < size; i++) {
for (int i = 0; i < getSize(); i++) {
if (!occupiedSlots.contains(i)) {
emptySlots.add(i);
}
Expand All @@ -210,12 +195,12 @@ public Set<Integer> getInventorySlots() {
protected void clone(@Nonnull DirtyChestMenu menu) {
menu.setPlayerInventoryClickable(true);

for (int slot : occupiedSlots) {
menu.addItem(slot, getItemInSlot(slot));
if (isSizeAutomaticallyInferred()) {
menu.addItem(getSize() - 1, null);
}

if (size > -1) {
menu.addItem(size - 1, null);
for (int slot : occupiedSlots) {
menu.addItem(slot, getItemInSlot(slot));
}

if (menu instanceof BlockMenu blockMenu) {
Expand Down

0 comments on commit e178bf3

Please sign in to comment.