Skip to content

Commit

Permalink
#355 - Add support of IItemHandlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlgorithmX2 committed Dec 21, 2017
1 parent 37e4095 commit a820784
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 138 deletions.
71 changes: 49 additions & 22 deletions src/main/java/mod/chiselsandbits/helpers/ContinousBits.java
Expand Up @@ -5,19 +5,20 @@

import mod.chiselsandbits.bitbag.BagInventory;
import mod.chiselsandbits.core.ChiselsAndBits;
import mod.chiselsandbits.helpers.ModUtil.ItemStackSlot;
import mod.chiselsandbits.items.ItemBitBag;
import mod.chiselsandbits.items.ItemChiseledBit;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

public class ContinousBits implements IContinuousInventory
{
final int stateID;
private final ActingPlayer who;
private final List<ItemStackSlot> options = new ArrayList<ItemStackSlot>();
private final List<IItemInInventory> options = new ArrayList<IItemInInventory>();
private final List<BagInventory> bags = new ArrayList<BagInventory>();
private final boolean canEdit;

Expand All @@ -38,25 +39,46 @@ public ContinousBits(
for ( int zz = 0; zz < inv.getSizeInventory(); zz++ )
{
final ItemStack which = inv.getStackInSlot( zz );
if ( which != null && which.getItem() instanceof ItemChiseledBit )
if ( which != null && which.getItem() != null )
{
if ( ItemChiseledBit.getStackState( which ) == stateID )
Item i = which.getItem();
if ( i instanceof ItemChiseledBit )
{
if ( zz == src.getCurrentItem() )
if ( ItemChiseledBit.getStackState( which ) == stateID )
{
handSlot = new ItemStackSlot( inv, zz, which, src, canEdit );
if ( zz == src.getCurrentItem() )
{
handSlot = new ItemStackSlot( inv, zz, which, src, canEdit );
}
else
{
options.add( new ItemStackSlot( inv, zz, which, src, canEdit ) );
}
}
else
}

else if ( i instanceof ItemBitBag )
{
bags.add( new BagInventory( which ) );
}

else if ( which.hasCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null ) )
{
IItemHandler internal = which.getCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null );
for ( int x = 0; x < internal.getSlots(); x++ )
{
options.add( new ItemStackSlot( inv, zz, which, src, canEdit ) );
ItemStack is = internal.getStackInSlot( x );

if ( is.getItem() instanceof ItemChiseledBit )
{
if ( ItemChiseledBit.getStackState( is ) == stateID )
{
options.add( new IItemHandlerSlot( internal, x, is, src, canEdit ) );
}
}
}
}
}

if ( which != null && which.getItem() instanceof ItemBitBag )
{
bags.add( new BagInventory( which ) );
}
}

if ( handSlot != null )
Expand All @@ -66,39 +88,44 @@ public ContinousBits(
}

