Making abbreviations for configs?!

Discussion in 'Plugin Development' started by PerezHD, Nov 23, 2014.

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

    PerezHD

    Well I am not sure what the name for it is called yet, but I called abbreviations.

    Basically I wanted to figure out how to make for example
    p.getName() usable in configs? So like I can make
    p.getName() = %player_name, which I can be able to type in config..
     
  2. Offline

    Creeoer

    Ugh...get a string in the config from the path and check if it contains the character sequence of %player_name%, if it does then display the player name in whatever you're doing
     
  3. Offline

    Rocoty

    Try String#replace.
     
  4. Offline

    PerezHD

    Would this be the correct way to do this?
    Code:java
    1. public static String translateToColorCode(String msg){
    2. return ChatColor.translateAlternateColorCodes('&', msg);
    3. }
    4.  
    5. public static String replaceVariables(String msg, Player player)
    6. {
    7. msg = msg.replace("%player_name", player.getName());
    8. return translateToColorCode(msg);
    9. }
     
  5. Offline

    leon3001

    Yes. Why is it static though? :confused:
     
  6. Offline

    PerezHD

    Assist leon3001 Eh, tried it in-game, it just ignored the whole process of %player_name and the color codes in the config for some odd reason.

    Its also static cause it thats what it needed to fix an error.

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

    leon3001

    Okay, you're probably right when he's writing an utility class.
     
  8. Offline

    PerezHD

    Any help for my messages above?
     
  9. How are you using the methods you showed above?
     
  10. Offline

    PerezHD

    Well, here...
    CODE:
    Code:java
    1. package me.perezhd.freeze;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.player.PlayerMoveEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class Main extends JavaPlugin implements Listener {
    16.  
    17. ArrayList<Player> frozen = new ArrayList<Player>();
    18. public static Main plugin;
    19. public String prefix;
    20.  
    21. public void onEnable() {
    22. plugin = this;
    23.  
    24. Bukkit.getServer().getLogger().info("Plugin Loaded!");
    25. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    26.  
    27. getConfig().options().copyDefaults(true);
    28. reloadConfig();
    29. saveDefaultConfig();
    30.  
    31. loadPrefix();
    32. }
    33.  
    34. public void onDisable() {
    35. Bukkit.getServer().getLogger().info("Plugin Disabled");
    36. }
    37.  
    38. public void loadPrefix(){
    39. this.prefix = translateToColorCode(getConfig().getString("prefix"));
    40. }
    41. public static String translateToColorCode(String msg){
    42. return ChatColor.translateAlternateColorCodes('&', msg);
    43. }
    44.  
    45. public static String replaceVariables(String msg, Player player)
    46. {
    47. msg = msg.replace("%player_name", player.getName());
    48. return translateToColorCode(msg);
    49. }
    50. @EventHandler
    51. public void onFreezeAll(PlayerMoveEvent e) {
    52. for (Player online : Bukkit.getOnlinePlayers()) {
    53. if(frozen.contains(online)) {
    54. e.setTo(e.getFrom());
    55. }
    56. }
    57. }
    58.  
    59. @EventHandler
    60. public void onPlayerMove(PlayerMoveEvent e) {
    61. Player p = e.getPlayer();
    62. if(frozen.contains(p)) {
    63. e.setTo(e.getFrom());
    64. p.sendMessage(prefix + getConfig().getString("Frozen-Toggled-Message"));
    65. }
    66.  
    67. }
    68. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args) {
    69.  
    70. Player p = (Player) sender;
    71.  
    72. if(cmd.getName().equalsIgnoreCase("freeze")) {
    73. if(p.hasPermission("pf.freeze")) {
    74. if(args.length == 0) {
    75. p.sendMessage(prefix + getConfig().getString("Specify-Player"));
    76. return true;
    77. }
    78.  
    79. Player t = Bukkit.getServer().getPlayer(args[0]);
    80.  
    81. if(t == null) {
    82. p.sendMessage(prefix + ChatColor.RED + " Could not find the player " + ChatColor.AQUA + args[0] + ChatColor.RED + "!");
    83. return true;
    84. }
    85.  
    86. if(frozen.contains(t)) {
    87. frozen.remove(t);
    88. p.sendMessage(prefix + ChatColor.YELLOW + " " + ChatColor.YELLOW + args[0] + ChatColor.GRAY + " is no longer frozen!");
    89. t.sendMessage(prefix + getConfig().getString("Unfreeze-Message"));
    90. return true;
    91. } else {
    92. frozen.add(t);
    93. p.sendMessage(prefix + ChatColor.YELLOW + " " + ChatColor.YELLOW + args[0] + ChatColor.GRAY + " is now frozen!");
    94. t.sendMessage(prefix + getConfig().getString("Freeze-Message") + " You have been frozen by " + ChatColor.YELLOW + p.getName() + ChatColor.GRAY + "!");
    95. return true;
    96. }
    97. } else {
    98. p.sendMessage(getConfig().getString("No-Permission-Message"));
    99. return true;
    100. }
    101. }
    102. if(args[1].equalsIgnoreCase("reload"))
    103. if(p.hasPermission("pf.freeze.reload")) {
    104. reloadConfig();
    105. p.sendMessage(prefix + "You have successfully reloaded the configuration file!");
    106. }
    107. return true;
    108.  
    109. }
    110.  
    111.  
    112. }
    113.  


    CONFIG:
    http://pastebin.com/dw5zAMgQ
     
  11. PerezHD
    I don't see you ever even using the replaceVariables method.
     
  12. Offline

    PerezHD

    .... it says it in the code..
     
  13. Offline

    teej107

    That's nice! Call it now.
     
  14. Offline

    mythbusterma

    PerezHD

    Also, don't leave a public static reference to your plugin in there, that's just lazy.
     
    ferrybig and teej107 like this.
Thread Status:
Not open for further replies.

Share This Page