Tutorial BlockInformation

Discussion in 'Resources' started by Ricecutter0, Apr 12, 2015.

?

Was this helpful?

Poll closed Apr 22, 2015.
  1. Yes

    2 vote(s)
    18.2%
  2. No

    9 vote(s)
    81.8%
Thread Status:
Not open for further replies.
  1. Offline

    Ricecutter0

    Below is a little plugin I made that I thought that may be useful to some people. It's all about getting a block's information just by right-clicking. In order to get the block's information.. you have to type the command "/blockinfo", then it gets your player's UniqueId, and puts it into a list.. and if your name is in the list, when right-clicking a block then it will give you the block's information.

    First, we will make the Chat.java <- in which we will use to send messages to players and the console and it also supports color codes.

    (WITH COMMENTS TO GUIDE)

    Code:java
    1. package me.Ricecutter0.BlockInfo;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7.  
    8. //import java.util.logging.Level;
    9.  
    10. public class Chat {
    11. /* This class is just to make messaging players easier.. If you have any questions about this Class..
    12.   * feel free to to ask them in the comments below :D
    13.   */
    14. public static void sendPlayer(Player p, String s) {
    15. p.sendMessage(color(s));
    16.  
    17. }
    18.  
    19. public static void sendConsole(String s) {
    20. Bukkit.getServer().getConsoleSender().sendMessage(color(s));
    21.  
    22. }
    23.  
    24. public static String color(String str) {
    25. return ChatColor.translateAlternateColorCodes('&', str);
    26. }
    27.  
    28. public static void send(CommandSender sender, String s) {
    29. sender.sendMessage(color(s));
    30. }
    31.  
    32. public static void send(Player sender, String s) {
    33. sender.sendMessage(color(s));
    34. }
    35.  
    36. // public static void log(Level level, String s) {
    37. // Bukkit.getLogger().log(level, s);
    38. // }
    39. //This method isn't used in this plugin.. but I thought if you wanted you could use it in another!
    40.  
    41. public static String strp(String s) {
    42. return net.md_5.bungee.api.ChatColor.stripColor(s);
    43. }
    44. }


    With that we will now add the ConfigManager.java

    Code:java
    1. package me.Ricecutter0.BlockInfo;
    2. import java.io.File;
    3. import java.io.IOException;
    4.  
    5.  
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.configuration.file.FileConfiguration;
    10. import org.bukkit.configuration.file.YamlConfiguration;
    11. import org.bukkit.plugin.Plugin;
    12.  
    13. public class ConfigManager {
    14.  
    15. private ConfigManager() {}
    16.  
    17.  
    18. static ConfigManager instance = new ConfigManager();/**You need this to be able to use this class in another
    19.   * class
    20.   */
    21.  
    22. public static ConfigManager getInstance(){
    23. return instance;//continued..
    24. }
    25.  
    26. static FileConfiguration config;
    27. static File cfile;
    28.  
    29. Plugin p;
    30. //declare stuff ^^
    31.  
    32. public static void setup(Plugin p){
    33. cfile = new File(p.getDataFolder(), "config.yml");
    34. config = p.getConfig();
    35. config.options().copyDefaults(true);
    36. p.saveDefaultConfig();
    37. //now this will go in our onEnable.
    38. }
    39.  
    40. public FileConfiguration getConfig(){
    41. return config;
    42. }
    43.  
    44. public void saveConfig(){//always remember to save the Config after changing it!
    45. try {
    46. config.save(cfile);
    47. } catch (IOException e) {
    48. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save config!");
    49. }
    50. }
    51.  
    52. public void reloadConfig(){//this is optional.. if you want to have a /reload command.. this is what you need!
    53. try{
    54. config = YamlConfiguration.loadConfiguration(cfile);
    55. }catch (Exception ex){
    56. Bukkit.getServer().getLogger().severe(ChatColor.RED + "Unable to reload config!");
    57. }
    58. }
    59. }


    Then we will add the BlockGet.java <- Will will get the block's information

    Code:java
    1. package me.Ricecutter0.BlockInfo;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.UUID;
    6.  
    7. import org.bukkit.block.Block;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandExecutor;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.Action;
    15. import org.bukkit.event.player.PlayerInteractEvent;
    16.  
    17. public class BlockGet implements CommandExecutor, Listener {
    18. public static List<UUID> players = new ArrayList<UUID>();
    19. /*
    20.   * That List is where we will hold the player's UUID.. opposed to the player name
    21.   * because Minecraft now offer's a name change.
    22.   */
    23.  
    24. @Override
    25. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    26. if(!(sender instanceof Player)){// Make sure that the player is actually a player.. (not a console)
    27. Chat.sendConsole("The console is unable to handle this command.");
    28. return true;
    29. }
    30. Player p = (Player) sender;
    31. if(!p.hasPermission("blockinfo.*") || !p.hasPermission("blockinfo.get")){// Checking permissions..
    32. Chat.send(sender, Main.prefix + "&4You don't have permission to execute this command!");
    33. return true;
    34. }
    35.  
    36. Chat.send(sender, (Main.prefix + "&7Right-Click the block you'd like to recieve information on."));
    37. players.add(p.getUniqueId());// Add the player to the cooldown when they execute the command.
    38. return true;
    39. }
    40.  
    41. @SuppressWarnings("deprecation")
    42. @EventHandler
    43. public void onPlayerInteract(PlayerInteractEvent e){
    44. Player p = e.getPlayer();// declare stuff..
    45.  
    46.  
    47. if(!players.contains(p.getUniqueId())){
    48. return;// If the player isn't in the List (cooldown).. don't continue.
    49. }
    50.  
    51.  
    52. Block b = e.getClickedBlock();
    53.  
    54. Action a = e.getAction();
    55.  
    56. if(a.equals(Action.RIGHT_CLICK_BLOCK)){
    57. /* The next five lines of code.. (54 - 58) Are to make the first letter of the word uppercase.
    58.   * and the rest is lower case.. very useful! If you have any other methods of doing this go
    59.   * ahead and comment them below!
    60.   */
    61. String bb = b.getType().toString();
    62. String string = bb.substring(0, 1).toUpperCase() + bb.substring(1).toLowerCase();
    63.  
    64. String bb2 = b.getWorld().getName();
    65. String string2 = bb2.substring(0, 1).toUpperCase() + bb2.substring(1).toLowerCase();
    66.  
    67.  
    68. Chat.send(p, "&8===== &6BlockInfo&8 =====");
    69. Chat.send(p, "&aBlockID -> &c" + b.getTypeId() + "&a:&c" + b.getData());
    70. /*
    71.   * Line 69.. is how you'd get an item's type and data
    72.   * Example: 5:2
    73.   */
    74. Chat.send(p, "&aBlock Name -> &6" + string);
    75. Chat.send(p, "");
    76. Chat.send(p, "&aBlock World -> &6" + string2);
    77. Chat.send(p, "&6X -> &c" + b.getX());
    78. Chat.send(p, "&6Y -> &c" + b.getY());
    79. Chat.send(p, "&6Z -> &c" + b.getZ());
    80. Chat.send(p, "&8===================");
    81. players.remove(p.getUniqueId());// Then remove the player from the cooldown.
    82.  
    83.  
    84. }
    85. }
    86. }


    And the final class.. Main.java

    Code:java
    1. package me.Ricecutter0.BlockInfo;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7.  
    8. //import java.util.logging.Level;
    9.  
    10. public class Chat {
    11. /* This class is just to make messaging players easier.. If you have any questions about this Class..
    12.   * feel free to to ask them in the comments below :D
    13.   */
    14. public static void sendPlayer(Player p, String s) {
    15. p.sendMessage(color(s));
    16.  
    17. }
    18.  
    19. public static void sendConsole(String s) {
    20. Bukkit.getServer().getConsoleSender().sendMessage(color(s));
    21.  
    22. }
    23.  
    24. public static String color(String str) {
    25. return ChatColor.translateAlternateColorCodes('&', str);
    26. }
    27.  
    28. public static void send(CommandSender sender, String s) {
    29. sender.sendMessage(color(s));
    30. }
    31.  
    32. public static void send(Player sender, String s) {
    33. sender.sendMessage(color(s));
    34. }
    35.  
    36. // public static void log(Level level, String s) {
    37. // Bukkit.getLogger().log(level, s);
    38. // }
    39. //This method isn't used in this plugin.. but I thought if you wanted you could use it in another!
    40.  
    41. public static String strp(String s) {
    42. return net.md_5.bungee.api.ChatColor.stripColor(s);
    43. }
    44. }


    Now with all the classes.. We will now add the config.yml & the plugin.yml

    The configuration file will consist only of the prefix. (or if you added anything else)

    Code:java
    1. # \----------------------------------------------/ #
    2. # | BlockInfo Configuration File | #
    3. # | Plugin by Ricecutter0 | #
    4. # /----------------------------------------------\ #
    5.  
    6.  
    7. prefix: '&7[&cBlockInfo&7] '


    Then the plugin.yml

    Code:java
    1. name: BlockInfo
    2. author: Ricecutter0
    3. version: 1.0
    4. main: me.Ricecutter0.BlockInfo.Main
    5. description: To retrieve block information!
    6.  
    7. commands:
    8. blockinfo:
    9. description: Retrieves block ID and Location
    10. aliases: [blocki, binfo, bi]
    11.  
    12. bireload:
    13. description: Reloads the Configuration File! -> Config.yml
    14. aliases: [breload]
    15.  
    16. permissions:
    17. blockinfo.*:
    18. description: Gives access to all the BlockInfo commands.
    19. default: op
    20. children:
    21. blockinfo.get: true
    22. blockinfo.reload: true


    Thank you for reading this tutorial and if you have feedback, please comment!

    If you'd like to see the Gist on GitHub.. Here you go!
     
    Last edited: Apr 14, 2015
    GrandmaJam and ChipDev like this.
  2. Offline

    ChipDev

    You should comment your code so it isn't 100% spoon-feeding!
     
  3. Offline

    Ricecutter0

    I'm actually going to update the code so it gives more detail and is more a tutorial soon.
     
    ChipDev likes this.
  4. Offline

    sethrem

    Nice tutorial.
     
  5. Offline

    Ricecutter0

    Updated my code with comments to make it more of a tutorial!


    @sethrem
    Thank you!
     
  6. Code:java
    1. public static void send(CommandSender sender, String s) {
    2. sender.sendMessage(color(s));
    3. }
    4.  
    5. public static void send(Player sender, String s) {
    6. sender.sendMessage(color(s));
    7. }

    The second method is useless. Player is an instance of CommandSender
     
    Konato_K likes this.
  7. Offline

    Cirno

    You could scrap the entire Chat class because it's pointless; it just adds another class to load in the ClassLoader and therefore, bloats more memory usage :/

    ConfigManager's singleton pattern doesn't "work"; you need to set the instance to null in your onDisable() method or else you leak memory.

    Try to use the lowest form of encapsulation; for example, most of the variables you declared in ConfigManager can be private rather than default.

    When you want to send information about the world name, just send the world name (Last I recall, Bukkit's getWorld() method is case-sensitive). Reason for this is because the player might want to use the world name and providing them with the string you create won't help them. Also, for that particular part, try to give your variables a better name. "string" and "string2" don't give much information on what it is.

    Just some tips lol. (BTW, your code box thats supposed to show "Main" is showing Chat)
     
  8. Offline

    _Filip

    @Ricecutter0
    @Cirno
    As Cirno said, why waste a few hundred bytes of the memory just to make your programming experience easier?
     
  9. Offline

    nverdier

    First of all, don't use that config manager. @Cirno said why.
    Second of all, if you do use the config manager for some reason, give credit to the creator.
     
  10. Offline

    Ricecutter0

    @Cirno


    I see.. but if the ConfigManager isn't exactly good.. how could it be changed to make it more... useful? or so that it "works"
     
  11. Offline

    teej107

    @Ricecutter0 Stop misusing static and just use Bukkit's way of saving config.yml
     
  12. Offline

    Ricecutter0

  13. Offline

    nverdier

    #saveDefaultConfig()
    #getConfig()
    #saveConfig()
     
    teej107 likes this.
  14. Same functions in less than 40 lines:
    Code:Java
    1. public class BlockInfos extends JavaPlugin implements Listener {
    2. String prefix;
    3. List<UUID> players = new ArrayList<UUID>();
    4. public void onEnable() {
    5. saveDefaultConfig();
    6. prefix = getConfig().getString("prefix");
    7. Bukkit.getPlugiManager().registerEvents(this, this);
    8. }
    9. public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
    10. if (!(sender instanceof Player)){
    11. sender.sendMessage(prefix + "§4The console is unable to handle this command.");
    12. return true;
    13. }
    14. Player p = (Player) sender;
    15. if(!p.hasPermission("blockinfo.*") || !p.hasPermission("blockinfo.get")){
    16. p.sendMessage(prefix + "§4You don't have permission to execute this command!");
    17. return true;
    18. }
    19. if (players.contains(p.getUniqueId()) {
    20. p.sendMessage(prefix + "§7You left the BlockInfo mode."));
    21. players.remove(p.getUniqueId());
    22. return true;
    23. }
    24. p.sendMessage(prefix + "§7Right-Click the block you'd like to recieve information on."));
    25. players.add(p.getUniqueId());
    26. return true;
    27. }
    28. @EventHandler
    29. public void onInteract(PlayerInteractEvent e) {
    30. Player p = e.getPlayer();
    31. if (!players.contains(p.getUniqueId()) || e.getAction != Action.RIGHT_CLICK_BLOCK) return;
    32. e.setCancelled(true);
    33. Block b = e.getClickedBlock();
    34. p.sendMessage("ID: "+b.getTypeId()+(b.getData() == 0 ? "" : ":"+b.getData()));
    35. p.sendMessage("Name: "+b.getType().name().toLowerCase());
    36. p.sendMessage("Location: "+b.getWorld().getName()+":"+b.getX()+":"+b.getY()+":"+b.getZ());
    37. }
    38. }
     
    Last edited: Apr 18, 2015
Thread Status:
Not open for further replies.

Share This Page