Applied-Energistics-2-tiler.../src/main/java/appeng/items/tools/powered/ToolColorApplicator.java

545 lines
14 KiB
Java
Raw Normal View History

2014-11-05 10:21:50 +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>.
*/
package appeng.items.tools.powered;
2014-11-05 10:21:50 +01:00
import java.util.Collections;
2014-07-27 19:12:25 +02:00
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
2014-11-05 10:21:50 +01:00
import java.util.Map;
import java.util.Set;
import net.minecraft.block.Block;
2015-06-16 02:44:59 +02:00
import net.minecraft.block.BlockColored;
import net.minecraft.block.BlockStainedGlass;
import net.minecraft.block.BlockStainedGlassPane;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
2015-06-16 02:44:59 +02:00
import net.minecraft.item.EnumDyeColor;
import net.minecraft.item.ItemSnowball;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumActionResult;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.oredict.OreDictionary;
2015-06-17 23:25:16 +02:00
import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.config.FuzzyMode;
import appeng.api.implementations.items.IItemGroup;
import appeng.api.implementations.items.IStorageCell;
import appeng.api.implementations.tiles.IColorableTile;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.storage.ICellInventory;
import appeng.api.storage.ICellInventoryHandler;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.api.util.AEColor;
import appeng.api.util.DimensionalCoord;
2014-07-29 03:52:40 +02:00
import appeng.block.networking.BlockCableBus;
import appeng.block.paint.BlockPaint;
import appeng.core.AEConfig;
import appeng.core.localization.GuiText;
import appeng.helpers.IMouseWheelItem;
import appeng.hooks.IBlockTool;
import appeng.items.contents.CellConfig;
import appeng.items.contents.CellUpgrades;
import appeng.items.misc.ItemPaintBall;
import appeng.items.tools.powered.powersink.AEBasePoweredItem;
import appeng.me.storage.CellInventoryHandler;
import appeng.tile.misc.TilePaint;
import appeng.util.ItemSorters;
import appeng.util.Platform;
import appeng.util.item.AEItemStack;
2014-11-05 10:21:50 +01:00
public class ToolColorApplicator extends AEBasePoweredItem implements IStorageCell, IItemGroup, IBlockTool, IMouseWheelItem
{
private static final Map<Integer, AEColor> ORE_TO_COLOR = new HashMap<Integer, AEColor>();
static
{
2015-09-30 14:24:40 +02:00
for( final AEColor col : AEColor.values() )
{
2016-09-17 15:02:51 +02:00
if( col == AEColor.TRANSPARENT )
2015-04-29 02:30:53 +02:00
{
continue;
2015-04-29 02:30:53 +02:00
}
2015-01-01 22:13:10 +01:00
ORE_TO_COLOR.put( OreDictionary.getOreID( "dye" + col.name() ), col );
}
2014-09-28 00:50:06 +02:00
}
2014-11-05 10:21:50 +01:00
public ToolColorApplicator()
{
super( AEConfig.instance().getColorApplicatorBattery() );
}
@Override
public EnumActionResult onItemUse( ItemStack is, EntityPlayer p, World w, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ )
{
2015-09-30 14:24:40 +02:00
final Block blk = w.getBlockState( pos ).getBlock();
2014-12-29 15:13:47 +01:00
ItemStack paintBall = this.getColor( is );
2015-09-30 14:24:40 +02:00
final IMEInventory<IAEItemStack> inv = AEApi.instance().registries().cell().getCellInventory( is, null, StorageChannel.ITEMS );
if( inv != null )
{
2015-09-30 14:24:40 +02:00
final IAEItemStack option = inv.extractItems( AEItemStack.create( paintBall ), Actionable.SIMULATE, new BaseActionSource() );
if( option != null )
{
paintBall = option.getItemStack();
2016-12-08 12:42:00 +01:00
paintBall.setCount( 1 );
}
else
2015-04-29 02:30:53 +02:00
{
paintBall = null;
2015-04-29 02:30:53 +02:00
}
2015-06-16 02:44:59 +02:00
if( !Platform.hasPermissions( new DimensionalCoord( w, pos ), p ) )
2015-04-29 02:30:53 +02:00
{
return EnumActionResult.FAIL;
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final double powerPerUse = 100;
if( paintBall != null && paintBall.getItem() instanceof ItemSnowball )
{
2015-09-30 14:24:40 +02:00
final TileEntity te = w.getTileEntity( pos );
// clean cables.
if( te instanceof IColorableTile )
{
2016-09-17 15:02:51 +02:00
if( this.getAECurrentPower( is ) > powerPerUse && ( (IColorableTile) te ).getColor() != AEColor.TRANSPARENT )
{
2016-09-17 15:02:51 +02:00
if( ( (IColorableTile) te ).recolourBlock( side, AEColor.TRANSPARENT, p ) )
{
inv.extractItems( AEItemStack.create( paintBall ), Actionable.MODULATE, new BaseActionSource() );
2014-12-29 15:13:47 +01:00
this.extractAEPower( is, powerPerUse );
return EnumActionResult.SUCCESS;
}
}
}
// clean paint balls..
2015-12-24 02:03:16 +01:00
final Block testBlk = w.getBlockState( pos.offset( side ) ).getBlock();
2015-09-30 14:24:40 +02:00
final TileEntity painted = w.getTileEntity( pos.offset( side ) );
if( this.getAECurrentPower( is ) > powerPerUse && testBlk instanceof BlockPaint && painted instanceof TilePaint )
{
inv.extractItems( AEItemStack.create( paintBall ), Actionable.MODULATE, new BaseActionSource() );
2014-12-29 15:13:47 +01:00
this.extractAEPower( is, powerPerUse );
2015-06-16 02:44:59 +02:00
( (TilePaint) painted ).cleanSide( side.getOpposite() );
return EnumActionResult.SUCCESS;
}
}
else if( paintBall != null )
{
2015-09-30 14:24:40 +02:00
final AEColor color = this.getColorFromItem( paintBall );
if( color != null && this.getAECurrentPower( is ) > powerPerUse )
{
2016-09-17 15:02:51 +02:00
if( color != AEColor.TRANSPARENT && this.recolourBlock( blk, side, w, pos, side, color, p ) )
{
inv.extractItems( AEItemStack.create( paintBall ), Actionable.MODULATE, new BaseActionSource() );
2014-12-29 15:13:47 +01:00
this.extractAEPower( is, powerPerUse );
return EnumActionResult.SUCCESS;
}
}
}
}
if( p.isSneaking() )
{
2014-12-29 15:13:47 +01:00
this.cycleColors( is, paintBall, 1 );
}
return EnumActionResult.FAIL;
}
@Override
2015-09-30 14:24:40 +02:00
public String getItemStackDisplayName( final ItemStack par1ItemStack )
{
String extra = GuiText.Empty.getLocal();
2015-09-30 14:24:40 +02:00
final AEColor selected = this.getActiveColor( par1ItemStack );
if( selected != null && Platform.isClient() )
2015-04-29 02:30:53 +02:00
{
extra = Platform.gui_localize( selected.unlocalizedName );
2015-04-29 02:30:53 +02:00
}
return super.getItemStackDisplayName( par1ItemStack ) + " - " + extra;
}
2015-09-30 14:24:40 +02:00
public AEColor getActiveColor( final ItemStack tol )
{
2014-12-29 15:13:47 +01:00
return this.getColorFromItem( this.getColor( tol ) );
}
private AEColor getColorFromItem( final ItemStack paintBall )
{
if( paintBall == null )
2015-04-29 02:30:53 +02:00
{
return null;
2015-04-29 02:30:53 +02:00
}
if( paintBall.getItem() instanceof ItemSnowball )
2015-04-29 02:30:53 +02:00
{
2016-09-17 15:02:51 +02:00
return AEColor.TRANSPARENT;
2015-04-29 02:30:53 +02:00
}
if( paintBall.getItem() instanceof ItemPaintBall )
{
2015-09-30 14:24:40 +02:00
final ItemPaintBall ipb = (ItemPaintBall) paintBall.getItem();
return ipb.getColor( paintBall );
}
else
{
2015-09-30 14:24:40 +02:00
final int[] id = OreDictionary.getOreIDs( paintBall );
2015-09-30 14:24:40 +02:00
for( final int oreID : id )
{
if( ORE_TO_COLOR.containsKey( oreID ) )
2015-04-29 02:30:53 +02:00
{
2015-01-01 22:13:10 +01:00
return ORE_TO_COLOR.get( oreID );
2015-04-29 02:30:53 +02:00
}
}
}
return null;
}
2015-09-30 14:24:40 +02:00
public ItemStack getColor( final ItemStack is )
{
2015-09-30 14:24:40 +02:00
final NBTTagCompound c = is.getTagCompound();
if( c != null && c.hasKey( "color" ) )
{
2015-09-30 14:24:40 +02:00
final NBTTagCompound color = c.getCompoundTag( "color" );
2016-12-08 12:42:00 +01:00
final ItemStack oldColor = new ItemStack( color );
if( oldColor != null )
2015-04-29 02:30:53 +02:00
{
return oldColor;
2015-04-29 02:30:53 +02:00
}
}
2014-12-29 15:13:47 +01:00
return this.findNextColor( is, null, 0 );
}
2015-09-30 14:24:40 +02:00
private ItemStack findNextColor( final ItemStack is, final ItemStack anchor, final int scrollOffset )
{
ItemStack newColor = null;
2015-09-30 14:24:40 +02:00
final IMEInventory<IAEItemStack> inv = AEApi.instance().registries().cell().getCellInventory( is, null, StorageChannel.ITEMS );
if( inv != null )
{
2015-09-30 14:24:40 +02:00
final IItemList<IAEItemStack> itemList = inv.getAvailableItems( AEApi.instance().storage().createItemList() );
if( anchor == null )
{
2015-09-30 14:24:40 +02:00
final IAEItemStack firstItem = itemList.getFirstItem();
if( firstItem != null )
2015-04-29 02:30:53 +02:00
{
newColor = firstItem.getItemStack();
2015-04-29 02:30:53 +02:00
}
}
else
{
2015-09-30 14:24:40 +02:00
final LinkedList<IAEItemStack> list = new LinkedList<IAEItemStack>();
2015-09-30 14:24:40 +02:00
for( final IAEItemStack i : itemList )
2015-04-29 02:30:53 +02:00
{
list.add( i );
2015-04-29 02:30:53 +02:00
}
Collections.sort( list, new Comparator<IAEItemStack>(){
2014-07-27 19:12:25 +02:00
@Override
2015-09-30 14:24:40 +02:00
public int compare( final IAEItemStack a, final IAEItemStack b )
2014-07-27 19:12:25 +02:00
{
return ItemSorters.compareInt( a.getItemDamage(), b.getItemDamage() );
}
} );
if( list.size() <= 0 )
2015-04-29 02:30:53 +02:00
{
return ItemStack.EMPTY;
2015-04-29 02:30:53 +02:00
}
IAEItemStack where = list.getFirst();
int cycles = 1 + list.size();
while( cycles > 0 && !where.equals( anchor ) )
{
list.addLast( list.removeFirst() );
cycles--;
where = list.getFirst();
}
if( scrollOffset > 0 )
2015-04-29 02:30:53 +02:00
{
list.addLast( list.removeFirst() );
2015-04-29 02:30:53 +02:00
}
if( scrollOffset < 0 )
2015-04-29 02:30:53 +02:00
{
list.addFirst( list.removeLast() );
2015-04-29 02:30:53 +02:00
}
return list.get( 0 ).getItemStack();
}
}
if( newColor != null )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.setColor( is, newColor );
2015-04-29 02:30:53 +02:00
}
return newColor;
}
private void setColor( final ItemStack is, final ItemStack newColor )
{
2015-09-30 14:24:40 +02:00
final NBTTagCompound data = Platform.openNbtData( is );
if( newColor == null )
2015-04-29 02:30:53 +02:00
{
data.removeTag( "color" );
2015-04-29 02:30:53 +02:00
}
else
{
2015-09-30 14:24:40 +02:00
final NBTTagCompound color = new NBTTagCompound();
newColor.writeToNBT( color );
data.setTag( "color", color );
}
}
2015-09-30 14:24:40 +02:00
private boolean recolourBlock( final Block blk, final EnumFacing side, final World w, final BlockPos pos, final EnumFacing orientation, final AEColor newColor, final EntityPlayer p )
{
2015-09-30 14:24:40 +02:00
final IBlockState state = w.getBlockState( pos );
2015-12-24 02:03:16 +01:00
2015-06-16 02:44:59 +02:00
if( blk instanceof BlockColored )
{
2015-12-24 02:03:16 +01:00
final EnumDyeColor color = (EnumDyeColor) state.getValue( BlockColored.COLOR );
2015-06-16 02:44:59 +02:00
if( newColor.dye == color )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
2015-12-24 02:03:16 +01:00
2015-06-16 02:44:59 +02:00
return w.setBlockState( pos, state.withProperty( BlockColored.COLOR, newColor.dye ) );
}
if( blk == Blocks.GLASS )
{
return w.setBlockState( pos, Blocks.STAINED_GLASS.getDefaultState().withProperty( BlockStainedGlass.COLOR, newColor.dye ) );
}
if( blk == Blocks.STAINED_GLASS )
{
2015-12-24 02:03:16 +01:00
final EnumDyeColor color = (EnumDyeColor) state.getValue( BlockStainedGlass.COLOR );
2015-06-16 02:44:59 +02:00
if( newColor.dye == color )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
2015-12-24 02:03:16 +01:00
2015-06-16 02:44:59 +02:00
return w.setBlockState( pos, state.withProperty( BlockStainedGlass.COLOR, newColor.dye ) );
}
if( blk == Blocks.GLASS_PANE )
{
return w.setBlockState( pos, Blocks.STAINED_GLASS_PANE.getDefaultState().withProperty( BlockStainedGlassPane.COLOR, newColor.dye ) );
}
if( blk == Blocks.STAINED_GLASS_PANE )
{
2015-12-24 02:03:16 +01:00
final EnumDyeColor color = (EnumDyeColor) state.getValue( BlockStainedGlassPane.COLOR );
2015-06-16 02:44:59 +02:00
if( newColor.dye == color )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
2015-12-24 02:03:16 +01:00
2015-06-16 02:44:59 +02:00
return w.setBlockState( pos, state.withProperty( BlockStainedGlassPane.COLOR, newColor.dye ) );
}
if( blk == Blocks.HARDENED_CLAY )
{
return w.setBlockState( pos, Blocks.STAINED_HARDENED_CLAY.getDefaultState().withProperty( BlockColored.COLOR, newColor.dye ) );
}
if( blk instanceof BlockCableBus )
2015-04-29 02:30:53 +02:00
{
2015-06-16 02:44:59 +02:00
return ( (BlockCableBus) blk ).recolorBlock( w, pos, side, newColor.dye, p );
2015-04-29 02:30:53 +02:00
}
2014-07-29 03:52:40 +02:00
2015-06-16 02:44:59 +02:00
return blk.recolorBlock( w, pos, side, newColor.dye );
}
2015-09-30 14:24:40 +02:00
public void cycleColors( final ItemStack is, final ItemStack paintBall, final int i )
{
if( paintBall == null )
{
2014-12-29 15:13:47 +01:00
this.setColor( is, this.getColor( is ) );
}
else
{
2014-12-29 15:13:47 +01:00
this.setColor( is, this.findNextColor( is, paintBall, i ) );
}
}
@Override
2015-09-30 14:24:40 +02:00
public void addCheckedInformation( final ItemStack stack, final EntityPlayer player, final List<String> lines, final boolean displayMoreInfo )
{
super.addCheckedInformation( stack, player, lines, displayMoreInfo );
2015-09-30 14:24:40 +02:00
final IMEInventory<IAEItemStack> cdi = AEApi.instance().registries().cell().getCellInventory( stack, null, StorageChannel.ITEMS );
if( cdi instanceof CellInventoryHandler )
{
2015-09-30 14:24:40 +02:00
final ICellInventory cd = ( (ICellInventoryHandler) cdi ).getCellInv();
if( cd != null )
{
lines.add( cd.getUsedBytes() + " " + GuiText.Of.getLocal() + ' ' + cd.getTotalBytes() + ' ' + GuiText.BytesUsed.getLocal() );
lines.add( cd.getStoredItemTypes() + " " + GuiText.Of.getLocal() + ' ' + cd.getTotalItemTypes() + ' ' + GuiText.Types.getLocal() );
}
}
}
@Override
2015-09-30 14:24:40 +02:00
public int getBytes( final ItemStack cellItem )
{
return 512;
}
@Override
2015-09-30 14:24:40 +02:00
public int getBytesPerType( final ItemStack cellItem )
{
return 8;
}
@Override
2015-09-30 14:24:40 +02:00
public int getTotalTypes( final ItemStack cellItem )
{
return 27;
}
@Override
2015-09-30 14:24:40 +02:00
public boolean isBlackListed( final ItemStack cellItem, final IAEItemStack requestedAddition )
{
if( requestedAddition != null )
{
2015-09-30 14:24:40 +02:00
final int[] id = OreDictionary.getOreIDs( requestedAddition.getItemStack() );
2015-09-30 14:24:40 +02:00
for( final int x : id )
{
if( ORE_TO_COLOR.containsKey( x ) )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
}
if( requestedAddition.getItem() instanceof ItemSnowball )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
2014-11-05 10:21:50 +01:00
return !( requestedAddition.getItem() instanceof ItemPaintBall && requestedAddition.getItemDamage() < 20 );
}
return true;
}
@Override
public boolean storableInStorageCell()
{
return true;
}
@Override
2015-09-30 14:24:40 +02:00
public boolean isStorageCell( final ItemStack i )
{
return true;
}
@Override
public double getIdleDrain()
{
return 0.5;
}
@Override
2015-09-30 14:24:40 +02:00
public String getUnlocalizedGroupName( final Set<ItemStack> others, final ItemStack is )
{
return GuiText.StorageCells.getUnlocalized();
}
@Override
2015-09-30 14:24:40 +02:00
public boolean isEditable( final ItemStack is )
{
return true;
}
@Override
2015-09-30 14:24:40 +02:00
public IInventory getUpgradesInventory( final ItemStack is )
{
return new CellUpgrades( is, 2 );
}
@Override
2015-09-30 14:24:40 +02:00
public IInventory getConfigInventory( final ItemStack is )
{
return new CellConfig( is );
}
@Override
2015-09-30 14:24:40 +02:00
public FuzzyMode getFuzzyMode( final ItemStack is )
{
2015-09-30 14:24:40 +02:00
final String fz = Platform.openNbtData( is ).getString( "FuzzyMode" );
try
{
return FuzzyMode.valueOf( fz );
}
2015-09-30 14:24:40 +02:00
catch( final Throwable t )
{
return FuzzyMode.IGNORE_ALL;
}
}
@Override
2015-09-30 14:24:40 +02:00
public void setFuzzyMode( final ItemStack is, final FuzzyMode fzMode )
{
Platform.openNbtData( is ).setString( "FuzzyMode", fzMode.name() );
}
@Override
2015-09-30 14:24:40 +02:00
public void onWheel( final ItemStack is, final boolean up )
{
2014-12-29 15:13:47 +01:00
this.cycleColors( is, this.getColor( is ), up ? 1 : -1 );
}
}