Help with a warping command

Discussion in 'Plugin Development' started by Phyla, Dec 4, 2013.

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

    Phyla

    So i'm trying to make it so if you do /1v1 it will send you a message saying "Warping to 1v1! Wait 3 seconds!" So, i've tried making the 3 second warmup for the command and now when i try doing /1v1 I get an internal error :/

    package me.Phyla.warps;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;

    public class main extends JavaPlugin {
    public main plugin;

    public void main (main instance) {
    plugin = instance;
    }

    public void onEnable() {

    System.out.println("Warps enabled!");
    }
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    final Player p = (Player)sender;

    if ((commandLabel.equalsIgnoreCase("1v1")) && ((sender instanceof Player))) {
    new BukkitRunnable() {
    public void run() {
    p.sendMessage(ChatColor.GOLD + "Warping to 1v1! Wait 3 seconds!");
    p.teleport(new Location(Bukkit.getWorld("world"), 0, 69, 0));

    p.getInventory().clear();
    p.setHealth(20.0D);

    p.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET));

    }
    }.runTaskLater(plugin, 60L);

    }
    return false;
    }
    }
     
  2. Offline

    the_merciless

    Place your message before the runnable and you need to return true,

    Change

    Return false;
    {
    {

    To

    Return true;
    {
    Return false;
    {

    And use cmd.getName() instead of commandLabel
    Then if you still get errors show us the stack trace.
     
  3. Offline

    beastman3226

    Stacktrace, and please use syntax/code tags
     
  4. By just looking at your code without the stacktrace or logs, I think that these are the problems:

    1. If you are using the command "1v1" in console, a ClassCastException would be thrown when you cast an invalid class (ConsoleCommandSender) to Player.
    2. You have to ensure Bukkit.getWorld("world") does not return null. Some servers may change the name of the world.
     
  5. Offline

    Phyla

    Hello and thanks for the feedback. I did some messing around and got this and it works fine. However, I would like the make it so when you are waiting 3 seconds, moving will cancel the teleport but i'm not sure how to do that?

    Here's the current code:

    public class main extends JavaPlugin {

    public void onEnable() {

    System.out.println("Warps enabled!");
    }
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    final Player p = (Player)sender;

    if ((commandLabel.equalsIgnoreCase("spawn")) && ((sender instanceof Player))) {
    sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "Warping to Spawn! Wait 3 seconds!");
    new BukkitRunnable() {
    public void run() {
    p.teleport(new Location(Bukkit.getServer().getWorld("world"), -70, 44, 168));
    }
    }.runTaskLater(this, 60L);
    }

    Bump.

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

    Phyla

    Bump
     
  7. Offline

    Wolfey

    Code:
    ArrayList<String> warping = new ArrayList<String>();
    
    if(cmd.getName().equalsIgnoreCase("spawn")) {
    p.sendMessage(ChatColor.GREEN + "You are now warping to spawn. Please wait 3 seconds.');
    warping.add(p.getName));
    Bukkit.getScheduler().scheduleSyncDelayedTask(Bukkit.getPluginManager().getPlugin("pluginName"), new Runnable() {
    public void run() {
    if(warping.contains(sender.getName()) {
    warping.remove(p.getName());
    p.teleport(new Location(Bukkit.getServer().getWorld("world"), -70, 44, 168)); 
    }
    }
    }, 3 * 20);
    }
    
    Then make an event with a PlayerMoveEvent, if warping contains the player, remove them, and send them a message saying "you have moved, teleportation canceled"
     
Thread Status:
Not open for further replies.

Share This Page