Files
src
build.gradle
settings.gradle
Code
4 287
src/main/java/com/kodari/dimensionspears/DimensionSpearsPlugin.java
package com.kodari.dimensionspears;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XPotion;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Keyed;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
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.inventory.PrepareItemCraftEvent;
import org.bukkit.event.player.PlayerInteractEvent;
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.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
public final class DimensionSpearsPlugin extends JavaPlugin implements Listener {
private static final String OVERWORLD = "overworld";
private static final String NETHER = "nether";
private static final String END = "end";
private static final String DIMENSION = "dimension";
private NamespacedKey spearTypeKey;
private NamespacedKey dimensionRecipeKey;
private final Map<String, Long> cooldowns = new HashMap<>();
@Override
public void onEnable() {
spearTypeKey = new NamespacedKey(this, "spear_type");
dimensionRecipeKey = new NamespacedKey(this, "dimension_spear");
Bukkit.getPluginManager().registerEvents(this, this);
registerRecipes();
}
private void registerRecipes() {
addRecipe("overworld_spear", createSpear(OVERWORLD), new String[]{" D", " S ", " S "},
'D', "DIAMOND", 'S', "STICK");
addRecipe("nether_spear", createSpear(NETHER), new String[]{" B ", "NMS", " S "},
'B', "BLAZE_ROD", 'N', "NETHERITE_INGOT", 'M', "MAGMA_CREAM", 'S', "STICK");
addRecipe("end_spear", createSpear(END), new String[]{" E ", "DSD", " S "},
'E', "ENDER_PEARL", 'D', "DIAMOND", 'S', "STICK");
ShapedRecipe dimensionRecipe = new ShapedRecipe(dimensionRecipeKey, createSpear(DIMENSION));
dimensionRecipe.shape("BEB", "ONT", "BSB");
dimensionRecipe.setIngredient('B', material("BEACON"));
dimensionRecipe.setIngredient('E', material("DRAGON_EGG"));
dimensionRecipe.setIngredient('O', material("TRIDENT"));
dimensionRecipe.setIngredient('N', material("TRIDENT"));
dimensionRecipe.setIngredient('T', material("TRIDENT"));
dimensionRecipe.setIngredient('S', material("NETHER_STAR"));
Bukkit.addRecipe(dimensionRecipe);
}
private void addRecipe(String key, ItemStack result, String[] shape, Object... ingredients) {
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, key), result);
recipe.shape(shape);
for (int index = 0; index < ingredients.length; index += 2) {
recipe.setIngredient((Character) ingredients[index], material((String) ingredients[index + 1]));
}
Bukkit.addRecipe(recipe);
}
private ItemStack createSpear(String type) {
ItemStack item = new ItemStack(material("TRIDENT"));
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(displayName(type));
meta.setLore(Arrays.asList(lore(type)));
meta.setUnbreakable(true);
meta.getPersistentDataContainer().set(spearTypeKey, PersistentDataType.STRING, type);
switch (type) {
case OVERWORLD -> {
addEnchantment(meta, "SHARPNESS", 5);
addEnchantment(meta, "LOOTING", 3);
addEnchantment(meta, "UNBREAKING", 10);
}
case NETHER -> {
addEnchantment(meta, "SHARPNESS", 6);
addEnchantment(meta, "FIRE_ASPECT", 3);
addEnchantment(meta, "UNBREAKING", 10);
}
case END -> {
addEnchantment(meta, "SHARPNESS", 6);
addEnchantment(meta, "KNOCKBACK", 3);
addEnchantment(meta, "UNBREAKING", 10);
}
case DIMENSION -> {
addEnchantment(meta, "SHARPNESS", 10);
addEnchantment(meta, "FIRE_ASPECT", 10);
addEnchantment(meta, "LOOTING", 10);
addEnchantment(meta, "KNOCKBACK", 10);
addEnchantment(meta, "MENDING", 10);
addEnchantment(meta, "UNBREAKING", 10);
}
default -> throw new IllegalArgumentException("Unknown spear type: " + type);
}
item.setItemMeta(meta);
return item;
}
private String displayName(String type) {
return switch (type) {
case OVERWORLD -> ChatColor.GREEN + "Overworld Spear";
case NETHER -> ChatColor.RED + "Nether Spear";
case END -> ChatColor.LIGHT_PURPLE + "End Spear";
case DIMENSION -> ChatColor.GOLD + "Dimension Spear";
default -> type;
};
}
private String[] lore(String type) {
return switch (type) {
case OVERWORLD -> new String[]{ChatColor.GRAY + "Right-click: regeneration and absorption"};
case NETHER -> new String[]{ChatColor.GRAY + "Right-click: ignite and blast nearby enemies"};
case END -> new String[]{ChatColor.GRAY + "Right-click: swift movement and lift nearby enemies"};
case DIMENSION -> new String[]{ChatColor.GOLD + "Right-click: unleash a devastating dimensional pulse"};
default -> new String[0];
};
}
private void addEnchantment(ItemMeta meta, String name, int level) {
XEnchantment.matchXEnchantment(name).ifPresent(enchantment ->
meta.addEnchant(enchantment.getEnchant(), level, true));
}
private Material material(String name) {
return XMaterial.matchXMaterial(name)
.map(XMaterial::parseMaterial)
.orElseThrow(() -> new IllegalStateException("Required material is unavailable: " + name));
}
@EventHandler
public void onPrepareCraft(PrepareItemCraftEvent event) {
if (!(event.getRecipe() instanceof Keyed keyed) || !keyed.getKey().equals(dimensionRecipeKey)) {
return;
}
ItemStack[] matrix = event.getInventory().getMatrix();
if (matrix.length != 9
|| !hasType(matrix[3], OVERWORLD)
|| !hasType(matrix[4], NETHER)
|| !hasType(matrix[5], END)) {
event.getInventory().setResult(null);
}
}
private boolean hasType(ItemStack item, String type) {
if (item == null || !item.hasItemMeta()) {
return false;
}
return type.equals(item.getItemMeta().getPersistentDataContainer()
.get(spearTypeKey, PersistentDataType.STRING));
}
@EventHandler
public void onSpearUse(PlayerInteractEvent event) {
if (event.getHand() == null || event.getHand().name().equals("OFF_HAND")
|| (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK)) {
return;
}
ItemStack item = event.getItem();
if (item == null || !item.hasItemMeta()) {
return;
}
String type = item.getItemMeta().getPersistentDataContainer()
.get(spearTypeKey, PersistentDataType.STRING);
if (type == null) {
return;
}
Player player = event.getPlayer();
long now = System.currentTimeMillis();
String cooldownKey = player.getUniqueId() + ":" + type;
long remaining = cooldowns.getOrDefault(cooldownKey, 0L) - now;
if (remaining > 0) {
player.sendMessage(ChatColor.RED + "Ability ready in " + (remaining + 999) / 1000 + "s.");
return;
}
long cooldownMillis = type.equals(DIMENSION) ? 60_000L : 20_000L;
cooldowns.put(cooldownKey, now + cooldownMillis);
useAbility(player, type);