Skip to content

Commit d8940ee

Browse files
committed
チェスト増殖できるバグを修正 & チェスト破壊時にドロップするように
1 parent 4b036e7 commit d8940ee

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/main/java/com/github/elic0de/hungergames/chest/DeathChest.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.util.HashMap;
1212
import java.util.Map;
13+
import java.util.Optional;
1314

1415
public class DeathChest {
1516

@@ -22,10 +23,18 @@ public void generateChest(GameUser user) {
2223
}
2324

2425
public void openDeathChest(Player player, Block block) {
25-
if (chestLocations.containsKey(block)) {
26-
final ItemStack[] contents = chestLocations.get(block);
27-
new DeathChestMenu(contents, player).show();
28-
}
26+
if (getChestContents(block).isPresent()) new DeathChestMenu(getChestContents(block).get(), itemStacks -> updateChestContents(block, itemStacks), player).show();
27+
}
28+
29+
public void breakDeathChest(Block block) {
30+
final Location location = block.getLocation();
31+
getChestContents(block).ifPresent(itemStacks -> {
32+
for (ItemStack item : itemStacks) {
33+
if (item == null) continue;
34+
location.getWorld().dropItemNaturally(location, item);
35+
}
36+
chestLocations.remove(block);
37+
});
2938
}
3039

3140
public void reset() {
@@ -43,6 +52,13 @@ public void updateChestContents(Block block, ItemStack[] contents) {
4352
if (chestLocations.containsKey(block)) chestLocations.put(block, contents);
4453
}
4554

55+
public Optional<ItemStack[]> getChestContents(Block block) {
56+
if (chestLocations.containsKey(block)) {
57+
return Optional.of(chestLocations.get(block));
58+
}
59+
return Optional.empty();
60+
}
61+
4662
public boolean containsDeathChest(Block block) {
4763
return chestLocations.containsKey(block);
4864
}

src/main/java/com/github/elic0de/hungergames/menu/DeathChestMenu.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.elic0de.hungergames.menu;
22

33
import com.github.elic0de.hungergames.HungerGames;
4+
import com.github.elic0de.hungergames.chest.DeathChest;
45
import de.themoep.inventorygui.GuiStorageElement;
56
import de.themoep.inventorygui.InventoryGui;
67
import org.bukkit.Bukkit;
@@ -9,14 +10,16 @@
910
import org.bukkit.inventory.Inventory;
1011
import org.bukkit.inventory.ItemStack;
1112

12-
import java.util.Optional;
13+
import java.util.function.Consumer;
1314

1415
public class DeathChestMenu {
1516

1617
private final InventoryGui menu;
1718

1819
private final Player player;
1920

21+
private final DeathChest deathChest = HungerGames.getInstance().getGame().getDeathChest();
22+
2023
private static final String[] MENU_LAYOUT = {
2124
"ppppppppp",
2225
"ppppppppp",
@@ -25,17 +28,15 @@ public class DeathChestMenu {
2528
"ppppppppp",
2629
"ppppppppp"
2730
};
28-
public DeathChestMenu(ItemStack[] contents, Player player) {
31+
32+
public DeathChestMenu(ItemStack[] itemStacks, Consumer<ItemStack[]> items, Player player) {
2933
this.player = player;
3034
this.menu = new InventoryGui(HungerGames.getInstance(), "DeathChest", MENU_LAYOUT);
3135
Inventory inv = Bukkit.createInventory(null, 54);
32-
inv.setContents(contents);
36+
inv.setContents(itemStacks);
3337
menu.addElement(new GuiStorageElement('p', inv));
3438
menu.setCloseAction(close -> {
35-
Block block = inv.getLocation().getBlock();
36-
if (block != null) {
37-
HungerGames.getInstance().getGame().getDeathChest().updateChestContents(block, inv.getContents());
38-
}
39+
items.accept(inv.getContents());
3940
return false;
4041
});
4142
}

0 commit comments

Comments
 (0)