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.
This commit is contained in:
tyra 2017-08-04 21:17:35 +02:00 committed by yueh
parent 5abb8bf886
commit 8d5cc916e0
4 changed files with 213 additions and 4 deletions

View file

@ -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

View file

@ -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 <http://www.gnu.org/licenses/lgpl>.
*/
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<PatternIngredient> outputs = new ArrayList<>();
private final List<PatternIngredient> 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<PatternIngredient> getOutputs()
{
return this.outputs;
}
public List<PatternIngredient> 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;
}
}
}

View file

@ -85,7 +85,13 @@ public class PatternHelper implements ICraftingPatternDetails, Comparable<Patter
for( int x = 0; x < inTag.tagCount(); x++ )
{
final ItemStack gs = new ItemStack( inTag.getCompoundTagAt( x ) );
NBTTagCompound ingredient = inTag.getCompoundTagAt( x );
final ItemStack gs = new ItemStack( ingredient );
if( !ingredient.hasNoTags() && gs.isEmpty() )
{
throw new IllegalArgumentException( "No pattern here!" );
}
this.crafting.setInventorySlotContents( x, gs );
@ -119,7 +125,13 @@ public class PatternHelper implements ICraftingPatternDetails, Comparable<Patter
for( int x = 0; x < outTag.tagCount(); x++ )
{
final ItemStack gs = new ItemStack( outTag.getCompoundTagAt( x ) );
NBTTagCompound resultItemTag = outTag.getCompoundTagAt( x );
final ItemStack gs = new ItemStack( resultItemTag );
if( !resultItemTag.hasNoTags() && gs.isEmpty() )
{
throw new IllegalArgumentException( "No pattern here!" );
}
if( !gs.isEmpty() )
{

View file

@ -43,6 +43,7 @@ import appeng.api.networking.crafting.ICraftingPatternDetails;
import appeng.api.storage.data.IAEItemStack;
import appeng.core.AppEng;
import appeng.core.localization.GuiText;
import appeng.helpers.InvalidPatternHelper;
import appeng.helpers.PatternHelper;
import appeng.items.AEBaseItem;
import appeng.util.Platform;
@ -108,10 +109,49 @@ public class ItemEncodedPattern extends AEBaseItem implements ICraftingPatternIt
if( details == null )
{
lines.add( TextFormatting.RED + GuiText.InvalidPattern.getLocal() );
if( !stack.hasTagCompound() )
{
return;
}
stack.setStackDisplayName( TextFormatting.RED + GuiText.InvalidPattern.getLocal() );
InvalidPatternHelper invalid = new InvalidPatternHelper( stack );
final String label = ( invalid.isCraftable() ? GuiText.Crafts.getLocal() : GuiText.Creates.getLocal() ) + ": ";
final String and = ' ' + GuiText.And.getLocal() + ' ';
final String with = GuiText.With.getLocal() + ": ";
boolean first = true;
for( final InvalidPatternHelper.PatternIngredient output : invalid.getOutputs() )
{
lines.add( ( first ? label : and ) + output.getFormattedToolTip() );
first = false;
}
first = true;
for( final InvalidPatternHelper.PatternIngredient input : invalid.getInputs() )
{
lines.add( ( first ? with : and ) + input.getFormattedToolTip() );
first = false;
}
if( invalid.isCraftable() )
{
final String substitutionLabel = GuiText.Substitute.getLocal() + " ";
final String canSubstitute = invalid.canSubstitute() ? GuiText.Yes.getLocal() : GuiText.No.getLocal();
lines.add( substitutionLabel + canSubstitute );
}
return;
}
if( stack.hasDisplayName() )
{
stack.removeSubCompound( "display" );
}
final boolean isCrafting = details.isCraftable();
final boolean substitute = details.canSubstitute();