/
BagInventory.java
571 lines (470 loc) · 12.1 KB
/
BagInventory.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package mod.chiselsandbits.bitbag;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import javax.annotation.Nonnull;
import mod.chiselsandbits.core.ChiselsAndBits;
import mod.chiselsandbits.helpers.LocalStrings;
import mod.chiselsandbits.helpers.ModUtil;
import mod.chiselsandbits.items.ItemBitBag;
import mod.chiselsandbits.items.ItemChiseledBit;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
public class BagInventory implements IInventory
{
// internal storage, the capability.
BagStorage inv;
// tmp storage, the IInventory
ItemStack[] stackSlots;
public BagInventory(
final ItemStack is )
{
inv = (BagStorage) is.getCapability( CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null );
stackSlots = new ItemStack[BagStorage.BAG_STORAGE_SLOTS];
// the cap is missing? then just make and load it ourselves.
if ( inv == null )
{
inv = new BagStorage();
inv.stack = is;
inv.setStorage( BagCapabilityProvider.getStorageArray( is, BagStorage.BAG_STORAGE_SLOTS * ItemBitBag.INTS_PER_BIT_TYPE ) );
}
for ( int x = 0; x < stackSlots.length; ++x )
{
stackSlots[x] = ModUtil.getEmptyStack();
}
}
public ItemStack getItemStack()
{
return inv.stack;
}
@Override
public String getName()
{
return "container.inventory";
}
@Override
public boolean hasCustomName()
{
return false;
}
@Override
public ITextComponent getDisplayName()
{
return hasCustomName() ? new TextComponentString( getName() ) : new TextComponentTranslation( getName(), new Object[0] );
}
@Override
public int getSizeInventory()
{
return stackSlots.length;
}
private int getStateInSlot(
int index )
{
final int qty = inv.contents[ItemBitBag.INTS_PER_BIT_TYPE * index + ItemBitBag.OFFSET_QUANTITY];
final int id = inv.contents[ItemBitBag.INTS_PER_BIT_TYPE * index + ItemBitBag.OFFSET_STATE_ID];
if ( qty > 0 )
{
return id;
}
return 0;
}
@Override
public @Nonnull ItemStack getStackInSlot(
final int index )
{
final int qty = inv.contents[ItemBitBag.INTS_PER_BIT_TYPE * index + ItemBitBag.OFFSET_QUANTITY];
final int id = inv.contents[ItemBitBag.INTS_PER_BIT_TYPE * index + ItemBitBag.OFFSET_STATE_ID];
if ( ModUtil.notEmpty( stackSlots[index] ) )
{
final ItemStack which = ModUtil.nonNull( stackSlots[index] );
ModUtil.setStackSize( which, qty );
return which;
}
if ( qty == 0 || id == 0 )
{
return ModUtil.getEmptyStack();
}
return stackSlots[index] = ItemChiseledBit.createStack( id, qty, false );
}
@Override
public ItemStack decrStackSize(
final int index,
int count )
{
final int qty = inv.contents[ItemBitBag.INTS_PER_BIT_TYPE * index + ItemBitBag.OFFSET_QUANTITY];
final int id = inv.contents[ItemBitBag.INTS_PER_BIT_TYPE * index + ItemBitBag.OFFSET_STATE_ID];
if ( qty == 0 || id == 0 )
{
return ModUtil.getEmptyStack();
}
if ( count > qty )
{
count = qty;
}
inv.contents[ItemBitBag.INTS_PER_BIT_TYPE * index + ItemBitBag.OFFSET_QUANTITY] -= count;
inv.onChange();
if ( ModUtil.notEmpty( stackSlots[index] ) )
{
ModUtil.adjustStackSize( ModUtil.nonNull( stackSlots[index] ), -count );
}
return ItemChiseledBit.createStack( id, count, false );
}
@Override
public ItemStack removeStackFromSlot(
final int index )
{
return ModUtil.getEmptyStack();
}
@Override
public void setInventorySlotContents(
final int index,
final ItemStack stack )
{
stackSlots[index] = ModUtil.getEmptyStack();
if ( stack != null && stack.getItem() instanceof ItemChiseledBit )
{
inv.contents[ItemBitBag.INTS_PER_BIT_TYPE * index + ItemBitBag.OFFSET_QUANTITY] = ModUtil.getStackSize( stack );
inv.contents[ItemBitBag.INTS_PER_BIT_TYPE * index + ItemBitBag.OFFSET_STATE_ID] = ItemChiseledBit.getStackState( stack );
}
else
{
inv.contents[ItemBitBag.INTS_PER_BIT_TYPE * index + ItemBitBag.OFFSET_QUANTITY] = 0;
inv.contents[ItemBitBag.INTS_PER_BIT_TYPE * index + ItemBitBag.OFFSET_STATE_ID] = 0;
}
inv.onChange();
}
@Override
public int getInventoryStackLimit()
{
return ChiselsAndBits.getConfig().bagStackSize;
}
@Override
public void markDirty()
{
for ( int x = 0; x < getSizeInventory(); x++ )
{
if ( ModUtil.notEmpty( stackSlots[x] ) )
{
inv.contents[ItemBitBag.INTS_PER_BIT_TYPE * x + ItemBitBag.OFFSET_QUANTITY] = ModUtil.getStackSize( stackSlots[x] );
stackSlots[x] = ModUtil.getEmptyStack();
inv.onChange();
}
}
}
@Override
public boolean isUseableByPlayer(
final EntityPlayer player )
{
return true;
}
@Override
public void openInventory(
final EntityPlayer player )
{
}
@Override
public void closeInventory(
final EntityPlayer player )
{
}
@Override
public boolean isItemValidForSlot(
final int index,
final ItemStack stack )
{
return stack != null && stack.getItem() instanceof ItemChiseledBit;
}
@Override
public int getField(
final int id )
{
return 0;
}
@Override
public void setField(
final int id,
final int value )
{
}
@Override
public int getFieldCount()
{
return 0;
}
private static class StateQtyPair
{
public StateQtyPair(
int state,
int qty )
{
this.qty = qty;
this.state = state;
}
int qty;
int state;
};
public void sort()
{
List<StateQtyPair> stacks = new ArrayList<StateQtyPair>();
for ( int x = 0; x < stackSlots.length; ++x )
{
int state = inv.contents[x * ItemBitBag.INTS_PER_BIT_TYPE + ItemBitBag.OFFSET_STATE_ID];
int qty = inv.contents[x * ItemBitBag.INTS_PER_BIT_TYPE + ItemBitBag.OFFSET_QUANTITY];
if ( state > 0 && qty > 0 )
{
stacks.add( new StateQtyPair( state, qty ) );
}
}
stacks.sort( new Comparator<StateQtyPair>() {
@Override
public int compare(
StateQtyPair o1,
StateQtyPair o2 )
{
if ( o1.state < o2.state )
return 1;
if ( o1.state > o2.state )
return -1;
if ( o1.qty < o2.qty )
return 1;
if ( o1.qty > o2.qty )
return -1;
return 0;
}
} );
for ( int x = 0; x < stacks.size() - 1; x++ )
{
StateQtyPair a = stacks.get( x );
StateQtyPair b = stacks.get( x + 1 );
if ( a.state == b.state )
{
if ( a.qty < getInventoryStackLimit() )
{
int shiftSize = getInventoryStackLimit() - a.qty;
shiftSize = Math.min( shiftSize, b.qty );
a.qty += shiftSize;
b.qty -= shiftSize;
if ( b.qty <= 0 )
stacks.remove( x + 1 );
--x;
}
}
}
for ( int x = 0; x < stackSlots.length; ++x )
{
int state = 0;
int qty = 0;
if ( stacks.size() > x )
{
state = stacks.get( x ).state;
qty = stacks.get( x ).qty;
}
inv.contents[x * ItemBitBag.INTS_PER_BIT_TYPE + ItemBitBag.OFFSET_STATE_ID] = state;
inv.contents[x * ItemBitBag.INTS_PER_BIT_TYPE + ItemBitBag.OFFSET_QUANTITY] = qty;
stackSlots[x] = ModUtil.getEmptyStack();
}
inv.onChange();
}
public void clear(
final ItemStack stack )
{
for ( int x = 0; x < stackSlots.length; ++x )
{
if ( matches( stack, stackSlots[x] ) )
{
stackSlots[x] = ModUtil.getEmptyStack();
inv.contents[x * ItemBitBag.INTS_PER_BIT_TYPE + ItemBitBag.OFFSET_STATE_ID] = 0;
inv.contents[x * ItemBitBag.INTS_PER_BIT_TYPE + ItemBitBag.OFFSET_QUANTITY] = 0;
}
}
inv.onChange();
}
public boolean matches(
final ItemStack cmpStack,
final ItemStack invStack )
{
if ( ModUtil.isEmpty( cmpStack ) || invStack == null )
{
return true;
}
return cmpStack.getItem() == invStack.getItem() && ItemStack.areItemStackTagsEqual( cmpStack, invStack );
}
public ItemStack restockItem(
final ItemStack target,
final ItemStack targetType )
{
int outSize = ModUtil.getStackSize( target );
for ( int x = getSizeInventory() - 1; x >= 0; x-- )
{
if ( ItemChiseledBit.sameBit( targetType, getStateInSlot( x ) ) )
{
final ItemStack is = getStackInSlot( x );
outSize += ModUtil.getStackSize( is );
final int total = outSize;
outSize = Math.min( is.getMaxStackSize(), outSize );
final int overage = total - outSize;
if ( overage > 0 )
{
ModUtil.setStackSize( is, overage );
}
else
{
setInventorySlotContents( x, ModUtil.getEmptyStack() );
}
markDirty();
if ( outSize == is.getMaxStackSize() )
{
// done!
break;
}
}
}
final ItemStack out = ModUtil.copy( targetType );
ModUtil.setStackSize( out, outSize );
return out;
}
public @Nonnull ItemStack insertItem(
final @Nonnull ItemStack which )
{
for ( int x = 0; x < getSizeInventory(); x++ )
{
final ItemStack is = getStackInSlot( x );
if ( !ModUtil.isEmpty( is ) && ItemChiseledBit.getStackState( which ) == ItemChiseledBit.getStackState( is ) )
{
ModUtil.adjustStackSize( is, ModUtil.getStackSize( which ) );
final int total = ModUtil.getStackSize( is );
ModUtil.setStackSize( is, Math.min( getInventoryStackLimit(), ModUtil.getStackSize( is ) ) );
final int overage = total - ModUtil.getStackSize( is );
if ( overage > 0 )
{
ModUtil.setStackSize( which, overage );
markDirty();
}
else
{
markDirty();
return ModUtil.getEmptyStack();
}
}
else if ( ModUtil.isEmpty( is ) )
{
setInventorySlotContents( x, which );
markDirty();
return ModUtil.getEmptyStack();
}
}
return which;
}
public int extractBit(
final int bitMeta,
int total )
{
int used = 0;
for ( int index = stackSlots.length - 1; index >= 0; index-- )
{
final int qty_idx = ItemBitBag.INTS_PER_BIT_TYPE * index + ItemBitBag.OFFSET_QUANTITY;
final int qty = inv.contents[qty_idx];
final int id = inv.contents[ItemBitBag.INTS_PER_BIT_TYPE * index + ItemBitBag.OFFSET_STATE_ID];
if ( id == bitMeta && qty > 0 )
{
inv.contents[qty_idx] -= total;
if ( inv.contents[qty_idx] < 0 )
{
inv.contents[qty_idx] = 0;
}
inv.onChange();
final int diff = qty - inv.contents[qty_idx];
used += diff;
total -= diff;
if ( 0 == total )
{
return used;
}
}
}
return used;
}
@SideOnly( Side.CLIENT )
public List<String> listContents(
final List<String> details )
{
final TreeMap<String, Integer> contents = new TreeMap<String, Integer>();
for ( int x = 0; x < getSizeInventory(); x++ )
{
final ItemStack is = getStackInSlot( x );
if ( !ModUtil.isEmpty( is ) )
{
final IBlockState state = ModUtil.getStateById( ItemChiseledBit.getStackState( is ) );
if ( state == null )
{
continue;
}
final String name = ItemChiseledBit.getBitStateName( state );
if ( name != null )
{
Integer count = contents.get( name );
if ( count == null )
{
count = ModUtil.getStackSize( is );
}
else
{
count += ModUtil.getStackSize( is );
}
contents.put( name, count );
}
}
}
if ( contents.isEmpty() )
{
details.add( LocalStrings.Empty.getLocal() );
}
final List<Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>();
list.addAll( contents.entrySet() );
Collections.sort( list, new Comparator<Entry<String, Integer>>() {
@Override
public int compare(
final Entry<String, Integer> o1,
final Entry<String, Integer> o2 )
{
final int y = o1.getValue();
final int x = o2.getValue();
return x < y ? -1 : x == y ? 0 : 1;
}
} );
for ( final Entry<String, Integer> e : list )
{
details.add( new StringBuilder().append( e.getValue() ).append( ' ' ).append( e.getKey() ).toString() );
}
return details;
}
@Override
public void clear()
{
clear( null );
}
@Override
public boolean func_191420_l()
{
for ( final ItemStack itemstack : stackSlots )
{
if ( !itemstack.func_190926_b() )
{
return false;
}
}
return true;
}
}