Files
src
build.gradle
settings.gradle
Code
4 475
src/main/java/com/yourname/customweapons/Main.java
package com.yourname.customweapons;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XPotion;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.particles.XParticle;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
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.command.TabCompleter;
import org.bukkit.entity.DragonFireball;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.util.Vector;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
public final class Main extends JavaPlugin implements Listener, CommandExecutor, TabCompleter {
private NamespacedKey weaponKey;
private boolean stealEnabled = true;
private final Map<String, ItemStack> weaponRegistry = new HashMap<>();
private final Map<UUID, Map<String, Long>> cooldowns = new HashMap<>();
@Override
public void onEnable() {
weaponKey = new NamespacedKey(this, "custom_weapon_id");
getServer().getPluginManager().registerEvents(this, this);
getCommand("sr").setExecutor(this);
getCommand("sr").setTabCompleter(this);
initWeapons();
registerRecipes();
startPassiveTask();
getLogger().info("CustomWeapons enabled.");
}
@Override
public void onDisable() {
cooldowns.clear();
getLogger().info("CustomWeapons disabled.");
}
private void initWeapons() {
ItemStack soulstrike = createWeapon("NETHERITE_SWORD", "soulstrike",
ChatColor.DARK_RED + "" + ChatColor.BOLD + "SOULSTRIKE — Soul Blade",
Arrays.asList(
ChatColor.GRAY + "Legendary Netherite Sword",
"",
ChatColor.GREEN + "PASSIVE ABILITIES:",
ChatColor.DARK_GREEN + "• Resistance: " + ChatColor.WHITE + "30s on hit or when hit",
ChatColor.DARK_GREEN + "• Strength I: " + ChatColor.WHITE + "While held",
"",
ChatColor.LIGHT_PURPLE + "SPECIAL ABILITIES:",
ChatColor.AQUA + "• Sonic Beam: " + ChatColor.YELLOW + "Shift + Left Click (8m cooldown)",
ChatColor.AQUA + "• Dragon Breath: " + ChatColor.YELLOW + "Right Click (6m cooldown)",
ChatColor.AQUA + "• Soul Power: " + ChatColor.YELLOW + "Shift + Right Click (3m cooldown)"
));
if (soulstrike != null) {
weaponRegistry.put("soulstrike", soulstrike);
}
ItemStack godbreaker = createWeapon("TRIDENT", "godbreaker",
ChatColor.AQUA + "" + ChatColor.BOLD + "GODBREAKER — Ocean Destroyer",
Arrays.asList(
ChatColor.GRAY + "Water Dominion — Second Strongest Weapon",
"",
ChatColor.GOLD + "PASSIVES & ENCHANTS:",
ChatColor.YELLOW + "• Riptide IV, Impaling V, Sharpness IV, Strength II",
ChatColor.YELLOW + "• Water Breathing: While held",
"",
ChatColor.LIGHT_PURPLE + "SPECIAL ABILITIES:",
ChatColor.AQUA + "• Water Tornado: " + ChatColor.YELLOW + "Right-click in water (10m cooldown)"
));
if (godbreaker != null) {
ItemMeta meta = godbreaker.getItemMeta();
addEnchantment(meta, "RIPTIDE", 4);
addEnchantment(meta, "IMPALING", 5);
addEnchantment(meta, "SHARPNESS", 4);
godbreaker.setItemMeta(meta);
weaponRegistry.put("godbreaker", godbreaker);
}
}
private ItemStack createWeapon(String materialName, String id, String displayName, List<String> lore) {
ItemStack item = XMaterial.matchXMaterial(materialName).map(XMaterial::parseItem).orElse(null);
if (item == null) {
getLogger().warning("Could not create weapon " + id + ": material " + materialName + " is unavailable.");
return null;
}
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return null;
}
meta.setDisplayName(displayName);
meta.setLore(lore);
meta.getPersistentDataContainer().set(weaponKey, PersistentDataType.STRING, id);
item.setItemMeta(meta);
return item;
}
private void addEnchantment(ItemMeta meta, String name, int level) {
XEnchantment.matchXEnchantment(name).ifPresent(enchantment ->
meta.addEnchant(enchantment.getEnchant(), level, true));
}
private void registerRecipes() {
ItemStack soulstrike = weaponRegistry.get("soulstrike");
if (soulstrike != null) {
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, "soulstrike_recipe"), soulstrike);
recipe.shape("BTB", "ESE", "ITI");
if (setIngredient(recipe, 'B', "BLAZE_ROD")
&& setIngredient(recipe, 'T', "TOTEM_OF_UNDYING")
&& setIngredient(recipe, 'E', "EYE_OF_ENDER")
&& setIngredient(recipe, 'S', "NETHERITE_SWORD")
&& setIngredient(recipe, 'I', "NETHERITE_INGOT")) {
Bukkit.addRecipe(recipe);
}
}
ItemStack godbreaker = weaponRegistry.get("godbreaker");
if (godbreaker != null) {
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, "godbreaker_recipe"), godbreaker);
recipe.shape("NIN", "WWS", "ETE");
if (setIngredient(recipe, 'N', "NETHERITE_INGOT")
&& setIngredient(recipe, 'I', "DIAMOND_BLOCK")
&& setIngredient(recipe, 'W', "POTION")
&& setIngredient(recipe, 'S', "WITHER_SKELETON_SKULL")
&& setIngredient(recipe, 'E', "ECHO_SHARD")
&& setIngredient(recipe, 'T', "TRIDENT")) {
Bukkit.addRecipe(recipe);
}
}
}
private boolean setIngredient(ShapedRecipe recipe, char symbol, String materialName) {
Optional<Material> material = XMaterial.matchXMaterial(materialName)
.map(XMaterial::parseMaterial);
if (material.isEmpty()) {
getLogger().warning("Recipe ingredient unavailable: " + materialName);
return false;
}
recipe.setIngredient(symbol, material.get());
return true;
}
private void startPassiveTask() {
Bukkit.getScheduler().runTaskTimer(this, () -> {
for (Player player : Bukkit.getOnlinePlayers()) {
String weaponId = getWeaponId(player.getInventory().getItemInMainHand());
if ("soulstrike".equals(weaponId)) {
applyPotion(player, "STRENGTH", 40, 0);
} else if ("godbreaker".equals(weaponId)) {
applyPotion(player, "STRENGTH", 40, 1);
applyPotion(player, "WATER_BREATHING", 40, 0);
}
}
}, 0L, 20L);
}
private void applyPotion(Player player, String potionName, int duration, int amplifier) {
XPotion.matchXPotion(potionName)
.map(potion -> potion.buildPotionEffect(duration, amplifier))
.ifPresent(effect -> player.addPotionEffect(effect, true));
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 0) {
sender.sendMessage(ChatColor.YELLOW + "Usage: /sr <give|take|steal|weapons>");
return true;
}