modified: src/main/java/com/simolzimol/levelcraft/LevelCraft.java
modified: src/main/java/com/simolzimol/levelcraft/item/ItemUtil.java new file: src/main/java/com/simolzimol/levelcraft/listener/CraftListener.java
This commit is contained in:
@@ -7,6 +7,7 @@ public class LevelCraft extends JavaPlugin {
|
||||
public void onEnable() {
|
||||
getLogger().info("LevelCraft aktiviert!");
|
||||
com.simolzimol.levelcraft.item.ItemUtil.init(this);
|
||||
getServer().getPluginManager().registerEvents(new com.simolzimol.levelcraft.listener.CraftListener(), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,23 +2,52 @@ package com.simolzimol.levelcraft.item;
|
||||
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ItemUtil {
|
||||
private static NamespacedKey rarityKey;
|
||||
private static NamespacedKey uuidKey;
|
||||
|
||||
public static void init(Plugin plugin) {
|
||||
rarityKey = new NamespacedKey(plugin, "rarity");
|
||||
uuidKey = new NamespacedKey(plugin, "unique_id");
|
||||
}
|
||||
|
||||
public static void setRarity(ItemStack item, Rarity rarity) {
|
||||
item.getItemMeta().getPersistentDataContainer().set(rarityKey, PersistentDataType.INTEGER, rarity.getValue());
|
||||
// Optional: ItemMeta updaten (z.B. Lore, Name)
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.getPersistentDataContainer().set(rarityKey, PersistentDataType.INTEGER, rarity.getValue());
|
||||
// Optional: Lore aktualisieren
|
||||
List<String> lore = meta.hasLore() ? meta.getLore() : new java.util.ArrayList<>();
|
||||
lore.add("§7Seltenheit: §f" + rarity.getDisplayName());
|
||||
meta.setLore(lore);
|
||||
item.setItemMeta(meta);
|
||||
}
|
||||
|
||||
public static Rarity getRarity(ItemStack item) {
|
||||
Integer value = item.getItemMeta().getPersistentDataContainer().get(rarityKey, PersistentDataType.INTEGER);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta == null) return Rarity.COMMON;
|
||||
Integer value = meta.getPersistentDataContainer().get(rarityKey, PersistentDataType.INTEGER);
|
||||
return value == null ? Rarity.COMMON : Rarity.fromValue(value);
|
||||
}
|
||||
|
||||
public static void assignUniqueId(ItemStack item) {
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta == null) return;
|
||||
if (!meta.getPersistentDataContainer().has(uuidKey, PersistentDataType.STRING)) {
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
meta.getPersistentDataContainer().set(uuidKey, PersistentDataType.STRING, uuid);
|
||||
item.setItemMeta(meta);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getUniqueId(ItemStack item) {
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta == null) return null;
|
||||
return meta.getPersistentDataContainer().get(uuidKey, PersistentDataType.STRING);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.simolzimol.levelcraft.listener;
|
||||
|
||||
import com.simolzimol.levelcraft.item.ItemUtil;
|
||||
import com.simolzimol.levelcraft.item.Rarity;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.CraftItemEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class CraftListener implements Listener {
|
||||
private final Random random = new Random();
|
||||
|
||||
@EventHandler
|
||||
public void onCraft(CraftItemEvent event) {
|
||||
ItemStack result = event.getCurrentItem();
|
||||
if (result == null) return;
|
||||
|
||||
// Rarity für gecraftete Items: -1 bis 3
|
||||
int rarityValue = random.nextInt(5) - 1; // -1,0,1,2,3
|
||||
if (rarityValue > 3) rarityValue = 3;
|
||||
if (rarityValue < -1) rarityValue = -1;
|
||||
|
||||
Rarity rarity = Rarity.fromValue(rarityValue);
|
||||
ItemUtil.setRarity(result, rarity);
|
||||
ItemUtil.assignUniqueId(result);
|
||||
event.setCurrentItem(result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user