Applied-Energistics-2-tiler.../src/main/java/appeng/client/me/ItemRepo.java

290 lines
6.7 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, 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>.
*/
2014-09-24 02:26:27 +02:00
package appeng.client.me;
2014-09-24 02:26:27 +02:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
2014-09-24 02:26:27 +02:00
import net.minecraft.item.ItemStack;
2015-12-24 02:07:03 +01:00
2014-09-24 02:26:27 +02:00
import appeng.api.AEApi;
import appeng.api.config.SearchBoxMode;
import appeng.api.config.Settings;
import appeng.api.config.SortOrder;
import appeng.api.config.ViewItems;
import appeng.api.config.YesNo;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.client.gui.widgets.IScrollSource;
import appeng.client.gui.widgets.ISortSource;
import appeng.core.AEConfig;
2016-10-04 01:34:29 +02:00
import appeng.integration.modules.JEI;
2014-09-24 02:26:27 +02:00
import appeng.items.storage.ItemViewCell;
import appeng.util.ItemSorters;
import appeng.util.Platform;
2016-11-01 16:15:05 +01:00
import appeng.util.prioritylist.IPartitionList;
2014-09-24 02:26:27 +02:00
2014-09-24 02:26:27 +02:00
public class ItemRepo
{
private final IItemList<IAEItemStack> list = AEApi.instance().storage().createItemList();
private final ArrayList<IAEItemStack> view = new ArrayList<IAEItemStack>();
private final ArrayList<ItemStack> dsp = new ArrayList<ItemStack>();
private final IScrollSource src;
private final ISortSource sortSrc;
2014-09-24 02:26:27 +02:00
private int rowSize = 9;
2014-09-24 02:26:27 +02:00
private String searchString = "";
private IPartitionList<IAEItemStack> myPartitionList;
2014-09-24 02:26:27 +02:00
private String innerSearch = "";
2016-10-04 01:34:29 +02:00
private String jeiSearch = null;
private boolean hasPower;
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
public ItemRepo( final IScrollSource src, final ISortSource sortSrc )
2014-09-24 02:26:27 +02:00
{
this.src = src;
this.sortSrc = sortSrc;
}
public IAEItemStack getReferenceItem( int idx )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
idx += this.src.getCurrentScroll() * this.rowSize;
2014-09-24 02:26:27 +02:00
if( idx >= this.view.size() )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return null;
2015-04-29 02:30:53 +02:00
}
2014-12-29 15:13:47 +01:00
return this.view.get( idx );
2014-09-24 02:26:27 +02:00
}
public ItemStack getItem( int idx )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
idx += this.src.getCurrentScroll() * this.rowSize;
2014-09-24 02:26:27 +02:00
if( idx >= this.dsp.size() )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
return null;
2015-04-29 02:30:53 +02:00
}
2014-12-29 15:13:47 +01:00
return this.dsp.get( idx );
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
void setSearch( final String search )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.searchString = search == null ? "" : search;
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
public void postUpdate( final IAEItemStack is )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final IAEItemStack st = this.list.findPrecise( is );
2014-09-24 02:26:27 +02:00
if( st != null )
2014-09-24 02:26:27 +02:00
{
st.reset();
st.add( is );
}
else
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.list.add( is );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
public void setViewCell( final ItemStack[] list )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.myPartitionList = ItemViewCell.createFilter( list );
this.updateView();
2014-09-24 02:26:27 +02:00
}
public void updateView()
{
2014-12-29 15:13:47 +01:00
this.view.clear();
this.dsp.clear();
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.view.ensureCapacity( this.list.size() );
this.dsp.ensureCapacity( this.list.size() );
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
final Enum viewMode = this.sortSrc.getSortDisplay();
final Enum searchMode = AEConfig.instance.settings.getSetting( Settings.SEARCH_MODE );
2016-10-04 01:34:29 +02:00
if( searchMode == SearchBoxMode.JEI_AUTOSEARCH || searchMode == SearchBoxMode.JEI_MANUAL_SEARCH )
2015-04-29 02:30:53 +02:00
{
2016-10-04 01:34:29 +02:00
this.updateJEI( this.searchString );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.innerSearch = this.searchString;
2015-09-30 14:24:40 +02:00
final boolean terminalSearchToolTips = AEConfig.instance.settings.getSetting( Settings.SEARCH_TOOLTIPS ) != YesNo.NO;
2015-01-01 22:13:10 +01:00
// boolean terminalSearchMods = Configuration.INSTANCE.settings.getSetting( Settings.SEARCH_MODS ) != YesNo.NO;
2014-09-24 02:26:27 +02:00
boolean searchMod = false;
if( this.innerSearch.startsWith( "@" ) )
2014-09-24 02:26:27 +02:00
{
searchMod = true;
2014-12-29 15:13:47 +01:00
this.innerSearch = this.innerSearch.substring( 1 );
2014-09-24 02:26:27 +02:00
}
Pattern m = null;
try
{
2014-12-29 15:13:47 +01:00
m = Pattern.compile( this.innerSearch.toLowerCase(), Pattern.CASE_INSENSITIVE );
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
catch( final Throwable ignore )
2014-09-24 02:26:27 +02:00
{
try
{
2014-12-29 15:13:47 +01:00
m = Pattern.compile( Pattern.quote( this.innerSearch.toLowerCase() ), Pattern.CASE_INSENSITIVE );
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
catch( final Throwable __ )
2014-09-24 02:26:27 +02:00
{
return;
}
}
boolean notDone = false;
for( IAEItemStack is : this.list )
2014-09-24 02:26:27 +02:00
{
if( this.myPartitionList != null )
2014-09-24 02:26:27 +02:00
{
if( !this.myPartitionList.isListed( is ) )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
continue;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
if( viewMode == ViewItems.CRAFTABLE && !is.isCraftable() )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
continue;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
if( viewMode == ViewItems.CRAFTABLE )
2014-09-24 02:26:27 +02:00
{
is = is.copy();
is.setStackSize( 0 );
}
if( viewMode == ViewItems.STORED && is.getStackSize() == 0 )
2015-04-29 02:30:53 +02:00
{
2014-09-24 02:26:27 +02:00
continue;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
final String dspName = searchMod ? Platform.getModId( is ) : Platform.getItemDisplayName( is );
2014-09-24 02:26:27 +02:00
notDone = true;
if( m.matcher( dspName.toLowerCase() ).find() )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.view.add( is );
2014-09-24 02:26:27 +02:00
notDone = false;
}
if( terminalSearchToolTips && notDone )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
for( final Object lp : Platform.getTooltip( is ) )
2015-04-29 02:30:53 +02:00
{
2015-05-09 00:00:52 +02:00
if( lp instanceof String && m.matcher( (CharSequence) lp ).find() )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.view.add( is );
2014-09-24 02:26:27 +02:00
notDone = false;
break;
}
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
/*
* if ( terminalSearchMods && notDone ) { if ( m.matcher( Platform.getMod( is.getItemStack() ) ).find() ) {
* view.add( is ); notDone = false; } }
*/
}
2015-09-30 14:24:40 +02:00
final Enum SortBy = this.sortSrc.getSortBy();
final Enum SortDir = this.sortSrc.getSortDir();
2014-09-24 02:26:27 +02:00
ItemSorters.setDirection( (appeng.api.config.SortDir) SortDir );
2014-09-24 02:26:27 +02:00
ItemSorters.init();
if( SortBy == SortOrder.MOD )
2015-04-29 02:30:53 +02:00
{
2015-01-01 22:13:10 +01:00
Collections.sort( this.view, ItemSorters.CONFIG_BASED_SORT_BY_MOD );
2015-04-29 02:30:53 +02:00
}
else if( SortBy == SortOrder.AMOUNT )
2015-04-29 02:30:53 +02:00
{
2015-01-01 22:13:10 +01:00
Collections.sort( this.view, ItemSorters.CONFIG_BASED_SORT_BY_SIZE );
2015-04-29 02:30:53 +02:00
}
else if( SortBy == SortOrder.INVTWEAKS )
2015-04-29 02:30:53 +02:00
{
2015-01-01 22:13:10 +01:00
Collections.sort( this.view, ItemSorters.CONFIG_BASED_SORT_BY_INV_TWEAKS );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
else
2015-04-29 02:30:53 +02:00
{
2015-01-01 22:13:10 +01:00
Collections.sort( this.view, ItemSorters.CONFIG_BASED_SORT_BY_NAME );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
for( final IAEItemStack is : this.view )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.dsp.add( is.getItemStack() );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
2016-10-04 01:34:29 +02:00
private void updateJEI( String filter )
{
2016-10-04 01:34:29 +02:00
JEI.instance.getJei().setSearchText( filter );
}
2014-09-24 02:26:27 +02:00
public int size()
{
2014-12-29 15:13:47 +01:00
return this.view.size();
2014-09-24 02:26:27 +02:00
}
public void clear()
{
2014-12-29 15:13:47 +01:00
this.list.resetStatus();
2014-09-24 02:26:27 +02:00
}
public boolean hasPower()
{
2014-12-29 15:13:47 +01:00
return this.hasPower;
2014-09-24 02:26:27 +02:00
}
2015-09-30 14:24:40 +02:00
public void setPower( final boolean hasPower )
2014-09-24 02:26:27 +02:00
{
this.hasPower = hasPower;
}
public int getRowSize()
{
return this.rowSize;
}
public void setRowSize( final int rowSize )
{
this.rowSize = rowSize;
}
public String getSearchString()
{
return this.searchString;
}
public void setSearchString( @Nonnull final String searchString )
{
this.searchString = searchString;
}
2014-09-24 02:26:27 +02:00
}