Introduce LootService for chest loot handling and ChestListener for in-game chest interactions
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package com.alttd.hunger_games.services;
|
||||
|
||||
import com.alttd.hunger_games.config.LootItems;
|
||||
import com.alttd.hunger_games.data_objects.RARITY;
|
||||
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;
|
||||
|
||||
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"));
|
||||
RARITY rarity = decideRarity();
|
||||
List<Material> possibleItems = LootItems.ITEMS.get(rarity);
|
||||
|
||||
if (possibleItems.isEmpty()) {
|
||||
return inventory;
|
||||
}
|
||||
|
||||
int itemCount = random.nextInt(3, 7);
|
||||
for (int i = 0; i < itemCount; i++) {
|
||||
int slot = random.nextInt(27);
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user