Skip to content

Commit

Permalink
Merge pull request #82 from AlbertoRutigliano/player-classes
Browse files Browse the repository at this point in the history
Player classes
  • Loading branch information
AlbertoRutigliano authored Jun 10, 2024
2 parents 3fde476 + 54f1d03 commit 07d762c
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 105 deletions.
1 change: 1 addition & 0 deletions src/main/java/lar/minecraft/hg/ServerSchedulers.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public static void lobbyPhase() {
ServerManager.getLivingPlayers().forEach(p -> {
p.setGameMode(GameMode.ADVENTURE);
p.getInventory().clear();
p.getInventory().addItem(ServerManager.getGameInstructionsBook());
});
gameStartTime = 0;
lobbyPhaseTaskId = server.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/lar/minecraft/hg/SpigotPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Properties;

import org.bukkit.Difficulty;
Expand All @@ -20,7 +19,6 @@
import lar.minecraft.hg.enums.Cmd;
import lar.minecraft.hg.enums.ConfigProperty;
import lar.minecraft.hg.enums.HGPhase;
import lar.minecraft.hg.enums.PlayerClass;
import lar.minecraft.hg.managers.DatabaseManager;
import lar.minecraft.hg.managers.PlayerManager;
import lar.minecraft.hg.managers.ServerManager;
Expand Down Expand Up @@ -109,11 +107,10 @@ public void onEnable() {
// Enable game commands
getCommand(Cmd.scoreboard).setExecutor(new ScoreboardCommand());
getCommand(Cmd.scoreboard).setTabCompleter(new ScoreboardCommand());
// Enable class selection commands
Arrays.asList(PlayerClass.values()).forEach(c -> {
getCommand(c.name()).setExecutor(new ClassCommand());
});
getCommand(Cmd.class_command).setExecutor(new ClassCommand());
getCommand(Cmd.class_command).setTabCompleter(new ClassCommand());

// Initialize PlayerManager listener
getServer().getPluginManager().registerEvents(new PlayerManager(), this);
Expand Down
122 changes: 76 additions & 46 deletions src/main/java/lar/minecraft/hg/commands/ClassCommand.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,76 @@
package lar.minecraft.hg.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import lar.minecraft.hg.SpigotPlugin;
import lar.minecraft.hg.entities.PlayerExtra;
import lar.minecraft.hg.enums.MessageKey;
import lar.minecraft.hg.enums.PlayerClass;
import lar.minecraft.hg.managers.PlayerManager;
import lar.minecraft.hg.utils.MessageUtils;

public class ClassCommand implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
PlayerClass cmdName = PlayerClass.valueOf(command.getName().toLowerCase());

if (sender instanceof Player) {
Player player = (Player) sender;
if (cmdName != null && !PlayerManager.playerExtras.isEmpty() && (SpigotPlugin.isWaitingForStart() || SpigotPlugin.isLobby())) {
PlayerExtra playerExtra = PlayerManager.playerExtras.get(player.getUniqueId());

if (playerExtra != null) {
// Check command selection: only premium users or who won last match can use premium classes
if (!cmdName.isPremium() || playerExtra.isPremium() || playerExtra.isLastWinner()) {
playerExtra.setPlayerClass(cmdName);
PlayerManager.playerExtras.put(player.getUniqueId(), playerExtra);
player.sendMessage(MessageUtils.getMessage(MessageKey.class_selected, cmdName.name()));
player.playSound(player, cmdName.getSound(), 10.0f, 10.0f);
} else {
player.sendMessage(MessageUtils.getMessage(MessageKey.class_premium));
}
} else {
return false;
}
} else {
player.sendMessage(MessageUtils.getMessage(MessageKey.class_not_selected));
}
}
return true;
}

}
package lar.minecraft.hg.commands;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;

import lar.minecraft.hg.SpigotPlugin;
import lar.minecraft.hg.entities.PlayerExtra;
import lar.minecraft.hg.enums.MessageKey;
import lar.minecraft.hg.enums.PlayerClass;
import lar.minecraft.hg.managers.PlayerManager;
import lar.minecraft.hg.utils.MessageUtils;

public class ClassCommand implements CommandExecutor, TabExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;

if (args.length == 1) {
PlayerClass className = PlayerClass.valueOf(args[0].toLowerCase());

if (className != null && !PlayerManager.playerExtras.isEmpty() && (SpigotPlugin.isWaitingForStart() || SpigotPlugin.isLobby())) {
PlayerExtra playerExtra = PlayerManager.playerExtras.get(player.getUniqueId());

if (playerExtra != null) {
// Check command selection: only premium users or who won last match can use premium classes
if (!className.isPremium() || playerExtra.isPremium() || playerExtra.isLastWinner()) {
playerExtra.setPlayerClass(className);
PlayerManager.playerExtras.put(player.getUniqueId(), playerExtra);
player.sendMessage(MessageUtils.getMessage(MessageKey.class_selected, className.name()));
player.playSound(player, className.getSound(), 10.0f, 10.0f);
} else {
player.sendMessage(MessageUtils.getMessage(MessageKey.class_premium));
}
} else {
player.sendMessage(MessageUtils.getMessage(MessageKey.class_not_selected));
return true;
}
} else {
player.sendMessage(MessageUtils.getMessage(MessageKey.class_not_selected));
}
} else {
player.sendMessage(MessageUtils.getMessage(MessageKey.class_not_selected));
return true;
}
}

return true;
}

@Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
List<String> completions = new ArrayList<>();

if (args.length == 1) {
List<String> tempList = Arrays.stream(PlayerClass.values())
.map(Enum::name)
.collect(Collectors.toList());
StringUtil.copyPartialMatches(args[0], tempList, completions);
return completions;
}

Collections.sort(completions);
return Collections.emptyList();
}
}
1 change: 1 addition & 0 deletions src/main/java/lar/minecraft/hg/enums/Cmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public final class Cmd {
public static final String restart_hg_server = "restart-hg-server";
public static final String start_hg = "start-hg";
public static final String scoreboard = "scoreboard";
public static final String class_command = "class";

// Private constructor to prevent instantiation
private Cmd() {}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/lar/minecraft/hg/enums/MessageKey.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package lar.minecraft.hg.enums;

public enum MessageKey {
welcome_message,
current_phase,
class_selected,
class_selection_lobby,
class_wrong,
class_premium,
class_not_selected,
class_instructions,
class_instructions_premium,
class_instructions_materials,
class_instructions_select,
supply_drop_alert,
supply_drop,
last_match_win,
Expand Down
Loading

0 comments on commit 07d762c

Please sign in to comment.