@Override
public ItemStackSlot getItem(
public IItemInInventory getItem(
final int BlockID )
{
return options.get( 0 );
}

@Override
public void useItem(
public boolean useItem(
final int blk )
{
final ItemStackSlot slot = options.get( 0 );
final IItemInInventory slot = options.get( 0 );

if ( ModUtil.getStackSize( slot.getStack() ) <= 1 )
if ( slot instanceof ItemStackSlot && ModUtil.getStackSize( slot.getStack() ) <= 1 )
{
for ( final BagInventory bag : bags )
{
slot.replaceStack( bag.restockItem( slot.getStack(), slot.getStackType() ) );
( (ItemStackSlot) slot ).replaceStack( bag.restockItem( slot.getStack(), slot.getStackType() ) );
}
}

slot.consume();
boolean worked = slot.consume();

if ( slot.isValid() )
{
for ( final BagInventory bag : bags )
if ( slot instanceof ItemStackSlot )
{
slot.replaceStack( bag.restockItem( slot.getStack(), slot.getStackType() ) );
for ( final BagInventory bag : bags )
{
( (ItemStackSlot) slot ).replaceStack( bag.restockItem( slot.getStack(), slot.getStackType() ) );
}
}
}
else
{
options.remove( 0 );
}

return worked;
}

@Override
Expand Down
Expand Up @@ -10,7 +10,6 @@
import com.google.common.collect.Lists;

import mod.chiselsandbits.core.ChiselsAndBits;
import mod.chiselsandbits.helpers.ModUtil.ItemStackSlot;
import mod.chiselsandbits.items.ItemChisel;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item.ToolMaterial;
Expand Down Expand Up @@ -79,7 +78,7 @@ public ContinousChisels(
}

@Override
public ItemStackSlot getItem(
public IItemInInventory getItem(
final int BlockID )
{
if ( !actionCache.containsKey( BlockID ) )
Expand All @@ -94,7 +93,7 @@ public ItemStackSlot getItem(
return new ItemStackSlot( who.getInventory(), -1, ModUtil.getEmptyStack(), who, canEdit );
}

final ItemStackSlot slot = choices.get( choices.size() - 1 );
final IItemInInventory slot = choices.get( choices.size() - 1 );

if ( slot.isValid() )
{
Expand Down Expand Up @@ -127,10 +126,11 @@ public boolean isValid()
}

@Override
public void useItem(
public boolean useItem(
final int blk )
{
getItem( blk ).damage( who );
return true;
}

}
@@ -1,19 +1,17 @@
package mod.chiselsandbits.helpers;

import mod.chiselsandbits.helpers.ModUtil.ItemStackSlot;

public interface IContinuousInventory
{

void useItem(
boolean useItem(
int blockId );

void fail(
int blockId );

boolean isValid();

ItemStackSlot getItem(
IItemInInventory getItem(
int blockId );

}
78 changes: 78 additions & 0 deletions src/main/java/mod/chiselsandbits/helpers/IItemHandlerSlot.java
@@ -0,0 +1,78 @@
package mod.chiselsandbits.helpers;

import javax.annotation.Nonnull;

import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandler;

public class IItemHandlerSlot implements IItemInInventory
{

private IItemHandler internal;
private int zz;
private @Nonnull ItemStack stack; // copy of itemstack
private final @Nonnull ItemStack originalStack;
private ActingPlayer src;
private boolean isEditable;

public IItemHandlerSlot(
IItemHandler internal,
int zz,
ItemStack which,
ActingPlayer src,
boolean canEdit )
{
this.internal = internal;
this.zz = zz;
this.stack = ModUtil.copy( which );
this.originalStack = ModUtil.copy( which );
this.src = src;
this.isEditable = canEdit;
}

@Override
public boolean isValid()
{
return isEditable && ( src.isCreative() || !ModUtil.isEmpty( stack ) && ModUtil.getStackSize( stack ) > 0 );
}

@Override
public void damage(
ActingPlayer who )
{
throw new RuntimeException( "Cannot damage an item in an inventory?" );
}

@Override
public boolean consume()
{
ItemStack is = internal.extractItem( zz, 1, true );
if ( is != null && ItemStack.areItemStackTagsEqual( is, stack ) && ItemStack.areItemStackTagsEqual( is, stack ) )
{
internal.extractItem( zz, 1, false );
ModUtil.adjustStackSize( stack, -1 );
return true;
}

return false;
}

@Override
public ItemStack getStack()
{
return stack;
}

@Override
public void swapWithWeapon()
{
throw new RuntimeException( "Cannot swap an item in an inventory?" );
}

@Override
public ItemStack getStackType()
{
return originalStack;
}

}
21 changes: 21 additions & 0 deletions src/main/java/mod/chiselsandbits/helpers/IItemInInventory.java
@@ -0,0 +1,21 @@
package mod.chiselsandbits.helpers;

import net.minecraft.item.ItemStack;

public interface IItemInInventory
{

boolean isValid();

void damage(
ActingPlayer who );

boolean consume();

ItemStack getStack();

void swapWithWeapon();

ItemStack getStackType();

}

0 comments on commit a820784

Please sign in to comment.