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);
}
}