use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.
the class BlockItemFrame method getDrops.
@Override
public Item[] getDrops(Item item) {
BlockEntity blockEntity = this.getLevel().getBlockEntity(this);
BlockEntityItemFrame itemFrame = (BlockEntityItemFrame) blockEntity;
int chance = new Random().nextInt(100) + 1;
if (itemFrame != null && chance <= (itemFrame.getItemDropChance() * 100)) {
return new Item[] { toItem(), Item.get(itemFrame.getItem().getId(), itemFrame.getItem().getDamage(), 1) };
} else {
return new Item[] { toItem() };
}
}
use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.
the class BlockEnderChest method onActivate.
@Override
public boolean onActivate(Item item, Player player) {
if (player != null) {
Block top = this.up();
if (!top.isTransparent()) {
return true;
}
BlockEntity t = this.getLevel().getBlockEntity(this);
BlockEntityEnderChest chest;
if (t instanceof BlockEntityEnderChest) {
chest = (BlockEntityEnderChest) t;
} else {
CompoundTag nbt = new CompoundTag("").putString("id", BlockEntity.ENDER_CHEST).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
chest = new BlockEntityEnderChest(this.getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
}
if (chest.namedTag.contains("Lock") && chest.namedTag.get("Lock") instanceof StringTag) {
if (!chest.namedTag.getString("Lock").equals(item.getCustomName())) {
return true;
}
}
player.setViewingEnderChest(this);
player.addWindow(player.getEnderChestInventory());
}
return true;
}
use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.
the class BlockFlowerPot method onActivate.
@Override
public boolean onActivate(Item item, Player player) {
BlockEntity blockEntity = getLevel().getBlockEntity(this);
if (!(blockEntity instanceof BlockEntityFlowerPot))
return false;
if (blockEntity.namedTag.getShort("item") != 0 || blockEntity.namedTag.getInt("mData") != 0)
return false;
int itemID;
int itemMeta;
if (!canPlaceIntoFlowerPot(item.getId())) {
if (!canPlaceIntoFlowerPot(item.getBlock().getId())) {
return true;
}
itemID = item.getBlock().getId();
itemMeta = item.getDamage();
} else {
itemID = item.getId();
itemMeta = item.getDamage();
}
blockEntity.namedTag.putShort("item", itemID);
blockEntity.namedTag.putInt("data", itemMeta);
this.setDamage(1);
this.getLevel().setBlock(this, this, true);
((BlockEntityFlowerPot) blockEntity).spawnToAll();
if (player.isSurvival()) {
item.setCount(item.getCount() - 1);
player.getInventory().setItemInHand(item.getCount() > 0 ? item : Item.get(Item.AIR));
}
return true;
}
use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.
the class BlockRedstoneComparator method updateState.
@Override
public void updateState() {
if (!this.level.isBlockTickPending(this, this)) {
int output = this.calculateOutput();
BlockEntity blockEntity = this.level.getBlockEntity(this);
int power = blockEntity instanceof BlockEntityComparator ? ((BlockEntityComparator) blockEntity).getOutputSignal() : 0;
if (output != power || this.isPowered() != this.shouldBePowered()) {
/*if(isFacingTowardsRepeater()) {
this.level.scheduleUpdate(this, this, 2, -1);
} else {
this.level.scheduleUpdate(this, this, 2, 0);
}*/
// System.out.println("schedule update 0");
this.level.scheduleUpdate(this, this, 2);
}
}
}
use of cn.nukkit.blockentity.BlockEntity in project Nukkit by Nukkit.
the class BlockCauldron method onActivate.
@Override
public boolean onActivate(Item item, Player player) {
BlockEntity be = this.level.getBlockEntity(this);
if (!(be instanceof BlockEntityCauldron)) {
return false;
}
BlockEntityCauldron cauldron = (BlockEntityCauldron) be;
switch(item.getId()) {
case Item.BUCKET:
if (item.getDamage() == 0) {
// empty bucket
if (!isFull() || cauldron.isCustomColor() || cauldron.hasPotion()) {
break;
}
ItemBucket bucket = (ItemBucket) item.clone();
// water bucket
bucket.setDamage(8);
PlayerBucketFillEvent ev = new PlayerBucketFillEvent(player, this, null, item, bucket);
this.level.getServer().getPluginManager().callEvent(ev);
if (!ev.isCancelled()) {
if (player.isSurvival()) {
player.getInventory().setItemInHand(ev.getItem());
}
// empty
this.setDamage(0);
this.level.setBlock(this, this, true);
cauldron.clearCustomColor();
this.getLevel().addSound(this.add(0.5, 1, 0.5), Sound.CAULDRON_TAKEWATER);
}
} else if (item.getDamage() == 8) {
if (isFull() && !cauldron.isCustomColor() && !cauldron.hasPotion()) {
break;
}
ItemBucket bucket = (ItemBucket) item.clone();
// empty bucket
bucket.setDamage(0);
PlayerBucketEmptyEvent ev = new PlayerBucketEmptyEvent(player, this, null, item, bucket);
this.level.getServer().getPluginManager().callEvent(ev);
if (!ev.isCancelled()) {
if (player.isSurvival()) {
player.getInventory().setItemInHand(ev.getItem());
}
if (cauldron.hasPotion()) {
// if has potion
// empty
this.setDamage(0);
// reset potion
cauldron.setPotionId(0xffff);
cauldron.setSplashPotion(false);
cauldron.clearCustomColor();
this.level.setBlock(this, this, true);
this.level.addSound(this.add(0.5, 0, 0.5), Sound.CAULDRON_EXPLODE);
} else {
// fill
this.setDamage(6);
cauldron.clearCustomColor();
this.level.setBlock(this, this, true);
this.level.addSound(this.add(0.5, 1, 0.5), Sound.BUCKET_FILL_WATER);
}
// this.update();
}
}
break;
case // TODO
Item.DYE:
break;
case Item.LEATHER_CAP:
case Item.LEATHER_TUNIC:
case Item.LEATHER_PANTS:
case Item.LEATHER_BOOTS:
break;
case Item.POTION:
if (isFull()) {
break;
}
this.setDamage(this.getDamage() + 1);
if (this.getDamage() > 0x06)
this.setDamage(0x06);
if (item.getCount() == 1) {
player.getInventory().setItemInHand(new ItemBlock(new BlockAir()));
} else if (item.getCount() > 1) {
item.setCount(item.getCount() - 1);
player.getInventory().setItemInHand(item);
Item bottle = new ItemGlassBottle();
if (player.getInventory().canAddItem(bottle)) {
player.getInventory().addItem(bottle);
} else {
player.getLevel().dropItem(player.add(0, 1.3, 0), bottle, player.getDirectionVector().multiply(0.4));
}
}
this.level.addSound(this.add(0.5, 0.5, 0.5), Sound.CAULDRON_FILLPOTION);
break;
case Item.GLASS_BOTTLE:
if (isEmpty()) {
break;
}
this.setDamage(this.getDamage() - 1);
if (this.getDamage() < 0x00)
this.setDamage(0x00);
if (item.getCount() == 1) {
player.getInventory().setItemInHand(new ItemPotion());
} else if (item.getCount() > 1) {
item.setCount(item.getCount() - 1);
player.getInventory().setItemInHand(item);
Item potion = new ItemPotion();
if (player.getInventory().canAddItem(potion)) {
player.getInventory().addItem(potion);
} else {
player.getLevel().dropItem(player.add(0, 1.3, 0), potion, player.getDirectionVector().multiply(0.4));
}
}
this.level.addSound(this.add(0.5, 0.5, 0.5), Sound.CAULDRON_TAKEPOTION);
break;
default:
return true;
}
this.level.updateComparatorOutputLevel(this);
return true;
}
Aggregations