-
Notifications
You must be signed in to change notification settings - Fork 79
/
ContinousBits.java
145 lines (125 loc) · 3.39 KB
/
ContinousBits.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package mod.chiselsandbits.helpers;
import java.util.ArrayList;
import java.util.List;
import mod.chiselsandbits.bitbag.BagInventory;
import mod.chiselsandbits.core.ChiselsAndBits;
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<IItemInInventory> options = new ArrayList<IItemInInventory>();
private final List<BagInventory> bags = new ArrayList<BagInventory>();
private final boolean canEdit;
public ContinousBits(
final ActingPlayer src,
final BlockPos pos,
final int stateID )
{
who = src;
this.stateID = stateID;
final IInventory inv = src.getInventory();
// test can edit...
canEdit = who.canPlayerManipulate( pos, EnumFacing.UP, new ItemStack( ChiselsAndBits.getItems().itemChiselDiamond, 1 ), true );
ItemStackSlot handSlot = null;
for ( int zz = 0; zz < inv.getSizeInventory(); zz++ )
{
final ItemStack which = inv.getStackInSlot( zz );
if ( which != null && which.getItem() != null )
{
Item i = which.getItem();
if ( i instanceof ItemChiseledBit )
{
if ( ItemChiseledBit.getStackState( which ) == stateID )
{
if ( zz == src.getCurrentItem() )
{
handSlot = new ItemStackSlot( inv, zz, which, src, canEdit );
}
else
{
options.add( new ItemStackSlot( inv, zz, which, src, canEdit ) );
}
}
}
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++ )
{
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 ( handSlot != null )
{
options.add( handSlot );
}
}
@Override
public IItemInInventory getItem(
final int BlockID )
{
return options.get( 0 );
}
@Override
public boolean useItem(
final int blk )
{
final IItemInInventory slot = options.get( 0 );
if ( slot instanceof ItemStackSlot && ModUtil.getStackSize( slot.getStack() ) <= 1 )
{
for ( final BagInventory bag : bags )
{
( (ItemStackSlot) slot ).replaceStack( bag.restockItem( slot.getStack(), slot.getStackType() ) );
}
}
boolean worked = slot.consume();
if ( slot.isValid() )
{
if ( slot instanceof ItemStackSlot )
{
for ( final BagInventory bag : bags )
{
( (ItemStackSlot) slot ).replaceStack( bag.restockItem( slot.getStack(), slot.getStackType() ) );
}
}
}
else
{
options.remove( 0 );
}
return worked;
}
@Override
public void fail(
final int BlockID )
{
// hmm.. nope?
}
@Override
public boolean isValid()
{
return !options.isEmpty();
}
}