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 151 neuen und 34 gelöschten Zeilen
Nur Änderungen aus Commit 24a5c3e995 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -16,6 +16,49 @@ tower:
keys: []
lavaY: 76
laveSpace: 9
meltingBlocks:
SMOOTH_STONE: 120
SMOOTH_STONE_SLAB: 60
ANDESITE: 120
JUKEBOX: 10
IRON_BLOCK: 240
END_STONE: 240
REDSTONE_LAMP: 30
GLASS: 30
GLASS_PANE: 3
TINTED_GLASS: 30
WHITE_STAINED_GLASS: 30
WHITE_STAINED_GLASS_PANE: 3
ORANGE_STAINED_GLASS: 30
ORANGE_STAINED_GLASS_PANE: 3
MAGENTA_STAINED_GLASS: 30
MAGENTA_STAINED_GLASS_PANE: 3
LIGHT_BLUE_STAINED_GLASS: 30
LIGHT_BLUE_STAINED_GLASS_PANE: 3
YELLOW_STAINED_GLASS: 30
YELLOW_STAINED_GLASS_PANE: 3
LIME_STAINED_GLASS: 30
LIME_STAINED_GLASS_PANE: 3
PINK_STAINED_GLASS: 30
PINK_STAINED_GLASS_PANE: 3
GRAY_STAINED_GLASS: 30
GRAY_STAINED_GLASS_PANE: 3
LIGHT_GRAY_STAINED_GLASS: 30
LIGHT_GRAY_STAINED_GLASS_PANE: 3
CYAN_STAINED_GLASS: 30
CYAN_STAINED_GLASS_PANE: 3
PURPLE_STAINED_GLASS: 30
PURPLE_STAINED_GLASS_PANE: 3
BLUE_STAINED_GLASS: 30
BLUE_STAINED_GLASS_PANE: 3
BROWN_STAINED_GLASS: 30
BROWN_STAINED_GLASS_PANE: 3
GREEN_STAINED_GLASS: 30
GREEN_STAINED_GLASS_PANE: 3
RED_STAINED_GLASS: 30
RED_STAINED_GLASS_PANE: 3
BLACK_STAINED_GLASS: 30
BLACK_STAINED_GLASS_PANE: 3
towerGenerator:
x: 0
@ -45,10 +88,10 @@ towerGenerator:
percentage: 1.0
material: MANGROVE_ROOTS
minX: -40
maxX: 109
minZ: -40
maxZ: 119
minX: -28
maxX: 98
minZ: -27
maxZ: 107
winconditions:
- LAST_REMAINING

Datei anzeigen

@ -38,8 +38,6 @@ 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;
YoyoNow markierte diese Unterhaltung als gelöst Veraltet
Veraltet
Review

unused Import

unused Import
import java.util.Optional;
@Plugin(name = "TowerRun", version = "1.0.0")
@Dependency("SpigotCore")
@Author("YoyoNow")

Datei anzeigen

