Files
src
build.gradle
settings.gradle
Code
5 521
src/main/java/com/kodari/totemgrave/TotemGravePlugin.java
package com.kodari.totemgrave;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Container;
import org.bukkit.block.DoubleChest;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ItemFrame;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class TotemGravePlugin extends JavaPlugin implements Listener, CommandExecutor {
private XMaterial totemType;
@Override
public void onEnable() {
saveDefaultConfig();
totemType = XMaterial.matchXMaterial("TOTEM_OF_UNDYING").orElse(null);
if (totemType == null) {
getLogger().severe("Could not resolve TOTEM_OF_UNDYING; disabling plugin.");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
registerViataRecipe();
getServer().getPluginManager().registerEvents(this, this);
getCommand("totemgrave").setExecutor(this);
}
private void registerViataRecipe() {
ItemStack result = totemType.parseItem();
if (result == null) {
getLogger().severe("Could not create the Viata totem recipe result.");
return;
}
ItemMeta meta = result.getItemMeta();
meta.setDisplayName(ChatColor.GOLD + "viata");
result.setItemMeta(meta);
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, "viata_totem"), result);
java.util.List<String> shape = getConfig().getStringList("recipe.shape");
if (shape.isEmpty() || shape.size() > 3) {
getLogger().severe("recipe.shape must contain between 1 and 3 rows.");
return;
}
recipe.shape(shape.toArray(new String[0]));
Map<Character, Material> ingredients = new HashMap<>();
if (getConfig().getConfigurationSection("recipe.ingredients") == null) {
getLogger().severe("Missing recipe.ingredients in config.yml.");
return;
}
for (String key : getConfig().getConfigurationSection("recipe.ingredients").getKeys(false)) {
if (key.length() != 1 || key.charAt(0) == ' ') {
getLogger().severe("Recipe ingredient keys must be one non-space character: " + key);
return;
}
String materialName = getConfig().getString("recipe.ingredients." + key);
Material material = materialName == null ? null : XMaterial.matchXMaterial(materialName)
.map(XMaterial::parseMaterial)
.orElse(null);
if (material == null) {
getLogger().severe("Unknown recipe material for key " + key + ": " + materialName);
return;
}
ingredients.put(key.charAt(0), material);
}
for (String row : shape) {
for (char symbol : row.toCharArray()) {
if (symbol != ' ' && !ingredients.containsKey(symbol)) {
getLogger().severe("No recipe ingredient is configured for symbol: " + symbol);
return;
}
}
}
ingredients.forEach(recipe::setIngredient);
Bukkit.addRecipe(recipe);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("This command can only be used by a player.");
return true;
}
if (args.length != 1) {
player.sendMessage(ChatColor.YELLOW + "Usage: /totemgrave <bind|unbind>");
return true;
}
if (args[0].equalsIgnoreCase("bind")) {
Entity targetedEntity = player.getTargetEntity(5);
if (targetedEntity instanceof ItemFrame frame) {
if (!isTotem(frame.getItem())) {
player.sendMessage(ChatColor.RED + "The item frame you are looking at must contain a totem.");
return true;
}
String playerId = player.getUniqueId().toString();
if (isFrameBoundToAnotherPlayer(frame.getUniqueId(), playerId)) {
player.sendMessage(ChatColor.RED + "That item frame is already bound to another player.");
return true;
}
String framePath = "graves." + playerId + ".frames." + frame.getUniqueId();
getConfig().set(framePath + ".world", frame.getWorld().getName());
getConfig().set(framePath + ".x", frame.getLocation().getBlockX());
getConfig().set(framePath + ".y", frame.getLocation().getBlockY());
getConfig().set(framePath + ".z", frame.getLocation().getBlockZ());
saveConfig();
player.sendMessage(ChatColor.GREEN + "Totem item frame bound to your grave. Bind each of its other totem frames too.");
return true;
}
Block target = player.getTargetBlock(Collections.emptySet(), 5);
BlockState state = target.getState();
if (!(state instanceof Container)) {
player.sendMessage(ChatColor.RED + "Look at your grave's chest or other container, within 5 blocks.");
return true;
}
if (isBoundToAnotherPlayer(target, player.getUniqueId().toString())) {
player.sendMessage(ChatColor.RED + "That grave is already bound to another player.");
return true;
}
String path = "graves." + player.getUniqueId();
getConfig().set(path + ".world", player.getWorld().getName());
getConfig().set(path + ".x", target.getX());
getConfig().set(path + ".y", target.getY());
getConfig().set(path + ".z", target.getZ());
saveConfig();
player.sendMessage(ChatColor.GREEN + "Your grave is bound. One totem will be removed from it each time you die.");
return true;
}
if (args[0].equalsIgnoreCase("unbind")) {
getConfig().set("graves." + player.getUniqueId(), null);
saveConfig();
player.sendMessage(ChatColor.GREEN + "Your grave has been unbound.");
return true;
}
player.sendMessage(ChatColor.YELLOW + "Usage: /totemgrave <bind|unbind>");
return true;
}
@EventHandler
public void onGraveInventoryClick(InventoryClickEvent event) {
if (!(event.getWhoClicked() instanceof Player player) || player.getGameMode() == GameMode.CREATIVE) {
return;
}
Inventory graveInventory = event.getView().getTopInventory();
if (!isBoundGraveInventory(graveInventory)) {
return;
}
Inventory clickedInventory = event.getClickedInventory();
boolean movesTotem = false;
if (clickedInventory == graveInventory) {
movesTotem |= isTotem(event.getCurrentItem()) || isTotem(event.getCursor());
if (event.getClick() == ClickType.NUMBER_KEY) {
int hotbarButton = event.getHotbarButton();