141 lines
4.8 KiB
Java
141 lines
4.8 KiB
Java
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.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.level.block.BlockBed;
|
|
import net.minecraft.world.level.block.state.IBlockData;
|
|
import org.apache.commons.lang.Validate;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.craftbukkit.CraftServer;
|
|
import org.bukkit.craftbukkit.util.CraftLocation;
|
|
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
|
import org.bukkit.entity.EntityType;
|
|
import org.bukkit.entity.Villager;
|
|
import org.bukkit.entity.ZombieVillager;
|
|
import org.bukkit.event.entity.CreatureSpawnEvent;
|
|
|
|
public class CraftVillager extends CraftAbstractVillager implements Villager {
|
|
|
|
public CraftVillager(CraftServer server, EntityVillager entity) {
|
|
super(server, entity);
|
|
}
|
|
|
|
@Override
|
|
public EntityVillager getHandle() {
|
|
return (EntityVillager) entity;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "CraftVillager";
|
|
}
|
|
|
|
@Override
|
|
public EntityType getType() {
|
|
return EntityType.VILLAGER;
|
|
}
|
|
|
|
@Override
|
|
public void remove() {
|
|
getHandle().releaseAllPois();
|
|
|
|
super.remove();
|
|
}
|
|
|
|
@Override
|
|
public Profession getProfession() {
|
|
return CraftVillager.nmsToBukkitProfession(getHandle().getVillagerData().getProfession());
|
|
}
|
|
|
|
@Override
|
|
public void setProfession(Profession profession) {
|
|
Validate.notNull(profession);
|
|
getHandle().setVillagerData(getHandle().getVillagerData().setProfession(CraftVillager.bukkitToNmsProfession(profession)));
|
|
}
|
|
|
|
@Override
|
|
public Type getVillagerType() {
|
|
return Type.valueOf(BuiltInRegistries.VILLAGER_TYPE.getKey(getHandle().getVillagerData().getType()).getPath().toUpperCase(Locale.ROOT));
|
|
}
|
|
|
|
@Override
|
|
public void setVillagerType(Type type) {
|
|
Validate.notNull(type);
|
|
getHandle().setVillagerData(getHandle().getVillagerData().setType(BuiltInRegistries.VILLAGER_TYPE.get(CraftNamespacedKey.toMinecraft(type.getKey()))));
|
|
}
|
|
|
|
@Override
|
|
public int getVillagerLevel() {
|
|
return getHandle().getVillagerData().getLevel();
|
|
}
|
|
|
|
@Override
|
|
public void setVillagerLevel(int level) {
|
|
Preconditions.checkArgument(1 <= level && level <= 5, "level must be between [1, 5]");
|
|
|
|
getHandle().setVillagerData(getHandle().getVillagerData().setLevel(level));
|
|
}
|
|
|
|
@Override
|
|
public int getVillagerExperience() {
|
|
return getHandle().getVillagerXp();
|
|
}
|
|
|
|
@Override
|
|
public void setVillagerExperience(int experience) {
|
|
Preconditions.checkArgument(experience >= 0, "Experience must be positive");
|
|
|
|
getHandle().setVillagerXp(experience);
|
|
}
|
|
|
|
@Override
|
|
public boolean sleep(Location location) {
|
|
Preconditions.checkArgument(location != null, "Location cannot be null");
|
|
Preconditions.checkArgument(location.getWorld() != null, "Location needs to be in a world");
|
|
Preconditions.checkArgument(location.getWorld().equals(getWorld()), "Cannot sleep across worlds");
|
|
Preconditions.checkState(!getHandle().generation, "Cannot sleep during world generation");
|
|
|
|
BlockPosition position = CraftLocation.toBlockPosition(location);
|
|
IBlockData iblockdata = getHandle().level.getBlockState(position);
|
|
if (!(iblockdata.getBlock() instanceof BlockBed)) {
|
|
return false;
|
|
}
|
|
|
|
getHandle().startSleeping(position);
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void wakeup() {
|
|
Preconditions.checkState(isSleeping(), "Cannot wakeup if not sleeping");
|
|
Preconditions.checkState(!getHandle().generation, "Cannot wakeup during world generation");
|
|
|
|
getHandle().stopSleeping();
|
|
}
|
|
|
|
@Override
|
|
public void shakeHead() {
|
|
getHandle().setUnhappy();
|
|
}
|
|
|
|
@Override
|
|
public ZombieVillager zombify() {
|
|
EntityZombieVillager entityzombievillager = EntityZombie.zombifyVillager(getHandle().level.getMinecraftWorld(), getHandle(), getHandle().blockPosition(), isSilent(), CreatureSpawnEvent.SpawnReason.CUSTOM);
|
|
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 VillagerProfession bukkitToNmsProfession(Profession bukkit) {
|
|
return BuiltInRegistries.VILLAGER_PROFESSION.get(CraftNamespacedKey.toMinecraft(bukkit.getKey()));
|
|
}
|
|
}
|