Added structure pasting - WIP

Does not work if chunks around the paste point are not loaded.
This commit is contained in:
tastybento 2022-12-03 09:40:08 -08:00
parent 29121d6fdb
commit 12e1568c75
3 changed files with 99 additions and 2 deletions

View File

@ -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) {

View File

@ -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);
}
}
}
}

View File

@ -0,0 +1,3 @@
structures:
new-island:
"minecraft:village": 100,0,100