Files
src
build.gradle
settings.gradle
Code
13 713
src/main/java/com/kodari/abysssmp/AbyssSMP.java
package com.kodari.abysssmp;
import com.kodari.abysssmp.command.AbyssCommand;
import com.kodari.abysssmp.listener.AbyssListener;
import com.kodari.abysssmp.resourcepack.ResourcePackService;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
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.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public final class AbyssSMP extends JavaPlugin {
private static final int SHARD_MODEL_DATA = 10001;
private static final int WEAPON_MODEL_DATA = 10002;
private ResourcePackService resourcePackService;
private NamespacedKey weaponKey;
@Override
public void onEnable() {
saveDefaultConfig();
weaponKey = new NamespacedKey(this, "abyss_reaver");
resourcePackService = new ResourcePackService(this);
resourcePackService.start();
registerWeaponRecipe();
getCommand("abyss").setExecutor(new AbyssCommand(this));
getServer().getPluginManager().registerEvents(new AbyssListener(this), this);
getLogger().info("AbyssSMP enabled.");
}
@Override
public void onDisable() {
if (resourcePackService != null) {
resourcePackService.close();
}
}
public ItemStack createShardItem(int amount) {
ItemStack item = XMaterial.matchXMaterial("AMETHYST_SHARD")
.map(XMaterial::parseItem)
.orElse(null);
if (item == null) {
getLogger().warning("Could not create an Abyss Shard: AMETHYST_SHARD is unavailable.");
return null;
}
item.setAmount(amount);
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return null;
}
meta.setDisplayName(color(getConfig().getString("shards.item-name", "&5&lAbyss Shard")));
List<String> lore = new ArrayList<>();
for (String line : getConfig().getStringList("shards.item-lore")) {
lore.add(color(line));
}
meta.setLore(lore);
meta.setCustomModelData(SHARD_MODEL_DATA);
item.setItemMeta(meta);
return item;
}
public boolean isShard(ItemStack item) {
if (item == null || item.getType().isAir() || !item.hasItemMeta()) {
return false;
}
ItemMeta meta = item.getItemMeta();
return meta != null && meta.hasCustomModelData() && meta.getCustomModelData() == SHARD_MODEL_DATA;
}
public ItemStack createWeaponItem() {
ItemStack item = XMaterial.matchXMaterial("NETHERITE_SWORD")
.map(XMaterial::parseItem)
.orElse(null);
if (item == null) {
getLogger().warning("Could not create the Abyss Reaver: NETHERITE_SWORD is unavailable.");
return null;
}
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return null;
}
meta.setDisplayName(color("&5&lAbyss Reaver"));
meta.setLore(List.of(
color("&8Forged where the void has no bottom."),
color("&dRight-click: &5Rift Rend"),
color("&7Tears a rift through foes in front of you."),
color("&8Ability cooldown: 14 seconds")
));
meta.setCustomModelData(WEAPON_MODEL_DATA);
meta.getPersistentDataContainer().set(weaponKey, PersistentDataType.BYTE, (byte) 1);
XEnchantment.matchXEnchantment("SHARPNESS")
.ifPresent(enchantment -> meta.addEnchant(enchantment.getEnchant(), 5, true));
XEnchantment.matchXEnchantment("UNBREAKING")
.ifPresent(enchantment -> meta.addEnchant(enchantment.getEnchant(), 3, true));
XEnchantment.matchXEnchantment("MENDING")
.ifPresent(enchantment -> meta.addEnchant(enchantment.getEnchant(), 1, true));
item.setItemMeta(meta);
return item;
}
public boolean isAbyssWeapon(ItemStack item) {
if (item == null || !item.hasItemMeta()) {
return false;
}
ItemMeta meta = item.getItemMeta();
return meta != null && meta.getPersistentDataContainer().has(weaponKey, PersistentDataType.BYTE);
}
public void giveWeapon(Player player) {
ItemStack weapon = createWeaponItem();
if (weapon == null) {
return;
}
Map<Integer, ItemStack> overflow = player.getInventory().addItem(weapon);
for (ItemStack item : overflow.values()) {
player.getWorld().dropItemNaturally(player.getLocation(), item);
}
}
private void registerWeaponRecipe() {
ItemStack weapon = createWeaponItem();
ItemStack shard = createShardItem(1);
org.bukkit.Material swordMaterial = XMaterial.matchXMaterial("NETHERITE_SWORD")
.map(XMaterial::parseMaterial)
.orElse(null);
if (weapon == null || shard == null || swordMaterial == null) {
getLogger().warning("Could not register the Abyss Reaver recipe because a required item is unavailable.");
return;
}
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, "abyss_reaver"), weapon);
recipe.shape(" A ", "ASA", " A ");
recipe.setIngredient('A', new RecipeChoice.ExactChoice(shard));
recipe.setIngredient('S', swordMaterial);
getServer().addRecipe(recipe);
}
public int countShards(Player player) {
int total = 0;
for (ItemStack item : player.getInventory().getStorageContents()) {
if (isShard(item)) {
total += item.getAmount();
}
}
return total;
}
public void giveShards(Player player, int amount) {
int remaining = amount;
while (remaining > 0) {
ItemStack shard = createShardItem(Math.min(remaining, 64));
if (shard == null) {
return;
}
Map<Integer, ItemStack> overflow = player.getInventory().addItem(shard);
for (ItemStack item : overflow.values()) {
player.getWorld().dropItemNaturally(player.getLocation(), item);
}
remaining -= shard.getAmount();
}
}
public ResourcePackService getResourcePackService() {
return resourcePackService;
}
public static String color(String text) {
return ChatColor.translateAlternateColorCodes('&', text == null ? "" : text);
}
}