Files
src
build.gradle
settings.gradle
Code
5 301
src/main/java/com/kodari/godspear/GodSpearPlugin.java
package com.kodari.godspear;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XPotion;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
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.util.Vector;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public final class GodSpearPlugin extends JavaPlugin implements Listener {
private NamespacedKey godSpearKey;
private final Map<UUID, Long> lightningCooldowns = new HashMap<>();
private final Map<UUID, Long> shockwaveCooldowns = new HashMap<>();
@Override
public void onEnable() {
godSpearKey = new NamespacedKey(this, "god_spear");
saveDefaultConfig();
registerRecipes();
Bukkit.getPluginManager().registerEvents(this, this);
Bukkit.getScheduler().runTaskTimer(this, this::applyGodSpearEffects, 0L, 20L);
}
private void registerRecipes() {
String[] spearTiers = {
"WOODEN_SPEAR",
"STONE_SPEAR",
"COPPER_SPEAR",
"IRON_SPEAR",
"GOLDEN_SPEAR",
"DIAMOND_SPEAR"
};
Material[] spearMaterials = new Material[spearTiers.length];
for (int i = 0; i < spearTiers.length; i++) {
spearMaterials[i] = Material.matchMaterial(spearTiers[i]);
if (spearMaterials[i] == null) {
getLogger().warning("Could not register the God Spear recipe for missing material: " + spearTiers[i]);
return;
}
}
for (String tier : spearTiers) {
Bukkit.removeRecipe(new NamespacedKey(this, "god_spear_" + tier.toLowerCase()));
}
NamespacedKey recipeKey = new NamespacedKey(this, "god_spear");
Bukkit.removeRecipe(recipeKey);
if (getConfig().getBoolean("god-spear-crafted", false)) {
return;
}
ShapedRecipe recipe = new ShapedRecipe(recipeKey, createGodSpear());
recipe.shape("ABC", "DEF", " G ");
recipe.setIngredient('A', spearMaterials[0]);
recipe.setIngredient('B', spearMaterials[1]);
recipe.setIngredient('C', spearMaterials[2]);
recipe.setIngredient('D', spearMaterials[3]);
recipe.setIngredient('E', spearMaterials[4]);
recipe.setIngredient('F', spearMaterials[5]);
recipe.setIngredient('G', XMaterial.matchXMaterial("DIAMOND_BLOCK")
.map(XMaterial::parseMaterial)
.orElse(Material.AIR));
Bukkit.addRecipe(recipe);
}
private ItemStack createGodSpear() {
Material diamondSpear = Material.matchMaterial("DIAMOND_SPEAR");
if (diamondSpear == null) {
throw new IllegalStateException("DIAMOND_SPEAR is unavailable");
}
ItemStack spear = new ItemStack(diamondSpear);
ItemMeta meta = spear.getItemMeta();
if (meta == null) {
throw new IllegalStateException("Could not create God Spear metadata");
}
meta.setDisplayName("&b&lGod Spear");
meta.setLore(List.of(
"&7Forged from the power of every spear tier.",
"&eWhile held:",
"&f Saturation",
"&f Fire Resistance",
"&f Regeneration",
"&f Speed III",
"&bRight-click: Lightning Strike &7(5s)",
"&bSneak + Right-click: Shockwave &7(10s)"
));
meta.setUnbreakable(true);
addMaxEnchantments(meta);
meta.getPersistentDataContainer().set(godSpearKey, PersistentDataType.BYTE, (byte) 1);
spear.setItemMeta(meta);
return spear;
}
private void addMaxEnchantments(ItemMeta meta) {
addEnchantment(meta, "SHARPNESS", 5);
addEnchantment(meta, "SMITE", 5);
addEnchantment(meta, "BANE_OF_ARTHROPODS", 5);
addEnchantment(meta, "FIRE_ASPECT", 2);
addEnchantment(meta, "LOOTING", 3);
addEnchantment(meta, "KNOCKBACK", 2);
addEnchantment(meta, "LUNGE", 3);
addEnchantment(meta, "UNBREAKING", 3);
addEnchantment(meta, "MENDING", 1);
}
private void addEnchantment(ItemMeta meta, String name, int level) {
XEnchantment.matchXEnchantment(name)
.ifPresent(enchantment -> meta.addEnchant(enchantment.getEnchant(), level, true));
}
private void applyGodSpearEffects() {
for (Player player : Bukkit.getOnlinePlayers()) {
if (!isGodSpear(player.getInventory().getItemInMainHand())
&& !isGodSpear(player.getInventory().getItemInOffHand())) {
continue;
}
player.setFoodLevel(20);
player.setSaturation(20.0f);
player.setExhaustion(0.0f);
XPotion.matchXPotion("SATURATION")
.map(potion -> potion.buildPotionEffect(40, 0))
.ifPresent(effect -> player.addPotionEffect(effect, true));
XPotion.matchXPotion("FIRE_RESISTANCE")
.map(potion -> potion.buildPotionEffect(60, 0))
.ifPresent(effect -> player.addPotionEffect(effect, true));
XPotion.matchXPotion("REGENERATION")
.map(potion -> potion.buildPotionEffect(60, 0))
.ifPresent(effect -> player.addPotionEffect(effect, true));
XPotion.matchXPotion("SPEED")
.map(potion -> potion.buildPotionEffect(60, 2))
.ifPresent(effect -> player.addPotionEffect(effect, true));
}
}
private boolean isGodSpear(ItemStack item) {
if (item == null || item.getType().isAir() || !item.hasItemMeta()) {
return false;
}
return item.getItemMeta().getPersistentDataContainer().has(godSpearKey, PersistentDataType.BYTE);
}
@EventHandler
public void onGodSpearAbility(PlayerInteractEvent event) {
if (event.getHand() != EquipmentSlot.HAND
|| (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK)
|| !isGodSpear(event.getItem())) {
return;
}
event.setUseItemInHand(Event.Result.DENY);
event.setUseInteractedBlock(Event.Result.DENY);
Player player = event.getPlayer();
if (player.isSneaking()) {
if (isOnCooldown(shockwaveCooldowns, player.getUniqueId(), 10_000L)) {
player.sendMessage("&cShockwave is on cooldown.");
return;
}
useShockwave(player);
return;
}
if (isOnCooldown(lightningCooldowns, player.getUniqueId(), 5_000L)) {
player.sendMessage("&cLightning Strike is on cooldown.");
return;
}
useLightningStrike(player);
}
private boolean isOnCooldown(Map<UUID, Long> cooldowns, UUID playerId, long cooldownMillis) {
long now = System.currentTimeMillis();