Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ Never hold the whole region in memory or load the seed world on a normal boot

`areas/AreaManager` loads `areas.yml`. Each `Area` (seed, biomes file, structures file) has its own seed worlds (`<worldname>/seed_<id>`), its own templates (`areas/<id>/*.bxt`) and owns one east-west **row** of the box grid, persisted in `areas/grid.yml` and never reassigned. The generator, biome provider and populator pick the template from the chunk's z alone (`AbstractBoxedChunkGenerator.rowCalc` -> `Boxed.getTemplate(env, chunkZ)`), so anything that generates chunks must pass real game-world chunk coordinates, not folded ones. Boxes are placed by `AreaLocationStrategy`, returned from `Boxed.getNewIslandLocationStrategy(user, bundle)` (a BentoBox 3.25.0 hook): the blueprint bundle's unique id names the area, unknown bundles use `default`. The y of a box's centre comes from `areas/BoxHeight`: BentoBox pastes the starter blueprint with its bedrock at the centre, so the centre is set to land the blueprint's `[spawn_here]` sign on the area's ground (read from the template; falls back to the island-height setting). The nether blueprint is pasted at the same y, so it only lines up where the nether ground happens to match. Structure lists are per area via `Boxed.getStructuresFile(location)`. `AreaRules` refuses a box (by throwing from the hook, before anything is created) when the area is closed (`enabled: false`), is waiting for a restart, or is chained (`requires` + the required area's `unlocks-next-at` box size) and not yet unlocked, or the player already has a box in that area (one per area; the box being reset does not count). `/boxadmin areas` (`AdminAreasCommand`) lists, visits, creates and requests rebuilds of areas; creating and rebuilding only take effect at the next start because templates are built in `createWorlds()`.

### Reachability

`reach/` answers whether an area can be played. `LandSurvey` counts, per box size, the blocks, biomes and template entities of an area's templates plus the starter blueprint and the structures file. `Requirements` reads `requirements.yml`, a hand-written model of what each scored advancement needs (and `provides` rules that turn biomes and structures into mobs). `GrowthSimulator` earns everything in reach, grows the box by the scores and repeats; `ReachReport` writes it up. `/boxadmin areas check <area>` runs the survey off the main thread. Every advancement with a score in `advancements.yml` must have an entry in `requirements.yml` (a test enforces it).

### Deleted boxes

