Skip to content

Commit

Permalink
Improve control over what worlds a structure can spawn in.
Browse files Browse the repository at this point in the history
- Adds a blacklist for structures.
- Adds a global whitelist and blacklist for worlds.
  • Loading branch information
ryandw11 committed Mar 24, 2024
1 parent 354c2fe commit 308f164
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 8 deletions.
21 changes: 21 additions & 0 deletions src/main/java/com/ryandw11/structure/CustomStructures.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.bstats.charts.AdvancedPie;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
Expand Down Expand Up @@ -810,4 +811,24 @@ public MythicalMobHook getMythicalMobHook() {
public CitizensNpcHook getCitizensNpcHook() {
return citizensNpcHook;
}

/**
* Check if a structure can spawn in a world based upon the global white and black lists.
*
* <p>This does not check a structure's local properties. Use {@link com.ryandw11.structure.structure.properties.StructureLocation#canSpawnInWorld(World)}
* instead. (That method also calls this method).</p>
*
* @param world The world to check.
* @return If a structure can spawn in a world based upon the global white and black lists.
*/
public boolean canStructureSpawnInWorld(World world) {
List<String> whitelist = getConfig().getStringList("GlobalWorldWhitelist");
List<String> blacklist = getConfig().getStringList("GlobalWorldBlacklist");

if (!whitelist.isEmpty() && !whitelist.contains(world.getName()))
return false;
if (blacklist.contains(world.getName()))
return false;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,8 @@ void psuedoCalculate(Player p, Structure structure, Block bl, Chunk ch) {
}

void canSpawn(Player p, Structure structure, Block block, Chunk chunk) {
if (!structure.getStructureLocation().getWorlds().isEmpty()) {
if (!structure.getStructureLocation().getWorlds().contains(chunk.getWorld().getName()))
quickSendMessage(p, "&cFailed world test! Cannot spawn in current world!");
if (!structure.getStructureLocation().canSpawnInWorld(chunk.getWorld())) {
quickSendMessage(p, "&cFailed world test! Cannot spawn in current world!");
}

// Check to see if the structure is far enough away from spawn.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

/**
* Ignore blocks for 1.20
* <p>
* TODO Maybe use built in list defined by data packs? Or have the option
*/
// TODO: Maybe use built in list defined by data packs? Or have the option
public class IgnoreBlocks_1_20 implements IgnoreBlocks {

private final List<Material> ignoreBlocks = List.of(
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/ryandw11/structure/structure/Structure.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ public double getBaseRotation() {
*/
public boolean canSpawn(@Nullable Block block, @NotNull Chunk chunk) {
// Check to see if the structure can spawn in the current world.
if (!getStructureLocation().getWorlds().isEmpty()) {
if (!getStructureLocation().getWorlds().contains(chunk.getWorld().getName()))
return false;
if (!getStructureLocation().canSpawnInWorld(chunk.getWorld())) {
return false;
}

// If the block is null, that means it is in the void, check if it can spawn in the void.
if (block == null && !getStructureProperties().canSpawnInVoid())
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ public void save(File file) throws IOException {
config.set("Probability.Denominator", probabilityDenominator);

config.set("StructureLocation.Worlds", structureLocation.getWorlds());
config.set("StructureLocation.WorldBlacklist", structureLocation.getWorldBlacklist());
config.set("StructureLocation.SpawnY", structureLocation.getSpawnSettings().getValue());
config.set("StructureLocation.SpawnYHeightMap", structureLocation.getSpawnSettings().getHeightMap().toString());
config.set("StructureLocation.Biome", structureLocation.getBiomes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.ryandw11.structure.exceptions.StructureConfigurationException;
import com.ryandw11.structure.structure.StructureBuilder;
import org.bukkit.HeightMap;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -16,6 +18,7 @@
public class StructureLocation implements StructureProperty {

private List<String> worlds;
private List<String> worldBlacklist;
private StructureYSpawning spawnY;
private List<String> biomes;
private double distanceFromOthers;
Expand Down Expand Up @@ -49,6 +52,12 @@ public StructureLocation(FileConfiguration fileConfiguration) {
this.worlds = cs.getStringList("Worlds");
else
this.worlds = new ArrayList<>();

if (cs.contains("WorldBlacklist"))
this.worldBlacklist = cs.getStringList("WorldBlacklist");
else
this.worldBlacklist = new ArrayList<>();

this.spawnY = new StructureYSpawning(fileConfiguration);
if (cs.contains("Biome"))
this.biomes = cs.getStringList("Biome");
Expand Down Expand Up @@ -86,6 +95,7 @@ public StructureLocation(FileConfiguration fileConfiguration) {
*/
public StructureLocation(List<String> worlds, StructureYSpawning spawnSettings, List<String> biomes) {
this.worlds = worlds;
this.worldBlacklist = new ArrayList<>();
this.spawnY = spawnSettings;
this.biomes = biomes;
this.distanceFromOthers = 100;
Expand All @@ -110,6 +120,34 @@ public List<String> getWorlds() {
return worlds;
}

/**
* Get the list of blacklisted worlds.
*
* @return The list of blacklisted worlds.
*/
public List<String> getWorldBlacklist() {
return worldBlacklist;
}

/**
* Check if a structure can spawn in the provided world.
*
* @param world The world to check.
* @return If the structure can spawn in that world.
*/
public boolean canSpawnInWorld(@NotNull World world) {
if (!worlds.isEmpty()) {
if (!worlds.contains(world.getName()))
return false;
}

if (worldBlacklist.contains(world.getName())) {
return false;
}

return true;
}

/**
* Get the Y Spawn settings.
*
Expand Down Expand Up @@ -255,6 +293,7 @@ public void setDistanceFromSame(double distance) {
@Override
public void saveToFile(ConfigurationSection configurationSection) {
configurationSection.set("Worlds", worlds);
configurationSection.set("WorldBlacklist", worldBlacklist);
configurationSection.set("SpawnY", spawnY.getValue());
configurationSection.set("SpawnYHeightMap", spawnY.getHeightMap().toString());
configurationSection.set("Biome", biomes);
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ bstats: true
# Enabling this option allows developers to use the API to get the location of structures.
logStructures: false

# A global list of worlds that structures are allow to spawn in.
GlobalWorldWhitelist: []
# A global list of worlds that structures are not allowed to spawn in.
GlobalWorldBlacklist: []

Structures:
- demo

0 comments on commit 308f164

Please sign in to comment.