Files
src
build.gradle
settings.gradle
Code
5 329
src/main/java/com/kodari/godstone/GodStonePlugin.java
package com.kodari.godstone;
import com.cryptomorin.xseries.XMaterial;
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;
import java.util.Map;
import java.util.Objects;
public final class GodStonePlugin extends JavaPlugin implements CommandExecutor {
private NamespacedKey swordKey;
private Material stoneSwordMaterial;
private GodStoneListener listener;
@Override
public void onEnable() {
XMaterial stoneSword = XMaterial.matchXMaterial("STONE_SWORD")
.orElseThrow(() -> new IllegalStateException("STONE_SWORD is unavailable"));
this.stoneSwordMaterial = Objects.requireNonNull(stoneSword.parseMaterial());
this.swordKey = new NamespacedKey(this, "god_stone_sword");
listener = new GodStoneListener(this);
getServer().getPluginManager().registerEvents(listener, this);
listener.start();
Objects.requireNonNull(getCommand("godstone")).setExecutor(this);
}
@Override
public void onDisable() {
if (listener != null) {
listener.stop();
}
}
@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 Stone Sword.");
return true;
}
if (!player.hasPermission("godstone.give")) {
player.sendMessage("You do not have permission to use this command.");
return true;
}
Map<Integer, ItemStack> leftovers = player.getInventory().addItem(createGodSword());
leftovers.values().forEach(item -> player.getWorld().dropItemNaturally(player.getLocation(), item));
player.sendMessage("You received the God Stone Sword.");
return true;
}
public ItemStack createGodSword() {
ItemStack sword = XMaterial.matchXMaterial("STONE_SWORD")
.map(XMaterial::parseItem)
.orElseThrow(() -> new IllegalStateException("STONE_SWORD is unavailable"));
ItemMeta meta = Objects.requireNonNull(sword.getItemMeta());
meta.getPersistentDataContainer().set(swordKey, PersistentDataType.BYTE, (byte) 1);
sword.setItemMeta(meta);
return sword;
}
public boolean isGodSword(ItemStack item) {
return item != null
&& item.getType() == stoneSwordMaterial
&& item.hasItemMeta()
&& item.getItemMeta().getPersistentDataContainer().has(swordKey, PersistentDataType.BYTE);
}
}