diff --git a/src/main/java/world/bentobox/boxed/Boxed.java b/src/main/java/world/bentobox/boxed/Boxed.java index af78414..ce424f0 100644 --- a/src/main/java/world/bentobox/boxed/Boxed.java +++ b/src/main/java/world/bentobox/boxed/Boxed.java @@ -2,6 +2,7 @@ package world.bentobox.boxed; import java.util.Collections; +import org.bukkit.Bukkit; import org.bukkit.ChunkSnapshot; import org.bukkit.Difficulty; import org.bukkit.Material; @@ -27,6 +28,7 @@ import world.bentobox.boxed.generators.BoxedChunkGenerator; import world.bentobox.boxed.generators.BoxedSeedChunkGenerator; import world.bentobox.boxed.listeners.AdvancementListener; import world.bentobox.boxed.listeners.EnderPearlListener; +import world.bentobox.boxed.listeners.NewAreaListener; /** * Main Boxed class - provides a survival game inside a box @@ -110,7 +112,7 @@ public class Boxed extends GameModeAddon { // Register listeners this.registerListener(new AdvancementListener(this)); this.registerListener(new EnderPearlListener(this)); - //this.registerListener(new DebugListener(this)); + this.registerListener(new NewAreaListener(this)); // Register placeholders PlaceholdersManager phManager = new PlaceholdersManager(this); @@ -152,7 +154,8 @@ public class Boxed extends GameModeAddon { .createWorld(); seedWorld.setDifficulty(Difficulty.PEACEFUL); // No damage wanted in this world. saveChunks(seedWorld); - + // Unload seed world + //Bukkit.getServer().unloadWorld("seed", false); String worldName = settings.getWorldName().toLowerCase(); if (getServer().getWorld(worldName) == null) { diff --git a/src/main/java/world/bentobox/boxed/listeners/NewAreaListener.java b/src/main/java/world/bentobox/boxed/listeners/NewAreaListener.java new file mode 100644 index 0000000..3174714 --- /dev/null +++ b/src/main/java/world/bentobox/boxed/listeners/NewAreaListener.java @@ -0,0 +1,91 @@ +package world.bentobox.boxed.listeners; + +import java.io.File; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; + +import world.bentobox.bentobox.api.events.island.IslandCreatedEvent; +import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.bentobox.util.Util; +import world.bentobox.boxed.Boxed; + +/** + * @author tastybento + * Place structures in areas after they are created + */ +public class NewAreaListener implements Listener { + + private final Boxed addon; + private final YamlConfiguration config; + + /** + * @param addon addon + */ + public NewAreaListener(Boxed addon) { + this.addon = addon; + // Load the config + File structureFile = new File(addon.getDataFolder(), "structures.yml"); + // Check if it exists and if not, save it from the jar + if (!structureFile.exists()) { + addon.saveResource("structures.yml", true); + } + config = YamlConfiguration.loadConfiguration(structureFile); + + } + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onIslandCreated(IslandCreatedEvent e) { + addon.log(e.getEventName()); + Island island = e.getIsland(); + addon.getPlugin().getIWM().getAddon(island.getWorld()).ifPresent(gma -> addon.log(gma.getDescription().getName())); + // Check if this island is in this game + /* + if (!addon.getPlugin().getIWM().getAddon(island.getWorld()).map(gma -> gma.equals(addon)).orElse(false)) { + // Not correct addon + return; + }*/ + Location center = island.getProtectionCenter(); + ConfigurationSection section = config.getConfigurationSection("structures.new"); + if (section == null) { + addon.logError("structures.new not found"); + return; + } + for (String structure : section.getKeys(false)) { + addon.log(structure); + String value = section.getString(structure,""); + // Extract coords + String[] coords = value.split(","); + if (coords.length == 3) { + int x = Integer.valueOf(coords[0]) + center.getBlockX(); + int y = Integer.valueOf(coords[1]) + center.getBlockY(); + int z = Integer.valueOf(coords[2]) + center.getBlockZ(); + Util.getChunkAtAsync(center.getWorld(), x >> 4, z >> 4, true).thenAccept(c -> { + // Run command + String cmd = "place structure minecraft:" + structure + " " + + x + " " + + y + " " + + z + " "; + Bukkit.getScheduler().runTaskLater(addon.getPlugin(), () -> { + addon.log("run command " + cmd); + addon.getServer().dispatchCommand(Bukkit.getConsoleSender(), cmd); + }, 1000); + }); + + } else { + addon.logError("Structure file syntax error: " + structure + " " + value); + } + + } + + + } + + + +} diff --git a/src/main/resources/structures.yml b/src/main/resources/structures.yml new file mode 100644 index 0000000..f7cc1cf --- /dev/null +++ b/src/main/resources/structures.yml @@ -0,0 +1,3 @@ +structures: + new-island: + "minecraft:village": 100,0,100