Bricks now spawn.
Some checks are pending
Build plugin / build (push) Waiting to run

This commit is contained in:
2vb 2024-07-04 18:56:22 -07:00
parent a2ad900b5f
commit a99da2da56
4 changed files with 73 additions and 56 deletions

View File

@ -16,15 +16,11 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import xyz.twovb.sgm.SGM;
import xyz.twovb.sgm.games.GameManager;
import xyz.twovb.sgm.games.Minigame;
import xyz.twovb.sgm.levels.LevelManager;
import xyz.twovb.toolbox.api.CustomPlayer;
import xyz.twovb.toolbox.utils.ChatUtils;
import xyz.twovb.toolbox.utils.CustomLogger;
import java.io.File;
import java.io.IOException;
@ -37,13 +33,15 @@ public class CTB implements Minigame {
private final List<Player> players;
private final ArrayList<Location> redBrickSpawns = new ArrayList<Location>();
private final ArrayList<Location> blueBrickSpawns = new ArrayList<Location>();
private ArrayList<Location> redSpawnArea = new ArrayList<Location>();
private ArrayList<Location> blueSpawnArea = new ArrayList<Location>();
private final ArrayList<Location> redSpawnArea = new ArrayList<Location>();
private final ArrayList<Location> blueSpawnArea = new ArrayList<Location>();
private World gameWorld;
private GameState state;
private int bricks;
private int territoryLevel;
private int boundsLevel;
private Location spawnLoc;
public CTB() {
this.uuid = UUID.randomUUID();
@ -53,18 +51,17 @@ public class CTB implements Minigame {
public void init(CommandSender owner, String world) throws IOException {
// Perform initialization logic here
Bukkit.getPluginManager().registerEvents(this, SGM.getInstance());
gameWorld = createGameWorld(world, uuid);
gameWorld.setAutoSave(false);
try {
if (applyOptions()) {
if (applyOptions(world)) {
Bukkit.getPluginManager().registerEvents(this, SGM.getInstance());
state = GameState.READY;
owner.sendMessage("A new " + name + " instance with ID " + uuid + " has been created and is now ready!");
} else {
owner.sendMessage("a");
// owner.sendMessage(ChatUtils.translate(SGM.getInstance().getMessages().getString("sgm.game.cant-start")));
}
} catch (Exception ex) {
owner.sendMessage(ChatUtils.translate(SGM.getInstance().getMessages().getString("sgm.game.cant-start")));
SGM.getInstance().getCLogger().error(ex.getMessage());
}
}
private Location parseString(String string) {
@ -78,8 +75,10 @@ public class CTB implements Minigame {
// Attempts to load the game's options as a YamlConfiguration
// before processing each section in the same order as the file.
private boolean applyOptions() {
File configFile = new File(LevelManager.gamePath + name.toLowerCase() + "/options.yml");
private boolean applyOptions(String worldName) {
File configFile = new File(LevelManager.mapPath + worldName + "/sgm.yml");
SGM.getInstance().getCLogger().log(configFile.getPath());
SGM.getInstance().getCLogger().log(configFile.getAbsolutePath());
YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
// Set map information
@ -88,7 +87,9 @@ public class CTB implements Minigame {
this.bricks = mapInfoSection.getInt("brickCount");
this.territoryLevel = mapInfoSection.getInt("territory");
this.boundsLevel = mapInfoSection.getInt("boundary");
this.spawnLoc = parseString(mapInfoSection.getString("spawn"));
} else {
SGM.getInstance().getCLogger().error("Invalid map info. Please fix before trying again.");
return false;
}
@ -131,12 +132,33 @@ public class CTB implements Minigame {
return false;
}
} else {
SGM.getInstance().getCLogger().error("Invalid teams. Please fix before trying again.");
return false;
}
}
return true;
}
@Override
public void start() {
if (state == GameState.READY) {
state = GameState.STARTED;
sendMessageToAllPlayers("The game has started!");
for (Player player : players) {
player.teleport(this.spawnLoc);
}
for (int i = 0; i < bricks; i++) {
for (Location brickLoc : redBrickSpawns) {
placeBrick(brickLoc, "red");
}
for (Location brickLoc : blueBrickSpawns) {
placeBrick(brickLoc, "blue");
}
}
Bukkit.getScheduler().runTaskTimer(SGM.getInstance(), this::onTick, 0L, 1L);
}
}
private void placeBrick(Location location, String team) {
Block brick = location.getBlock();
if (Objects.equals(team, "red")) {
@ -148,18 +170,6 @@ public class CTB implements Minigame {
SGM.getInstance().getCLogger().log("Placed " + brick.getType() + " at " + location);
}
@Override
public void start() {
if (state == GameState.READY) {
state = GameState.STARTED;
sendMessageToAllPlayers("The game has started!");
for (Player player : players) {
player.teleport(player.getWorld().getSpawnLocation());
}
Bukkit.getScheduler().runTaskTimer(SGM.getInstance(), this::onTick, 0L, 1L);
}
}
@Override
public void stop() {
// Stop game logic
@ -172,13 +182,16 @@ public class CTB implements Minigame {
players.clear(); // Clear player list
GameManager.getActiveGames().remove(uuid);
HandlerList.unregisterAll(this);
for (Player player : gameWorld.getPlayers()) {
player.teleport(Bukkit.getServer().getWorlds().get(0).getSpawnLocation());
}
Bukkit.unloadWorld(gameWorld, false);
}
@Override
public void addPlayer(Player player) {
players.add(player);
player.teleport(gameWorld.getSpawnLocation());
player.teleport(new Location(gameWorld, 0, 0, 0));
player.teleport(this.spawnLoc);
sendMessageToAllPlayers(player.getName() + " has joined the game!");
}
@ -225,15 +238,15 @@ public class CTB implements Minigame {
return state;
}
@EventHandler
void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
CustomPlayer cPlayer = new CustomPlayer(player);
cPlayer.sendMessage("move");
if (event.getFrom().getX() != event.getTo().getX() || event.getFrom().getZ() != event.getTo().getZ()) {
cPlayer.sendMessage("move full block");
}
}
// @EventHandler
// void onPlayerMove(PlayerMoveEvent event) {
// Player player = event.getPlayer();
// CustomPlayer cPlayer = new CustomPlayer(player);
// cPlayer.sendMessage("move");
// if (event.getFrom().getX() != event.getTo().getX() || event.getFrom().getZ() != event.getTo().getZ()) {
// cPlayer.sendMessage("move full block");
// }
// }
@Override
public void onTick() {

View File

@ -1,13 +0,0 @@
package xyz.twovb.sgm.levels;
/*
* Created by 2vb - 4/6/2024
*/
import org.bukkit.World;
public interface Level {
String getName();
World getWorld();
}

View File

@ -34,7 +34,8 @@ public class LevelManager {
public HashMap<String, List<String>> enabledMaps = new HashMap<>();
private static void loadWorld(String worldName, LevelType type) {
// This isn't really used rn but I can't be bothered to do anything thb
public void loadWorld(String worldName, LevelType type) {
// Check if the world is already loaded
String key = type.toString().toLowerCase() + "_" + worldName;
WorldCreator wc = null;
@ -130,6 +131,9 @@ public class LevelManager {
player.teleport(Bukkit.getServer().getWorlds().get(0).getSpawnLocation());
}
if (!Bukkit.isTickingWorlds() && Bukkit.getServer().unloadWorld(world, true)) {
if (mapDir.exists()) {
FileUtils.deleteDirectory(mapDir);
}
FileUtils.copyDirectory(levelDir, mapDir);
FileUtils.delete(new File(mapDir + "/uid.dat"));
loadWorld(name, LevelType.LEVEL);
@ -158,10 +162,22 @@ public class LevelManager {
return LevelResult.UNKNOWN;
}
Location location = new Location(world, 0.5, 0, 0.5);
Block block = location.subtract(0, 2, 0).getBlock();
// Create a 3x3 stone platform
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
Location blockLocation = location.clone().add(x, -2, z);
Block block = blockLocation.getBlock();
block.setType(Material.STONE);
}
}
// Block block = location.subtract(0, 2, 0).getBlock();
// block.setType(Material.STONE);
world.setSpawnLocation(location);
world.setSpawnFlags(false, false);
world.setGameRule(GameRule.KEEP_INVENTORY, true);
world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);
world.setGameRule(GameRule.DO_WEATHER_CYCLE, false);
world.setTime(1000);
genDataFile(level, name, game.toLowerCase());
return LevelResult.SUCCESS;
}

View File

@ -2,6 +2,7 @@ mapInfo:
brickCount: 2
territory: 1
boundary: 3
spawn: 0,0,0
teams:
red:
brickSpawns: