cn.nukkit.blockentity.BlockEntityShulkerBox Maven / Gradle / Ivy
package cn.nukkit.blockentity;
import cn.nukkit.Player;
import cn.nukkit.api.Since;
import cn.nukkit.block.Block;
import cn.nukkit.block.BlockID;
import cn.nukkit.inventory.BaseInventory;
import cn.nukkit.inventory.InventoryHolder;
import cn.nukkit.inventory.ShulkerBoxInventory;
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;
import java.util.HashSet;
/**
* @author PetteriM1
*/
public class BlockEntityShulkerBox extends BlockEntitySpawnable implements InventoryHolder, BlockEntityContainer, BlockEntityNameable {
protected ShulkerBoxInventory inventory;
public BlockEntityShulkerBox(FullChunk chunk, CompoundTag nbt) {
super(chunk, nbt);
}
@Override
protected void initBlockEntity() {
this.inventory = new ShulkerBoxInventory(this);
if (!this.namedTag.contains("Items") || !(this.namedTag.get("Items") instanceof ListTag)) {
this.namedTag.putList(new ListTag("Items"));
}
ListTag list = (ListTag) this.namedTag.getList("Items");
for (CompoundTag compound : list.getAll()) {
Item item = NBTIO.getItemHelper(compound);
this.inventory.slots.put(compound.getByte("Slot"), item);
}
if (!this.namedTag.contains("facing")) {
this.namedTag.putByte("facing", 0);
}
super.initBlockEntity();
}
@Override
public void close() {
if (!closed) {
for (Player player : new HashSet<>(this.getInventory().getViewers())) {
player.removeWindow(this.getInventory());
}
super.close();
}
}
@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));
}
}
@Since("1.6.0.0-PNX")
@Override
public void loadNBT() {
super.loadNBT();
ListTag list = (ListTag) this.namedTag.getList("Items");
for (CompoundTag compound : list.getAll()) {
Item item = NBTIO.getItemHelper(compound);
this.inventory.slots.put(compound.getByte("Slot"), item);
}
}
@Override
public boolean isBlockEntityValid() {
int blockID = this.getBlock().getId();
return blockID == Block.SHULKER_BOX || blockID == Block.UNDYED_SHULKER_BOX;
}
@Override
public int getSize() {
return 27;
}
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(Block.get(BlockID.AIR), 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").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 BaseInventory getInventory() {
return this.inventory;
}
public ShulkerBoxInventory getRealInventory() {
return inventory;
}
@Override
public String getName() {
return this.hasName() ? this.namedTag.getString("CustomName") : "Shulker Box";
}
@Override
public boolean hasName() {
return this.namedTag.contains("CustomName");
}
@Override
public void setName(String name) {
if (name == null || name.isEmpty()) {
this.namedTag.remove("CustomName");
return;
}
this.namedTag.putString("CustomName", name);
}
@Override
public CompoundTag getSpawnCompound() {
CompoundTag c = getDefaultCompound(this, SHULKER_BOX)
.putByte("facing", this.namedTag.getByte("facing"));
if (this.hasName()) {
c.put("CustomName", this.namedTag.get("CustomName"));
}
return c;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy