diff --git a/CLAUDE.md b/CLAUDE.md
index 670bded..e0f63ed 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -42,7 +42,9 @@ Never hold the whole region in memory or load the seed world on a normal boot
### Finding a place to start
-`/boxadmin areas find [seed|random] [radius]` (`AdminAreasFindCommand`) looks for good spots in a seed. `reach/SpotFinder` searches the vanilla biomes (no chunks needed) for wooded land with water nearby; the best few are generated in a throwaway `/finder_` world, using the area's biomes file laid out around every spot at once (`AbstractSeedBiomeProvider.setCentres`), and scored by `reach/SpotSurvey` (dry middle, then the box size that takes in logs, surface water, shallow stone, iron ore and coal). The world is unloaded without saving and deleted. An area uses a spot through `centre: x,z` in `areas.yml` (`Area.centreX/centreZ`, overworld only; the Nether template is still taken from 0,0).
+`/boxadmin areas find [seed|random] [radius]` (`AdminAreasFindCommand`) looks for good spots in a seed. `reach/SpotFinder` searches the vanilla biomes (no chunks needed) for wooded land with water nearby; the best few are generated in a throwaway `/finder_` world, using the area's biomes file laid out around every spot at once (`AbstractSeedBiomeProvider.setCentres`), and scored by `reach/SpotSurvey` (dry middle, then the box size that takes in logs, surface water, shallow stone, iron ore and coal). The world is unloaded without saving and deleted. An area uses a spot through `centre: x,z` in `areas.yml` (`Area.centreX/centreZ`) and `nether-centre: x,z` for the Nether. Fortresses have no structure templates, so they can only come from the land: after the overworld, the command locates the nearest fortresses in a throwaway Nether, generates each and uses `reach/FortressSpot` to suggest a `nether-centre` on open ground just clear of a blaze spawner.
+
+`listeners/FortressMobsListener` makes a copied fortress work: natural spawns on nether bricks in the game Nether are swapped for wither skeletons, blazes and skeletons at vanilla fortress weights (`world.nether.fortress-mobs`). `LandSurvey` records spawners as the mob they make, and `requirements.yml` treats nether bricks as the fortress.
### Deleted boxes
diff --git a/README.md b/README.md
index 0cfdfe6..6d17544 100644
--- a/README.md
+++ b/README.md
@@ -75,7 +75,7 @@ You can also do the same by hand: add the area to `areas.yml` and make a bluepri
| `/boxadmin areas` | Lists the areas with their row, seed and number of boxes. |
| `/boxadmin areas tp ` | Takes you to the land where the area's next box will be, so you can look at it and place structures before any player has a box there. |
| `/boxadmin areas create [seed]` | Makes a new area and its blueprint bundle. Needs a restart. |
-| `/boxadmin areas rebuild [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, centre 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 rebuild [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, centres 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 ` | 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). |
| `/boxadmin areas find [seed\|random] [radius]` | Looks through a seed for good places to start a box. See [Finding a good place to start](#finding-a-good-place-to-start). |
@@ -182,7 +182,19 @@ The three best places are shown and the full list is written to `areas/ /sp
dry land (100% dry), wood at size 4, water at size 14, stone at size 1, iron ore at size 2, coal at size 2
```
-To use a place, set the area's `seed` and `centre` in `areas.yml`, run `/boxadmin areas rebuild regenerate` and restart the server. Boxes that players have already explored do not change, so do this before the area is in use. `centre` moves the overworld only. The Nether of an area is always taken from 0,0 of its seed.
+To use a place, set the area's `seed` and `centre` in `areas.yml`, run `/boxadmin areas rebuild regenerate` and restart the server. Boxes that players have already explored do not change, so do this before the area is in use. `centre` moves the overworld only. The Nether has its own setting, `nether-centre`.
+
+#### The Nether fortress
+
+Blaze rods and wither skulls only come from a fortress, and a fortress cannot be placed with `/boxadmin place` the way a bastion can, because the game builds fortresses from code and not from structure files. The only way to give boxes a fortress is to take the Nether from a part of the seed that has one. The search does this for you: it finds the fortresses nearest the middle of the seed's Nether and, for each, lists a `nether-centre` on open ground a short way from a blaze spawner:
+
+```
+nether-centre: 272,128 (fortress at size 12, blaze spawner at size 34)
+```
+
+If you move the Nether, structures you placed there by hand (the bastion in the default `structures.yml`, for example) will need to be placed again to suit the new land.
+
+The Nether of a box is a copy, so the game does not know the fortress is there and would never spawn its mobs. Boxed does it instead: some of the mobs that spawn on nether bricks in the Nether become wither skeletons, blazes and skeletons, as often as they would in a real fortress. Blaze spawners are copied with the land and work as normal. Turn this off with `world.nether.fortress-mobs` in `config.yml`.
A good start does not mean the area can be finished. Run `/boxadmin areas check ` after the restart to see how far a box there can grow.
diff --git a/src/main/java/world/bentobox/boxed/Boxed.java b/src/main/java/world/bentobox/boxed/Boxed.java
index ea7d389..b1ba63f 100644
--- a/src/main/java/world/bentobox/boxed/Boxed.java
+++ b/src/main/java/world/bentobox/boxed/Boxed.java
@@ -63,6 +63,7 @@
import world.bentobox.boxed.listeners.AdvancementListener;
import world.bentobox.boxed.listeners.BoxCleaner;
import world.bentobox.boxed.listeners.EnderPearlListener;
+import world.bentobox.boxed.listeners.FortressMobsListener;
import world.bentobox.boxed.listeners.NewAreaListener;
/**
@@ -153,7 +154,8 @@ private boolean loadSettings() {
generatorMaps.put(area.seedWorldName(worldName, false), new BoxedSeedChunkGenerator(this,
Environment.NORMAL, new SeedBiomeGenerator(this, area.biomes(), area.centreX(), area.centreZ())));
generatorMaps.put(area.seedWorldName(worldName, true), new BoxedSeedChunkGenerator(this,
- Environment.NETHER, new NetherSeedBiomeGenerator(this, area.biomes())));
+ Environment.NETHER, new NetherSeedBiomeGenerator(this, area.biomes(), area.netherCentreX(),
+ area.netherCentreZ())));
}
return true;
@@ -184,6 +186,7 @@ public void onEnable() {
// Register listeners
this.registerListener(new AdvancementListener(this));
this.registerListener(new EnderPearlListener(this));
+ this.registerListener(new FortressMobsListener(this));
newAreaListener = new NewAreaListener(this);
this.registerListener(newAreaListener);
boxCleaner = new BoxCleaner(this);
@@ -231,8 +234,8 @@ public void createWorlds() {
boolean regenerate = checkRebuildRequest(area);
loadTemplate(area, worldName, Environment.NORMAL, area.centreX(), area.centreZ(), regenerate);
if (settings.isNetherGenerate()) {
- loadTemplate(area, worldName, Environment.NETHER, settings.getNetherSeedX(),
- settings.getNetherSeedZ(), regenerate);
+ loadTemplate(area, worldName, Environment.NETHER, area.netherCentreX(), area.netherCentreZ(),
+ regenerate);
}
}
// Create overworld
diff --git a/src/main/java/world/bentobox/boxed/Settings.java b/src/main/java/world/bentobox/boxed/Settings.java
index 2027d2b..5f471f5 100644
--- a/src/main/java/world/bentobox/boxed/Settings.java
+++ b/src/main/java/world/bentobox/boxed/Settings.java
@@ -173,6 +173,13 @@ public class Settings implements WorldSettings {
@ConfigEntry(path = "world.nether.generate")
private boolean netherGenerate = true;
+ @ConfigComment("Spawn fortress mobs in the Nether. The Nether of a box is copied block by block, so the")
+ @ConfigComment("game does not know where its fortress is and would never spawn wither skeletons there.")
+ @ConfigComment("With this on, some of the mobs that spawn on nether bricks become wither skeletons,")
+ @ConfigComment("blazes and skeletons, as often as they would in a real fortress.")
+ @ConfigEntry(path = "world.nether.fortress-mobs", since = "3.5.0")
+ private boolean netherFortressMobs = true;
+
@ConfigComment("This option indicates if nether portals should be linked via dimensions.")
@ConfigComment("Option will simulate vanilla portal mechanics that links portals together")
@ConfigComment("or creates a new portal, if there is not a portal in that dimension.")
@@ -640,6 +647,20 @@ public boolean isNetherGenerate() {
return netherGenerate;
}
+ /**
+ * @return true if fortress mobs are spawned on nether bricks in the Nether
+ */
+ public boolean isNetherFortressMobs() {
+ return netherFortressMobs;
+ }
+
+ /**
+ * @param netherFortressMobs true to spawn fortress mobs on nether bricks in the Nether
+ */
+ public void setNetherFortressMobs(boolean netherFortressMobs) {
+ this.netherFortressMobs = netherFortressMobs;
+ }
+
/**
* @return the netherIslands
*/
diff --git a/src/main/java/world/bentobox/boxed/areas/Area.java b/src/main/java/world/bentobox/boxed/areas/Area.java
index 31fbc07..188cc72 100644
--- a/src/main/java/world/bentobox/boxed/areas/Area.java
+++ b/src/main/java/world/bentobox/boxed/areas/Area.java
@@ -15,10 +15,13 @@
* 0 means that just having a box in the area is enough.
* @param centreX block x in the overworld seed world that the middle of every box is taken from
* @param centreZ block z in the overworld seed world that the middle of every box is taken from
+ * @param netherCentreX block x in the Nether seed world that the middle of every box is taken from
+ * @param netherCentreZ block z in the Nether seed world that the middle of every box is taken from
* @author tastybento
*/
public record Area(String id, long seed, String biomes, String structures, int row, boolean enabled,
- String requires, int unlockSize, int centreX, int centreZ) {
+ String requires, int unlockSize, int centreX, int centreZ,
+ int netherCentreX, int netherCentreZ) {
/**
* Name of the area that always exists
diff --git a/src/main/java/world/bentobox/boxed/areas/AreaManager.java b/src/main/java/world/bentobox/boxed/areas/AreaManager.java
index 97f72c8..9b82cdb 100644
--- a/src/main/java/world/bentobox/boxed/areas/AreaManager.java
+++ b/src/main/java/world/bentobox/boxed/areas/AreaManager.java
@@ -69,9 +69,11 @@ public AreaManager(YamlConfiguration config, File gridFile, long defaultSeed,
}
String requires = getRequirement(s, id, ids, errorLog);
int[] centre = parseCentre(s.getString("centre", s.getString("center", "")), id, errorLog);
+ int[] netherCentre = parseCentre(s.getString("nether-centre", s.getString("nether-center", "")), id,
+ errorLog);
Area area = new Area(id, s.getLong("seed", defaultSeed), s.getString("biomes", "biomes.yml"),
s.getString("structures", "structures.yml"), grid.getInt(path), s.getBoolean("enabled", true),
- requires, Math.max(0, s.getInt("unlocks-next-at", 0)), centre[0], centre[1]);
+ requires, Math.max(0, s.getInt("unlocks-next-at", 0)), centre[0], centre[1], netherCentre[0], netherCentre[1]);
areas.put(id, area);
areaByRow.put(area.row(), area);
}
diff --git a/src/main/java/world/bentobox/boxed/commands/AdminAreasFindCommand.java b/src/main/java/world/bentobox/boxed/commands/AdminAreasFindCommand.java
index 3bde1a7..7fb3976 100644
--- a/src/main/java/world/bentobox/boxed/commands/AdminAreasFindCommand.java
+++ b/src/main/java/world/bentobox/boxed/commands/AdminAreasFindCommand.java
@@ -6,6 +6,8 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Map;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
@@ -17,6 +19,13 @@
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.ChunkSnapshot;
+import org.bukkit.Location;
+import org.bukkit.Material;
+import org.bukkit.block.BlockState;
+import org.bukkit.block.CreatureSpawner;
+import org.bukkit.entity.EntityType;
+import org.bukkit.generator.structure.Structure;
+import org.bukkit.util.StructureSearchResult;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.WorldCreator;
@@ -28,8 +37,10 @@
import world.bentobox.bentobox.util.Util;
import world.bentobox.boxed.Boxed;
import world.bentobox.boxed.areas.Area;
+import world.bentobox.boxed.generators.biomes.NetherSeedBiomeGenerator;
import world.bentobox.boxed.generators.biomes.SeedBiomeGenerator;
import world.bentobox.boxed.generators.chunks.BoxedSeedChunkGenerator;
+import world.bentobox.boxed.reach.FortressSpot;
import world.bentobox.boxed.reach.SpotFinder;
import world.bentobox.boxed.reach.SpotFinder.Candidate;
import world.bentobox.boxed.reach.SpotSurvey;
@@ -80,7 +91,9 @@ private final class Search {
private final int radius;
private final SeedBiomeGenerator biomes;
private final List scored = new ArrayList<>();
+ private final List netherSpots = new ArrayList<>();
private World world;
+ private World nether;
private List spots = List.of();
Search(User user, Area area, long seed, int radius) {
@@ -135,7 +148,7 @@ void start() {
*/
private void survey(int index) {
if (index >= spots.size()) {
- finish(null);
+ startNether();
return;
}
Candidate spot = spots.get(index);
@@ -175,19 +188,116 @@ private void survey(int index) {
}));
}
+ /**
+ * Looks for the fortresses of the seed's Nether. Call on the main thread.
+ */
+ private void startNether() {
+ Boxed addon = getAddon();
+ if (!addon.getSettings().isNetherGenerate()) {
+ finish(null);
+ return;
+ }
+ nether = WorldCreator.name(world.getName() + "_nether")
+ .generator(new BoxedSeedChunkGenerator(addon, Environment.NETHER,
+ new NetherSeedBiomeGenerator(addon, area.biomes())))
+ .environment(Environment.NETHER).seed(seed).createWorld();
+ if (nether == null) {
+ finish(null);
+ return;
+ }
+ nether.setAutoSave(false);
+ // Fortresses are looked for around the middle of the seed and a way off on each side
+ List fortresses = new ArrayList<>();
+ for (int[] from : FORTRESS_SEARCHES) {
+ StructureSearchResult result = nether.locateNearestStructure(
+ new Location(nether, from[0], 64, from[1]), Structure.FORTRESS, FORTRESS_SEARCH_CHUNKS, false);
+ if (result != null && fortresses.size() < FORTRESSES) {
+ int[] at = { result.getLocation().getBlockX() >> 4, result.getLocation().getBlockZ() >> 4 };
+ if (fortresses.stream().noneMatch(f -> f[0] == at[0] && f[1] == at[1])) {
+ fortresses.add(at);
+ }
+ }
+ }
+ surveyFortress(fortresses, 0);
+ }
+
+ /**
+ * Generates a fortress and picks where the middle of the Nether should go to have it.
+ */
+ private void surveyFortress(List fortresses, int index) {
+ if (index >= fortresses.size()) {
+ finish(null);
+ return;
+ }
+ int[] fortress = fortresses.get(index);
+ List> chunks = new ArrayList<>();
+ for (int x = -FORTRESS_REACH; x <= FORTRESS_REACH; x++) {
+ for (int z = -FORTRESS_REACH; z <= FORTRESS_REACH; z++) {
+ chunks.add(nether.getChunkAtAsync(fortress[0] + x, fortress[1] + z));
+ }
+ }
+ CompletableFuture.allOf(chunks.toArray(CompletableFuture[]::new)).whenComplete((v, error) ->
+ Bukkit.getScheduler().runTask(getPlugin(), () -> {
+ if (error != null) {
+ getAddon().logError("Could not generate the fortress at chunk " + fortress[0] + "," + fortress[1]
+ + ": " + error);
+ surveyFortress(fortresses, index + 1);
+ return;
+ }
+ List spawners = new ArrayList<>();
+ Map snapshots = new HashMap<>();
+ for (CompletableFuture chunk : chunks) {
+ Chunk c = chunk.join();
+ addBlazeSpawners(c, spawners);
+ snapshots.put(key(c.getX(), c.getZ()), c.getChunkSnapshot(false, false, false));
+ nether.unloadChunk(c.getX(), c.getZ(), false);
+ }
+ Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> {
+ Optional spot = chooseNetherSpot(fortress, spawners, snapshots);
+ Bukkit.getScheduler().runTask(getPlugin(), () -> {
+ spot.ifPresent(netherSpots::add);
+ surveyFortress(fortresses, index + 1);
+ });
+ });
+ }));
+ }
+
+ private Optional chooseNetherSpot(int[] fortress, List spawners,
+ Map snapshots) {
+ List bricks = new ArrayList<>();
+ snapshots.values().forEach(s -> {
+ for (int x = 0; x < 16; x++) {
+ for (int z = 0; z < 16; z++) {
+ if (hasBricks(s, x, z)) {
+ bricks.add(new int[] { (s.getX() << 4) + x, (s.getZ() << 4) + z });
+ }
+ }
+ }
+ });
+ int[] min = { (fortress[0] - FORTRESS_REACH) << 4, (fortress[1] - FORTRESS_REACH) << 4 };
+ int[] max = { ((fortress[0] + FORTRESS_REACH) << 4) + 15, ((fortress[1] + FORTRESS_REACH) << 4) + 15 };
+ return FortressSpot.choose(min, max, spawners, bricks, (x, z) -> {
+ ChunkSnapshot s = snapshots.get(key(x >> 4, z >> 4));
+ return s != null && isOpen(s, x & 15, z & 15) ? 1 : 0;
+ });
+ }
+
/**
* Reports what was found and throws the world away. Call on the main thread.
* @param problem message to send if the search did not work, or null if it did
*/
private void finish(String problem) {
File folder = world == null ? null : world.getWorldFolder();
- if (world != null && !Bukkit.unloadWorld(world, false)) {
- getAddon().logWarning("Could not unload " + world.getName());
- folder = null;
+ for (World w : new World[] { world, nether }) {
+ if (w != null && !Bukkit.unloadWorld(w, false)) {
+ getAddon().logWarning("Could not unload " + w.getName());
+ folder = null;
+ }
}
File toDelete = folder;
File report = new File(AdminAreasFindCommand.this.getAddon().getAreaFolder(area), "spots.txt");
scored.sort(Comparator.comparingInt((Scored s) -> s.score().score()).reversed());
+ netherSpots.sort(Comparator.comparingInt(FortressSpot.Spot::spawnerSize));
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> {
deleteWorlds(toDelete);
boolean written = problem == null && write(report);
@@ -200,6 +310,9 @@ private void finish(String problem) {
scored.stream().limit(SHOWN).forEach(s -> user.sendMessage(PREFIX + "result", TextVariables.XYZ,
s.spot().x() + "," + s.spot().z(), TextVariables.NUMBER,
String.valueOf(s.score().score()), TextVariables.DESCRIPTION, s.score().describe()));
+ netherSpots.stream().findFirst().ifPresent(n -> user.sendMessage(PREFIX + "nether",
+ TextVariables.XYZ, n.x() + "," + n.z(), TextVariables.NUMBER,
+ String.valueOf(n.spawnerSize())));
user.sendMessage(PREFIX + "report", "[file]", written ? report.getPath() : "-");
user.sendMessage(PREFIX + "how-to", TextVariables.NAME, area.id(), "[seed]", String.valueOf(seed));
});
@@ -253,6 +366,16 @@ private boolean write(File report) {
c.waterDistance() < 0 ? "more than 128" : String.valueOf(c.waterDistance()), c.landPercent()));
lines.add(" " + s.score().describe());
}
+ lines.add("");
+ lines.add("Places for the middle of the Nether that give every box a fortress");
+ lines.add("(set nether-centre for the area in areas.yml, then rebuild it as above):");
+ if (netherSpots.isEmpty()) {
+ lines.add(" none found");
+ }
+ for (FortressSpot.Spot n : netherSpots) {
+ lines.add(" nether-centre: " + n.x() + "," + n.z() + " (fortress at size " + n.fortressSize()
+ + ", blaze spawner at size " + n.spawnerSize() + ")");
+ }
try {
Files.createDirectories(report.getParentFile().toPath());
Files.write(report.toPath(), lines);
@@ -264,6 +387,63 @@ private boolean write(File report) {
}
}
+ private static void addBlazeSpawners(Chunk chunk, List spawners) {
+ for (BlockState tile : chunk.getTileEntities()) {
+ if (tile instanceof CreatureSpawner spawner && spawner.getSpawnedType() == EntityType.BLAZE) {
+ spawners.add(new int[] { tile.getX(), tile.getZ() });
+ }
+ }
+ }
+
+ private static long key(int chunkX, int chunkZ) {
+ return ((long) chunkX << 32) | (chunkZ & 0xFFFFFFFFL);
+ }
+
+ private static boolean isBrick(Material m) {
+ return m == Material.NETHER_BRICKS || m == Material.NETHER_BRICK_FENCE || m == Material.NETHER_BRICK_STAIRS;
+ }
+
+ /**
+ * @return true if a column of the Nether has part of a fortress in it
+ */
+ private static boolean hasBricks(ChunkSnapshot s, int x, int z) {
+ for (int y = NETHER_FLOOR; y < NETHER_ROOF; y++) {
+ if (isBrick(s.getBlockType(x, y, z))) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * @return true if a column of the Nether has solid ground with room to stand on it
+ */
+ private static boolean isOpen(ChunkSnapshot s, int x, int z) {
+ for (int y = NETHER_FLOOR; y < NETHER_ROOF; y++) {
+ Material floor = s.getBlockType(x, y, z);
+ if (floor.isSolid() && !isBrick(floor) && s.getBlockType(x, y + 1, z).isAir()
+ && s.getBlockType(x, y + 2, z).isAir()) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * The lava sea is below this
+ */
+ private static final int NETHER_FLOOR = 32;
+ private static final int NETHER_ROOF = 110;
+ /**
+ * How many fortresses are looked at
+ */
+ private static final int FORTRESSES = 3;
+ /**
+ * How far around a fortress the land is generated, in chunks
+ */
+ private static final int FORTRESS_REACH = 8;
+ private static final int FORTRESS_SEARCH_CHUNKS = 100;
+ private static final int[][] FORTRESS_SEARCHES = { { 0, 0 }, { 1500, 0 }, { -1500, 0 }, { 0, 1500 }, { 0, -1500 } };
private static final String SHALLOW_NOTE = "eight blocks of the surface, three iron ore and one coal ore at any depth.";
/**
diff --git a/src/main/java/world/bentobox/boxed/generators/biomes/NetherSeedBiomeGenerator.java b/src/main/java/world/bentobox/boxed/generators/biomes/NetherSeedBiomeGenerator.java
index 5119080..86f6341 100644
--- a/src/main/java/world/bentobox/boxed/generators/biomes/NetherSeedBiomeGenerator.java
+++ b/src/main/java/world/bentobox/boxed/generators/biomes/NetherSeedBiomeGenerator.java
@@ -22,4 +22,14 @@ public NetherSeedBiomeGenerator(Boxed boxed) {
public NetherSeedBiomeGenerator(Boxed boxed, String biomesFile) {
super(boxed, Environment.NETHER, biomesFile);
}
+
+ /**
+ * @param boxed addon
+ * @param biomesFile name of the file that defines the biomes
+ * @param centreX block x of the spot the biomes are laid out around
+ * @param centreZ block z of the spot the biomes are laid out around
+ */
+ public NetherSeedBiomeGenerator(Boxed boxed, String biomesFile, int centreX, int centreZ) {
+ super(boxed, Environment.NETHER, biomesFile, centreX, centreZ);
+ }
}
diff --git a/src/main/java/world/bentobox/boxed/listeners/FortressMobsListener.java b/src/main/java/world/bentobox/boxed/listeners/FortressMobsListener.java
new file mode 100644
index 0000000..bb0610f
--- /dev/null
+++ b/src/main/java/world/bentobox/boxed/listeners/FortressMobsListener.java
@@ -0,0 +1,111 @@
+package world.bentobox.boxed.listeners;
+
+import java.util.Random;
+
+import org.bukkit.Location;
+import org.bukkit.Material;
+import org.bukkit.block.Block;
+import org.bukkit.block.BlockFace;
+import org.bukkit.entity.EntityType;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.EventPriority;
+import org.bukkit.event.Listener;
+import org.bukkit.event.entity.CreatureSpawnEvent;
+import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
+
+import world.bentobox.boxed.Boxed;
+
+/**
+ * Makes the fortress in the Nether of a box work. The Nether is copied block by block, so
+ * the game does not know there is a fortress there and never spawns its mobs. This swaps
+ * some of the mobs that spawn on nether bricks for wither skeletons, blazes and skeletons,
+ * in the same proportions as a real fortress.
+ *
+ * @author tastybento
+ */
+public class FortressMobsListener implements Listener {
+
+ /**
+ * How often each mob spawns in a real fortress, out of {@link #TOTAL}. The rest of the
+ * time the mob the game picked is left alone.
+ */
+ private static final int WITHER_SKELETON = 8;
+ private static final int BLAZE = 10;
+ private static final int SKELETON = 2;
+ private static final int TOTAL = 28;
+ /**
+ * Skeletons need it this dark. Blazes spawn in a little more light.
+ */
+ private static final int DARK = 7;
+ private static final int BLAZE_LIGHT = 11;
+
+ private final Boxed addon;
+ private final Random random;
+ private boolean swapping;
+
+ /**
+ * @param addon addon
+ */
+ public FortressMobsListener(Boxed addon) {
+ this(addon, new Random());
+ }
+
+ /**
+ * @param addon addon
+ * @param random source of the dice rolls
+ */
+ FortressMobsListener(Boxed addon, Random random) {
+ this.addon = addon;
+ this.random = random;
+ }
+
+ /**
+ * Swaps a mob that is spawning on nether bricks for a fortress mob.
+ * @param e event
+ */
+ @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
+ public void onSpawn(CreatureSpawnEvent e) {
+ Location loc = e.getLocation();
+ if (swapping || e.getSpawnReason() != SpawnReason.NATURAL || !addon.getSettings().isNetherFortressMobs()
+ || loc.getWorld() == null || !loc.getWorld().equals(addon.getNetherWorld())) {
+ return;
+ }
+ Block feet = loc.getBlock();
+ if (feet.getRelative(BlockFace.DOWN).getType() != Material.NETHER_BRICKS) {
+ return;
+ }
+ EntityType type = pick(feet);
+ if (type == null || type == e.getEntityType()) {
+ return;
+ }
+ e.setCancelled(true);
+ // The new mob fires this event too
+ swapping = true;
+ try {
+ loc.getWorld().spawnEntity(loc, type, SpawnReason.NATURAL);
+ } finally {
+ swapping = false;
+ }
+ }
+
+ /**
+ * @param feet the block the mob spawns in
+ * @return the fortress mob to spawn instead, or null to leave the spawn alone
+ */
+ private EntityType pick(Block feet) {
+ int roll = random.nextInt(TOTAL);
+ int light = feet.getLightFromBlocks();
+ if (roll < WITHER_SKELETON) {
+ // Wither skeletons are nearly three blocks tall
+ boolean room = feet.getRelative(BlockFace.UP).isPassable() && feet.getRelative(BlockFace.UP, 2).isPassable();
+ return light <= DARK && room ? EntityType.WITHER_SKELETON : null;
+ }
+ if (roll < WITHER_SKELETON + BLAZE) {
+ return light <= BLAZE_LIGHT ? EntityType.BLAZE : null;
+ }
+ if (roll < WITHER_SKELETON + BLAZE + SKELETON) {
+ return light <= DARK ? EntityType.SKELETON : null;
+ }
+ return null;
+ }
+}
diff --git a/src/main/java/world/bentobox/boxed/reach/FortressSpot.java b/src/main/java/world/bentobox/boxed/reach/FortressSpot.java
new file mode 100644
index 0000000..b2229e9
--- /dev/null
+++ b/src/main/java/world/bentobox/boxed/reach/FortressSpot.java
@@ -0,0 +1,76 @@
+package world.bentobox.boxed.reach;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.function.IntBinaryOperator;
+
+/**
+ * Picks where the middle of an area's Nether should be so that its boxes get a fortress.
+ * A fortress cannot be placed like other structures, so the only way to have one is to take
+ * the land from a part of the seed that has one. The middle is put on open ground a short
+ * way from a blaze spawner: close enough that the box soon grows to take it in, but not so
+ * close that new arrivals land among the blazes.
+ *
+ * @author tastybento
+ */
+public final class FortressSpot {
+
+ /**
+ * The nearest a blaze spawner may be, as a box size
+ */
+ public static final int MIN_SPAWNER_SIZE = 32;
+ /**
+ * The nearest any part of the fortress may be, as a box size
+ */
+ public static final int MIN_FORTRESS_SIZE = 8;
+
+ /**
+ * A place for the middle of the Nether.
+ * @param x block x
+ * @param z block z
+ * @param spawnerSize the box size that takes in a blaze spawner
+ * @param fortressSize the box size that takes in the first of the fortress
+ */
+ public record Spot(int x, int z, int spawnerSize, int fortressSize) {
+ }
+
+ private FortressSpot() {
+ }
+
+ /**
+ * Looks over the chunk corners of a piece of land for the best place.
+ * @param min lowest block x and z of the land
+ * @param max highest block x and z of the land
+ * @param spawners block x and z of each blaze spawner
+ * @param bricks block x and z of each column that has fortress in it
+ * @param open says if a column is open ground: 1 if it is, 0 if not
+ * @return the best place, or empty if the land has no spawner or no open ground near one
+ */
+ public static Optional choose(int[] min, int[] max, List spawners, List bricks,
+ IntBinaryOperator open) {
+ Spot best = null;
+ for (int x = (min[0] + 15) >> 4 << 4; x <= max[0]; x += 16) {
+ for (int z = (min[1] + 15) >> 4 << 4; z <= max[1]; z += 16) {
+ int spawner = nearest(spawners, x, z);
+ int fortress = nearest(bricks, x, z);
+ if (spawner >= MIN_SPAWNER_SIZE && spawner != Integer.MAX_VALUE && fortress >= MIN_FORTRESS_SIZE
+ && open.applyAsInt(x, z) == 1 && (best == null || spawner < best.spawnerSize()
+ || (spawner == best.spawnerSize() && fortress < best.fortressSize()))) {
+ best = new Spot(x, z, spawner, fortress);
+ }
+ }
+ }
+ return Optional.ofNullable(best);
+ }
+
+ /**
+ * @return the smallest box around a place that takes in one of the columns
+ */
+ private static int nearest(List columns, int x, int z) {
+ int nearest = Integer.MAX_VALUE;
+ for (int[] c : columns) {
+ nearest = Math.min(nearest, LandSurvey.sizeFor(c[0] - x, c[1] - z));
+ }
+ return nearest;
+ }
+}
diff --git a/src/main/java/world/bentobox/boxed/reach/LandSurvey.java b/src/main/java/world/bentobox/boxed/reach/LandSurvey.java
index bc79f48..9d39067 100644
--- a/src/main/java/world/bentobox/boxed/reach/LandSurvey.java
+++ b/src/main/java/world/bentobox/boxed/reach/LandSurvey.java
@@ -11,6 +11,8 @@
import org.bukkit.block.data.BlockData;
import org.bukkit.configuration.ConfigurationSection;
+import world.bentobox.bentobox.blueprints.dataobjects.BlueprintCreatureSpawner;
+import world.bentobox.boxed.generators.chunks.AbstractBoxedChunkGenerator.ChestData;
import world.bentobox.boxed.generators.chunks.AbstractBoxedChunkGenerator.EntityData;
import world.bentobox.boxed.generators.template.Template;
import world.bentobox.boxed.generators.template.TemplateChunk;
@@ -186,6 +188,7 @@ public void addTemplate(Template template, boolean nether) {
addBlocks(chunk, chunkX << 4, chunkZ << 4, nether, blockCounters);
addBiomes(template, chunkX, chunkZ, nether, biomeCounters);
addEntities(chunk, chunkX << 4, chunkZ << 4, nether);
+ addSpawners(chunk, chunkX << 4, chunkZ << 4, nether);
}
}
}
@@ -227,6 +230,19 @@ private void addEntities(TemplateChunk chunk, int baseX, int baseZ, boolean neth
}
}
+ /**
+ * A spawner is as good as the mob it makes, so it is recorded as that mob.
+ */
+ private void addSpawners(TemplateChunk chunk, int baseX, int baseZ, boolean nether) {
+ for (ChestData tile : chunk.getChests()) {
+ BlueprintCreatureSpawner spawner = tile.chest().getCreatureSpawner();
+ if (spawner != null && spawner.getSpawnedType() != null) {
+ add(nether ? Kind.NETHER_ENTITY : Kind.ENTITY, spawner.getSpawnedType().getKey().getKey(),
+ sizeFor(baseX + tile.relativeLoc().getBlockX(), baseZ + tile.relativeLoc().getBlockZ()), 1);
+ }
+ }
+ }
+
private static final int[] BIOME_HEIGHTS = { 64, 32, 0, -32, -52, 100, 160 };
/**
diff --git a/src/main/resources/areas.yml b/src/main/resources/areas.yml
index 3b8cfad..8c2435e 100644
--- a/src/main/resources/areas.yml
+++ b/src/main/resources/areas.yml
@@ -13,6 +13,9 @@
# centre - the place in the seed that the middle of every box is taken from, as x,z.
# The default is 0,0. /boxadmin areas find [seed|random] looks through
# a seed for good places to start and lists them with their scores.
+# nether-centre - the same for the Nether. A fortress cannot be placed like other
+# structures, so the Nether has to be taken from a place that has one.
+# /boxadmin areas find lists places that do.
# biomes - the file that says which biomes are placed around the middle of the area.
# structures - the file that lists the structures placed in each new box of this area.
# enabled - set to false to stop new boxes being made in this area. Boxes already there
@@ -32,7 +35,7 @@
# /boxadmin areas check and look for a better place to start with /boxadmin areas find.
#
# The land of an area is built once, when the server first starts with the area in this
-# file, and saved in the areas folder. Changing an area's seed, centre or biomes afterwards does
+# file, and saved in the areas folder. Changing an area's seed, centres or biomes afterwards does
# nothing until you run /boxadmin areas rebuild regenerate and restart.
# Boxes that players have already explored do not change.
#
diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml
index 7cd4731..2cf75f1 100644
--- a/src/main/resources/config.yml
+++ b/src/main/resources/config.yml
@@ -87,6 +87,12 @@ world:
# Note that with a standard nether all players arrive at the same portal and entering a
# portal will return them back to their areas.
generate: true
+ # Spawn fortress mobs in the Nether. The Nether of a box is copied block by block, so the
+ # game does not know where its fortress is and would never spawn wither skeletons there.
+ # With this on, some of the mobs that spawn on nether bricks become wither skeletons,
+ # blazes and skeletons, as often as they would in a real fortress.
+ # Added since 3.5.0.
+ fortress-mobs: true
# This option indicates if nether portals should be linked via dimensions.
# Option will simulate vanilla portal mechanics that links portals together
# or creates a new portal, if there is not a portal in that dimension.
diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml
index 394874d..eb47dbd 100755
--- a/src/main/resources/locales/en-US.yml
+++ b/src/main/resources/locales/en-US.yml
@@ -618,6 +618,7 @@ boxed:
none: '&c No dry, wooded land was found. Try another seed or a bigger search radius.'
failed: '&c The search could not be run. Check the console for the error'
result: '&e [xyz] &a scores [number]: &7 [description]'
+ nether: '&a Nether: &e nether-centre: [xyz] &a gives every box a fortress, with a blaze spawner at size [number]'
report: '&a The full list is in &e [file]'
how-to: '&a To use a place, set &e seed: [seed] &a and &e centre: x,z &a for [name] in areas.yml, run &e /boxadmin areas rebuild [name] regenerate &a and restart.'
island:
diff --git a/src/main/resources/requirements.yml b/src/main/resources/requirements.yml
index e38039b..bbcae24 100644
--- a/src/main/resources/requirements.yml
+++ b/src/main/resources/requirements.yml
@@ -86,8 +86,10 @@ provides:
- nether-biome:crimson_forest -> nether-entity:hoglin
- nether-biome:basalt_deltas -> nether-entity:magma_cube
- nether-block:lava -> nether-entity:strider
- - nether-structure:fortress/* -> nether-entity:blaze
- - nether-structure:fortress/* -> nether-entity:wither_skeleton
+ # A fortress cannot be placed as a structure. It is part of the land, so it shows up as
+ # nether bricks, and its blaze spawners count as blazes without a rule here.
+ # Boxed spawns wither skeletons on nether bricks (world.nether.fortress-mobs in config.yml).
+ - nether-block:nether_bricks -> nether-entity:wither_skeleton
- nether-structure:bastion/* -> nether-entity:piglin
sets:
@@ -116,7 +118,7 @@ sets:
ghast:
- nether-entity:ghast
fortress:
- - nether-structure:fortress/*
+ - nether-block:nether_bricks
crying-obsidian:
- block:crying_obsidian || nether-block:crying_obsidian || structure:ruined_portal/* || nether-entity:piglin
explore-nether:
diff --git a/src/test/java/world/bentobox/boxed/areas/AreaManagerTest.java b/src/test/java/world/bentobox/boxed/areas/AreaManagerTest.java
index a80215a..81309f4 100644
--- a/src/test/java/world/bentobox/boxed/areas/AreaManagerTest.java
+++ b/src/test/java/world/bentobox/boxed/areas/AreaManagerTest.java
@@ -199,6 +199,7 @@ void testCentre() throws Exception {
areas:
moved:
centre: 1210, -650
+ nether-centre: 272,128
american:
center: "32,48"
broken:
@@ -209,6 +210,9 @@ void testCentre() throws Exception {
// The spot is moved to the corner of its chunk
assertEquals(1200, am.getArea("moved").centreX());
assertEquals(-656, am.getArea("moved").centreZ());
+ assertEquals(272, am.getArea("moved").netherCentreX());
+ assertEquals(128, am.getArea("moved").netherCentreZ());
+ assertEquals(0, am.getDefaultArea().netherCentreX());
assertEquals(32, am.getArea("american").centreX());
assertEquals(48, am.getArea("american").centreZ());
assertEquals(0, am.getArea("broken").centreX());
diff --git a/src/test/java/world/bentobox/boxed/listeners/FortressMobsListenerTest.java b/src/test/java/world/bentobox/boxed/listeners/FortressMobsListenerTest.java
new file mode 100644
index 0000000..e14970a
--- /dev/null
+++ b/src/test/java/world/bentobox/boxed/listeners/FortressMobsListenerTest.java
@@ -0,0 +1,170 @@
+package world.bentobox.boxed.listeners;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.Random;
+
+import org.bukkit.Material;
+import org.bukkit.World;
+import org.bukkit.block.Block;
+import org.bukkit.block.BlockFace;
+import org.bukkit.entity.EntityType;
+import org.bukkit.event.entity.CreatureSpawnEvent;
+import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+
+import world.bentobox.boxed.Boxed;
+import world.bentobox.boxed.CommonTestSetup;
+import world.bentobox.boxed.Settings;
+
+/**
+ * Tests the swapping of Nether mobs for fortress mobs.
+ */
+class FortressMobsListenerTest extends CommonTestSetup {
+
+ @Mock
+ private Boxed addon;
+ @Mock
+ private Random random;
+ @Mock
+ private CreatureSpawnEvent event;
+ @Mock
+ private Block feet;
+ @Mock
+ private Block floor;
+ @Mock
+ private Block above;
+
+ private Settings settings;
+ private FortressMobsListener listener;
+
+ @Override
+ @BeforeEach
+ public void setUp() {
+ super.setUp();
+ settings = new Settings();
+ when(addon.getSettings()).thenReturn(settings);
+ when(addon.getNetherWorld()).thenReturn(world);
+ when(location.getWorld()).thenReturn(world);
+ when(location.getBlock()).thenReturn(feet);
+ when(feet.getRelative(BlockFace.DOWN)).thenReturn(floor);
+ when(feet.getRelative(BlockFace.UP)).thenReturn(above);
+ when(feet.getRelative(BlockFace.UP, 2)).thenReturn(above);
+ when(above.isPassable()).thenReturn(true);
+ when(floor.getType()).thenReturn(Material.NETHER_BRICKS);
+ when(feet.getLightFromBlocks()).thenReturn((byte) 0);
+ when(event.getLocation()).thenReturn(location);
+ when(event.getSpawnReason()).thenReturn(SpawnReason.NATURAL);
+ when(event.getEntityType()).thenReturn(EntityType.ZOMBIFIED_PIGLIN);
+ listener = new FortressMobsListener(addon, random);
+ }
+
+ @Override
+ @AfterEach
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ private void roll(int value) {
+ when(random.nextInt(28)).thenReturn(value);
+ }
+
+ private void verifySwappedFor(EntityType type) {
+ verify(event).setCancelled(true);
+ verify(world).spawnEntity(location, type, SpawnReason.NATURAL);
+ }
+
+ private void verifyLeftAlone() {
+ verify(event, never()).setCancelled(true);
+ verify(world, never()).spawnEntity(any(), any(EntityType.class), any(SpawnReason.class));
+ }
+
+ @Test
+ void testWitherSkeleton() {
+ roll(0);
+ listener.onSpawn(event);
+ verifySwappedFor(EntityType.WITHER_SKELETON);
+ }
+
+ @Test
+ void testBlaze() {
+ roll(8);
+ // Blazes spawn in more light than skeletons
+ when(feet.getLightFromBlocks()).thenReturn((byte) 11);
+ listener.onSpawn(event);
+ verifySwappedFor(EntityType.BLAZE);
+ }
+
+ @Test
+ void testSkeleton() {
+ roll(18);
+ listener.onSpawn(event);
+ verifySwappedFor(EntityType.SKELETON);
+ }
+
+ @Test
+ void testMostSpawnsAreLeftAlone() {
+ roll(20);
+ listener.onSpawn(event);
+ verifyLeftAlone();
+ }
+
+ @Test
+ void testNotOnNetherBricks() {
+ roll(0);
+ when(floor.getType()).thenReturn(Material.NETHERRACK);
+ listener.onSpawn(event);
+ verifyLeftAlone();
+ }
+
+ @Test
+ void testNotInTheLight() {
+ roll(0);
+ when(feet.getLightFromBlocks()).thenReturn((byte) 12);
+ listener.onSpawn(event);
+ verifyLeftAlone();
+ }
+
+ @Test
+ void testNoRoomForAWitherSkeleton() {
+ roll(0);
+ when(above.isPassable()).thenReturn(false);
+ listener.onSpawn(event);
+ verifyLeftAlone();
+ }
+
+ @Test
+ void testOtherWorldsAndSpawnReasons() {
+ roll(0);
+ World other = mock(World.class);
+ when(addon.getNetherWorld()).thenReturn(other);
+ listener.onSpawn(event);
+ when(addon.getNetherWorld()).thenReturn(world);
+ when(event.getSpawnReason()).thenReturn(SpawnReason.SPAWNER);
+ listener.onSpawn(event);
+ verifyLeftAlone();
+ }
+
+ @Test
+ void testSwitchedOff() {
+ roll(0);
+ settings.setNetherFortressMobs(false);
+ listener.onSpawn(event);
+ verifyLeftAlone();
+ }
+
+ @Test
+ void testSameMobIsNotSwapped() {
+ roll(18);
+ when(event.getEntityType()).thenReturn(EntityType.SKELETON);
+ listener.onSpawn(event);
+ verifyLeftAlone();
+ }
+}
diff --git a/src/test/java/world/bentobox/boxed/reach/FortressSpotTest.java b/src/test/java/world/bentobox/boxed/reach/FortressSpotTest.java
new file mode 100644
index 0000000..85caebd
--- /dev/null
+++ b/src/test/java/world/bentobox/boxed/reach/FortressSpotTest.java
@@ -0,0 +1,60 @@
+package world.bentobox.boxed.reach;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.List;
+import java.util.Optional;
+
+import org.junit.jupiter.api.Test;
+
+import world.bentobox.boxed.reach.FortressSpot.Spot;
+
+/**
+ * Tests picking the middle of the Nether next to a fortress.
+ */
+class FortressSpotTest {
+
+ private static final int[] MIN = { -128, -128 };
+ private static final int[] MAX = { 127, 127 };
+
+ @Test
+ void testPicksOpenGroundJustClearOfTheSpawner() {
+ List spawners = List.of(new int[] { 0, 0 });
+ List bricks = List.of(new int[] { 0, 0 }, new int[] { 0, 10 });
+ Optional spot = FortressSpot.choose(MIN, MAX, spawners, bricks, (x, z) -> 1);
+ assertTrue(spot.isPresent());
+ // The nearest chunk corners that are far enough away are 32 blocks off
+ assertEquals(FortressSpot.MIN_SPAWNER_SIZE, spot.get().spawnerSize());
+ assertEquals(0, (spot.get().x() | spot.get().z()) & 15);
+ assertTrue(spot.get().fortressSize() >= FortressSpot.MIN_FORTRESS_SIZE);
+ }
+
+ @Test
+ void testSkipsGroundThatIsNotOpen() {
+ List spawners = List.of(new int[] { 0, 0 });
+ // Only the east side is open
+ Optional spot = FortressSpot.choose(MIN, MAX, spawners, List.of(), (x, z) -> x >= 64 ? 1 : 0);
+ assertTrue(spot.isPresent());
+ assertEquals(64, spot.get().x());
+ assertEquals(64, spot.get().spawnerSize());
+ }
+
+ @Test
+ void testKeepsOutOfTheFortress() {
+ List spawners = List.of(new int[] { 0, 0 });
+ // A corridor runs east from the spawner
+ List bricks = new java.util.ArrayList<>();
+ for (int x = 0; x < 128; x++) {
+ bricks.add(new int[] { x, 0 });
+ }
+ Spot spot = FortressSpot.choose(MIN, MAX, spawners, bricks, (x, z) -> x > 0 ? 1 : 0).orElseThrow();
+ assertTrue(Math.abs(spot.z()) >= FortressSpot.MIN_FORTRESS_SIZE, spot.toString());
+ }
+
+ @Test
+ void testNothingWithoutASpawnerOrOpenGround() {
+ assertTrue(FortressSpot.choose(MIN, MAX, List.of(), List.of(), (x, z) -> 1).isEmpty());
+ assertTrue(FortressSpot.choose(MIN, MAX, List.of(new int[] { 0, 0 }), List.of(), (x, z) -> 0).isEmpty());
+ }
+}