0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MinecraftのPaperPluginでエンチャントテーブルに常にラピスラズリを配置する方法

Posted at

まず、これは紛いもない某ゲーム (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);
            }
        }
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?