Move Game Functions out of Main
This commit is contained in:
parent
f5f7803d50
commit
397e61823a
7 changed files with 107 additions and 69 deletions
|
|
@ -15,6 +15,7 @@ import me.firephoenix.ps3minigames.listener.MoveListener;
|
||||||
import me.firephoenix.ps3minigames.listener.WorldListener;
|
import me.firephoenix.ps3minigames.listener.WorldListener;
|
||||||
import me.firephoenix.ps3minigames.states.GameState;
|
import me.firephoenix.ps3minigames.states.GameState;
|
||||||
import me.firephoenix.ps3minigames.states.LobbyState;
|
import me.firephoenix.ps3minigames.states.LobbyState;
|
||||||
|
import me.firephoenix.ps3minigames.util.GameUtil;
|
||||||
import me.firephoenix.ps3minigames.util.Timer;
|
import me.firephoenix.ps3minigames.util.Timer;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
|
|
@ -47,6 +48,8 @@ public final class PS3Minigames extends JavaPlugin {
|
||||||
|
|
||||||
public ArrayList<UUID> frozenPlayer = new ArrayList<>();
|
public ArrayList<UUID> frozenPlayer = new ArrayList<>();
|
||||||
|
|
||||||
|
public GameUtil gameUtil = new GameUtil();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
// Set Instance
|
// Set Instance
|
||||||
|
|
@ -76,67 +79,4 @@ public final class PS3Minigames extends JavaPlugin {
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
// Plugin shutdown logic
|
// Plugin shutdown logic
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startNewGame(ArrayList<UUID> players, World map) {
|
|
||||||
String newName = map.getName() + "game" + getGames().size() + 1;
|
|
||||||
boolean copydone = getMultiverseCore().getMVWorldManager().cloneWorld(map.getName(), newName);
|
|
||||||
if (copydone) {
|
|
||||||
boolean loadingdone = getMultiverseCore().getMVWorldManager().loadWorld(newName);
|
|
||||||
if (loadingdone) {
|
|
||||||
getMultiverseCore().getMVWorldManager().getMVWorld(newName).setAlias(newName);
|
|
||||||
World gameWorld = Bukkit.getWorld(newName);
|
|
||||||
Game newGame = new Game(getGames().size() + 1, players, gameWorld, GameState.STARTING);
|
|
||||||
games.add(newGame);
|
|
||||||
worldToGameHashMap.put(gameWorld, newGame);
|
|
||||||
int spawnnumber = 1;
|
|
||||||
for (UUID uuid : players) {
|
|
||||||
if (getServer().getPlayer(uuid) == null) return;
|
|
||||||
getServer().getPlayer(uuid).sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("messages.teleporting")));
|
|
||||||
String configpathtospawnloc = "maps." + map.getName() + ".spawn" + spawnnumber++;
|
|
||||||
Location location = new Location(gameWorld, getConfig().getDouble(configpathtospawnloc + ".x"), getConfig().getDouble(configpathtospawnloc + ".y"), getConfig().getDouble(configpathtospawnloc + ".z"), (float) getConfig().getDouble(configpathtospawnloc + ".yaw"), (float) getConfig().getDouble(configpathtospawnloc + ".pitch"));
|
|
||||||
System.out.println(configpathtospawnloc + ".x");
|
|
||||||
Bukkit.getServer().getPlayer(uuid).teleport(location);
|
|
||||||
getFrozenPlayer().add(uuid);
|
|
||||||
}
|
|
||||||
Timer timer = new Timer(10, PS3Minigames.INSTANCE);
|
|
||||||
timer.start();
|
|
||||||
timer.eachSecond(() -> {
|
|
||||||
for (UUID uuid : newGame.getPlayers()) {
|
|
||||||
if (Bukkit.getServer().getPlayer(uuid) == null) return;
|
|
||||||
Bukkit.getServer().getPlayer(uuid).sendTitle(ChatColor.translateAlternateColorCodes('&',"&6" + timer.getCounter()), "");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
timer.whenComplete(() -> {
|
|
||||||
newGame.setGameState(GameState.RUNNING);
|
|
||||||
gameWorld.getPlayers().forEach(player -> {
|
|
||||||
frozenPlayer.remove(player.getUniqueId());
|
|
||||||
player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("messages.game-start-no-countdown")));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
System.out.println("error while trying to load the world!");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
System.out.println("error while trying to copy the world!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stopGame(Game game) {
|
|
||||||
if (game.getGameState() != GameState.STOPPING) game.setGameState(GameState.STOPPING);
|
|
||||||
game.getPlayers().forEach(uuid -> Bukkit.getServer().getPlayer(uuid).teleport(new Location(getServer().getWorld(getConfig().getString("spawn-lobby.world")), getConfig().getDouble("spawn-lobby.x"), getConfig().getDouble("spawn-lobby.y"), getConfig().getDouble("spawn-lobby.z"), (float) getConfig().getDouble("spawn-lobby.yaw"), (float) getConfig().getDouble("spawn-lobby.pitch"))));
|
|
||||||
if (game.getMap().getPlayers().size() == 0) {
|
|
||||||
getMultiverseCore().getMVWorldManager().deleteWorld(game.getMap().getName());
|
|
||||||
}
|
|
||||||
games.remove(game);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Game getGameByWorld(World world) {
|
|
||||||
return worldToGameHashMap.get(world);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public Game getGameByID(int id) {
|
|
||||||
//we use id - 1 because the id is the absolute size of the list which starts at 1 and the arraylist position starts at 0
|
|
||||||
return getGames().stream().anyMatch(game -> game.getGameid() == id) ? getGames().get(id - 1) : null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ public class ForceStart implements CommandExecutor {
|
||||||
if (world != null) {
|
if (world != null) {
|
||||||
ArrayList<UUID> players = new ArrayList<>();
|
ArrayList<UUID> players = new ArrayList<>();
|
||||||
PS3Minigames.INSTANCE.getLobby().getPlayers().forEach(player -> players.add(player.getUniqueId()));
|
PS3Minigames.INSTANCE.getLobby().getPlayers().forEach(player -> players.add(player.getUniqueId()));
|
||||||
PS3Minigames.INSTANCE.startNewGame(players, world);
|
PS3Minigames.INSTANCE.getGameUtil().startNewGame(players, world);
|
||||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.force-starting-game").replace("%map%", world.getName())));
|
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.force-starting-game").replace("%map%", world.getName())));
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -28,11 +28,11 @@ public class ForceStop implements CommandExecutor {
|
||||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.invalid-game-id").replace("%id%", args[0])));
|
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.invalid-game-id").replace("%id%", args[0])));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (PS3Minigames.INSTANCE.getGameByID(gameID) == null || gameID == 0) {
|
if (PS3Minigames.INSTANCE.getGameUtil().getGameByID(gameID) == null || gameID == 0) {
|
||||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.cant-find-game")));
|
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.cant-find-game")));
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
PS3Minigames.INSTANCE.stopGame(Objects.requireNonNull(PS3Minigames.INSTANCE.getGameByID(gameID)));
|
PS3Minigames.INSTANCE.getGameUtil().stopGame(Objects.requireNonNull(PS3Minigames.INSTANCE.getGameUtil().getGameByID(gameID)));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@ import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author NieGestorben
|
* @author NieGestorben
|
||||||
* Copyright© (c) 2023, All Rights Reserved.
|
* Copyright© (c) 2023, All Rights Reserved.
|
||||||
|
|
@ -23,7 +25,7 @@ public class DeathListener implements Listener {
|
||||||
|
|
||||||
player.getWorld().getPlayers().forEach(players -> players.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.player-death").replace("%player%", player.getDisplayName()).replace("%cause%", killer == null ? "enviroment" : killer.getDisplayName()))));
|
player.getWorld().getPlayers().forEach(players -> players.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("messages.player-death").replace("%player%", player.getDisplayName()).replace("%cause%", killer == null ? "enviroment" : killer.getDisplayName()))));
|
||||||
if (player.getWorld().getPlayers().size() == 1) {
|
if (player.getWorld().getPlayers().size() == 1) {
|
||||||
PS3Minigames.INSTANCE.stopGame(PS3Minigames.INSTANCE.getGameByWorld(player.getWorld()));
|
PS3Minigames.INSTANCE.getGameUtil().stopGame(Objects.requireNonNull(PS3Minigames.INSTANCE.getGameUtil().getGameByWorld(player.getWorld())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package me.firephoenix.ps3minigames.listener;
|
||||||
|
|
||||||
import me.firephoenix.ps3minigames.PS3Minigames;
|
import me.firephoenix.ps3minigames.PS3Minigames;
|
||||||
import me.firephoenix.ps3minigames.states.LobbyState;
|
import me.firephoenix.ps3minigames.states.LobbyState;
|
||||||
|
import me.firephoenix.ps3minigames.util.GameUtil;
|
||||||
import me.firephoenix.ps3minigames.util.Timer;
|
import me.firephoenix.ps3minigames.util.Timer;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.GameMode;
|
import org.bukkit.GameMode;
|
||||||
|
|
@ -70,7 +71,7 @@ public class JoinQuitListener implements Listener {
|
||||||
|
|
||||||
gameTimer.start();
|
gameTimer.start();
|
||||||
gameTimer = new Timer(plugin.getLobby().getPlayers().size() >= 3 ? 10 : 25, plugin);
|
gameTimer = new Timer(plugin.getLobby().getPlayers().size() >= 3 ? 10 : 25, plugin);
|
||||||
gameTimer.whenComplete(() -> PS3Minigames.INSTANCE.startNewGame(lobbyPlayers, plugin.getServer().getWorld("cavern")));
|
gameTimer.whenComplete(() -> PS3Minigames.INSTANCE.getGameUtil().startNewGame(lobbyPlayers, plugin.getServer().getWorld("cavern")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package me.firephoenix.ps3minigames.listener;
|
package me.firephoenix.ps3minigames.listener;
|
||||||
|
|
||||||
import me.firephoenix.ps3minigames.PS3Minigames;
|
import me.firephoenix.ps3minigames.PS3Minigames;
|
||||||
|
import org.bukkit.Location;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerMoveEvent;
|
import org.bukkit.event.player.PlayerMoveEvent;
|
||||||
|
|
@ -13,8 +14,16 @@ public class MoveListener implements Listener {
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onMove(PlayerMoveEvent e) {
|
public void onMove(PlayerMoveEvent e) {
|
||||||
if (PS3Minigames.INSTANCE.getFrozenPlayer().contains(e.getPlayer().getUniqueId()) && e.getFrom() != e.getTo()) {
|
if (PS3Minigames.INSTANCE.getFrozenPlayer().contains(e.getPlayer().getUniqueId()) && !isSimilarLocation(e.getFrom(), e.getTo())) {
|
||||||
e.setTo(e.getFrom());
|
e.setTo(e.getFrom());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isSimilarLocation(Location location, Location location2) {
|
||||||
|
if (location.getX() != location2.getX() || location.getY() != location2.getY() || location.getZ() != location2.getZ()) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
86
src/main/java/me/firephoenix/ps3minigames/util/GameUtil.java
Normal file
86
src/main/java/me/firephoenix/ps3minigames/util/GameUtil.java
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
package me.firephoenix.ps3minigames.util;
|
||||||
|
|
||||||
|
import me.firephoenix.ps3minigames.PS3Minigames;
|
||||||
|
import me.firephoenix.ps3minigames.game.Game;
|
||||||
|
import me.firephoenix.ps3minigames.states.GameState;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
|
import org.bukkit.Location;
|
||||||
|
import org.bukkit.World;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author NieGestorben
|
||||||
|
* Copyright© (c) 2023, All Rights Reserved.
|
||||||
|
*/
|
||||||
|
public class GameUtil {
|
||||||
|
|
||||||
|
|
||||||
|
public void startNewGame(ArrayList<UUID> players, World map) {
|
||||||
|
int ID = PS3Minigames.INSTANCE.getGames().size() + 1;
|
||||||
|
String newName = map.getName() + "game" + ID;
|
||||||
|
boolean copydone = PS3Minigames.INSTANCE.getMultiverseCore().getMVWorldManager().cloneWorld(map.getName(), newName);
|
||||||
|
if (copydone) {
|
||||||
|
boolean loadingdone = PS3Minigames.INSTANCE.getMultiverseCore().getMVWorldManager().loadWorld(newName);
|
||||||
|
if (loadingdone) {
|
||||||
|
PS3Minigames.INSTANCE.getMultiverseCore().getMVWorldManager().getMVWorld(newName).setAlias(newName);
|
||||||
|
World gameWorld = Bukkit.getWorld(newName);
|
||||||
|
Game newGame = new Game(ID, players, gameWorld, GameState.STARTING);
|
||||||
|
PS3Minigames.INSTANCE.getGames().add(newGame);
|
||||||
|
PS3Minigames.INSTANCE.getWorldToGameHashMap().put(gameWorld, newGame);
|
||||||
|
int spawnnumber = 1;
|
||||||
|
for (UUID uuid : players) {
|
||||||
|
if (PS3Minigames.INSTANCE.getServer().getPlayer(uuid) == null) return;
|
||||||
|
PS3Minigames.INSTANCE.getServer().getPlayer(uuid).sendMessage(ChatColor.translateAlternateColorCodes('&', PS3Minigames.INSTANCE.getConfig().getString("messages.teleporting")));
|
||||||
|
String configpathtospawnloc = "maps." + map.getName() + ".spawn" + spawnnumber++;
|
||||||
|
Location location = new Location(gameWorld, PS3Minigames.INSTANCE.getConfig().getDouble(configpathtospawnloc + ".x"), PS3Minigames.INSTANCE.getConfig().getDouble(configpathtospawnloc + ".y"), PS3Minigames.INSTANCE.getConfig().getDouble(configpathtospawnloc + ".z"), (float) PS3Minigames.INSTANCE.getConfig().getDouble(configpathtospawnloc + ".yaw"), (float) PS3Minigames.INSTANCE.getConfig().getDouble(configpathtospawnloc + ".pitch"));
|
||||||
|
Bukkit.getServer().getPlayer(uuid).teleport(location);
|
||||||
|
PS3Minigames.INSTANCE.getFrozenPlayer().add(uuid);
|
||||||
|
}
|
||||||
|
Timer timer = new Timer(10, PS3Minigames.INSTANCE);
|
||||||
|
timer.start();
|
||||||
|
timer.eachSecond(() -> {
|
||||||
|
for (UUID uuid : newGame.getPlayers()) {
|
||||||
|
if (Bukkit.getServer().getPlayer(uuid) == null) return;
|
||||||
|
Bukkit.getServer().getPlayer(uuid).sendTitle(ChatColor.translateAlternateColorCodes('&',"&6" + timer.getCounter()), "");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
timer.whenComplete(() -> {
|
||||||
|
newGame.setGameState(GameState.RUNNING);
|
||||||
|
gameWorld.getPlayers().forEach(player -> {
|
||||||
|
PS3Minigames.INSTANCE.getFrozenPlayer().remove(player.getUniqueId());
|
||||||
|
player.sendMessage(ChatColor.translateAlternateColorCodes('&', PS3Minigames.INSTANCE.getConfig().getString("messages.game-start-no-countdown")));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
System.out.println("error while trying to load the world!");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.out.println("error while trying to copy the world!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stopGame(Game game) {
|
||||||
|
if (game.getGameState() != GameState.STOPPING) game.setGameState(GameState.STOPPING);
|
||||||
|
game.getPlayers().forEach(uuid -> Bukkit.getServer().getPlayer(uuid).teleport(new Location(PS3Minigames.INSTANCE.getServer().getWorld(PS3Minigames.INSTANCE.getConfig().getString("spawn-lobby.world")), PS3Minigames.INSTANCE.getConfig().getDouble("spawn-lobby.x"), PS3Minigames.INSTANCE.getConfig().getDouble("spawn-lobby.y"), PS3Minigames.INSTANCE.getConfig().getDouble("spawn-lobby.z"), (float) PS3Minigames.INSTANCE.getConfig().getDouble("spawn-lobby.yaw"), (float) PS3Minigames.INSTANCE.getConfig().getDouble("spawn-lobby.pitch"))));
|
||||||
|
if (game.getMap().getPlayers().size() == 0) {
|
||||||
|
PS3Minigames.INSTANCE.getMultiverseCore().getMVWorldManager().deleteWorld(game.getMap().getName());
|
||||||
|
}
|
||||||
|
PS3Minigames.INSTANCE.getGames().remove(game);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public Game getGameByWorld(World world) {
|
||||||
|
return PS3Minigames.INSTANCE.getWorldToGameHashMap().get(world);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public Game getGameByID(int id) {
|
||||||
|
//we use id - 1 because the id is the absolute size of the list which starts at 1 and the arraylist position starts at 0
|
||||||
|
return PS3Minigames.INSTANCE.getGames().stream().anyMatch(game -> game.getGameid() == id) ? PS3Minigames.INSTANCE.getGames().get(id - 1) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue