Files
src
build.gradle
README.md
settings.gradle
Code
7 1,381
src/main/java/de/kodari/saroitem/CustomItemCreatorPlugin.java
package de.kodari.saroitem;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitTask;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class CustomItemCreatorPlugin extends org.bukkit.plugin.java.JavaPlugin implements Listener, CommandExecutor {
private static final String[] ABILITIES = {"NONE", "HEAL", "SPEED", "JUMP", "STRENGTH", "RESISTANCE", "REGENERATION", "ABSORPTION"};
private static final String[] TRIGGERS = {"RIGHT_CLICK", "HIT", "DAMAGE_TAKEN"};
private final Map<String, ItemDefinition> definitions = new LinkedHashMap<>();
private final Map<UUID, PendingEdit> pendingEdits = new LinkedHashMap<>();
private final Map<String, Long> cooldowns = new LinkedHashMap<>();
private NamespacedKey definitionKey;
private File definitionsFile;
@Override
public void onEnable() {
saveDefaultConfig();
definitionKey = new NamespacedKey(this, "definition_id");
definitionsFile = new File(getDataFolder(), "items.yml");
loadDefinitions();
Bukkit.getPluginManager().registerEvents(this, this);
if (getCommand("saroitem") != null) getCommand("saroitem").setExecutor(this);
Bukkit.getScheduler().runTaskTimer(this, this::applyArmorSetBonuses, 20L, 40L);
}
private void loadDefinitions() {
definitions.clear();
if (!definitionsFile.exists()) return;
YamlConfiguration yaml = YamlConfiguration.loadConfiguration(definitionsFile);
ConfigurationSection root = yaml.getConfigurationSection("items");
if (root == null) return;
for (String id : root.getKeys(false)) {
ConfigurationSection section = root.getConfigurationSection(id);
if (section == null) continue;
ItemDefinition definition = new ItemDefinition();
definition.setId(id);
definition.setCategory(section.getString("category", "ITEM"));
definition.setMaterial(section.getString("material", "STONE"));
definition.setDisplayName(section.getString("display-name", id));
definition.setLore(section.getStringList("lore"));
definition.setRarity(section.getString("rarity", "COMMON"));
definition.setPrefix(section.getString("prefix", ""));
Map<String, Integer> enchants = new LinkedHashMap<>();
ConfigurationSection enchantSection = section.getConfigurationSection("enchantments");
if (enchantSection != null) {
for (String enchant : enchantSection.getKeys(false)) enchants.put(enchant, enchantSection.getInt(enchant, 1));
}
definition.setEnchantments(enchants);
definition.setDurabilityPercent(section.getInt("durability-percent", 100));
definition.setGlow(section.getBoolean("glow", false));
definition.setStackSize(section.getInt("stack-size", 1));
definition.setCustomModelData(section.getInt("custom-model-data", -1));
definition.setArmor(section.getDouble("armor", 0));
definition.setArmorToughness(section.getDouble("armor-toughness", 0));
definition.setActiveAbility(section.getString("active-ability", "NONE"));
definition.setPassiveAbility(section.getString("passive-ability", "NONE"));
definition.setTrigger(section.getString("trigger", "RIGHT_CLICK"));
definition.setCooldownSeconds(section.getInt("cooldown-seconds", 5));
definition.setArmorSet(section.getString("armor-set", ""));
definition.setSetBonus(section.getString("set-bonus", "NONE"));
definition.setRequiredSetPieces(section.getInt("required-set-pieces", 4));
definitions.put(id.toLowerCase(Locale.ROOT), definition);
}
}
private void saveDefinitions() {
if (definitionsFile.exists() && definitionsFile.length() > 0) createBackup();
YamlConfiguration yaml = new YamlConfiguration();
for (ItemDefinition definition : definitions.values()) {
String path = "items." + definition.getId();
yaml.set(path + ".category", definition.getCategory());
yaml.set(path + ".material", definition.getMaterial());
yaml.set(path + ".display-name", definition.getDisplayName());
yaml.set(path + ".lore", definition.getLore());
yaml.set(path + ".rarity", definition.getRarity());
yaml.set(path + ".prefix", definition.getPrefix());
yaml.set(path + ".enchantments", definition.getEnchantments());
yaml.set(path + ".durability-percent", definition.getDurabilityPercent());
yaml.set(path + ".glow", definition.isGlow());
yaml.set(path + ".stack-size", definition.getStackSize());
yaml.set(path + ".custom-model-data", definition.getCustomModelData());
yaml.set(path + ".armor", definition.getArmor());
yaml.set(path + ".armor-toughness", definition.getArmorToughness());
yaml.set(path + ".active-ability", definition.getActiveAbility());
yaml.set(path + ".passive-ability", definition.getPassiveAbility());
yaml.set(path + ".trigger", definition.getTrigger());
yaml.set(path + ".cooldown-seconds", definition.getCooldownSeconds());
yaml.set(path + ".armor-set", definition.getArmorSet());
yaml.set(path + ".set-bonus", definition.getSetBonus());
yaml.set(path + ".required-set-pieces", definition.getRequiredSetPieces());
}
File temporary = new File(getDataFolder(), "items.yml.tmp");
try {
if (!getDataFolder().exists()) getDataFolder().mkdirs();
yaml.save(temporary);
try {
Files.move(temporary.toPath(), definitionsFile.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
} catch (IOException ignored) {
Files.move(temporary.toPath(), definitionsFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException exception) {
getLogger().severe("Konnte items.yml nicht speichern: " + exception.getMessage());
}
}
private void createBackup() {
File directory = new File(getDataFolder(), "backups");
if (!directory.exists() && !directory.mkdirs()) return;
String timestamp = new SimpleDateFormat("yyyyMMdd-HHmmss-SSS").format(new Date());
try {
Files.copy(definitionsFile.toPath(), new File(directory, "items-" + timestamp + ".yml").toPath());
File[] backups = directory.listFiles((dir, name) -> name.endsWith(".yml"));
if (backups != null && backups.length > 10) {
Arrays.sort(backups, (a, b) -> Long.compare(a.lastModified(), b.lastModified()));
for (int i = 0; i < backups.length - 10; i++) Files.deleteIfExists(backups[i].toPath());
}
} catch (IOException exception) {
getLogger().warning("Backup konnte nicht erstellt werden: " + exception.getMessage());
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!allowed(sender, "use")) return true;
if (args.length == 0 || args[0].equalsIgnoreCase("menu")) {
if (sender instanceof Player player) openHome(player);
else send(sender, "&cDieser Befehl muss im Spiel ausgeführt werden.");
return true;
}
String sub = args[0].toLowerCase(Locale.ROOT);
switch (sub) {
case "help" -> sendHelp(sender);
case "list" -> commandList(sender);
case "create" -> commandCreate(sender, args);
case "give" -> commandGive(sender, args);
case "copy" -> commandCopy(sender, args);
case "delete" -> commandDelete(sender, args);
case "search" -> commandSearch(sender, args);
case "reload" -> {
if (!allowed(sender, "reload")) return true;
reloadConfig();
loadDefinitions();
send(sender, "&aKonfiguration und Items neu geladen.");
}
default -> sendHelp(sender);
}
return true;
}
private void sendHelp(CommandSender sender) {
send(sender, "&b/saroitem &7- Hauptmenü");
send(sender, "&b/saroitem list &7- Definitionen auflisten");
send(sender, "&b/saroitem create <id> [material] &7- Item erstellen");
send(sender, "&b/saroitem give <spieler> <id> [anzahl] &7- Item geben");