cn.nukkit.blockentity.BlockEntityEjectable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of powernukkit Show documentation
Show all versions of powernukkit Show documentation
A Minecraft Bedrock Edition server software implementation made in Java from scratch which supports all new features.
package cn.nukkit.blockentity;
import cn.nukkit.api.PowerNukkitOnly;
import cn.nukkit.api.Since;
import cn.nukkit.block.BlockAir;
import cn.nukkit.inventory.EjectableInventory;
import cn.nukkit.inventory.InventoryHolder;
import cn.nukkit.item.Item;
import cn.nukkit.item.ItemBlock;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.nbt.NBTIO;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.ListTag;
@PowerNukkitOnly
@Since("1.4.0.0-PN")
public abstract class BlockEntityEjectable extends BlockEntitySpawnable implements BlockEntityContainer, BlockEntityNameable, InventoryHolder {
@PowerNukkitOnly
@Since("1.4.0.0-PN")
protected EjectableInventory inventory;
@PowerNukkitOnly
@Since("1.4.0.0-PN")
public BlockEntityEjectable(FullChunk chunk, CompoundTag nbt) {
super(chunk, nbt);
}
@PowerNukkitOnly
@Since("1.4.0.0-PN")
protected abstract EjectableInventory createInventory();
@PowerNukkitOnly
@Since("1.4.0.0-PN")
protected abstract String getBlockEntityName();
@Override
protected void initBlockEntity() {
this.inventory = createInventory();
if (!this.namedTag.contains("Items") || !(this.namedTag.get("Items") instanceof ListTag)) {
this.namedTag.putList(new ListTag("Items"));
}
for (int i = 0; i < this.getSize(); i++) {
this.inventory.setItem(i, this.getItem(i));
}
super.initBlockEntity();
}
@Override
public int getSize() {
return 9;
}
@PowerNukkitOnly
@Since("1.4.0.0-PN")
protected int getSlotIndex(int index) {
ListTag list = this.namedTag.getList("Items", CompoundTag.class);
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getByte("Slot") == index) {
return i;
}
}
return -1;
}
@Override
public Item getItem(int index) {
int i = this.getSlotIndex(index);
if (i < 0) {
return new ItemBlock(new BlockAir(), 0, 0);
} else {
CompoundTag data = (CompoundTag) this.namedTag.getList("Items").get(i);
return NBTIO.getItemHelper(data);
}
}
@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 EjectableInventory getInventory() {
return inventory;
}
@Override
public CompoundTag getSpawnCompound() {
CompoundTag c = new CompoundTag()
.putString("id", getBlockEntityName())
.putInt("x", (int) this.x)
.putInt("y", (int) this.y)
.putInt("z", (int) this.z);
if (this.hasName()) {
c.put("CustomName", this.namedTag.get("CustomName"));
}
return c;
}
@Override
public void saveNBT() {
this.namedTag.putList(new ListTag("Items"));
for (int index = 0; index < this.getSize(); index++) {
this.setItem(index, this.inventory.getItem(index));
}
super.saveNBT();
}
@Override
public String getName() {
return this.hasName() ? this.namedTag.getString("CustomName") : getBlockEntityName();
}
@Override
public boolean hasName() {
return this.namedTag.contains("CustomName");
}
@Override
public void setName(String name) {
if (name == null || name.equals("")) {
this.namedTag.remove("CustomName");
return;
}
this.namedTag.putString("CustomName", name);
}
@Override
public void onBreak() {
for (Item content : inventory.getContents().values()) {
level.dropItem(this, content);
}
}
}