Teleporting Players Help?

Discussion in 'Plugin Development' started by Rehmedy, Jul 31, 2012.

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

    Rehmedy

    Hey, I made a plugin for my friend to teleport his players to an event he set up, but one thing I do not know how to do. I want him to be able to type the command "/tc setspawn red" and "/tc setspawn blue" and I want those to set each side's spawn. But, I also want the "/tc join" command to teleport players to them. I was wondering how to make it so the first player would type "/tc join" and it would teleport them to red, then second person to blue, third to red, fourth to blue, and so on. Is there anyway to do this?

    Oh, and I also wanted to know how to set both the red and blue spawn in a config file so it saves it and isn't a temporary spawn.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  2. Offline

    pzxc

    Use a boolean variable to keep track of whether the last person who joined was blue or red, and do the opposite then toggle the boolean.

    To store the locations in the config you can just store them as a comma-delimited string like "world,5,5,5" then read that string from the config and use string.split(",") to split it and parse it
     
  3. Offline

    Rehmedy

    Well, here's my code, what do i need to add to it?
    Code:
    package com.github.rehmedy.TowerControl;
     
    import java.util.logging.Logger;
     
    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;
     
    public class TowerControl extends JavaPlugin {
        Logger log;
        public void onEnable(){
            log = this.getLogger();
            log.info("[TowerControl] TowerControl has been enabled!");
            }
        public void onDisable(){
            log.warning ("[TowerControl] TowerControl has been disabled");
        }   
        Location spawn = null;
        Location lobby = null;
     
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(command.getName().equalsIgnoreCase("tc")) {
                if(args.length >= 1) {
                    Player permPlayer = (Player) sender;
                    if(args[0].equalsIgnoreCase("setspawn")) {
                        if (permPlayer.hasPermission("tc.setspawn") || permPlayer.hasPermission("tc.*"))
                            if(args[0].equalsIgnoreCase("red")){
                                if(sender instanceof Player) {
                                    spawn = ((Player) sender).getLocation();
                                } else {
                                    sender.sendMessage("Bukkit sad. Our databases show that you are not a player, so we cannot allow you to perform this command.");
                                }
                            }
                            if (args[0].equalsIgnoreCase("blue")){
                                if(sender instanceof Player) {
                                    spawn = ((Player) sender).getLocation();
                                } else {
                                    sender.sendMessage("Bukkit sad. Our databases show that you are not a player, so we cannot allow you to perform this command.");
                                }
                            }
                            if (args[1].equalsIgnoreCase("lobby")){
                                if(sender instanceof Player) {
                                    lobby = ((Player) sender).getLocation();
                                } else {
                                    sender.sendMessage("Bukkit sad. Our databases show that you are not a player, so we cannot allow you to perform this command.");
                                }
                            }
                       
                    }
                    if(args[0].equalsIgnoreCase("join")) {
                        if (permPlayer.hasPermission("tc.join") || permPlayer.hasPermission("tc.*"))
                        if(sender instanceof Player) {
                            if(spawn != null) {
                                ((Player) sender).teleport(spawn);
                            } else {
                                sender.sendMessage("Can't teleport because spawn is not set!");
                            }
                        } else {
                            sender.sendMessage("Bukkit sad. Our databases show that you are not a player, so we cannot allow you to perform this command.");
                        }
                    }
                    if(args[0].equalsIgnoreCase("leave")){
                        if(sender instanceof Player){
                            if(lobby != null) {
                                ((Player)sender).teleport(lobby);
                            } else {
                                sender.sendMessage("Can't return to spawn because spawn is not set!");
                            }
                        } else {
                            sender.sendMessage("Bukkit sad. Our databases show that you are not a player, so we cannot allow you to perform this command.");
                        }
                   
                    }
                   
                }
            }
            else {
                sender.sendMessage("TowerControl Commands:");
                sender.sendMessage("tc join - Teleports you to the TowerControl event spawn");
                sender.sendMessage("tc leave - Leaves the current game and teleports you back to the event lobby");
                sender.sendMessage("tc setspawn - Command to set the spawn for the TowerControl Game");
            }
            return false;
        }
    }
    
    Bump? Code is^.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  4. Offline

    pzxc

    Just giving you the code is like doing your homework for you; you don't learn anything. So I'll help you without giving you the final product, since from your code you're obviously new at this.

    First of all, in programming (not just bukkit, not just java, ANY programming) if you have the same thing repeated several times, you should try to abstract it into something simpler. If necessary make a separate function that does what is being repeated, then call that function every time it needs to happen instead of repeating the code. But lots of times there are ways to simplify without even needing to make a separate function.

    Look at this section:
    Code:
    [FONT=Consolas]if(sender instanceof Player) {
                                    spawn = ((Player) sender).getLocation();
                                } else {
                                    sender.sendMessage("Bukkit sad. Our databases show that you are not a player, so we cannot allow you to perform this command.");
                                }[/FONT]
    
    You have this repeated SO MANY times with slight variations throughout all your code
    Of course you have this at the beginning so it fails anyway: Player permPlayer = (Player) sender;

    You are trying to cast sender into a Player variable BEFORE you do all the checks whether it even is a player!

    You can get rid of all those checks and just put ONE at the start of your code (BEFORE you try to cast it into a player variable)

    Like so:
    if (!(sender instanceof Player)) {
    sender.sendMessage("Bukkit sad. Our databases show...");
    return;
    }

    Your code would literally be like less than half as long if you simplified that one thing.
    Simpler code = easier to understand code = easier to modify/fix/extend code

    Once you simplify that, it's much more obvious "where" to put the new code, because there's much fewer places to put it.
    You need a global variable outside your function: boolean lastPlayerWasRedTeam = false;
    then inside your function you say:

    if (lastPlayerWasRedTeam) {
    makePlayerBlueTeam();
    lastPlayerWasRedTeam = false;
    } else {
    makePlayerRedTeam();
    lastPlayerWasRedTeam = true;
    }

    Make sense? Of course makePlayerRedTeam() and makePlayerBlueTeam() are just pseudocode, those should be replaced with the code that actually DOES make a player one team or the other. In a separate function if it's more than 10 lines, or right there inline if it's very short
     
Thread Status:
Not open for further replies.

Share This Page