Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.26 KB | None | 0 0
  1. package us.mcpvpmod.gui.info;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. import org.apache.commons.lang3.StringUtils;
  9.  
  10. import net.minecraft.client.Minecraft;
  11. import net.minecraft.client.gui.FontRenderer;
  12. import net.minecraft.client.gui.ScaledResolution;
  13. import us.mcpvpmod.Data;
  14. import us.mcpvpmod.Server;
  15. import us.mcpvpmod.config.all.ConfigHUD;
  16. import us.mcpvpmod.game.FriendsList;
  17. import us.mcpvpmod.game.state.State;
  18. import us.mcpvpmod.game.vars.AllVars;
  19. import us.mcpvpmod.gui.Draw;
  20. import us.mcpvpmod.util.Format;
  21. import cpw.mods.fml.common.FMLLog;
  22.  
  23. /**
  24.  * The core item on the HUD.
  25.  * Contains information relevant to games and the player.
  26.  * @author NomNuggetNom
  27.  */
  28. public class InfoBlock implements ISelectable {
  29.    
  30.     /** An array of all InfoBlocks, not just ones that are visible! */
  31.     public static ArrayList<InfoBlock> blocks = new ArrayList<InfoBlock>();
  32.    
  33.     /** The original information to process. **/
  34.     ArrayList<String> toDisplay = new ArrayList<String>();
  35.     /** The processed information to display. **/
  36.     ArrayList<String> display = new ArrayList<String>();
  37.    
  38.     /** Used to test for position arguments in a string. */
  39.     static String rePos = ".*\\((\".*\")*(\\+|-)*(\\d+)(%)*, (\".*\")*(\\+|-)*(\\d+)(%)*\\).*";
  40.     /** Used to get the position arguments. */
  41.     static String rePosNoCapture = "\\((\".*\")*(\\+|-)*(\\d+)(%)*, (\".*\")*(\\+|-)*(\\d+)(%)*\\)";
  42.     /** Used to remove all of the position arguments. */
  43.     static String rePosCapture = "(\\((\".*\")*(\\+|-)*(\\d+)(%)*, (\".*\")*(\\+|-)*(\\d+)(%)*\\))";
  44.     /** The unique divider separates strings when processing. */
  45.     static String uniqueString = "=:::=";
  46.     /** Used to identify title strings, both in config and in processing. */
  47.     static String titleString = "---";
  48.     /** The number of pixels between text and the edge of the drawn box. */
  49.     static int padding = 3;
  50.     /** Whether or not to render BGs */
  51.     static boolean bg = ConfigHUD.renderBG;
  52.    
  53.     private String title;
  54.     State state;
  55.     Server server;
  56.     String coords;
  57.     public int baseX = ConfigHUD.margin;
  58.     public int baseY = ConfigHUD.margin;
  59.     public int w;
  60.     public int h;
  61.    
  62.     boolean matchW = ConfigHUD.alignWidths;
  63.     boolean matchH = ConfigHUD.alignHeights;
  64.     boolean selected = false;
  65.     public static InfoBlock selectedBlock;
  66.    
  67.     static Minecraft mc = Minecraft.getMinecraft();
  68.     static FontRenderer fR = mc.fontRenderer;
  69.  
  70.     /**
  71.      * The backbone of the HUD.
  72.      * @param title The title of the block. The title is rendered with a special darker back to distinguish it.
  73.      * @param info The lines to render, which will be processed every render tick.
  74.      * @param server The server to render the block on.
  75.      * @param state The state to render the block on.
  76.      */
  77.     public InfoBlock(String title, ArrayList<String> info, Server server, State state) {
  78.         this.coords = title.replaceAll(rePosCapture, "$1");
  79.         this.setTitle(title.replaceAll(rePosNoCapture, "").replaceAll("^\\s\\s*", "").replaceAll("\\s\\s*$", ""));
  80.         this.toDisplay = info;
  81.         this.state = state;
  82.         this.server = server;
  83.        
  84.         blocks.add(this);
  85.        
  86.         //infoBlocks.put(Format.process(title), this);
  87.         FMLLog.info("[MCPVP] Registered new InfoBlock %s", this.getTitle());
  88.     }
  89.    
  90.     /**
  91.      * Returns an InfoBlock that has the specified id (title)
  92.      * @param getTitle
  93.      * @return
  94.      */
  95.     public static InfoBlock get(String getTitle) {     
  96.         for (InfoBlock block : blocks) {
  97.             if (block.getTitle().equals(getTitle)) {
  98.                 return block;
  99.             }
  100.         }
  101.         FMLLog.info("[MCPVP] Couldn't find an InfoBlock with the title \"%s\"", getTitle);
  102.         return null;
  103.     }
  104.    
  105.     /**
  106.      * Returns all InfoBlocks in the specified Server.
  107.      * @param server
  108.      * @return
  109.      */
  110.     public static ArrayList<InfoBlock> get(Server server, State state) {
  111.         ArrayList<InfoBlock> toReturn = new ArrayList<InfoBlock>();
  112.         for (InfoBlock block : blocks) {
  113.             if (block.server == server && block.state == state) {
  114.                 toReturn.add(block);
  115.             }
  116.         }
  117.         return toReturn;
  118.     }
  119.    
  120.     /**
  121.      * Cycles through the given String[] and breaks it up into blocks.
  122.      * Information is NOT processed here.
  123.      * @param configList
  124.      */
  125.     public static void createBlocks(String[] configList, Server server, State state) {
  126.         FMLLog.info("[MCPVP] Creating info blocks for server %s and state %s", server, state);
  127.        
  128.         String finalString = "";
  129.        
  130.         for (String string : configList) {
  131.             finalString = finalString + string + uniqueString;
  132.         }
  133.        
  134.         for (String blockText : finalString.split(titleString)) {
  135.            
  136.             // Skip empty blocks.
  137.             if (blockText.equals("") || blockText.equals(uniqueString)) continue;
  138.  
  139.             // The title is the first entry of the string.
  140.             String title = blockText.split(uniqueString)[0];
  141.            
  142.             // Cover the rest of the words
  143.             String theRest = blockText.replace(title, "");
  144.             ArrayList<String> lines = new ArrayList<String>(Arrays.asList(theRest.split(uniqueString)));
  145.             if (lines.size() == 0) return;
  146.             lines.remove(0);
  147.  
  148.             if (lines.size() > 0) {
  149.                 InfoBlock block = new InfoBlock(Format.process(title), lines, server, state);
  150.             } else {
  151.                 FMLLog.info("[MCPVP] Not enough lines for InfoBlock " + title);
  152.             }
  153.         }
  154.     }
  155.    
  156.     /**
  157.      * The main call that displays the block.
  158.      * Updates information and dimensions then draws.
  159.      */
  160.     public void display() {
  161.         fR = mc.fontRenderer;
  162.  
  163.         this.update();
  164.         this.setW();
  165.         this.setH();
  166.         this.setX();
  167.         this.setY();
  168.         this.draw();
  169.     }
  170.    
  171.     /**
  172.      * Replaces {variables} with their value.
  173.      * Handles formatting codes.
  174.      */
  175.     public void update() {
  176.         display.clear();
  177.        
  178.         for (String line : this.toDisplay) {
  179.             // Run the line through our processors.
  180.             line = processVars(line);
  181.             line = Format.process(line);
  182.            
  183.             if (line.equals("friends")) {
  184.                 // This adds all our friends.
  185.                 display.addAll(FriendsList.getFriends());
  186.                 // Friends block shouldn't match height.
  187.                 this.matchH = false;
  188.             } else {
  189.                 // Add the changed line to our info block for rendering.
  190.                 // Sometimes "null" will be returned to signal that the string doesn't need to be shown.
  191.                 // e.g. Free day returns "null" when it's not free day.
  192.                 if (line != "null") {
  193.                     display.add(line);
  194.                 }
  195.             }
  196.         }
  197.     }
  198.    
  199.     /**
  200.      * Calculates the width of the block based only on the current block.
  201.      * @return Width
  202.      */
  203.     public int calcW() {
  204.         int calcW = 0;
  205.         int lineW = 0;
  206.        
  207.         // Cycle through our information and calculate the length.
  208.         for (String line : this.display) {     
  209.             if (ConfigHUD.alignItems)  
  210.                 lineW = fR.getStringWidth(line) + align(line);
  211.             else lineW = fR.getStringWidth(line);
  212.            
  213.             // Check if it's the longest yet.
  214.             if (lineW > calcW) {
  215.                 calcW = lineW;
  216.             }
  217.         }
  218.        
  219.         // Include our title in the check.
  220.         if (fR.getStringWidth(this.getTitle()) > calcW) {
  221.             calcW = fR.getStringWidth(this.getTitle());
  222.         }
  223.         return calcW;
  224.     }
  225.    
  226.     /**
  227.      * Sets the width of the block.
  228.      * Accounts for matching widths.
  229.      */
  230.     public void setW() {
  231.         int newW = this.calcW();
  232.        
  233.         // Cycle through all the blocks being displayed.
  234.         for (InfoBlock block : this.get(this.server, this.state)) {
  235.            
  236.             // Don't take the current block into account.
  237.             if (block.getTitle() == this.getTitle()) continue;
  238.            
  239.             // Make sure we have the same X coordinates.
  240.             if (block.baseX == this.baseX) {
  241.                 if (block.w > newW && this.matchW) {
  242.                     newW = block.w;
  243.                 }
  244.             }
  245.         }
  246.  
  247.         this.w = newW;
  248.     }
  249.    
  250.     /**
  251.      * Calculates the height of only this block.
  252.      * @return Height
  253.      */
  254.     public int calcH() {
  255.         int calcH;
  256.         int stringHeight = this.display.size() * fR.FONT_HEIGHT;
  257.         int spacing = (this.display.size()-1) * 2;
  258.         calcH = 2 + stringHeight + spacing + 10;
  259.        
  260.         return calcH;
  261.     }
  262.    
  263.     /**
  264.      * Sets the height of the block.
  265.      */
  266.     public void setH() {
  267.         int newH = this.calcH();
  268.        
  269.         // Cycle through all the blocks being displayed.
  270.         for (InfoBlock block : this.get(this.server, this.state)) {
  271.            
  272.             // Don't take the current block into account.
  273.             if (block.getTitle() == this.getTitle()) continue;
  274.            
  275.             // Make sure we have the same X coordinates.
  276.             if (block.baseY == this.baseY) {
  277.                 if (block.h > newH && this.matchH) {
  278.                     newH = block.h;
  279.                 }
  280.             }
  281.         }
  282.         this.h = newH;
  283.     }
  284.    
  285.     /**
  286.      * Responsible for setting the X coordinate of the block.
  287.      */
  288.     public void setX() {
  289.        
  290.         if (Data.contains(this.title + ".x")) {
  291.             this.baseX = Integer.parseInt((String) Data.get(this.title + ".x"));
  292.             return;
  293.         }
  294.        
  295.         ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight);
  296.  
  297.         // Check if there is a request for coordinate moving
  298.         if (getTitle().matches(rePos) || !this.coords.equals("")) {
  299.            
  300.             // The title of the block we are moving off of
  301.             String titleX = coords.replaceAll(rePos, "$1").replaceAll("\"", "");
  302.            
  303.             // + or -
  304.             String symbolX = coords.replaceAll(rePos, "$2");
  305.             // Determine the mode
  306.             String modeX = coords.replaceAll(rePos, "$4");
  307.             // The number of units we want to move
  308.             double moveX = 0;
  309.             // The calculated offset based on the mode.
  310.             double shiftX = 0;
  311.            
  312.             if (coords.replaceAll(rePos, "$3").matches("\\d+")) {
  313.                 moveX = Double.parseDouble(coords.replaceAll(rePos, "$3"));
  314.             }
  315.  
  316.             /*
  317.             if (modeX.equals("%")) {
  318.                 shiftX = ((double)res.getScaledWidth() * (double)moveX/100);
  319.             } else {
  320.                 System.out.println("shiftX != %");
  321.                 //shiftX = Math.sqrt(res.getScaledWidth()*res.getScaledHeight()/100);
  322.                 shiftX = ((double)res.getScaledWidth() * (double)1/100);
  323.             }
  324.             */
  325.             shiftX = ((double)res.getScaledWidth() * (double)moveX/100);
  326.            
  327.             // Check if we are moving off of a block
  328.             if (!titleX.equals("")) {
  329.  
  330.                 // Check our symbol
  331.                 if (symbolX.equals("+") || symbolX.equals("")) {
  332.                     // Move to the right.
  333.                     InfoBlock oldBlock = this.get(titleX);
  334.                     if (oldBlock != null) {
  335.                         this.baseX = (int) (oldBlock.baseX + oldBlock.w + shiftX + padding*2);
  336.                     }
  337.                
  338.                 } else if (symbolX.equals("-")) {
  339.                     // Move to the left.
  340.                     InfoBlock oldBlock = this.get(titleX);
  341.                     if (oldBlock != null) {
  342.                         this.baseX =  oldBlock.baseX - (int) shiftX - this.w;
  343.                     }
  344.                 }
  345.                
  346.             } else {
  347.                 // If we're not moving off of a block, we're moving off the sides of the screen
  348.                 if (symbolX.equals("+") || symbolX.equals("")) {
  349.                     // Move to the right.
  350.                     this.baseX = (int) shiftX + padding;
  351.                 } else if (symbolX.equals("-")) {
  352.                     // Move to the left.
  353.                     this.baseX = res.getScaledWidth() - (int) shiftX - this.w - padding;
  354.                 }
  355.             }
  356.         }
  357.     }
  358.    
  359.     /**
  360.      * Responsible for setting the Y coordinate of the block.
  361.      */
  362.     public void setY() {
  363.        
  364.         if (Data.contains(this.title + ".y")) {
  365.             System.out.println("Loaded position from storage!");
  366.             this.baseX = Integer.parseInt((String) Data.get(this.title + ".y"));
  367.             return;
  368.         }
  369.        
  370.         ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight);
  371.  
  372.         // Check if there is a request for coordinate moving
  373.         if (getTitle().matches(rePos) || !this.coords.equals("")) {
  374.            
  375.             // The title of the block we are moving off of
  376.             String titleY = coords.replaceAll(rePos, "$5").replaceAll("\"", "");
  377.             // + or -
  378.             String symbolY = coords.replaceAll(rePos, "$6");
  379.             // Determine the mode
  380.             String modeY = coords.replaceAll(rePos, "$8");
  381.             // The number of units we want to move
  382.             double moveY = 0;
  383.             // The calculated offset based on the mode.
  384.             double shiftY = 0;
  385.            
  386.             if (coords.replaceAll(rePos, "$7").matches("\\d+")) {
  387.                 moveY = Double.parseDouble(coords.replaceAll(rePos, "$7"));
  388.             }
  389.  
  390.             /*
  391.             if (modeY.equals("%")) {
  392.                 shiftY = ((double)res.getScaledHeight() * (double)moveY/100);
  393.             } else {
  394.                 //shiftY = Math.sqrt(res.getScaledWidth()*res.getScaledHeight()/100);
  395.                 shiftY = ((double)res.getScaledWidth() * (double)moveY/100);
  396.             }
  397.             */
  398.             shiftY = ((double)res.getScaledHeight() * (double)moveY/100);
  399.            
  400.             if (!titleY.equals("")) {
  401.  
  402.                 if (symbolY.equals("+") || symbolY.equals("")) {
  403.                     // Move up.
  404.                     InfoBlock oldBlock = this.get(titleY);
  405.  
  406.                     if (oldBlock != null) {
  407.                         this.baseY = (int) (oldBlock.baseY + oldBlock.h + shiftY + padding*2);
  408.                     }
  409.  
  410.                 } else if (symbolY.equals("-")) {
  411.                     // Move down.
  412.                     InfoBlock oldBlock = this.get(titleY);
  413.                    
  414.                     if (oldBlock != null) {
  415.                         this.baseY = oldBlock.baseY - this.h - padding*2 - 1;
  416.                     }
  417.                 }
  418.                
  419.             } else {
  420.                 if (symbolY.equals("+") || symbolY.equals("")) {
  421.                     // Starts at 0 and increases
  422.                     this.baseY = (int) (shiftY) + padding;
  423.                    
  424.                 } else if (symbolY.equals("-")) {
  425.                     // Move down - make the value bigger.
  426.                     //this.baseY = res.getScaledHeight() - ((int) ((double)res.getScaledHeight() * (double)moveY/100) + this.h);
  427.                     this.baseY = res.getScaledHeight() - this.h - padding;
  428.                 }
  429.             }
  430.         }
  431.     }
  432.    
  433.     public int align(String line) {
  434.         if (!ConfigHUD.alignItems) return 0;
  435.         if (!line.contains(">>")) return 0;
  436.        
  437.         int toMiddle = 0;
  438.         // Get the maximum width of any string.
  439.         for (String string : this.display) {
  440.             if (!string.contains(">>")) continue;
  441.             if (fR.getStringWidth(string.replaceAll(">>.*", "")) > toMiddle) {
  442.                 toMiddle = fR.getStringWidth(string.replaceAll(">>.*", ""));
  443.             }
  444.         }
  445.  
  446.         String adjusted = line.replaceAll(">>.*", "");
  447.         int needed = toMiddle - fR.getStringWidth(adjusted);
  448.         return needed;
  449.     }
  450.    
  451.     /**
  452.      * Draw the block!
  453.      */
  454.     public void draw() {
  455.         int titleHeight = fR.FONT_HEIGHT-1;
  456.         bg = ConfigHUD.renderBG;
  457.        
  458.         if (bg) {
  459.             if (getTitle() != "") {
  460.                
  461.                 // Render the title area.
  462.                 Draw.rect(baseX-padding,
  463.                                baseY-1-padding,
  464.                                this.w+padding*2,
  465.                                titleHeight+padding*2,
  466.                                0, 0, 0, (float) 0.42/2);
  467.                
  468.                 // Render the bottom half.
  469.                 Draw.rect((float) baseX-padding,
  470.                            (float) baseY+titleHeight+2,
  471.                            (float) this.w+padding*2,
  472.                            (float) this.h + padding - titleHeight - 3,
  473.                            0, 0, 0, (float) 0.32/2);
  474.                
  475.             } else {
  476.                 // Render the bottom half, without a title.
  477.                 Draw.rect((float) baseX-padding,
  478.                            (float) baseY+2,
  479.                            (float) w+padding*2,
  480.                            (float) this.h + padding,
  481.                            0, 0, 0, (float) 0.32/2);
  482.  
  483.             }
  484.         }
  485.         int offset = padding*2;
  486.        
  487.         // Render the title.
  488.         if (getTitle() != "") {
  489.             // Check our config for centering titles.
  490.             if (ConfigHUD.centerTitles) {
  491.                 fR.drawStringWithShadow(getTitle(), baseX + centerPos(w, getTitle()), baseY-1, 0xFFFFFF);
  492.             } else {
  493.                 fR.drawStringWithShadow(getTitle(), baseX, baseY-1, 0xFFFFFF);
  494.             }
  495.             // Set our offset. All titles result in an offset of 12.
  496.             offset = 12;
  497.         }
  498.  
  499.         // Render all other strings under the title.
  500.         for (String string : display) {
  501.             fR.drawStringWithShadow(string, baseX + align(string), baseY+offset, 0xFFFFFF);
  502.             offset = offset + fR.FONT_HEIGHT+2;
  503.         }
  504.     }
  505.    
  506.     /**
  507.      * Processes {variables} in text, e.g. {kills} -> 2
  508.      * @param line The line to process.
  509.      * @return The processed line.
  510.      */
  511.     public static String processVars(String line) {
  512.         String reVar = "\\{(.+?)\\}";
  513.  
  514.         // Form our matcher for variables.
  515.         Matcher varMatch = Pattern.compile(reVar).matcher(line);
  516.         while (varMatch.find()) {
  517.             String var = varMatch.group().replaceAll("\\{", "").replaceAll("\\}", "");
  518.  
  519.             // Check AllVars first.
  520.             if (AllVars.get(var) != "") {
  521.                 line = line.replaceAll("\\{" + var + "\\}", AllVars.get(var));
  522.                
  523.             } else if (Server.getVar(var) != null && !(Server.getVar(var).equals("")) && !Server.getVar(var).equals("-1")) {       
  524.                 // Replace the occurance of the var with the actual info.
  525.                 line = line.replaceAll("\\{" + var + "\\}", Server.getVar(var));
  526.                
  527.             } else {
  528.                 // Return "null" to prevent the string from being rendered.
  529.                 line = "null";
  530.             }
  531.         }
  532.         return line;
  533.     }
  534.  
  535.     /**
  536.      * Calculate the center position of text.
  537.      * @param area
  538.      * @param string
  539.      * @return
  540.      */
  541.     public static int centerPos(int area, String string) {
  542.         return ((area/2) - (fR.getStringWidth(string)/2));
  543.     }
  544.    
  545.     /**
  546.      * Sets whether or not to match width.
  547.      * @param match
  548.      * @return
  549.      */
  550.     public InfoBlock setMatch(boolean match) {
  551.         this.matchW = match;
  552.         return this;
  553.     }
  554.  
  555.     public String getTitle() {
  556.         return title;
  557.     }
  558.  
  559.     public void setTitle(String title) {
  560.         this.title = title;
  561.     }
  562.    
  563.     @Override
  564.     public void click() {
  565.         if (Selectable.selected == this) {
  566.             Selectable.selected = null;
  567.         } else {
  568.             Selectable.selected = this;
  569.         }
  570.     }
  571.    
  572.     @Override
  573.     public void drawOutline() {
  574.        
  575.         // Draw the top bar.
  576.         Draw.rect(baseX-padding*2,
  577.                    baseY-1-padding*2,
  578.                    this.w + padding*4,
  579.                    padding,
  580.                    1, 0, 0, 1);
  581.        
  582.         // Draw the bottom bar.
  583.         Draw.rect(baseX-padding*2,
  584.                    baseY + this.h + padding-1,
  585.                    this.w + padding*4,
  586.                    padding,
  587.                    1, 0, 0, 0.5F);
  588.        
  589.         // Draw the left bar.
  590.         Draw.rect(baseX-padding*2,
  591.                    baseY-1-padding*2,
  592.                    padding,
  593.                    this.h+padding*4,
  594.                    1, 0, 0, 1);
  595.        
  596.         // Draw the right bar.
  597.         Draw.rect(this.baseX + this.w + padding,
  598.                    baseY-1-padding*2,
  599.                    padding,
  600.                    this.h+padding*4,
  601.                    1, 0, 0, 1);
  602.        
  603.     }
  604.    
  605.     @Override
  606.     public void move(char direction, int moveBy) {
  607.         // Move left
  608.         if (direction == 'l') baseX -= moveBy;
  609.         // Move right
  610.         if (direction == 'r') baseX += moveBy;
  611.         // Move up
  612.         if (direction == 'u') baseY -= moveBy;
  613.         // Move down
  614.         if (direction == 'd') baseY += moveBy;
  615.     }
  616.    
  617. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement