下面列出了怎么用org.bukkit.craftbukkit.CraftWorld的API类实例代码及写法,或者点击链接到github查看源代码。
public boolean teleport(Location location, TeleportCause cause) {
Preconditions.checkArgument(location != null, "location");
location.checkFinite();
if (entity.isBeingRidden() || entity.isDead) {
return false;
}
// If this entity is riding another entity, we must dismount before teleporting.
entity.dismountRidingEntity();
entity.world = ((CraftWorld) location.getWorld()).getHandle();
// entity.setLocation() throws no event, and so cannot be cancelled
entity.setPositionAndRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
// SPIGOT-619: Force sync head rotation also
entity.setRotationYawHead(location.getYaw());
return true;
}
@Override
public boolean update(boolean force, boolean applyPhysics) {
boolean result = super.update(force, applyPhysics);
if (result && this.isPlaced() && this.getType() == Material.JUKEBOX) {
CraftWorld world = (CraftWorld) this.getWorld();
Material record = this.getPlaying();
if (record == Material.AIR) {
world.getHandle().setBlockState(new BlockPos(this.getX(), this.getY(), this.getZ()),
Blocks.JUKEBOX.getDefaultState()
.withProperty(BlockJukebox.HAS_RECORD, false), 3);
} else {
world.getHandle().setBlockState(new BlockPos(this.getX(), this.getY(), this.getZ()),
Blocks.JUKEBOX.getDefaultState()
.withProperty(BlockJukebox.HAS_RECORD, true), 3);
}
world.playEffect(this.getLocation(), Effect.RECORD_PLAY, record.getId());
}
return result;
}
public CraftBlockState(final Block block) {
this.world = (CraftWorld) block.getWorld();
this.x = block.getX();
this.y = block.getY();
this.z = block.getZ();
this.type = block.getTypeId();
this.chunk = (CraftChunk) block.getChunk();
this.flag = 3;
createData(block.getData());
TileEntity te = world.getHandle().getTileEntity(new BlockPos(this.x, this.y, this.z));
if (te != null) {
nbt = new NBTTagCompound();
te.writeToNBT(nbt);
} else nbt = null;
}
public static BlockPlaceEvent callBlockPlaceEvent(World world, EntityPlayer who, EnumHand hand, BlockState replacedBlockState, int clickedX, int clickedY, int clickedZ) {
CraftWorld craftWorld = world.getWorld();
CraftServer craftServer = world.getServer();
Player player = (Player) who.getBukkitEntity();
Block blockClicked = craftWorld.getBlockAt(clickedX, clickedY, clickedZ);
Block placedBlock = replacedBlockState.getBlock();
boolean canBuild = canBuild(craftWorld, player, placedBlock.getX(), placedBlock.getZ());
org.bukkit.inventory.ItemStack item;
EquipmentSlot equipmentSlot;
if (hand == EnumHand.MAIN_HAND) {
item = player.getInventory().getItemInMainHand();
equipmentSlot = EquipmentSlot.HAND;
} else {
item = player.getInventory().getItemInOffHand();
equipmentSlot = EquipmentSlot.OFF_HAND;
}
BlockPlaceEvent event = new BlockPlaceEvent(placedBlock, replacedBlockState, blockClicked, item, player, canBuild, equipmentSlot);
craftServer.getPluginManager().callEvent(event);
return event;
}
private static PlayerEvent getPlayerBucketEvent(boolean isFilling, EntityPlayer who, int clickedX, int clickedY, int clickedZ, EnumFacing clickedFace, ItemStack itemstack, net.minecraft.item.Item item) {
Player player = (who == null) ? null : (Player) who.getBukkitEntity();
CraftItemStack itemInHand = CraftItemStack.asNewCraftStack(item);
Material bucket = CraftMagicNumbers.getMaterial(itemstack.getItem());
CraftWorld craftWorld = (CraftWorld) player.getWorld();
CraftServer craftServer = (CraftServer) player.getServer();
Block blockClicked = craftWorld.getBlockAt(clickedX, clickedY, clickedZ);
BlockFace blockFace = CraftBlock.notchToBlockFace(clickedFace);
PlayerEvent event = null;
if (isFilling) {
event = new PlayerBucketFillEvent(player, blockClicked, blockFace, bucket, itemInHand);
((PlayerBucketFillEvent) event).setCancelled(!canBuild(craftWorld, player, clickedX, clickedZ));
} else {
event = new PlayerBucketEmptyEvent(player, blockClicked, blockFace, bucket, itemInHand);
((PlayerBucketEmptyEvent) event).setCancelled(!canBuild(craftWorld, player, clickedX, clickedZ));
}
craftServer.getPluginManager().callEvent(event);
return event;
}
public boolean teleport(Location location, TeleportCause cause) {
if (entity.ridingEntity != null || entity.riddenByEntity != null || entity.isDead) {
return false;
}
// Spigot start
net.minecraft.world.WorldServer newWorld = ((CraftWorld) location.getWorld()).getHandle();
if (newWorld != entity.worldObj) {
entity.teleportTo(location, cause.isPortal());
return true;
}
// Spigot end
entity.setPositionAndRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
//entity.worldObj.entityJoinedWorld(entity, false); // PaperSpigot - Prevent Server from thinking a player teleporting within the world has joined the world
// entity.setLocation() throws no event, and so cannot be cancelled
return true;
}
public CraftBlockState(final Block block) {
this.world = (CraftWorld) block.getWorld();
this.x = block.getX();
this.y = block.getY();
this.z = block.getZ();
this.type = block.getTypeId();
this.light = block.getLightLevel();
this.chunk = (CraftChunk) block.getChunk();
this.flag = 3;
// Cauldron start - save TE data
TileEntity te = world.getHandle().getTileEntity(x, y, z);
if (te != null)
{
nbt = new NBTTagCompound();
te.writeToNBT(nbt);
}
else nbt = null;
// Cauldron end
createData(block.getData());
}
private static net.minecraft.command.ICommandSender getListener(CommandSender sender)
{
if ( sender instanceof CraftPlayer )
{
return new PlayerListener( ( (CraftPlayer) sender ).getHandle() );
}
if ( sender instanceof CraftBlockCommandSender )
{
CraftBlockCommandSender commandBlock = (CraftBlockCommandSender) sender;
Block block = commandBlock.getBlock();
return ( (net.minecraft.tileentity.TileEntityCommandBlock) ( (CraftWorld) block.getWorld() ).getTileEntityAt( block.getX(), block.getY(), block.getZ() ) ).func_145993_a();
}
if ( sender instanceof CraftMinecartCommand )
{
return ( (net.minecraft.entity.EntityMinecartCommandBlock) ( (CraftMinecartCommand) sender ).getHandle() ).func_145822_e();
}
return new ConsoleListener(sender); // Assume console/rcon
}
private void update() {
WorldServer world = ((CraftWorld) getWorld()).getHandle();
EntityPainting painting = new EntityPainting(world);
painting.hangingPosition = getHandle().getHangingPosition();
painting.art = getHandle().art;
painting.updateFacingWithBoundingBox(getHandle().facingDirection);
getHandle().setDead();
getHandle().velocityChanged = true; // because this occurs when the painting is broken, so it might be important
world.spawnEntity(painting);
this.entity = painting;
}
private void update() {
EntityItemFrame old = this.getHandle();
WorldServer world = ((CraftWorld) getWorld()).getHandle();
BlockPos position = old.getPosition();
EnumFacing direction = old.getHorizontalFacing();
ItemStack item = old.getDisplayedItem() != null ? old.getDisplayedItem().copy() : null;
old.setDead();
EntityItemFrame frame = new EntityItemFrame(world, position, direction);
frame.setDisplayedItem(item);
world.spawnEntity(frame);
this.entity = frame;
}
@Override
public void setRabbitType(Type type) {
EntityRabbit entity = getHandle();
if (getRabbitType() == Type.THE_KILLER_BUNNY) {
// Reset goals and target finders.
World world = ((CraftWorld) this.getWorld()).getHandle();
entity.tasks = new EntityAITasks(world != null && world.profiler != null ? world.profiler : null);
entity.targetTasks = new EntityAITasks(world != null && world.profiler != null ? world.profiler : null);
entity.initializePathFinderGoals();
}
entity.setRabbitType(CraftMagicMapping.toMagic(type));
}
public World getWorld() {
int dimension = worldMap.dimension;
for (World world : Bukkit.getServer().getWorlds()) {
if (((CraftWorld) world).getHandle().dimension == dimension) {
return world;
}
}
return null;
}
@Override
public boolean eject() {
requirePlaced();
TileEntity tileEntity = this.getTileEntityFromWorld();
if (!(tileEntity instanceof TileEntityJukebox)) return false;
TileEntityJukebox jukebox = (TileEntityJukebox) tileEntity;
boolean result = !jukebox.getRecord().isEmpty();
CraftWorld world = (CraftWorld) this.getWorld();
((BlockJukebox) Blocks.JUKEBOX).dropRecord(world.getHandle(), new BlockPos(getX(), getY(), getZ()), null);
return result;
}
@Override
public boolean play() {
Block block = getBlock();
if (block.getType() == Material.NOTE_BLOCK) {
TileEntityNote note = (TileEntityNote) this.getTileEntityFromWorld();
CraftWorld world = (CraftWorld) this.getWorld();
note.triggerNote(world.getHandle(), new BlockPos(getX(), getY(), getZ()));
return true;
} else {
return false;
}
}
@Override
public boolean play(byte instrument, byte note) {
Block block = getBlock();
if (block.getType() == Material.NOTE_BLOCK) {
CraftWorld world = (CraftWorld) this.getWorld();
world.getHandle().addBlockEvent(new BlockPos(getX(), getY(), getZ()), CraftMagicNumbers.getBlock(block), instrument, note);
return true;
} else {
return false;
}
}
@Override
public boolean play(Instrument instrument, Note note) {
Block block = getBlock();
if (block.getType() == Material.NOTE_BLOCK) {
CraftWorld world = (CraftWorld) this.getWorld();
world.getHandle().addBlockEvent(new BlockPos(getX(), getY(), getZ()), CraftMagicNumbers.getBlock(block), instrument.getType(), note.getId());
return true;
} else {
return false;
}
}
public CraftBlockEntityState(Block block, Class<T> tileEntityClass) {
super(block);
this.tileEntityClass = tileEntityClass;
// get tile entity from block:
CraftWorld world = (CraftWorld) this.getWorld();
this.tileEntity = tileEntityClass.cast(world.getTileEntityAt(this.getX(), this.getY(), this.getZ()));
// copy tile entity data:
this.snapshot = this.createSnapshot(tileEntity);
this.load(snapshot);
}
private static boolean canBuild(CraftWorld world, Player player, int x, int z) {
WorldServer worldServer = world.getHandle();
int spawnSize = Bukkit.getServer().getSpawnRadius();
if (world.getHandle().dimension != 0) return true;
if (spawnSize <= 0) return true;
if (((CraftServer) Bukkit.getServer()).getHandle().getOppedPlayers().isEmpty()) return true;
if (player.isOp()) return true;
BlockPos chunkcoordinates = worldServer.getSpawnPoint();
int distanceFromSpawn = Math.max(Math.abs(x - chunkcoordinates.getX()), Math.abs(z - chunkcoordinates.getZ()));
return distanceFromSpawn > spawnSize;
}
/**
* Block place methods
*/
public static BlockMultiPlaceEvent callBlockMultiPlaceEvent(World world, EntityPlayer who, EnumHand hand, List<BlockState> blockStates, int clickedX, int clickedY, int clickedZ) {
CraftWorld craftWorld = world.getWorld();
CraftServer craftServer = world.getServer();
Player player = (Player) who.getBukkitEntity();
Block blockClicked = craftWorld.getBlockAt(clickedX, clickedY, clickedZ);
boolean canBuild = true;
for (int i = 0; i < blockStates.size(); i++) {
if (!canBuild(craftWorld, player, blockStates.get(i).getX(), blockStates.get(i).getZ())) {
canBuild = false;
break;
}
}
org.bukkit.inventory.ItemStack item;
if (hand == EnumHand.MAIN_HAND) {
item = player.getInventory().getItemInMainHand();
} else {
item = player.getInventory().getItemInOffHand();
}
BlockMultiPlaceEvent event = new BlockMultiPlaceEvent(blockStates, blockClicked, item, player, canBuild);
craftServer.getPluginManager().callEvent(event);
return event;
}
public static PlayerInteractEvent callPlayerInteractEvent(EntityPlayer who, Action action, BlockPos position, EnumFacing direction, ItemStack itemstack, boolean cancelledBlock, EnumHand hand) {
Player player = (who == null) ? null : (Player) who.getBukkitEntity();
CraftItemStack itemInHand = CraftItemStack.asCraftMirror(itemstack);
CraftWorld craftWorld = (CraftWorld) player.getWorld();
CraftServer craftServer = (CraftServer) player.getServer();
Block blockClicked = null;
if (position != null) {
blockClicked = craftWorld.getBlockAt(position.getX(), position.getY(), position.getZ());
} else {
switch (action) {
case LEFT_CLICK_BLOCK:
action = Action.LEFT_CLICK_AIR;
break;
case RIGHT_CLICK_BLOCK:
action = Action.RIGHT_CLICK_AIR;
break;
}
}
BlockFace blockFace = CraftBlock.notchToBlockFace(direction);
if (itemInHand.getType() == Material.AIR || itemInHand.getAmount() == 0) {
itemInHand = null;
}
PlayerInteractEvent event = new PlayerInteractEvent(player, action, itemInHand, blockClicked, blockFace, (hand == null) ? null : ((hand == EnumHand.OFF_HAND) ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND));
if (cancelledBlock) {
event.setUseInteractedBlock(Event.Result.DENY);
}
craftServer.getPluginManager().callEvent(event);
return event;
}
/**
* BlockDamageEvent
*/
public static BlockDamageEvent callBlockDamageEvent(EntityPlayer who, int x, int y, int z, ItemStack itemstack, boolean instaBreak) {
Player player = (who == null) ? null : (Player) who.getBukkitEntity();
CraftItemStack itemInHand = CraftItemStack.asCraftMirror(itemstack);
CraftWorld craftWorld = (CraftWorld) player.getWorld();
CraftServer craftServer = (CraftServer) player.getServer();
Block blockClicked = craftWorld.getBlockAt(x, y, z);
BlockDamageEvent event = new BlockDamageEvent(player, blockClicked, itemInHand, instaBreak);
craftServer.getPluginManager().callEvent(event);
return event;
}
public FakeArmorStand(World world, @Nullable String name, boolean invisible, boolean marker, boolean small) {
super(new EntityArmorStand(((CraftWorld) world).getHandle()));
entity.setInvisible(invisible);
entity.setMarker(marker);
entity.setSmall(small);
entity.setBasePlate(false);
entity.setArms(false);
if(name != null) {
entity.setCustomName(name);
entity.setCustomNameVisible(true);
}
}
@Override
public void sendBlockChange(Location loc, int material, byte data) {
if (getHandle().playerNetServerHandler == null) return;
S23PacketBlockChange packet = new S23PacketBlockChange(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), ((CraftWorld) loc.getWorld()).getHandle());
packet.field_148883_d = CraftMagicNumbers.getBlock(material);
packet.field_148884_e = data;
getHandle().playerNetServerHandler.sendPacket(packet);
}
public Location getBedSpawnLocation() {
World world = getServer().getWorld(getHandle().spawnWorld);
net.minecraft.util.ChunkCoordinates bed = getHandle().getBedLocation();
if (world != null && bed != null) {
bed = net.minecraft.entity.player.EntityPlayer.verifyRespawnCoordinates(((CraftWorld) world).getHandle(), bed, getHandle().isSpawnForced());
if (bed != null) {
return new Location(world, bed.posX, bed.posY, bed.posZ);
}
}
return null;
}
private void update() {
net.minecraft.world.WorldServer world = ((CraftWorld) getWorld()).getHandle();
net.minecraft.entity.item.EntityPainting painting = new net.minecraft.entity.item.EntityPainting(world);
painting.field_146063_b = getHandle().field_146063_b;
painting.field_146064_c = getHandle().field_146064_c;
painting.field_146062_d = getHandle().field_146062_d;
painting.art = getHandle().art;
painting.setDirection(getHandle().hangingDirection);
getHandle().setDead();
getHandle().velocityChanged = true; // because this occurs when the painting is broken, so it might be important
world.spawnEntityInWorld(painting);
this.entity = painting;
}
public boolean setFacingDirection(BlockFace face, boolean force) {
if (!super.setFacingDirection(face, force)) {
return false;
}
net.minecraft.world.WorldServer world = ((CraftWorld) this.getWorld()).getHandle();
world.getEntityTracker().removeEntityFromAllTrackingPlayers(this.getHandle());
world.getEntityTracker().addEntityToTracker(this.getHandle());
return true;
}
public World getWorld() {
int dimension = worldMap.dimension; // Cauldron - byte -> int for Forge
for (World world : Bukkit.getServer().getWorlds()) {
if (((CraftWorld) world).getHandle().provider.dimensionId == dimension) {
return world;
}
}
return null;
}
public boolean isEmpty() {
// Cauldron start - support custom air blocks (Railcraft player aura tracking block)
//return getType() == Material.AIR;
if (getType() == Material.AIR) return true;
if (!(getWorld() instanceof CraftWorld)) return false;
return ((CraftWorld) getWorld()).getHandle().isAirBlock(getX(), getY(), getZ());
// Cauldron end
}
public CraftCommandBlock(Block block) {
super(block);
CraftWorld world = (CraftWorld) block.getWorld();
commandBlock = (TileEntityCommandBlock) world.getTileEntityAt(getX(), getY(), getZ());
command = commandBlock.func_145993_a().field_145763_e;
name = commandBlock.func_145993_a().getCommandSenderName();
}
public CraftSign(final Block block) {
super(block);
CraftWorld world = (CraftWorld) block.getWorld();
sign = (net.minecraft.tileentity.TileEntitySign) world.getTileEntityAt(getX(), getY(), getZ());
// Spigot start
if (sign == null) {
lines = new String[]{"", "", "", ""};
return;
}
// Spigot end
lines = new String[sign.signText.length];
System.arraycopy(sign.signText, 0, lines, 0, lines.length);
}