-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BiomeDefinition - start work on custom biomes
- Loading branch information
Showing
1 changed file
with
151 additions
and
0 deletions.
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
src/main/java/com/shanebeestudios/nms/api/world/biome/BiomeDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package com.shanebeestudios.nms.api.world.biome; | ||
|
||
import com.shanebeestudios.nms.api.util.McUtils; | ||
import com.shanebeestudios.nms.api.util.ReflectionUtils; | ||
import net.minecraft.core.Holder; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.core.RegistryAccess; | ||
import net.minecraft.core.registries.Registries; | ||
import net.minecraft.resources.ResourceKey; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.server.dedicated.DedicatedServer; | ||
import net.minecraft.world.level.biome.Biome; | ||
import net.minecraft.world.level.biome.BiomeGenerationSettings; | ||
import net.minecraft.world.level.biome.BiomeSpecialEffects; | ||
import net.minecraft.world.level.biome.BiomeSpecialEffects.GrassColorModifier; | ||
import net.minecraft.world.level.biome.MobSpawnSettings; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.Color; | ||
import org.bukkit.NamespacedKey; | ||
|
||
import java.util.IdentityHashMap; | ||
|
||
/** | ||
* Create/Register a new Biome | ||
*/ | ||
@SuppressWarnings("unused") | ||
public class BiomeDefinition { | ||
|
||
private final ResourceLocation key; | ||
private final Biome.BiomeBuilder biomeBuilder; | ||
private BiomeSpecialEffects.Builder specialEffects = null; | ||
|
||
public BiomeDefinition(NamespacedKey key) { | ||
this.key = McUtils.getResourceLocation(key); | ||
this.biomeBuilder = new Biome.BiomeBuilder(); | ||
} | ||
|
||
public BiomeDefinition temperature(float temperature) { | ||
this.biomeBuilder.temperature(temperature); | ||
return this; | ||
} | ||
|
||
public BiomeDefinition downfall(float downfall) { | ||
this.biomeBuilder.downfall(downfall); | ||
return this; | ||
} | ||
|
||
public BiomeDefinition hasPrecipitation(boolean hasPrecipitation) { | ||
this.biomeBuilder.hasPrecipitation(hasPrecipitation); | ||
return this; | ||
} | ||
|
||
public BiomeDefinition fogColor(Color fogColor) { | ||
this.checkAndCreateSpecialEffects(); | ||
this.specialEffects.fogColor(fogColor.asRGB()); | ||
return this; | ||
} | ||
|
||
public BiomeDefinition waterColor(Color waterColor) { | ||
this.checkAndCreateSpecialEffects(); | ||
this.specialEffects.waterColor(waterColor.asRGB()); | ||
return this; | ||
} | ||
|
||
public BiomeDefinition waterFogColor(Color waterFogColor) { | ||
this.checkAndCreateSpecialEffects(); | ||
this.specialEffects.waterFogColor(waterFogColor.asRGB()); | ||
return this; | ||
} | ||
|
||
public BiomeDefinition skyColor(Color skyColor) { | ||
this.checkAndCreateSpecialEffects(); | ||
this.specialEffects.skyColor(skyColor.asRGB()); | ||
return this; | ||
} | ||
|
||
public BiomeDefinition foliageColorOverride(Color foliageColor) { | ||
this.checkAndCreateSpecialEffects(); | ||
this.specialEffects.foliageColorOverride(foliageColor.asRGB()); | ||
return this; | ||
} | ||
|
||
public BiomeDefinition grassColorOverride(Color grassColor) { | ||
this.checkAndCreateSpecialEffects(); | ||
this.specialEffects.grassColorOverride(grassColor.asRGB()); | ||
return this; | ||
} | ||
|
||
public BiomeDefinition grassColorModifier(GrassModifier grassModifier) { | ||
this.checkAndCreateSpecialEffects(); | ||
this.specialEffects.grassColorModifier(grassModifier.getModifier()); | ||
return this; | ||
} | ||
|
||
private void checkAndCreateSpecialEffects() { | ||
if (this.specialEffects == null) { | ||
this.specialEffects = new BiomeSpecialEffects.Builder(); | ||
} | ||
|
||
} | ||
|
||
public void register() { | ||
registerAndReturn(); | ||
} | ||
|
||
@SuppressWarnings({"UnusedReturnValue", "ReplaceNullCheck"}) | ||
public Biome registerAndReturn() { | ||
if (this.specialEffects != null) { | ||
this.biomeBuilder.specialEffects(this.specialEffects.build()); | ||
} else { | ||
// Match from Plains | ||
this.biomeBuilder.specialEffects((new BiomeSpecialEffects.Builder()) | ||
.fogColor(12638463) | ||
.skyColor(7907327) | ||
.waterColor(4159204) | ||
.waterFogColor(329011) | ||
.build()); | ||
} | ||
|
||
this.biomeBuilder.generationSettings((new BiomeGenerationSettings.PlainBuilder()).build()); | ||
this.biomeBuilder.mobSpawnSettings((new MobSpawnSettings.Builder()).build()); | ||
Biome biome = this.biomeBuilder.build(); | ||
DedicatedServer minecraftServer = McUtils.getMinecraftServer(Bukkit.getServer()); | ||
RegistryAccess.Frozen registryAccess = minecraftServer.registryAccess(); | ||
Registry<Biome> biomeRegistry = registryAccess.registry(Registries.BIOME).orElseThrow(); | ||
ReflectionUtils.setField("frozen", biomeRegistry, false); | ||
ReflectionUtils.setField("unregisteredIntrusiveHolders", biomeRegistry, new IdentityHashMap<>()); | ||
Holder.Reference<Biome> holder = biomeRegistry.createIntrusiveHolder(biome); | ||
ResourceKey<Biome> resourceKey = ResourceKey.create(Registries.BIOME, this.key); | ||
Registry.register(biomeRegistry, resourceKey, (Biome)holder.value()); | ||
biomeRegistry.freeze(); | ||
return biome; | ||
} | ||
|
||
public static enum GrassModifier { | ||
NONE(GrassColorModifier.NONE), | ||
DARK_FOREST(GrassColorModifier.DARK_FOREST), | ||
SWAMP(GrassColorModifier.SWAMP); | ||
|
||
private final GrassColorModifier modifier; | ||
|
||
private GrassModifier(GrassColorModifier modifier) { | ||
this.modifier = modifier; | ||
} | ||
|
||
public GrassColorModifier getModifier() { | ||
return this.modifier; | ||
} | ||
} | ||
|
||
} |