まず、これは紛いもない某ゲーム (Annihilationって言うんですけど) の再現をしようとしているわけである。
エンチャントテーブルの設定をしたい!
まずInventoryOpenEvent
というイベントを使う。同時に、InventoryCloseEvent
を定義すると良い感じになる。
@EventHandler
public void onInventoryOpen(InventoryOpenEvent e) {
if (e.getInventory().getType() == InventoryType.ENCHANTING) {
EnchantingInventory inv = (EnchantingInventory) e.getInventory();
inv.setItem(1, new ItemStack(Material.LAPIS_LAZULI, 64));
}
}
↑
正直これだけでいいが、これだとインベントリを閉じた時にラピスラズリがプレイヤーのインベントリに入ってしまう。
なので、以下を追加する。
@EventHandler
public void onInventoryCloseEvent(InventoryCloseEvent e){
if (e.getInventory().getType() == InventoryType.ENCHANTING) {
EnchantingInventory inv = (EnchantingInventory) e.getInventory();
inv.setItem(1, new ItemStack(Material.AIR, 1));
}
}
これで閉じる時にair
に置き換え、ラピスラズリの削除を可能にしている。
某ゲーム (Annihilation) を完全に再現するなら
あのゲームにおいて、ラピスラズリが取れちゃうなんてことも避けたい(正直、そんなに実害はないが)。
なので、InventoryType.ENCHANTING
に絞ってイベントをsetCancelled(true);
してしまえば完了。
@EventHandler
public void onInventoryClickEvent(InventoryClickEvent e) {
if(e.getInventory().getType() == InventoryType.ENCHANTING){
if(e.getCurrentItem() == null) return;
if(Objects.requireNonNull(e.getCurrentItem()).getType() == Material.LAPIS_LAZULI) {
e.setCancelled(true);
}
}
}