001    package net.minecraft.src;
002    
003    import cpw.mods.fml.common.Side;
004    import cpw.mods.fml.common.asm.SideOnly;
005    import net.minecraft.client.Minecraft;
006    
007    @SideOnly(Side.CLIENT)
008    public class EntityPlayerSP extends EntityPlayer
009    {
010        public MovementInput movementInput;
011        protected Minecraft mc;
012    
013        /**
014         * Used to tell if the player pressed forward twice. If this is at 0 and it's pressed (And they are allowed to
015         * sprint, aka enough food on the ground etc) it sets this to 7. If it's pressed and it's greater than 0 enable
016         * sprinting.
017         */
018        protected int sprintToggleTimer = 0;
019    
020        /** Ticks left before sprinting is disabled. */
021        public int sprintingTicksLeft = 0;
022        public float renderArmYaw;
023        public float renderArmPitch;
024        public float prevRenderArmYaw;
025        public float prevRenderArmPitch;
026        private MouseFilter field_71162_ch = new MouseFilter();
027        private MouseFilter field_71160_ci = new MouseFilter();
028        private MouseFilter field_71161_cj = new MouseFilter();
029    
030        /** The amount of time an entity has been in a Portal */
031        public float timeInPortal;
032    
033        /** The amount of time an entity has been in a Portal the previous tick */
034        public float prevTimeInPortal;
035    
036        public EntityPlayerSP(Minecraft par1Minecraft, World par2World, Session par3Session, int par4)
037        {
038            super(par2World);
039            this.mc = par1Minecraft;
040            this.dimension = par4;
041    
042            if (par3Session != null && par3Session.username != null && par3Session.username.length() > 0)
043            {
044                this.skinUrl = "http://skins.minecraft.net/MinecraftSkins/" + StringUtils.stripControlCodes(par3Session.username) + ".png";
045            }
046    
047            this.username = par3Session.username;
048        }
049    
050        /**
051         * Tries to moves the entity by the passed in displacement. Args: x, y, z
052         */
053        public void moveEntity(double par1, double par3, double par5)
054        {
055            super.moveEntity(par1, par3, par5);
056        }
057    
058        public void updateEntityActionState()
059        {
060            super.updateEntityActionState();
061            this.moveStrafing = this.movementInput.moveStrafe;
062            this.moveForward = this.movementInput.moveForward;
063            this.isJumping = this.movementInput.jump;
064            this.prevRenderArmYaw = this.renderArmYaw;
065            this.prevRenderArmPitch = this.renderArmPitch;
066            this.renderArmPitch = (float)((double)this.renderArmPitch + (double)(this.rotationPitch - this.renderArmPitch) * 0.5D);
067            this.renderArmYaw = (float)((double)this.renderArmYaw + (double)(this.rotationYaw - this.renderArmYaw) * 0.5D);
068        }
069    
070        /**
071         * Returns whether the entity is in a local (client) world
072         */
073        protected boolean isClientWorld()
074        {
075            return true;
076        }
077    
078        /**
079         * Called frequently so the entity can update its state every tick as required. For example, zombies and skeletons
080         * use this to react to sunlight and start to burn.
081         */
082        public void onLivingUpdate()
083        {
084            if (this.sprintingTicksLeft > 0)
085            {
086                --this.sprintingTicksLeft;
087    
088                if (this.sprintingTicksLeft == 0)
089                {
090                    this.setSprinting(false);
091                }
092            }
093    
094            if (this.sprintToggleTimer > 0)
095            {
096                --this.sprintToggleTimer;
097            }
098    
099            if (this.mc.playerController.func_78747_a())
100            {
101                this.posX = this.posZ = 0.5D;
102                this.posX = 0.0D;
103                this.posZ = 0.0D;
104                this.rotationYaw = (float)this.ticksExisted / 12.0F;
105                this.rotationPitch = 10.0F;
106                this.posY = 68.5D;
107            }
108            else
109            {
110                if (!this.mc.statFileWriter.hasAchievementUnlocked(AchievementList.openInventory))
111                {
112                    this.mc.guiAchievement.queueAchievementInformation(AchievementList.openInventory);
113                }
114    
115                this.prevTimeInPortal = this.timeInPortal;
116    
117                if (this.inPortal)
118                {
119                    if (this.mc.currentScreen != null)
120                    {
121                        this.mc.displayGuiScreen((GuiScreen)null);
122                    }
123    
124                    if (this.timeInPortal == 0.0F)
125                    {
126                        this.mc.sndManager.playSoundFX("portal.trigger", 1.0F, this.rand.nextFloat() * 0.4F + 0.8F);
127                    }
128    
129                    this.timeInPortal += 0.0125F;
130    
131                    if (this.timeInPortal >= 1.0F)
132                    {
133                        this.timeInPortal = 1.0F;
134                    }
135    
136                    this.inPortal = false;
137                }
138                else if (this.isPotionActive(Potion.confusion) && this.getActivePotionEffect(Potion.confusion).getDuration() > 60)
139                {
140                    this.timeInPortal += 0.006666667F;
141    
142                    if (this.timeInPortal > 1.0F)
143                    {
144                        this.timeInPortal = 1.0F;
145                    }
146                }
147                else
148                {
149                    if (this.timeInPortal > 0.0F)
150                    {
151                        this.timeInPortal -= 0.05F;
152                    }
153    
154                    if (this.timeInPortal < 0.0F)
155                    {
156                        this.timeInPortal = 0.0F;
157                    }
158                }
159    
160                if (this.timeUntilPortal > 0)
161                {
162                    --this.timeUntilPortal;
163                }
164    
165                boolean var1 = this.movementInput.jump;
166                float var2 = 0.8F;
167                boolean var3 = this.movementInput.moveForward >= var2;
168                this.movementInput.updatePlayerMoveState();
169    
170                if (this.isUsingItem())
171                {
172                    this.movementInput.moveStrafe *= 0.2F;
173                    this.movementInput.moveForward *= 0.2F;
174                    this.sprintToggleTimer = 0;
175                }
176    
177                if (this.movementInput.sneak && this.ySize < 0.2F)
178                {
179                    this.ySize = 0.2F;
180                }
181    
182                this.pushOutOfBlocks(this.posX - (double)this.width * 0.35D, this.boundingBox.minY + 0.5D, this.posZ + (double)this.width * 0.35D);
183                this.pushOutOfBlocks(this.posX - (double)this.width * 0.35D, this.boundingBox.minY + 0.5D, this.posZ - (double)this.width * 0.35D);
184                this.pushOutOfBlocks(this.posX + (double)this.width * 0.35D, this.boundingBox.minY + 0.5D, this.posZ - (double)this.width * 0.35D);
185                this.pushOutOfBlocks(this.posX + (double)this.width * 0.35D, this.boundingBox.minY + 0.5D, this.posZ + (double)this.width * 0.35D);
186                boolean var4 = (float)this.getFoodStats().getFoodLevel() > 6.0F || this.capabilities.allowFlying;
187    
188                if (this.onGround && !var3 && this.movementInput.moveForward >= var2 && !this.isSprinting() && var4 && !this.isUsingItem() && !this.isPotionActive(Potion.blindness))
189                {
190                    if (this.sprintToggleTimer == 0)
191                    {
192                        this.sprintToggleTimer = 7;
193                    }
194                    else
195                    {
196                        this.setSprinting(true);
197                        this.sprintToggleTimer = 0;
198                    }
199                }
200    
201                if (this.isSneaking())
202                {
203                    this.sprintToggleTimer = 0;
204                }
205    
206                if (this.isSprinting() && (this.movementInput.moveForward < var2 || this.isCollidedHorizontally || !var4))
207                {
208                    this.setSprinting(false);
209                }
210    
211                if (this.capabilities.allowFlying && !var1 && this.movementInput.jump)
212                {
213                    if (this.flyToggleTimer == 0)
214                    {
215                        this.flyToggleTimer = 7;
216                    }
217                    else
218                    {
219                        this.capabilities.isFlying = !this.capabilities.isFlying;
220                        this.sendPlayerAbilities();
221                        this.flyToggleTimer = 0;
222                    }
223                }
224    
225                if (this.capabilities.isFlying)
226                {
227                    if (this.movementInput.sneak)
228                    {
229                        this.motionY -= 0.15D;
230                    }
231    
232                    if (this.movementInput.jump)
233                    {
234                        this.motionY += 0.15D;
235                    }
236                }
237    
238                super.onLivingUpdate();
239    
240                if (this.onGround && this.capabilities.isFlying)
241                {
242                    this.capabilities.isFlying = false;
243                    this.sendPlayerAbilities();
244                }
245            }
246        }
247    
248        /**
249         * Gets the player's field of view multiplier. (ex. when flying)
250         */
251        public float getFOVMultiplier()
252        {
253            float var1 = 1.0F;
254    
255            if (this.capabilities.isFlying)
256            {
257                var1 *= 1.1F;
258            }
259    
260            var1 *= (this.landMovementFactor * this.getSpeedModifier() / this.speedOnGround + 1.0F) / 2.0F;
261    
262            if (this.isUsingItem() && this.getItemInUse().itemID == Item.bow.shiftedIndex)
263            {
264                int var2 = this.getItemInUseDuration();
265                float var3 = (float)var2 / 20.0F;
266    
267                if (var3 > 1.0F)
268                {
269                    var3 = 1.0F;
270                }
271                else
272                {
273                    var3 *= var3;
274                }
275    
276                var1 *= 1.0F - var3 * 0.15F;
277            }
278    
279            return var1;
280        }
281    
282        public void updateCloak()
283        {
284            this.playerCloakUrl = "http://skins.minecraft.net/MinecraftCloaks/" + StringUtils.stripControlCodes(this.username) + ".png";
285            this.cloakUrl = this.playerCloakUrl;
286        }
287    
288        /**
289         * sets current screen to null (used on escape buttons of GUIs)
290         */
291        public void closeScreen()
292        {
293            super.closeScreen();
294            this.mc.displayGuiScreen((GuiScreen)null);
295        }
296    
297        /**
298         * Displays the GUI for editing a sign. Args: tileEntitySign
299         */
300        public void displayGUIEditSign(TileEntity par1TileEntity)
301        {
302            if (par1TileEntity instanceof TileEntitySign)
303            {
304                this.mc.displayGuiScreen(new GuiEditSign((TileEntitySign)par1TileEntity));
305            }
306            else if (par1TileEntity instanceof TileEntityCommandBlock)
307            {
308                this.mc.displayGuiScreen(new GuiCommandBlock((TileEntityCommandBlock)par1TileEntity));
309            }
310        }
311    
312        /**
313         * Displays the GUI for interacting with a book.
314         */
315        public void displayGUIBook(ItemStack par1ItemStack)
316        {
317            Item var2 = par1ItemStack.getItem();
318    
319            if (var2 == Item.writtenBook)
320            {
321                this.mc.displayGuiScreen(new GuiScreenBook(this, par1ItemStack, false));
322            }
323            else if (var2 == Item.writableBook)
324            {
325                this.mc.displayGuiScreen(new GuiScreenBook(this, par1ItemStack, true));
326            }
327        }
328    
329        /**
330         * Displays the GUI for interacting with a chest inventory. Args: chestInventory
331         */
332        public void displayGUIChest(IInventory par1IInventory)
333        {
334            this.mc.displayGuiScreen(new GuiChest(this.inventory, par1IInventory));
335        }
336    
337        /**
338         * Displays the crafting GUI for a workbench.
339         */
340        public void displayGUIWorkbench(int par1, int par2, int par3)
341        {
342            this.mc.displayGuiScreen(new GuiCrafting(this.inventory, this.worldObj, par1, par2, par3));
343        }
344    
345        public void displayGUIEnchantment(int par1, int par2, int par3)
346        {
347            this.mc.displayGuiScreen(new GuiEnchantment(this.inventory, this.worldObj, par1, par2, par3));
348        }
349    
350        /**
351         * Displays the GUI for interacting with an anvil.
352         */
353        public void displayGUIAnvil(int par1, int par2, int par3)
354        {
355            this.mc.displayGuiScreen(new GuiRepair(this.inventory, this.worldObj, par1, par2, par3));
356        }
357    
358        /**
359         * Displays the furnace GUI for the passed in furnace entity. Args: tileEntityFurnace
360         */
361        public void displayGUIFurnace(TileEntityFurnace par1TileEntityFurnace)
362        {
363            this.mc.displayGuiScreen(new GuiFurnace(this.inventory, par1TileEntityFurnace));
364        }
365    
366        /**
367         * Displays the GUI for interacting with a brewing stand.
368         */
369        public void displayGUIBrewingStand(TileEntityBrewingStand par1TileEntityBrewingStand)
370        {
371            this.mc.displayGuiScreen(new GuiBrewingStand(this.inventory, par1TileEntityBrewingStand));
372        }
373    
374        /**
375         * Displays the GUI for interacting with a beacon.
376         */
377        public void displayGUIBeacon(TileEntityBeacon par1TileEntityBeacon)
378        {
379            this.mc.displayGuiScreen(new GuiBeacon(this.inventory, par1TileEntityBeacon));
380        }
381    
382        /**
383         * Displays the dipsenser GUI for the passed in dispenser entity. Args: TileEntityDispenser
384         */
385        public void displayGUIDispenser(TileEntityDispenser par1TileEntityDispenser)
386        {
387            this.mc.displayGuiScreen(new GuiDispenser(this.inventory, par1TileEntityDispenser));
388        }
389    
390        public void displayGUIMerchant(IMerchant par1IMerchant)
391        {
392            this.mc.displayGuiScreen(new GuiMerchant(this.inventory, par1IMerchant, this.worldObj));
393        }
394    
395        /**
396         * Called when the player performs a critical hit on the Entity. Args: entity that was hit critically
397         */
398        public void onCriticalHit(Entity par1Entity)
399        {
400            this.mc.effectRenderer.addEffect(new EntityCrit2FX(this.mc.theWorld, par1Entity));
401        }
402    
403        public void onEnchantmentCritical(Entity par1Entity)
404        {
405            EntityCrit2FX var2 = new EntityCrit2FX(this.mc.theWorld, par1Entity, "magicCrit");
406            this.mc.effectRenderer.addEffect(var2);
407        }
408    
409        /**
410         * Called whenever an item is picked up from walking over it. Args: pickedUpEntity, stackSize
411         */
412        public void onItemPickup(Entity par1Entity, int par2)
413        {
414            this.mc.effectRenderer.addEffect(new EntityPickupFX(this.mc.theWorld, par1Entity, this, -0.5F));
415        }
416    
417        /**
418         * Returns if this entity is sneaking.
419         */
420        public boolean isSneaking()
421        {
422            return this.movementInput.sneak && !this.sleeping;
423        }
424    
425        /**
426         * Updates health locally.
427         */
428        public void setHealth(int par1)
429        {
430            int var2 = this.getHealth() - par1;
431    
432            if (var2 <= 0)
433            {
434                this.setEntityHealth(par1);
435    
436                if (var2 < 0)
437                {
438                    this.hurtResistantTime = this.maxHurtResistantTime / 2;
439                }
440            }
441            else
442            {
443                this.lastDamage = var2;
444                this.setEntityHealth(this.getHealth());
445                this.hurtResistantTime = this.maxHurtResistantTime;
446                this.damageEntity(DamageSource.generic, var2);
447                this.hurtTime = this.maxHurtTime = 10;
448            }
449        }
450    
451        /**
452         * Add a chat message to the player
453         */
454        public void addChatMessage(String par1Str)
455        {
456            this.mc.ingameGUI.getChatGUI().addTranslatedMessage(par1Str, new Object[0]);
457        }
458    
459        /**
460         * Adds a value to a statistic field.
461         */
462        public void addStat(StatBase par1StatBase, int par2)
463        {
464            if (par1StatBase != null)
465            {
466                if (par1StatBase.isAchievement())
467                {
468                    Achievement var3 = (Achievement)par1StatBase;
469    
470                    if (var3.parentAchievement == null || this.mc.statFileWriter.hasAchievementUnlocked(var3.parentAchievement))
471                    {
472                        if (!this.mc.statFileWriter.hasAchievementUnlocked(var3))
473                        {
474                            this.mc.guiAchievement.queueTakenAchievement(var3);
475                        }
476    
477                        this.mc.statFileWriter.readStat(par1StatBase, par2);
478                    }
479                }
480                else
481                {
482                    this.mc.statFileWriter.readStat(par1StatBase, par2);
483                }
484            }
485        }
486    
487        private boolean isBlockTranslucent(int par1, int par2, int par3)
488        {
489            return this.worldObj.isBlockNormalCube(par1, par2, par3);
490        }
491    
492        /**
493         * Adds velocity to push the entity out of blocks at the specified x, y, z position Args: x, y, z
494         */
495        protected boolean pushOutOfBlocks(double par1, double par3, double par5)
496        {
497            int var7 = MathHelper.floor_double(par1);
498            int var8 = MathHelper.floor_double(par3);
499            int var9 = MathHelper.floor_double(par5);
500            double var10 = par1 - (double)var7;
501            double var12 = par5 - (double)var9;
502    
503            if (this.isBlockTranslucent(var7, var8, var9) || this.isBlockTranslucent(var7, var8 + 1, var9))
504            {
505                boolean var14 = !this.isBlockTranslucent(var7 - 1, var8, var9) && !this.isBlockTranslucent(var7 - 1, var8 + 1, var9);
506                boolean var15 = !this.isBlockTranslucent(var7 + 1, var8, var9) && !this.isBlockTranslucent(var7 + 1, var8 + 1, var9);
507                boolean var16 = !this.isBlockTranslucent(var7, var8, var9 - 1) && !this.isBlockTranslucent(var7, var8 + 1, var9 - 1);
508                boolean var17 = !this.isBlockTranslucent(var7, var8, var9 + 1) && !this.isBlockTranslucent(var7, var8 + 1, var9 + 1);
509                byte var18 = -1;
510                double var19 = 9999.0D;
511    
512                if (var14 && var10 < var19)
513                {
514                    var19 = var10;
515                    var18 = 0;
516                }
517    
518                if (var15 && 1.0D - var10 < var19)
519                {
520                    var19 = 1.0D - var10;
521                    var18 = 1;
522                }
523    
524                if (var16 && var12 < var19)
525                {
526                    var19 = var12;
527                    var18 = 4;
528                }
529    
530                if (var17 && 1.0D - var12 < var19)
531                {
532                    var19 = 1.0D - var12;
533                    var18 = 5;
534                }
535    
536                float var21 = 0.1F;
537    
538                if (var18 == 0)
539                {
540                    this.motionX = (double)(-var21);
541                }
542    
543                if (var18 == 1)
544                {
545                    this.motionX = (double)var21;
546                }
547    
548                if (var18 == 4)
549                {
550                    this.motionZ = (double)(-var21);
551                }
552    
553                if (var18 == 5)
554                {
555                    this.motionZ = (double)var21;
556                }
557            }
558    
559            return false;
560        }
561    
562        /**
563         * Set sprinting switch for Entity.
564         */
565        public void setSprinting(boolean par1)
566        {
567            super.setSprinting(par1);
568            this.sprintingTicksLeft = par1 ? 600 : 0;
569        }
570    
571        /**
572         * Sets the current XP, total XP, and level number.
573         */
574        public void setXPStats(float par1, int par2, int par3)
575        {
576            this.experience = par1;
577            this.experienceTotal = par2;
578            this.experienceLevel = par3;
579        }
580    
581        public void sendChatToPlayer(String par1Str)
582        {
583            this.mc.ingameGUI.getChatGUI().printChatMessage(par1Str);
584        }
585    
586        /**
587         * Returns true if the command sender is allowed to use the given command.
588         */
589        public boolean canCommandSenderUseCommand(int par1, String par2Str)
590        {
591            return par1 <= 0;
592        }
593    
594        /**
595         * Return the coordinates for this player as ChunkCoordinates.
596         */
597        public ChunkCoordinates getPlayerCoordinates()
598        {
599            return new ChunkCoordinates(MathHelper.floor_double(this.posX + 0.5D), MathHelper.floor_double(this.posY + 0.5D), MathHelper.floor_double(this.posZ + 0.5D));
600        }
601    
602        /**
603         * Returns the item that this EntityLiving is holding, if any.
604         */
605        public ItemStack getHeldItem()
606        {
607            return this.inventory.getCurrentItem();
608        }
609    
610        protected void func_85030_a(String par1Str, float par2, float par3)
611        {
612            this.worldObj.playSound(this.posX, this.posY - (double)this.yOffset, this.posZ, par1Str, par2, par3);
613        }
614    }