Made error handling for rendering grid stacks a bit more robust

This commit is contained in:
raoulvdberge
2019-10-28 12:52:14 +01:00
parent db076b7b8d
commit dd76961f8e
3 changed files with 91 additions and 63 deletions

View File

@@ -25,6 +25,8 @@ import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.client.config.GuiCheckBox;
import net.minecraftforge.fml.client.config.GuiUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lwjgl.opengl.GL11;
import javax.annotation.Nonnull;
@@ -36,6 +38,8 @@ public abstract class BaseScreen<T extends Container> extends ContainerScreen<T>
private static final Map<String, ResourceLocation> TEXTURE_CACHE = new HashMap<>();
private static final Map<Class, Queue<Consumer>> ACTIONS = new HashMap<>();
private Logger logger = LogManager.getLogger(getClass());
private int sideButtonY;
public BaseScreen(T container, int xSize, int ySize, PlayerInventory inventory, ITextComponent title) {
@@ -227,39 +231,27 @@ public abstract class BaseScreen<T extends Container> extends ContainerScreen<T>
}
public void renderItem(int x, int y, ItemStack stack) {
renderItem(x, y, stack, false);
renderItem(x, y, stack, false, null);
}
public void renderItem(int x, int y, ItemStack stack, boolean withOverlay) {
renderItem(x, y, stack, withOverlay, null);
}
public void renderItem(int x, int y, ItemStack stack, boolean withOverlay, @Nullable String text) {
public void renderItem(int x, int y, ItemStack stack, boolean overlay, @Nullable String text) {
try {
itemRenderer.zLevel = 200.0F;
try {
itemRenderer.renderItemIntoGUI(stack, x, y);
} catch (Throwable t) {
// NO OP
}
if (withOverlay) {
renderItemOverlay(stack, text, x, y);
}
itemRenderer.zLevel = 0.0F;
}
public void renderItemOverlay(ItemStack stack, @Nullable String text, int x, int y) {
try {
if (overlay) {
this.itemRenderer.renderItemOverlayIntoGUI(font, stack, x, y, "");
} catch (Throwable t) {
// NO OP
}
if (text != null) {
renderQuantity(x, y, text);
}
itemRenderer.zLevel = 0.0F;
} catch (Throwable t) {
logger.warn("Couldn't render stack: " + stack.getItem().toString(), t);
}
}
public void renderQuantity(int x, int y, String qty) {

View File

@@ -7,18 +7,25 @@ import com.raoulvdberge.refinedstorage.screen.BaseScreen;
import net.minecraft.client.resources.I18n;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fluids.FluidStack;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.annotation.Nullable;
import java.util.UUID;
public class FluidGridStack implements IGridStack {
private Logger logger = LogManager.getLogger(getClass());
private UUID id;
private FluidStack stack;
@Nullable
private StorageTrackerEntry entry;
private boolean craftable;
private String modId;
private String modName;
private String cachedName;
private String cachedTooltip;
private String cachedModId;
private String cachedModName;
public FluidGridStack(UUID id, FluidStack stack, @Nullable StorageTrackerEntry entry, boolean craftable) {
this.id = id;
@@ -43,35 +50,45 @@ public class FluidGridStack implements IGridStack {
@Override
public String getName() {
return stack.getDisplayName().getFormattedText();
if (cachedName == null) {
try {
cachedName = stack.getDisplayName().getFormattedText();
} catch (Throwable t) {
logger.warn("Could not retrieve fluid name of " + stack.getFluid().getRegistryName().toString(), t);
cachedName = "<Error>";
}
}
return cachedName;
}
@Override
public String getModId() {
if (modId == null) {
if (cachedModId == null) {
ResourceLocation registryName = stack.getFluid().getRegistryName();
if (registryName != null) {
modId = registryName.getNamespace();
cachedModId = registryName.getNamespace();
} else {
modId = "???";
cachedModId = "<Error>";
}
}
return modId;
return cachedModId;
}
@Override
public String getModName() {
if (modName == null) {
modName = ItemGridStack.getModNameByModId(getModId());
if (cachedModName == null) {
cachedModName = ItemGridStack.getModNameByModId(getModId());
if (modName == null) {
modName = "???";
if (cachedModName == null) {
cachedModName = "<Error>";
}
}
return modName;
return cachedModName;
}
@Override
@@ -82,7 +99,17 @@ public class FluidGridStack implements IGridStack {
@Override
public String getTooltip() {
return stack.getDisplayName().getFormattedText();
if (cachedTooltip == null) {
try {
cachedTooltip = stack.getDisplayName().getFormattedText();
} catch (Throwable t) {
cachedTooltip = "<Error>";
logger.warn("Could not retrieve fluid tooltip of " + stack.getFluid().getRegistryName().toString(), t);
}
}
return cachedTooltip;
}
@Override

View File

@@ -8,6 +8,8 @@ import net.minecraft.client.resources.I18n;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.ModContainer;
import net.minecraftforge.fml.ModList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.annotation.Nullable;
import java.util.Optional;
@@ -15,6 +17,8 @@ import java.util.UUID;
import java.util.stream.Collectors;
public class ItemGridStack implements IGridStack {
private Logger logger = LogManager.getLogger(getClass());
private UUID id;
private ItemStack stack;
private String cachedName;
@@ -22,9 +26,10 @@ public class ItemGridStack implements IGridStack {
private String[] oreIds = null;
@Nullable
private StorageTrackerEntry entry;
private String modId;
private String modName;
private String tooltip;
private String cachedModId;
private String cachedModName;
private String cachedTooltip;
public ItemGridStack(ItemStack stack) {
this.stack = stack;
@@ -60,41 +65,43 @@ public class ItemGridStack implements IGridStack {
@Override
public String getName() {
try {
if (cachedName == null) {
try {
cachedName = stack.getDisplayName().getFormattedText();
} catch (Throwable t) {
logger.warn("Could not retrieve item name of " + stack.getItem().toString(), t);
cachedName = "<Error>";
}
}
return cachedName;
} catch (Throwable t) {
return "";
}
}
@Override
public String getModId() {
if (modId == null) {
modId = stack.getItem().getCreatorModId(stack);
if (cachedModId == null) {
cachedModId = stack.getItem().getCreatorModId(stack);
if (modId == null) {
modId = "???";
if (cachedModId == null) {
cachedModId = "<Error>";
}
}
return modId;
return cachedModId;
}
@Override
public String getModName() {
if (modName == null) {
modName = getModNameByModId(getModId());
if (cachedModName == null) {
cachedModName = getModNameByModId(getModId());
if (modName == null) {
modName = "???";
if (cachedModName == null) {
cachedModName = "<Error>";
}
}
return modName;
return cachedModName;
}
@Override
@@ -113,15 +120,17 @@ public class ItemGridStack implements IGridStack {
@Override
public String getTooltip() {
if (tooltip == null) {
if (cachedTooltip == null) {
try {
tooltip = RenderUtils.getTooltipFromItem(stack).stream().collect(Collectors.joining("\n"));
cachedTooltip = RenderUtils.getTooltipFromItem(stack).stream().collect(Collectors.joining("\n"));
} catch (Throwable t) {
tooltip = "";
logger.warn("Could not retrieve item tooltip of " + stack.getItem().toString(), t);
cachedTooltip = "<Error>";
}
}
return tooltip;
return cachedTooltip;
}
@Override