Files
src
build.gradle
settings.gradle
Code
8 910
src/main/java/com/kodari/keypad/KeypadPlugin.java
package com.kodari.keypad;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.InventoryHolder;
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.Arrays;
public final class KeypadPlugin extends JavaPlugin {
private NamespacedKey keypadItemKey;
private KeypadManager keypadManager;
private KeypadListener keypadListener;
@Override
public void onEnable() {
keypadItemKey = new NamespacedKey(this, "keypad_item");
keypadManager = new KeypadManager(this);
keypadListener = new KeypadListener(this, keypadManager);
Bukkit.getPluginManager().registerEvents(keypadListener, this);
getCommand("keypadprotection").setExecutor(this);
registerRecipe();
Bukkit.getScheduler().runTaskTimer(this, keypadManager::showMarkers, 10L, 10L);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("Este comando solo puede ejecutarlo un jugador OP.");
return true;
}
if (!player.isOp()) {
player.sendMessage("§cSolo los operadores pueden usar este comando.");
return true;
}
if (args.length != 1 || !args[0].equalsIgnoreCase("remove")) {
player.sendMessage("§eUso: /keypadprotection remove");
return true;
}
Block target = player.getTargetBlockExact(6);
if (target == null || !(target.getState() instanceof InventoryHolder)) {
player.sendMessage("§cMira directamente a un contenedor compatible.");
return true;
}
if (keypadManager.getLock(target.getLocation()) == null) {
player.sendMessage("§cEse contenedor no tiene un keypad.");
return true;
}
keypadManager.removeLock(target.getLocation());
giveKeypad(player);
player.sendMessage("§aKeypad retirado; recibiste el objeto.");
return true;
}
public ItemStack createKeypadItem() {
ItemStack item = XMaterial.matchXMaterial("IRON_INGOT")
.map(XMaterial::parseItem)
.orElse(null);
if (item == null) {
return null;
}
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("§bKeypad");
meta.setLore(Arrays.asList("§7Colócalo en un contenedor", "§7para protegerlo con permisos."));
meta.getPersistentDataContainer().set(keypadItemKey, PersistentDataType.BYTE, (byte) 1);
item.setItemMeta(meta);
return item;
}
public boolean isKeypad(ItemStack item) {
if (item == null || !item.hasItemMeta()) {
return false;
}
return item.getItemMeta().getPersistentDataContainer().has(keypadItemKey, PersistentDataType.BYTE);
}
public KeypadManager getKeypadManager() {
return keypadManager;
}
public void giveKeypad(Player player) {
ItemStack item = createKeypadItem();
if (item == null) {
return;
}
player.getInventory().addItem(item).values().forEach(remaining ->
player.getWorld().dropItemNaturally(player.getLocation(), remaining));
}
public void playSound(Player player, String sound) {
XSound.matchXSound(sound).ifPresent(xSound -> xSound.play(player));
}
private void registerRecipe() {
Material iron = XMaterial.matchXMaterial("IRON_INGOT")
.map(XMaterial::parseMaterial)
.orElse(null);
Material glassPane = XMaterial.matchXMaterial("GLASS_PANE")
.map(XMaterial::parseMaterial)
.orElse(null);
ItemStack result = createKeypadItem();
if (iron == null || glassPane == null || result == null) {
getLogger().warning("No se pudo registrar la receta del keypad: faltan materiales compatibles.");
return;
}
NamespacedKey recipeKey = new NamespacedKey(this, "keypad_recipe");
ShapedRecipe recipe = new ShapedRecipe(recipeKey, result);
recipe.shape(" I ", "IPI", " I ");
recipe.setIngredient('I', iron);
recipe.setIngredient('P', glassPane);
Bukkit.addRecipe(recipe);
}
}