From 8d5cc916e09d9f248c4a845b64ffebf84a760e50 Mon Sep 17 00:00:00 2001 From: tyra Date: Fri, 4 Aug 2017 21:17:35 +0200 Subject: [PATCH] Adds improved tooltip to invalid encodedPattern (#2992) * Adds improved tooltip to invalid encodedPattern This also fixes that encodedPattern might be shown as valid, even though one or more input or output item is missing. --- .../ItemEncodedPatternBakedModel.java | 2 +- .../appeng/helpers/InvalidPatternHelper.java | 157 ++++++++++++++++++ .../java/appeng/helpers/PatternHelper.java | 16 +- .../appeng/items/misc/ItemEncodedPattern.java | 42 ++++- 4 files changed, 213 insertions(+), 4 deletions(-) create mode 100644 src/main/java/appeng/helpers/InvalidPatternHelper.java diff --git a/src/main/java/appeng/client/render/crafting/ItemEncodedPatternBakedModel.java b/src/main/java/appeng/client/render/crafting/ItemEncodedPatternBakedModel.java index 951c04938..7410bf671 100644 --- a/src/main/java/appeng/client/render/crafting/ItemEncodedPatternBakedModel.java +++ b/src/main/java/appeng/client/render/crafting/ItemEncodedPatternBakedModel.java @@ -227,7 +227,7 @@ class ItemEncodedPatternBakedModel implements IBakedModel { ItemEncodedPattern iep = (ItemEncodedPattern) stack.getItem(); ItemStack output = iep.getOutput( stack ); - if( output != null ) + if( !output.isEmpty() ) { IBakedModel realModel = Minecraft.getMinecraft().getRenderItem().getItemModelMesher().getItemModel( output ); // Give the item model a chance to handle the overrides as well diff --git a/src/main/java/appeng/helpers/InvalidPatternHelper.java b/src/main/java/appeng/helpers/InvalidPatternHelper.java new file mode 100644 index 000000000..b0ff460e2 --- /dev/null +++ b/src/main/java/appeng/helpers/InvalidPatternHelper.java @@ -0,0 +1,157 @@ +/* + * This file is part of Applied Energistics 2. + * Copyright (c) 2017, tyra314, All rights reserved. + * + * Applied Energistics 2 is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Applied Energistics 2 is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Applied Energistics 2. If not, see . + */ + +package appeng.helpers; + + +import java.util.ArrayList; +import java.util.List; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.util.text.TextFormatting; + +import appeng.util.Platform; + + +public class InvalidPatternHelper +{ + + private final List outputs = new ArrayList<>(); + private final List inputs = new ArrayList<>(); + private final boolean isCrafting; + private final boolean canSubstitute; + + public InvalidPatternHelper( final ItemStack is ) + { + final NBTTagCompound encodedValue = is.getTagCompound(); + + if( encodedValue == null ) + { + throw new IllegalArgumentException( "No pattern here!" ); + } + + final NBTTagList inTag = encodedValue.getTagList( "in", 10 ); + final NBTTagList outTag = encodedValue.getTagList( "out", 10 ); + this.isCrafting = encodedValue.getBoolean( "crafting" ); + + this.canSubstitute = this.isCrafting && encodedValue.getBoolean( "substitute" ); + + for( int i = 0; i < outTag.tagCount(); i++ ) + { + outputs.add( new PatternIngredient( outTag.getCompoundTagAt( i ) ) ); + } + + for( int i = 0; i < inTag.tagCount(); i++ ) + { + NBTTagCompound in = inTag.getCompoundTagAt( i ); + + // skip empty slots in the crafting grid + if( in.hasNoTags() ) + { + continue; + } + + inputs.add( new PatternIngredient( in ) ); + } + } + + public List getOutputs() + { + return this.outputs; + } + + public List getInputs() + { + return this.inputs; + } + + public boolean isCraftable() + { + return this.isCrafting; + } + + public boolean canSubstitute() + { + return this.canSubstitute; + } + + public class PatternIngredient + { + private String id; + private int count; + private int damage; + + private ItemStack stack; + + public PatternIngredient( NBTTagCompound tag ) + { + this.stack = new ItemStack( tag ); + + if( stack.isEmpty() ) + { + this.id = tag.getString( "id" ); + this.count = tag.getByte( "Count" ); + this.damage = Math.max( 0, tag.getShort( "Damage" ) ); + } + } + + public boolean isValid() + { + return !stack.isEmpty(); + } + + public String getName() + { + return isValid() ? Platform.getItemDisplayName( stack ) : id + '@' + String.valueOf( getDamage() ); + } + + public int getDamage() + { + return isValid() ? stack.getItemDamage() : damage; + } + + public int getCount() + { + return isValid() ? stack.getCount() : count; + } + + public ItemStack getItem() + { + if( !isValid() ) + { + throw new IllegalArgumentException( "There is no valid ItemStack for this PatternIngredient" ); + } + + return stack; + } + + public String getFormattedToolTip() + { + String result = String.valueOf( getCount() ) + ' ' + getName(); + + if( !isValid() ) + { + result = TextFormatting.RED + ( ' ' + result ); + } + + return result; + } + } +} diff --git a/src/main/java/appeng/helpers/PatternHelper.java b/src/main/java/appeng/helpers/PatternHelper.java index 3ba938fcd..6a1d9ab86 100644 --- a/src/main/java/appeng/helpers/PatternHelper.java +++ b/src/main/java/appeng/helpers/PatternHelper.java @@ -85,7 +85,13 @@ public class PatternHelper implements ICraftingPatternDetails, Comparable