#1501: Make Biome an interface

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2024-11-01 08:05:35 +11:00
parent 2960bff05e
commit 7b903b41cb
6 changed files with 105 additions and 21 deletions

View File

@@ -1,28 +1,23 @@
package org.bukkit.craftbukkit.block;
import com.google.common.base.Preconditions;
import java.util.Locale;
import net.minecraft.core.Holder;
import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.biome.BiomeBase;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.block.Biome;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
import org.jetbrains.annotations.NotNull;
public class CraftBiome {
public class CraftBiome implements Biome, Handleable<BiomeBase> {
private static int count = 0;
public static Biome minecraftToBukkit(BiomeBase minecraft) {
Preconditions.checkArgument(minecraft != null);
IRegistry<BiomeBase> registry = CraftRegistry.getMinecraftRegistry(Registries.BIOME);
Biome bukkit = Registry.BIOME.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
if (bukkit == null) {
return Biome.CUSTOM;
}
return bukkit;
return CraftRegistry.minecraftToBukkit(minecraft, Registries.BIOME, Registry.BIOME);
}
public static Biome minecraftHolderToBukkit(Holder<BiomeBase> minecraft) {
@@ -30,16 +25,15 @@ public class CraftBiome {
}
public static BiomeBase bukkitToMinecraft(Biome bukkit) {
if (bukkit == null || bukkit == Biome.CUSTOM) {
if (bukkit == Biome.CUSTOM) {
return null;
}
return CraftRegistry.getMinecraftRegistry(Registries.BIOME)
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
return CraftRegistry.bukkitToMinecraft(bukkit);
}
public static Holder<BiomeBase> bukkitToMinecraftHolder(Biome bukkit) {
if (bukkit == null || bukkit == Biome.CUSTOM) {
if (bukkit == Biome.CUSTOM) {
return null;
}
@@ -52,4 +46,75 @@ public class CraftBiome {
throw new IllegalArgumentException("No Reference holder found for " + bukkit
+ ", this can happen if a plugin creates its own biome base with out properly registering it.");
}
private final NamespacedKey key;
private final BiomeBase biomeBase;
private final String name;
private final int ordinal;
public CraftBiome(NamespacedKey key, BiomeBase biomeBase) {
this.key = key;
this.biomeBase = biomeBase;
// For backwards compatibility, minecraft values will stile return the uppercase name without the namespace,
// in case plugins use for example the name as key in a config file to receive biome specific values.
// Custom biomes will return the key with namespace. For a plugin this should look than like a new biome
// (which can always be added in new minecraft versions and the plugin should therefore handle it accordingly).
if (NamespacedKey.MINECRAFT.equals(key.getNamespace())) {
this.name = key.getKey().toUpperCase(Locale.ROOT);
} else {
this.name = key.toString();
}
this.ordinal = count++;
}
@Override
public BiomeBase getHandle() {
return biomeBase;
}
@NotNull
@Override
public NamespacedKey getKey() {
return key;
}
@Override
public int compareTo(@NotNull Biome biome) {
return ordinal - biome.ordinal();
}
@NotNull
@Override
public String name() {
return name;
}
@Override
public int ordinal() {
return ordinal;
}
@Override
public String toString() {
// For backwards compatibility
return name();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CraftBiome otherBiome)) {
return false;
}
return getKey().equals(otherBiome.getKey());
}
@Override
public int hashCode() {
return getKey().hashCode();
}
}