ayumu koizumi

tntキャノンを追加するプラグイン

レシピはそっちが考えていいですよー モデルを作れますか? アイテムを右クリックするとみている方向にtntを発射、 耐久地 300

Kodari AI
KodariSep 28 at 1:33 pm 1
TntCannonLauncher
Adds a craftable TNT cannon item to Minecraft. Players right-click to launch TNT projectiles in the direction they are facing. The cannon has 300 durability, consuming one durability point per shot. Includes custom crafting recipes for the cannon item. When durability reaches zero, the cannon breaks. Provides a powerful ranged explosive tool for gameplay.
  1. TNTキャノン本体:右クリック発射、300発分の耐久表示、クラフトレシピを実装します。

  2. 専用モデル:カスタムモデルデータ対応のリソースパック用モデルを追加します。

TNTキャノンプラグインと専用モデルを追加しました。

  • ダイヤモンドのクワをベースにしたTNTキャノンを実装。右クリックで視線の方向へTNTを発射し、300回使用すると壊れます。残り耐久はアイテムの説明欄と耐久バーに表示します。

  • レシピ:上段「鉄インゴット・TNT・鉄インゴット」、中段「レッドストーン・火打石と打ち金・レッドストーン」、下段は鉄インゴット3個です。

  • resource-pack/ に専用モデルを用意しました。モデルを表示するには、リソースパックをプレイヤーに適用してください。

  • Paper 1.21.4 / Java 21 向けです。

ayumu koizumi

resource-pack/をどうやってダウンロードしますか?またtntcannonの耐久地を10にし、発射クールダウンを2にします

Kodari AI
KodariSep 28 at 1:58 pm 1
  1. resource-pack/の取得方法を確認し、単体で配布しやすい形にできるか確認します。

  2. TNTキャノンの耐久値を10、発射クールダウンを2秒に変更します。

  1. resource-pack/はプロジェクト内にあります。ファイル一覧からフォルダ内のファイルを保存し、ZIPにまとめてください。ZIPを開いたときにpack.mcmetaが直下にある構成にします(resource-pack/pack.mcmetaのようにフォルダが一段入らないようにしてください)。Minecraftの「リソースパック」画面から「パックフォルダーを開く」でZIPを入れて有効化できます。

  2. TNTキャノンの耐久値を10発、発射クールダウンを**2秒(40 tick)**に変更しました。既存のキャノンも使用時に10発分へ調整されます。更新後のプラグインをサーバーに入れ直してください。

Kodari can make mistakes. Review important code before using.
Files
resource-pack
src
build.gradle
settings.gradle
Code
7 242
src/main/java/com/kodari/tntcannon/TNTCannonPlugin.java
package com.kodari.tntcannon;

import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.List;

public final class TNTCannonPlugin extends JavaPlugin implements Listener {
    private static final int MAX_DURABILITY = 10;
    private static final int FIRE_COOLDOWN_TICKS = 40;
    private static final int CUSTOM_MODEL_DATA = 10001;

    private NamespacedKey cannonKey;
    private NamespacedKey durabilityKey;

    @Override
    public void onEnable() {
        cannonKey = new NamespacedKey(this, "tnt_cannon");
        durabilityKey = new NamespacedKey(this, "durability");
        Bukkit.getPluginManager().registerEvents(this, this);
        registerRecipe();
    }

    private void registerRecipe() {
        ItemStack result = createCannon();
        ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, "tnt_cannon"), result);
        recipe.shape("ITI", "RFR", "III");
        recipe.setIngredient('I', material("IRON_INGOT"));
        recipe.setIngredient('T', material("TNT"));
        recipe.setIngredient('R', material("REDSTONE"));
        recipe.setIngredient('F', material("FLINT_AND_STEEL"));
        Bukkit.addRecipe(recipe);
    }

    private ItemStack createCannon() {
        ItemStack item = XMaterial.matchXMaterial("DIAMOND_HOE")
                .map(XMaterial::parseItem)
                .orElseThrow(() -> new IllegalStateException("DIAMOND_HOE is unavailable"));
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(color("&6TNT Cannon"));
        meta.getPersistentDataContainer().set(cannonKey, PersistentDataType.BYTE, (byte) 1);
        meta.getPersistentDataContainer().set(durabilityKey, PersistentDataType.INTEGER, MAX_DURABILITY);
        meta.setCustomModelData(CUSTOM_MODEL_DATA);
        updateDurabilityDisplay(meta, MAX_DURABILITY, item);
        item.setItemMeta(meta);
        return item;
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (event.getHand() != EquipmentSlot.HAND) {
            return;
        }
        if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK) {
            return;
        }

        ItemStack item = event.getItem();
        if (!isCannon(item)) {
            return;
        }

        event.setCancelled(true);
        ItemMeta meta = item.getItemMeta();
        Integer remaining = meta.getPersistentDataContainer().get(durabilityKey, PersistentDataType.INTEGER);
        if (remaining == null || remaining <= 0) {
            return;
        }
        remaining = Math.min(remaining, MAX_DURABILITY);

        Player player = event.getPlayer();
        if (player.hasCooldown(item.getType())) {
            return;
        }
        player.setCooldown(item.getType(), FIRE_COOLDOWN_TICKS);

        TNTPrimed tnt = player.getWorld().spawn(
                player.getEyeLocation().add(player.getEyeLocation().getDirection()),
                TNTPrimed.class
        );
        tnt.setVelocity(player.getEyeLocation().getDirection().multiply(1.5));
        tnt.setFuseTicks(60);
        tnt.setSource(player);

        int nextDurability = remaining - 1;
        if (nextDurability == 0) {
            player.getInventory().setItemInMainHand(null);
            player.sendMessage(color("&cYour TNT Cannon broke."));
            return;
        }

        meta.getPersistentDataContainer().set(durabilityKey, PersistentDataType.INTEGER, nextDurability);
        updateDurabilityDisplay(meta, nextDurability, item);
        item.setItemMeta(meta);
    }

    private boolean isCannon(ItemStack item) {
        return item != null
                && item.hasItemMeta()
                && item.getItemMeta().getPersistentDataContainer().has(cannonKey, PersistentDataType.BYTE);
    }

    private void updateDurabilityDisplay(ItemMeta meta, int remaining, ItemStack item) {
        meta.setLore(List.of(color("&7Durability: &f" + remaining + "/" + MAX_DURABILITY)));
        if (meta instanceof Damageable damageable) {
            int maxDamage = item.getType().getMaxDurability();
            damageable.setDamage(Math.round((MAX_DURABILITY - remaining) * maxDamage / (float) MAX_DURABILITY));
        }
    }

    private org.bukkit.Material material(String name) {
        return XMaterial.matchXMaterial(name)
                .map(XMaterial::parseMaterial)
                .orElseThrow(() -> new IllegalStateException(name + " is unavailable"));
    }

    private String color(String text) {
        return ChatColor.translateAlternateColorCodes('&', text);
    }
}