Errors with /burn

Discussion in 'Plugin Development' started by TheMinecraftKnight, Jan 20, 2017.

Thread Status:
Not open for further replies.
  1. I'm trying to make a /burn command for practice in Java, as I'm pretty new.
    When I type /burn I get an internal error and it says my username in chat, same with /burn [name] (except a made up name, I get the "NAME CAN NOT BE FOUND" message that I made.)
    When I type /burn [myname] -s it works fine but the message is a bit messed up, as you can see in the screenshots:

    SCREENSHOTS (ignore the excessive health):
    Show Spoiler

    /burn and /burn [myname]:
    2017-01-20_21.35.53.png
    /burn [myname] -s (works but messed up)
    2017-01-20_21.41.54.png


    And now my code:
    Show Spoiler

    Plugin.yml - Ignore all the other commands except /burn, I removed them from the class file to make it easier to debug and read.
    Code:
    name: KnightzMCBasics
    version: 1.1
    main: com.knightzmc.basics.KnightzMCRandomTP
    description: A collection of basic commands made for KnightzMC
    commands:
      randomtp:
        description: Teleports you to a random location in the current world you are in.
        usage: /randomtp
      heal:
        description: Heals you up to full health!
        permission: knightzmc.heal
        usage: /heal
      superheal:
        description: Heals you up to full health - and makes your max health 40!
        permission: knightzmc.superheal
        usage: /superheal on/off
      hint:
        description: This command totally won't help you find collectables
        usage: /hint
      burn:
        description: Burn all the scrubs
        usage: '/burn [player]'
    My only class file (others don't work for some reason):

    Code:
    package com.knightzmc.basics;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class KnightzMCRandomTP extends JavaPlugin {
    
        public String bold = ChatColor.BOLD + "";
        public String red = ChatColor.RED + "";
        public String gold = ChatColor.GOLD + "";
        public String aqua = ChatColor.AQUA + "";
        public String darkaqua = ChatColor.DARK_AQUA + "";
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player) sender;
            player.sendMessage(player.getName());
            if (cmd.getName().equalsIgnoreCase("burn")) {
                Player playeroffline = Bukkit.getPlayerExact(args[0]);
                if (!(sender instanceof Player)) {
                    sender.sendMessage("Only players can set other players on fire.");
                    sender.sendMessage("This is an arbitrary requirement for demonstration purposes only.");
                }
                else {
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    if (args.length == 0) {
                        sender.sendMessage(gold + bold + "Usage: " + aqua + "/burn " + darkaqua + "<player> " + gold + "[-s] (silent)" );
                    }
                    else {
                        if (playeroffline == null) {sender.sendMessage(red + bold + args[0] + " could not be found!");
                        }
                        else {
                            if (args[1] == "-s") {
                                target.setFireTicks(20);   
                            }
                            else {
                                target.setFireTicks(20);
                                target.sendMessage(ChatColor.GOLD + "You have been set on fire by " + ChatColor.RED + sender);
                                sender.sendMessage(red + target + "" + ChatColor.GOLD + " has been set on fire");
                            }
                        }
                    }
                }
    
            }
            return true;
        }
    }
    

    Any help would be greatly appreciated, thanks <3
     

    Attached Files:

  2. Offline

    Alex_Cboy

    @TheMinecraftKnight
    Could you please use sender.getName() or getDisplayName() and hit me up once you've tested it?
     
  3. For instance make a random command with
    Code:
    public String getName =  sender.getName()
    sender.sendMessage(getName)
    
    ?
     
  4. Offline

    Alex_Cboy

    Yeah but instead of all this there's a copy of your code edited:
    Code:
     
    else {
    target.setFireTicks(20);
    target.sendMessage(ChatColor.GOLD + "You have been set on fire by " + ChatColor.RED + sender.getName());
    sender.sendMessage(red + target.getName() + "" + ChatColor.GOLD + " has been set on fire");
    }
    
     
  5. Okay, /burn [player] -s works now but it's a bit weird
    Instead of being silent (-s) it does the things that the /burn [player] is meant to do.
    Also, this is really odd - when I do /burn player -s it tells me the usage from the plugin.yml.
    /burn and /burn [player] still don't work :(
    Screenshots
    Show Spoiler
    2017-01-21_08.55.58.png
     
  6. Offline

    Alex_Cboy

    @TheMinecraftKnight Sorry for being late, let me check your code, and i'll hit you up once i'ce checked the whole thing

    EDIT: Check args.length == 1 for player and args.length == 2 for -s
    args.length == 0 means /burn but args[0] means /burn arg[0]
    Hope you understood!
    and when you want it to not return the usage, use: return true;
    @TheMinecraftKnight Also, hit me up with a dm!
     
    Last edited: Jan 21, 2017
  7. Offline

    DoggyCode™

    public?
     
  8. It doesn't really matter, does it? I'll change it to private
     
  9. Offline

    DoggyCode™

    PHP:
    // command arr[0] arr[1] arr[2] (...)
    if(arr.length<1) {
      
    //player's name was not specified
    } else {
      
    // get index 0 in the arguments, which would be the player's name.
      
    String name arr[0];
      
    // Use the Bukkit-API to get a Player instance from the name
      
    Player target Bukkit.getPlayer(name);
      
    // check if the target is on the server or exists
      
    if(target==null) {
        
    // target is not online or does not exist
      
    } else {
        
    target.setFireTicks(20); // 20 ticks ~~ 1 second (actually it's 19.xyziabmnwqr)
        
    target.sendMessage(ChatColor.GOLD "You have been set on fire by " ChatColor.RED sender.getName());
        
    sender.sendMessage(ChatColor.REDtarget.getName() + "" ChatColor.GOLD " has been set on fire");
      }
    }
    It doesn't get much harder.

    EDIT:
    Yeh bro, it fucking (sorry, we're adults here) matters.

    Even my IDE knows this...
    [​IMG]
    https://gyazo.com/f8d8e044518045fa327b2b5c9fcc0f0e

    You can't have public modifiers inside methods. onCommand is a method. And the public modifier is mostly for getter and setter methods.

    EDIT2:
    Make it final, if you really need something in front of your variable.

    EDIT3:
    No, you can't make it private neither. Study object orientated programming languages, because obviously you're teaching wrong here. of how it works.
     
    Last edited: Jan 21, 2017
  10. Ok, I get your point. I'll use private. Before you criticize though, read the OP.
     
  11. Offline

    DoggyCode™

    Don't use private neither, and I didn't realize you were OP. Your comment makes it look like you are trying to give "working" code.
     
  12. Fair enough. What should I use then, static?
     
  13. Offline

    DoggyCode™

    No. You don't have to use anything if it's inside a method (not a class global variable). You can make it final, which means that you can't modify it's value after the initial value is set. But you don't have to use anything. For example:

    final String s = "this is a final value!";
    s = "but i'm trying to change it!"; // this would not work as it's final, setting a new value won't be allowed.
    System.out.println(s): // would print "this is a final value!" if your compiler does't refuse to compile before you remove the changing its value part


    However, this will work:
    String s = "this is not a final value.";
    s = "Wohoo! I'm changing it.";
    System.out.println(s); // would print "Wohoo! I'm changing it." because we're allowed to change it.


    However, when it's a class based value (I guess you can call it that), instead of a method based one (idk.), you can use modifiers such as private, public (usually only for getter methods to retrieve private variables and not variables). And static too (be careful with this though).
     
Thread Status:
Not open for further replies.

Share This Page