@ -35,8 +35,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.util.Vector;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
@UtilityClass
public class WorldConfig {
@ -55,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 Map<Material, Integer> MELTING_TIMES;
public static final TowerGeneratorConfig TOWER_GENERATOR_CONFIG;
public static final List<WinCondition> WINCONDITIONS = new ArrayList<>();
@ -160,6 +160,17 @@ public class WorldConfig {
MAP_MAX_X = config.getInt("maxX");
MAP_MAX_Z = config.getInt("maxZ");
ConfigurationSection meltingBlocksSection = tower.getConfigurationSection("meltingBlocks");
if (meltingBlocksSection != null) {
Map<Material, Integer> meltingTimes = new HashMap<>();
meltingBlocksSection.getKeys(false).forEach(s -> {
meltingTimes.put(Material.valueOf(s), meltingBlocksSection.getInt(s));
});
MELTING_TIMES = Collections.unmodifiableMap(meltingTimes);
} else {
MELTING_TIMES = Collections.emptyMap();
}
ACTIVE_WINCONDITIONS = config.getStringList("winconditions");
WINCONDITIONS.stream().filter(winCondition -> ACTIVE_WINCONDITIONS.contains(winCondition.getName())).forEach(winCondition -> winCondition.setActive(true));
@ -203,7 +214,7 @@ public class WorldConfig {
public final int y;
public final int z;
public final SchematicType schematicType;
public final TowerGeneratorFillRegion[] FILL_REGIONS;
public final TowerGeneratorFillRegion[] fillRegions;
public TowerGeneratorConfig(ConfigurationSection section) {
x = section.getInt("x");
@ -217,12 +228,12 @@ public class WorldConfig {
.map(fillRegionsSection::getConfigurationSection)
.toList();
FILL_REGIONS = new TowerGeneratorFillRegion[fillRegions.size()];
this.fillRegions = new TowerGeneratorFillRegion[fillRegions.size()];
for (int i = 0; i < fillRegions.size(); i++) {
FILL_REGIONS[i] = new TowerGeneratorFillRegion(fillRegions.get(i));
this.fillRegions[i] = new TowerGeneratorFillRegion(fillRegions.get(i));
}
} else {
FILL_REGIONS = new TowerGeneratorFillRegion[0];
fillRegions = new TowerGeneratorFillRegion[0];
}
}
}

Datei anzeigen

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

Datei anzeigen

@ -48,7 +48,7 @@ public class TowerRunGame {
return PLAYERS_ALIVE.contains(player);
}
public static void prepareTower() {
public static void prepareTowerOrStart() {
YoyoNow markierte diese Unterhaltung als gelöst Veraltet
Veraltet
Review

prepareTower ist glaube ich der falsche Name für diese Funktion. Da die Funktion des generierens nur in einem bruchteil der fälle ausgeführt wird.

`prepareTower` ist glaube ich der falsche Name für diese Funktion. Da die Funktion des generierens nur in einem bruchteil der fälle ausgeführt wird.
if (GameState.getCurrentState() == GameStates.LOBBY) {
GameState.nextState();
if (TowerRun.getTowerGenerator() == null) {
@ -56,13 +56,13 @@ public class TowerRunGame {
return;
}
TowerRun.getTowerGenerator().generate(TowerRunGame::start);
TowerRun.getTowerGenerator().generate();
YoyoNow markierte diese Unterhaltung als gelöst Veraltet
Veraltet
Review

die Generate Funktion hat nur eine Usage, warum braucht die dann nen lambda?

die Generate Funktion hat nur eine Usage, warum braucht die dann nen lambda?
} else {
throw new IllegalStateException("Game is already running!");
}
}
private static void start() {
public static void start() {
if (GameState.getCurrentState() == GameStates.GENERATING_TOWER) {
PLAYERS_ALIVE.addAll(TowerRunPlayer.getAll());
PLAYERS_ALIVE.forEach(p -> {

Datei anzeigen

@ -45,7 +45,7 @@ public record TowerRunPlayer(Player player) {
public void reset() {
if (TowerRun.getTowerGenerator() != null) {
player.teleport(WorldConfig.SPAWN.clone().add(0, TowerRun.getTowerGenerator().getHeight(), 0));
player.teleport(TowerRun.getTowerGenerator().getSpawn());
} else {
player.teleport(WorldConfig.SPAWN);
}

Datei anzeigen

@ -11,11 +11,12 @@ 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 de.steamwar.towerrun.game.TowerRunGame;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.scheduler.BukkitRunnable;
@ -28,24 +29,27 @@ import java.util.Random;
public class TowerGenerator {
YoyoNow markierte diese Unterhaltung als gelöst Veraltet
Veraltet
Review

Could be final,
Could be static

Could be final, Could be static
private Random random = new Random();
private WorldConfig.TowerGeneratorConfig config;
private List<SchematicNode> ALL_SCHEMATICS;
private static final Random random = new Random();
YoyoNow markierte diese Unterhaltung als gelöst Veraltet
Veraltet
Review

could be final

could be final
private final WorldConfig.TowerGeneratorConfig config;
YoyoNow markierte diese Unterhaltung als gelöst Veraltet
Veraltet
Review

Attribute sind im camelCase

Attribute sind im camelCase
private List<SchematicNode> allSchematics;
private Clipboard roof;
@Getter
private int height;
@Getter
private Location spawn;
public TowerGenerator(WorldConfig.TowerGeneratorConfig config) {
File file = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "Roof.schem");
if (!file.exists()) {
Bukkit.shutdown();
return;
throw new SecurityException("TowerRun schematic not found");
}
roof = loadSchematic(file);
this.config = config;
ALL_SCHEMATICS = SchematicNode.getAllSchematicsOfType(config.schematicType);
allSchematics = SchematicNode.getAllSchematicsOfType(config.schematicType);
}
private Clipboard loadSchematic(File file) {
@ -59,7 +63,7 @@ public class TowerGenerator {
return clipboard;
}
public void generate(Runnable finishRunnable) {
public void generate() {
new BukkitRunnable() {
int height = random.nextInt(50) + 200;
int y = TowerGenerator.this.config.y;
@ -71,7 +75,7 @@ public class TowerGenerator {
@Override
public void run() {
if (height > 0) {
SchematicNode schematicNode = ALL_SCHEMATICS.get(random.nextInt(ALL_SCHEMATICS.size()));
SchematicNode schematicNode = allSchematics.get(random.nextInt(allSchematics.size()));
SchematicData schematicData = new SchematicData(schematicNode);
int currentY;
try {
@ -84,11 +88,12 @@ public class TowerGenerator {
y += clipboard.getDimensions().getY();
height -= clipboard.getDimensions().getY();
} catch (IOException e) {
ALL_SCHEMATICS.remove(schematicNode);
allSchematics.remove(schematicNode);
return;
}
spawn = WorldConfig.SPAWN.clone().add(0, y, 0);
for (WorldConfig.TowerGeneratorFillRegion fillRegion : config.FILL_REGIONS) {
for (WorldConfig.TowerGeneratorFillRegion fillRegion : config.fillRegions) {
for (int x = fillRegion.getMinX(); x < fillRegion.getMaxX(); x++) {
for (int z = fillRegion.getMinZ(); z < fillRegion.getMaxZ(); z++) {
for (int y = currentY; y < this.y; y++) {
@ -109,7 +114,7 @@ public class TowerGenerator {
ClipboardHolder ch = new ClipboardHolder(roof);
Operations.completeBlindly(ch.createPaste(e).to(BlockVector3.at(config.x, y, config.z)).build());
YoyoNow markierte diese Unterhaltung als gelöst Veraltet
Veraltet
Review

Könnte man das nicht eher als Countdown umbauen?

Könnte man das nicht eher als Countdown umbauen?
}
finishRunnable.run();
TowerRunGame.start();
}
}
}.runTaskTimer(TowerRun.getInstance(), 0, 10);

Datei anzeigen

@ -26,30 +26,69 @@ import de.steamwar.towerrun.config.WorldConfig;
import de.steamwar.towerrun.state.GameStateBukkitListener;
import de.steamwar.towerrun.state.GameStates;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.*;
public class IngameListener extends GameStateBukkitListener {
private int time = 0;
private Map<Integer, List<Block>> blocksToMelt = new HashMap<>();
private BukkitRunnable runnable;
public IngameListener() {
super(EnumSet.of(GameStates.INGAME));
}
@Override
public void enable() {
super.enable();
runnable = new BukkitRunnable() {
@Override
public void run() {
List<Block> blocks = blocksToMelt.get(time);
time++;
if (blocks == null) {
return;
}
blocks.forEach(block -> {
if (!WorldConfig.MELTING_TIMES.containsKey(block.getType())) return;
block.setType(Material.AIR);
block.getWorld().playSound(block.getLocation(), Sound.BLOCK_FIRE_EXTINGUISH, 0.1F, 1);
});
}
};
runnable.runTaskTimer(TowerRun.getInstance(), 0, 1);
}
@Override
public void disable() {
super.disable();
runnable.cancel();
runnable = null;
blocksToMelt.clear();
time = 0;
}
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event) {
event.setDeathMessage(null);
event.getEntity().setGameMode(GameMode.SPECTATOR);
Bukkit.getOnlinePlayers().forEach(player -> {
player.sendTitle("", TowerRun.getMessage().parse("PLAYER_DIED", player, event.getEntity().getPlayer().getName()), 10, 70, 20);
player.playSound(player.getLocation(), Sound.ENTITY_WITHER_DEATH, 1, 1);
@ -113,9 +152,31 @@ public class IngameListener extends GameStateBukkitListener {
@EventHandler
public void onPlayerRespawn(PlayerRespawnEvent event) {
if (TowerRun.getTowerGenerator() != null) {
event.setRespawnLocation(WorldConfig.SPAWN.clone().add(0, TowerRun.getTowerGenerator().getHeight(), 0));
event.setRespawnLocation(TowerRun.getTowerGenerator().getSpawn());
} else {
event.setRespawnLocation(WorldConfig.SPAWN);
}
}
@EventHandler
public void onBlockPhysics(BlockPhysicsEvent event) {
if (event.getSourceBlock().getType() != Material.LAVA) {
return;
}
Block block = event.getSourceBlock();
shouldMelt(block.getRelative(0, 1, 0));
shouldMelt(block.getRelative(0, -1, 0));
shouldMelt(block.getRelative(1, 0, 0));
shouldMelt(block.getRelative(-1, 0, 0));
shouldMelt(block.getRelative(0, 0, 1));
shouldMelt(block.getRelative(0, 0, -1));
}
private void shouldMelt(Block block) {
int meltingTime = WorldConfig.MELTING_TIMES.getOrDefault(block.getType(), -1);
if (meltingTime == -1) {
return;
}
blocksToMelt.computeIfAbsent(time + meltingTime * 20, integer -> new ArrayList<>()).add(block);
}
}

Datei anzeigen

@ -23,7 +23,6 @@ import de.steamwar.towerrun.TowerRun;
import de.steamwar.towerrun.config.WorldConfig;
import de.steamwar.towerrun.state.GameStateBukkitListener;
import de.steamwar.towerrun.state.GameStates;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
@ -45,7 +44,7 @@ public class LobbyListener extends GameStateBukkitListener {
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
if (TowerRun.getTowerGenerator() != null) {
player.teleport(WorldConfig.SPAWN.clone().add(0, TowerRun.getTowerGenerator().getHeight(), 0));
player.teleport(TowerRun.getTowerGenerator().getSpawn());
player.setGameMode(GameMode.SPECTATOR);
} else {
player.teleport(WorldConfig.SPAWN);

Datei anzeigen

@ -38,7 +38,7 @@ public class NotLobbyListener extends GameStateBukkitListener {
public void onPlayerJoin(PlayerJoinEvent event) {
event.getPlayer().setGameMode(GameMode.SPECTATOR);
if (TowerRun.getTowerGenerator() != null) {
event.getPlayer().teleport(WorldConfig.SPAWN.clone().add(0, TowerRun.getTowerGenerator().getHeight(), 0));
event.getPlayer().teleport(TowerRun.getTowerGenerator().getSpawn());
} else {
event.getPlayer().teleport(WorldConfig.SPAWN);
}