-
Notifications
You must be signed in to change notification settings - Fork 79
/
ModConflictContext.java
228 lines (196 loc) · 4.75 KB
/
ModConflictContext.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
package mod.chiselsandbits.client;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;
import mod.chiselsandbits.api.KeyBindingContext;
import mod.chiselsandbits.core.ClientSide;
import mod.chiselsandbits.helpers.ChiselToolType;
import mod.chiselsandbits.helpers.ModUtil;
import mod.chiselsandbits.interfaces.IVoxelBlobItem;
import mod.chiselsandbits.network.packets.PacketAccurateSneakPlace.IItemBlockAccurate;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumHand;
import net.minecraftforge.client.settings.IKeyConflictContext;
import net.minecraftforge.client.settings.KeyConflictContext;
public enum ModConflictContext implements IKeyConflictContext
{
HOLDING_OFFGRID
{
@Override
public boolean isActive()
{
if ( super.isActive() )
{
return true;
}
try
{
final ItemStack held = getPlayer().getHeldItemMainhand();
return !ModUtil.isEmpty( held ) && held.getItem() instanceof IItemBlockAccurate;
}
catch ( final NoPlayerException e )
{
// just fail.
}
return false;
}
@Override
public boolean conflicts(
final IKeyConflictContext other )
{
return this == other || other == KeyConflictContext.IN_GAME;
}
},
HOLDING_ROTATEABLE
{
@Override
public boolean isActive()
{
if ( super.isActive() )
{
return true;
}
try
{
final ItemStack held = getPlayer().getHeldItemMainhand();
return !ModUtil.isEmpty( held ) && held.getItem() instanceof IVoxelBlobItem;
}
catch ( final NoPlayerException e )
{
// just fail.
}
return false;
}
@Override
public boolean conflicts(
final IKeyConflictContext other )
{
return this == other || other == KeyConflictContext.IN_GAME || other == HOLDING_MENUITEM;
}
},
HOLDING_MENUITEM
{
@Override
public boolean isActive()
{
if ( super.isActive() )
{
return true;
}
final ChiselToolType tool = ClientSide.instance.getHeldToolType( EnumHand.MAIN_HAND );
return tool != null && tool.hasMenu();
}
@Override
public boolean conflicts(
final IKeyConflictContext other )
{
return this == other || other == KeyConflictContext.IN_GAME || other == HOLDING_POSTIVEPATTERN || other == HOLDING_CHISEL || other == HOLDING_TAPEMEASURE;
}
},
HOLDING_TAPEMEASURE
{
@Override
public boolean isActive()
{
return super.isActive() || ClientSide.instance.getHeldToolType( EnumHand.MAIN_HAND ) == ChiselToolType.TAPEMEASURE;
}
@Override
public boolean conflicts(
final IKeyConflictContext other )
{
return this == other || other == KeyConflictContext.IN_GAME;
}
},
HOLDING_POSTIVEPATTERN
{
@Override
public boolean isActive()
{
return super.isActive() || ClientSide.instance.getHeldToolType( EnumHand.MAIN_HAND ) == ChiselToolType.POSITIVEPATTERN;
}
@Override
public boolean conflicts(
final IKeyConflictContext other )
{
return this == other || other == KeyConflictContext.IN_GAME;
}
},
HOLDING_CHISEL
{
@Override
public boolean isActive()
{
if ( super.isActive() )
{
return true;
}
final ChiselToolType tool = ClientSide.instance.getHeldToolType( EnumHand.MAIN_HAND );
return tool != null && tool.isBitOrChisel();
}
@Override
public boolean conflicts(
final IKeyConflictContext other )
{
return this == other || other == KeyConflictContext.IN_GAME;
}
};
private final Set<Class<? extends Item>> activeItemClasses = new HashSet<Class<? extends Item>>();
public void setItemActive(
final Item item )
{
activeItemClasses.add( item.getClass() );
}
@Override
public boolean isActive()
{
try
{
final ItemStack held = getPlayer().getHeldItemMainhand();
if ( ModUtil.isEmpty( held ) )
{
return false;
}
for ( final Class<? extends Item> itemClass : activeItemClasses )
{
if ( itemClass.isInstance( held.getItem() ) )
{
return true;
}
}
if ( held.getItem().getClass().isAnnotationPresent( KeyBindingContext.class ) )
{
final Annotation annotation = held.getItem().getClass().getAnnotation( KeyBindingContext.class );
if ( annotation instanceof KeyBindingContext )
{
for ( final String name : ( (KeyBindingContext) annotation ).value() )
{
if ( name.equals( getName() ) )
{
return true;
}
}
}
}
}
catch ( final NoPlayerException e )
{
// just fail.
}
return false;
}
private static EntityPlayer getPlayer() throws NoPlayerException
{
final EntityPlayer player = ClientSide.instance.getPlayer();
if ( player == null )
{
throw new NoPlayerException();
}
return player;
}
public String getName()
{
return toString().toLowerCase().replace( "holding_", "" );
}
}