下面列出了io.netty.util.collection.CharObjectHashMap#cn.nukkit.item.Item 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void sendContents(Player... players) {
InventoryContentPacket pk = new InventoryContentPacket();
pk.slots = new Item[this.getSize()];
for (int i = 0; i < this.getSize(); ++i) {
pk.slots[i] = this.getItem(i);
}
for (Player player : players) {
int id = player.getWindowId(this);
if (id == -1 || !player.spawned) {
this.close(player);
continue;
}
pk.inventoryId = id;
player.dataPacket(pk);
}
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
Block down = this.down();
if (down.getId() == SAND || down.getId() == CACTUS) {
Block block0 = north();
Block block1 = south();
Block block2 = west();
Block block3 = east();
if (block0.isTransparent() && block1.isTransparent() && block2.isTransparent() && block3.isTransparent()) {
this.getLevel().setBlock(this, this, true);
return true;
}
}
return false;
}
@Override
public Item[] getDrops(Item item) {
if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_IRON) {
int count = new Random().nextInt(2) + 4;
Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
if (fortune != null && fortune.getLevel() >= 1) {
count += new Random().nextInt(fortune.getLevel() + 1);
}
return new Item[]{
new ItemRedstone(0, count)
};
} else {
return new Item[0];
}
}
private Item[][] cloneItemMap(Item[][] map) {
Item[][] newMap = new Item[map.length][];
for (int i = 0; i < newMap.length; i++) {
Item[] old = map[i];
Item[] n = new Item[old.length];
System.arraycopy(old, 0, n, 0, n.length);
newMap[i] = n;
}
for (int y = 0; y < newMap.length; y++) {
Item[] row = newMap[y];
for (int x = 0; x < row.length; x++) {
Item item = newMap[y][x];
newMap[y][x] = item.clone();
}
}
return newMap;
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
if (super.place(item, block, target, face, fx, fy, fz, player)) {
CompoundTag nbt = new CompoundTag()
.putList(new ListTag<>("Items"))
.putString("id", BlockEntity.COMPARATOR)
.putInt("x", (int) this.x)
.putInt("y", (int) this.y)
.putInt("z", (int) this.z);
new BlockEntityComparator(this.level.getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
onUpdate(Level.BLOCK_UPDATE_REDSTONE);
return true;
}
return false;
}
@Override
public int onUpdate(int type) {
int[] faces = {
3,
2,
5,
4,
};
if (type == Level.BLOCK_UPDATE_NORMAL) {
if (this.getDamage() >= 2 && this.getDamage() <= 5) {
if (this.getSide(BlockFace.fromIndex(faces[this.getDamage() - 2])).getId() == Item.AIR) {
this.getLevel().useBreakOn(this);
}
return Level.BLOCK_UPDATE_NORMAL;
}
}
return 0;
}
@Override
public boolean place(Item item, Block b, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
boolean concrete = false;
for (int side = 1; side <= 5; side++) {
Block block = this.getSide(BlockFace.fromIndex(side));
if (block.getId() == Block.WATER || block.getId() == Block.STILL_WATER || block.getId() == Block.LAVA || block.getId() == Block.STILL_LAVA) {
concrete = true;
break;
}
}
if (concrete) {
this.level.setBlock(this, Block.get(Block.CONCRETE, this.getDamage()), true, true);
} else {
this.level.setBlock(this, this, true, true);
}
return true;
}
@Override
public boolean onActivate(Item item, Player player) {
BlockEntity blockEntity = this.getLevel().getBlockEntity(this);
if (blockEntity == null || !(blockEntity instanceof BlockEntityJukebox)) {
blockEntity = this.createBlockEntity();
}
BlockEntityJukebox jukebox = (BlockEntityJukebox) blockEntity;
if (jukebox.getRecordItem().getId() != 0) {
jukebox.dropItem();
} else if (item instanceof ItemRecord) {
jukebox.setRecordItem(item);
jukebox.play();
player.getInventory().decreaseCount(player.getInventory().getHeldItemIndex());
}
return false;
}
@Override
public void setItem(int index, Item item) {
int i = this.getSlotIndex(index);
CompoundTag d = NBTIO.putItemHelper(item, index);
if (item.getId() == Item.AIR || item.getCount() <= 0) {
if (i >= 0) {
this.namedTag.getList("Items").getAll().remove(i);
}
} else if (i < 0) {
(this.namedTag.getList("Items", CompoundTag.class)).add(d);
} else {
(this.namedTag.getList("Items", CompoundTag.class)).add(i, d);
}
}
@Override
public Item[] getDrops(Item item) {
if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_IRON) {
int count = 1;
Enchantment fortune = item.getEnchantment(Enchantment.ID_FORTUNE_DIGGING);
if (fortune != null && fortune.getLevel() >= 1) {
int i = ThreadLocalRandom.current().nextInt(fortune.getLevel() + 2) - 1;
if (i < 0) {
i = 0;
}
count = i + 1;
}
return new Item[]{
new ItemDiamond(0, count)
};
} else {
return new Item[0];
}
}
@Override
public Item[] getDrops(Item item) {
if (item.isShears()) {
return new Item[]{
toItem()
};
} else {
return new Item[0];
}
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
if ((!target.isTransparent() || target.getId() == SLAB)) {
BlockFace facing;
boolean top;
if (face.getAxis().isHorizontal() || player == null) {
facing = face;
top = fy > 0.5;
} else {
facing = player.getDirection().getOpposite();
top = face != BlockFace.UP;
}
int faceBit = getMetaFromFacing(facing);
this.meta |= faceBit;
if (top) {
this.meta |= 0x08;
}
this.getLevel().setBlock(block, this, true, true);
return true;
}
return false;
}
public static CompoundTag putItemHelper(Item item, Integer slot) {
CompoundTag tag = new CompoundTag(null)
.putShort("id", item.getId())
.putByte("Count", item.getCount())
.putShort("Damage", item.getDamage());
if (slot != null) {
tag.putByte("Slot", slot);
}
if (item.hasCompoundTag()) {
tag.putCompound("tag", item.getNamedTag());
}
return tag;
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
if (!target.isTransparent() && face.getIndex() > 1 && !block.isSolid()) {
switch (face) {
case NORTH:
this.setDamage(3);
break;
case SOUTH:
this.setDamage(2);
break;
case WEST:
this.setDamage(1);
break;
case EAST:
this.setDamage(0);
break;
default:
return false;
}
this.getLevel().setBlock(block, this, true, true);
CompoundTag nbt = new CompoundTag()
.putString("id", BlockEntity.ITEM_FRAME)
.putInt("x", (int) block.x)
.putInt("y", (int) block.y)
.putInt("z", (int) block.z)
.putByte("ItemRotation", 0)
.putFloat("ItemDropChance", 1.0f);
if (item.hasCustomBlockData()) {
for (Tag aTag : item.getCustomBlockData().getAllTags()) {
nbt.put(aTag.getName(), aTag);
}
}
new BlockEntityItemFrame(this.getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
this.getLevel().addSound(this, Sound.BLOCK_ITEMFRAME_PLACE);
return true;
}
return false;
}
@Override
public boolean onActivate(Item item, Player player) {
this.level.getServer().getPluginManager().callEvent(new BlockRedstoneEvent(this, isPowerOn() ? 15 : 0, isPowerOn() ? 0 : 15));
this.setDamage(this.getDamage() ^ 0x08);
this.getLevel().setBlock(this, this, false, true);
this.getLevel().addSound(this, Sound.RANDOM_CLICK); //TODO: correct pitch
LeverOrientation orientation = LeverOrientation.byMetadata(this.isPowerOn() ? this.getDamage() ^ 0x08 : this.getDamage());
BlockFace face = orientation.getFacing();
//this.level.updateAroundRedstone(this, null);
this.level.updateAroundRedstone(this.getLocation().getSide(face.getOpposite()), isPowerOn() ? face : null);
return true;
}
public void putRecipeIngredient(Item ingredient) {
if (ingredient == null || ingredient.getId() == 0) {
this.putVarInt(0);
return;
}
this.putVarInt(ingredient.getId());
int damage;
if (ingredient.hasMeta()) {
damage = ingredient.getDamage();
} else {
damage = 0x7fff;
}
this.putVarInt(damage);
this.putVarInt(ingredient.getCount());
}
@Override
public Item[] getDrops(Item item) {
if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_WOODEN) {
return new Item[]{
this.toItem()
};
} else {
return new Item[0];
}
}
@Override
public Item[] getDrops(Item item) {
if (item.isShears()) {
return new Item[]{
toItem()
};
} else {
return new Item[]{
new ItemStick(0, new Random().nextInt(3))
};
}
}
@Override
public boolean isEmpty() {
if (this.getMaxStackSize() <= 0) {
return false;
}
for (Item item : this.slots.values()) {
if (item != null && item.getId() != 0 && item.getCount() > 0) {
return false;
}
}
return true;
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
BlockFace facing = face.getOpposite();
if (facing == BlockFace.UP) {
facing = BlockFace.DOWN;
}
this.meta = facing.getIndex();
boolean powered = this.level.isBlockPowered(this);
if (powered == this.isEnabled()) {
this.setEnabled(!powered);
}
this.level.setBlock(this, this);
CompoundTag nbt = new CompoundTag()
.putList(new ListTag<>("Items"))
.putString("id", BlockEntity.HOPPER)
.putInt("x", (int) this.x)
.putInt("y", (int) this.y)
.putInt("z", (int) this.z);
new BlockEntityHopper(this.level.getChunk(this.getFloorX() >> 4, this.getFloorZ() >> 4), nbt);
return true;
}
@Override
public Item getItem(int index) {
int i = this.getSlotIndex(index);
if (i < 0) {
return new ItemBlock(Block.get(BlockID.AIR), 0, 0);
} else {
CompoundTag data = (CompoundTag) this.namedTag.getList("Items").get(i);
return NBTIO.getItemHelper(data);
}
}
@Override
public Item[] getDrops(Item item) {
if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_STONE) {
return new Item[]{
toItem()
};
} else {
return new Item[0];
}
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
Block down = this.down();
if (down.getId() == 2 || down.getId() == 3 || down.getId() == 60) {
this.getLevel().setBlock(block, this, true, true);
return true;
}
return false;
}
@Override
public Item[] getDrops(Item item) {
BlockEntity blockEntity = getLevel().getBlockEntity(this);
int dropMeta = 0;
if (blockEntity != null) dropMeta = blockEntity.namedTag.getByte("SkullType");
return new Item[]{
new ItemSkull(dropMeta)
};
}
@Override
public Item[] getDrops(Item item) {
if (item.isSilkTouch()){
return new Item[]{
this.toItem()
};
} else {
return new Item[]{
new ItemGlowstoneDust(0, new NukkitRandom().nextRange(2, 4))
};
}
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
this.level.setBlock(block, this, true, true);
if (this.level.isBlockPowered(this)) {
this.isPowered = true;
} else {
this.isPowered = false;
}
return true;
}
@Override
public Item[] getDrops(Item item) {
if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_WOODEN) {
return new Item[]{toItem()};
} else {
return new Item[0];
}
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
if (target.isTransparent()) {
return false;
}
this.meta = face.getIndex();
this.level.setBlock(block, this, true, true);
return true;
}
@Override
public Item[] getDrops(Item item) {
if (item.isPickaxe() && item.getTier() >= ItemTool.TIER_WOODEN) {
return new Item[]{
toItem()
};
} else {
return new Item[0];
}
}
public void setPrimaryOutput(Item item) {
if (primaryOutput == null) {
primaryOutput = item.clone();
} else if (!primaryOutput.equals(item)) {
throw new RuntimeException("Primary result item has already been set and does not match the current item (expected " + primaryOutput + ", got " + item + ")");
}
}