Fix book pages showing tools of invalid materials
Now only shows the materials being used on valid parts in the display tools, using the first materials for a given type to fill in gaps
Skips a display tool if there are no parts of the material on it
@@ -207,6 +207,21 @@ public class ContentMaterial extends TinkerPage {
return lineData;
}
+ /** Checks if the given material has the given stat type */
+ private static boolean hasStatType(MaterialId materialId, MaterialStatsId statsId) {
+ return MaterialRegistry.getInstance().getMaterialStats(materialId, statsId).isPresent();
+ }
+
+ /** Gets the first material from the registry for the given stat type */
+ private static IMaterial getFirstMaterialWithType(MaterialStatsId statsId) {
+ for (IMaterial material : MaterialRegistry.getMaterials()) {
+ if (hasStatType(material.getIdentifier(), statsId)) {
+ return material;
+ }
+ }
+ return IMaterial.UNKNOWN;
+ }
+
private void addDisplayItems(ArrayList<BookElement> list, int x, MaterialId materialId) {
List<ItemElement> displayTools = Lists.newArrayList();
@@ -258,18 +273,29 @@ public class ContentMaterial extends TinkerPage {
if (item instanceof IModifiable) {
IModifiable tool = ((IModifiable) item);
List<IToolPart> requirements = tool.getToolDefinition().getRequiredComponents();
+ // start building the tool with the given material
List<IMaterial> materials = new ArrayList<>(requirements.size());
- for (int i = 0; i < requirements.size(); i++) {
- materials.add(i, MaterialRegistry.getInstance().getMaterial(materialId));
+ IMaterial material = MaterialRegistry.getMaterial(materialId);
+ boolean usedMaterial = false;
+ for (IToolPart part : requirements) {
+ if (hasStatType(materialId, part.getStatType())) {
+ materials.add(material);
+ usedMaterial = true;
+ } else {
+ materials.add(getFirstMaterialWithType(part.getStatType()));
+ }
}
+
+ // only add a stack if our material showed up
+ if (usedMaterial) {
ItemStack display = ToolBuildHandler.buildItemFromMaterials(tool, materials);
displayTools.add(new TinkerItemElement(display));
-
if (displayTools.size() == 9) {
break;
}
}
}
+ }
// built tools
if (!displayTools.isEmpty()) {
