#1250: Standardize and centralize Bukkit / Minecraft registry conversion

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2023-09-22 03:02:16 +10:00
parent 0b9699cc2e
commit 4248b8a4d8
47 changed files with 468 additions and 252 deletions

View File

@@ -0,0 +1,38 @@
package org.bukkit.craftbukkit.attribute;
import com.google.common.base.Preconditions;
import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.entity.ai.attributes.AttributeBase;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.attribute.Attribute;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
public class CraftAttribute {
public static Attribute minecraftToBukkit(AttributeBase minecraft) {
Preconditions.checkArgument(minecraft != null);
IRegistry<AttributeBase> registry = CraftRegistry.getMinecraftRegistry(Registries.ATTRIBUTE);
Attribute bukkit = Registry.ATTRIBUTE.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
Preconditions.checkArgument(bukkit != null);
return bukkit;
}
public static Attribute stringToBukkit(String bukkit) {
Preconditions.checkArgument(bukkit != null);
return Registry.ATTRIBUTE.get(NamespacedKey.fromString(bukkit));
}
public static AttributeBase bukkitToMinecraft(Attribute bukkit) {
Preconditions.checkArgument(bukkit != null);
return CraftRegistry.getMinecraftRegistry(Registries.ATTRIBUTE)
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
}
}

View File

@@ -1,14 +1,10 @@
package org.bukkit.craftbukkit.attribute;
import com.google.common.base.Preconditions;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.entity.ai.attributes.AttributeBase;
import net.minecraft.world.entity.ai.attributes.AttributeMapBase;
import org.bukkit.Registry;
import org.bukkit.attribute.Attributable;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
public class CraftAttributeMap implements Attributable {
@@ -21,16 +17,8 @@ public class CraftAttributeMap implements Attributable {
@Override
public AttributeInstance getAttribute(Attribute attribute) {
Preconditions.checkArgument(attribute != null, "attribute");
net.minecraft.world.entity.ai.attributes.AttributeModifiable nms = handle.getInstance(toMinecraft(attribute));
net.minecraft.world.entity.ai.attributes.AttributeModifiable nms = handle.getInstance(CraftAttribute.bukkitToMinecraft(attribute));
return (nms == null) ? null : new CraftAttributeInstance(nms, attribute);
}
public static AttributeBase toMinecraft(Attribute attribute) {
return BuiltInRegistries.ATTRIBUTE.get(CraftNamespacedKey.toMinecraft(attribute.getKey()));
}
public static Attribute fromMinecraft(String nms) {
return Registry.ATTRIBUTE.get(CraftNamespacedKey.fromString(nms));
}
}