-
Notifications
You must be signed in to change notification settings - Fork 79
/
ContinousBits.java
169 lines (139 loc) · 3.52 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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.helpers.ModUtil.ItemStackSlot;
import mod.chiselsandbits.items.ItemChiseledBit;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
public class ContinousBits implements IContinuousInventory
{
final int stateID;
private final ActingPlayer who;
private final List<ItemStackSlot> options = new ArrayList<ItemStackSlot>();
private final List<BagInventory> bags = new ArrayList<BagInventory>();
private final InfiniteBitStorage infiniteStorage;
private final IContinuousInventory chisels;
private final boolean canEdit;
ItemStackSlot fakeSlot;
public ContinousBits(
final ActingPlayer src,
final BlockPos pos,
final int stateID,
IContinuousInventory chisels,
InfiniteBitStorage infiniteStorage )
{
who = src;
this.stateID = stateID;
final IInventory inv = src.getInventory();
this.chisels = chisels;
this.infiniteStorage = infiniteStorage;
// test can edit...
canEdit = who.canPlayerManipulate( pos, EnumFacing.UP, new ItemStack( ChiselsAndBits.getItems().itemChiselDiamond, 1 ), true );
ItemStackSlot handSlot = null;
fakeSlot = new ItemStackSlot( ItemChiseledBit.createStack( stateID, 1, true ) );
for ( int zz = 0; zz < inv.getSizeInventory(); zz++ )
{
final ItemStack which = inv.getStackInSlot( zz );
if ( which != null && which.getItem() 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 ) );
}
}
}
if ( BagInventory.isBag( which ) )
{
bags.add( new BagInventory( which ) );
}
}
if ( handSlot != null )
{
options.add( handSlot );
}
}
@Override
public ItemStackSlot getItem(
final int BlockID )
{
if ( options.isEmpty() )
{
return fakeSlot;
}
return options.get( 0 );
}
@Override
public boolean hasUses(
int state,
int fullSize )
{
if ( who.isCreative() )
return true;
int total = infiniteStorage.getCount( state );
for ( ItemStackSlot slot : options )
{
total += ModUtil.getStackSize( slot.getStack() );
}
for ( final BagInventory bag : bags )
{
total += bag.countItems( state );
}
return total >= fullSize;
}
@Override
public void useItem(
final int blk )
{
// if possible remove it form here first.
if ( infiniteStorage.dec( blk ) )
{
return;
}
final ItemStackSlot slot = options.get( 0 );
if ( ModUtil.getStackSize( slot.getStack() ) <= 1 )
{
for ( final BagInventory bag : bags )
{
slot.replaceStack( bag.restockItem( slot.getStack(), slot.getStackType() ) );
}
}
slot.consume();
if ( slot.isValid() )
{
for ( final BagInventory bag : bags )
{
slot.replaceStack( bag.restockItem( slot.getStack(), slot.getStackType() ) );
}
}
else
{
options.remove( 0 );
}
}
@Override
public void fail(
final int BlockID )
{
// hmm.. nope?
}
@Override
public boolean isValid()
{
if ( !options.isEmpty() )
return true;
int left = infiniteStorage.getCount( stateID );
if ( left == 0 && infiniteStorage.chiselBlock( stateID, who, chisels ) )
return true;
return left > 0;
}
}