HungerGames/src/main/java/com/alttd/hunger_games/services/LootService.java

67 lines
2.1 KiB
Java

package com.alttd.hunger_games.services;
import com.alttd.hunger_games.config.LootItems;
import com.alttd.hunger_games.data_objects.RARITY;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
@Slf4j
public class LootService {
private final HashMap<Location, Inventory> chestInventories = new HashMap<>();
private final Random random = ThreadLocalRandom.current();
public Inventory getChestInventory(Location location) {
if (chestInventories.containsKey(location)) {
return chestInventories.get(location);
}
Inventory inventory = generateLoot();
chestInventories.put(location, inventory);
return inventory;
}
private Inventory generateLoot() {
Inventory inventory = Bukkit.createInventory(null, 27, Component.text("Chest"));
int itemCount = random.nextInt(3, 7);
for (int i = 0; i < itemCount; i++) {
int slot = random.nextInt(27);
RARITY rarity = decideRarity();
List<Material> possibleItems = LootItems.ITEMS.get(rarity);
if (possibleItems == null || possibleItems.isEmpty()) {
log.warn("No items found for rarity: {}", rarity);
continue;
}
Material material = possibleItems.get(random.nextInt(possibleItems.size()));
inventory.setItem(slot, new ItemStack(material));
}
return inventory;
}
private RARITY decideRarity() {
double roll = random.nextDouble();
if (roll < 0.05) return RARITY.EPIC; // 5%
if (roll < 0.15) return RARITY.RARE; // 10%
if (roll < 0.40) return RARITY.UNCOMMON;// 25%
return RARITY.COMMON; // 60%
}
public void clear() {
chestInventories.clear();
}
}