Skip to content

Commit

Permalink
Code improvements and fixing exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaacquafredda committed Jun 2, 2024
1 parent 398bea0 commit 7fa5c3e
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 75 deletions.
18 changes: 9 additions & 9 deletions src/main/java/lar/minecraft/hg/ServerSchedulers.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ServerSchedulers {
public static void init(SpigotPlugin plugin) {
ServerSchedulers.plugin = plugin;
ServerSchedulers.server = plugin.getServer();
ServerSchedulers.world = server.getWorld("world");
ServerSchedulers.world = SpigotPlugin.world;
}

private static long gameStartTime = 0;
Expand Down Expand Up @@ -132,7 +132,7 @@ public static void safeAreaPhase() {
p.teleport(world.getSpawnLocation());

// Write player join on Database
DatabaseManager.addPlayerJoin(ConfigUtils.getInt(ConfigProperty.server_id), currentHGGameId, p);
DatabaseManager.addPlayerJoin(SpigotPlugin.serverId, currentHGGameId, p);
});
PlayerClassManager.giveClasses();
ServerManager.sendSound(Sound.EVENT_RAID_HORN);
Expand Down Expand Up @@ -168,10 +168,10 @@ public static void playingPhase() {
worldBorderCollapseTime = 0;
winnerCelebrationsTime = 0;
server.setIdleTimeout(ConfigUtils.getInt(ConfigProperty.duration_idle_timeout));
ServerManager.getLivingPlayers().forEach(p -> p.spigot().sendMessage(
ChatMessageType.ACTION_BAR,
new TextComponent(MessageUtils.getMessage(MessageKey.playing_phase_alert))));
DatabaseManager.saveStartingDateTime(ConfigUtils.getInt(ConfigProperty.server_id), currentHGGameId);
ServerManager.getLivingPlayers()
.forEach(p -> p.sendTitle(MessageUtils.getMessage(MessageKey.playing_phase_alert), null, 10, 100, 10));

DatabaseManager.saveStartingDateTime(SpigotPlugin.serverId, currentHGGameId);
playingPhaseTaskId = server.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
public void run() {
long execTime = world.getTime();
Expand Down Expand Up @@ -215,12 +215,12 @@ public void run() {
// First runnable run
if(winnerCelebrationsTime == 0) {
Player winner = ServerManager.getLivingPlayers().iterator().next();
SpigotPlugin.server.broadcastMessage(winner.getName() + " wins the Hunger Games!");
SpigotPlugin.server.broadcastMessage(MessageUtils.getMessage(MessageKey.wins_the_hunger_games, winner.getName()));
winner.sendTitle("You win the Hunger Games!", null, 10, 70, 20);
winnerCelebrationsTime = execTime + (20 * ConfigUtils.getInt(ConfigProperty.duration_winner_celebrations));

// Save the winning player on Database
DatabaseManager.savePlayerWin(ConfigUtils.getInt(ConfigProperty.server_id), currentHGGameId, winner);
DatabaseManager.savePlayerWin(SpigotPlugin.serverId, currentHGGameId, winner);
fireworkEffect(winner);
}

Expand All @@ -229,7 +229,7 @@ public void run() {
for(Player p : SpigotPlugin.server.getOnlinePlayers()) {
p.spigot().sendMessage(
ChatMessageType.ACTION_BAR,
new TextComponent(MessageUtils.getMessage(MessageKey.safe_area_expires_alert, Math.abs(passedSeconds))));
new TextComponent(MessageUtils.getMessage(MessageKey.server_to_restart_alert, Math.abs(passedSeconds))));
}
if (passedSeconds == 0) {
SpigotPlugin.setPhase(HGPhase.WAITING);
Expand Down
40 changes: 28 additions & 12 deletions src/main/java/lar/minecraft/hg/SpigotPlugin.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package lar.minecraft.hg;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Properties;

import org.bukkit.Difficulty;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -25,26 +30,37 @@ public class SpigotPlugin extends JavaPlugin {

public static FileConfiguration config;

public static HGPhase phase;
public static Properties serverProps = new Properties();

public static int serverId;
public static HGPhase phase = HGPhase.WAITING;

@Override
public void onLoad() {
public static int serverId = 0;

public static World world;

@Override
public void onEnable() {
server = getServer();

try {
serverProps.load(new FileInputStream("server.properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
world = server.getWorld(serverProps.getProperty("level-name"));
config = getConfig();
saveDefaultConfig();
ConfigUtils.setConfig(config);

ServerSchedulers.init(this);
}

@Override
public void onEnable() {
server.getWorld("world").setDifficulty(Difficulty.NORMAL);
serverId = ConfigUtils.getInt(ConfigProperty.server_id);
world.setDifficulty(Difficulty.NORMAL);

// Create world border
server.getWorld("world").getWorldBorder().setCenter(server.getWorld("world").getSpawnLocation());
server.getWorld("world").getWorldBorder().setSize(ConfigUtils.getInt(ConfigProperty.world_border_max_size));
world.getWorldBorder().setCenter(world.getSpawnLocation());
world.getWorldBorder().setSize(ConfigUtils.getInt(ConfigProperty.world_border_max_size));

// Initialize MessageUtils for messages
MessageUtils.init();
Expand All @@ -54,7 +70,7 @@ public void onEnable() {
String dbConnectionString = ConfigUtils.getString(ConfigProperty.database_connection_string);
String dbUser = ConfigUtils.getString(ConfigProperty.database_user);
String dbPassword = ConfigUtils.getString(ConfigProperty.database_password);
DatabaseManager.Init(databaseEnabled, dbConnectionString, dbUser, dbPassword);
DatabaseManager.init(databaseEnabled, dbConnectionString, dbUser, dbPassword);

// Enable test commands
getCommand("start-hg").setExecutor(new TestCommand(this));
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/lar/minecraft/hg/enums/MessageKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public enum MessageKey {
safe_area_expires_alert,
playing_phase_alert,
server_to_restart_alert,
scoreboard_list_header
scoreboard_list_header,
wins_the_hunger_games
}
4 changes: 2 additions & 2 deletions src/main/java/lar/minecraft/hg/managers/DatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static String getDbConnectionString() {
* @param dbUser Database user (user must have permission to create tables and views)
* @param dbPassword Database user password
*/
public static void Init(boolean databaseEnabled, String connectionString, String dbUser, String dbPassword) {
public static void init(boolean databaseEnabled, String connectionString, String dbUser, String dbPassword) {
DatabaseManager.databaseEnabled = databaseEnabled;

// Set static fields and connect to database
Expand Down Expand Up @@ -104,7 +104,7 @@ public static int createTables() {
+ " count(`hunger_games`.`hg_games`.`winner_uuid`) AS `wins_count`"
+ " from"
+ " (`hunger_games`.`hg_games`"
+ " join `hunger_games`.`players` on"
+ " right outer join `hunger_games`.`players` on"
+ " ((`hunger_games`.`players`.`uuid` = `hunger_games`.`hg_games`.`winner_uuid`)))"
+ " group by"
+ " `hunger_games`.`hg_games`.`winner_uuid`);");
Expand Down
96 changes: 48 additions & 48 deletions src/main/java/lar/minecraft/hg/managers/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,54 +30,6 @@ public class PlayerManager implements Listener {

private int winnerParticleEffectTaskId = 0;

/**
* Player death event
*/
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event){
Player deathPlayer = event.getEntity().getPlayer();
deathPlayer.getWorld().strikeLightningEffect(deathPlayer.getLocation());
deathPlayer.setGameMode(GameMode.SPECTATOR);

// Stop reproducing particles of the winner player
if (PlayerManager.playerExtras.get(deathPlayer.getUniqueId()).isLastWinner()) {
SpigotPlugin.server.getScheduler().cancelTask(winnerParticleEffectTaskId);
}

ServerManager.sendSound(Sound.ENTITY_LIGHTNING_BOLT_THUNDER);
retrieveKilledPlayerHead(event);
}

/**
* Player quit event
*/
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event){
if (SpigotPlugin.isWinning() || SpigotPlugin.isLobby()) {
PlayerManager.playerExtras.remove(event.getPlayer().getUniqueId());
}
if (SpigotPlugin.isSafeArea() || SpigotPlugin.isWinning() || SpigotPlugin.isPlaying()) {
Player player = event.getPlayer();
player.getWorld().strikeLightningEffect(player.getLocation());
ServerManager.sendSound(Sound.ENTITY_LIGHTNING_BOLT_THUNDER);
}
// Stop reproducing particles of the winner player
PlayerExtra playerExtra = PlayerManager.playerExtras.getOrDefault(event.getPlayer().getUniqueId(), null);
if (playerExtra != null && playerExtra.isLastWinner()) {
SpigotPlugin.server.getScheduler().cancelTask(winnerParticleEffectTaskId);
}
}

/**
* Player damage event
*/
@EventHandler
public void onPlayerDamage(EntityDamageEvent event) {
if (SpigotPlugin.isLobby() || SpigotPlugin.isSafeArea() || SpigotPlugin.isWinning() || SpigotPlugin.isWaitingForStart()) {
event.setCancelled(true);
}
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
if (SpigotPlugin.isWaitingForStart() || SpigotPlugin.isLobby()) {
Expand Down Expand Up @@ -117,6 +69,54 @@ public void run() {
}
}

/**
* Player damage event
*/
@EventHandler
public void onPlayerDamage(EntityDamageEvent event) {
if (SpigotPlugin.isLobby() || SpigotPlugin.isSafeArea() || SpigotPlugin.isWinning() || SpigotPlugin.isWaitingForStart()) {
event.setCancelled(true);
}
}

/**
* Player death event
*/
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event){
Player deathPlayer = event.getEntity().getPlayer();
deathPlayer.getWorld().strikeLightningEffect(deathPlayer.getLocation());
deathPlayer.setGameMode(GameMode.SPECTATOR);

// Stop reproducing particles of the winner player
if (PlayerManager.playerExtras.get(deathPlayer.getUniqueId()).isLastWinner()) {
SpigotPlugin.server.getScheduler().cancelTask(winnerParticleEffectTaskId);
}

ServerManager.sendSound(Sound.ENTITY_LIGHTNING_BOLT_THUNDER);
retrieveKilledPlayerHead(event);
}

/**
* Player quit event
*/
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event){
if (SpigotPlugin.isWinning() || SpigotPlugin.isLobby()) {
PlayerManager.playerExtras.remove(event.getPlayer().getUniqueId());
}
if (SpigotPlugin.isSafeArea() || SpigotPlugin.isWinning() || SpigotPlugin.isPlaying()) {
Player player = event.getPlayer();
player.getWorld().strikeLightningEffect(player.getLocation());
ServerManager.sendSound(Sound.ENTITY_LIGHTNING_BOLT_THUNDER);
}
// Stop reproducing particles of the winner player
PlayerExtra playerExtra = PlayerManager.playerExtras.getOrDefault(event.getPlayer().getUniqueId(), null);
if (playerExtra != null && playerExtra.isLastWinner()) {
SpigotPlugin.server.getScheduler().cancelTask(winnerParticleEffectTaskId);
}
}

public static Player getNearestPlayer(Player player, double range) {
double distance = Double.POSITIVE_INFINITY;
Player target = null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/lar/minecraft/hg/managers/ServerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static ArrayList<Player> getLivingPlayers() {

public static void spawnSupplyDrop() {
// Get the spawn location
Location spawnLocation = Bukkit.getWorld("world").getSpawnLocation();
Location spawnLocation = SpigotPlugin.world.getSpawnLocation();

// Generate random offsets for X and Z coordinates
Random random = new Random();
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class_selected:
- "&7You choose to be a &6{0}&7!"
- "&6{0}&7 class selected!"
class_wrong: "&7There's no &6&m{0}&7 class"
class_premium: "&7You must win a match or be &6premium &7to use this class!"
class_premium: "&7You must have won last match or be &6premium &7to use this class!"
class_not_selected: "&7Class selection is only available before starting the game"
supply_drop_alert: "&fA &6supply drop chest &fwill be spawned in &6{0} &fseconds"
supply_drop: "&7Supply chest dropped at XYZ:&2&o {0} / {1} / {2}"
Expand All @@ -17,4 +17,5 @@ safe_area_phase_alert: "&fIt's starting the Hunger Games!"
safe_area_expires_alert: "&fSafe area expires in &6{0} &fseconds"
playing_phase_alert: "&fIt's Hunger Games time!"
server_to_restart_alert: "&fA new Hunger Games will start in &6{0} &fseconds. Server will be restarted"
scoreboard_list_header: "&6Name &f--> &6Wins"
scoreboard_list_header: "&6Name &f--> &6Wins"
wins_the_hunger_games: "&6{0} &fwins the Hunger Games!"

0 comments on commit 7fa5c3e

Please sign in to comment.