Skip to content

Instantly share code, notes, and snippets.

@Deamon5550
Created September 10, 2016 02:05
Show Gist options
  • Save Deamon5550/9de5133a60958c81b447ca6fdb6275b0 to your computer and use it in GitHub Desktop.
Save Deamon5550/9de5133a60958c81b447ca6fdb6275b0 to your computer and use it in GitHub Desktop.
Sponge Virtual biomes test
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.biomes;
import com.flowpowered.math.vector.Vector2i;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.world.biome.BiomeGenerationSettings;
import org.spongepowered.api.world.biome.BiomeType;
import org.spongepowered.api.world.biome.BiomeTypes;
import org.spongepowered.api.world.biome.VirtualBiomeType;
import org.spongepowered.api.world.extent.MutableBiomeArea;
import org.spongepowered.api.world.gen.BiomeGenerator;
import org.spongepowered.api.world.gen.WorldGenerator;
import org.spongepowered.api.world.gen.WorldGeneratorModifier;
import org.spongepowered.api.world.storage.WorldProperties;
@Plugin(id = "custombiomes_test")
public class CustomBiomes implements WorldGeneratorModifier {
private BiomeType custom;
@Listener
public void onPreInit(GamePreInitializationEvent event) {
this.custom = VirtualBiomeType.builder().name("Custom biome").persistedBiome(BiomeTypes.DESERT).build("custombiomes_test:custom");
Sponge.getRegistry().register(BiomeType.class, this.custom);
System.out.println(this.custom);
System.out.println(Sponge.getRegistry().getType(BiomeType.class, "custombiomes_test:custom").orElse(null));
Sponge.getRegistry().register(WorldGeneratorModifier.class, this);
}
@Override
public String getId() {
return "custombiomes_test:gen";
}
@Override
public String getName() {
return "custombiomes_test generator";
}
@Override
public void modifyWorldGenerator(WorldProperties world, DataContainer container, WorldGenerator worldGenerator) {
System.out.println("Applying custom biome generator");
BiomeGen gen = new BiomeGen(worldGenerator.getBiomeGenerator(), this.custom);
worldGenerator.setBiomeGenerator(gen);
BiomeGenerationSettings settings = worldGenerator.getBiomeSettings(this.custom);
settings.getGenerationPopulators().clear();
settings.getPopulators().clear();
settings.getGroundCoverLayers().clear();
}
public static class BiomeGen implements BiomeGenerator {
private final BiomeGenerator wrapped;
private final BiomeType virtual;
public BiomeGen(BiomeGenerator wrapped, BiomeType virtual) {
this.wrapped = wrapped;
this.virtual = virtual;
}
@Override
public void generateBiomes(MutableBiomeArea buffer) {
Vector2i chunkMin = buffer.getBiomeMin().div(16);
if (Math.abs(chunkMin.getX()) < 4 && Math.abs(chunkMin.getY()) < 4) {
System.out.println("Setting " + chunkMin + " to custom biome");
buffer.getBiomeWorker().fill((x, y) -> this.virtual);
} else if (Math.abs(chunkMin.getX()) == 4 || Math.abs(chunkMin.getY()) == 4) {
buffer.getBiomeWorker().fill((x, y) -> BiomeTypes.DESERT);
} else {
this.wrapped.generateBiomes(buffer);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment