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 package me.Ricecutter0.BlockInfo; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player; //import java.util.logging.Level; public class Chat { /* This class is just to make messaging players easier.. If you have any questions about this Class.. * feel free to to ask them in the comments below :D */ public static void sendPlayer(Player p, String s) { p.sendMessage(color(s)); } public static void sendConsole(String s) { Bukkit.getServer().getConsoleSender().sendMessage(color(s)); } public static String color(String str) { return ChatColor.translateAlternateColorCodes('&', str); } public static void send(CommandSender sender, String s) { sender.sendMessage(color(s)); } public static void send(Player sender, String s) { sender.sendMessage(color(s)); } // public static void log(Level level, String s) { // Bukkit.getLogger().log(level, s); // } //This method isn't used in this plugin.. but I thought if you wanted you could use it in another! public static String strp(String s) { return net.md_5.bungee.api.ChatColor.stripColor(s); }} With that we will now add the ConfigManager.java Code:java package me.Ricecutter0.BlockInfo;import java.io.File;import java.io.IOException; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.configuration.file.FileConfiguration;import org.bukkit.configuration.file.YamlConfiguration;import org.bukkit.plugin.Plugin; public class ConfigManager { private ConfigManager() {} static ConfigManager instance = new ConfigManager();/**You need this to be able to use this class in another * class */ public static ConfigManager getInstance(){ return instance;//continued.. } static FileConfiguration config; static File cfile; Plugin p; //declare stuff ^^ public static void setup(Plugin p){ cfile = new File(p.getDataFolder(), "config.yml"); config = p.getConfig(); config.options().copyDefaults(true); p.saveDefaultConfig(); //now this will go in our onEnable. } public FileConfiguration getConfig(){ return config; } public void saveConfig(){//always remember to save the Config after changing it! try { config.save(cfile); } catch (IOException e) { Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save config!"); } } public void reloadConfig(){//this is optional.. if you want to have a /reload command.. this is what you need! try{ config = YamlConfiguration.loadConfiguration(cfile); }catch (Exception ex){ Bukkit.getServer().getLogger().severe(ChatColor.RED + "Unable to reload config!"); } }} Then we will add the BlockGet.java <- Will will get the block's information Code:java package me.Ricecutter0.BlockInfo; import java.util.ArrayList;import java.util.List;import java.util.UUID; import org.bukkit.block.Block;import org.bukkit.command.Command;import org.bukkit.command.CommandExecutor;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.block.Action;import org.bukkit.event.player.PlayerInteractEvent; public class BlockGet implements CommandExecutor, Listener { public static List<UUID> players = new ArrayList<UUID>(); /* * That List is where we will hold the player's UUID.. opposed to the player name * because Minecraft now offer's a name change. */ @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if(!(sender instanceof Player)){// Make sure that the player is actually a player.. (not a console) Chat.sendConsole("The console is unable to handle this command."); return true; } Player p = (Player) sender; if(!p.hasPermission("blockinfo.*") || !p.hasPermission("blockinfo.get")){// Checking permissions.. Chat.send(sender, Main.prefix + "&4You don't have permission to execute this command!"); return true; } Chat.send(sender, (Main.prefix + "&7Right-Click the block you'd like to recieve information on.")); players.add(p.getUniqueId());// Add the player to the cooldown when they execute the command. return true; } @SuppressWarnings("deprecation") @EventHandler public void onPlayerInteract(PlayerInteractEvent e){ Player p = e.getPlayer();// declare stuff.. if(!players.contains(p.getUniqueId())){ return;// If the player isn't in the List (cooldown).. don't continue. } Block b = e.getClickedBlock(); Action a = e.getAction(); if(a.equals(Action.RIGHT_CLICK_BLOCK)){ /* The next five lines of code.. (54 - 58) Are to make the first letter of the word uppercase. * and the rest is lower case.. very useful! If you have any other methods of doing this go * ahead and comment them below! */ String bb = b.getType().toString(); String string = bb.substring(0, 1).toUpperCase() + bb.substring(1).toLowerCase(); String bb2 = b.getWorld().getName(); String string2 = bb2.substring(0, 1).toUpperCase() + bb2.substring(1).toLowerCase(); Chat.send(p, "&8===== &6BlockInfo&8 ====="); Chat.send(p, "&aBlockID -> &c" + b.getTypeId() + "&a:&c" + b.getData()); /* * Line 69.. is how you'd get an item's type and data * Example: 5:2 */ Chat.send(p, "&aBlock Name -> &6" + string); Chat.send(p, ""); Chat.send(p, "&aBlock World -> &6" + string2); Chat.send(p, "&6X -> &c" + b.getX()); Chat.send(p, "&6Y -> &c" + b.getY()); Chat.send(p, "&6Z -> &c" + b.getZ()); Chat.send(p, "&8==================="); players.remove(p.getUniqueId());// Then remove the player from the cooldown. } }} And the final class.. Main.java Code:java package me.Ricecutter0.BlockInfo; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player; //import java.util.logging.Level; public class Chat { /* This class is just to make messaging players easier.. If you have any questions about this Class.. * feel free to to ask them in the comments below :D */ public static void sendPlayer(Player p, String s) { p.sendMessage(color(s)); } public static void sendConsole(String s) { Bukkit.getServer().getConsoleSender().sendMessage(color(s)); } public static String color(String str) { return ChatColor.translateAlternateColorCodes('&', str); } public static void send(CommandSender sender, String s) { sender.sendMessage(color(s)); } public static void send(Player sender, String s) { sender.sendMessage(color(s)); } // public static void log(Level level, String s) { // Bukkit.getLogger().log(level, s); // } //This method isn't used in this plugin.. but I thought if you wanted you could use it in another! public static String strp(String s) { return net.md_5.bungee.api.ChatColor.stripColor(s); }} 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 # \----------------------------------------------/ ## | BlockInfo Configuration File | ## | Plugin by Ricecutter0 | ## /----------------------------------------------\ # prefix: '&7[&cBlockInfo&7] ' Then the plugin.yml Code:java name: BlockInfoauthor: Ricecutter0version: 1.0main: me.Ricecutter0.BlockInfo.Maindescription: To retrieve block information! commands: blockinfo: description: Retrieves block ID and Location aliases: [blocki, binfo, bi] bireload: description: Reloads the Configuration File! -> Config.yml aliases: [breload] permissions: blockinfo.*: description: Gives access to all the BlockInfo commands. default: op children: blockinfo.get: true 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!
Code:java public static void send(CommandSender sender, String s) { sender.sendMessage(color(s)); } public static void send(Player sender, String s) { sender.sendMessage(color(s)); } The second method is useless. Player is an instance of CommandSender
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)
@Ricecutter0 @Cirno As Cirno said, why waste a few hundred bytes of the memory just to make your programming experience easier?
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.
@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"
Same functions in less than 40 lines: Code:Java public class BlockInfos extends JavaPlugin implements Listener {String prefix;List<UUID> players = new ArrayList<UUID>();public void onEnable() {saveDefaultConfig();prefix = getConfig().getString("prefix");Bukkit.getPlugiManager().registerEvents(this, this);}public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {if (!(sender instanceof Player)){sender.sendMessage(prefix + "§4The console is unable to handle this command.");return true;}Player p = (Player) sender;if(!p.hasPermission("blockinfo.*") || !p.hasPermission("blockinfo.get")){p.sendMessage(prefix + "§4You don't have permission to execute this command!");return true;}if (players.contains(p.getUniqueId()) {p.sendMessage(prefix + "§7You left the BlockInfo mode."));players.remove(p.getUniqueId());return true;}p.sendMessage(prefix + "§7Right-Click the block you'd like to recieve information on."));players.add(p.getUniqueId());return true;}@EventHandlerpublic void onInteract(PlayerInteractEvent e) {Player p = e.getPlayer();if (!players.contains(p.getUniqueId()) || e.getAction != Action.RIGHT_CLICK_BLOCK) return;e.setCancelled(true);Block b = e.getClickedBlock();p.sendMessage("ID: "+b.getTypeId()+(b.getData() == 0 ? "" : ":"+b.getData()));p.sendMessage("Name: "+b.getType().name().toLowerCase());p.sendMessage("Location: "+b.getWorld().getName()+":"+b.getX()+":"+b.getY()+":"+b.getZ());}}