`listeners/BoxCleaner` restores a deleted (soft-deleted, `isDeletable()`) box's whole position from the template — generated chunks only, both dimensions, within a 10 ms per tick budget — then removes the island record so `AreaLocationStrategy` can hand the position out again (it always takes the first position in the row with no record). Do not rely on BentoBox's region-file purge for Boxed: it only deletes a region when every position overlapping it is deletable, and 640-block positions always share 512-block regions with neighbours. Deleted boxes are re-queued from the island cache on `BentoBoxReadyEvent`, so no extra persistence is needed.
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ You can also do the same by hand: add the area to `areas.yml` and make a bluepri
| `/boxadmin areas create <name> [seed]` | Makes a new area and its blueprint bundle. Needs a restart. |
| `/boxadmin areas rebuild <area> [regenerate]` | Builds the area's land again at the next restart. With `regenerate` the seed worlds are thrown away first, which is needed after changing the area's seed or biomes. Without it, the land is rebuilt from the seed worlds as they are, which keeps changes made to them by hand. |
| `/boxadmin areas clean [x z]` | Puts the land of the box position you are in (or the one at x, z) back the way it was. Use it after trying out structures in a position nobody has a box in. Positions with a box in use are refused. |
| `/boxadmin areas check <area>` | Plays out how a box grows in the area and reports where players get stuck. See [Checking that an area can be played](#checking-that-an-area-can-be-played). |

Areas can be chained so that a player's box in one area has to reach a certain size before the next area is unlocked, and can be closed to new boxes:

Expand Down Expand Up @@ -142,6 +143,28 @@ advancements:
...
```

### Checking that an area can be played

`/boxadmin areas check <area>` answers the question "can a player in this area actually grow their box?". It looks at the area's land, the starter blueprint and the area's structures file, then plays the game out on paper: starting from a new box, it earns every advancement whose needs are inside the box, grows the box by their scores, and repeats until nothing more can be earned.

The command prints one line, for example:

```
The box stops growing at size 263 of 320. 48 advancements earned for 262 points, 1 out of reach, 48 impossible (276 points).
```

and writes the full report to `areas/<area>/reachability.txt`. The report lists:

* the advancements earned in each round and the box size at the time,
* the advancements that were still out of reach when the box stopped growing, with what was missing and the box size at which it first appears,
* the advancements that are impossible in the area, with the reason,
* the box size at which each biome, mob and structure first comes into the box.

What each advancement needs is set in `requirements.yml`, which is written to the Boxed folder the first time you run the command. It is a model written by hand: it lists the raw resources an advancement comes down to (blocks, biomes, mobs, structures, other advancements), not every crafting step, and some entries are simplified. Mobs cannot be seen in the land itself, so the file also says which biomes and structures bring which mobs. Correct it to suit your server and your data pack. The check takes around half a minute and does not slow the server down.

An advancement that scores points but can never be earned is worth either removing from `advancements.yml` or making possible, for example by placing a structure with `/boxadmin place`.


### biomes.yml
The player's land has biomes and they are defined here. It's not possible to define where the biomes are right now, only what affect they have on the terrain.

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/world/bentobox/boxed/areas/BoxHeight.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static int height(int ground, int signAboveBedrock) {
* in the first bundle if the area has no bundle of its own.
*/
@Nullable
private Blueprint getBlueprint(Area area) {
public Blueprint getBlueprint(Area area) {
Map<String, BlueprintBundle> bundles = addon.getPlugin().getBlueprintsManager().getBlueprintBundles(addon);
BlueprintBundle own = bundles.get(area.id());
String name = Stream.concat(Stream.ofNullable(own),
Expand Down
124 changes: 124 additions & 0 deletions src/main/java/world/bentobox/boxed/commands/AdminAreasCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package world.bentobox.boxed.commands;

import java.util.HashMap;
import java.util.Map;
import java.util.EnumMap;
import java.util.Comparator;
Expand All @@ -8,25 +9,38 @@
import java.nio.file.Files;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
import java.util.concurrent.ThreadLocalRandom;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.blueprints.Blueprint;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBundle;
import world.bentobox.bentobox.util.Util;
import world.bentobox.boxed.Boxed;
import world.bentobox.boxed.areas.Area;
import world.bentobox.boxed.areas.AreaLocationStrategy;
import world.bentobox.boxed.areas.AreaManager;
import world.bentobox.boxed.areas.BoxHeight;
import world.bentobox.boxed.generators.template.Template;
import world.bentobox.boxed.reach.GrowthSimulator;
import world.bentobox.boxed.reach.LandSurvey;
import world.bentobox.boxed.reach.ReachReport;
import world.bentobox.boxed.reach.Requirements;

/**
* Admin command that lists the areas. Its sub-commands visit an area, make a new one and
Expand All @@ -39,6 +53,7 @@ public class AdminAreasCommand extends CompositeCommand {
private static final String PREFIX = "boxed.commands.boxadmin.areas.";

private static final String AREAS = "areas.";
private static final String REQUIREMENTS = "requirements.yml";
private static final String UNKNOWN_AREA = PREFIX + "unknown-area";

public AdminAreasCommand(CompositeCommand parent) {
Expand All @@ -54,6 +69,7 @@ public void setup() {
new Create(this);
new Rebuild(this);
new Clean(this);
new Check(this);
}

@Override
Expand Down Expand Up @@ -345,4 +361,112 @@ public boolean execute(User user, String label, List<String> args) {
return true;
}
}

/**
* Plays out how a box grows in an area and reports where players get stuck.
*/
static class Check extends CompositeCommand {

Check(CompositeCommand parent) {
super(parent, "check");
}

@Override
public void setup() {
this.setPermission("admin.areas.check");
this.setOnlyPlayer(false);
this.setParametersHelp(PREFIX + "check.parameters");
this.setDescription(PREFIX + "check.description");
}

@Override
public boolean execute(User user, String label, List<String> args) {
Boxed addon = getAddon();
if (args.size() != 1) {
this.showHelp(this, user);
return false;
}
Area area = addon.getAreas().getArea(args.get(0).toLowerCase(Locale.ENGLISH));
Template normal = area == null ? null : addon.getAreas().getTemplate(area, Environment.NORMAL);
if (area == null || normal == null) {
user.sendMessage(PREFIX + "unknown-area");
return false;
}
Template nether = addon.getAreas().getTemplate(area, Environment.NETHER);
// Everything is read on the main thread. Only the survey of the land runs off it.
File folder = addon.getDataFolder();
if (!new File(folder, REQUIREMENTS).exists()) {
addon.saveResource(REQUIREMENTS, false);
}
Requirements requirements = new Requirements(
YamlConfiguration.loadConfiguration(new File(folder, REQUIREMENTS)), addon::logWarning);
YamlConfiguration structures = YamlConfiguration.loadConfiguration(new File(folder, area.structures()));
Map<String, Integer> scores = getScores(folder);
Map<Vector, String> starter = getStarterBlocks(addon, area);
int radius = normal.getSize() * 16;
int startSize = Math.max(1, addon.getSettings().getIslandProtectionRange());
File report = new File(addon.getAreaFolder(area), "reachability.txt");
user.sendMessage(PREFIX + "check.started", TextVariables.NAME, area.id());
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> {
LandSurvey survey = new LandSurvey(radius);
survey.addTemplate(normal, false);
if (nether != null) {
survey.addTemplate(nether, true);
}
survey.addBlueprint(starter);
survey.addStructures(structures.getConfigurationSection("normal"), false);
survey.addStructures(structures.getConfigurationSection("nether"), true);
requirements.applyTo(survey);
GrowthSimulator.Result result = new GrowthSimulator(survey, requirements, scores, startSize).run();
String summary = ReachReport.summary(result);
String written;
try {
Files.write(report.toPath(), ReachReport.lines(area.id(), survey, result));
written = report.getPath();
} catch (IOException e) {
addon.logError("Could not write " + report + ": " + e.getMessage());
written = "-";
}
String path = written;
Bukkit.getScheduler().runTask(getPlugin(), () -> {
addon.log("Area " + area.id() + ": " + summary);
user.sendMessage(PREFIX + "check.result", TextVariables.DESCRIPTION, summary);
user.sendMessage(PREFIX + "check.report", "[file]", path);
});
});
return true;
}

/**
* @return how much each advancement grows the box, from advancements.yml
*/
private static Map<String, Integer> getScores(File folder) {
Map<String, Integer> scores = new HashMap<>();
ConfigurationSection adv = YamlConfiguration.loadConfiguration(new File(folder, "advancements.yml"))
.getConfigurationSection("advancements");
if (adv != null) {
adv.getKeys(false).forEach(k -> scores.put(k.contains(":") ? k : "minecraft:" + k, adv.getInt(k)));
}
return scores;
}

/**
* @return the starter blocks of an area, by position relative to the centre of the box
*/
private static Map<Vector, String> getStarterBlocks(Boxed addon, Area area) {
Map<Vector, String> starter = new HashMap<>();
Blueprint bp = new BoxHeight(addon).getBlueprint(area);
if (bp != null) {
Vector bedrock = bp.getBedrock() == null ? new Vector() : bp.getBedrock();
Stream.of(bp.getBlocks(), bp.getAttached()).filter(Objects::nonNull).forEach(
m -> m.forEach((pos, block) -> starter.put(pos.clone().subtract(bedrock), block.getBlockData())));
}
return starter;
}

@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
return areaNames(getAddon(), args);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,49 @@ public boolean isAirSection(int y) {
return sections[section] == null && palette[uniform[section]].getMaterial() == Material.AIR;
}

/**
* Something to do with each block of a chunk.
*/
@FunctionalInterface
public interface BlockVisitor {
/**
* @param x x in the chunk, 0 to 15
* @param y world y
* @param z z in the chunk, 0 to 15
* @param data the block
*/
void visit(int x, int y, int z, BlockData data);
}

/**
* Visits every block of the chunk that is not plain air.
* @param visitor called for each block
*/
public void forEachBlock(BlockVisitor visitor) {
for (int section = 0; section < sections.length; section++) {
short[] blocks = sections[section];
BlockData same = blocks == null ? palette[uniform[section]] : null;
if (same == null || same.getMaterial() != Material.AIR) {
visitSection(visitor, minY + (section << 4), blocks, same);
}
}
}

private void visitSection(BlockVisitor visitor, int baseY, short[] blocks, BlockData same) {
int i = 0;
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
BlockData data = same != null ? same : palette[blocks[i]];
i++;
if (data.getMaterial() != Material.AIR) {
visitor.visit(x, baseY + y, z, data);
}
}
}
}
}

/**
* Finds the ground in a column: the highest block that can be stood on, ignoring trees.
* @param x x in the chunk, 0 to 15
Expand Down
Loading
Loading