#1499: Make Fluid an interface and add missing entry

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2024-10-29 06:43:20 +11:00
parent 08c83835f3
commit af6f2c9b41
5 changed files with 91 additions and 16 deletions

View File

@@ -1,30 +1,94 @@
package org.bukkit.craftbukkit;
import com.google.common.base.Preconditions;
import net.minecraft.core.IRegistry;
import java.util.Locale;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.material.FluidType;
import org.bukkit.Fluid;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
import org.jetbrains.annotations.NotNull;
public class CraftFluid {
public class CraftFluid implements Fluid, Handleable<FluidType> {
private static int count = 0;
public static Fluid minecraftToBukkit(FluidType minecraft) {
Preconditions.checkArgument(minecraft != null);
IRegistry<FluidType> registry = CraftRegistry.getMinecraftRegistry(Registries.FLUID);
Fluid bukkit = Registry.FLUID.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
Preconditions.checkArgument(bukkit != null);
return bukkit;
return CraftRegistry.minecraftToBukkit(minecraft, Registries.FLUID, Registry.FLUID);
}
public static FluidType bukkitToMinecraft(Fluid bukkit) {
Preconditions.checkArgument(bukkit != null);
return CraftRegistry.bukkitToMinecraft(bukkit);
}
return CraftRegistry.getMinecraftRegistry(Registries.FLUID)
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
private final NamespacedKey key;
private final FluidType fluidType;
private final String name;
private final int ordinal;
public CraftFluid(NamespacedKey key, FluidType fluidType) {
this.key = key;
this.fluidType = fluidType;
// 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 fluid specific values.
// Custom fluids will return the key with namespace. For a plugin this should look than like a new fluid
// (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 FluidType getHandle() {
return fluidType;
}
@NotNull
@Override
public NamespacedKey getKey() {
return key;
}
@Override
public int compareTo(@NotNull Fluid fluid) {
return ordinal - fluid.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 CraftFluid otherFluid)) {
return false;
}
return getKey().equals(otherFluid.getKey());
}
@Override
public int hashCode() {
return getKey().hashCode();
}
}