SPIGOT-5180: Add Villager#sleep() and #wakeup() methods

This commit is contained in:
LelouBil 2019-07-22 00:09:06 +02:00 committed by md_5
parent c03b2befb2
commit 1215188ff7
2 changed files with 29 additions and 1 deletions

View File

@ -173,7 +173,8 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity {
@Override
public boolean sleep(Location location, boolean force) {
Preconditions.checkArgument(location != null, "Location == null");
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");
BlockPosition blockposition = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ());

View File

@ -5,7 +5,11 @@ import java.util.Locale;
import net.minecraft.server.EntityVillager;
import net.minecraft.server.IRegistry;
import net.minecraft.server.VillagerProfession;
import net.minecraft.server.IBlockData;
import net.minecraft.server.BlockPosition;
import net.minecraft.server.BlockBed;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.entity.EntityType;
@ -78,6 +82,29 @@ public class CraftVillager extends CraftAbstractVillager implements Villager {
getHandle().setExperience(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");
BlockPosition position = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ());
IBlockData iblockdata = getHandle().world.getType(position);
if (!(iblockdata.getBlock() instanceof BlockBed)) {
return false;
}
getHandle().e(position); // PAIL rename sleep
return true;
}
@Override
public void wakeup() {
Preconditions.checkState(isSleeping(), "Cannot wakeup if not sleeping");
getHandle().dy(); // PAIL rename wakeup
}
public static Profession nmsToBukkitProfession(VillagerProfession nms) {
return Profession.valueOf(IRegistry.VILLAGER_PROFESSION.getKey(nms).getKey().toUpperCase(Locale.ROOT));
}