Skip to content

Commit

Permalink
Added structure pasting - WIP
Browse files Browse the repository at this point in the history
Does not work if chunks around the paste point are not loaded.
  • Loading branch information
tastybento committed Dec 3, 2022
1 parent 29121d6 commit 12e1568
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/main/java/world/bentobox/boxed/Boxed.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collections;

import org.bukkit.Bukkit;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Difficulty;
import org.bukkit.Material;
Expand All @@ -27,6 +28,7 @@
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
Expand Down Expand Up @@ -110,7 +112,7 @@ public void onEnable() {
// 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);
Expand Down Expand Up @@ -152,7 +154,8 @@ public void createWorlds() {
.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) {
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/world/bentobox/boxed/listeners/NewAreaListener.java
Original file line number Diff line number Diff line change
@@ -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);
}

}


}



}
3 changes: 3 additions & 0 deletions src/main/resources/structures.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
structures:
new-island:
"minecraft:village": 100,0,100

0 comments on commit 12e1568

Please sign in to comment.