Lots of errors!

Discussion in 'Plugin Development' started by CrazyTaco, Aug 30, 2014.

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

    CrazyTaco

    Hello bukkit community,
    This my first plugin and first bukkit code attempt, this is just the layout still incomplete.I am trying to make a plugin that despawns all item on the ground like inventory drops and player deaths drops.If any one can tell me whats did I do wrong and tell me how to finish this plugin that would be very much be appreciated.

    Code:
    package com.kevin.bukkit;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
     
    public class NoDrop extends JavaPlugin{
       
     
        @Override
        public void onEnable(){
            getLogger().info("OnEnable as started!");
        }
       
       
        @Override
        public void onDisable(){
            getLogger().info("onDisable has started!");   
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args, boolean PlayerDropItemEvent){
            if (cmd.getName().equalsIgnoreCase("nodrop Enabled") && sender instanceof Player){
                Player player = (Player)sender;
                player.sendMessage(ChatColor.GREEN + "NoDrop has been Enabled!");
            } else {
                sender.sendMessage(ChatColor.RED +"Command Can Only Be Used In-Game!");
            }
        } else {
            if (cmd.getName().equalsIgnoreCase("nodrop Enabled")){ 
                nodropdisabled(sender);
            }
        }
        return false;   
        }         
                   
        public void nodropdisabled(CommandSender sender){
        sender.sendMessage("NoDrop has been Disabled!");
        }
    }
     
  2. CrazyTaco
    You said you have lots of errors, what are those errors? And I know this is your first attempt at coding a plugin, but what makes you think that sending a message would remove ground drops?
     
  3. Offline

    Necrodoom

    Also, a command cannot have spaces in it. "Enabled" would be an argument.
     
  4. Offline

    CrazyTaco

    Assist In eclipse it says that Syntax error on token "else", { expected and sender cannot be resolved to a variable.

    I was hopping someone can help me find the code that makes all drops despawn.

    Necrodoom how can make I change it so I can do /nodrop enabled in-game?
     
  5. Offline

    Necrodoom

    CrazyTaco Well, why did you try to add an else to a method? It doesnt work that way.
    Also, read on bukkit wiki on how to write a plugin. If you dont understand it, head to a java tutorial.
     
  6. CrazyTaco
    When I started, I also had some trouble with the brackets. Using an IDE helps a lot with them, however it seems like you don't know anything at all about coding (using this syntax, at least). As said, you should start reading/watching some Java tutorials, then head over to the Bukkit wiki and read the big plugin tutorial to get started.
     
  7. Offline

    Halginzz

    You should create a boolean variable at first.
    Code:java
    1. private boolean dropEnabled;


    And completly rewrite the onCommand method. Example:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    2. //The command is "/nodrop"
    3. if (label.equalsIgnoreCase("nodrop")) {
    4.  
    5. //If the command is without any args (it means the command is just "/nodrop")
    6. if (args.length == 0) {
    7.  
    8. //If sender is player
    9. if (sender instanceof Player) {
    10. Player player = (Player) sender;
    11.  
    12. //Do something here. For example, you can send the list of commands.
    13. }
    14.  
    15. //If there's one arg (it means the command is "/nodrop <arg>")
    16. } else if (args.length == 1) {
    17.  
    18. //If arg is "enable" (/nodrop enable)
    19. if (args[0].equalsIgnoreCase("enable")) {
    20.  
    21. //If sender is player
    22. if (sender instanceof Player) {
    23. Player player = (Player) sender;
    24.  
    25. //This will set dropEnabled to true (players can't drop anything)
    26. dropEnabled = true;
    27. player.sendMessage(ChatColor.GREEN + "Enabled!");
    28. }
    29.  
    30.  
    31. //If arg is "disable" (/nodrop disable)
    32. } else if (args[0].equalsIgnoreCase("disable")) {
    33.  
    34. //If sender is player
    35. if (sender instanceof Player) {
    36. Player player = (Player) sender;
    37.  
    38. //This will set dropEnabled to false (players can drop anything);
    39. dropEnabled = false;
    40. player.sendMessage(ChatColor.RED + "Disabled");
    41. }
    42. }
    43. }
    44. }
    45. return false;
    46. }


    If you want to prevent players from dropping items while dropEnabled is true, you need something like this:
    Code:java
    1. @EventHandler
    2. public void onDrop(PlayerDropItemEvent e) {
    3. Player p = e.getPlayer();
    4.  
    5. //If dropEnabled is set to true...
    6. if (dropEnabled == true) {
    7.  
    8. //...it will cancel the event and players won't be able to drop anything.
    9. e.setCancelled(true);
    10. p.sendMessage("You can't do that now!");
    11. }
    12. }
     
    CrazyTaco likes this.
  8. Offline

    CrazyTaco

    i was following a java tut and they added }else{ can help fix my code or give me a link to a good tut on bukkit java on youtube.

    video I was watching
     
  9. Offline

    Halginzz

    Btw, don't copy and paste the code. Read it and try to understand. Then make your own based code on what people give you. :)

    \\Edit:
    Oh, you want to remove items on the ground. My mistake. Here's an example:
    Code:java
    1. public void clearItems(World world) {
    2. //Check if the world isn't null to prevent exceptions.
    3. if (world != null) {
    4.  
    5. //Get list of all entities.
    6. List<Entity> entitiesList = world.getEntities();
    7.  
    8. //List through all entities...
    9. for (Entity current : entitiesList) {
    10.  
    11. //...and remove them, if they're item.
    12. if (current instanceof Item) {
    13. current.remove();
    14. }
    15. }
    16. }
    17. }


    Then call the method via:
    Code:java
    1. clearItems(world);
     
  10. Offline

    CrazyTaco

    yeah thx for all the instructions there so easy to understand :) BTW where did you learn java/bukkit can you link the video if you can?
     
  11. Offline

    Necrodoom

  12. Offline

    CrazyTaco

    Necrodoom I learned basic java for a while but then thought to go to bukkit .

    @Halginzz
    Sorry to ask but can you repost the code with some empty places (So I can get more practice) I kinda got cunfused with all the codes that are up.

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

    Necrodoom

    If you try to else a method, i think you may want to practice more.
     
Thread Status:
Not open for further replies.

Share This Page