Advertisement
PaleoCrafter

The potion hud

Mar 29th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.92 KB | None | 0 0
  1. public class PotionHud implements IHud
  2. {
  3.  
  4.     private boolean iconsOnly;
  5.     private HudBox box;
  6.     protected static final ResourceLocation icons = new ResourceLocation("textures/gui/container/inventory.png");
  7.  
  8.     public PotionHud()
  9.     {
  10.         this.box = new HudBox(0, 0, 100, 25);
  11.         iconsOnly = false;
  12.     }
  13.  
  14.     @Override
  15.     public void draw()
  16.     {
  17.         // Check whether the inventory is opened, if so, don't render potions
  18.         if (!(RenderHelper.getMC().currentScreen instanceof InventoryEffectRenderer))
  19.         {
  20.             int x = 10;
  21.             int y = 10;
  22.             int yOffGlob = 0;
  23.             // Get all active potion effects
  24.             LinkedList<?> activePotions = new LinkedList<Object>(RenderHelper.getMC().thePlayer.getActivePotionEffects());
  25.             Collections.sort(activePotions, new Comparator<Object>()
  26.             {
  27.                 @Override
  28.                 public int compare(Object o, Object o2)
  29.                 {
  30.                     return ((PotionEffect) o2).getDuration() - ((PotionEffect) o).getDuration();
  31.                 }
  32.             });
  33.             // If there are any active effects, render them into the screen
  34.             if (!activePotions.isEmpty())
  35.             {
  36.                 GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
  37.                 GL11.glDisable(GL11.GL_LIGHTING);
  38.                 int yOff = 28;
  39.  
  40.                 if (activePotions.size() > 5)
  41.                 {
  42.                     yOff = 132 / (activePotions.size() - 1);
  43.                 }
  44.  
  45.                 if (iconsOnly)
  46.                 {
  47.                     box.setPosition(10, 10);
  48.                     box.setSize(120, 25);
  49.                     box.draw();
  50.                 }
  51.  
  52.                 for (int i = 0; i < activePotions.size(); i++)
  53.                 {
  54.                     PotionEffect effect = (PotionEffect) activePotions.toArray()[i];
  55.                     Potion potion = Potion.potionTypes[effect.getPotionID()];
  56.                     GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
  57.                     float alpha = 1F;
  58.  
  59.                     // Slowly fade out the potion
  60.                     if (effect.getDuration() <= 20)
  61.                     {
  62.                         GL11.glEnable(GL11.GL_BLEND);
  63.                         GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
  64.                         alpha = 1F / (21 - effect.getDuration());
  65.                         GL11.glColor4f(1, 1, 1, alpha);
  66.                     }
  67.                     if (!iconsOnly)
  68.                     {
  69.                         if (RenderHelper.getMC().gameSettings.guiScale == 1 && (box.getWidth() == 100 || box.getWidth() == 120))
  70.                         {
  71.                             box.setSize(140, 25);
  72.                             yOffGlob = 1;
  73.                         } else if (box.getWidth() == 140 || box.getWidth() == 120)
  74.                         {
  75.                             box.setSize(100, 25);
  76.                             yOffGlob = 0;
  77.                         }
  78.                         box.setPosition(x, y + yOff * i);
  79.                         box.draw();
  80.  
  81.                         if (potion.hasStatusIcon())
  82.                         {
  83.                             int icon = potion.getStatusIconIndex();
  84.                             RenderHelper.drawRectangle(icons, x + 5, y + i * yOff + 4, icon % 8 * 18, 198 + icon / 8 * 18, 18, 18, 1);
  85.                         }
  86.  
  87.                         String name = I18n.format(potion.getName());
  88.  
  89.                         if (effect.getAmplifier() == 1)
  90.                         {
  91.                             name = name + " II";
  92.                         } else if (effect.getAmplifier() == 2)
  93.                         {
  94.                             name = name + " III";
  95.                         } else if (effect.getAmplifier() == 3)
  96.                         {
  97.                             name = name + " IV";
  98.                         }
  99.  
  100.                         int alphaColorName = ((int) Math.floor(255 * alpha)) << 24 | 0xFFFFFF;
  101.                         int alphaColorDuration = ((int) Math.floor(255 * alpha)) << 24 | 0xCCCCCC;
  102.  
  103.                         RenderHelper.drawSmallString(name, x + 7 + 18, y + i * yOff + 4, alphaColorName, true, 1);
  104.                         String duration = Potion.getDurationString(effect);
  105.                         RenderHelper.drawSmallString(duration, x + 7 + 18, y + i * yOff + 4 + 8 + yOffGlob, alphaColorDuration, true, 1);
  106.                     } else
  107.                     {
  108.                         if (potion.hasStatusIcon())
  109.                         {
  110.                             int icon = potion.getStatusIconIndex();
  111.                             RenderHelper.drawRectangle(icons, 15 + i * 19, 14, icon % 8 * 18, 198 + icon / 8 * 18, 18, 18, 1);
  112.                         }
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.     }
  118.  
  119.     public void toggleMode()
  120.     {
  121.         this.iconsOnly = !iconsOnly;
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement