-
Notifications
You must be signed in to change notification settings - Fork 79
/
ContinousChisels.java
136 lines (111 loc) · 3.25 KB
/
ContinousChisels.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
package mod.chiselsandbits.helpers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Nonnull;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import mod.chiselsandbits.core.ChiselsAndBits;
import mod.chiselsandbits.items.ItemChisel;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item.ToolMaterial;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
public class ContinousChisels implements IContinuousInventory
{
private final ActingPlayer who;
private final List<ItemStackSlot> options = new ArrayList<ItemStackSlot>();
private final HashMap<Integer, List<ItemStackSlot>> actionCache = new HashMap<Integer, List<ItemStackSlot>>();
private final boolean canEdit;
public ContinousChisels(
final @Nonnull ActingPlayer who,
final @Nonnull BlockPos pos,
final @Nonnull EnumFacing side )
{
this.who = who;
final ItemStack inHand = who.getCurrentEquippedItem();
final IInventory inv = who.getInventory();
// test can edit...
canEdit = who.canPlayerManipulate( pos, side, new ItemStack( ChiselsAndBits.getItems().itemChiselDiamond, 1 ), false );
if ( inHand != null && ModUtil.notEmpty( inHand ) && inHand.getItem() instanceof ItemChisel )
{
if ( who.canPlayerManipulate( pos, side, inHand, false ) )
{
options.add( new ItemStackSlot( inv, who.getCurrentItem(), inHand, who, canEdit ) );
}
}
else
{
final ArrayListMultimap<Integer, ItemStackSlot> discovered = ArrayListMultimap.create();
for ( int x = 0; x < inv.getSizeInventory(); x++ )
{
final ItemStack is = inv.getStackInSlot( x );
if ( is == inHand )
{
continue;
}
if ( !who.canPlayerManipulate( pos, side, is, false ) )
{
continue;
}
if ( is != null && ModUtil.notEmpty( is ) && is.getItem() instanceof ItemChisel )
{
final ToolMaterial newMat = ( (ItemChisel) is.getItem() ).whatMaterial();
discovered.put( newMat.getHarvestLevel(), new ItemStackSlot( inv, x, is, who, canEdit ) );
}
}
final List<ItemStackSlot> allValues = Lists.newArrayList( discovered.values() );
for ( final ItemStackSlot f : Lists.reverse( allValues ) )
{
options.add( f );
}
}
}
@Override
public IItemInInventory getItem(
final int BlockID )
{
if ( !actionCache.containsKey( BlockID ) )
{
actionCache.put( BlockID, new ArrayList<ItemStackSlot>( options ) );
}
final List<ItemStackSlot> choices = actionCache.get( BlockID );
if ( choices.isEmpty() )
{
return new ItemStackSlot( who.getInventory(), -1, ModUtil.getEmptyStack(), who, canEdit );
}
final IItemInInventory slot = choices.get( choices.size() - 1 );
if ( slot.isValid() )
{
return slot;
}
else
{
fail( BlockID );
}
return getItem( BlockID );
}
@Override
public void fail(
final int BlockID )
{
final List<ItemStackSlot> choices = actionCache.get( BlockID );
if ( !choices.isEmpty() )
{
choices.remove( choices.size() - 1 );
}
}
@Override
public boolean isValid()
{
return !options.isEmpty() || who.isCreative();
}
@Override
public boolean useItem(
final int blk )
{
getItem( blk ).damage( who );
return true;
}
}