Files
src
Code
3 294
src/main/java/com/kodari/godsword/GodSwordPlugin.java
package com.kodari.godsword;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
public final class GodSwordPlugin extends JavaPlugin implements CommandExecutor {
private NamespacedKey godSwordKey;
@Override
public void onEnable() {
godSwordKey = new NamespacedKey(this, "god_sword");
GodSwordListener listener = new GodSwordListener(this);
Bukkit.getPluginManager().registerEvents(listener, this);
Bukkit.getScheduler().runTaskTimer(this, listener::applyPassiveBuffs, 0L, 20L);
getCommand("godsword").setExecutor(this);
}
public boolean isGodSword(ItemStack item) {
if (item == null || item.getType().isAir()) {
return false;
}
if (!XMaterial.matchXMaterial(item.getType()).name().equals("STONE_SWORD")) {
return false;
}
ItemMeta meta = item.getItemMeta();
return meta != null && meta.getPersistentDataContainer()
.getOrDefault(godSwordKey, PersistentDataType.BYTE, (byte) 0) == 1;
}
private ItemStack createGodSword() {
ItemStack sword = XMaterial.matchXMaterial("STONE_SWORD")
.map(XMaterial::parseItem)
.orElseThrow(() -> new IllegalStateException("STONE_SWORD is not available"));
ItemMeta meta = sword.getItemMeta();
meta.getPersistentDataContainer().set(godSwordKey, PersistentDataType.BYTE, (byte) 1);
sword.setItemMeta(meta);
return sword;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("Only a player can receive the God Sword.");
return true;
}
ItemStack sword = createGodSword();
player.getInventory().addItem(sword).values()
.forEach(item -> player.getWorld().dropItemNaturally(player.getLocation(), item));
player.sendMessage("Your God Sword is ready.");
return true;
}
}