Lots of problems :P

Discussion in 'Plugin Development' started by TheSmallBones, Aug 11, 2012.

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

    TheSmallBones

    Here is my code:
    Code:
    package me.Kyle.HCC2;
     
    import java.io.File;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class HCC2 extends JavaPlugin implements Listener {
        File deathsfile;
        FileConfiguration deaths;
        @Override
        public void onDisable() {
        }
        @Override
        public void onEnable() {     
            this.getServer().getPluginManager().registerEvents(this, this);
            deathsfile = new File(getDataFolder(), "deaths.yml");
            deaths = YamlConfiguration.loadConfiguration(deathsfile);
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("hc") && (args.length > 0)) { // Basis for hc plugin.
                if (args[0].equals("addlife") && args[1] != null){
                    this.reloadConfig();
                    String name = args[1];
                    if(this.getConfig().getString(name) != null){
                        this.getConfig().set(name, this.getConfig().getInt(name) + 1);
                        this.saveConfig();
                        Bukkit.broadcastMessage(ChatColor.AQUA+name+" has recieved 1 life from an admin!");           
                    }
                }
                else if (args[0].equals("slay") && args.length > 1) {
                    this.reloadConfig();
                    String name = args[1];             
                    Player slayed = this.getServer().getPlayer(name);
                    this.getConfig().set(name, this.getConfig().getInt(name) - 1);
                    this.saveConfig();
                    if(slayed != null){             
                        Bukkit.broadcastMessage(ChatColor.RED + name + " has been slain by an admin!");               
                        slayed.kickPlayer("Slain by an admin! Submit a ban appeal, or buy a life token!");
                    }
                    else {
                        sender.sendMessage(ChatColor.RED + "[URGENT]Player is offline. I have taken away their current life, but their inventory still exists!");
                    }
                }   
                else{
                    sender.sendMessage("Correct Usage: /hc <rez|slay> <player>");
                }
            }
            return true;
        }
     
     
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event) {
            String name = event.getEntity().getName();
            this.getConfig().set(name, this.getConfig().getInt(name) - 1);
            this.saveConfig();
            event.getEntity().getInventory().clear();
            event.getEntity().kickPlayer("You have died! Come back tomorrow or buy a life token!");
            Bukkit.broadcastMessage(ChatColor.RED + name + " has been DEATHBANNED!1!");
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player p = event.getPlayer();
            this.reloadConfig();
            if(this.getConfig().get(p.getName()) != null){
                if (this.getConfig().getInt(p.getName()) == 0) {
                    event.getPlayer().kickPlayer("You are dead. Purchase a life token or wait till midnight! :P");
                }
            }
            else{
                this.getConfig().set(p.getName(), 1);
                this.saveConfig();   
            }
        }
    }
    Problem 1: When I do /hc addlife thesmallbones it just tells me in chat /hc addlife thesmallbones, and I am NOT reutrnning false;
    Problem 2: When a life is taken away, a new string is for some reason added into the yml, it looks like this:
    Code:
    TheSmallBones: 1
    thesmallbones: -1
    
    The problem with the yml above is their lifes should be zero, not -1 since I think I am taking away 1 life.
     
  2. Offline

    ThatBox

    1.
    PHP:
    return true;
    is what you are missing.

    2. Not sure i think it is case sensitive. Try using slayed.getName() instead of name.
     
    TheSmallBones likes this.
  3. Offline

    TheSmallBones

    I fixed a bunch of things such as the duplicates, by adding
    Code:
    String n = args[1];
                    String name = n.toLowerCase();;
    but now when I slay a player their lifes are -1 which should be 0, and well when I do /hc addlife thesmallbones it just tells me the command. I will see where to add return true;
     
  4. Offline

    ThatBox

    add the return true right before the else if statement
    PHP:
    return true;
    } else if(
    args.....
     
    TheSmallBones likes this.
  5. Offline

    TheSmallBones

    hehe just did that :p and also I have figured out that the reason why it 'wasn't writing sometimes' was because whenever I manipulate or check a config i should just reload it. It doesn't hurt anyone and it can help make the plugin more user friendly. i have done this and my plugin seems fine. i will post here again if i have any more issues as you are quick and straight forward to your answers. :)

    Another: when a player dies naturally, which uses this code:
    Code:
    @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event) {
            String n = event.getEntity().getName();
            String name = n.toLowerCase();;
            this.getConfig().set(name, this.getConfig().getInt(name) - 1);
            this.saveConfig();
            event.getEntity().getInventory().clear();
            event.getEntity().kickPlayer("You have died! Come back tomorrow or buy a life token!");
            Bukkit.broadcastMessage(ChatColor.RED + n + " has been DEATHBANNED!1!");
        }
    It uses the case sensitive version of the player name and adds it to the file, messing up my shit. Why?

    ThatBox I now have removed all known bugs. But it writes shit like this:
    Code:
    thesmallbones: 1notch: 1
    
    How do I make it go on a new line?

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

    The_Coder

    What code is formating this file
     
  7. Offline

    TheSmallBones

    I'm an idiot. I was using notepad and it looked weird... Opened ++ and it's fine lol.
     
Thread Status:
Not open for further replies.

Share This Page