modified: src/main/java/com/simolzimol/levelcraft/item/ItemUtil.java

modified:   src/main/java/com/simolzimol/levelcraft/item/Rarity.java
	modified:   src/main/java/com/simolzimol/levelcraft/listener/CraftListener.java
This commit is contained in:
SimolZimol
2025-05-23 21:27:40 +02:00
parent d762e55465
commit 81d048f238
3 changed files with 34 additions and 21 deletions

View File

@@ -21,9 +21,9 @@ public class ItemUtil {
public static void setRarity(ItemStack item, Rarity rarity) { public static void setRarity(ItemStack item, Rarity rarity) {
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
meta.getPersistentDataContainer().set(rarityKey, PersistentDataType.INTEGER, rarity.getValue()); meta.getPersistentDataContainer().set(rarityKey, PersistentDataType.INTEGER, rarity.getValue());
// Optional: Lore aktualisieren // Lore aktualisieren (alte Zeile ersetzen)
List<String> lore = meta.hasLore() ? meta.getLore() : new java.util.ArrayList<>(); java.util.List<String> lore = meta.hasLore() ? meta.getLore() : new java.util.ArrayList<>();
lore.add("§7Seltenheit: §f" + rarity.getDisplayName()); lore.add(rarity.getDisplayName() + "§7 Seltenheit");
meta.setLore(lore); meta.setLore(lore);
item.setItemMeta(meta); item.setItemMeta(meta);
} }

View File

@@ -1,22 +1,24 @@
package com.simolzimol.levelcraft.item; package com.simolzimol.levelcraft.item;
public enum Rarity { public enum Rarity {
CURSED(-1, "Verflucht"), CURSED(-1, "§8Verflucht", 0.02), // Sehr selten, negativ
COMMON(0, "Gewöhnlich"), COMMON(0, "§7Gewöhnlich", 0.50), // Am häufigsten (50%)
UNCOMMON(1, "Ungewöhnlich"), UNCOMMON(1, "§aUngewöhnlich", 0.20), // 20%
RARE(2, "Selten"), RARE(2, "§9Selten", 0.12), // 12%
EPIC(3, "Episch"), EPIC(3, "§5Episch", 0.08), // 8%
LEGENDARY(4, "Legendär"), LEGENDARY(4, "§6Legendär", 0.05), // 5%
MYTHIC(5, "Mythisch"), MYTHIC(5, "§dMythisch", 0.025), // 2.5%
ANCIENT(6, "Uralte"), ANCIENT(6, "§eUralte", 0.012), // 1.2%
DIVINE(7, "Göttlich"); DIVINE(7, "§bGöttlich", 0.003); // 0.3%
private final int value; private final int value;
private final String displayName; private final String displayName;
private final double chance;
Rarity(int value, String displayName) { Rarity(int value, String displayName, double chance) {
this.value = value; this.value = value;
this.displayName = displayName; this.displayName = displayName;
this.chance = chance;
} }
public int getValue() { public int getValue() {
@@ -27,10 +29,27 @@ public enum Rarity {
return displayName; return displayName;
} }
public double getChance() {
return chance;
}
public static Rarity fromValue(int value) { public static Rarity fromValue(int value) {
for (Rarity r : values()) { for (Rarity r : values()) {
if (r.value == value) return r; if (r.value == value) return r;
} }
return COMMON; return COMMON;
} }
// Zufällige Rarity nach Gewichtung (ohne CURSED)
public static Rarity getRandomRarity(java.util.Random random, boolean allowCursed, int maxRarity) {
double roll = random.nextDouble();
double cumulative = 0.0;
for (Rarity rarity : values()) {
if (rarity.value < (allowCursed ? -1 : 0)) continue;
if (rarity.value > maxRarity) continue;
cumulative += rarity.chance;
if (roll < cumulative) return rarity;
}
return COMMON;
}
} }

View File

@@ -16,16 +16,10 @@ public class CraftListener implements Listener {
public void onCraft(CraftItemEvent event) { public void onCraft(CraftItemEvent event) {
ItemStack result = event.getCurrentItem(); ItemStack result = event.getCurrentItem();
if (result == null) return; if (result == null) return;
// Nur Items mit Haltbarkeit (Waffen, Rüstung, Werkzeuge)
if (result.getType().getMaxDurability() <= 0) return; if (result.getType().getMaxDurability() <= 0) return;
// Rarity für gecraftete Items: -1 bis 3 // Gecraftete Items: -1 (selten) bis 3 (häufig)
int rarityValue = random.nextInt(5) - 1; // -1,0,1,2,3 Rarity rarity = Rarity.getRandomRarity(random, true, 3);
if (rarityValue > 3) rarityValue = 3;
if (rarityValue < -1) rarityValue = -1;
Rarity rarity = Rarity.fromValue(rarityValue);
ItemUtil.setRarity(result, rarity); ItemUtil.setRarity(result, rarity);
ItemUtil.assignUniqueId(result); ItemUtil.assignUniqueId(result);
event.setCurrentItem(result); event.setCurrentItem(result);