Files
src
build.gradle
settings.gradle
Code
4 188
src/main/java/com/kodari/supermace/SuperMacePlugin.java
package com.kodari.supermace;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.util.Vector;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public final class SuperMacePlugin extends JavaPlugin implements Listener {
private static final long COOLDOWN_MILLIS = 15_000L;
private static final double LAUNCH_VELOCITY = 1.7D;
private static final double DAMAGE_MULTIPLIER = 2.0D;
private final Map<UUID, Long> cooldowns = new HashMap<>();
private NamespacedKey itemKey;
private NamespacedKey recipeKey;
private Material maceMaterial;
private Material windChargeMaterial;
private Material netheriteIngotMaterial;
@Override
public void onEnable() {
itemKey = new NamespacedKey(this, "super_mace");
recipeKey = new NamespacedKey(this, "super_mace_recipe");
maceMaterial = requireMaterial("MACE");
windChargeMaterial = requireMaterial("WIND_CHARGE");
netheriteIngotMaterial = requireMaterial("NETHERITE_INGOT");
registerRecipe();
getServer().getPluginManager().registerEvents(this, this);
}
@Override
public void onDisable() {
cooldowns.clear();
}
private void registerRecipe() {
Bukkit.removeRecipe(recipeKey);
ShapedRecipe recipe = new ShapedRecipe(recipeKey, createSuperMace());
recipe.shape("WNW", "NMN", "WNW");
recipe.setIngredient('W', new RecipeChoice.MaterialChoice(windChargeMaterial));
recipe.setIngredient('N', new RecipeChoice.MaterialChoice(netheriteIngotMaterial));
recipe.setIngredient('M', new RecipeChoice.MaterialChoice(maceMaterial));
Bukkit.addRecipe(recipe);
}
private ItemStack createSuperMace() {
ItemStack mace = requireItem("MACE");
ItemMeta meta = mace.getItemMeta();
meta.setDisplayName(ChatColor.GOLD + "Super Mace");
meta.setLore(Arrays.asList(
ChatColor.GRAY + "Double mace damage",
ChatColor.GRAY + "Right-click to launch yourself",
ChatColor.DARK_GRAY + "Ability cooldown: 15 seconds"
));
meta.getPersistentDataContainer().set(itemKey, PersistentDataType.BYTE, (byte) 1);
mace.setItemMeta(meta);
return mace;
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onMaceDamage(EntityDamageByEntityEvent event) {
if (!(event.getDamager() instanceof Player player)) {
return;
}
if (isSuperMace(player.getInventory().getItemInMainHand())) {
event.setDamage(event.getDamage() * DAMAGE_MULTIPLIER);
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onRightClick(PlayerInteractEvent event) {
if (event.getHand() == null || !event.getHand().isHand()) {
return;
}
Action action = event.getAction();
if (action != Action.RIGHT_CLICK_AIR && action != Action.RIGHT_CLICK_BLOCK) {
return;
}
Player player = event.getPlayer();
if (!isSuperMace(event.getItem())) {
return;
}
event.setCancelled(true);
long now = System.currentTimeMillis();
long availableAt = cooldowns.getOrDefault(player.getUniqueId(), 0L);
if (availableAt > now) {
long seconds = (availableAt - now + 999L) / 1_000L;
player.sendMessage(ChatColor.RED + "Super Mace is on cooldown for " + seconds + "s.");
return;
}
Vector velocity = player.getVelocity().clone();
velocity.setY(LAUNCH_VELOCITY);
player.setVelocity(velocity);
cooldowns.put(player.getUniqueId(), now + COOLDOWN_MILLIS);
player.sendMessage(ChatColor.GOLD + "Super Mace launched you into the air!");
}
private boolean isSuperMace(ItemStack item) {
if (item == null || item.getType() != maceMaterial || !item.hasItemMeta()) {
return false;
}
ItemMeta meta = item.getItemMeta();
return meta.getPersistentDataContainer().has(itemKey, PersistentDataType.BYTE);
}
private Material requireMaterial(String name) {
return XMaterial.matchXMaterial(name)
.map(XMaterial::parseMaterial)
.orElseThrow(() -> new IllegalStateException("Missing required material: " + name));
}
private ItemStack requireItem(String name) {
return XMaterial.matchXMaterial(name)
.map(XMaterial::parseItem)
.orElseThrow(() -> new IllegalStateException("Missing required item: " + name));
}
}