下面列出了怎么用org.bukkit.craftbukkit.util.CraftMagicNumbers的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void setTypeId(int type) {
if (getTypeId() == type) {
return;
} else if (type == 0) {
handle = null;
} else if (CraftMagicNumbers.getItem(type) == null) { // :(
handle = null;
} else if (handle == null) {
handle = new net.minecraft.item.ItemStack(CraftMagicNumbers.getItem(type), 1, 0);
} else {
handle.setItem(CraftMagicNumbers.getItem(type));
if (hasItemMeta()) {
// This will create the appropriate item meta, which will contain all the data we intend to keep
setItemMeta(handle, getItemMeta(handle));
}
}
setData(null);
}
CraftMetaBookSigned(NBTTagCompound tag) {
super(tag, false);
boolean resolved = true;
if (tag.hasKey(RESOLVED.NBT)) {
resolved = tag.getBoolean(RESOLVED.NBT);
}
if (tag.hasKey(BOOK_PAGES.NBT)) {
NBTTagList pages = tag.getTagList(BOOK_PAGES.NBT, CraftMagicNumbers.NBT.TAG_STRING);
for (int i = 0; i < Math.min(pages.tagCount(), MAX_PAGES); i++) {
String page = pages.getStringTagAt(i);
if (resolved) {
try {
this.pages.add(ITextComponent.Serializer.jsonToComponent(page));
continue;
} catch (Exception e) {
// Ignore and treat as an old book
}
}
addPage(page);
}
}
}
static Map<Enchantment, Integer> buildEnchantments(NBTTagCompound tag, ItemMetaKey key) {
if (!tag.hasKey(key.NBT)) {
return null;
}
NBTTagList ench = tag.getTagList(key.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND);
Map<Enchantment, Integer> enchantments = new HashMap<Enchantment, Integer>(ench.tagCount());
for (int i = 0; i < ench.tagCount(); i++) {
int id = 0xffff & ((NBTTagCompound) ench.get(i)).getShort(ENCHANTMENTS_ID.NBT);
int level = 0xffff & ((NBTTagCompound) ench.get(i)).getShort(ENCHANTMENTS_LVL.NBT);
Enchantment enchant = Enchantment.getById(id);
if (enchant != null) {
enchantments.put(enchant, level);
}
}
return enchantments;
}
CraftMetaBanner(NBTTagCompound tag) {
super(tag);
if (!tag.hasKey("BlockEntityTag")) {
return;
}
NBTTagCompound entityTag = tag.getCompoundTag("BlockEntityTag");
base = entityTag.hasKey(BASE.NBT) ? DyeColor.getByDyeData((byte) entityTag.getInteger(BASE.NBT)) : null;
if (entityTag.hasKey(PATTERNS.NBT)) {
NBTTagList patterns = entityTag.getTagList(PATTERNS.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND);
for (int i = 0; i < Math.min(patterns.tagCount(), 20); i++) {
NBTTagCompound p = patterns.getCompoundTagAt(i);
this.patterns.add(new Pattern(DyeColor.getByDyeData((byte) p.getInteger(COLOR.NBT)), PatternType.getByIdentifier(p.getString(PATTERN.NBT))));
}
}
}
CraftMetaFirework(NBTTagCompound tag) {
super(tag);
if (!tag.hasKey(FIREWORKS.NBT)) {
return;
}
NBTTagCompound fireworks = tag.getCompoundTag(FIREWORKS.NBT);
power = 0xff & fireworks.getByte(FLIGHT.NBT);
if (!fireworks.hasKey(EXPLOSIONS.NBT)) {
return;
}
NBTTagList fireworkEffects = fireworks.getTagList(EXPLOSIONS.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND);
List<FireworkEffect> effects = this.effects = new ArrayList<FireworkEffect>(fireworkEffects.tagCount());
for (int i = 0; i < fireworkEffects.tagCount(); i++) {
effects.add(getEffect((NBTTagCompound) fireworkEffects.get(i)));
}
}
public static StatBase getMaterialStatistic(Statistic stat, Material material) {
try {
if (stat == Statistic.MINE_BLOCK) {
return StatList.getBlockStats(CraftMagicNumbers.getBlock(material)); // PAIL: getMineBlockStatistic
}
if (stat == Statistic.CRAFT_ITEM) {
return StatList.getCraftStats(CraftMagicNumbers.getItem(material)); // PAIL: getCraftItemStatistic
}
if (stat == Statistic.USE_ITEM) {
return StatList.getObjectUseStats(CraftMagicNumbers.getItem(material)); // PAIL: getUseItemStatistic
}
if (stat == Statistic.BREAK_ITEM) {
return StatList.getObjectBreakStats(CraftMagicNumbers.getItem(material)); // PAIL: getBreakItemStatistic
}
if (stat == Statistic.PICKUP) {
return StatList.getObjectsPickedUpStats(CraftMagicNumbers.getItem(material)); // PAIL: getPickupStatistic
}
if (stat == Statistic.DROP) {
return StatList.getDroppedObjectStats(CraftMagicNumbers.getItem(material)); // PAIL: getDropItemStatistic
}
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
return null;
}
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;
}
@Override
public void setPlaying(Material record) {
if (record == null || CraftMagicNumbers.getItem(record) == null) {
record = Material.AIR;
jukebox.func_145857_a(null);
} else {
jukebox.func_145857_a(new ItemStack(CraftMagicNumbers.getItem(record), 1));
}
jukebox.markDirty();
if (record == Material.AIR) {
world.getHandle().setBlockMetadataWithNotify(getX(), getY(), getZ(), 0, 3);
} else {
world.getHandle().setBlockMetadataWithNotify(getX(), getY(), getZ(), 1, 3);
}
world.playEffect(getLocation(), Effect.RECORD_PLAY, record.getId());
}
@Override
public void setTypeId(int type) {
if (getTypeId() == type) {
return;
} else if (type == 0) {
handle = null;
} else if (CraftMagicNumbers.getItem(type) == null) { // :(
handle = null;
} else if (handle == null) {
handle = new net.minecraft.item.ItemStack(CraftMagicNumbers.getItem(type), 1, 0);
} else {
handle.func_150996_a(CraftMagicNumbers.getItem(type));
if (hasItemMeta()) {
// This will create the appropriate item meta, which will contain all the data we intend to keep
setItemMeta(handle, getItemMeta(handle));
}
}
setData(null);
}
public void addToCraftingManager() {
Object[] data;
String[] shape = this.getShape();
Map<Character, ItemStack> ingred = this.getIngredientMap();
int datalen = shape.length;
datalen += ingred.size() * 2;
int i = 0;
data = new Object[datalen];
for (; i < shape.length; i++) {
data[i] = shape[i];
}
for (char c : ingred.keySet()) {
ItemStack mdata = ingred.get(c);
if (mdata == null) continue;
data[i] = c;
i++;
int id = mdata.getTypeId();
short dmg = mdata.getDurability();
data[i] = new net.minecraft.item.ItemStack(CraftMagicNumbers.getItem(id), 1, dmg);
i++;
}
net.minecraft.item.crafting.CraftingManager.getInstance().addRecipe(CraftItemStack.asNMSCopy(this.getResult()), data);
}
public void setDisplayBlock(MaterialData material) {
if (material != null) {
IBlockState block = CraftMagicNumbers.getBlock(material.getItemTypeId()).getStateFromMeta(material.getData());
this.getHandle().setDisplayTile(block);
} else {
// Set block to air (default) and set the flag to not have a display block.
this.getHandle().setDisplayTile(Blocks.AIR.getDefaultState());
this.getHandle().setHasDisplayTile(false);
}
}
@Override
public int getCooldown(Material material) {
Preconditions.checkArgument(material != null, "material");
CooldownTracker.Cooldown cooldown = getHandle().getCooldownTracker().cooldowns.get(CraftMagicNumbers.getItem(material));
return (cooldown == null) ? 0 : Math.max(0, cooldown.expireTicks - getHandle().getCooldownTracker().ticks);
}
@Override
public void setCooldown(Material material, int ticks) {
Preconditions.checkArgument(material != null, "material");
Preconditions.checkArgument(ticks >= 0, "Cannot have negative cooldown");
getHandle().getCooldownTracker().setCooldown(CraftMagicNumbers.getItem(material), ticks);
}
@Override
public void sendBlockChange(Location loc, int material, byte data) {
if (getHandle().connection == null) return;
SPacketBlockChange packet = new SPacketBlockChange(((CraftWorld) loc.getWorld()).getHandle(), new BlockPos(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
packet.blockState = CraftMagicNumbers.getBlock(material).getStateFromMeta(data);
getHandle().connection.sendPacket(packet);
}
public FallingBlock spawnFallingBlock(Location location, org.bukkit.Material material, byte data) throws IllegalArgumentException {
Validate.notNull(location, "Location cannot be null");
Validate.notNull(material, "Material cannot be null");
Validate.isTrue(material.isBlock(), "Material must be a block");
EntityFallingBlock entity = new EntityFallingBlock(world, location.getX(), location.getY(), location.getZ(), CraftMagicNumbers.getBlock(material).getStateFromMeta(data));
entity.ticksExisted = 1;
world.spawnEntity(entity, SpawnReason.CUSTOM);
return (FallingBlock) entity.getBukkitEntity();
}
public Collection<ItemStack> getDrops() {
List<ItemStack> drops = new ArrayList<ItemStack>();
net.minecraft.block.Block block = this.getNMSBlock();
if (block != Blocks.AIR) {
IBlockState data = getData0();
// based on nms.Block.dropNaturally
int count = block.quantityDroppedWithBonus(0, chunk.getHandle().getWorld().rand);
for (int i = 0; i < count; ++i) {
Item item = block.getItemDropped(data, chunk.getHandle().getWorld().rand, 0);
if (item != Items.AIR) {
// Skulls are special, their data is based on the tile entity
if (Blocks.SKULL == block) {
net.minecraft.item.ItemStack nmsStack = new net.minecraft.item.ItemStack(item, 1, block.damageDropped(data));
TileEntitySkull tileentityskull = (TileEntitySkull) chunk.getHandle().getWorld().getTileEntity(new BlockPos(x, y, z));
if (tileentityskull.getSkullType() == 3 && tileentityskull.getPlayerProfile() != null) {
nmsStack.setTagCompound(new NBTTagCompound());
NBTTagCompound nbttagcompound = new NBTTagCompound();
NBTUtil.writeGameProfile(nbttagcompound, tileentityskull.getPlayerProfile());
nmsStack.getTagCompound().setTag("SkullOwner", nbttagcompound);
}
drops.add(CraftItemStack.asBukkitCopy(nmsStack));
// We don't want to drop cocoa blocks, we want to drop cocoa beans.
} else if (Blocks.COCOA == block) {
int age = (Integer) data.getValue(BlockCocoa.AGE);
int dropAmount = (age >= 2 ? 3 : 1);
for (int j = 0; j < dropAmount; ++j) {
drops.add(new ItemStack(Material.INK_SACK, 1, (short) 3));
}
} else {
drops.add(new ItemStack(CraftMagicNumbers.getMaterial(item), 1, (short) block.damageDropped(data)));
}
}
}
}
return drops;
}
@Override
public Material getPlaying() {
ItemStack record = this.getSnapshot().getRecord();
if (record.isEmpty()) {
return Material.AIR;
}
return CraftMagicNumbers.getMaterial(record.getItem());
}
@Override
public void setPlaying(Material record) {
if (record == null || CraftMagicNumbers.getItem(record) == null) {
record = Material.AIR;
}
this.getSnapshot().setRecord(new ItemStack(CraftMagicNumbers.getItem(record), 1));
if (record == Material.AIR) {
setRawData((byte) 0);
} else {
setRawData((byte) 1);
}
}
public boolean update(boolean force, boolean applyPhysics) {
if (!isPlaced()) {
return true;
}
Block block = getBlock();
if (block.getType() != getType()) {
if (!force) {
return false;
}
}
BlockPos pos = new BlockPos(x, y, z);
IBlockState newBlock = CraftMagicNumbers.getBlock(getType()).getStateFromMeta(this.getRawData());
block.setTypeIdAndData(getTypeId(), getRawData(), applyPhysics);
world.getHandle().notifyBlockUpdate(
pos,
CraftMagicNumbers.getBlock(block).getStateFromMeta(block.getData()),
newBlock,
3
);
// Update levers etc
if (applyPhysics && getData() instanceof Attachable) {
world.getHandle().notifyNeighborsOfStateChange(pos.offset(CraftBlock.blockFaceToNotch(((Attachable) getData()).getAttachedFace())), newBlock.getBlock(), false);
}
if (nbt != null) {
TileEntity te = world.getHandle().getTileEntity(new BlockPos(this.x, this.y, this.z));
if (te != null) {
te.readFromNBT(nbt);
}
}
return true;
}
@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;
}
}
CraftMetaBook(NBTTagCompound tag, boolean handlePages) {
super(tag);
if (tag.hasKey(BOOK_TITLE.NBT)) {
this.title = tag.getString(BOOK_TITLE.NBT);
}
if (tag.hasKey(BOOK_AUTHOR.NBT)) {
this.author = tag.getString(BOOK_AUTHOR.NBT);
}
boolean resolved = false;
if (tag.hasKey(RESOLVED.NBT)) {
resolved = tag.getBoolean(RESOLVED.NBT);
}
if (tag.hasKey(GENERATION.NBT)) {
generation = tag.getInteger(GENERATION.NBT);
}
if (tag.hasKey(BOOK_PAGES.NBT) && handlePages) {
NBTTagList pages = tag.getTagList(BOOK_PAGES.NBT, CraftMagicNumbers.NBT.TAG_STRING);
for (int i = 0; i < Math.min(pages.tagCount(), MAX_PAGES); i++) {
String page = pages.getStringTagAt(i);
if (resolved) {
try {
this.pages.add(ITextComponent.Serializer.jsonToComponent(page));
continue;
} catch (Exception e) {
// Ignore and treat as an old book
}
}
addPage(page);
}
}
}
CraftMetaBlockState(NBTTagCompound tag, Material material) {
super(tag);
this.material = material;
if (tag.hasKey(BLOCK_ENTITY_TAG.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND)) {
blockEntityTag = tag.getCompoundTag(BLOCK_ENTITY_TAG.NBT);
} else {
blockEntityTag = null;
}
}
public static boolean handleBlockFormEvent(World world, BlockPos pos, IBlockState block, @Nullable Entity entity) {
BlockState blockState = world.getWorld().getBlockAt(pos.getX(), pos.getY(), pos.getZ()).getState();
blockState.setType(CraftMagicNumbers.getMaterial(block.getBlock()));
blockState.setRawData((byte) block.getBlock().getMetaFromState(block));
BlockFormEvent event = (entity == null) ? new BlockFormEvent(blockState.getBlock(), blockState) : new EntityBlockFormEvent(entity.getBukkitEntity(), blockState.getBlock(), blockState);
world.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
blockState.update(true);
}
return !event.isCancelled();
}
public static void playBlockPlaceSound(World bukkitWorld, Vector pos, Material material, float volume) {
if (!material.isBlock()) {
return;
}
playCustomSound(bukkitWorld,
pos.toLocation(bukkitWorld),
CraftMagicNumbers.getBlock(material).getStepSound().placeSound(),
SoundCategory.BLOCKS,
volume,
1f);
}
/**
* Test if the given tool is capable of "efficiently" mining the given block.
*
* Derived from CraftBlock.itemCausesDrops()
*/
public static boolean canMineBlock(MaterialData blockMaterial, org.bukkit.inventory.ItemStack tool) {
if(!blockMaterial.getItemType().isBlock()) {
throw new IllegalArgumentException("Material '" + blockMaterial + "' is not a block");
}
net.minecraft.server.Block nmsBlock = CraftMagicNumbers.getBlock(blockMaterial.getItemType());
net.minecraft.server.Item nmsTool = tool == null ? null : CraftMagicNumbers.getItem(tool.getType());
return nmsBlock != null && (nmsBlock.getBlockData().getMaterial().isAlwaysDestroyable() ||
(nmsTool != null && nmsTool.canDestroySpecialBlock(nmsBlock.getBlockData())));
}
public static Set<MaterialData> getBlockStates(Material material) {
ImmutableSet.Builder<MaterialData> materials = ImmutableSet.builder();
Block nmsBlock = CraftMagicNumbers.getBlock(material);
List<IBlockData> states = nmsBlock.s().a();
if(states != null) {
for(IBlockData state : states) {
int data = nmsBlock.toLegacyData(state);
materials.add(material.getNewData((byte) data));
}
}
return materials.build();
}
@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 Collection<ItemStack> getDrops() {
List<ItemStack> drops = new ArrayList<ItemStack>();
net.minecraft.block.Block block = this.getNMSBlock();
if (block != Blocks.air) {
byte data = getData();
// based on nms.Block.dropNaturally
int count = block.quantityDroppedWithBonus(0, chunk.getHandle().worldObj.rand);
for (int i = 0; i < count; ++i) {
Item item = block.getItemDropped(data, chunk.getHandle().worldObj.rand, 0);
if (item != null) {
// Skulls are special, their data is based on the tile entity
if (Blocks.skull == block) {
net.minecraft.item.ItemStack nmsStack = new net.minecraft.item.ItemStack(item, 1, block.getDamageValue(chunk.getHandle().worldObj, x, y, z));
TileEntitySkull tileentityskull = (TileEntitySkull) chunk.getHandle().worldObj.getTileEntity(x, y, z);
if (tileentityskull.func_145904_a() == 3 && tileentityskull.func_152108_a() != null) {
nmsStack.setTagCompound(new NBTTagCompound());
NBTTagCompound nbttagcompound = new NBTTagCompound();
NBTUtil.func_152460_a(nbttagcompound, tileentityskull.func_152108_a());
nmsStack.getTagCompound().setTag("SkullOwner", nbttagcompound);
}
drops.add(CraftItemStack.asBukkitCopy(nmsStack));
// We don't want to drop cocoa blocks, we want to drop cocoa beans.
} else if (Blocks.cocoa == block) {
int dropAmount = (BlockCocoa.func_149987_c(data) >= 2 ? 3 : 1);
for (int j = 0; j < dropAmount; ++j) {
drops.add(new ItemStack(Material.INK_SACK, 1, (short) 3));
}
} else {
drops.add(new ItemStack(org.bukkit.craftbukkit.util.CraftMagicNumbers.getMaterial(item), 1, (short) block.damageDropped(data)));
}
}
}
}
return drops;
}
@Override
public Material getPlaying() {
ItemStack record = jukebox.func_145856_a();
if (record == null) {
return Material.AIR;
}
return CraftMagicNumbers.getMaterial(record.getItem());
}