Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: protect against crashes from other mods when trying to build tooltip #738

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Fixed External Fluid not connecting properly to fluid storages.
- Fixed Interface filter not respecting maximum stack size of a resource.
- Fixed potential crash when trying to build cable shapes.
- Fixed storage disk upgrade recipes not showing properly in recipe viewers.
- Protect against crashes from other mods when trying to build the cached Grid tooltip.

## [2.0.0-milestone.4.10] - 2024-11-24

### Added
Expand All @@ -20,13 +28,6 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- The Autocrafting Monitor now has a sidebar with all tasks instead of using tabs.
- The auto-selected search box mode is now a global option used in the Autocrafter Manager as well.

### Fixed

- Fixed External Fluid not connecting properly to fluid storages.
- Fixed Interface filter not respecting maximum stack size of a resource.
- Fixed potential crash when trying to build cable shapes.
- Fixed storage disk upgrade recipes not showing properly in recipe viewers.

### Removed

- Block of Quartz Enriched Iron (has been moved to addon mod)
Expand Down
1 change: 0 additions & 1 deletion config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
<property name="ignoreConstructorParameter" value="true"/>
<property name="ignoreSetter" value="true"/>
</module>
<module name="IllegalCatch"/>
<module name="IllegalInstantiation"/>
<module name="IllegalThrows"/>
<module name="IllegalToken"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractItemGridResourceFactory implements GridResourceFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractItemGridResourceFactory.class);

@Override
public Optional<GridResource> apply(final ResourceKey resource, final boolean autocraftable) {
if (!(resource instanceof ItemResource itemResource)) {
Expand Down Expand Up @@ -46,11 +50,16 @@ public Optional<GridResource> apply(final ResourceKey resource, final boolean au
}

private String getTooltip(final ItemStack itemStack) {
return itemStack
.getTooltipLines(Item.TooltipContext.EMPTY, null, TooltipFlag.ADVANCED)
.stream()
.map(Component::getString)
.collect(Collectors.joining("\n"));
try {
return itemStack
.getTooltipLines(Item.TooltipContext.EMPTY, null, TooltipFlag.ADVANCED)
.stream()
.map(Component::getString)
.collect(Collectors.joining("\n"));
} catch (final Throwable t) {
LOGGER.warn("Failed to get tooltip for item {}", itemStack, t);
return "";
}
}

private Set<String> getTags(final Item item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.refinedmods.refinedstorage.common.support.resource.ResourceTypes;
import com.refinedmods.refinedstorage.common.support.tooltip.MouseClientTooltipComponent;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -28,11 +29,15 @@
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.format;
import static com.refinedmods.refinedstorage.common.util.IdentifierUtil.formatWithUnits;

public class ItemGridResource extends AbstractPlatformGridResource<ItemResource> {
private static final Logger LOGGER = LoggerFactory.getLogger(ItemGridResource.class);

private final int id;
private final ItemStack itemStack;
private final ItemResource itemResource;
Expand Down Expand Up @@ -106,8 +111,12 @@ public void onScroll(final GridScrollMode scrollMode, final GridScrollingStrateg
@Override
public void render(final GuiGraphics graphics, final int x, final int y) {
final Font font = Minecraft.getInstance().font;
graphics.renderItem(itemStack, x, y);
graphics.renderItemDecorations(font, itemStack, x, y, null);
try {
graphics.renderItem(itemStack, x, y);
graphics.renderItemDecorations(font, itemStack, x, y, null);
} catch (final Throwable t) {
LOGGER.warn("Failed to render item {}", itemStack, t);
}
}

@Override
Expand All @@ -128,7 +137,12 @@ public boolean belongsToResourceType(final ResourceType resourceType) {
@Override
public List<Component> getTooltip() {
final Minecraft minecraft = Minecraft.getInstance();
return Screen.getTooltipFromItem(minecraft, itemStack);
try {
return Screen.getTooltipFromItem(minecraft, itemStack);
} catch (final Throwable t) {
LOGGER.warn("Failed to get tooltip for item {}", itemStack, t);
return Collections.emptyList();
}
}

@Override
Expand Down
Loading