[Sothatsit's Useful Snippets] - Block Letters, MobBarAPI, AutoRespawn + More!

Discussion in 'Resources' started by SoThatsIt, Feb 1, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    SoThatsIt

    Just take a look at the source code for it, the methods are pretty self-explanatory
     
  2. Offline

    somebody7136

    What method spells things out though
     
  3. Offline

    SoThatsIt

    look at the methods and maybe if you dont completely understand, whats inside them, this is not a tutorial and i do not feel like giving it to people who dont know enough java to read java.
     
  4. Offline

    chasechocolate

    For writing letters on blocks, how about using Bukkit's map API (well, not exactly, but revise the code to work for blocks)? That way you don't have to use all those clunky booleans ^^
     
  5. Offline

    SoThatsIt

    Codename_B actually proposed i do the exact same thing after i had finished creating it, if i were to make it again i would definitely take this approach as it was very arduous to type all the letters out by hand and then adjust them to look good and even then some look pretty horrible. One of my main concerns in using the map lettering system is that the letters are made to occupy more pixels than just 5x5 so would look a bit un-proffesional when shrunk down although i think this could be worked around and using the letter system from the map classes would also allow you to make letters in which you could define the height of the text you wanted to print.
     
  6. Offline

    chasechocolate

    SoThatsIt I'll try it out this weekend, but if it doesn't look good, how about using java's graphics API? You could create a blank image and then draw some text on it. Then, loop though all the pixels, check if it isn't white, and then place blocks accordingly.
     
  7. Offline

    SoThatsIt

    Sounds good, would probably be good to create a reference map for each letter because the creation of the image, drawing on it and looping through the pixels could cause some lag potentially. Good luck on it, cant wait to see the results.
     
  8. Offline

    Panda_Crafter

  9. Offline

    SoThatsIt

    did you saveblocks?
     
  10. Offline

    Panda_Crafter

    Yes but it doesn't work, i also tried saveblock
     
  11. Offline

    SoThatsIt

    i dont really know then, i use this is many of my plugins and it works quite well, are you getting any errors in the console maybe?
     
  12. Offline

    Panda_Crafter

    That's the problem, there is no error, maybe you can give me a class where you use it in?
     
  13. Offline

    ItsLeoFTW

    How do I use the ColourWave class with BarAPI? I'm attempting to make the bar message smoothly loop through the colors, but with no idea how to use the ColourWave class, I can't get it. :/
     
  14. Offline

    SoThatsIt


    here is a simple example of how to use the colourwave class:
    Code:
    private static Random r = new Random();
    private static ColourWave wave = new ColourWave("text here");
        private static long ticks = 0;
     
        public static String getWaveStr()
        {
            return wave.toString();
        }
     
        public void onEnable()
        {
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this , new Runnable()
            {
                public void run()
                {
                    if ( ticks % wave.getBaseString().length() == 0 )
                    {
                        // put in this array all colours which color wave can be
                        ChatColor[] colours =
                        { ChatColor.RED , ChatColor.BLUE , ChatColor.AQUA , ChatColor.DARK_AQUA , ChatColor.GREEN , ChatColor.YELLOW };
                     
                        ChatColor colour = colours[r.nextInt(colours.length)];
                     
                        wave.next();
                    }
                 
                    ticks++;
                }
            } , 4 , 4);
    }
    and then when you want to get the string from the colourwave do

    Code:
    getWaveStr()
    simplez
     
  15. Offline

    ItsLeoFTW

    Oh thanks trying right now

    There's not any getWaveStr() method that I can see. Should I use next()?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  16. Offline

    SoThatsIt

    it is in the code that i gave you.
    Code:
    public static String getWaveStr()
        {
            return wave.toString();
        }
     
  17. Offline

    97WaterPolo

    SoThatsIt
    Is there a way to get part of the current message for your MobBarAPI? I am trying to get the previous percent and message and that is turning into quite a hassle. Any ideas how I would get it? From what I understand every time a new message is made, it destroys the previous one and just creates a new dragon.
     
  18. Offline

    ItsLeoFTW

    Update on the colorwave: The message is showing up, but it is not colored. It's just white.

    CODE:
    Code:java
    1.  
    2. @EventHandler
    3. public void onJoin(PlayerJoinEvent e){
    4. try {
    5. MobBarAPI.getInstance().setStatus(e.getPlayer(), getWaveStr(), 100, false);
    6. } catch (Exception ex) {
    7. }
    8.  
    9. }
    10.  

    What am I doing wrong...
     
  19. Offline

    Panda_Crafter

    Selection.java don't work for me, can you help me please? Here is my code:

    Code:java
    1. package net.PandaCrafter1.RB.Main;
    2.  
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Location;
    6. import org.bukkit.block.Block;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class RBmain extends JavaPlugin implements Listener{
    13.  
    14. @Override
    15. public void onEnable() {
    16. getServer().getPluginManager().registerEvents(this, this);
    17. }
    18.  
    19. @Override
    20. public boolean onCommand(CommandSender sender, Command command,
    21. String label, String[] args) {
    22. if(command.getName().equalsIgnoreCase("restore")){
    23. Location min = new Location(Bukkit.getWorld("world"), 176, 100, 222);
    24. Location max = new Location(Bukkit.getWorld("world"), 180, 110, 230);
    25. new Selection(min, max).restoreBlocks();
    26.  
    27. }
    28. if(command.getName().equalsIgnoreCase("save")){
    29. Location min = new Location(Bukkit.getWorld("world"), 176, 100, 222);
    30. Location max = new Location(Bukkit.getWorld("world"), 180, 110, 230);
    31. // Block b = Bukkit.getWorld("world").getBlockAt(min);
    32.  
    33. new Selection(min, max).saveBlocks();
    34.  
    35. }
    36. return true;
    37. }
    38.  
    39. }
     
  20. Offline

    SoThatsIt

    It does not create a new dragon, but the dragons are not actual entities, they are sent to the players via packets and when you update their name it just sends a metadata packet. To accomplish this just create 2 variables per player in a list or in a custom class like
    Code:
    String last_message = "";
    int last_percentage = 0;
    then when you change the mob bar just get these 2 variables from however you store them and then use them and then after you set the mob bar set them to the new values you set.

    with some modification you could save this data in the dragon class in the mob bar api.]

    can i see the rest of your code please. Are you adding colours to the colour wave? and are you ticking the colour wave?

    You need to save the selection object somewhere. It saves the information of the blocks in the class so if you make a new one every time the saved data wont be in that object, but in an other one. just make a variable in your class called
    Code:
    private Selection sel;
    and then in your onEnable create the object
    Code:
    Location min = new Location(Bukkit.getWorld("world"), 176, 100, 222);
    Location max = new Location(Bukkit.getWorld("world"), 180, 110, 230);
    sel = new Selection(min, max);
    and then instead of using new Selection() every time use the variable sel
     
  21. Offline

    Wizehh

    PlayerData.java (too tired to add a synopsis):
    PHP:
    // Don't forget to put your package name here!
    import java.io.File;
    import java.io.IOException;
     
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.Plugin;
     
    /**
    * A class to dynamically create configuration files for each player.
    * <br>
    * Use this as opposed to storing all of your players' data in one file.
    * <br>
    * Feel free to use this class in any manner which you see fit!
    * @author Wizehh
    */
     
    public class PlayerData {
     
        private 
    String name;
        private 
    FileConfiguration config;
        private 
    File file;
        private 
    File dir;
        private 
    Plugin plugin;
        private 
    boolean debug;
     
        
    /**
        * Constructor to access plugin's methods
        * @param plugin the main class
        */
     
        
    public PlayerData(Plugin plugin) {
            
    this.plugin plugin;
        }
     
        
    /**
        * Creates the directories.
        * This must be in your onEnable() method!
        */
     
        
    public void setup() {
            
    this.dir = new File(plugin.getDataFolder() + File.separator "players");
            if (!
    dir.exists()) {
                
    dir.mkdir();
                
    plugin.getLogger().fine("The player data directories have been setup.");
            }
        }
     
        
    /**
        * Creates a new player data object.
        * If the file doesn't exist, it will be created
        * @param name the name of the player
        */
     
        
    public PlayerData(String name) {
            
    this.name name;
            
    this.file = new File(this.dir File.separator this.name ".yml");
            if (
    file.exists()) {
                
    file = new File(this.dir File.separator this.name ".yml");
                try {
                    
    file.createNewFile();
                    if (
    this.debug)
                        
    plugin.getLogger().fine("The data file for " this.name " has been created.");
                } catch (
    IOException e) {
                    
    plugin.getLogger().severe("The data file for " this.name " could not be created! Reason: " e.getMessage());
                    if (
    this.debug)
                        
    e.printStackTrace();
                }
            }
            
    this.config YamlConfiguration.loadConfiguration(file);
        }
     
        
    /**
        * Returns the configuration object
        * @return the config
        */
     
        
    public FileConfiguration getData() {
            return 
    this.config;
        }
     
        
    /**
        * Returns the hard file
        * @return the file
        */
     
        
    public File getFile() {
            return 
    this.file;
        }
     
        
    /**
        * Attempts to save the configuration file.
        * <h1>You must do this after any edits to the file!
        */
     
        
    public void save() {
            try {
                
    config.save(this.file);
            } catch (
    IOException e) {
                
    plugin.getLogger().severe("The data file for " this.name " could not be saved! Reason: " e.getMessage());
                if (
    this.debug)
                    
    e.printStackTrace();
            }
        }
     
    }
     
  22. Offline

    ItsLeoFTW

    SoThatsIt

    My onEnable:

    Code:java
    1. @Override
    2. public void onEnable()
    3. {
    4.  
    5. getServer().getPluginManager().registerEvents(this, this);
    6. getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
    7. Bukkit.getScheduler().scheduleSyncRepeatingTask(this , new Runnable()
    8. {
    9. public void run()
    10. {
    11. if ( ticks % wave.getBaseStr().length() == 0 )
    12. {
    13. // put in this array all colours which color wave can be
    14. ChatColor[] colours =
    15. { ChatColor.RED , ChatColor.BLUE , ChatColor.AQUA , ChatColor.DARK_AQUA , ChatColor.GREEN , ChatColor.YELLOW };
    16.  
    17. ChatColor colour = colours[r.nextInt(colours.length)];
    18.  
    19. wave.next();
    20. }
    21.  
    22. ticks++;
    23. }
    24. } , 4 , 4);
    25. }

    (ignore the "BungeeCord", that's there for something else")

    The colourwave class (I added the getBaseStr() method since it was not there)
    Code:java
    1.  
    2.  
    3.  
    4. import java.util.ArrayList;
    5. import java.util.List;
    6.  
    7. import org.bukkit.ChatColor;
    8.  
    9. public class ColourWave
    10. {
    11.  
    12. private String str;
    13. private List< Colour > colours;
    14.  
    15. public ColourWave( String str )
    16. {
    17. this.str = str;
    18. this.colours = new ArrayList< Colour >();
    19. }
    20.  
    21. public ChatColor getLastColour()
    22. {
    23. if ( colours.size() <= 0 )
    24. return null;
    25. else
    26. return colours.get(0).getColour();
    27. }
    28.  
    29. private String insert(String str, String insert, int index)
    30. {
    31. if ( index < 0 )
    32. index = 0;
    33.  
    34. String a = str.substring(0 , index);
    35. String b = str.substring(index);
    36.  
    37. return a + insert + b;
    38. }
    39. public String getBaseStr(){
    40. return this.toString();
    41.  
    42. }
    43. private void cleanColours()
    44. {
    45. int i = 0;
    46. while (i < colours.size())
    47. {
    48. if ( colours.get(i).getIndex() > str.length() )
    49. colours.remove(i);
    50. else
    51. i++;
    52. }
    53. }
    54.  
    55. public String toString()
    56. {
    57. String str = "" + this.str;
    58.  
    59. int before = 0;
    60. for ( Colour c : colours )
    61. {
    62. if ( c.getIndex() < 0 )
    63. before++;
    64. }
    65.  
    66. int add = 0;
    67. for ( Colour colour : colours )
    68. {
    69. if ( before > 0 )
    70. before--;
    71. if ( before <= 0 )
    72. {
    73. str = insert(str , colour.getColour().toString() , colour.getIndex() + add);
    74. add += 2;
    75. }
    76. }
    77.  
    78. return str;
    79. }
    80.  
    81. public String next()
    82. {
    83. String str = "" + this.str;
    84.  
    85. int before = 0;
    86. for ( Colour c : colours )
    87. {
    88. if ( c.getIndex() < 0 )
    89. before++;
    90. }
    91.  
    92. int add = 0;
    93. for ( Colour colour : colours )
    94. {
    95. if ( before > 0 )
    96. before--;
    97. if ( before <= 0 )
    98. {
    99. str = insert(str , colour.getColour().toString() , colour.getIndex() + add);
    100. colour.next();
    101. add += 2;
    102. }
    103. }
    104.  
    105. cleanColours();
    106.  
    107. return str;
    108. }
    109.  
    110. public void addColour(ChatColor colour)
    111. {
    112. Colour c = new Colour(colour, -str.length());
    113. colours.add(0 , c);
    114. }
    115.  
    116. private class Colour
    117. {
    118. private ChatColor colour;
    119. private int index;
    120.  
    121. public Colour( ChatColor colour , int index )
    122. {
    123. this.colour = colour;
    124. this.index = index;
    125. }
    126.  
    127. public ChatColor getColour()
    128. {
    129. return colour;
    130. }
    131.  
    132. public int getIndex()
    133. {
    134. return index;
    135. }
    136.  
    137. public void next()
    138. {
    139. index++;
    140. }
    141. }
    142.  
    143. }
    144.  


    And the code to send the bar:

    Code:java
    1.  
    2. @EventHandler
    3. public void onJoin(PlayerJoinEvent e){
    4. try {
    5. MobBarAPI.getInstance().setStatus(e.getPlayer(), getWaveStr(), 100, true);
    6. } catch (Exception ex) {
    7. }
    8.  
    9. }
    10.  
     
  23. Offline

    Panda_Crafter

    If i use EnchantGlow my server will crash xD
     
  24. Offline

    SoThatsIt

    probably an outdated version :p
     
  25. Offline

    jusjus112

    SoThatsIt
    The inventory factory doesnt work!
     
  26. Offline

    Panda_Crafter

    Of your EnchantGlow class?
     
  27. Offline

    SoThatsIt

    The version you are using is probably updated for the version of bukkit you are using as i have not updated it.
     
  28. Offline

    Mr360zack

    SoThatsIt how could I add bold to the entire colourwave? When I try adding +ChatColor.BOLD to the string, when I update it sometimes a wierd [0-9/a-f]l shows up in front of the string.
     
  29. Offline

    ChipDev

    So, Thats it?
    ;)
    Just kidding. So much stuff. I now want more....
    *Your name goes here*?
     
  30. Offline

    Saith1998

    Thank you for doing this you helped me a lot :D!
     
Thread Status:
Not open for further replies.

Share This Page