Solved Help killing players and creating explosion

Discussion in 'Plugin Development' started by dragonfreak1000, Sep 9, 2013.

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

    dragonfreak1000

    So, I'm trying to make a weird little plugin just for fun to test out some stuff, and I came across something. So, I'm trying to kill someone with an explosion in this code:
    Code:java
    1. package com.gmail.[Sensored].test;
    2. import org.bukkit.command.Command;
    3. import org.bukkit.command.CommandSender;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6. public final class Apples extends JavaPlugin {
    7. @Override
    8. public void onEnable(){
    9. getLogger().info("OpMeFX started successfully!");
    10. }
    11.  
    12. @Override
    13. public void onDisable(){
    14. getLogger().info("OpMeFX closed!");
    15.  
    16. }
    17. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    18. if(cmd.getName().equalsIgnoreCase("opme")){
    19. if (!(sender instanceof Player)){
    20. sender.sendMessage("This command can only be run by a player.");
    21. } else {
    22.  
    23. Player player = (Player) sender;
    24. player.sendMessage("§4You really though that would work?");
    25. float explosionPower = 4F;
    26. Player target = sender.getWorld().getPlayer(args[0]);
    27. target.getWorld().createExplosion(target.getLocation(), explosionPower);
    28. target.setHealth(0);
    29. return true;
    30. }
    31. }
    32. return false;
    33. }
    34. }
    35.  

    but, in eclipse, getworld() is underlined in red, and it says The method getWorld() is undefined for the type CommandSender. I am kinda a newb to programming bukkit plugins, so any help would be appreciated! If you do help, then [diamond][diamond][diamond] to you!
     
  2. Offline

    Tarestudio

    dragonfreak1000
    CommandSender can be the Console, and the Console has no world ;) I think you want to use player instead of sender there.
     
  3. Offline

    dragonfreak1000

    Thanks, but there's still one more problem. So, now that I have
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. if(cmd.getName().equalsIgnoreCase("opme")){
    3. if (!(sender instanceof Player)){
    4. sender.sendMessage("This command can only be run by a player.");
    5. } else {
    6.  
    7. Player player = (Player) sender;
    8. player.sendMessage("§4You really though that would work?");
    9. float explosionPower = 4F;
    10. Player target = player.getWorld().getPlayer(args[0]);
    11. target.getWorld().createExplosion(target.getLocation(), explosionPower);
    12. target.setHealth(0);
    13. return true;
    14. }
    15. }
    16. return false;
    17. }

    Now getPlayer is underlined and it says The method getPlayer(String) is undefined for the type World.
    I also tried
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. if(cmd.getName().equalsIgnoreCase("opme")){
    3. if (!(sender instanceof Player)){
    4. sender.sendMessage("This command can only be run by a player.");
    5. } else {
    6.  
    7. Player player = (Player) player;
    8. player.sendMessage("§4You really though that would work?");
    9. float explosionPower = 4F;
    10. Player target = player.getWorld().getPlayer(args[0]);
    11. target.getWorld().createExplosion(target.getLocation(), explosionPower);
    12. target.setHealth(0);
    13. return true;
    14. }
    15. }
    16. return false;
    17. }
     
  4. Offline

    Tarestudio

    dragonfreak1000
    Yes the World dont gives you a specific Player, only a List of the Players that are on this World. The Server can give you the specific Player: Bukkit.getPlayer(args[0]);
    If you want that the target is on the same World, you have to check this on your own.
     
  5. Offline

    beastman3226

    Try creating multiple variables, one getting the location, another getting the player and another getting the world.
     
  6. Offline

    dragonfreak1000

    Ok, sorry I'm such a newb but now when I replace getPlayer() with Bukkit.getPlayer(), it underlines that in red. (And I imported the correct classes at the top.) but anyways, all I really want to do, is get where the player is and make an explosion and kill them.
     
  7. Offline

    Tarestudio

    dragonfreak1000
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. if(cmd.getName().equalsIgnoreCase("opme")) {
    3. if (!(sender instanceof Player)) {
    4. sender.sendMessage("This command can only be run by a player.");
    5. } else {
    6. if (args.length == 1) {
    7. Player player = (Player) sender;
    8. player.sendMessage("§4You really though that would work?");
    9. Player target = Bukkit.getPlayer(args[0]);
    10. if (target != null) {
    11. target.getWorld().createExplosion(target.getLocation(), 4F);
    12. target.setHealth(0);
    13. return true;
    14. }
    15. }
    16. }
    17. }
    18. return false;
    19. }

    What I dont get is, that the command is opme, you send a message a to the commandsender, but you kill a player provided by the first argument. Why?
     
  8. Offline

    beastman3226

    Yes, you should do
    Code:java
    1. Player target = (Player) sender;
     
  9. Offline

    Chinwe

    I'd suggest looking in the docs before posting about an underlined error in Eclipse, eg, getPlayer(String), as it saves a lot of time and effort if you can fix most problems yourself :)
     
  10. Offline

    dragonfreak1000

    Ok I combined these 2 ideas and got it working with:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if(cmd.getName().equalsIgnoreCase("opme")) {
        if (!(sender instanceof Player)) {
        sender.sendMessage("This command can only be run by a player.");
        } else {
        if (args.length < 1) {
        Player target = (Player) sender;
        target.sendMessage("§4You really though that would work?");
        if (target != null) {
        target.getWorld().createExplosion(target.getLocation(), 4F);
        target.setHealth(0);
        return true;
        }
        }
        }
        }
        return false;
        }
    so [diamond][diamond][diamond] to you both!

    Plugin now live with my other account (HaitherecreeperMC) on bukkitdev. Yes it says I'm 13, as I entered my age wrong on this one :p so it's right. It's currently awaiting approval.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
Thread Status:
Not open for further replies.

Share This Page