use of net.glowstone.block.entity.ContainerEntity in project Glowstone by GlowstoneMC.
the class BlockHopper method pullItems.
private void pullItems(GlowBlock block, HopperEntity hopper) {
GlowBlock source = block.getRelative(BlockFace.UP);
MaterialData data = source.getState().getData();
if (!source.getType().isSolid() || (data instanceof Step && !((Step) data).isInverted()) || (data instanceof WoodenStep && !((WoodenStep) data).isInverted()) || (data instanceof Sign) || (data instanceof Rails)) {
GlowItem item = getFirstDroppedItem(source.getLocation());
if (item == null) {
return;
}
ItemStack stack = item.getItemStack();
HashMap<Integer, ItemStack> add = hopper.getInventory().addItem(stack);
if (add.size() > 0) {
item.setItemStack(add.get(0));
} else {
item.remove();
}
} else if (source.getBlockEntity() != null && source.getBlockEntity() instanceof ContainerEntity) {
ContainerEntity sourceContainer = (ContainerEntity) source.getBlockEntity();
if (sourceContainer.getInventory() == null || sourceContainer.getInventory().getContents().length == 0) {
return;
}
ItemStack item = getFirstItem(sourceContainer);
if (item == null) {
return;
}
ItemStack clone = item.clone();
clone.setAmount(1);
if (hopper.getInventory().addItem(clone).size() > 0) {
return;
}
if (item.getAmount() - 1 == 0) {
sourceContainer.getInventory().remove(item);
} else {
item.setAmount(item.getAmount() - 1);
}
}
}
use of net.glowstone.block.entity.ContainerEntity in project Glowstone by GlowstoneMC.
the class BlockHopper method pushItems.
private boolean pushItems(GlowBlock block, HopperEntity hopper) {
if (hopper.getInventory() == null || hopper.getInventory().getContents().length == 0) {
return false;
}
GlowBlock target = block.getRelative(((Hopper) block.getState().getData()).getFacing());
if (target.getType() != null && target.getBlockEntity() instanceof ContainerEntity) {
if (target.getState() instanceof GlowHopper) {
if (((Hopper) block.getState().getData()).getFacing() == BlockFace.DOWN) {
// If the hopper is facing downwards, the target hopper can do the pulling task itself
return false;
}
}
ItemStack item = getFirstItem(hopper);
if (item == null) {
return false;
}
ItemStack clone = item.clone();
clone.setAmount(1);
if (((ContainerEntity) target.getBlockEntity()).getInventory().addItem(clone).size() > 0) {
return false;
}
if (item.getAmount() - 1 == 0) {
hopper.getInventory().remove(item);
} else {
item.setAmount(item.getAmount() - 1);
}
return true;
}
return false;
}
Aggregations