Files
src
Code
2 445
src/main/java/com/kodari/customweapons/CustomWeapons.java
package com.kodari.customweapons;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XItemFlag;
import com.cryptomorin.xseries.particles.XParticle;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.AbstractArrow;
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.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
public final class CustomWeapons extends JavaPlugin implements Listener {
private NamespacedKey typeKey, ownerKey, coreKey, arrowKey;
private final Map<String, Core> cores = new HashMap<>();
private final Set<String> crafted = new HashSet<>();
private final Map<UUID, Map<String, Integer>> charges = new HashMap<>();
private final Map<UUID, Map<String, Long>> cooldowns = new HashMap<>();
private final Map<UUID, BossBar> coreBars = new HashMap<>();
private final Map<UUID, BossBar> chargeBars = new HashMap<>();
private File dataFile;
private YamlConfiguration data;
private static final class Core {
final String type;
final UUID owner;
final Location location;
final long unlock;
Core(String type, UUID owner, Location location, long unlock) {
this.type = type; this.owner = owner; this.location = location; this.unlock = unlock;
}
}
@Override public void onEnable() {
typeKey = new NamespacedKey(this, "weapon_type");
ownerKey = new NamespacedKey(this, "weapon_owner");
coreKey = new NamespacedKey(this, "weapon_core");
arrowKey = new NamespacedKey(this, "breaker_arrow");
dataFile = new File(getDataFolder(), "weapons.yml");
data = YamlConfiguration.loadConfiguration(dataFile);
crafted.addAll(data.getStringList("crafted"));
ConfigurationSection section = data.getConfigurationSection("cores");
if (section != null) for (String type : section.getKeys(false)) {
try {
World world = Bukkit.getWorld(section.getString(type + ".world", ""));
if (world == null) { getLogger().warning("Core world unavailable for " + type); continue; }
Location loc = new Location(world, section.getInt(type + ".x"), section.getInt(type + ".y"), section.getInt(type + ".z"));
cores.put(type, new Core(type, UUID.fromString(section.getString(type + ".owner", "")), loc, section.getLong(type + ".unlock")));
} catch (IllegalArgumentException ex) { getLogger().warning("Invalid saved core: " + type); }
}
registerRecipes();
Bukkit.getPluginManager().registerEvents(this, this);
Bukkit.getScheduler().runTaskTimer(this, this::tick, 1L, 20L);
}
@Override public void onDisable() {
saveData();
coreBars.values().forEach(BossBar::removeAll);
chargeBars.values().forEach(BossBar::removeAll);
}
private void saveData() {
data.set("crafted", Arrays.asList(crafted.toArray(new String[0])));
// Preserve entries for worlds that were not loaded during startup.
for (Core core : cores.values()) {
String path = "cores." + core.type;
data.set(path + ".owner", core.owner.toString());
data.set(path + ".world", core.location.getWorld().getName());
data.set(path + ".x", core.location.getBlockX());
data.set(path + ".y", core.location.getBlockY());
data.set(path + ".z", core.location.getBlockZ());
data.set(path + ".unlock", core.unlock);
}
getDataFolder().mkdirs();
try { data.save(dataFile); } catch (IOException ex) { getLogger().severe("Could not save weapons.yml: " + ex.getMessage()); }
}
private ItemStack material(String name) {
ItemStack item = XMaterial.matchXMaterial(name).map(XMaterial::parseItem).orElse(null);
if (item == null) throw new IllegalStateException("Missing material " + name);
return item;
}
private String display(String type) {
switch (type) {
case "crownbreaker": return "Crownbreaker";
case "kn_dasher": return "Kn_Dasher";
case "tide_breaker": return "Tide Breaker";
case "breaker_bow": return "Breaker Bow";
default: return type;
}
}
private boolean validType(String type) {
return type != null && (type.equals("crownbreaker") || type.equals("kn_dasher") || type.equals("tide_breaker") || type.equals("breaker_bow"));
}
private ItemStack coreItem(String type) {
ItemStack item = material("PLAYER_HEAD");
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("§6" + display(type) + " Core");
meta.setLore(Arrays.asList("§7Place to begin the 5-minute unlock.", "§7Only the placer can wield this weapon."));
meta.getPersistentDataContainer().set(coreKey, PersistentDataType.STRING, type);
item.setItemMeta(meta);
return item;
}
private ItemStack weapon(String type, UUID owner) {
String material = type.equals("crownbreaker") ? "MACE" : type.equals("kn_dasher") ? "DIAMOND_SWORD" : type.equals("tide_breaker") ? "TRIDENT" : "BOW";
ItemStack item = material(material);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("§6§l" + display(type));
switch (type) {
case "crownbreaker":
meta.setLore(Arrays.asList("§7Legendary Weapon", "", "§bSky Launcher", "§7Launch yourself 15 blocks in the", "§7direction you're looking", "§8(Shift + Right Click) §7| §c4s Cooldown", "", "§bWater Dash", "§7Dash 15 blocks forward while in water", "§8(Right Click) §7| §c5s Cooldown", "", "§c§lSoul Reaper", "§7Unleash a devastating 7x7 AOE", "§7that forces every player to pop", "§7their totem of undying", "§8(Requires 25/25 Ultimate)"));
enchant(meta, "BREACH", 29); enchant(meta, "DENSITY", 70); break;
case "kn_dasher":
meta.setLore(Arrays.asList("§7Legendary Blade", "", "§bUltimate Ability", "§7Hit any player 10 times to charge", "§7your ultimate. Once ready, press", "§eShift + Left Click §7to unleash", "§7devastating damage in a 7x7 radius.", "§cCooldown: 150 seconds", "", "§8All enchants are hidden"));
enchant(meta, "SHARPNESS", 135); break;
case "tide_breaker":
meta.setLore(Arrays.asList("§7God of the Seas", "", "§bLightning Strike", "§7Every hit summons a lightning bolt", "§7that deals 5 hearts of true damage", "", "§c§lUltimate God Ability", "§7Hit any player 15 times to charge.", "§7Once ready, press §eShift + Right Click", "§7to call down lightning on every", "§7player in a 10x10 radius", "§7(deals 7 hearts of damage)")); break;
case "breaker_bow":
meta.setLore(Arrays.asList("§7Armor Destroyer", "", "§c§lArmor Breaker", "§7Any player hit by an arrow from", "§7this bow will have their entire", "§7armor set completely destroyed.", "§cCooldown: 100 seconds", "", "§8All enchants are hidden"));
enchant(meta, "SHARPNESS", 245); break;
}
meta.setEnchantmentGlintOverride(true);
XItemFlag.of("HIDE_ENCHANTS").ifPresent(flag -> flag.set(meta));
meta.getPersistentDataContainer().set(typeKey, PersistentDataType.STRING, type);
meta.getPersistentDataContainer().set(ownerKey, PersistentDataType.STRING, owner.toString());
item.setItemMeta(meta);
return item;
}
private void enchant(ItemMeta meta, String name, int level) {
XEnchantment.matchXEnchantment(name).ifPresent(e -> meta.addEnchant(e.getEnchant(), level, true));
}
private String tag(ItemStack item, NamespacedKey key) {
return item == null || !item.hasItemMeta() ? null : item.getItemMeta().getPersistentDataContainer().get(key, PersistentDataType.STRING);
}
private boolean owned(Player player, ItemStack item) {
String type = tag(item, typeKey);
return validType(type) && player.getUniqueId().toString().equals(tag(item, ownerKey));
}
private void registerRecipes() {
recipe("crownbreaker", new String[]{"TTT", "SMS", "S S"}, "TOTEM_OF_UNDYING", "ECHO_SHARD", "MACE");
recipe("kn_dasher", new String[]{" ", "TMT", "T T"}, "TOTEM_OF_UNDYING", null, "DIAMOND_SWORD");
recipe("tide_breaker", new String[]{"TTT", "SMS", "S S"}, "ICE", "QUARTZ", "TRIDENT");
recipe("breaker_bow", new String[]{" T ", "SMS", "S S"}, "DEEPSLATE_BRICKS", "BLACKSTONE", "BOW");
}
private void recipe(String type, String[] shape, String top, String side, String middle) {
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, type + "_core"), coreItem(type));