001    package net.minecraft.src;
002    
003    import cpw.mods.fml.common.Side;
004    import cpw.mods.fml.common.asm.SideOnly;
005    import java.io.ByteArrayOutputStream;
006    import java.io.DataOutputStream;
007    import org.lwjgl.input.Keyboard;
008    import org.lwjgl.opengl.GL11;
009    
010    @SideOnly(Side.CLIENT)
011    public class GuiScreenBook extends GuiScreen
012    {
013        private final EntityPlayer field_74169_a;
014        private final ItemStack field_74167_b;
015        private final boolean field_74168_c;
016        private boolean field_74166_d;
017        private boolean field_74172_m;
018        private int field_74170_n;
019        private int field_74171_o = 192;
020        private int field_74180_p = 192;
021        private int field_74179_q = 1;
022        private int field_74178_r;
023        private NBTTagList field_74177_s;
024        private String field_74176_t = "";
025        private GuiButtonNextPage field_74175_u;
026        private GuiButtonNextPage field_74174_v;
027        private GuiButton field_74173_w;
028        private GuiButton field_74183_x;
029        private GuiButton field_74182_y;
030        private GuiButton field_74181_z;
031    
032        public GuiScreenBook(EntityPlayer par1EntityPlayer, ItemStack par2ItemStack, boolean par3)
033        {
034            this.field_74169_a = par1EntityPlayer;
035            this.field_74167_b = par2ItemStack;
036            this.field_74168_c = par3;
037    
038            if (par2ItemStack.hasTagCompound())
039            {
040                NBTTagCompound var4 = par2ItemStack.getTagCompound();
041                this.field_74177_s = var4.getTagList("pages");
042    
043                if (this.field_74177_s != null)
044                {
045                    this.field_74177_s = (NBTTagList)this.field_74177_s.copy();
046                    this.field_74179_q = this.field_74177_s.tagCount();
047    
048                    if (this.field_74179_q < 1)
049                    {
050                        this.field_74179_q = 1;
051                    }
052                }
053            }
054    
055            if (this.field_74177_s == null && par3)
056            {
057                this.field_74177_s = new NBTTagList("pages");
058                this.field_74177_s.appendTag(new NBTTagString("1", ""));
059                this.field_74179_q = 1;
060            }
061        }
062    
063        /**
064         * Called from the main game loop to update the screen.
065         */
066        public void updateScreen()
067        {
068            super.updateScreen();
069            ++this.field_74170_n;
070        }
071    
072        /**
073         * Adds the buttons (and other controls) to the screen in question.
074         */
075        public void initGui()
076        {
077            this.controlList.clear();
078            Keyboard.enableRepeatEvents(true);
079    
080            if (this.field_74168_c)
081            {
082                this.controlList.add(this.field_74183_x = new GuiButton(3, this.width / 2 - 100, 4 + this.field_74180_p, 98, 20, StatCollector.translateToLocal("book.signButton")));
083                this.controlList.add(this.field_74173_w = new GuiButton(0, this.width / 2 + 2, 4 + this.field_74180_p, 98, 20, StatCollector.translateToLocal("gui.done")));
084                this.controlList.add(this.field_74182_y = new GuiButton(5, this.width / 2 - 100, 4 + this.field_74180_p, 98, 20, StatCollector.translateToLocal("book.finalizeButton")));
085                this.controlList.add(this.field_74181_z = new GuiButton(4, this.width / 2 + 2, 4 + this.field_74180_p, 98, 20, StatCollector.translateToLocal("gui.cancel")));
086            }
087            else
088            {
089                this.controlList.add(this.field_74173_w = new GuiButton(0, this.width / 2 - 100, 4 + this.field_74180_p, 200, 20, StatCollector.translateToLocal("gui.done")));
090            }
091    
092            int var1 = (this.width - this.field_74171_o) / 2;
093            byte var2 = 2;
094            this.controlList.add(this.field_74175_u = new GuiButtonNextPage(1, var1 + 120, var2 + 154, true));
095            this.controlList.add(this.field_74174_v = new GuiButtonNextPage(2, var1 + 38, var2 + 154, false));
096            this.func_74161_g();
097        }
098    
099        /**
100         * Called when the screen is unloaded. Used to disable keyboard repeat events
101         */
102        public void onGuiClosed()
103        {
104            Keyboard.enableRepeatEvents(false);
105        }
106    
107        private void func_74161_g()
108        {
109            this.field_74175_u.drawButton = !this.field_74172_m && (this.field_74178_r < this.field_74179_q - 1 || this.field_74168_c);
110            this.field_74174_v.drawButton = !this.field_74172_m && this.field_74178_r > 0;
111            this.field_74173_w.drawButton = !this.field_74168_c || !this.field_74172_m;
112    
113            if (this.field_74168_c)
114            {
115                this.field_74183_x.drawButton = !this.field_74172_m;
116                this.field_74181_z.drawButton = this.field_74172_m;
117                this.field_74182_y.drawButton = this.field_74172_m;
118                this.field_74182_y.enabled = this.field_74176_t.trim().length() > 0;
119            }
120        }
121    
122        private void func_74163_a(boolean par1)
123        {
124            if (this.field_74168_c && this.field_74166_d)
125            {
126                if (this.field_74177_s != null)
127                {
128                    while (this.field_74177_s.tagCount() > 1)
129                    {
130                        NBTTagString var2 = (NBTTagString)this.field_74177_s.tagAt(this.field_74177_s.tagCount() - 1);
131    
132                        if (var2.data != null && var2.data.length() != 0)
133                        {
134                            break;
135                        }
136    
137                        this.field_74177_s.func_74744_a(this.field_74177_s.tagCount() - 1);
138                    }
139    
140                    if (this.field_74167_b.hasTagCompound())
141                    {
142                        NBTTagCompound var7 = this.field_74167_b.getTagCompound();
143                        var7.setTag("pages", this.field_74177_s);
144                    }
145                    else
146                    {
147                        this.field_74167_b.func_77983_a("pages", this.field_74177_s);
148                    }
149    
150                    String var8 = "MC|BEdit";
151    
152                    if (par1)
153                    {
154                        var8 = "MC|BSign";
155                        this.field_74167_b.func_77983_a("author", new NBTTagString("author", this.field_74169_a.username));
156                        this.field_74167_b.func_77983_a("title", new NBTTagString("title", this.field_74176_t.trim()));
157                        this.field_74167_b.itemID = Item.writtenBook.shiftedIndex;
158                    }
159    
160                    ByteArrayOutputStream var3 = new ByteArrayOutputStream();
161                    DataOutputStream var4 = new DataOutputStream(var3);
162    
163                    try
164                    {
165                        Packet.writeItemStack(this.field_74167_b, var4);
166                        this.mc.getSendQueue().addToSendQueue(new Packet250CustomPayload(var8, var3.toByteArray()));
167                    }
168                    catch (Exception var6)
169                    {
170                        var6.printStackTrace();
171                    }
172                }
173            }
174        }
175    
176        /**
177         * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
178         */
179        protected void actionPerformed(GuiButton par1GuiButton)
180        {
181            if (par1GuiButton.enabled)
182            {
183                if (par1GuiButton.id == 0)
184                {
185                    this.mc.displayGuiScreen((GuiScreen)null);
186                    this.func_74163_a(false);
187                }
188                else if (par1GuiButton.id == 3 && this.field_74168_c)
189                {
190                    this.field_74172_m = true;
191                }
192                else if (par1GuiButton.id == 1)
193                {
194                    if (this.field_74178_r < this.field_74179_q - 1)
195                    {
196                        ++this.field_74178_r;
197                    }
198                    else if (this.field_74168_c)
199                    {
200                        this.func_74165_h();
201    
202                        if (this.field_74178_r < this.field_74179_q - 1)
203                        {
204                            ++this.field_74178_r;
205                        }
206                    }
207                }
208                else if (par1GuiButton.id == 2)
209                {
210                    if (this.field_74178_r > 0)
211                    {
212                        --this.field_74178_r;
213                    }
214                }
215                else if (par1GuiButton.id == 5 && this.field_74172_m)
216                {
217                    this.func_74163_a(true);
218                    this.mc.displayGuiScreen((GuiScreen)null);
219                }
220                else if (par1GuiButton.id == 4 && this.field_74172_m)
221                {
222                    this.field_74172_m = false;
223                }
224    
225                this.func_74161_g();
226            }
227        }
228    
229        private void func_74165_h()
230        {
231            if (this.field_74177_s != null && this.field_74177_s.tagCount() < 50)
232            {
233                this.field_74177_s.appendTag(new NBTTagString("" + (this.field_74179_q + 1), ""));
234                ++this.field_74179_q;
235                this.field_74166_d = true;
236            }
237        }
238    
239        /**
240         * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e).
241         */
242        protected void keyTyped(char par1, int par2)
243        {
244            super.keyTyped(par1, par2);
245    
246            if (this.field_74168_c)
247            {
248                if (this.field_74172_m)
249                {
250                    this.func_74162_c(par1, par2);
251                }
252                else
253                {
254                    this.func_74164_b(par1, par2);
255                }
256            }
257        }
258    
259        private void func_74164_b(char par1, int par2)
260        {
261            switch (par1)
262            {
263                case 22:
264                    this.func_74160_b(GuiScreen.getClipboardString());
265                    return;
266                default:
267                    switch (par2)
268                    {
269                        case 14:
270                            String var3 = this.func_74158_i();
271    
272                            if (var3.length() > 0)
273                            {
274                                this.func_74159_a(var3.substring(0, var3.length() - 1));
275                            }
276    
277                            return;
278                        case 28:
279                            this.func_74160_b("\n");
280                            return;
281                        default:
282                            if (ChatAllowedCharacters.isAllowedCharacter(par1))
283                            {
284                                this.func_74160_b(Character.toString(par1));
285                            }
286                    }
287            }
288        }
289    
290        private void func_74162_c(char par1, int par2)
291        {
292            switch (par2)
293            {
294                case 14:
295                    if (this.field_74176_t.length() > 0)
296                    {
297                        this.field_74176_t = this.field_74176_t.substring(0, this.field_74176_t.length() - 1);
298                        this.func_74161_g();
299                    }
300    
301                    return;
302                case 28:
303                    if (this.field_74176_t.length() > 0)
304                    {
305                        this.func_74163_a(true);
306                        this.mc.displayGuiScreen((GuiScreen)null);
307                    }
308    
309                    return;
310                default:
311                    if (this.field_74176_t.length() < 16 && ChatAllowedCharacters.isAllowedCharacter(par1))
312                    {
313                        this.field_74176_t = this.field_74176_t + Character.toString(par1);
314                        this.func_74161_g();
315                        this.field_74166_d = true;
316                    }
317            }
318        }
319    
320        private String func_74158_i()
321        {
322            if (this.field_74177_s != null && this.field_74178_r >= 0 && this.field_74178_r < this.field_74177_s.tagCount())
323            {
324                NBTTagString var1 = (NBTTagString)this.field_74177_s.tagAt(this.field_74178_r);
325                return var1.toString();
326            }
327            else
328            {
329                return "";
330            }
331        }
332    
333        private void func_74159_a(String par1Str)
334        {
335            if (this.field_74177_s != null && this.field_74178_r >= 0 && this.field_74178_r < this.field_74177_s.tagCount())
336            {
337                NBTTagString var2 = (NBTTagString)this.field_74177_s.tagAt(this.field_74178_r);
338                var2.data = par1Str;
339                this.field_74166_d = true;
340            }
341        }
342    
343        private void func_74160_b(String par1Str)
344        {
345            String var2 = this.func_74158_i();
346            String var3 = var2 + par1Str;
347            int var4 = this.fontRenderer.splitStringWidth(var3 + "\u00a70_", 118);
348    
349            if (var4 <= 118 && var3.length() < 256)
350            {
351                this.func_74159_a(var3);
352            }
353        }
354    
355        /**
356         * Draws the screen and all the components in it.
357         */
358        public void drawScreen(int par1, int par2, float par3)
359        {
360            int var4 = this.mc.renderEngine.getTexture("/gui/book.png");
361            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
362            this.mc.renderEngine.bindTexture(var4);
363            int var5 = (this.width - this.field_74171_o) / 2;
364            byte var6 = 2;
365            this.drawTexturedModalRect(var5, var6, 0, 0, this.field_74171_o, this.field_74180_p);
366            String var7;
367            String var8;
368            int var9;
369    
370            if (this.field_74172_m)
371            {
372                var7 = this.field_74176_t;
373    
374                if (this.field_74168_c)
375                {
376                    if (this.field_74170_n / 6 % 2 == 0)
377                    {
378                        var7 = var7 + "\u00a70_";
379                    }
380                    else
381                    {
382                        var7 = var7 + "\u00a77_";
383                    }
384                }
385    
386                var8 = StatCollector.translateToLocal("book.editTitle");
387                var9 = this.fontRenderer.getStringWidth(var8);
388                this.fontRenderer.drawString(var8, var5 + 36 + (116 - var9) / 2, var6 + 16 + 16, 0);
389                int var10 = this.fontRenderer.getStringWidth(var7);
390                this.fontRenderer.drawString(var7, var5 + 36 + (116 - var10) / 2, var6 + 48, 0);
391                String var11 = String.format(StatCollector.translateToLocal("book.byAuthor"), new Object[] {this.field_74169_a.username});
392                int var12 = this.fontRenderer.getStringWidth(var11);
393                this.fontRenderer.drawString("\u00a78" + var11, var5 + 36 + (116 - var12) / 2, var6 + 48 + 10, 0);
394                String var13 = StatCollector.translateToLocal("book.finalizeWarning");
395                this.fontRenderer.drawSplitString(var13, var5 + 36, var6 + 80, 116, 0);
396            }
397            else
398            {
399                var7 = String.format(StatCollector.translateToLocal("book.pageIndicator"), new Object[] {Integer.valueOf(this.field_74178_r + 1), Integer.valueOf(this.field_74179_q)});
400                var8 = "";
401    
402                if (this.field_74177_s != null && this.field_74178_r >= 0 && this.field_74178_r < this.field_74177_s.tagCount())
403                {
404                    NBTTagString var14 = (NBTTagString)this.field_74177_s.tagAt(this.field_74178_r);
405                    var8 = var14.toString();
406                }
407    
408                if (this.field_74168_c)
409                {
410                    if (this.fontRenderer.getBidiFlag())
411                    {
412                        var8 = var8 + "_";
413                    }
414                    else if (this.field_74170_n / 6 % 2 == 0)
415                    {
416                        var8 = var8 + "\u00a70_";
417                    }
418                    else
419                    {
420                        var8 = var8 + "\u00a77_";
421                    }
422                }
423    
424                var9 = this.fontRenderer.getStringWidth(var7);
425                this.fontRenderer.drawString(var7, var5 - var9 + this.field_74171_o - 44, var6 + 16, 0);
426                this.fontRenderer.drawSplitString(var8, var5 + 36, var6 + 16 + 16, 116, 0);
427            }
428    
429            super.drawScreen(par1, par2, par3);
430        }
431    }