83 lines
2.5 KiB
Java
83 lines
2.5 KiB
Java
package com.alttd.hunger_games.services;
|
|
|
|
import com.alttd.hunger_games.config.LootItems;
|
|
import com.alttd.hunger_games.data_objects.LootItem;
|
|
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<LootItem> possibleItems = LootItems.ITEMS.get(rarity);
|
|
|
|
if (possibleItems == null || possibleItems.isEmpty()) {
|
|
log.warn("No items found for rarity: {}", rarity);
|
|
continue;
|
|
}
|
|
|
|
LootItem lootItem = possibleItems.get(random.nextInt(possibleItems.size()));
|
|
if (lootItem == null || lootItem.material() == null) {
|
|
log.warn("LootItem or material is null for rarity: {}", rarity);
|
|
continue;
|
|
}
|
|
int amount = lootItem.maxAmount() > 1 ? random.nextInt(lootItem.maxAmount()) + 1 : 1;
|
|
inventory.setItem(slot, new ItemStack(lootItem.material(), amount));
|
|
}
|
|
|
|
return inventory;
|
|
}
|
|
|
|
private RARITY decideRarity() {
|
|
double roll = random.nextDouble();
|
|
// 5%
|
|
if (roll < 0.05) {
|
|
return RARITY.EPIC;
|
|
}
|
|
// 10%
|
|
if (roll < 0.15) {
|
|
return RARITY.RARE;
|
|
}
|
|
// 25%
|
|
if (roll < 0.40) {
|
|
return RARITY.UNCOMMON;
|
|
}
|
|
// 60%
|
|
return RARITY.COMMON;
|
|
}
|
|
|
|
public void clear() {
|
|
chestInventories.clear();
|
|
}
|
|
}
|