Move UHC utility methods to new UhcUtils class

This commit is contained in:
Bea 2021-01-10 00:52:06 +01:00
parent c1a34875cf
commit 8ff335cff0
5 changed files with 90 additions and 75 deletions

View File

@ -3,6 +3,7 @@ package net.mindoverflow.network.uhccore.listeners;
import net.mindoverflow.network.uhccore.UhcCore;
import net.mindoverflow.network.uhccore.utils.CommonValues;
import net.mindoverflow.network.uhccore.utils.Debugger;
import net.mindoverflow.network.uhccore.utils.UhcUtils;
import org.bukkit.*;
import org.bukkit.entity.Firework;
import org.bukkit.entity.Player;
@ -61,7 +62,7 @@ public class PlayerDeathRespawnListener implements Listener
{
// Spawn a Firework where the player died.
CommonValues.spawnFirework(player.getLocation(), 15L);
UhcUtils.spawnFirework(player.getLocation(), 15L);
// Load the player name.
String playerName = player.getName();
@ -82,7 +83,7 @@ public class PlayerDeathRespawnListener implements Listener
{
// Update the total number of players in each team, and the total number of alive teams.
CommonValues.updatePlayersPerTeam();
UhcUtils.updatePlayersPerTeam();
// Check how many players are left in the dead player's team.
int thisPlayerTeamPlayers = CommonValues.playersPerTeam.get(thisPlayerTeamNumber);
@ -124,7 +125,7 @@ public class PlayerDeathRespawnListener implements Listener
{
currentPlayer.teleport(CommonValues.spawn);
// Clear his inventory and give him the Teams selector item.
CommonValues.giveTeamsSelectorItem(currentPlayer);
UhcUtils.giveTeamsSelectorItem(currentPlayer);
plugin.getLogger().log(Level.INFO,"UHC Finished!");
currentPlayer.sendTitle("Team " + teamName + " Vincitore!", "", 20 * 2, 20 * 10, 20 * 2);
@ -175,7 +176,7 @@ public class PlayerDeathRespawnListener implements Listener
{
plugin.getServer().getScheduler().runTaskLater(plugin, () ->
{
CommonValues.tpSpawnAndGiveItem(player);
UhcUtils.tpSpawnAndGiveItem(player);
}, 10L);
}
@ -188,7 +189,7 @@ public class PlayerDeathRespawnListener implements Listener
plugin.getServer().getScheduler().runTaskLater(plugin, () ->
{
CommonValues.tpSpawnAndGiveItem(player);
UhcUtils.tpSpawnAndGiveItem(player);
}, 10L);
}
@ -223,7 +224,7 @@ public class PlayerDeathRespawnListener implements Listener
for(Location loc : CommonValues.fireworksLocations)
{
debugger.sendDebugMessage(Level.INFO, "FIREWORK LOC: " + loc);
CommonValues.spawnFirework(loc, 10L);
UhcUtils.spawnFirework(loc, 10L);
}

View File

@ -2,6 +2,7 @@ package net.mindoverflow.network.uhccore.listeners;
import net.mindoverflow.network.uhccore.utils.CommonValues;
import net.mindoverflow.network.uhccore.utils.UhcUtils;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -74,7 +75,7 @@ public class PlayerInteractListener implements Listener
CommonValues.playerTeam.put(player.getName(), teamNumber);
// Update the total number of players in each team, and the total number of alive teams.
CommonValues.updatePlayersPerTeam();
UhcUtils.updatePlayersPerTeam();
// Tell the player he has joined a team.
player.sendMessage("§7Aggiunto al team " + im.getDisplayName());
@ -90,7 +91,7 @@ public class PlayerInteractListener implements Listener
CommonValues.playerTeam.remove(player.getName());
// Update the total number of players in each team, and the total number of alive teams.
CommonValues.updatePlayersPerTeam();
UhcUtils.updatePlayersPerTeam();
} else
{
player.sendMessage("§cIn nessun team!");

View File

@ -2,6 +2,7 @@ package net.mindoverflow.network.uhccore.listeners;
import net.mindoverflow.network.uhccore.utils.CommonValues;
import net.mindoverflow.network.uhccore.utils.Debugger;
import net.mindoverflow.network.uhccore.utils.UhcUtils;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -29,7 +30,7 @@ public class PlayerJoinListener implements Listener
if(!(CommonValues.playerTeam.containsKey(player.getName())))
{
CommonValues.tpSpawnAndGiveItem(player);
UhcUtils.tpSpawnAndGiveItem(player);
}

View File

@ -58,70 +58,4 @@ public class CommonValues {
public static int borderX, borderZ, borderSize;
// todo: move following methods to their separate Utils class; this one is just for caching values...
// Function to check how many players a team has.
// This function returns the total number of alive teams.
public static void updatePlayersPerTeam()
{
// Integer to check how many teams are left alive.
int playingTeams = 0;
// Iterate through every existing team.
for(int i = 0; i < totalTeams; i++)
{
// Int to store the players number for each team.
int playersNumber = 0;
// Iterate through every player and...
for(String s : playerTeam.keySet())
{
//if his team is the current checked one...
if(playerTeam.get(s) == i)
{
//increase the playersNumber by 1.
playersNumber++;
}
}
// Finally, put the team number and his player count in the playersPerTeam HashMap.
playersPerTeam.put(i, playersNumber);
// If there is at least 1 player in this team, then count this as an "alive team".
if(playersNumber != 0) playingTeams++;
}
// Return the number of alive teams.
CommonValues.playingTeams = playingTeams;
}
public static void giveTeamsSelectorItem(Player player)
{
player.getInventory().clear();
player.getInventory().setItem(4, CommonValues.teamsItem);
}
public static void spawnFirework(Location location, long detonateDelay) {
Firework firework = (Firework) location.getWorld().spawnEntity(location, EntityType.FIREWORK);
FireworkMeta fireworkMeta = firework.getFireworkMeta();
fireworkMeta.setPower(100);
fireworkMeta.addEffect(FireworkEffect.builder().withColor(Color.RED).flicker(true).build());
firework.setFireworkMeta(fireworkMeta);
UhcCore plugin = UhcCore.getInstance();
plugin.getServer().getScheduler().runTaskLater(plugin, firework::detonate, detonateDelay);
}
public static void tpSpawnAndGiveItem(Player player)
{
player.teleport(spawn);
// Clear the player's inventory and give hims the Teams selector item.
giveTeamsSelectorItem(player);
}
}

View File

@ -0,0 +1,78 @@
package net.mindoverflow.network.uhccore.utils;
import net.mindoverflow.network.uhccore.UhcCore;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Firework;
import org.bukkit.entity.Player;
import org.bukkit.inventory.meta.FireworkMeta;
public class UhcUtils {
// Function to check how many players a team has.
// This function returns the total number of alive teams.
public static void updatePlayersPerTeam()
{
// Integer to check how many teams are left alive.
int playingTeams = 0;
// Iterate through every existing team.
for(int i = 0; i < CommonValues.totalTeams; i++)
{
// Int to store the players number for each team.
int playersNumber = 0;
// Iterate through every player and...
for(String s : CommonValues.playerTeam.keySet())
{
//if his team is the current checked one...
if(CommonValues.playerTeam.get(s) == i)
{
//increase the playersNumber by 1.
playersNumber++;
}
}
// Finally, put the team number and his player count in the playersPerTeam HashMap.
CommonValues.playersPerTeam.put(i, playersNumber);
// If there is at least 1 player in this team, then count this as an "alive team".
if(playersNumber != 0) playingTeams++;
}
// Return the number of alive teams.
CommonValues.playingTeams = playingTeams;
}
public static void giveTeamsSelectorItem(Player player)
{
player.getInventory().clear();
player.getInventory().setItem(4, CommonValues.teamsItem);
}
public static void spawnFirework(Location location, long detonateDelay) {
Firework firework = (Firework) location.getWorld().spawnEntity(location, EntityType.FIREWORK);
FireworkMeta fireworkMeta = firework.getFireworkMeta();
fireworkMeta.setPower(100);
fireworkMeta.addEffect(FireworkEffect.builder().withColor(Color.RED).flicker(true).build());
firework.setFireworkMeta(fireworkMeta);
UhcCore plugin = UhcCore.getInstance();
plugin.getServer().getScheduler().runTaskLater(plugin, firework::detonate, detonateDelay);
}
public static void tpSpawnAndGiveItem(Player player)
{
player.teleport(CommonValues.spawn);
// Clear the player's inventory and give hims the Teams selector item.
UhcUtils.giveTeamsSelectorItem(player);
}
}