Compare commits

..

3 Commits

7 changed files with 194 additions and 4 deletions

View File

@ -3,9 +3,11 @@ package com.alttd.hunger_games;
import com.alttd.hunger_games.commands.BaseCommand;
import com.alttd.hunger_games.config.Config;
import com.alttd.hunger_games.config.Messages;
import com.alttd.hunger_games.event_listeners.ChestListener;
import com.alttd.hunger_games.event_listeners.PlayerDamageListener;
import com.alttd.hunger_games.event_listeners.PlayerDisconnectListener;
import com.alttd.hunger_games.event_listeners.PlayerJoinListener;
import com.alttd.hunger_games.services.LootService;
import com.alttd.hunger_games.services.PlayerService;
import com.alttd.hunger_games.services.PlayerTeleporterService;
import com.alttd.hunger_games.services.Round;
@ -21,6 +23,7 @@ public final class Main extends JavaPlugin {
private RoundService roundService;
private PlayerService playerService;
private PlayerTeleporterService playerTeleporterService;
private LootService lootService;
@Override
public void onEnable() {
@ -34,7 +37,8 @@ public final class Main extends JavaPlugin {
private void registerServices() {
round = Round.createSingletonInstance();
roundService = RoundService.createSingletonInstance(round, this);
lootService = new LootService();
roundService = RoundService.createSingletonInstance(round, this, lootService);
playerTeleporterService = PlayerTeleporterService.createSingletonInstance();
playerService = PlayerService.createSingletonInstance(round, roundService, playerTeleporterService);
}
@ -53,6 +57,7 @@ public final class Main extends JavaPlugin {
pluginManager.registerEvents(new PlayerDisconnectListener(playerService), this);
pluginManager.registerEvents(new PlayerJoinListener(playerService), this);
pluginManager.registerEvents(new PlayerDamageListener(roundService, playerService), this);
pluginManager.registerEvents(new ChestListener(roundService, lootService), this);
}
public void reloadConfigs() {

View File

@ -0,0 +1,45 @@
package com.alttd.hunger_games.config;
import com.alttd.hunger_games.data_objects.RARITY;
import org.bukkit.Material;
import java.io.File;
import java.util.HashMap;
import java.util.List;
public class LootItems extends AbstractConfig {
static LootItems config;
LootItems() {
super(
new File(File.separator
+ "mnt" + File.separator
+ "configs" + File.separator
+ "HungerGames"),
"loot-items.yml");
}
public static void reload() {
config = new LootItems();
config.readConfig(LootItems.class, null);
}
public static class ITEMS {
private static final String prefix = "items.";
private static final HashMap<RARITY, List<Material>> ITEMS = new HashMap<>();
public static List<Material> get(RARITY rarity) {
return ITEMS.get(rarity);
}
@SuppressWarnings("unused")
private static void load() {
for (RARITY rarity : RARITY.values()) {
List<String> materialNameList = ITEMS.get(rarity).stream().map(Material::name).toList();
List<String> materialList = config.getList(prefix, rarity.getConfigName(), materialNameList);
ITEMS.put(rarity, materialList.stream().map(Material::getMaterial).toList());
}
}
}
}

View File

@ -76,12 +76,14 @@ public class Messages extends AbstractConfig {
public static String WARMUP = "<gold>The Hunger Games are starting soon! Get ready!</gold>";
public static String STARTED = "<green><bold>The Hunger Games has begun! Good luck!</bold></green>";
public static String BORDER_SHRINK = "<red>The border is shrinking to <size> blocks!</red>";
public static String CHEST_EMPTY = "<red>This chest is empty!</red>";
@SuppressWarnings("unused")
private static void load() {
WARMUP = config.getString(prefix, "warmup", WARMUP);
STARTED = config.getString(prefix, "started", STARTED);
BORDER_SHRINK = config.getString(prefix, "border-shrink", BORDER_SHRINK);
CHEST_EMPTY = config.getString(prefix, "chest-empty", CHEST_EMPTY);
}
}

View File

@ -0,0 +1,15 @@
package com.alttd.hunger_games.data_objects;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Getter
public enum RARITY {
COMMON("common"),
UNCOMMON("uncommon"),
RARE("rare"),
EPIC("epic");
private final String configName;
}

View File

@ -0,0 +1,54 @@
package com.alttd.hunger_games.event_listeners;
import com.alttd.hunger_games.config.Messages;
import com.alttd.hunger_games.data_objects.PLAYER_STATE;
import com.alttd.hunger_games.data_objects.ROUND_STATE;
import com.alttd.hunger_games.services.LootService;
import com.alttd.hunger_games.services.RoundService;
import lombok.RequiredArgsConstructor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.Inventory;
@RequiredArgsConstructor
public class ChestListener implements Listener {
private final RoundService roundService;
private final LootService lootService;
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return;
}
Block block = event.getClickedBlock();
if (block == null || block.getType() != Material.CHEST) {
return;
}
ROUND_STATE roundState = roundService.getRoundState();
if (roundState != ROUND_STATE.SAFE_PHASE && roundState != ROUND_STATE.KILL_PHASE && roundState != ROUND_STATE.FINALE) {
return;
}
Player player = event.getPlayer();
if (roundService.getPlayerState(player.getUniqueId()).orElse(null) != PLAYER_STATE.REGISTERED) {
return;
}
event.setCancelled(true);
Inventory inventory = lootService.getChestInventory(block.getLocation());
if (inventory.isEmpty()) {
player.sendRichMessage(Messages.GAME.CHEST_EMPTY);
} else {
player.openInventory(inventory);
}
}
}

View File

@ -0,0 +1,66 @@
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();
}
}

View File

@ -17,22 +17,24 @@ public class RoundService implements RoundListener {
private static RoundService instance = null;
private PlayerMovementListener playerMovementListener;
private final Main main;
private final LootService lootService;
@Getter
private ROUND_STATE roundState;
private final HashMap<UUID, PLAYER_STATE> players = new HashMap<>();
public static RoundService createSingletonInstance(Round round, Main main) {
public static RoundService createSingletonInstance(Round round, Main main, LootService lootService) {
if (instance != null) {
throw new IllegalStateException("RoundService is already initialized.");
}
instance = new RoundService(round, main);
instance = new RoundService(round, main, lootService);
return instance;
}
private RoundService(Round round, Main main) {
private RoundService(Round round, Main main, LootService lootService) {
this.roundState = round.register(this);
this.main = main;
this.lootService = lootService;
}
public Set<UUID> getPlayers(PLAYER_STATE playerState) {
@ -74,6 +76,7 @@ public class RoundService implements RoundListener {
stopFreeze();
if (roundState.equals(ROUND_STATE.PLAYER_REGISTRATION)) {
clear();
lootService.clear();
}
if (roundState.equals(ROUND_STATE.COUNTDOWN)) {
startFreeze();