use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.
the class CompoundIngredient method getItems.
@Override
@Nonnull
public ItemStack[] getItems() {
if (stacks == null) {
List<ItemStack> tmp = Lists.newArrayList();
for (Ingredient child : children) Collections.addAll(tmp, child.getItems());
stacks = tmp.toArray(new ItemStack[tmp.size()]);
}
return stacks;
}
use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.
the class ForgeHooksClient method gatherTooltipComponents.
public static List<ClientTooltipComponent> gatherTooltipComponents(ItemStack stack, List<? extends FormattedText> textElements, Optional<TooltipComponent> itemComponent, int mouseX, int screenWidth, int screenHeight, @Nullable Font forcedFont, Font fallbackFont) {
Font font = getTooltipFont(forcedFont, stack, fallbackFont);
List<Either<FormattedText, TooltipComponent>> elements = textElements.stream().map((Function<FormattedText, Either<FormattedText, TooltipComponent>>) Either::left).collect(Collectors.toCollection(ArrayList::new));
itemComponent.ifPresent(c -> elements.add(1, Either.right(c)));
var event = new RenderTooltipEvent.GatherComponents(stack, screenWidth, screenHeight, elements, -1);
MinecraftForge.EVENT_BUS.post(event);
if (event.isCanceled())
return List.of();
// text wrapping
int tooltipTextWidth = event.getTooltipElements().stream().mapToInt(either -> either.map(font::width, component -> 0)).max().orElse(0);
boolean needsWrap = false;
int tooltipX = mouseX + 12;
if (tooltipX + tooltipTextWidth + 4 > screenWidth) {
tooltipX = mouseX - 16 - tooltipTextWidth;
if (// if the tooltip doesn't fit on the screen
tooltipX < 4) {
if (mouseX > screenWidth / 2)
tooltipTextWidth = mouseX - 12 - 8;
else
tooltipTextWidth = screenWidth - 16 - mouseX;
needsWrap = true;
}
}
if (event.getMaxWidth() > 0 && tooltipTextWidth > event.getMaxWidth()) {
tooltipTextWidth = event.getMaxWidth();
needsWrap = true;
}
int tooltipTextWidthF = tooltipTextWidth;
if (needsWrap) {
return event.getTooltipElements().stream().flatMap(either -> either.map(text -> font.split(text, tooltipTextWidthF).stream().map(ClientTooltipComponent::create), component -> Stream.of(ClientTooltipComponent.create(component)))).toList();
}
return event.getTooltipElements().stream().map(either -> either.map(text -> ClientTooltipComponent.create(text instanceof Component ? ((Component) text).getVisualOrderText() : Language.getInstance().getVisualOrder(text)), ClientTooltipComponent::create)).toList();
}
use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.
the class DispenseFluidContainer method fillContainer.
/**
* Picks up fluid in front of a Dispenser and fills a container with it.
*/
@Nonnull
private ItemStack fillContainer(@Nonnull BlockSource source, @Nonnull ItemStack stack) {
Level world = source.getLevel();
Direction dispenserFacing = source.getBlockState().getValue(DispenserBlock.FACING);
BlockPos blockpos = source.getPos().relative(dispenserFacing);
FluidActionResult actionResult = FluidUtil.tryPickUpFluid(stack, null, world, blockpos, dispenserFacing.getOpposite());
ItemStack resultStack = actionResult.getResult();
if (!actionResult.isSuccess() || resultStack.isEmpty()) {
return super.execute(source, stack);
}
if (stack.getCount() == 1) {
return resultStack;
} else if (((DispenserBlockEntity) source.getEntity()).addItem(resultStack) < 0) {
this.dispenseBehavior.dispense(source, resultStack);
}
ItemStack stackCopy = stack.copy();
stackCopy.shrink(1);
return stackCopy;
}
use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.
the class FluidUtil method interactWithFluidHandler.
/**
* Used to handle the common case of a player holding a fluid item and right-clicking on a fluid handler.
* First it tries to fill the item from the handler,
* if that action fails then it tries to drain the item into the handler.
* Automatically updates the item in the player's hand and stashes any extra items created.
*
* @param player The player doing the interaction between the item and fluid handler.
* @param hand The player's hand that is holding an item that should interact with the fluid handler.
* @param handler The fluid handler.
* @return true if the interaction succeeded and updated the item held by the player, false otherwise.
*/
public static boolean interactWithFluidHandler(@Nonnull Player player, @Nonnull InteractionHand hand, @Nonnull IFluidHandler handler) {
Preconditions.checkNotNull(player);
Preconditions.checkNotNull(hand);
Preconditions.checkNotNull(handler);
ItemStack heldItem = player.getItemInHand(hand);
if (!heldItem.isEmpty()) {
return player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).map(playerInventory -> {
FluidActionResult fluidActionResult = tryFillContainerAndStow(heldItem, handler, playerInventory, Integer.MAX_VALUE, player, true);
if (!fluidActionResult.isSuccess()) {
fluidActionResult = tryEmptyContainerAndStow(heldItem, handler, playerInventory, Integer.MAX_VALUE, player, true);
}
if (fluidActionResult.isSuccess()) {
player.setItemInHand(hand, fluidActionResult.getResult());
return true;
}
return false;
}).orElse(false);
}
return false;
}
use of net.minecraft.world.item.ItemStack in project MinecraftForge by MinecraftForge.
the class RecipeWrapper method removeItemNoUpdate.
/**
* Removes the stack contained in this slot from the underlying handler, and returns it.
*/
@Override
public ItemStack removeItemNoUpdate(int index) {
ItemStack s = getItem(index);
if (s.isEmpty())
return ItemStack.EMPTY;
setItem(index, ItemStack.EMPTY);
return s;
}
Aggregations