Trying to create TogglePvP

Discussion in 'Plugin Development' started by ImpaTheImpaler, Jan 6, 2015.

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

    ImpaTheImpaler

    I just started trying to code Bukkit and I am trying to code a plugin that enables pvp or disables pvp for only the player that types the command. But I cannot get it to work.

    My code -
    Code:
        package me.impatheimpaler.test;
    
        import java.util.ArrayList;
        import java.util.List;
    
        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 togglepvp extends JavaPlugin {
    
     
        List<String> toggled = new ArrayList<String>();
     
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[]           args) {
            if (cmd.getName().equalsIgnoreCase("togglepvp")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("Only a Player can run this command.");
                    return false;
                    }
                Player player = (Player) sender;
                if (toggled.contains(player.getName())) {
                    player.sendMessage(ChatColor.RED + "PVP Enabled!");
                toggled.add(player.getName());
                return true;
                }
             
                player.sendMessage(ChatColor.GREEN + "PvP Disabled!");
                toggled.remove(player.getName());
                return true;
             
             
             
            }
            return false;
             }
     
     
     
         }
    
    

    plugin.yml -
    Code:
        name: togglepvp
        main: me.impatheimpaler.test.togglepvp
       version: 1.0
       commands:
        togglepvp:
            usage: /<command>
            description: Toggle PvP!
    
     
    Last edited: Jan 6, 2015
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    SuperOriginal

    You're checking if the player is in the list, then adding them?
     
    Last edited: Jan 6, 2015
  4. @SuperOriginal
    The other code will only run if the if (toggled.contains(player.getName())) { returns false because he added return true.
    But
    With his current code it will always run the code below because no player is in the list
     
  5. Offline

    ImpaTheImpaler

    Just edited that in, sorry. When I type /togglepvp it will say PvP Enabled ( or Disabled ). But even though it is disabled players can still hit others.
     
  6. Offline

    SuperOriginal

    teej107 likes this.
  7. Offline

    gal0511Dev

  8. Offline

    ImpaTheImpaler

    @gal0511Dev I am new to coding, how would I go about that :l
     
    Last edited: Jan 6, 2015
  9. Offline

    teej107

  10. Offline

    Zombie_Striker

    You don't have an onEnable() in your class. If you're new to bukkit, I suggest watching some tutorials first.
     
  11. Offline

    nverdier

    @Zombie_Striker Don't tell him that; He will find the BCBroz!


    @ImpaTheImpaler If you are new to coding, then first learn Java. Bukkit is an implementation of Java, and you never learn to run before you can walk. After you're sufficient at Java, you can start learning Bukkit. If you want to watch videos to learn Java, and then Bukkit, then I recommend @PogoStick29's videos, which can be found here.
     
    teej107 likes this.
  12. Offline

    ImpaTheImpaler

    @teej107 Sorry for my stupidity, but I cannot find what I am supposed to do. Is there any video tutorials that can help?
     
  13. Offline

    teej107

Thread Status:
Not open for further replies.

Share This Page