Alloy furnace - Search box

This commit is contained in:
Emmanuel BENOîT 2016-06-24 16:03:51 +02:00
parent 1970cfe84d
commit 10a0b6cb86
4 changed files with 138 additions and 30 deletions

View file

@ -41,7 +41,6 @@ materials.ores No Tin
materials.ores No Zinc materials.ores No Zinc
------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------
tech.base No Alloy furnace tech.base No Alloy furnace
-> search recipe
-> XP -> XP
-> let output hoppers take empty buckets / invalid input -> let output hoppers take empty buckets / invalid input
(make that configurable) (make that configurable)

View file

@ -135,12 +135,19 @@ public class TBAlloyFurnaceContainer
public void setCurrentRecipe( final ResourceLocation name , final boolean confirm ) public void setCurrentRecipe( final ResourceLocation name , final boolean confirm )
{ {
final MAlloyRecipe recipe = MAlloyRecipe.REGISTRY.getRecipe( name ); final MAlloyRecipe recipe;
if ( recipe == null ) { if ( name == null ) {
return; recipe = null;
} else {
recipe = MAlloyRecipe.REGISTRY.getRecipe( name );
} }
this.recipe.clear( ); this.recipe.clear( );
if ( recipe == null ) {
// XXX log if confirm is set
return;
}
for ( int i = 0 ; i < recipe.inputs.length ; i++ ) { for ( int i = 0 ; i < recipe.inputs.length ; i++ ) {
this.recipe.setInventorySlotContents( i , recipe.inputs[ i ] ); this.recipe.setInventorySlotContents( i , recipe.inputs[ i ] );
} }

View file

@ -9,6 +9,7 @@ import mmm.materials.MAlloyRecipe;
import mmm.utils.URegistry; import mmm.utils.URegistry;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.resources.I18n; import net.minecraft.client.resources.I18n;
@ -99,11 +100,16 @@ public class TBAlloyFurnaceGui
private static final int TAB_ICON_HEIGHT = 16; private static final int TAB_ICON_HEIGHT = 16;
private final TBAlloyFurnaceContainer container; private final TBAlloyFurnaceContainer container;
private TBAlloyFurnaceGui.Tab selectedTab = Tab.MAIN;
private ArrowButton bPrevious; private ArrowButton bPrevious;
private ArrowButton bNext; private ArrowButton bNext;
private GuiButton bConfirm; private GuiButton bConfirm;
private TBAlloyFurnaceGui.Tab selectedTab = Tab.MAIN; private GuiTextField tfSearch;
private ArrayList< MAlloyRecipe > recipes;
private int currentRecipe = 0; private int currentRecipe = 0;
private String searchString = "";
public TBAlloyFurnaceGui( final InventoryPlayer inventoryPlayer , final TBAlloyFurnaceTileEntity tileEntity ) public TBAlloyFurnaceGui( final InventoryPlayer inventoryPlayer , final TBAlloyFurnaceTileEntity tileEntity )
@ -119,7 +125,8 @@ public class TBAlloyFurnaceGui
} }
} }
this.setRecipe( getRecipeIndex( tileEntity.recipe ) ); this.recipes = MAlloyRecipe.REGISTRY.getRecipes( );
this.setRecipe( this.getRecipeIndex( tileEntity.recipe ) );
} }
@ -134,7 +141,15 @@ public class TBAlloyFurnaceGui
this.bPrevious = new ArrowButton( 1 , 8 + x , 47 + y , false ); this.bPrevious = new ArrowButton( 1 , 8 + x , 47 + y , false );
this.bNext = new ArrowButton( 2 , 156 + x , 47 + y , true ); this.bNext = new ArrowButton( 2 , 156 + x , 47 + y , true );
this.bConfirm = new GuiButton( 3 , 24 + x , 84 + y , 128 , 20 , I18n.format( "gui.mmm.select_recipe" ) ); this.bConfirm = new GuiButton( 3 , 24 + x , 84 + y , 128 , 20 , I18n.format( "gui.mmm.select_recipe" ) );
this.tfSearch = new GuiTextField( 4 , this.fontRendererObj , 24 + x , 9 + y , 128 , 20 );
this.tfSearch.setTextColor( 0xffffff );
this.tfSearch.setDisabledTextColour( 0x7f7f7f );
this.tfSearch.setEnableBackgroundDrawing( true );
this.tfSearch.setMaxStringLength( 30 );
this.bPrevious.visible = this.bNext.visible = this.bConfirm.visible = false; this.bPrevious.visible = this.bNext.visible = this.bConfirm.visible = false;
this.tfSearch.setVisible( false );
this.buttonList.add( this.bNext ); this.buttonList.add( this.bNext );
this.buttonList.add( this.bPrevious ); this.buttonList.add( this.bPrevious );
@ -144,34 +159,56 @@ public class TBAlloyFurnaceGui
} }
@Override
public void drawScreen( final int mouseX , final int mouseY , final float partialTicks )
{
super.drawScreen( mouseX , mouseY , partialTicks );
}
private void enableConfigButtons( ) private void enableConfigButtons( )
{ {
if ( this.bNext != null ) { if ( this.bNext != null ) {
boolean visible = ( this.selectedTab == Tab.CONFIG ); final boolean visible = this.selectedTab == Tab.CONFIG;
this.bNext.visible = this.bPrevious.visible = this.bConfirm.visible = visible; this.bNext.visible = this.bPrevious.visible = this.bConfirm.visible = visible;
this.tfSearch.setVisible( visible );
this.bNext.enabled = this.currentRecipe < MAlloyRecipe.REGISTRY.getRecipes( ).size( ) - 1; if ( this.recipes.isEmpty( ) ) {
this.bPrevious.enabled = this.currentRecipe > 0; this.bNext.enabled = this.bPrevious.enabled = this.bConfirm.enabled = false;
this.bConfirm.enabled = ( this.currentRecipe != getRecipeIndex( this.container.tileEntity.recipe ) ); } else {
this.bNext.enabled = this.currentRecipe < this.recipes.size( ) - 1;
this.bPrevious.enabled = this.currentRecipe > 0;
this.bConfirm.enabled = this.currentRecipe != this.getRecipeIndex( this.container.tileEntity.recipe );
}
} }
} }
private void setRecipe( final int index ) private void setRecipe( int index )
{ {
ArrayList< MAlloyRecipe > list = MAlloyRecipe.REGISTRY.getRecipes( ); if ( index == -1 && !this.recipes.isEmpty( ) ) {
MAlloyRecipe recipe = list.get( index ); index = 0;
this.container.setCurrentRecipe( recipe.name , false ); }
URegistry.network.sendToServer( new TBAlloyFurnaceMessage(
( (TBAlloyFurnaceContainer) this.inventorySlots ).position , recipe.name , false ) );
this.currentRecipe = index; this.currentRecipe = index;
ResourceLocation rName;
if ( index != -1 ) {
final MAlloyRecipe recipe = this.recipes.get( index );
rName = recipe.name;
} else {
rName = null;
}
this.container.setCurrentRecipe( rName , false );
URegistry.network.sendToServer( //
new TBAlloyFurnaceMessage( this.container.position , rName , false ) );
this.enableConfigButtons( ); this.enableConfigButtons( );
} }
private static int getRecipeIndex( MAlloyRecipe recipe ) private int getRecipeIndex( final MAlloyRecipe recipe )
{ {
return MAlloyRecipe.REGISTRY.getRecipes( ).indexOf( recipe ); return this.recipes.indexOf( recipe );
} }
@ -181,7 +218,6 @@ public class TBAlloyFurnaceGui
GlStateManager.color( 1f , 1f , 1f , 1f ); GlStateManager.color( 1f , 1f , 1f , 1f );
GlStateManager.enableBlend( ); GlStateManager.enableBlend( );
GlStateManager.disableLighting( ); GlStateManager.disableLighting( );
// RenderHelper.enableGUIStandardItemLighting( );
// Inactive tab // Inactive tab
this.mc.getTextureManager( ).bindTexture( TBAlloyFurnaceGui.TEXTURES[ 0 ] ); this.mc.getTextureManager( ).bindTexture( TBAlloyFurnaceGui.TEXTURES[ 0 ] );
@ -196,24 +232,29 @@ public class TBAlloyFurnaceGui
this.drawTexturedModalRect( this.guiLeft , this.guiTop , 0 , 0 , this.xSize , this.ySize ); this.drawTexturedModalRect( this.guiLeft , this.guiTop , 0 , 0 , this.xSize , this.ySize );
if ( this.selectedTab == Tab.MAIN ) { if ( this.selectedTab == Tab.MAIN ) {
int x = ( this.width - this.xSize ) / 2; final int x = ( this.width - this.xSize ) / 2;
int y = ( this.height - this.ySize ) / 2; final int y = ( this.height - this.ySize ) / 2;
TileEntity atPos = this.container.world.getTileEntity( this.container.position ); final TileEntity atPos = this.container.world.getTileEntity( this.container.position );
if ( atPos instanceof TBAlloyFurnaceTileEntity ) { if ( atPos instanceof TBAlloyFurnaceTileEntity ) {
TBAlloyFurnaceTileEntity te = (TBAlloyFurnaceTileEntity) atPos; final TBAlloyFurnaceTileEntity te = (TBAlloyFurnaceTileEntity) atPos;
// Burn // Burn
if ( te.isBurning( ) ) { if ( te.isBurning( ) ) {
int burn = te.getBurnProgress( 13 ); final int burn = te.getBurnProgress( 13 );
this.drawTexturedModalRect( x + 89 , y + 38 + 13 - burn , 176 , 28 - burn , 14 , burn + 1 ); this.drawTexturedModalRect( x + 89 , y + 38 + 13 - burn , 176 , 28 - burn , 14 , burn + 1 );
} }
// Alloying progress // Alloying progress
if ( te.isAlloying( ) ) { if ( te.isAlloying( ) ) {
int alloy = te.getAlloyingProgress( 47 ); final int alloy = te.getAlloyingProgress( 47 );
this.drawTexturedModalRect( x + 73 , y + 17 , 176 , 29 , alloy + 1 , 16 ); this.drawTexturedModalRect( x + 73 , y + 17 , 176 , 29 , alloy + 1 , 16 );
} }
} }
} else {
GlStateManager.disableBlend( );
this.tfSearch.drawTextBox( );
GlStateManager.enableBlend( );
GlStateManager.color( 1f , 1f , 1f , 1f );
} }
// Active tab // Active tab
@ -243,6 +284,60 @@ public class TBAlloyFurnaceGui
return; return;
} }
} }
if ( this.selectedTab == Tab.CONFIG ) {
this.tfSearch.mouseClicked( mouseX , mouseY , mouseButton );
}
}
@Override
protected void keyTyped( final char typedChar , final int keyCode )
throws IOException
{
if ( this.tfSearch.getVisible( ) && this.tfSearch.textboxKeyTyped( typedChar , keyCode ) ) {
this.handleFiltering( );
} else {
super.keyTyped( typedChar , keyCode );
}
}
private void handleFiltering( )
{
final String newText = this.tfSearch.getText( ).trim( ).toLowerCase( );
if ( this.searchString.equals( newText ) ) {
return;
}
MAlloyRecipe selected = this.currentRecipe == -1 ? null : this.recipes.get( this.currentRecipe );
final ArrayList< MAlloyRecipe > fullList = MAlloyRecipe.REGISTRY.getRecipes( );
this.searchString = newText;
if ( "".equals( newText ) ) {
this.recipes = fullList;
} else {
if ( this.recipes == fullList ) {
this.recipes = new ArrayList<>( );
} else {
this.recipes.clear( );
}
final int nRecipes = fullList.size( );
for ( int i = 0 ; i < nRecipes ; i++ ) {
final MAlloyRecipe recipe = fullList.get( i );
if ( recipe.getLocalizedName( ).toLowerCase( ).contains( newText ) ) {
this.recipes.add( recipe );
}
}
}
if ( selected == null && !this.recipes.isEmpty( ) ) {
this.setRecipe( 0 );
} else if ( selected != null ) {
this.setRecipe( this.getRecipeIndex( selected ) );
} else {
this.setRecipe( -1 );
}
} }
@ -255,8 +350,7 @@ public class TBAlloyFurnaceGui
} else if ( button == this.bPrevious ) { } else if ( button == this.bPrevious ) {
this.setRecipe( this.currentRecipe - 1 ); this.setRecipe( this.currentRecipe - 1 );
} else if ( button == this.bConfirm ) { } else if ( button == this.bConfirm ) {
ArrayList< MAlloyRecipe > list = MAlloyRecipe.REGISTRY.getRecipes( ); final MAlloyRecipe recipe = this.recipes.get( this.currentRecipe );
MAlloyRecipe recipe = list.get( this.currentRecipe );
URegistry.network.sendToServer( URegistry.network.sendToServer(
new TBAlloyFurnaceMessage( this.container.tileEntity.getPos( ) , recipe.name , true ) ); new TBAlloyFurnaceMessage( this.container.tileEntity.getPos( ) , recipe.name , true ) );
this.container.tileEntity.recipe = recipe; this.container.tileEntity.recipe = recipe;

View file

@ -37,7 +37,12 @@ public class TBAlloyFurnaceMessage
public void fromBytes( final ByteBuf buf ) public void fromBytes( final ByteBuf buf )
{ {
this.blockPos = new BlockPos( buf.readInt( ) , buf.readInt( ) , buf.readInt( ) ); this.blockPos = new BlockPos( buf.readInt( ) , buf.readInt( ) , buf.readInt( ) );
this.selected = new ResourceLocation( ByteBufUtils.readUTF8String( buf ) , ByteBufUtils.readUTF8String( buf ) ); if ( buf.readBoolean( ) ) {
this.selected = new ResourceLocation( ByteBufUtils.readUTF8String( buf ) ,
ByteBufUtils.readUTF8String( buf ) );
} else {
this.selected = null;
}
this.confirm = buf.readBoolean( ); this.confirm = buf.readBoolean( );
} }
@ -48,8 +53,11 @@ public class TBAlloyFurnaceMessage
buf.writeInt( this.blockPos.getX( ) ); buf.writeInt( this.blockPos.getX( ) );
buf.writeInt( this.blockPos.getY( ) ); buf.writeInt( this.blockPos.getY( ) );
buf.writeInt( this.blockPos.getZ( ) ); buf.writeInt( this.blockPos.getZ( ) );
ByteBufUtils.writeUTF8String( buf , this.selected.getResourceDomain( ) ); buf.writeBoolean( this.selected != null );
ByteBufUtils.writeUTF8String( buf , this.selected.getResourcePath( ) ); if ( this.selected != null ) {
ByteBufUtils.writeUTF8String( buf , this.selected.getResourceDomain( ) );
ByteBufUtils.writeUTF8String( buf , this.selected.getResourcePath( ) );
}
buf.writeBoolean( this.confirm ); buf.writeBoolean( this.confirm );
} }