Skip to content

Commit

Permalink
Update to 1.19.2 with new custom world generator (#44)
Browse files Browse the repository at this point in the history
* Baseline world gen. Random.

* Work in progress - noise gen needs fixing.

* WIP

* WIP - redesign to use new world gen API

Work still to do - handle island deletion (may not be supported) and
test with multiple players. Clean up config.

* Bug fixes

Added better handling of aquatic elements. Avoid informing players of
revoked achievements if the score is zero. Added the mangrove boat
recipe to be ignored.

* Add default score of unknown recipes

* Fixed some biomes

* Now with correct math for repeating islands

* Java 17 workflow for Github

* Put back what was accidentally removed.

* Code cleanup. Remove debug.

* Double for int calcs

* More code cleanup and using new switch syntax
  • Loading branch information
tastybento committed Nov 27, 2022
1 parent 8b8dcfa commit 29121d6
Show file tree
Hide file tree
Showing 20 changed files with 575 additions and 701 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 16
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 16
java-version: 17
- name: Cache SonarCloud packages
uses: actions/cache@v1
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/pom.xml.versionsBackup
/bin/
/boxed.iml
/.idea/
17 changes: 5 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>16</java.version>
<java.version>17</java.version>
<!-- Non-minecraft related dependencies -->
<powermock.version>2.0.9</powermock.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.17-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.18.0-SNAPSHOT</bentobox.version>
<spigot.version>1.19.2-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.22.0-SNAPSHOT</bentobox.version>
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
<!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number>
<!-- This allows to change between versions. -->
<build.version>1.2.2</build.version>
<build.version>2.0.2</build.version>

<sonar.projectKey>BentoBoxWorld_Boxed</sonar.projectKey>
<sonar.organization>bentobox-world</sonar.organization>
Expand Down Expand Up @@ -141,7 +141,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.11.1</version>
<version>4.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -162,13 +162,6 @@
<version>${bentobox.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>nl.rutgerkok</groupId>
<artifactId>worldgeneratorapi</artifactId>
<version>1.2.0</version>
<scope>provided</scope> <!-- Important, otherwise WorldGeneratorApi will be placed in your JAR file
if you're using maven-shade-plugin -->
</dependency>
</dependencies>

<build>
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/world/bentobox/boxed/AdvancementsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class AdvancementsManager {
private final Map<String, IslandAdvancements> cache;
private final YamlConfiguration advConfig;
private int unknownAdvChange;
private int unknownRecipeChange;

/**
* @param addon addon
Expand All @@ -57,6 +58,7 @@ public AdvancementsManager(Boxed addon) {
try {
advConfig.load(advFile);
unknownAdvChange = advConfig.getInt("settings.unknown-advancement-increase", 0);
unknownRecipeChange = advConfig.getInt("settings.unknown-recipe-increase", 0);
} catch (IOException | InvalidConfigurationException e) {
addon.logError("advancements.yml cannot be found! " + e.getLocalizedMessage());
}
Expand Down Expand Up @@ -212,10 +214,25 @@ private void setProtectionSize(@NonNull Island island, int newSize, @Nullable UU

}

private int getScore(String string) {
/**
* Get the score for this advancement
* @param string - advancement key as stored in the config file
* @return score of advancement, or default values if the key is not in the file
*/
public int getScore(String string) {
String adv = "advancements." + string;
// Check score of advancement
return !advConfig.contains(adv) && adv.endsWith("/root") ? advConfig.getInt("settings.default-root-increase") : advConfig.getInt(adv, this.unknownAdvChange);
if (advConfig.contains(adv)) {
return advConfig.getInt(adv, this.unknownAdvChange);
}
// Unknowns
if (adv.endsWith("/root")) {
return advConfig.getInt("settings.default-root-increase");
}
if (adv.contains("minecraft:recipes")) {
return this.unknownRecipeChange;
}
return this.unknownAdvChange;
}

}
132 changes: 81 additions & 51 deletions src/main/java/world/bentobox/boxed/Boxed.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import java.util.Collections;

import org.bukkit.Bukkit;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Difficulty;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.WorldCreator;
import org.bukkit.WorldType;
import org.bukkit.entity.SpawnCategory;
import org.bukkit.generator.BiomeProvider;
import org.bukkit.generator.ChunkGenerator;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -21,13 +22,14 @@
import world.bentobox.bentobox.api.flags.Flag.Mode;
import world.bentobox.bentobox.api.flags.Flag.Type;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.boxed.generators.BoxedBiomeGenerator;
import world.bentobox.boxed.generators.BoxedChunkGenerator;
import world.bentobox.boxed.generators.DeleteGen;
import world.bentobox.boxed.generators.BoxedSeedChunkGenerator;
import world.bentobox.boxed.listeners.AdvancementListener;
import world.bentobox.boxed.listeners.EnderPearlListener;

/**
* Main Boxed class - provides an survival game inside a box
* Main Boxed class - provides a survival game inside a box
* @author tastybento
*/
public class Boxed extends GameModeAddon {
Expand All @@ -48,41 +50,27 @@ public class Boxed extends GameModeAddon {

// Settings
private Settings settings;
private ChunkGenerator chunkGenerator;
private BoxedChunkGenerator chunkGenerator;
private final Config<Settings> configObject = new Config<>(this, Settings.class);
private AdvancementsManager advManager;
private DeleteGen delChunks;
private ChunkGenerator netherChunkGenerator;
private World seedWorld;
private BiomeProvider boxedBiomeProvider;

@Override
public void onLoad() {
// Save the default config from config.yml
saveDefaultConfig();
// Load settings from config.yml. This will check if there are any issues with it too.
loadSettings();
// Save biomes
this.saveResource("biomes.yml", false);
// Check for WGAPI
if (isNoWGAPI()) {
logError("WorldGeneratorAPI plugin is required.");
logError("Download the correct one for your server from https://github.com/rutgerkok/WorldGeneratorApi/releases");
this.setState(State.DISABLED);
return;
}
// Chunk generator
chunkGenerator = new BoxedChunkGenerator(this).getGenerator();
netherChunkGenerator = new BoxedChunkGenerator(this).getNetherGenerator();

// Register commands
playerCommand = new DefaultPlayerCommand(this) {};

adminCommand = new DefaultAdminCommand(this) {};

}

private boolean isNoWGAPI() {
return Bukkit.getPluginManager().getPlugin("WorldGeneratorApi") == null;
}

private boolean loadSettings() {
// Load settings again to get worlds
settings = configObject.loadConfigObject();
Expand All @@ -92,16 +80,13 @@ private boolean loadSettings() {
setState(State.DISABLED);
return false;
}
// Initialize the Generator because createWorlds will be run after onLoad
this.chunkGenerator = new BoxedChunkGenerator(this);
return true;
}

@Override
public void onEnable(){
// Disable in onEnable
if (isNoWGAPI()) {
this.setState(State.DISABLED);
return;
}
public void onEnable() {
// Check for recommended addons
if (this.getPlugin().getAddonsManager().getAddonByName("Border").isEmpty()) {
this.logWarning("Boxed normally requires the Border addon.");
Expand All @@ -111,8 +96,6 @@ public void onEnable(){
}
// Advancements manager
advManager = new AdvancementsManager(this);
// Get delete chunk generator
delChunks = new DeleteGen(this);
// Make flags only applicable to this game mode
MOVE_BOX.setGameModes(Collections.singleton(this));
ALLOW_MOVE_BOX.setGameModes(Collections.singleton(this));
Expand All @@ -127,6 +110,7 @@ public void onEnable(){
// Register listeners
this.registerListener(new AdvancementListener(this));
this.registerListener(new EnderPearlListener(this));
//this.registerListener(new DebugListener(this));

// Register placeholders
PlaceholdersManager phManager = new PlaceholdersManager(this);
Expand Down Expand Up @@ -157,47 +141,89 @@ public Settings getSettings() {

@Override
public void createWorlds() {
if (isNoWGAPI()) {
return;
}
// Create seed world
log("Creating Boxed Seed world ...");
seedWorld = WorldCreator
.name("seed")
.generator(new BoxedSeedChunkGenerator())
.environment(Environment.NORMAL)
.generateStructures(false)
.seed(getSettings().getSeed())
.createWorld();
seedWorld.setDifficulty(Difficulty.PEACEFUL); // No damage wanted in this world.
saveChunks(seedWorld);

String worldName = settings.getWorldName().toLowerCase();

if (getServer().getWorld(worldName) == null) {
log("Creating Boxed world ...");
}

// Create the world if it does not exist
islandWorld = getWorld(worldName, World.Environment.NORMAL, chunkGenerator);
islandWorld = getWorld(worldName, World.Environment.NORMAL);
/*
// Make the nether if it does not exist
if (settings.isNetherGenerate()) {
if (getServer().getWorld(worldName + NETHER) == null) {
log("Creating Boxed's Nether...");
}
netherWorld = settings.isNetherIslands() ? getWorld(worldName, World.Environment.NETHER, netherChunkGenerator) : getWorld(worldName, World.Environment.NETHER, null);
netherWorld = settings.isNetherIslands() ? getWorld(worldName, World.Environment.NETHER) : getWorld(worldName, World.Environment.NETHER);
}
// Make the end if it does not exist
if (settings.isEndGenerate()) {
if (getServer().getWorld(worldName + THE_END) == null) {
log("Creating Boxed's End World...");
}
endWorld = settings.isEndIslands() ? getWorld(worldName, World.Environment.THE_END, chunkGenerator) : getWorld(worldName, World.Environment.THE_END, null);
endWorld = settings.isEndIslands() ? getWorld(worldName, World.Environment.THE_END) : getWorld(worldName, World.Environment.THE_END);
}
*/
}

private void saveChunks(World seedWorld) {
// Convert to chunks
int size = (int)(this.getSettings().getIslandDistance() / 16D);
double percent = size * 4D * size;
int count = 0;
int last = 0;
for (int x = -size; x < size; x ++) {
for (int z = -size; z < size; z++) {
ChunkSnapshot chunk = seedWorld.getChunkAt(x, z).getChunkSnapshot(true, true, false);
this.chunkGenerator.setChunk(chunk);
count++;
int p = (int) (count / percent * 100);
if (p % 10 == 0 && p != last) {
last = p;
this.log("Storing seed chunks. " + p + "% done");
}

}
}
}

/**
* @return the chunkGenerator
*/
public BoxedChunkGenerator getChunkGenerator() {
return chunkGenerator;
}

/**
* Gets a world or generates a new world if it does not exist
* @param worldName2 - the overworld name
* @param env - the environment
* @param chunkGenerator2 - the chunk generator. If <tt>null</tt> then the generator will not be specified
* @return world loaded or generated
*/
private World getWorld(String worldName2, Environment env, ChunkGenerator chunkGenerator2) {
private World getWorld(String worldName2, Environment env) {
// Set world name
worldName2 = env.equals(World.Environment.NETHER) ? worldName2 + NETHER : worldName2;
worldName2 = env.equals(World.Environment.THE_END) ? worldName2 + THE_END : worldName2;
World w = WorldCreator.name(worldName2).type(WorldType.FLAT).environment(env).generator(chunkGenerator2).seed(settings.getSeed()).createWorld();
// Backup world
World b = WorldCreator.name(worldName2 + "_bak").type(WorldType.FLAT).environment(env).generator(chunkGenerator2).seed(settings.getSeed()).createWorld();
b.setDifficulty(Difficulty.PEACEFUL); // No damage wanted in this world.
boxedBiomeProvider = new BoxedBiomeGenerator(this);
World w = WorldCreator
.name(worldName2)
.generator(chunkGenerator)
.environment(env)
.seed(seedWorld.getSeed()) // For development
.createWorld();
// Set spawn rates
if (w != null) {
setSpawnRates(w);
Expand All @@ -206,24 +232,31 @@ private World getWorld(String worldName2, Environment env, ChunkGenerator chunkG

}

/**
* @return the boxedBiomeProvider
*/
public BiomeProvider getBoxedBiomeProvider() {
return boxedBiomeProvider;
}

private void setSpawnRates(World w) {
if (getSettings().getSpawnLimitMonsters() > 0) {
w.setMonsterSpawnLimit(getSettings().getSpawnLimitMonsters());
w.setSpawnLimit(SpawnCategory.MONSTER, getSettings().getSpawnLimitMonsters());
}
if (getSettings().getSpawnLimitAmbient() > 0) {
w.setAmbientSpawnLimit(getSettings().getSpawnLimitAmbient());
w.setSpawnLimit(SpawnCategory.AMBIENT, getSettings().getSpawnLimitAmbient());
}
if (getSettings().getSpawnLimitAnimals() > 0) {
w.setAnimalSpawnLimit(getSettings().getSpawnLimitAnimals());
w.setSpawnLimit(SpawnCategory.ANIMAL, getSettings().getSpawnLimitAnimals());
}
if (getSettings().getSpawnLimitWaterAnimals() > 0) {
w.setWaterAnimalSpawnLimit(getSettings().getSpawnLimitWaterAnimals());
w.setSpawnLimit(SpawnCategory.WATER_ANIMAL, getSettings().getSpawnLimitWaterAnimals());
}
if (getSettings().getTicksPerAnimalSpawns() > 0) {
w.setTicksPerAnimalSpawns(getSettings().getTicksPerAnimalSpawns());
w.setTicksPerSpawns(SpawnCategory.ANIMAL, getSettings().getTicksPerAnimalSpawns());
}
if (getSettings().getTicksPerMonsterSpawns() > 0) {
w.setTicksPerMonsterSpawns(getSettings().getTicksPerMonsterSpawns());
w.setTicksPerSpawns(SpawnCategory.MONSTER, getSettings().getTicksPerMonsterSpawns());
}
}

Expand All @@ -234,9 +267,6 @@ public WorldSettings getWorldSettings() {

@Override
public @Nullable ChunkGenerator getDefaultWorldGenerator(String worldName, String id) {
if (id != null && id.equals("delete")) {
return delChunks;
}
return worldName.endsWith(NETHER) ? netherChunkGenerator : chunkGenerator;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/world/bentobox/boxed/BoxedPladdon.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class BoxedPladdon extends Pladdon {
public Addon getAddon() {
return new Boxed();
}

}

0 comments on commit 29121d6

Please sign in to comment.