Files
src
build.gradle
Code
3 516
src/main/java/com/kodari/leviathan/LeviathanTreasuresPlugin.java
package com.kodari.leviathan;
import com.cryptomorin.xseries.XAttribute;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XItemFlag;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XPotion;
import com.cryptomorin.xseries.particles.XParticle;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Waterlogged;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Trident;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityTargetLivingEntityEvent;
import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffect;
import org.bukkit.util.Vector;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
public final class LeviathanTreasuresPlugin extends JavaPlugin implements Listener {
private final Map<String, Long> cooldowns = new HashMap<>();
private final Map<UUID, Long> waterWalkUntil = new HashMap<>();
private final Map<UUID, Long> fallProtectionUntil = new HashMap<>();
private NamespacedKey itemIdKey;
private long ticks;
@Override
public void onEnable() {
itemIdKey = new NamespacedKey(this, "item_id");
Bukkit.getPluginManager().registerEvents(this, this);
registerRecipes();
Bukkit.getScheduler().runTaskTimer(this, this::updatePlayers, 20L, 20L);
}
private void registerRecipes() {
addRecipe("leviathan_scale", scale(), new String[]{"PPP", "PNP", "PPP"},
'P', material("PRISMARINE_SHARD"), 'N', material("NAUTILUS_SHELL"));
addRecipe("leviathan_heart", leviathanHeart(), new String[]{"PSP", "SHS", "PSP"},
'P', material("PRISMARINE_CRYSTALS"), 'S', scale(), 'H', material("HEART_OF_THE_SEA"));
addRecipe("leviathan_helmet", armor("helmet"), new String[]{"SSS", "SHS", " "},
'S', scale(), 'H', leviathanHeart());
addRecipe("leviathan_chestplate", armor("chestplate"), new String[]{"S S", "SHS", "SSS"},
'S', scale(), 'H', leviathanHeart());
addRecipe("leviathan_leggings", armor("leggings"), new String[]{"SHS", "S S", "S S"},
'S', scale(), 'H', leviathanHeart());
addRecipe("leviathan_boots", armor("boots"), new String[]{" ", "S S", "SHS"},
'S', scale(), 'H', leviathanHeart());
addRecipe("fishing_masters_rod", namedItem("fishing_rod", "FISHING_ROD", "&bFishing Master's Rod",
"&7Better rewards from every catch.", 0, 0, 0),
new String[]{" S", " SF", "S F"}, 'S', material("STICK"), 'F', material("STRING"));
addRecipe("ocean_wind", namedItem("ocean_wind", "POTION", "&bBottle of Ocean Wind",
"&7Right-click for Speed II.", 0, 0, 0),
new String[]{" P ", " G ", " P "}, 'P', material("PRISMARINE_CRYSTALS"), 'G', material("GLASS_BOTTLE"));
addRecipe("poseidons_trident", namedItem("poseidons_trident", "TRIDENT", "&3Poseidon's Trident",
"&725% chance to call lightning on hit.", "&7Right-click to unleash a damaging wave.", 0, 0, 0),
new String[]{" P ", "PSP", " H "}, 'P', material("PRISMARINE_CRYSTALS"),
'S', scale(), 'H', material("HEART_OF_THE_SEA"));
addRecipe("tidal_blade", namedItem("tidal_blade", "DIAMOND_SWORD", "&bTidal Blade",
"&7Right-click to dash (10s cooldown).", "&7Shocks enemies and deals extra damage in water.", 0, 0, 0),
new String[]{" P ", " S ", " H "}, 'P', material("PRISMARINE_CRYSTALS"),
'S', scale(), 'H', material("DIAMOND"));
addRecipe("sailors_grappling_hook", namedItem("sailors_grappling_hook", "TRIPWIRE_HOOK",
"&bSailor's Grappling Hook", "&7Right-click a block within 20 blocks to pull yourself to it.",
"&7Pulls hit players to you. (10s cooldown)", 0, 0, 0),
new String[]{"IS ", "IT ", "I "}, 'I', material("IRON_INGOT"),
'S', material("STRING"), 'T', material("TRIPWIRE_HOOK"));
}
private void addRecipe(String id, ItemStack result, String[] shape, Object... ingredients) {
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, id), result);
recipe.shape(shape);
for (int i = 0; i < ingredients.length; i += 2) {
char symbol = (Character) ingredients[i];
Object ingredient = ingredients[i + 1];
if (ingredient instanceof ItemStack stack) {
recipe.setIngredient(symbol, new RecipeChoice.ExactChoice(stack));
} else {
recipe.setIngredient(symbol, (Material) ingredient);
}
}
Bukkit.addRecipe(recipe);
}
private ItemStack scale() {
return namedItem("leviathan_scale", "PRISMARINE_SHARD", "&bLeviathan Scale",
"&7A scale from an ancient sea king.", 0, 0, 0);
}
private ItemStack leviathanHeart() {
return namedItem("leviathan_heart", "HEART_OF_THE_SEA", "&3Leviathan Heart",
"&7The beating core of the deep.", 0, 0, 0);
}
private ItemStack armor(String piece) {
return switch (piece) {
case "helmet" -> namedItem("leviathan_helmet", "IRON_HELMET",
"&3Leviathan Helm", new String[]{"&7The eyes of the deep watch through this helmet.",
"&b+3 Armor", "&bWater Breathing", "&bNight Vision underwater", "&b+10% Fishing Luck"},
1, 0, 0, EquipmentSlot.HEAD);
case "chestplate" -> namedItem("leviathan_chestplate", "IRON_CHESTPLATE",
"&3Leviathan Chestplate", new String[]{"&7Forged from scales harder than diamond.",
"&b+8 Armor", "&b+2 Armor Toughness", "&b20% knockback resistance",
"&bResistance I while submerged"}, 2, 2, 0.2, EquipmentSlot.CHEST);
case "leggings" -> namedItem("leviathan_leggings", "IRON_LEGGINGS",
"&3Leviathan Leggings", new String[]{"&7Swift as the ocean currents.",
"&b+6 Armor", "&bDolphin's Grace", "&bFaster swimming"},
1, 0, 0, EquipmentSlot.LEGS);
default -> {
ItemStack boots = namedItem("leviathan_boots", "IRON_BOOTS",
"&3Leviathan Boots", new String[]{"&7The sea itself carries your steps.",
"&b+3 Armor", "&bDepth Strider III", "&bSoul Speed III",
"&bWalk on water briefly after jumping"}, 1, 0, 0, EquipmentSlot.FEET);
XEnchantment.matchXEnchantment("DEPTH_STRIDER").ifPresent(enchantment -> boots.addUnsafeEnchantment(enchantment.getEnchant(), 3));
XEnchantment.matchXEnchantment("SOUL_SPEED").ifPresent(enchantment -> boots.addUnsafeEnchantment(enchantment.getEnchant(), 3));
yield boots;
}
};
}
private ItemStack namedItem(String id, String materialName, String name, String lore, int armor,
double toughness, double knockback) {
return namedItem(id, materialName, name, new String[]{lore}, armor, toughness, knockback, EquipmentSlot.HAND);
}
private ItemStack namedItem(String id, String materialName, String name, String lore1, String lore2,
int armor, double toughness, double knockback) {
return namedItem(id, materialName, name, new String[]{lore1, lore2}, armor, toughness, knockback, EquipmentSlot.HAND);
}
private ItemStack namedItem(String id, String materialName, String name, String lore1, String lore2,
String lore3, String lore4, int armor, double toughness, double knockback) {
return namedItem(id, materialName, name, new String[]{lore1, lore2, lore3, lore4}, armor, toughness, knockback, EquipmentSlot.HAND);
}
private ItemStack namedItem(String id, String materialName, String name, String lore1, String lore2,
String lore3, String lore4, String lore5, int armor, double toughness, double knockback) {
return namedItem(id, materialName, name, new String[]{lore1, lore2, lore3, lore4, lore5}, armor, toughness, knockback, EquipmentSlot.HAND);
}
private ItemStack namedItem(String id, String materialName, String name, String lore1, String lore2,
String lore3, String lore4, String lore5, String lore6, int armor,
double toughness, double knockback, EquipmentSlot slot) {
return namedItem(id, materialName, name, new String[]{lore1, lore2, lore3, lore4, lore5, lore6}, armor, toughness, knockback, slot);
}
private ItemStack namedItem(String id, String materialName, String name, String[] lore,
int armor, double toughness, double knockback, EquipmentSlot slot) {
Material material = material(materialName);
if (material == air()) {
getLogger().warning("Could not resolve material " + materialName + " for item " + id);
return new ItemStack(material);
}
ItemStack item = new ItemStack(material);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(color(name));
meta.setLore(java.util.Arrays.stream(lore).map(this::color).toList());
meta.getPersistentDataContainer().set(itemIdKey, PersistentDataType.STRING, id);
XItemFlag.of("HIDE_ATTRIBUTES").ifPresent(flag -> flag.set(meta));
if (armor > 0) {
XAttribute.of("ARMOR").ifPresent(attribute -> meta.addAttributeModifier(attribute.get(),
XAttribute.createModifier("kodari:" + id + "_armor", armor, AttributeModifier.Operation.ADD_NUMBER, slot)));
}
if (toughness > 0) {
XAttribute.of("ARMOR_TOUGHNESS").ifPresent(attribute -> meta.addAttributeModifier(attribute.get(),
XAttribute.createModifier("kodari:" + id + "_toughness", toughness, AttributeModifier.Operation.ADD_NUMBER, slot)));
}
if (knockback > 0) {
XAttribute.of("KNOCKBACK_RESISTANCE").ifPresent(attribute -> meta.addAttributeModifier(attribute.get(),
XAttribute.createModifier("kodari:" + id + "_knockback", knockback, AttributeModifier.Operation.ADD_NUMBER, slot)));
}
item.setItemMeta(meta);