Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复BlockMenuPreset 设置大小后最后一个物品丢失的bug 整改迁移大小的方法 #855

Merged
Merged
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
Expand Up @@ -11,6 +11,7 @@
import javax.annotation.Nonnull;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
Expand All @@ -28,6 +29,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 +54,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 @@ -116,14 +123,9 @@ public ChestMenu addPlayerInventoryClickHandler(MenuClickHandler handler) {
* @return The ChestMenu Instance
*/
public ChestMenu addItem(int slot, ItemStack item) {
final int size = this.items.size();
if (size > slot) this.items.set(slot, item);
else {
for (int i = 0; i < slot - size; i++) {
this.items.add(null);
}
this.items.add(item);
}
setSize((int) (Math.max(getSize(), Math.ceil((slot + 1) / 9d) * 9)));
this.items.set(slot, item);
this.inventory.setItem(slot, item);
return this;
}

Expand All @@ -150,6 +152,9 @@ public ChestMenu addItem(int slot, ItemStack item, MenuClickHandler clickHandler
*/
public ItemStack getItemInSlot(int slot) {
setup();
if (items.size() - 1 < slot) {
addItem(slot, new ItemStack(Material.AIR));
}
return this.inventory.getItem(slot);
}

Expand Down Expand Up @@ -225,7 +230,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 @@ -235,8 +240,10 @@ private void setup() {
* Resets this ChestMenu to a Point BEFORE the User interacted with it
*/
public void reset(boolean update) {
if (this.inventory == null || this.inventory.getSize() != getSize())
this.inventory = Bukkit.createInventory(this, getSize(), title);
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 All @@ -250,6 +257,8 @@ public void reset(boolean update) {
*/
public void replaceExistingItem(int slot, ItemStack item) {
setup();
setSize((int) (Math.max(getSize(), Math.ceil((slot + 1) / 9d) * 9)));
this.items.set(slot, item);
this.inventory.setItem(slot, item);
}

Expand Down Expand Up @@ -315,6 +324,36 @@ public Inventory toInventory() {
return this.inventory;
}

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

public ChestMenu setSize(int size) {
if (size % 9 == 0 && size >= 0 && size < 55) {
if (size > items.size()) {
while (items.size() < size) {
this.items.add(null);
}
} else if (size < items.size()) {
while (items.size() > size) {
this.items.remove(items.size() - 1);
}
} else return this;
this.size = size;
reset(false);
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,14 +195,14 @@ public Set<Integer> getInventorySlots() {
protected void clone(@Nonnull DirtyChestMenu menu) {
menu.setPlayerInventoryClickable(true);

if (isSizeAutomaticallyInferred()) {
menu.addItem(getSize() - 1, null);
} else menu.setSize(getSize());

for (int slot : occupiedSlots) {
menu.addItem(slot, getItemInSlot(slot));
}

if (size > -1) {
menu.addItem(size - 1, null);
}

if (menu instanceof BlockMenu blockMenu) {
newInstance(blockMenu, blockMenu.getLocation());
}
Expand Down
Loading