#1250: Standardize and centralize Bukkit / Minecraft registry conversion
By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
@@ -110,7 +110,7 @@ public class CraftAreaEffectCloud extends CraftEntity implements AreaEffectCloud
|
||||
|
||||
@Override
|
||||
public Particle getParticle() {
|
||||
return CraftParticle.toBukkit(getHandle().getParticle());
|
||||
return CraftParticle.minecraftToBukkit(getHandle().getParticle().getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package org.bukkit.craftbukkit.entity;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.core.IRegistry;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.world.entity.animal.CatVariant;
|
||||
import net.minecraft.world.entity.animal.EntityCat;
|
||||
import net.minecraft.world.item.EnumColor;
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.craftbukkit.CraftRegistry;
|
||||
import org.bukkit.craftbukkit.CraftServer;
|
||||
import org.bukkit.entity.Cat;
|
||||
import org.bukkit.entity.Cat.Type;
|
||||
|
||||
public class CraftCat extends CraftTameableAnimal implements Cat {
|
||||
|
||||
@@ -27,14 +29,14 @@ public class CraftCat extends CraftTameableAnimal implements Cat {
|
||||
|
||||
@Override
|
||||
public Type getCatType() {
|
||||
return Type.values()[BuiltInRegistries.CAT_VARIANT.getId(getHandle().getVariant())];
|
||||
return CraftType.minecraftToBukkit(getHandle().getVariant());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCatType(Type type) {
|
||||
Preconditions.checkArgument(type != null, "Cannot have null Type");
|
||||
|
||||
getHandle().setVariant(BuiltInRegistries.CAT_VARIANT.byId(type.ordinal()));
|
||||
getHandle().setVariant(CraftType.bukkitToMinecraft(type));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -46,4 +48,22 @@ public class CraftCat extends CraftTameableAnimal implements Cat {
|
||||
public void setCollarColor(DyeColor color) {
|
||||
getHandle().setCollarColor(EnumColor.byId(color.getWoolData()));
|
||||
}
|
||||
|
||||
public static class CraftType {
|
||||
|
||||
public static Type minecraftToBukkit(CatVariant minecraft) {
|
||||
Preconditions.checkArgument(minecraft != null);
|
||||
|
||||
IRegistry<CatVariant> registry = CraftRegistry.getMinecraftRegistry(Registries.CAT_VARIANT);
|
||||
|
||||
return Type.values()[registry.getId(minecraft)];
|
||||
}
|
||||
|
||||
public static CatVariant bukkitToMinecraft(Type bukkit) {
|
||||
Preconditions.checkArgument(bukkit != null);
|
||||
|
||||
return CraftRegistry.getMinecraftRegistry(Registries.CAT_VARIANT)
|
||||
.byId(bukkit.ordinal());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,8 +210,7 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
|
||||
public CraftEntity(final CraftServer server, final Entity entity) {
|
||||
this.server = server;
|
||||
this.entity = entity;
|
||||
EntityType type = Registry.ENTITY_TYPE.get(CraftNamespacedKey.fromMinecraft(EntityTypes.getKey(entity.getType())));
|
||||
this.entityType = (type != null) ? type : EntityType.UNKNOWN;
|
||||
this.entityType = CraftEntityType.minecraftToBukkit(entity.getType());
|
||||
}
|
||||
|
||||
public static CraftEntity getEntity(CraftServer server, Entity entity) {
|
||||
@@ -778,17 +777,17 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
|
||||
|
||||
@Override
|
||||
public Sound getSwimSound() {
|
||||
return CraftSound.getBukkit(getHandle().getSwimSound0());
|
||||
return CraftSound.minecraftToBukkit(getHandle().getSwimSound0());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sound getSwimSplashSound() {
|
||||
return CraftSound.getBukkit(getHandle().getSwimSplashSound0());
|
||||
return CraftSound.minecraftToBukkit(getHandle().getSwimSplashSound0());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sound getSwimHighSpeedSplashSound() {
|
||||
return CraftSound.getBukkit(getHandle().getSwimHighSpeedSplashSound0());
|
||||
return CraftSound.minecraftToBukkit(getHandle().getSwimHighSpeedSplashSound0());
|
||||
}
|
||||
|
||||
public void setHandle(final Entity entity) {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.bukkit.craftbukkit.entity;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import net.minecraft.core.IRegistry;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.world.entity.EntityTypes;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.craftbukkit.CraftRegistry;
|
||||
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
||||
import org.bukkit.entity.EntityType;
|
||||
|
||||
public class CraftEntityType {
|
||||
|
||||
public static EntityType minecraftToBukkit(EntityTypes<?> minecraft) {
|
||||
Preconditions.checkArgument(minecraft != null);
|
||||
|
||||
IRegistry<EntityTypes<?>> registry = CraftRegistry.getMinecraftRegistry(Registries.ENTITY_TYPE);
|
||||
EntityType bukkit = Registry.ENTITY_TYPE.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
|
||||
|
||||
Preconditions.checkArgument(bukkit != null);
|
||||
|
||||
return bukkit;
|
||||
}
|
||||
|
||||
public static EntityTypes<?> bukkitToMinecraft(EntityType bukkit) {
|
||||
Preconditions.checkArgument(bukkit != null);
|
||||
|
||||
return CraftRegistry.getMinecraftRegistry(Registries.ENTITY_TYPE)
|
||||
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
package org.bukkit.craftbukkit.entity;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.core.IRegistry;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.world.entity.animal.FrogVariant;
|
||||
import net.minecraft.world.entity.animal.frog.Frog;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.craftbukkit.CraftRegistry;
|
||||
import org.bukkit.craftbukkit.CraftServer;
|
||||
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
||||
import org.bukkit.entity.Entity;
|
||||
@@ -40,13 +43,34 @@ public class CraftFrog extends CraftAnimals implements org.bukkit.entity.Frog {
|
||||
|
||||
@Override
|
||||
public Variant getVariant() {
|
||||
return Registry.FROG_VARIANT.get(CraftNamespacedKey.fromMinecraft(BuiltInRegistries.FROG_VARIANT.getKey(getHandle().getVariant())));
|
||||
return CraftVariant.minecraftToBukkit(getHandle().getVariant());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVariant(Variant variant) {
|
||||
Preconditions.checkArgument(variant != null, "variant");
|
||||
|
||||
getHandle().setVariant(BuiltInRegistries.FROG_VARIANT.get(CraftNamespacedKey.toMinecraft(variant.getKey())));
|
||||
getHandle().setVariant(CraftVariant.bukkitToMinecraft(variant));
|
||||
}
|
||||
|
||||
public static class CraftVariant {
|
||||
|
||||
public static Variant minecraftToBukkit(FrogVariant minecraft) {
|
||||
Preconditions.checkArgument(minecraft != null);
|
||||
|
||||
IRegistry<FrogVariant> registry = CraftRegistry.getMinecraftRegistry(Registries.FROG_VARIANT);
|
||||
Variant bukkit = Registry.FROG_VARIANT.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
|
||||
|
||||
Preconditions.checkArgument(bukkit != null);
|
||||
|
||||
return bukkit;
|
||||
}
|
||||
|
||||
public static FrogVariant bukkitToMinecraft(Variant bukkit) {
|
||||
Preconditions.checkArgument(bukkit != null);
|
||||
|
||||
return CraftRegistry.getMinecraftRegistry(Registries.FROG_VARIANT)
|
||||
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -710,51 +710,51 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
|
||||
|
||||
@Override
|
||||
public <T> T getMemory(MemoryKey<T> memoryKey) {
|
||||
return (T) getHandle().getBrain().getMemory(CraftMemoryKey.fromMemoryKey(memoryKey)).map(CraftMemoryMapper::fromNms).orElse(null);
|
||||
return (T) getHandle().getBrain().getMemory(CraftMemoryKey.bukkitToMinecraft(memoryKey)).map(CraftMemoryMapper::fromNms).orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void setMemory(MemoryKey<T> memoryKey, T t) {
|
||||
getHandle().getBrain().setMemory(CraftMemoryKey.fromMemoryKey(memoryKey), CraftMemoryMapper.toNms(t));
|
||||
getHandle().getBrain().setMemory(CraftMemoryKey.bukkitToMinecraft(memoryKey), CraftMemoryMapper.toNms(t));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sound getHurtSound() {
|
||||
SoundEffect sound = getHandle().getHurtSound0(getHandle().damageSources().generic());
|
||||
return (sound != null) ? CraftSound.getBukkit(sound) : null;
|
||||
return (sound != null) ? CraftSound.minecraftToBukkit(sound) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sound getDeathSound() {
|
||||
SoundEffect sound = getHandle().getDeathSound0();
|
||||
return (sound != null) ? CraftSound.getBukkit(sound) : null;
|
||||
return (sound != null) ? CraftSound.minecraftToBukkit(sound) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sound getFallDamageSound(int fallHeight) {
|
||||
return CraftSound.getBukkit(getHandle().getFallDamageSound0(fallHeight));
|
||||
return CraftSound.minecraftToBukkit(getHandle().getFallDamageSound0(fallHeight));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sound getFallDamageSoundSmall() {
|
||||
return CraftSound.getBukkit(getHandle().getFallSounds().small());
|
||||
return CraftSound.minecraftToBukkit(getHandle().getFallSounds().small());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sound getFallDamageSoundBig() {
|
||||
return CraftSound.getBukkit(getHandle().getFallSounds().big());
|
||||
return CraftSound.minecraftToBukkit(getHandle().getFallSounds().big());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sound getDrinkingSound(ItemStack itemStack) {
|
||||
Preconditions.checkArgument(itemStack != null, "itemStack must not be null");
|
||||
return CraftSound.getBukkit(getHandle().getDrinkingSound0(CraftItemStack.asNMSCopy(itemStack)));
|
||||
return CraftSound.minecraftToBukkit(getHandle().getDrinkingSound0(CraftItemStack.asNMSCopy(itemStack)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sound getEatingSound(ItemStack itemStack) {
|
||||
Preconditions.checkArgument(itemStack != null, "itemStack must not be null");
|
||||
return CraftSound.getBukkit(getHandle().getEatingSound0(CraftItemStack.asNMSCopy(itemStack)));
|
||||
return CraftSound.minecraftToBukkit(getHandle().getEatingSound0(CraftItemStack.asNMSCopy(itemStack)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -50,7 +50,7 @@ public abstract class CraftMob extends CraftLivingEntity implements Mob {
|
||||
@Override
|
||||
public Sound getAmbientSound() {
|
||||
SoundEffect sound = getHandle().getAmbientSound0();
|
||||
return (sound != null) ? CraftSound.getBukkit(sound) : null;
|
||||
return (sound != null) ? CraftSound.minecraftToBukkit(sound) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,8 +17,7 @@ public class CraftPainting extends CraftHanging implements Painting {
|
||||
|
||||
@Override
|
||||
public Art getArt() {
|
||||
Holder<PaintingVariant> art = getHandle().getVariant();
|
||||
return CraftArt.NotchToBukkit(art);
|
||||
return CraftArt.minecraftHolderToBukkit(getHandle().getVariant());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -30,7 +29,7 @@ public class CraftPainting extends CraftHanging implements Painting {
|
||||
public boolean setArt(Art art, boolean force) {
|
||||
EntityPainting painting = this.getHandle();
|
||||
Holder<PaintingVariant> oldArt = painting.getVariant();
|
||||
painting.setVariant(CraftArt.BukkitToNotch(art));
|
||||
painting.setVariant(CraftArt.bukkitToMinecraftHolder(art));
|
||||
painting.setDirection(painting.getDirection());
|
||||
if (!force && !getHandle().generation && !painting.survives()) {
|
||||
// Revert painting since it doesn't fit
|
||||
|
||||
@@ -436,29 +436,29 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
||||
|
||||
if (getHandle().connection == null) return;
|
||||
|
||||
String instrumentName = switch (instrument.ordinal()) {
|
||||
case 0 -> "harp";
|
||||
case 1 -> "basedrum";
|
||||
case 2 -> "snare";
|
||||
case 3 -> "hat";
|
||||
case 4 -> "bass";
|
||||
case 5 -> "flute";
|
||||
case 6 -> "bell";
|
||||
case 7 -> "guitar";
|
||||
case 8 -> "chime";
|
||||
case 9 -> "xylophone";
|
||||
case 10 -> "iron_xylophone";
|
||||
case 11 -> "cow_bell";
|
||||
case 12 -> "didgeridoo";
|
||||
case 13 -> "bit";
|
||||
case 14 -> "banjo";
|
||||
case 15 -> "pling";
|
||||
case 16 -> "xylophone";
|
||||
Sound instrumentSound = switch (instrument.ordinal()) {
|
||||
case 0 -> Sound.BLOCK_NOTE_BLOCK_HARP;
|
||||
case 1 -> Sound.BLOCK_NOTE_BLOCK_BASEDRUM;
|
||||
case 2 -> Sound.BLOCK_NOTE_BLOCK_SNARE;
|
||||
case 3 -> Sound.BLOCK_NOTE_BLOCK_HAT;
|
||||
case 4 -> Sound.BLOCK_NOTE_BLOCK_BASS;
|
||||
case 5 -> Sound.BLOCK_NOTE_BLOCK_FLUTE;
|
||||
case 6 -> Sound.BLOCK_NOTE_BLOCK_BELL;
|
||||
case 7 -> Sound.BLOCK_NOTE_BLOCK_GUITAR;
|
||||
case 8 -> Sound.BLOCK_NOTE_BLOCK_CHIME;
|
||||
case 9 -> Sound.BLOCK_NOTE_BLOCK_XYLOPHONE;
|
||||
case 10 -> Sound.BLOCK_NOTE_BLOCK_IRON_XYLOPHONE;
|
||||
case 11 -> Sound.BLOCK_NOTE_BLOCK_COW_BELL;
|
||||
case 12 -> Sound.BLOCK_NOTE_BLOCK_DIDGERIDOO;
|
||||
case 13 -> Sound.BLOCK_NOTE_BLOCK_BIT;
|
||||
case 14 -> Sound.BLOCK_NOTE_BLOCK_BANJO;
|
||||
case 15 -> Sound.BLOCK_NOTE_BLOCK_PLING;
|
||||
case 16 -> Sound.BLOCK_NOTE_BLOCK_XYLOPHONE;
|
||||
default -> null;
|
||||
};
|
||||
|
||||
float f = (float) Math.pow(2.0D, (note.getId() - 12.0D) / 12.0D);
|
||||
getHandle().connection.send(new PacketPlayOutNamedSoundEffect(BuiltInRegistries.SOUND_EVENT.wrapAsHolder(CraftSound.getSoundEffect("block.note_block." + instrumentName)), net.minecraft.sounds.SoundCategory.RECORDS, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), 3.0f, f, getHandle().getRandom().nextLong()));
|
||||
getHandle().connection.send(new PacketPlayOutNamedSoundEffect(CraftSound.bukkitToMinecraftHolder(instrumentSound), net.minecraft.sounds.SoundCategory.RECORDS, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), 3.0f, f, getHandle().getRandom().nextLong()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -475,7 +475,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
||||
public void playSound(Location loc, Sound sound, org.bukkit.SoundCategory category, float volume, float pitch) {
|
||||
if (loc == null || sound == null || category == null || getHandle().connection == null) return;
|
||||
|
||||
playSound0(loc, BuiltInRegistries.SOUND_EVENT.wrapAsHolder(CraftSound.getSoundEffect(sound)), net.minecraft.sounds.SoundCategory.valueOf(category.name()), volume, pitch);
|
||||
playSound0(loc, CraftSound.bukkitToMinecraftHolder(sound), net.minecraft.sounds.SoundCategory.valueOf(category.name()), volume, pitch);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -508,7 +508,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
||||
public void playSound(org.bukkit.entity.Entity entity, Sound sound, org.bukkit.SoundCategory category, float volume, float pitch) {
|
||||
if (!(entity instanceof CraftEntity craftEntity) || sound == null || category == null || getHandle().connection == null) return;
|
||||
|
||||
playSound0(entity, BuiltInRegistries.SOUND_EVENT.wrapAsHolder(CraftSound.getSoundEffect(sound)), net.minecraft.sounds.SoundCategory.valueOf(category.name()), volume, pitch);
|
||||
playSound0(entity, CraftSound.bukkitToMinecraftHolder(sound), net.minecraft.sounds.SoundCategory.valueOf(category.name()), volume, pitch);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -106,6 +106,6 @@ public abstract class CraftRaider extends CraftMonster implements Raider {
|
||||
|
||||
@Override
|
||||
public Sound getCelebrationSound() {
|
||||
return CraftSound.getBukkit(getHandle().getCelebrateSound());
|
||||
return CraftSound.minecraftToBukkit(getHandle().getCelebrateSound());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
package org.bukkit.craftbukkit.entity;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import java.util.Locale;
|
||||
import net.minecraft.core.BlockPosition;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.core.IRegistry;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.world.entity.monster.EntityZombie;
|
||||
import net.minecraft.world.entity.monster.EntityZombieVillager;
|
||||
import net.minecraft.world.entity.npc.EntityVillager;
|
||||
import net.minecraft.world.entity.npc.VillagerProfession;
|
||||
import net.minecraft.world.entity.npc.VillagerType;
|
||||
import net.minecraft.world.level.block.BlockBed;
|
||||
import net.minecraft.world.level.block.state.IBlockData;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.craftbukkit.CraftRegistry;
|
||||
import org.bukkit.craftbukkit.CraftServer;
|
||||
import org.bukkit.craftbukkit.util.CraftLocation;
|
||||
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
||||
@@ -43,24 +46,24 @@ public class CraftVillager extends CraftAbstractVillager implements Villager {
|
||||
|
||||
@Override
|
||||
public Profession getProfession() {
|
||||
return CraftVillager.nmsToBukkitProfession(getHandle().getVillagerData().getProfession());
|
||||
return CraftProfession.minecraftToBukkit(getHandle().getVillagerData().getProfession());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProfession(Profession profession) {
|
||||
Preconditions.checkArgument(profession != null, "Profession cannot be null");
|
||||
getHandle().setVillagerData(getHandle().getVillagerData().setProfession(CraftVillager.bukkitToNmsProfession(profession)));
|
||||
getHandle().setVillagerData(getHandle().getVillagerData().setProfession(CraftProfession.bukkitToMinecraft(profession)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getVillagerType() {
|
||||
return Type.valueOf(BuiltInRegistries.VILLAGER_TYPE.getKey(getHandle().getVillagerData().getType()).getPath().toUpperCase(Locale.ROOT));
|
||||
return CraftType.minecraftToBukkit(getHandle().getVillagerData().getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVillagerType(Type type) {
|
||||
Preconditions.checkArgument(type != null, "Type cannot be null");
|
||||
getHandle().setVillagerData(getHandle().getVillagerData().setType(BuiltInRegistries.VILLAGER_TYPE.get(CraftNamespacedKey.toMinecraft(type.getKey()))));
|
||||
getHandle().setVillagerData(getHandle().getVillagerData().setType(CraftType.bukkitToMinecraft(type)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -123,11 +126,45 @@ public class CraftVillager extends CraftAbstractVillager implements Villager {
|
||||
return (entityzombievillager != null) ? (ZombieVillager) entityzombievillager.getBukkitEntity() : null;
|
||||
}
|
||||
|
||||
public static Profession nmsToBukkitProfession(VillagerProfession nms) {
|
||||
return Profession.valueOf(BuiltInRegistries.VILLAGER_PROFESSION.getKey(nms).getPath().toUpperCase(Locale.ROOT));
|
||||
public static class CraftType {
|
||||
|
||||
public static Type minecraftToBukkit(VillagerType minecraft) {
|
||||
Preconditions.checkArgument(minecraft != null);
|
||||
|
||||
IRegistry<VillagerType> registry = CraftRegistry.getMinecraftRegistry(Registries.VILLAGER_TYPE);
|
||||
Type bukkit = Registry.VILLAGER_TYPE.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
|
||||
|
||||
Preconditions.checkArgument(bukkit != null);
|
||||
|
||||
return bukkit;
|
||||
}
|
||||
|
||||
public static VillagerType bukkitToMinecraft(Type bukkit) {
|
||||
Preconditions.checkArgument(bukkit != null);
|
||||
|
||||
return CraftRegistry.getMinecraftRegistry(Registries.VILLAGER_TYPE)
|
||||
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
|
||||
}
|
||||
}
|
||||
|
||||
public static VillagerProfession bukkitToNmsProfession(Profession bukkit) {
|
||||
return BuiltInRegistries.VILLAGER_PROFESSION.get(CraftNamespacedKey.toMinecraft(bukkit.getKey()));
|
||||
public static class CraftProfession {
|
||||
|
||||
public static Profession minecraftToBukkit(VillagerProfession minecraft) {
|
||||
Preconditions.checkArgument(minecraft != null);
|
||||
|
||||
IRegistry<VillagerProfession> registry = CraftRegistry.getMinecraftRegistry(Registries.VILLAGER_PROFESSION);
|
||||
Profession bukkit = Registry.VILLAGER_PROFESSION.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
|
||||
|
||||
Preconditions.checkArgument(bukkit != null);
|
||||
|
||||
return bukkit;
|
||||
}
|
||||
|
||||
public static VillagerProfession bukkitToMinecraft(Profession bukkit) {
|
||||
Preconditions.checkArgument(bukkit != null);
|
||||
|
||||
return CraftRegistry.getMinecraftRegistry(Registries.VILLAGER_PROFESSION)
|
||||
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
package org.bukkit.craftbukkit.entity;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import java.util.Locale;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.resources.MinecraftKey;
|
||||
import net.minecraft.world.effect.MobEffects;
|
||||
import net.minecraft.world.entity.monster.EntityZombieVillager;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.craftbukkit.CraftServer;
|
||||
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
||||
import org.bukkit.entity.Villager;
|
||||
import org.bukkit.entity.ZombieVillager;
|
||||
|
||||
@@ -31,24 +27,24 @@ public class CraftVillagerZombie extends CraftZombie implements ZombieVillager {
|
||||
|
||||
@Override
|
||||
public Villager.Profession getVillagerProfession() {
|
||||
return Villager.Profession.valueOf(BuiltInRegistries.VILLAGER_PROFESSION.getKey(getHandle().getVillagerData().getProfession()).getPath().toUpperCase(Locale.ROOT));
|
||||
return CraftVillager.CraftProfession.minecraftToBukkit(getHandle().getVillagerData().getProfession());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVillagerProfession(Villager.Profession profession) {
|
||||
Preconditions.checkArgument(profession != null, "Villager.Profession cannot be null");
|
||||
getHandle().setVillagerData(getHandle().getVillagerData().setProfession(BuiltInRegistries.VILLAGER_PROFESSION.get(new MinecraftKey(profession.name().toLowerCase(Locale.ROOT)))));
|
||||
getHandle().setVillagerData(getHandle().getVillagerData().setProfession(CraftVillager.CraftProfession.bukkitToMinecraft(profession)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Villager.Type getVillagerType() {
|
||||
return Villager.Type.valueOf(BuiltInRegistries.VILLAGER_TYPE.getKey(getHandle().getVillagerData().getType()).getPath().toUpperCase(Locale.ROOT));
|
||||
return CraftVillager.CraftType.minecraftToBukkit(getHandle().getVillagerData().getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVillagerType(Villager.Type type) {
|
||||
Preconditions.checkArgument(type != null, "Villager.Type cannot be null");
|
||||
getHandle().setVillagerData(getHandle().getVillagerData().setType(BuiltInRegistries.VILLAGER_TYPE.get(CraftNamespacedKey.toMinecraft(type.getKey()))));
|
||||
getHandle().setVillagerData(getHandle().getVillagerData().setType(CraftVillager.CraftType.bukkitToMinecraft(type)));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
package org.bukkit.craftbukkit.entity.memory;
|
||||
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import com.google.common.base.Preconditions;
|
||||
import net.minecraft.core.IRegistry;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.craftbukkit.CraftRegistry;
|
||||
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
||||
import org.bukkit.entity.memory.MemoryKey;
|
||||
|
||||
@@ -9,11 +13,23 @@ public final class CraftMemoryKey {
|
||||
|
||||
private CraftMemoryKey() {}
|
||||
|
||||
public static <T, U> MemoryModuleType<U> fromMemoryKey(MemoryKey<T> memoryKey) {
|
||||
return (MemoryModuleType<U>) BuiltInRegistries.MEMORY_MODULE_TYPE.get(CraftNamespacedKey.toMinecraft(memoryKey.getKey()));
|
||||
public static <T, U> MemoryKey<U> minecraftToBukkit(MemoryModuleType<T> minecraft) {
|
||||
if (minecraft == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
IRegistry<MemoryModuleType<?>> registry = CraftRegistry.getMinecraftRegistry(Registries.MEMORY_MODULE_TYPE);
|
||||
MemoryKey<U> bukkit = Registry.MEMORY_MODULE_TYPE.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
|
||||
|
||||
return bukkit;
|
||||
}
|
||||
|
||||
public static <T, U> MemoryKey<U> toMemoryKey(MemoryModuleType<T> memoryModuleType) {
|
||||
return MemoryKey.getByKey(CraftNamespacedKey.fromMinecraft(BuiltInRegistries.MEMORY_MODULE_TYPE.getKey(memoryModuleType)));
|
||||
public static <T, U> MemoryModuleType<U> bukkitToMinecraft(MemoryKey<T> bukkit) {
|
||||
if (bukkit == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (MemoryModuleType<U>) CraftRegistry.getMinecraftRegistry(Registries.MEMORY_MODULE_TYPE)
|
||||
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user