Introduce LootService for chest loot handling and ChestListener for in-game chest interactions
This commit is contained in:
parent
58d0b7fa19
commit
25c95dd46a
|
|
@ -3,9 +3,11 @@ package com.alttd.hunger_games;
|
||||||
import com.alttd.hunger_games.commands.BaseCommand;
|
import com.alttd.hunger_games.commands.BaseCommand;
|
||||||
import com.alttd.hunger_games.config.Config;
|
import com.alttd.hunger_games.config.Config;
|
||||||
import com.alttd.hunger_games.config.Messages;
|
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.PlayerDamageListener;
|
||||||
import com.alttd.hunger_games.event_listeners.PlayerDisconnectListener;
|
import com.alttd.hunger_games.event_listeners.PlayerDisconnectListener;
|
||||||
import com.alttd.hunger_games.event_listeners.PlayerJoinListener;
|
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.PlayerService;
|
||||||
import com.alttd.hunger_games.services.PlayerTeleporterService;
|
import com.alttd.hunger_games.services.PlayerTeleporterService;
|
||||||
import com.alttd.hunger_games.services.Round;
|
import com.alttd.hunger_games.services.Round;
|
||||||
|
|
@ -21,6 +23,7 @@ public final class Main extends JavaPlugin {
|
||||||
private RoundService roundService;
|
private RoundService roundService;
|
||||||
private PlayerService playerService;
|
private PlayerService playerService;
|
||||||
private PlayerTeleporterService playerTeleporterService;
|
private PlayerTeleporterService playerTeleporterService;
|
||||||
|
private LootService lootService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
|
|
@ -34,7 +37,8 @@ public final class Main extends JavaPlugin {
|
||||||
|
|
||||||
private void registerServices() {
|
private void registerServices() {
|
||||||
round = Round.createSingletonInstance();
|
round = Round.createSingletonInstance();
|
||||||
roundService = RoundService.createSingletonInstance(round, this);
|
lootService = new LootService();
|
||||||
|
roundService = RoundService.createSingletonInstance(round, this, lootService);
|
||||||
playerTeleporterService = PlayerTeleporterService.createSingletonInstance();
|
playerTeleporterService = PlayerTeleporterService.createSingletonInstance();
|
||||||
playerService = PlayerService.createSingletonInstance(round, roundService, playerTeleporterService);
|
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 PlayerDisconnectListener(playerService), this);
|
||||||
pluginManager.registerEvents(new PlayerJoinListener(playerService), this);
|
pluginManager.registerEvents(new PlayerJoinListener(playerService), this);
|
||||||
pluginManager.registerEvents(new PlayerDamageListener(roundService, playerService), this);
|
pluginManager.registerEvents(new PlayerDamageListener(roundService, playerService), this);
|
||||||
|
pluginManager.registerEvents(new ChestListener(roundService, lootService), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reloadConfigs() {
|
public void reloadConfigs() {
|
||||||
|
|
|
||||||
|
|
@ -29,10 +29,15 @@ public class LootItems extends AbstractConfig {
|
||||||
|
|
||||||
private static final HashMap<RARITY, List<Material>> ITEMS = new HashMap<>();
|
private static final HashMap<RARITY, List<Material>> ITEMS = new HashMap<>();
|
||||||
|
|
||||||
|
public static List<Material> get(RARITY rarity) {
|
||||||
|
return ITEMS.get(rarity);
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private static void load() {
|
private static void load() {
|
||||||
for (RARITY rarity : RARITY.values()) {
|
for (RARITY rarity : RARITY.values()) {
|
||||||
List<String> materialList = config.getList(prefix, rarity.getConfigName(), ITEMS.get(rarity).stream().map(Material::name));
|
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());
|
ITEMS.put(rarity, materialList.stream().map(Material::getMaterial).toList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 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 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 BORDER_SHRINK = "<red>The border is shrinking to <size> blocks!</red>";
|
||||||
|
public static String CHEST_EMPTY = "<red>This chest is empty!</red>";
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private static void load() {
|
private static void load() {
|
||||||
WARMUP = config.getString(prefix, "warmup", WARMUP);
|
WARMUP = config.getString(prefix, "warmup", WARMUP);
|
||||||
STARTED = config.getString(prefix, "started", STARTED);
|
STARTED = config.getString(prefix, "started", STARTED);
|
||||||
BORDER_SHRINK = config.getString(prefix, "border-shrink", BORDER_SHRINK);
|
BORDER_SHRINK = config.getString(prefix, "border-shrink", BORDER_SHRINK);
|
||||||
|
CHEST_EMPTY = config.getString(prefix, "chest-empty", CHEST_EMPTY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,22 +17,24 @@ public class RoundService implements RoundListener {
|
||||||
private static RoundService instance = null;
|
private static RoundService instance = null;
|
||||||
private PlayerMovementListener playerMovementListener;
|
private PlayerMovementListener playerMovementListener;
|
||||||
private final Main main;
|
private final Main main;
|
||||||
|
private final LootService lootService;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private ROUND_STATE roundState;
|
private ROUND_STATE roundState;
|
||||||
private final HashMap<UUID, PLAYER_STATE> players = new HashMap<>();
|
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) {
|
if (instance != null) {
|
||||||
throw new IllegalStateException("RoundService is already initialized.");
|
throw new IllegalStateException("RoundService is already initialized.");
|
||||||
}
|
}
|
||||||
instance = new RoundService(round, main);
|
instance = new RoundService(round, main, lootService);
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private RoundService(Round round, Main main) {
|
private RoundService(Round round, Main main, LootService lootService) {
|
||||||
this.roundState = round.register(this);
|
this.roundState = round.register(this);
|
||||||
this.main = main;
|
this.main = main;
|
||||||
|
this.lootService = lootService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<UUID> getPlayers(PLAYER_STATE playerState) {
|
public Set<UUID> getPlayers(PLAYER_STATE playerState) {
|
||||||
|
|
@ -74,6 +76,7 @@ public class RoundService implements RoundListener {
|
||||||
stopFreeze();
|
stopFreeze();
|
||||||
if (roundState.equals(ROUND_STATE.PLAYER_REGISTRATION)) {
|
if (roundState.equals(ROUND_STATE.PLAYER_REGISTRATION)) {
|
||||||
clear();
|
clear();
|
||||||
|
lootService.clear();
|
||||||
}
|
}
|
||||||
if (roundState.equals(ROUND_STATE.COUNTDOWN)) {
|
if (roundState.equals(ROUND_STATE.COUNTDOWN)) {
|
||||||
startFreeze();
|
startFreeze();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user