Files
src
build.gradle
settings.gradle
Code
5 338
src/main/java/com/kodari/chainsword/ChainSwordPlugin.java
package com.kodari.chainsword;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.ChatColor;
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.Arrays;
public final class ChainSwordPlugin extends JavaPlugin implements CommandExecutor {
private NamespacedKey swordKey;
private NamespacedKey fireSpearKey;
@Override
public void onEnable() {
swordKey = new NamespacedKey(this, "chain_sword");
fireSpearKey = new NamespacedKey(this, "fire_spear");
getServer().getPluginManager().registerEvents(new ChainSwordListener(this), this);
getCommand("chainsword").setExecutor(this);
getCommand("firespear").setExecutor(this);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("Only players can receive these items.");
return true;
}
boolean fireSpearCommand = command.getName().equalsIgnoreCase("firespear");
ItemStack givenItem = fireSpearCommand ? createFireSpear() : createSword();
if (givenItem == null) {
player.sendMessage(ChatColor.RED + "Could not create the " + (fireSpearCommand ? "Fire Spear." : "Chain Sword."));
return true;
}
player.getInventory().addItem(givenItem).values()
.forEach(overflow -> player.getWorld().dropItemNaturally(player.getLocation(), overflow));
player.sendMessage(ChatColor.AQUA + "You received the " + (fireSpearCommand ? "Fire Spear!" : "Chain Sword!"));
return true;
}
private ItemStack createFireSpear() {
ItemStack spear = XMaterial.matchXMaterial("TRIDENT")
.map(XMaterial::parseItem)
.orElse(null);
if (spear == null) {
return null;
}
ItemMeta meta = spear.getItemMeta();
if (meta == null) {
return null;
}
meta.setDisplayName(ChatColor.GOLD + "Fire Spear");
meta.setLore(Arrays.asList(
ChatColor.GRAY + "Right-click to fire a flame beam.",
ChatColor.GRAY + "One-shots the first player hit."
));
meta.getPersistentDataContainer().set(fireSpearKey, PersistentDataType.BYTE, (byte) 1);
spear.setItemMeta(meta);
return spear;
}
private ItemStack createSword() {
ItemStack sword = XMaterial.matchXMaterial("IRON_SWORD")
.map(XMaterial::parseItem)
.orElse(null);
if (sword == null) {
return null;
}
ItemMeta meta = sword.getItemMeta();
if (meta == null) {
return null;
}
meta.setDisplayName(ChatColor.AQUA + "Chain Sword");
meta.setLore(Arrays.asList(
ChatColor.GRAY + "Every 15 player hits,",
ChatColor.GRAY + "stuns nearby enemies for 2 seconds."
));
meta.getPersistentDataContainer().set(swordKey, PersistentDataType.BYTE, (byte) 1);
sword.setItemMeta(meta);
return sword;
}
NamespacedKey getSwordKey() {
return swordKey;
}
NamespacedKey getFireSpearKey() {
return fireSpearKey;
}
}