/
BagGui.java
288 lines (244 loc) · 7.75 KB
/
BagGui.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
package mod.chiselsandbits.bitbag;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import mod.chiselsandbits.core.ChiselsAndBits;
import mod.chiselsandbits.core.ClientSide;
import mod.chiselsandbits.helpers.LocalStrings;
import mod.chiselsandbits.helpers.ModUtil;
import mod.chiselsandbits.network.NetworkRouter;
import mod.chiselsandbits.network.packets.PacketBagGui;
import mod.chiselsandbits.network.packets.PacketClearBagGui;
import mod.chiselsandbits.network.packets.PacketSortBagGui;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
public class BagGui extends GuiContainer
{
private static final ResourceLocation BAG_GUI_TEXTURE = new ResourceLocation( ChiselsAndBits.MODID, "textures/gui/container/bitbag.png" );
private static int INNER_SLOT_SIZE = 16;
private static GuiBagFontRenderer specialFontRenderer = null;
private GuiIconButton trashBtn;
private GuiIconButton sortBtn;
public BagGui(
final EntityPlayer player,
final World world,
final int x,
final int y,
final int z )
{
super( new BagContainer( player, world, x, y, z ) );
allowUserInput = false;
ySize = 239;
}
@Override
public void initGui()
{
super.initGui();
buttonList.add( trashBtn = new GuiIconButton( 1, guiLeft - 18, guiTop + 0, "help.trash", ClientSide.trashIcon ) );
buttonList.add( sortBtn = new GuiIconButton( 1, guiLeft - 18, guiTop + 18, "help.sort", ClientSide.sortIcon ) );
}
BagContainer getBagContainer()
{
return (BagContainer) inventorySlots;
}
@Override
protected boolean checkHotbarKeys(
final int keyCode )
{
if ( theSlot instanceof SlotBit )
{
final Slot s = inventorySlots.getSlotFromInventory( getBagContainer().thePlayer.inventory, 0 );
if ( s != null )
{
theSlot = s;
}
}
return super.checkHotbarKeys( keyCode );
}
@Override
public void drawScreen(
final int mouseX,
final int mouseY,
final float partialTicks )
{
drawDefaultBackground();
super.drawScreen( mouseX, mouseY, partialTicks );
func_191948_b( mouseX, mouseY );
}
@Override
protected void drawGuiContainerBackgroundLayer(
final float partialTicks,
final int mouseX,
final int mouseY )
{
final int xOffset = ( width - xSize ) / 2;
final int yOffset = ( height - ySize ) / 2;
GlStateManager.color( 1.0F, 1.0F, 1.0F, 1.0F );
mc.getTextureManager().bindTexture( BAG_GUI_TEXTURE );
this.drawTexturedModalRect( xOffset, yOffset, 0, 0, xSize, ySize );
}
private Slot getSlotAtPosition(
final int x,
final int y )
{
for ( int slotIdx = 0; slotIdx < getBagContainer().customSlots.size(); ++slotIdx )
{
final Slot slot = getBagContainer().customSlots.get( slotIdx );
if ( isMouseOverSlot( slot, x, y ) )
{
return slot;
}
}
return null;
}
@Override
protected void mouseClicked(
final int mouseX,
final int mouseY,
final int mouseButton ) throws IOException
{
// This is what vanilla does...
final boolean duplicateButton = mouseButton == mc.gameSettings.keyBindPickBlock.getKeyCode() + 100;
final Slot slot = getSlotAtPosition( mouseX, mouseY );
if ( slot != null )
{
final PacketBagGui bagGuiPacket = new PacketBagGui();
bagGuiPacket.slotNumber = slot.slotNumber;
bagGuiPacket.mouseButton = mouseButton;
bagGuiPacket.duplicateButton = duplicateButton;
bagGuiPacket.holdingShift = ClientSide.instance.holdingShift();
bagGuiPacket.doAction( ClientSide.instance.getPlayer() );
NetworkRouter.instance.sendToServer( bagGuiPacket );
return;
}
super.mouseClicked( mouseX, mouseY, mouseButton );
}
@Override
protected void drawGuiContainerForegroundLayer(
final int mouseX,
final int mouseY )
{
fontRendererObj.drawString( ChiselsAndBits.getItems().itemBitBag.getItemStackDisplayName( ModUtil.getEmptyStack() ), 8, 6, 0x404040 );
fontRendererObj.drawString( I18n.format( "container.inventory", new Object[0] ), 8, ySize - 93, 0x404040 );
RenderHelper.enableGUIStandardItemLighting();
if ( specialFontRenderer == null )
{
specialFontRenderer = new GuiBagFontRenderer( fontRendererObj, ChiselsAndBits.getConfig().bagStackSize );
}
for ( int slotIdx = 0; slotIdx < getBagContainer().customSlots.size(); ++slotIdx )
{
final Slot slot = getBagContainer().customSlots.get( slotIdx );
final FontRenderer defaultFontRenderer = fontRendererObj;
try
{
fontRendererObj = specialFontRenderer;
drawSlot( slot );
}
finally
{
fontRendererObj = defaultFontRenderer;
}
if ( isMouseOverSlot( slot, mouseX, mouseY ) && slot.canBeHovered() )
{
final int xDisplayPos = slot.xDisplayPosition;
final int yDisplayPos = slot.yDisplayPosition;
theSlot = slot;
GlStateManager.disableLighting();
GlStateManager.disableDepth();
GlStateManager.colorMask( true, true, true, false );
drawGradientRect( xDisplayPos, yDisplayPos, xDisplayPos + INNER_SLOT_SIZE, yDisplayPos + INNER_SLOT_SIZE, 0x80FFFFFF, 0x80FFFFFF );
GlStateManager.colorMask( true, true, true, true );
GlStateManager.enableLighting();
GlStateManager.enableDepth();
}
}
if ( sortBtn.isMouseOver() )
{
final List<String> text = Arrays
.asList( new String[] { LocalStrings.Sort.getLocal() } );
drawHoveringText( text, mouseX - guiLeft, mouseY - guiTop, fontRendererObj );
}
if ( trashBtn.isMouseOver() )
{
if ( isValidBitItem() )
{
final String msgNotConfirm = ModUtil.notEmpty( getInHandItem() ) ? LocalStrings.TrashItem.getLocal( getInHandItem().getDisplayName() ) : LocalStrings.Trash.getLocal();
final String msgConfirm = ModUtil.notEmpty( getInHandItem() ) ? LocalStrings.ReallyTrashItem.getLocal( getInHandItem().getDisplayName() ) : LocalStrings.ReallyTrash.getLocal();
final List<String> text = Arrays
.asList( new String[] { requireConfirm ? msgNotConfirm : msgConfirm } );
drawHoveringText( text, mouseX - guiLeft, mouseY - guiTop, fontRendererObj );
}
else
{
final List<String> text = Arrays
.asList( new String[] { LocalStrings.TrashInvalidItem.getLocal( getInHandItem().getDisplayName() ) } );
drawHoveringText( text, mouseX - guiLeft, mouseY - guiTop, fontRendererObj );
}
}
else
{
requireConfirm = true;
}
}
private ItemStack getInHandItem()
{
return getBagContainer().thePlayer.inventory.getItemStack();
}
boolean requireConfirm = true;
boolean dontThrow = false;
@Override
protected void actionPerformed(
final GuiButton button ) throws IOException
{
if ( button == sortBtn )
{
NetworkRouter.instance.sendToServer( new PacketSortBagGui() );
}
if ( button == trashBtn )
{
if ( requireConfirm )
{
dontThrow = true;
if ( isValidBitItem() )
{
requireConfirm = false;
}
}
else
{
requireConfirm = true;
// server side!
NetworkRouter.instance.sendToServer( new PacketClearBagGui( getInHandItem() ) );
dontThrow = false;
}
}
}
private boolean isValidBitItem()
{
return ModUtil.isEmpty( getInHandItem() ) || getInHandItem().getItem() == ChiselsAndBits.getItems().itemBlockBit;
}
@Override
protected void handleMouseClick(
final Slot slotIn,
final int slotId,
final int mouseButton,
final ClickType type )
{
if ( ( type == ClickType.PICKUP || type == ClickType.THROW ) && dontThrow )
{
dontThrow = false;
return;
}
super.handleMouseClick( slotIn, slotId, mouseButton, type );
}
}