Files
src
Code
2 278
src/main/java/com/kodari/goditems/GodItemsPlugin.java
package com.kodari.goditems;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XPotion;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
public final class GodItemsPlugin extends JavaPlugin implements Listener {
private static final Map<String, String> ITEM_MATERIALS = Map.of(
"god_helmet", "NETHERITE_HELMET",
"god_chestplate", "NETHERITE_CHESTPLATE",
"god_leggings", "NETHERITE_LEGGINGS",
"god_boots", "NETHERITE_BOOTS",
"god_sword", "NETHERITE_SWORD",
"god_axe", "NETHERITE_AXE",
"god_bow", "BOW",
"god_mace", "MACE"
);
private static final Map<String, String> DISPLAY_NAMES = Map.of(
"god_helmet", "God Helmet",
"god_chestplate", "God Chestplate",
"god_leggings", "God Leggings",
"god_boots", "God Boots",
"god_sword", "God Sword",
"god_axe", "God Axe",
"god_bow", "God Bow",
"god_mace", "God Mace"
);
private NamespacedKey itemIdKey;
private final Set<UUID> stunnedPlayers = new HashSet<>();
private final Map<UUID, BukkitTask> stunTasks = new HashMap<>();
@Override
public void onEnable() {
itemIdKey = new NamespacedKey(this, "god_item");
Bukkit.getPluginManager().registerEvents(this, this);
registerRecipes();
Bukkit.getScheduler().runTaskTimer(this, this::applyArmorPowers, 20L, 20L);
}
private void registerRecipes() {
registerRecipe("god_helmet", new String[]{"DHD", "HCH", "DHD"}, Map.of(
'D', "DIAMOND", 'H', "PLAYER_HEAD", 'C', "NETHERITE_HELMET"));
registerRecipe("god_chestplate", new String[]{"AHA", "HCH", "AHA"}, Map.of(
'A', "GOLDEN_APPLE", 'H', "PLAYER_HEAD", 'C', "NETHERITE_CHESTPLATE"));
registerRecipe("god_leggings", new String[]{"WHW", "HLH", "WHW"}, Map.of(
'W', "COBWEB", 'H', "PLAYER_HEAD", 'L', "NETHERITE_LEGGINGS"));
registerRecipe("god_boots", new String[]{"FHF", "HBH", "FHF"}, Map.of(
'F', "FEATHER", 'H', "PLAYER_HEAD", 'B', "NETHERITE_BOOTS"));
registerRecipe("god_sword", new String[]{" D ", "DSD", " H "}, Map.of(
'D', "DIAMOND", 'S', "NETHERITE_SWORD", 'H', "PLAYER_HEAD"));
registerRecipe("god_axe", new String[]{"DH ", "DAD", " H "}, Map.of(
'D', "DIAMOND", 'H', "PLAYER_HEAD", 'A', "NETHERITE_AXE"));
registerRecipe("god_bow", new String[]{"FHF", "HBH", "FSF"}, Map.of(
'F', "FEATHER", 'H', "PLAYER_HEAD", 'B', "BOW", 'S', "STRING"));
registerRecipe("god_mace", new String[]{"BHB", "HMC", "BHB"}, Map.of(
'B', "BREEZE_ROD", 'H', "PLAYER_HEAD", 'M', "MACE", 'C', "HEAVY_CORE"));
}
private void registerRecipe(String id, String[] shape, Map<Character, String> ingredients) {
ItemStack result = createGodItem(id);
if (result == null) {
getLogger().warning("Could not create " + id + "; its base item is not available on this server.");
return;
}
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, id), result);
recipe.shape(shape);
for (Map.Entry<Character, String> entry : ingredients.entrySet()) {
Material ingredient = material(entry.getValue());
if (ingredient == null) {
getLogger().warning("Skipping " + id + " recipe; material " + entry.getValue() + " is unavailable.");
return;
}
recipe.setIngredient(entry.getKey(), new RecipeChoice.MaterialChoice(ingredient));
}
Bukkit.addRecipe(recipe);
}
private ItemStack createGodItem(String id) {
String materialName = ITEM_MATERIALS.get(id);
if (materialName == null) {
return null;
}
ItemStack item = XMaterial.matchXMaterial(materialName).map(XMaterial::parseItem).orElse(null);
if (item == null) {
return null;
}
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return null;
}
meta.setDisplayName(ChatColor.GOLD + "✦ " + DISPLAY_NAMES.get(id));
meta.setLore(List.of(ChatColor.GRAY + "A relic of immense power."));
meta.getPersistentDataContainer().set(itemIdKey, PersistentDataType.STRING, id);
item.setItemMeta(meta);
return item;
}
private Material material(String name) {
return XMaterial.matchXMaterial(name).map(XMaterial::parseMaterial).orElse(null);
}
private String getGodItemId(ItemStack item) {
if (item == null || !item.hasItemMeta()) {
return null;
}
return item.getItemMeta().getPersistentDataContainer().get(itemIdKey, PersistentDataType.STRING);
}
private void applyArmorPowers() {
for (Player player : Bukkit.getOnlinePlayers()) {
Set<String> worn = new HashSet<>();
for (ItemStack armorPiece : player.getInventory().getArmorContents()) {
String id = getGodItemId(armorPiece);
if (id != null) {
worn.add(id);
}
}
if (worn.contains("god_helmet")) {
applyPotion(player, "NIGHT_VISION", 0);
}
if (worn.contains("god_chestplate")) {
applyPotion(player, "DAMAGE_RESISTANCE", 0);
}
if (worn.contains("god_leggings")) {
applyPotion(player, "SPEED", 1);
}
if (worn.contains("god_boots")) {
applyPotion(player, "JUMP", 1);
}
if (worn.containsAll(Set.of("god_helmet", "god_chestplate", "god_leggings", "god_boots"))) {
applyPotion(player, "INCREASE_DAMAGE", 0);
applyPotion(player, "REGENERATION", 0);
}
}
}
private void applyPotion(Player player, String potionName, int amplifier) {
XPotion.matchXPotion(potionName)
.map(potion -> potion.buildPotionEffect(60, amplifier))
.ifPresent(player::addPotionEffect);
}
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event) {
ItemStack head = XMaterial.matchXMaterial("PLAYER_HEAD").map(XMaterial::parseItem).orElse(null);
if (head == null) {
return;
}
ItemMeta itemMeta = head.getItemMeta();
if (!(itemMeta instanceof SkullMeta skullMeta)) {
return;
}
Player victim = event.getEntity();
skullMeta.setOwningPlayer(victim);
skullMeta.setDisplayName(ChatColor.GOLD + victim.getName() + "'s Head");
head.setItemMeta(skullMeta);
event.getDrops().add(head);
}
@EventHandler
public void onEntityDamage(EntityDamageByEntityEvent event) {
if (event.getDamager() instanceof Player attacker) {
if (stunnedPlayers.contains(attacker.getUniqueId())) {
event.setCancelled(true);
return;



