Merge pull request #1695 from yueh/b-prevents-crafting-of-disabled-parts

Prevents crafting of disabled parts
This commit is contained in:
yueh
2015-07-24 22:27:21 +02:00
29 changed files with 488 additions and 165 deletions
@@ -1,3 +1,21 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, 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.core.api.definitions;
@@ -10,6 +28,7 @@ import appeng.api.util.AEColor;
import appeng.api.util.AEColoredItemDefinition;
import appeng.core.FeatureHandlerRegistry;
import appeng.core.FeatureRegistry;
import appeng.core.features.ActivityState;
import appeng.core.features.ColoredItemDefinition;
import appeng.core.features.IAEFeature;
import appeng.core.features.IFeatureHandler;
@@ -76,7 +95,9 @@ public class DefinitionConstructor
{
for( AEColor color : AEColor.VALID_COLORS )
{
definition.add( color, new ItemStackSrc( targetItem, offset + color.ordinal() ) );
final ActivityState state = ActivityState.from( target.isEnabled() );
definition.add( color, new ItemStackSrc( targetItem, offset + color.ordinal(), state ) );
}
}
@@ -29,6 +29,7 @@ import appeng.api.definitions.ITileDefinition;
import appeng.block.AEBaseTileBlock;
import appeng.core.CommonHelper;
import appeng.core.CreativeTab;
import appeng.tile.AEBaseTile;
import appeng.util.Platform;
@@ -82,6 +83,7 @@ public final class AETileBlockFeatureHandler implements IFeatureHandler
GameRegistry.registerBlock( this.featured, null, registryName );
GameRegistry.registerItem( this.definition.maybeItem().get(), registryName );
GameRegistry.registerTileEntity( this.featured.getTileEntityClass(), this.featured.toString() );
AEBaseTile.registerTileItem( this.featured.getTileEntityClass(), new BlockStackSrc( this.featured, 0, ActivityState.from( this.isFeatureAvailable() ) ) );
}
}
}
@@ -1,7 +1,38 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, 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.core.features;
public enum ActivityState
{
Enabled, Disabled
Enabled,
Disabled;
public static ActivityState from( boolean enabled )
{
if( enabled )
{
return ActivityState.Enabled;
}
else
{
return ActivityState.Disabled;
}
}
}
@@ -21,6 +21,7 @@ package appeng.core.features;
import java.lang.reflect.Constructor;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ObjectArrays;
@@ -37,8 +38,8 @@ import appeng.block.AEBaseBlock;
public class BlockDefinition extends ItemDefinition implements IBlockDefinition
{
private final Block block;
private final boolean enabled;
private static final ItemBlockTransformer ITEMBLOCK_TRANSFORMER = new ItemBlockTransformer();
private final Optional<Block> block;
public BlockDefinition( Block block, ActivityState state )
{
@@ -47,8 +48,14 @@ public class BlockDefinition extends ItemDefinition implements IBlockDefinition
Preconditions.checkNotNull( block );
Preconditions.checkNotNull( state );
this.block = block;
this.enabled = state == ActivityState.Enabled;
if( state == ActivityState.Enabled )
{
this.block = Optional.of( block );
}
else
{
this.block = Optional.absent();
}
}
/**
@@ -92,7 +99,7 @@ public class BlockDefinition extends ItemDefinition implements IBlockDefinition
*
* TODO: throw an exception instead of returning null? As this could cause issue later on.
*
* @param block the block to create the {@link ItemBlock} from
* @param block the block to create the {@link ItemBlock} from
* @param itemclass the class used to construct it.
*
* @return an {@link Item} for the block. Actually always a sub type of {@link ItemBlock}
@@ -121,38 +128,51 @@ public class BlockDefinition extends ItemDefinition implements IBlockDefinition
@Override
public final Optional<Block> maybeBlock()
{
return Optional.of( this.block );
return this.block;
}
@Override
public final Optional<ItemBlock> maybeItemBlock()
{
if( this.enabled )
{
return Optional.of( new ItemBlock( this.block ) );
}
else
{
return Optional.absent();
}
return this.block.transform( ITEMBLOCK_TRANSFORMER );
}
@Override
public final Optional<ItemStack> maybeStack( int stackSize )
{
if( this.enabled )
{
return Optional.of( new ItemStack( this.block ) );
}
else
{
return Optional.absent();
}
return this.block.transform( new ItemStackTransformer( stackSize ) );
}
@Override
public final boolean isSameAs( IBlockAccess world, int x, int y, int z )
{
return this.enabled && world.getBlock( x, y, z ) == this.block;
return this.isEnabled() && world.getBlock( x, y, z ) == this.block.get();
}
private static class ItemBlockTransformer implements Function<Block, ItemBlock>
{
@Override
public ItemBlock apply( Block input )
{
return new ItemBlock( input );
}
}
private static class ItemStackTransformer implements Function<Block, ItemStack>
{
private final int stackSize;
public ItemStackTransformer( int stackSize )
{
Preconditions.checkArgument( stackSize > 0 );
this.stackSize = stackSize;
}
@Override
public ItemStack apply( Block input )
{
return new ItemStack( input, this.stackSize );
}
}
}
@@ -0,0 +1,74 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, 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.core.features;
import javax.annotation.Nullable;
import com.google.common.base.Preconditions;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
public class BlockStackSrc implements IStackSrc
{
public final Block block;
public final int damage;
private final boolean enabled;
public BlockStackSrc( Block block, int damage, ActivityState state )
{
Preconditions.checkNotNull( block );
Preconditions.checkArgument( damage >= 0 );
Preconditions.checkNotNull( state );
Preconditions.checkArgument( state == ActivityState.Enabled || state == ActivityState.Disabled );
this.block = block;
this.damage = damage;
this.enabled = state == ActivityState.Enabled;
}
@Nullable
@Override
public ItemStack stack( int i )
{
return new ItemStack( this.block, i, this.damage );
}
@Override
public Item getItem()
{
return null;
}
@Override
public int getDamage()
{
return this.damage;
}
@Override
public boolean isEnabled()
{
return this.enabled;
}
}
@@ -1,6 +1,6 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
* Copyright (c) 2013 - 2015, 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
@@ -54,7 +54,7 @@ public final class ColoredItemDefinition implements AEColoredItemDefinition
return null;
}
return is.item;
return is.getItem();
}
@Override
@@ -97,6 +97,6 @@ public final class ColoredItemDefinition implements AEColoredItemDefinition
return false;
}
return comparableItem.getItem() == is.item && comparableItem.getItemDamage() == is.damage;
return comparableItem.getItem() == is.getItem() && comparableItem.getItemDamage() == is.damage;
}
}
@@ -1,6 +1,6 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
* Copyright (c) 2013 - 2015, 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
@@ -21,6 +21,7 @@ package appeng.core.features;
import javax.annotation.Nonnull;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
@@ -33,27 +34,39 @@ import appeng.api.definitions.IItemDefinition;
public final class DamagedItemDefinition implements IItemDefinition
{
private final IStackSrc source;
private static final ItemTransformer ITEM_TRANSFORMER = new ItemTransformer();
private final Optional<IStackSrc> source;
public DamagedItemDefinition( @Nonnull IStackSrc source )
{
this.source = Preconditions.checkNotNull( source );
Preconditions.checkNotNull( source );
if( source.isEnabled() )
{
this.source = Optional.of( source );
}
else
{
this.source = Optional.absent();
}
}
@Override
public Optional<Item> maybeItem()
{
final Item item = this.source.getItem();
return Optional.fromNullable( item );
return this.source.transform( ITEM_TRANSFORMER );
}
@Override
public Optional<ItemStack> maybeStack( int stackSize )
{
final ItemStack stack = this.source.stack( stackSize );
return this.source.transform( new ItemStackTransformer( stackSize ) );
}
return Optional.fromNullable( stack );
@Override
public boolean isEnabled()
{
return this.source.isPresent();
}
@Override
@@ -64,7 +77,7 @@ public final class DamagedItemDefinition implements IItemDefinition
return false;
}
return comparableStack.getItem() == this.source.getItem() && comparableStack.getItemDamage() == this.source.getDamage();
return this.isEnabled() && comparableStack.getItem() == this.source.get().getItem() && comparableStack.getItemDamage() == this.source.get().getDamage();
}
@Override
@@ -72,4 +85,31 @@ public final class DamagedItemDefinition implements IItemDefinition
{
return false;
}
private static class ItemTransformer implements Function<IStackSrc, Item>
{
@Override
public Item apply( IStackSrc input )
{
return input.getItem();
}
}
private static class ItemStackTransformer implements Function<IStackSrc, ItemStack>
{
private final int stackSize;
public ItemStackTransformer( int stackSize )
{
Preconditions.checkArgument( stackSize > 0 );
this.stackSize = stackSize;
}
@Override
public ItemStack apply( IStackSrc input )
{
return input.stack( this.stackSize );
}
}
}
@@ -1,6 +1,6 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
* Copyright (c) 2013 - 2015, 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
@@ -31,4 +31,6 @@ public interface IStackSrc
Item getItem();
int getDamage();
boolean isEnabled();
}
@@ -1,6 +1,6 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
* Copyright (c) 2013 - 2015, 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
@@ -19,6 +19,7 @@
package appeng.core.features;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
@@ -32,41 +33,45 @@ import appeng.util.Platform;
public class ItemDefinition implements IItemDefinition
{
private final Item item;
private final boolean enabled;
private final Optional<Item> item;
public ItemDefinition( Item item, ActivityState state )
{
Preconditions.checkNotNull( item );
Preconditions.checkNotNull( state );
this.item = item;
this.enabled = state == ActivityState.Enabled;
if( state == ActivityState.Enabled )
{
this.item = Optional.of( item );
}
else
{
this.item = Optional.absent();
}
}
@Override
public final Optional<Item> maybeItem()
{
return Optional.of( this.item );
return this.item;
}
@Override
public Optional<ItemStack> maybeStack( int stackSize )
{
if( this.enabled )
{
return Optional.of( new ItemStack( this.item ) );
}
else
{
return Optional.absent();
}
return this.item.transform( new ItemStackTransformer( stackSize ) );
}
@Override
public boolean isEnabled()
{
return this.item.isPresent();
}
@Override
public final boolean isSameAs( ItemStack comparableStack )
{
return this.enabled && Platform.isSameItemType( comparableStack, this.maybeStack( 1 ).get() );
return this.isEnabled() && Platform.isSameItemType( comparableStack, this.maybeStack( 1 ).get() );
}
@Override
@@ -74,4 +79,22 @@ public class ItemDefinition implements IItemDefinition
{
return false;
}
private static class ItemStackTransformer implements Function<Item, ItemStack>
{
private final int stackSize;
public ItemStackTransformer( int stackSize )
{
Preconditions.checkArgument( stackSize > 0 );
this.stackSize = stackSize;
}
@Override
public ItemStack apply( Item input )
{
return new ItemStack( input, this.stackSize );
}
}
}
@@ -1,6 +1,6 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
* Copyright (c) 2013 - 2015, 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
@@ -21,7 +21,8 @@ package appeng.core.features;
import javax.annotation.Nullable;
import net.minecraft.block.Block;
import com.google.common.base.Preconditions;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
@@ -29,39 +30,27 @@ import net.minecraft.item.ItemStack;
public class ItemStackSrc implements IStackSrc
{
public final Item item;
public final Block block;
private final Item item;
public final int damage;
private final boolean enabled;
public ItemStackSrc( Item i, int dmg )
public ItemStackSrc( Item item, int damage, ActivityState state )
{
this.block = null;
this.item = i;
this.damage = dmg;
}
Preconditions.checkNotNull( item );
Preconditions.checkArgument( damage >= 0 );
Preconditions.checkNotNull( state );
Preconditions.checkArgument( state == ActivityState.Enabled || state == ActivityState.Disabled );
public ItemStackSrc( Block b, int dmg )
{
this.item = null;
this.block = b;
this.damage = dmg;
this.item = item;
this.damage = damage;
this.enabled = state == ActivityState.Enabled;
}
@Nullable
@Override
public ItemStack stack( int i )
{
if( this.block != null )
{
return new ItemStack( this.block, i, this.damage );
}
if( this.item != null )
{
return new ItemStack( this.item, i, this.damage );
}
return null;
return new ItemStack( this.item, i, this.damage );
}
@Override
@@ -75,4 +64,10 @@ public class ItemStackSrc implements IStackSrc
{
return this.damage;
}
@Override
public boolean isEnabled()
{
return this.enabled;
}
}
@@ -1,6 +1,6 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
* Copyright (c) 2013 - 2015, 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
@@ -19,6 +19,8 @@
package appeng.core.features;
import com.google.common.base.Preconditions;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
@@ -27,12 +29,11 @@ import appeng.items.materials.MaterialType;
public class MaterialStackSrc implements IStackSrc
{
final MaterialType src;
private final MaterialType src;
public MaterialStackSrc( MaterialType src )
{
assert src != null;
Preconditions.checkNotNull( src );
this.src = src;
}
@@ -54,4 +55,10 @@ public class MaterialStackSrc implements IStackSrc
{
return this.src.damageValue;
}
@Override
public boolean isEnabled()
{
return true;
}
}
@@ -19,6 +19,7 @@
package appeng.core.features;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
@@ -30,7 +31,8 @@ import appeng.block.AEBaseTileBlock;
public final class TileDefinition extends BlockDefinition implements ITileDefinition
{
private final AEBaseTileBlock block;
private static final TileEntityTransformer TILEENTITY_TRANSFORMER = new TileEntityTransformer();
private final Optional<AEBaseTileBlock> block;
public TileDefinition( AEBaseTileBlock block, ActivityState state )
{
@@ -40,14 +42,30 @@ public final class TileDefinition extends BlockDefinition implements ITileDefini
Preconditions.checkNotNull( state );
Preconditions.checkNotNull( block.getTileEntityClass() );
this.block = block;
if( state == ActivityState.Enabled )
{
this.block = Optional.of( block );
}
else
{
this.block = Optional.absent();
}
}
@Override
public Optional<? extends Class<? extends TileEntity>> maybeEntity()
{
final Class<? extends TileEntity> entity = this.block.getTileEntityClass();
return this.block.transform( TILEENTITY_TRANSFORMER );
}
return Optional.of( entity );
private static class TileEntityTransformer implements Function<AEBaseTileBlock, Class<? extends TileEntity>>
{
@Override
public Class<? extends TileEntity> apply( AEBaseTileBlock input )
{
final Class<? extends TileEntity> entity = input.getTileEntityClass();
return entity;
}
}
}
@@ -1,6 +1,6 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
* Copyright (c) 2013 - 2015, 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
@@ -77,6 +77,12 @@ public final class WrappedDamageItemDefinition implements ITileDefinition
return this.definition.maybeBlock().transform( new BlockTransformFunction( stackSize, this.damage ) );
}
@Override
public boolean isEnabled()
{
return this.definition.isEnabled();
}
@Override
public boolean isSameAs( ItemStack comparableStack )
{