cn.nukkit.inventory.ContainerInventory 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.inventory;
import cn.nukkit.Player;
import cn.nukkit.entity.Entity;
import cn.nukkit.item.Item;
import cn.nukkit.math.NukkitMath;
import cn.nukkit.math.Vector3;
import cn.nukkit.network.protocol.ContainerClosePacket;
import cn.nukkit.network.protocol.ContainerOpenPacket;
import java.util.Map;
/**
* @author MagicDroidX (Nukkit Project)
*/
public abstract class ContainerInventory extends BaseInventory {
public ContainerInventory(InventoryHolder holder, InventoryType type) {
super(holder, type);
}
public ContainerInventory(InventoryHolder holder, InventoryType type, Map items) {
super(holder, type, items);
}
public ContainerInventory(InventoryHolder holder, InventoryType type, Map items, Integer overrideSize) {
super(holder, type, items, overrideSize);
}
public ContainerInventory(InventoryHolder holder, InventoryType type, Map items, Integer overrideSize, String overrideTitle) {
super(holder, type, items, overrideSize, overrideTitle);
}
@Override
public void onOpen(Player who) {
super.onOpen(who);
ContainerOpenPacket pk = new ContainerOpenPacket();
pk.windowId = who.getWindowId(this);
pk.type = this.getType().getNetworkType();
InventoryHolder holder = this.getHolder();
if (holder instanceof Vector3) {
pk.x = (int) ((Vector3) holder).getX();
pk.y = (int) ((Vector3) holder).getY();
pk.z = (int) ((Vector3) holder).getZ();
} else {
pk.x = pk.y = pk.z = 0;
}
if (holder instanceof Entity) {
pk.entityId = ((Entity) holder).getId();
}
who.dataPacket(pk);
this.sendContents(who);
}
@Override
public void onClose(Player who) {
ContainerClosePacket pk = new ContainerClosePacket();
pk.windowId = who.getWindowId(this);
pk.wasServerInitiated = false;
who.dataPacket(pk);
super.onClose(who);
}
public static int calculateRedstone(Inventory inv) {
if (inv == null) {
return 0;
} else {
int itemCount = 0;
float averageCount = 0;
for (int slot = 0; slot < inv.getSize(); ++slot) {
Item item = inv.getItem(slot);
if (item.getId() != 0) {
averageCount += (float) item.getCount() / (float) Math.min(inv.getMaxStackSize(), item.getMaxStackSize());
++itemCount;
}
}
averageCount = averageCount / (float) inv.getSize();
return NukkitMath.floorFloat(averageCount * 14) + (itemCount > 0 ? 1 : 0);
}
}
}