Move map loading into GameCreationManager

This commit is contained in:
Sam 2018-06-21 01:34:33 +01:00 committed by Alexander Meech
parent 582d7ad726
commit 8799d44fd4
7 changed files with 246 additions and 289 deletions

View File

@ -137,7 +137,7 @@ public class SetCommand extends CommandBase<ArcadeManager>
// No particular source specified, we'll use all of them
if (selectedSource == null)
{
List<GameType> mapTypes = Arrays.asList(Game.GetWorldHostNames(gameType, gameClass));
List<GameType> mapTypes = Arrays.asList(Game.getWorldHostNames(gameType, gameClass));
matchedMaps = matchMaps(mapTypes, mapStr, false);
}
else
@ -252,7 +252,7 @@ public class SetCommand extends CommandBase<ArcadeManager>
private List<GameType> getSources(GameType type, Class<? extends Game> gameClass, String input, boolean isTabCompletion)
{
return matchGameType(input, Game.GetWorldHostNames(type, gameClass), isTabCompletion);
return matchGameType(input, Game.getWorldHostNames(type, gameClass), isTabCompletion);
}
private List<String> matchMaps(List<GameType> source, String input, boolean isTabCompletion)
@ -417,7 +417,7 @@ public class SetCommand extends CommandBase<ArcadeManager>
// No particular source specified, we'll use all of them
if (gameSourceStr == null)
{
List<GameType> mapTypes = Arrays.asList(Game.GetWorldHostNames(gameType, gameClass));
List<GameType> mapTypes = Arrays.asList(Game.getWorldHostNames(gameType, gameClass));
return matchMaps(mapTypes, mapStr, true).stream().map(str -> MAP_PREFIX + str).collect(Collectors.toList());
}

View File

@ -157,8 +157,6 @@ public abstract class Game extends ListenerComponent implements Lifetimed
protected String[] _gameDesc;
private PhasedLifetime<GameState> _lifetime = new PhasedLifetime<>();
// Map
private HashMap<GameType, ArrayList<String>> _files;
// State
private GameState _gameState = GameState.Loading;
@ -434,45 +432,6 @@ public abstract class Game extends ListenerComponent implements Lifetimed
// Scoreboard
Scoreboard = new GameScoreboard(this);
// Map Select
_files = new HashMap<>();
for (GameType type : GetWorldHostNames())
{
_files.put(type, Manager.LoadFiles(type.getName()));
}
if (Manager.GetGameCreationManager().MapPref != null)
{
System.out.println("Map Preference: " + Manager.GetGameCreationManager().MapPref);
HashMap<GameType, ArrayList<String>> matches = new HashMap<>();
for (GameType game : _files.keySet())
{
ArrayList<String> list = new ArrayList<>();
for (String cur : _files.get(game))
{
if (cur.replaceAll(" ", "").toLowerCase().contains(Manager.GetGameCreationManager().MapPref.toLowerCase()))
{
if (Manager.GetGameCreationManager().MapSource == null || game == Manager.GetGameCreationManager().MapSource)
{
list.add(cur);
System.out.print("Map Preference: " + cur);
matches.put(game, list);
}
}
}
}
if (matches.size() > 0)
_files = matches;
Manager.GetGameCreationManager().MapPref = null;
Manager.GetGameCreationManager().MapSource = null;
}
else
{
System.out.println("Map Preference: None");
}
WorldData = new WorldData(this);
// Stat Trackers
@ -600,22 +559,12 @@ public abstract class Game extends ListenerComponent implements Lifetimed
_kits = kits;
}
public HashMap<GameType, ArrayList<String>> GetFiles()
{
return _files;
}
public String GetName()
{
return _gameType.getName();
}
public GameType[] GetWorldHostNames()
{
return GetWorldHostNames(GetType(), getClass());
}
public static GameType[] GetWorldHostNames(GameType targetType, Class<? extends Game> gameMode)
public static GameType[] getWorldHostNames(GameType targetType, Class<? extends Game> gameMode)
{
GameType[] mapSource = new GameType[]
{

View File

@ -10,6 +10,7 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.ChatColor;
import org.bukkit.Material;
@ -17,6 +18,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import mineplex.core.common.Pair;
import mineplex.core.common.timing.TimingManager;
import mineplex.core.common.util.UtilAlg;
import mineplex.core.common.util.UtilMath;
@ -37,6 +39,8 @@ import nautilus.game.arcade.managers.voting.types.GameVote;
public class GameCreationManager implements Listener
{
private static final int MAX_ATTEMPTS = 50;
final ArcadeManager Manager;
private final VotingManager _votingManager;
@ -47,6 +51,7 @@ public class GameCreationManager implements Listener
private String _lastMap = "";
private GameMode _lastMode = null;
private final List<GameType> _lastGames = new ArrayList<>();
private Map<GameType, List<String>> _maps;
public String MapPref = null;
public GameType MapSource = null;
@ -214,7 +219,7 @@ public class GameCreationManager implements Listener
}
else
{
for (int i = 0; i < 50; i++)
for (int i = 0; i < MAX_ATTEMPTS; i++)
{
gameType = UtilAlg.Random(Manager.GetGameList());
ModePref = randomGameMode(gameType);
@ -227,6 +232,11 @@ public class GameCreationManager implements Listener
}
}
if (gameType == null)
{
return;
}
//Reset Changes
Manager.GetCreature().SetDisableCustomDrops(false);
Manager.GetDamage().resetConfiguration();
@ -242,11 +252,11 @@ public class GameCreationManager implements Listener
_lastGames.add(0, gameType);
boolean setVars = false;
Class<? extends Game> gameClass = gameType.getGameClass();
try
{
GameMode mode = null;
Class<? extends Game> gameClass = gameType.getGameClass();
if (ModePref != null)
{
@ -268,6 +278,9 @@ public class GameCreationManager implements Listener
gameClass = UtilMath.randomElement(gameType.getGameModes()).getGameClass();
}
// Maps
loadMaps(gameType, gameClass);
ModePref = null;
Game game = gameClass.getConstructor(ArcadeManager.class).newInstance(Manager);
@ -310,6 +323,90 @@ public class GameCreationManager implements Listener
TimingManager.stop("registerEvents");
}
private void loadMaps(GameType gameType, Class<? extends Game> gameClass)
{
_maps = new HashMap<>();
// Map
for (GameType type : Game.getWorldHostNames(gameType, gameClass))
{
_maps.put(type, Manager.LoadFiles(type.getName()));
}
if (MapPref != null)
{
System.out.println("Map Preference: " + MapPref);
Map<GameType, List<String>> matches = new HashMap<>();
for (Entry<GameType, List<String>> entry : _maps.entrySet())
{
GameType entryType = entry.getKey();
List<String> maps = entry.getValue();
List<String> matchList = new ArrayList<>();
maps.forEach(map ->
{
if (map.replace(" ", "").toLowerCase().contains(MapPref.toLowerCase()))
{
if (MapSource == null || entryType == MapSource)
{
matchList.add(map);
System.out.print("Map Preference: " + map);
}
}
});
matches.put(entryType, matchList);
}
if (!matches.isEmpty())
{
_maps = matches;
}
MapPref = null;
MapSource = null;
}
else
{
System.out.println("Map Preference: None");
}
}
public Pair<GameType, String> getMapFile()
{
GameType gameType = null;
String selectedMap = null;
for (int i = 0; i < MAX_ATTEMPTS; i++)
{
int gameTypeIndex = UtilMath.r(_maps.size());
int index = 0;
for (GameType mapType : _maps.keySet())
{
if (index++ == gameTypeIndex)
{
gameType = mapType;
break;
}
}
selectedMap = UtilAlg.Random(_maps.get(gameType));
// Not the last map
if (!selectedMap.equals(_lastMap))
{
break;
}
}
_lastMap = selectedMap;
return Pair.create(gameType, selectedMap);
}
private GameMode randomGameMode(GameType type)
{
ArrayList<String> modes = Manager.GetServerConfig().GameModeList;

View File

@ -378,6 +378,10 @@ public abstract class LobbyManager implements Listener
_manager.getMineplexGameManager().clearKitNPCs();
// Remove Old Kits
getKitBlocks().forEach(Block::setType);
getKitBlocks().clear();
//Remove Old Ents
getTeams().keySet().forEach(Entity::remove);
getTeams().clear();

View File

@ -222,9 +222,6 @@ public class NewGameLobbyManager extends LobbyManager
.collect(Collectors.toList())
.forEach(Entity::remove);
getKitBlocks().forEach(Block::setType);
getKitBlocks().clear();
List<Kit> kitList = Lists.newArrayList(game.GetKits()).stream()
.filter(kit -> !(kit instanceof NullKit))
.filter(kit -> kit.GetAvailability() != KitAvailability.Hide)

View File

@ -9,7 +9,6 @@ import org.bukkit.DyeColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Sheep;
@ -20,7 +19,6 @@ import mineplex.core.common.util.UtilAlg;
import mineplex.core.common.util.UtilEnt;
import mineplex.core.common.util.UtilWorld;
import mineplex.core.game.kit.KitAvailability;
import mineplex.core.newnpc.NPC;
import nautilus.game.arcade.ArcadeManager;
import nautilus.game.arcade.GameType;
@ -98,14 +96,6 @@ public class LegacyGameLobbyManager extends LobbyManager
writeTeamLine("Select", 0, 159, (byte) 15);
writeTeamLine("Team", 1, 159, (byte) 4);
//Remove Old Ents
getTeams().keySet().forEach(Entity::remove);
getTeams().clear();
//Remove Blocks
getTeamBlocks().forEach(Block::setType);
getTeamBlocks().clear();
//Smash
if (game.HideTeamSheep)
{
@ -168,10 +158,6 @@ public class LegacyGameLobbyManager extends LobbyManager
writeKitLine("Select", 0, 159, (byte) 15);
writeKitLine("Kit", 1, 159, (byte) 4);
//Remove Blocks
getKitBlocks().forEach(Block::setType);
getKitBlocks().clear();
//Display
List<Kit> kits = Lists.newArrayList();
for (Kit kit : game.GetKits())

View File

@ -1,25 +1,5 @@
package nautilus.game.arcade.world;
import com.mineplex.spigot.ChunkPreLoadEvent;
import mineplex.core.common.util.FileUtil;
import mineplex.core.common.util.MapUtil;
import mineplex.core.common.util.UtilMath;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.WorldUtil;
import mineplex.core.common.util.ZipUtil;
import mineplex.core.common.util.worldgen.WorldGenCleanRoom;
import mineplex.core.common.api.enderchest.EnderchestWorldLoader;
import mineplex.core.common.timing.TimingManager;
import nautilus.game.arcade.GameType;
import nautilus.game.arcade.game.Game;
import nautilus.game.arcade.game.games.uhc.UHC;
import org.bukkit.Difficulty;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.spigotmc.SpigotConfig;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
@ -28,19 +8,38 @@ import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class WorldData
import org.bukkit.Difficulty;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.spigotmc.SpigotConfig;
import mineplex.core.common.Pair;
import mineplex.core.common.api.enderchest.EnderchestWorldLoader;
import mineplex.core.common.timing.TimingManager;
import mineplex.core.common.util.FileUtil;
import mineplex.core.common.util.MapUtil;
import mineplex.core.common.util.UtilMath;
import mineplex.core.common.util.WorldUtil;
import mineplex.core.common.util.ZipUtil;
import mineplex.core.common.util.worldgen.WorldGenCleanRoom;
import nautilus.game.arcade.GameType;
import nautilus.game.arcade.game.Game;
import nautilus.game.arcade.game.games.uhc.UHC;
public class WorldData
{
public Game Host;
public int Id = -1;
public final Game Host;
private final int Id;
public String File = null;
public String Folder = null;
private String Folder = null;
public World World;
public int MinX = 0;
public int MinZ = 0;
@ -49,82 +48,67 @@ public class WorldData
public int MinY = -1;
public int MaxY = 256;
public String MapName = "Null";
public String MapAuthor = "Null";
public GameType Game = null;
public HashMap<String, ArrayList<Location>> SpawnLocs = new LinkedHashMap<String, ArrayList<Location>>();
private HashMap<String, ArrayList<Location>> DataLocs = new LinkedHashMap<String, ArrayList<Location>>();
private HashMap<String, ArrayList<Location>> CustomLocs = new LinkedHashMap<String, ArrayList<Location>>();
public final Map<String, ArrayList<Location>> SpawnLocs = new LinkedHashMap<>();
private final Map<String, ArrayList<Location>> DataLocs = new LinkedHashMap<>();
private final Map<String, ArrayList<Location>> CustomLocs = new LinkedHashMap<>();
private final Map<String, String> _dataEntries = new LinkedHashMap<>();
public WorldData(Game game)
{
Host = game;
Initialize();
Id = GetNewId();
}
public void Initialize()
{
final WorldData worldData = this;
GetFile();
UtilServer.getServer().getScheduler().runTaskAsynchronously(Host.Manager.getPlugin(), new Runnable()
Host.getArcadeManager().runAsync(() ->
{
public void run()
//Unzip
if (Host instanceof UHC)
{
//Unzip
if (Host instanceof UHC) {
boolean uhcLoaded = loadUHCMap(); // attempt to load from enderchest
if (!uhcLoaded)
{
// failsafe on normal UHC map
worldData.UnzipWorld();
}
} else
boolean uhcLoaded = loadUHCMap(); // attempt to load from enderchest
if (!uhcLoaded)
{
// failsafe on normal UHC map
worldData.UnzipWorld();
}
//Load World Data Sync
UtilServer.getServer().getScheduler().runTask(Host.Manager.getPlugin(), new Runnable()
{
public void run()
{
TimingManager.start("WorldData loading world.");
WorldCreator creator = new WorldCreator(GetFolder());
creator.generator(new WorldGenCleanRoom());
World = WorldUtil.LoadWorld(creator);
TimingManager.stop("WorldData loading world.");
World.setDifficulty(Difficulty.HARD);
World.setGameRuleValue("showDeathMessages", "false");
TimingManager.start("WorldData loading WorldConfig.");
//Load World Data
worldData.LoadWorldConfig();
TimingManager.stop("WorldData loading WorldConfig.");
/*
TimingManager.start("WinEffect Room Builder.");
Location loc = GetRandomXZ().add(1000, 0, 1000);
loc.setY(200);
worldData.Host.WinEffectManager.prepareSetup(loc);
TimingManager.stop("WinEffect Room Builder.");
*/
}
});
}
else
{
worldData.UnzipWorld();
}
Host.getArcadeManager().runSync(() ->
{
TimingManager.start("WorldData loading world.");
WorldCreator creator = new WorldCreator(GetFolder());
creator.generator(new WorldGenCleanRoom());
World = WorldUtil.LoadWorld(creator);
TimingManager.stop("WorldData loading world.");
World.setDifficulty(Difficulty.HARD);
World.setGameRuleValue("showDeathMessages", "false");
TimingManager.start("WorldData loading WorldConfig.");
//Load World Data
worldData.LoadWorldConfig();
TimingManager.stop("WorldData loading WorldConfig.");
});
});
}
@ -142,7 +126,8 @@ public class WorldData
worldLoader.loadMap("uhc", GetFolder());
SpigotConfig.config.set("world-settings." + GetFolder() + ".view-distance", UHC.VIEW_DISTANCE);
success = true;
} catch (Exception e)
}
catch (Exception e)
{
attempt++;
e.printStackTrace();
@ -151,78 +136,34 @@ public class WorldData
return success;
}
protected GameType GetGame()
{
return Game;
}
protected String GetFile()
private String GetFile()
{
if (File == null)
{
GameType game = null;
int gameRandom = UtilMath.r(Host.GetFiles().size());
int i = 0;
for(GameType type : Host.GetFiles().keySet())
{
if(i == gameRandom)
{
game = type;
break;
}
i++;
}
Game = game;
int map;
try
{
map = UtilMath.r(Host.GetFiles().get(game).size());
} catch (IllegalArgumentException e)
{
System.out.println("No maps found!");
return null;
}
File = Host.GetFiles().get(game).get(map);
//Don't allow repeat maps.
if (Host.GetFiles().get(game).size() > 1)
{
while (File.equals(Host.Manager.GetGameCreationManager().GetLastMap()))
{
GameType _game = null;
int _gameRandom = UtilMath.r(Host.GetFiles().size());
int _i = 0;
for(GameType _type : Host.GetFiles().keySet())
{
if(_i == _gameRandom)
{
_game = _type;
break;
}
_i++;
}
int _map = UtilMath.r(Host.GetFiles().get(game).size());
File = Host.GetFiles().get(_game).get(_map);
}
}
Pair<GameType, String> mapFile = Host.getArcadeManager().GetGameCreationManager().getMapFile();
Game = mapFile.getLeft();
File = mapFile.getRight();
}
Host.Manager.GetGameCreationManager().SetLastMap(File);
return File;
}
public String GetFolder()
{
if (Folder == null)
if (Folder == null)
{
Folder = "Game" + Id + "_" + GetGame().getName() + "_" + GetFile();
}
}
return Folder;
}
protected void UnzipWorld()
protected void UnzipWorld()
{
TimingManager.start("UnzipWorld creating folders");
String folder = GetFolder();
@ -230,38 +171,38 @@ public class WorldData
new File(folder + java.io.File.separator + "region").mkdir();
new File(folder + java.io.File.separator + "data").mkdir();
TimingManager.stop("UnzipWorld creating folders");
TimingManager.start("UnzipWorld UnzipToDirectory");
ZipUtil.UnzipToDirectory("../../update/maps/" + GetGame().getName() + "/" + GetFile() + ".zip", folder);
TimingManager.stop("UnzipWorld UnzipToDirectory");
}
public void LoadWorldConfig()
public void LoadWorldConfig()
{
//Load Track Data
String line = null;
try
{
FileInputStream fstream = new FileInputStream(GetFolder() + java.io.File.separator + "WorldConfig.dat");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
ArrayList<Location> currentTeam = null;
ArrayList<Location> currentData = null;
int currentDirection = 0;
while ((line = br.readLine()) != null)
while ((line = br.readLine()) != null)
{
String[] tokens = line.split(":");
if (tokens.length < 2)
continue;
if (tokens[0].length() == 0)
continue;
//Name & Author
if (tokens[0].equalsIgnoreCase("MAP_NAME"))
{
@ -271,11 +212,11 @@ public class WorldData
{
MapAuthor = tokens[1];
}
//Spawn Locations
else if (tokens[0].equalsIgnoreCase("TEAM_NAME"))
{
SpawnLocs.put(tokens[1], new ArrayList<Location>());
SpawnLocs.put(tokens[1], new ArrayList<>());
currentTeam = SpawnLocs.get(tokens[1]);
currentDirection = 0;
}
@ -285,51 +226,51 @@ public class WorldData
}
else if (tokens[0].equalsIgnoreCase("TEAM_SPAWNS"))
{
for (int i=1 ; i<tokens.length ; i++)
for (int i = 1; i < tokens.length; i++)
{
Location loc = StrToLoc(tokens[i]);
if (loc == null) continue;
if (loc == null) continue;
loc.setYaw(currentDirection);
currentTeam.add(loc);
}
}
//Data Locations
else if (tokens[0].equalsIgnoreCase("DATA_NAME"))
{
DataLocs.put(tokens[1], new ArrayList<Location>());
DataLocs.put(tokens[1], new ArrayList<>());
currentData = DataLocs.get(tokens[1]);
}
else if (tokens[0].equalsIgnoreCase("DATA_LOCS"))
{
for (int i=1 ; i<tokens.length ; i++)
for (int i = 1; i < tokens.length; i++)
{
Location loc = StrToLoc(tokens[i]);
if (loc == null) continue;
if (loc == null) continue;
currentData.add(loc);
}
}
//Custom Locations
else if (tokens[0].equalsIgnoreCase("CUSTOM_NAME"))
{
CustomLocs.put(tokens[1], new ArrayList<Location>());
CustomLocs.put(tokens[1], new ArrayList<>());
currentData = CustomLocs.get(tokens[1]);
}
else if (tokens[0].equalsIgnoreCase("CUSTOM_LOCS"))
{
for (int i=1 ; i<tokens.length ; i++)
for (int i = 1; i < tokens.length; i++)
{
Location loc = StrToLoc(tokens[i]);
if (loc == null) continue;
if (loc == null) continue;
currentData.add(loc);
}
}
//Map Bounds
else if (tokens[0].equalsIgnoreCase("MIN_X"))
{
@ -341,7 +282,7 @@ public class WorldData
{
System.out.println("World Data Read Error: Invalid MinX [" + tokens[1] + "]");
}
}
else if (tokens[0].equalsIgnoreCase("MAX_X"))
{
@ -405,7 +346,7 @@ public class WorldData
}
in.close();
Host.Manager.GetGameWorldManager().RegisterWorld(this);
}
catch (Exception e)
@ -414,58 +355,37 @@ public class WorldData
System.err.println("Line: " + line);
}
}
protected Location StrToLoc(String loc)
{
String[] coords = loc.split(",");
try
{
return new Location(World, Integer.valueOf(coords[0])+0.5, Integer.valueOf(coords[1]), Integer.valueOf(coords[2])+0.5);
return new Location(World, Integer.valueOf(coords[0]) + 0.5, Integer.valueOf(coords[1]), Integer.valueOf(coords[2]) + 0.5);
}
catch (Exception e)
{
System.out.println("World Data Read Error: Invalid Location String [" + loc + "]");
}
return null;
}
public void Uninitialize()
{
public void Uninitialize()
{
if (World == null)
return;
//Wipe World
MapUtil.UnloadWorld(Host.Manager.getPlugin(), World);
MapUtil.ClearWorldReferences(World.getName());
FileUtil.DeleteFolder(new File(World.getName()));
World = null;
}
public void ChunkLoad(ChunkPreLoadEvent event)
{
if (World == null)
return;
if (!event.getWorld().equals(World))
return;
int x = event.getX();
int z = event.getZ();
if (x >= MinX >> 4 && x <= MaxX >> 4 && z >= MinZ >> 4 && z <= MaxZ >> 4)
{
return;
}
event.setCancelled(true);
}
public int GetNewId()
public int GetNewId()
{
File file = new File("GameId.dat");
@ -527,36 +447,40 @@ public class WorldData
public ArrayList<Location> GetDataLocs(String data)
{
if (!DataLocs.containsKey(data))
return new ArrayList<Location>();
{
return new ArrayList<>(0);
}
return DataLocs.get(data);
}
public ArrayList<Location> GetCustomLocs(String id)
{
if (!CustomLocs.containsKey(id))
return new ArrayList<Location>();
{
return new ArrayList<>(0);
}
return CustomLocs.get(id);
}
public HashMap<String, ArrayList<Location>> GetAllCustomLocs()
public Map<String, ArrayList<Location>> GetAllCustomLocs()
{
return CustomLocs;
}
public HashMap<String, ArrayList<Location>> GetAllDataLocs()
public Map<String, ArrayList<Location>> GetAllDataLocs()
{
return DataLocs;
}
public Location GetRandomXZ()
public Location GetRandomXZ()
{
Location loc = new Location(World, 0, 250, 0);
int xVar = MaxX - MinX;
int zVar = MaxZ - MinZ;
loc.setX(MinX + UtilMath.r(xVar));
loc.setZ(MinZ + UtilMath.r(zVar));
@ -567,5 +491,5 @@ public class WorldData
{
return _dataEntries.get(key);
}
}