Archiviert
13
0

tower-generator #6

Zusammengeführt
YoyoNow hat 16 Commits von tower-generator nach master 2023-09-23 21:29:06 +02:00 zusammengeführt
10 geänderte Dateien mit 182 neuen und 8 gelöschten Zeilen
Nur Änderungen aus Commit 394c0d5d43 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -21,8 +21,10 @@ package de.steamwar.towerrun;
import de.steamwar.message.Message;
import de.steamwar.towerrun.commands.StartCommand;
import de.steamwar.towerrun.config.WorldConfig;
import de.steamwar.towerrun.countdowns.EndCountdown;
import de.steamwar.towerrun.countdowns.LobbyCountdown;
import de.steamwar.towerrun.generator.TowerGenerator;
import de.steamwar.towerrun.listener.GlobalListener;
import de.steamwar.towerrun.listener.IngameListener;
import de.steamwar.towerrun.listener.LobbyListener;
@ -34,6 +36,8 @@ import org.bukkit.plugin.java.annotation.plugin.Description;
import org.bukkit.plugin.java.annotation.plugin.Plugin;
import org.bukkit.plugin.java.annotation.plugin.author.Author;
import java.util.Optional;
@Plugin(name = "TowerRun", version = "1.0.0")
@Dependency(value = "SpigotCore")
@Author(value = "YoyoNow")
@ -47,11 +51,18 @@ public class TowerRun extends JavaPlugin {
@Getter
private static Message message;
@Getter
private static TowerGenerator towerGenerator = null;
@Override
public void onEnable() {
instance = this;
message = new Message("TowerRun", getClassLoader());
if (WorldConfig.TOWER_GENERATOR_CONFIG != null) {
towerGenerator = new TowerGenerator(WorldConfig.TOWER_GENERATOR_CONFIG);
}
new LobbyListener();
new IngameListener();
new GlobalListener();

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.towerrun.config;
import de.steamwar.sql.SchematicType;
import de.steamwar.towerrun.TowerRun;
import de.steamwar.towerrun.winconditions.FirstOutsideWincondition;
import de.steamwar.towerrun.winconditions.LastOutsideWincondition;
@ -53,6 +54,7 @@ public class WorldConfig {
public static final int MAP_MIN_Z;
public static final int MAP_MAX_X;
public static final int MAP_MAX_Z;
public static final TowerGeneratorConfig TOWER_GENERATOR_CONFIG;
public static final List<WinCondition> WINCONDITIONS = new ArrayList<>();
static {
@ -149,6 +151,13 @@ public class WorldConfig {
ACTIVE_WINCONDITIONS = config.getStringList("winconditions");
WINCONDITIONS.stream().filter(winCondition -> ACTIVE_WINCONDITIONS.contains(winCondition.getName())).forEach(winCondition -> winCondition.setActive(true));
ConfigurationSection towerGeneratorSection = tower.getConfigurationSection("towerGenerator");
if (towerGeneratorSection == null) {
TOWER_GENERATOR_CONFIG = null;
} else {
TOWER_GENERATOR_CONFIG = new TowerGeneratorConfig(towerGeneratorSection);
}
}
@Getter
@ -176,4 +185,19 @@ public class WorldConfig {
&& vector.getZ() >= min.getZ() && vector.getZ() <= max.getZ();
}
}
@Getter
public static final class TowerGeneratorConfig {
public final int x;
public final int y;
public final int z;
public final SchematicType schematicType;
public TowerGeneratorConfig(ConfigurationSection section) {
x = section.getInt("x");
y = section.getInt("y");
z = section.getInt("z");
schematicType = SchematicType.fromDB(section.getString("schematicType"));
}
}
}

Datei anzeigen

@ -47,7 +47,7 @@ public class LobbyCountdown extends Countdown {
@Override
void timerEnd() {
TowerRunGame.start();
TowerRunGame.prepareTower();
}
@Override

Datei anzeigen

@ -48,8 +48,22 @@ public class TowerRunGame {
return PLAYERS_ALIVE.contains(player);
}
public static void start() {
public static void prepareTower() {
if (GameState.getCurrentState() == GameStates.LOBBY) {
GameState.nextState();
if (TowerRun.getTowerGenerator() == null) {
start();
return;
}
TowerRun.getTowerGenerator().generate(TowerRunGame::start);
} else {
throw new IllegalStateException("Game is already running!");
}
}
private static void start() {
if (GameState.getCurrentState() == GameStates.GENERATING_TOWER) {
PLAYERS_ALIVE.addAll(TowerRunPlayer.getAll());
PLAYERS_ALIVE.forEach(TowerRunPlayer::reset);
GameState.nextState();

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.towerrun.game;
import de.steamwar.towerrun.TowerRun;
import de.steamwar.towerrun.config.WorldConfig;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
@ -43,7 +44,11 @@ public record TowerRunPlayer(Player player) {
}
public void reset() {
if (TowerRun.getTowerGenerator() != null) {
player.teleport(WorldConfig.SPAWN.add(0, TowerRun.getTowerGenerator().getHeight(), 0));
} else {
player.teleport(WorldConfig.SPAWN);
}
player.setVelocity(new Vector(0, 0, 0));
}

Datei anzeigen

@ -0,0 +1,104 @@
package de.steamwar.towerrun.generator;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.session.ClipboardHolder;
import de.steamwar.sql.SchematicData;
import de.steamwar.sql.SchematicNode;
import de.steamwar.sql.SchematicType;
import de.steamwar.towerrun.TowerRun;
import de.steamwar.towerrun.config.WorldConfig;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitRunnable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Random;
public class TowerGenerator {
private Random random = new Random();
private int x;
private int y;
private int z;
private List<SchematicNode> ALL_SCHEMATICS;
private Clipboard roof;
@Getter
private int height;
public TowerGenerator(WorldConfig.TowerGeneratorConfig config) {
this(config.x, config.y, config.z, config.schematicType);
}
public TowerGenerator(int x, int y, int z, SchematicType schematicType) {
File file = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "Roof.schematic");
if (!file.exists()) {
Bukkit.shutdown();
return;
}
roof = loadSchematic(file);
this.x = x;
this.y = y;
this.z = z;
ALL_SCHEMATICS = SchematicNode.getAllSchematicsOfType(schematicType);
}
private Clipboard loadSchematic(File file) {
Clipboard clipboard;
try (ClipboardReader reader = Objects.requireNonNull(ClipboardFormats.findByFile(file)).getReader(new FileInputStream(file))) {
clipboard = reader.read();
} catch (NullPointerException | IOException e) {
Bukkit.shutdown();
throw new SecurityException("TowerRun schematic not found", e);
}
return clipboard;
}
public void generate(Runnable finishRunnable) {
new BukkitRunnable() {
int height = random.nextInt(50) + 200;
int y = TowerGenerator.this.y;
{
TowerGenerator.this.height = height;
}
@Override
public void run() {
if (height > 0) {
SchematicNode schematicNode = ALL_SCHEMATICS.get(random.nextInt(ALL_SCHEMATICS.size()));
SchematicData schematicData = new SchematicData(schematicNode);
try {
Clipboard clipboard = schematicData.load();
try (EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(Bukkit.getWorlds().get(0)), -1)) {
ClipboardHolder ch = new ClipboardHolder(clipboard);
Operations.completeBlindly(ch.createPaste(e).to(BlockVector3.at(x, y, z)).build());
}
y += clipboard.getDimensions().getY();
height -= clipboard.getDimensions().getY();
} catch (IOException e) {
ALL_SCHEMATICS.remove(schematicNode);
}
} else {
try (EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(Bukkit.getWorlds().get(0)), -1)) {
ClipboardHolder ch = new ClipboardHolder(roof);
Operations.completeBlindly(ch.createPaste(e).to(BlockVector3.at(x, y, z)).build());
}
finishRunnable.run();
}
}
}.runTaskTimer(TowerRun.getInstance(), 0, 1);
}
}

Datei anzeigen

@ -112,6 +112,10 @@ public class IngameListener extends GameStateBukkitListener {
@EventHandler
public void onPlayerRespawn(PlayerRespawnEvent event) {
if (TowerRun.getTowerGenerator() != null) {
event.setRespawnLocation(WorldConfig.SPAWN.add(0, TowerRun.getTowerGenerator().getHeight(), 0));
} else {
event.setRespawnLocation(WorldConfig.SPAWN);
}
}
}

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.towerrun.listener;
import de.steamwar.towerrun.TowerRun;
import de.steamwar.towerrun.config.WorldConfig;
import de.steamwar.towerrun.state.GameStateBukkitListener;
import de.steamwar.towerrun.state.GameStates;
@ -42,9 +43,14 @@ public class LobbyListener extends GameStateBukkitListener {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
if (TowerRun.getTowerGenerator() != null) {
player.teleport(WorldConfig.SPAWN.add(0, TowerRun.getTowerGenerator().getHeight(), 0));
player.setGameMode(GameMode.SPECTATOR);
} else {
player.teleport(WorldConfig.SPAWN);
player.setGameMode(GameMode.SURVIVAL);
}
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.towerrun.listener;
import de.steamwar.towerrun.TowerRun;
import de.steamwar.towerrun.config.WorldConfig;
import de.steamwar.towerrun.state.GameStateBukkitListener;
import de.steamwar.towerrun.state.GameStates;
@ -36,6 +37,10 @@ public class NotLobbyListener extends GameStateBukkitListener {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
event.getPlayer().setGameMode(GameMode.SPECTATOR);
if (TowerRun.getTowerGenerator() != null) {
event.getPlayer().teleport(WorldConfig.SPAWN.add(0, TowerRun.getTowerGenerator().getHeight(), 0));
} else {
event.getPlayer().teleport(WorldConfig.SPAWN);
}
}
}

Datei anzeigen

@ -27,7 +27,8 @@ import lombok.Getter;
public enum GameStates {
ENDING(null),
INGAME(ENDING),
LOBBY(INGAME);
GENERATING_TOWER(INGAME),
LOBBY(GENERATING_TOWER);
private final GameStates nextState;
}