use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleCrafter method openAttachedGui.
/**
* Triggers opening the first possible crafting provider or inventory GUI by using onBlockActivated.
*
* @return true, if a GUI was opened (server-side only)
*/
public boolean openAttachedGui(EntityPlayer player) {
if (MainProxy.isClient(player.world)) {
if (player instanceof EntityPlayerMP) {
player.closeScreen();
} else if (player instanceof EntityPlayerSP) {
player.closeScreen();
}
MainProxy.sendPacketToServer(PacketHandler.getPacket(CraftingPipeOpenConnectedGuiPacket.class).setModulePos(this));
return false;
}
final IPipeServiceProvider service = _service;
if (service == null)
return false;
final IWorldProvider worldProvider = _world;
if (worldProvider == null)
return false;
// hack to avoid wrenching blocks
int savedEquipped = player.inventory.currentItem;
boolean foundSlot = false;
// try to find a empty slot
for (int i = 0; i < 9; i++) {
if (player.inventory.getStackInSlot(i).isEmpty()) {
foundSlot = true;
player.inventory.currentItem = i;
break;
}
}
// okay, anything that's a block?
if (!foundSlot) {
for (int i = 0; i < 9; i++) {
ItemStack is = player.inventory.getStackInSlot(i);
if (is.getItem() instanceof ItemBlock) {
foundSlot = true;
player.inventory.currentItem = i;
break;
}
}
}
// give up and select whatever is right of the current slot
if (!foundSlot) {
player.inventory.currentItem = (player.inventory.currentItem + 1) % 9;
}
final boolean guiOpened = service.getAvailableAdjacent().neighbors().keySet().stream().anyMatch(neighbor -> {
if (neighbor.canHandleItems() || SimpleServiceLocator.craftingRecipeProviders.stream().anyMatch(provider -> provider.canOpenGui(neighbor.getTileEntity()))) {
final BlockPos pos = neighbor.getTileEntity().getPos();
IBlockState blockState = worldProvider.getWorld().getBlockState(pos);
return !blockState.getBlock().isAir(blockState, worldProvider.getWorld(), pos) && blockState.getBlock().onBlockActivated(worldProvider.getWorld(), pos, neighbor.getTileEntity().getWorld().getBlockState(pos), player, EnumHand.MAIN_HAND, EnumFacing.UP, 0, 0, 0);
} else {
return false;
}
});
if (!guiOpened) {
LogisticsPipes.log.warn("Ignored open attached GUI request at " + player.world + " @ " + getBlockPos());
}
player.inventory.currentItem = savedEquipped;
return guiOpened;
}
use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleCrafter method canProvide.
@Override
public void canProvide(RequestTreeNode tree, RequestTree root, List<IFilter> filters) {
final IPipeServiceProvider service = _service;
if (service == null)
return;
if (!service.getItemOrderManager().hasExtras() || tree.hasBeenQueried(service.getItemOrderManager())) {
return;
}
IResource requestedItem = tree.getRequestType();
if (!canCraft(requestedItem)) {
return;
}
for (IFilter filter : filters) {
if (filter.isBlocked() == filter.isFilteredItem(requestedItem) || filter.blockProvider()) {
return;
}
}
int remaining = 0;
for (LogisticsItemOrder extra : service.getItemOrderManager()) {
if (extra.getType() == ResourceType.EXTRA) {
if (extra.getResource().getItem().equals(requestedItem.getAsItem())) {
remaining += extra.getResource().stack.getStackSize();
}
}
}
final ItemIdentifierStack craftedItem = getCraftedItem();
if (craftedItem == null)
return;
remaining -= root.getAllPromissesFor(this, craftedItem.getItem());
if (remaining < 1) {
return;
}
if (getUpgradeManager().isFuzzyUpgrade() && outputFuzzy().nextSetBit(0) != -1) {
DictResource dict = new DictResource(craftedItem, null).loadFromBitSet(outputFuzzy().copyValue());
LogisticsExtraDictPromise promise = new LogisticsExtraDictPromise(dict, Math.min(remaining, tree.getMissingAmount()), this, true);
tree.addPromise(promise);
} else {
LogisticsExtraPromise promise = new LogisticsExtraPromise(craftedItem.getItem(), Math.min(remaining, tree.getMissingAmount()), this, true);
tree.addPromise(promise);
}
tree.setQueried(service.getItemOrderManager());
}
use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleCrafter method registerExtras.
@Override
public void registerExtras(IPromise promise) {
final IPipeServiceProvider service = _service;
if (service == null)
return;
if (promise instanceof LogisticsDictPromise) {
service.getItemOrderManager().addExtra(((LogisticsDictPromise) promise).getResource());
} else {
ItemIdentifierStack stack = new ItemIdentifierStack(promise.getItemType(), promise.getAmount());
service.getItemOrderManager().addExtra(new DictResource(stack, null));
}
}
use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleCrafter method spaceFor.
protected int spaceFor(@Nonnull ItemStack stack, ItemIdentifier item, boolean includeInTransit) {
Pair<String, ItemIdentifier> key = new Pair<>("spaceFor", item);
final IPipeServiceProvider service = _service;
if (service == null)
return 0;
Object cache = service.getCacheHolder().getCacheFor(CacheTypes.Inventory, key);
int onRoute = 0;
if (includeInTransit) {
onRoute = service.countOnRoute(item);
}
if (cache != null) {
return ((Integer) cache) - onRoute;
}
if (includeInTransit) {
stack = stack.copy();
stack.grow(onRoute);
}
final ISlotUpgradeManager upgradeManager = Objects.requireNonNull(getUpgradeManager());
final ItemStack finalStack = stack;
final Integer count = AdjacentUtilKt.sneakyInventoryUtils(service.getAvailableAdjacent(), upgradeManager).stream().map(invUtil -> invUtil.roomForItem(finalStack)).reduce(Integer::sum).orElse(0);
service.getCacheHolder().setCache(CacheTypes.Inventory, key, count);
return count - onRoute;
}
use of logisticspipes.interfaces.IPipeServiceProvider in project LogisticsPipes by RS485.
the class ModuleCrafter method fullFill.
@Override
public LogisticsItemOrder fullFill(LogisticsPromise promise, IRequestItems destination, IAdditionalTargetInformation info) {
final IPipeServiceProvider service = _service;
if (service == null)
return null;
if (promise instanceof LogisticsExtraDictPromise) {
service.getItemOrderManager().removeExtras(((LogisticsExtraDictPromise) promise).getResource());
}
if (promise instanceof LogisticsExtraPromise) {
service.getItemOrderManager().removeExtras(new DictResource(new ItemIdentifierStack(promise.item, promise.numberOfItems), null));
}
if (promise instanceof LogisticsDictPromise) {
service.spawnParticle(Particles.WhiteParticle, 2);
return service.getItemOrderManager().addOrder(((LogisticsDictPromise) promise).getResource(), destination, ResourceType.CRAFTING, info);
}
service.spawnParticle(Particles.WhiteParticle, 2);
return service.getItemOrderManager().addOrder(new ItemIdentifierStack(promise.item, promise.numberOfItems), destination, ResourceType.CRAFTING, info);
}
Aggregations