Solved adding strings to a config.yml

Discussion in 'Plugin Development' started by Ethan Rocks 365, Aug 15, 2016.

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

    Ethan Rocks 365

    Ok ive been searching the web for a few hours now and i cant find what Im looking for...

    So want im tring to do it make a config file that would look like this

    Code:
    Players:
         Jimmy:
               Latest Ban: No bans
               Latest Kick: No kicks
               Latest Mute: No mutes
    
    and then when billy joins add the same thing but Billy instead of Jimmy so like

    Code:
    Players: 
       Jimmy:
          Latest Ban: No bans
          Latest Kick: No kicks
          Latest Mute: No mutes
       Billy:
          Latest Ban: No bans
          Latest Kick: No kicks
          Latest Mute: No mutes
    How would I do this?
     
  2. Offline

    MCMastery

    try config.set("Jimmy.Latest Ban", "No bans");
     
  3. Offline

    Ethan Rocks 365

    @MCMastery i did that but its still not updateing in the file here ill send the sorce code

    Code:
    @EventHandler
          public void Login(PlayerLoginEvent e)
          {
            Player p = e.getPlayer();
            if (p.isBanned())
            {
              BanEntry entry = Bukkit.getServer().getBanList(BanList.Type.NAME).getBanEntry(p.getName());
              if (entry.getReason().contains("ETHANBANENTRY"))
              {
                String bs = BuildBanString(entry);
                e.disallow(PlayerLoginEvent.Result.KICK_OTHER, bs);
              }
            }
            if(!(p.hasPlayedBefore())){
                
            }
          }
         
          public String BuildBanString(BanEntry entry)
          {
            String b = getConfig().getString("banstring");
            Date creationtime = entry.getCreated();
            SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm");
            String time = "[" + format.format(creationtime) + "] ";
            b = b.replace("{TIME}", time);
            b = b.replace("{BANNER}", entry.getSource());
            b = b.replace("{REASON}", entry.getReason());
            b = b.replace("&", "");
            return b;
          }
          public void onPlayerChat(AsyncPlayerChatEvent e){
              Player chatter = e.getPlayer();
              if(getConfig().getString(chatter.getName()+".muted").equalsIgnoreCase("yes")){
                  e.setCancelled(true);
                  chatter.sendMessage(ChatColor.RED+"You cant talk there is ducktape on your mouth...");
                  }
          }
    }
     
    Last edited: Aug 15, 2016
  4. Offline

    MCMastery

    i dont see where you did that
     
  5. Offline

    Ethan Rocks 365

    @MCMastery probably because somewhere in there alot of my class disapered :p
    Code:
    package dev.gosintary.punish;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.bukkit.BanEntry;
    import org.bukkit.BanList;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin{
        public void onEnable(){
            saveDefaultConfig();
        }
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(label.equalsIgnoreCase("ban")){
                if(sender.hasPermission("asteroid.ban")){
                  if(args[0].length()>1){
                      if(args[1].length()>1){
                          String reason = args[1];
                          Date now = new Date();
                          SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm");
                          String time = "[" + format.format(now) + "] ";
                          Bukkit.getServer().getBanList(BanList.Type.NAME).addBan(args[0], reason, null,                                                                 sender.getName()).setCreated(now);
                          getConfig().set(target.getName()+".latestban", "Latest Ban - "+sender.getName()+" - "+time+" - "+reason);
                      }
                  }
                }
            }
            if(label.equalsIgnoreCase("unban")){
                if(sender.hasPermission("asteroid.pardon")){
                    if(args[0].length()>1){
                     Bukkit.getServer().getBanList(BanList.Type.NAME).pardon(args[0]);
                    }
                }
            }
            if(label.equalsIgnoreCase("kick")){
                if(sender.hasPermission("asteroid.kick")){
                    if(args[0].length() >1){
                        if(args[1].length()>1){
                            Date now = new Date();
                            SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm");
                            String time = "[" + format.format(now) + "] ";
                            Player target = Bukkit.getServer().getPlayer(args[0]);
                            String reason = args[1];
                            target.kickPlayer(reason);
                            int totalKicks = getConfig().getInt(target.getName()+".totalkicks");
                            getConfig().set(target.getName()+".latestkick", "Latest Kick - "+sender.getName()+" - "+time+" - "+reason);
                            saveDefaultConfig();
                        }
                    }
                }
            }
            if(label.equalsIgnoreCase("history")){
                if(sender.hasPermission("asteroid.history")){
                    if(args[0].length()>1){
                        Player target = Bukkit.getServer().getPlayer(args[0]);
                        sender.sendMessage(ChatColor.DARK_GRAY+"-----"+ChatColor.GOLD+"History of "+target.getName()+ChatColor.GRAY+"-----");
                        sender.sendMessage(ChatColor.RED+getConfig().getString(target.getName()+".latestban"));
                        sender.sendMessage(ChatColor.RED+getConfig().getString(target.getName()+".latestkick"));
                        sender.sendMessage(ChatColor.RED+getConfig().getString(target.getName()+".latestmute"));
                    }else{
                        sender.sendMessage("You need to enter a players name!");
                    }
                }
            }
            if(label.equalsIgnoreCase("mute")){
                if(sender.hasPermission("asteroid.mute")){
                    if(args[0].length()>1){
                        if(args[1].length()>1){
                            Date now = new Date();
                            SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm");
                            String time = "[" + format.format(now) + "] ";
                            String reason = args[0];
                        Player target = Bukkit.getServer().getPlayer(args[0]);
                        if(getConfig().getString(target.getName()+".muted").equalsIgnoreCase("yes")){
                            getConfig().set(target.getName()+".muted", "no");
                            target.sendMessage(ChatColor.RED+"Your ducktape has been removed");
                        }else if(getConfig().getString(target.getName()+".muted").equalsIgnoreCase("no")){
                            getConfig().set(target.getName()+".latestmute","Latest Mute - "+sender.getName()+" - "+time+" - "+reason);
                            getConfig().set(target.getName()+".muted", "yes");
                            target.sendMessage(ChatColor.RED+sender.getName()+" has ducktaped your mouth!");
                        }
                        }
                    }
                }
            }
                                return false;
                              }
        @EventHandler
          public void Login(PlayerLoginEvent e)
          {
            Player p = e.getPlayer();
            if (p.isBanned())
            {
              BanEntry entry = Bukkit.getServer().getBanList(BanList.Type.NAME).getBanEntry(p.getName());
              String bs = BuildBanString(entry);
              p.kickPlayer(bs);
            }
            if(!(p.hasPlayedBefore())){
                
            }
          }
         
          public String BuildBanString(BanEntry entry)
          {
            String b = getConfig().getString("banstring");
            Date creationtime = entry.getCreated();
            SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm");
            String time = "[" + format.format(creationtime) + "] ";
            b = b.replace("{TIME}", time);
            b = b.replace("{BANNER}", entry.getSource());
            b = b.replace("{REASON}", entry.getReason());
            b = b.replace("&", "");
            return b;
          }
          public void onPlayerChat(AsyncPlayerChatEvent e){
              Player chatter = e.getPlayer();
              if(getConfig().getString(chatter.getName()+".muted").equalsIgnoreCase("yes")){
                  e.setCancelled(true);
                  chatter.sendMessage(ChatColor.RED+"You cant talk there is ducktape on your mouth...");
                  }
          }
    }
     
  6. Offline

    MCMastery

    you forgot to save the config :p atleast i saw in the ban command
     
  7. Online

    timtower Administrator Administrator Moderator

  8. Offline

    Ethan Rocks 365

    @bwfcwalshy the hell is up with the naming convention?
     
  9. Offline

    MCMastery

    that's normal Java naming convention...
    you are mixing it up with C#'s, which would be SaveConfig (I think that's what ur doing)
     
  10. @Ethan Rocks 365 Method names also chat event will never fire, no event handler
     
  11. Offline

    Irantwomiles

    I don't see you saving the config.
     
  12. Offline

    Ethan Rocks 365

    It's ffixed
     
Thread Status:
Not open for further replies.

Share This Page