Skip to content

Commit 52fb55b

Browse files
[1.13.7.0]新增InventoryHelper,用于提供一些容器相关的便捷方法,修复StoredMenu刷新页面时会将放入的物品删除的bug
1 parent 6c25029 commit 52fb55b

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
java.sourceCompatibility = JavaVersion.VERSION_1_8
22
java.targetCompatibility = JavaVersion.VERSION_1_8
33
rootProject.group = "com.crypticlib"
4-
rootProject.version = "1.13.6.0"
4+
rootProject.version = "1.13.7.0"
55
//全项目重构时更新大版本号
66
//添加模块或有较大更改时更新次版本号
77
//有API变动(新增/删除/更改声明)时更新修订号

module/bukkit/ui/src/main/java/crypticlib/ui/menu/Menu.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ public void onLayoutUpdated() {}
172172
*/
173173
public void updateMenuIcons() {
174174
if (inventoryCache != null) {
175-
inventoryCache.clear();
175+
//只把有按钮的槽位清空
176+
slotMap.forEach((slot, icon) -> {
177+
inventoryCache.setItem(slot, new ItemStack(Material.AIR));
178+
});
176179
draw(inventoryCache);
177180
}
178181
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package crypticlib.util;
2+
3+
import org.bukkit.Location;
4+
import org.bukkit.inventory.Inventory;
5+
import org.bukkit.inventory.ItemStack;
6+
7+
import java.util.HashMap;
8+
9+
public class InventoryHelper {
10+
11+
/**
12+
* 将一堆物品放入某个容器中,如果容器放不下则丢在地上
13+
* @param inventory 要添加物品的容器
14+
* @param items 要添加的物品
15+
*/
16+
public static void addItemOrDrop(Inventory inventory, ItemStack... items) {
17+
HashMap<Integer, ItemStack> failed = inventory.addItem(items);
18+
Location location = inventory.getLocation();
19+
if (location == null) {
20+
//如果一个容器没有坐标,那么其大概率为虚拟容器,此时直接不再处理添加失败的物品
21+
return;
22+
}
23+
failed.forEach((_slot, item) -> {
24+
location.getWorld().dropItem(location, item);
25+
});
26+
}
27+
28+
}

0 commit comments

Comments
 (0)