From 24a5c3e99596fbd17e054b6d6ee569e732da2abd Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 19 Aug 2023 12:30:57 +0200 Subject: [PATCH] Add WorldConfig.MELTING_TIMES --- configs/steamtower.yml | 51 ++++++++++++-- src/de/steamwar/towerrun/TowerRun.java | 2 - .../steamwar/towerrun/config/WorldConfig.java | 23 +++++-- .../towerrun/countdowns/LobbyCountdown.java | 2 +- .../steamwar/towerrun/game/TowerRunGame.java | 6 +- .../towerrun/game/TowerRunPlayer.java | 2 +- .../towerrun/generator/TowerGenerator.java | 27 +++++--- .../towerrun/listener/IngameListener.java | 67 ++++++++++++++++++- .../towerrun/listener/LobbyListener.java | 3 +- .../towerrun/listener/NotLobbyListener.java | 2 +- 10 files changed, 151 insertions(+), 34 deletions(-) diff --git a/configs/steamtower.yml b/configs/steamtower.yml index 397307f..1f820a1 100644 --- a/configs/steamtower.yml +++ b/configs/steamtower.yml @@ -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 diff --git a/src/de/steamwar/towerrun/TowerRun.java b/src/de/steamwar/towerrun/TowerRun.java index 6d6a214..f72eade 100644 --- a/src/de/steamwar/towerrun/TowerRun.java +++ b/src/de/steamwar/towerrun/TowerRun.java @@ -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; -import java.util.Optional; - @Plugin(name = "TowerRun", version = "1.0.0") @Dependency("SpigotCore") @Author("YoyoNow") diff --git a/src/de/steamwar/towerrun/config/WorldConfig.java b/src/de/steamwar/towerrun/config/WorldConfig.java index 43b08f7..f26e12e 100644 --- a/src/de/steamwar/towerrun/config/WorldConfig.java +++ b/src/de/steamwar/towerrun/config/WorldConfig.java @@ -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 MELTING_TIMES; public static final TowerGeneratorConfig TOWER_GENERATOR_CONFIG; public static final List 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 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]; } } } diff --git a/src/de/steamwar/towerrun/countdowns/LobbyCountdown.java b/src/de/steamwar/towerrun/countdowns/LobbyCountdown.java index df8a5a0..40808f1 100644 --- a/src/de/steamwar/towerrun/countdowns/LobbyCountdown.java +++ b/src/de/steamwar/towerrun/countdowns/LobbyCountdown.java @@ -47,7 +47,7 @@ public class LobbyCountdown extends Countdown { @Override void timerEnd() { - TowerRunGame.prepareTower(); + TowerRunGame.prepareTowerOrStart(); override = false; } diff --git a/src/de/steamwar/towerrun/game/TowerRunGame.java b/src/de/steamwar/towerrun/game/TowerRunGame.java index 477d4e1..04824a8 100644 --- a/src/de/steamwar/towerrun/game/TowerRunGame.java +++ b/src/de/steamwar/towerrun/game/TowerRunGame.java @@ -48,7 +48,7 @@ public class TowerRunGame { return PLAYERS_ALIVE.contains(player); } - public static void prepareTower() { + public static void prepareTowerOrStart() { 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(); } 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 -> { diff --git a/src/de/steamwar/towerrun/game/TowerRunPlayer.java b/src/de/steamwar/towerrun/game/TowerRunPlayer.java index b2ac89d..b60ddb7 100644 --- a/src/de/steamwar/towerrun/game/TowerRunPlayer.java +++ b/src/de/steamwar/towerrun/game/TowerRunPlayer.java @@ -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); } diff --git a/src/de/steamwar/towerrun/generator/TowerGenerator.java b/src/de/steamwar/towerrun/generator/TowerGenerator.java index 5df358f..07fcc58 100644 --- a/src/de/steamwar/towerrun/generator/TowerGenerator.java +++ b/src/de/steamwar/towerrun/generator/TowerGenerator.java @@ -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 { - private Random random = new Random(); - private WorldConfig.TowerGeneratorConfig config; - private List ALL_SCHEMATICS; + private static final Random random = new Random(); + private final WorldConfig.TowerGeneratorConfig config; + private List 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()); } - finishRunnable.run(); + TowerRunGame.start(); } } }.runTaskTimer(TowerRun.getInstance(), 0, 10); diff --git a/src/de/steamwar/towerrun/listener/IngameListener.java b/src/de/steamwar/towerrun/listener/IngameListener.java index e084d45..5c56088 100644 --- a/src/de/steamwar/towerrun/listener/IngameListener.java +++ b/src/de/steamwar/towerrun/listener/IngameListener.java @@ -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> 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 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); + } } diff --git a/src/de/steamwar/towerrun/listener/LobbyListener.java b/src/de/steamwar/towerrun/listener/LobbyListener.java index d579ab3..5c937ed 100644 --- a/src/de/steamwar/towerrun/listener/LobbyListener.java +++ b/src/de/steamwar/towerrun/listener/LobbyListener.java @@ -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); diff --git a/src/de/steamwar/towerrun/listener/NotLobbyListener.java b/src/de/steamwar/towerrun/listener/NotLobbyListener.java index 4dacaf2..4d3525e 100644 --- a/src/de/steamwar/towerrun/listener/NotLobbyListener.java +++ b/src/de/steamwar/towerrun/listener/NotLobbyListener.java @@ -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); }