Teleporting always facing South, and pitch/yaw aren't doing anything.

Discussion in 'Plugin Development' started by OhYes, May 12, 2016.

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

    OhYes

    Hey, whenever I right click my ender teleporter, it will teleport me to the location, but it will ALWAYS make me face SOUTH. No matter what I try. How would I let the player keep their pitch and yaw without making an arraylist?

    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onRightClick(PlayerInteractEvent e){
            final Player p = e.getPlayer();
            if(!(p.getItemInHand().getType() == Material.EYE_OF_ENDER)) return;
            e.setCancelled(true);
            if(!(e.getAction() == Action.RIGHT_CLICK_AIR || !(e.getAction() == Action.RIGHT_CLICK_BLOCK))) return;
         
            if(!(cooldown.contains(p))){
                cooldown.add(p);
             
             
             
             
             
            Block block = p.getTargetBlock((HashSet<Byte>)null, 100);
            Location vis = block.getLocation();
         
            float pitch = p.getEyeLocation().getPitch();
            float yaw = p.getEyeLocation().getYaw();
    
         
            p.teleport(vis.add(0, 1, 0));
         
            p.getLocation().setYaw(yaw);
            p.getLocation().setPitch(pitch);
    
            p.playSound(p.getLocation(), Sound.ENTITY_ENDERMEN_TELEPORT, 5, 5);
            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    
                @Override
                public void run() {
                    cooldown.remove(p);
                    p.sendMessage(ChatColor.YELLOW + "You may now Teleport.");
                }
             
            }, 30L);
            }else{
                p.sendMessage(ChatColor.RED + "Teleport is on Cooldown!");
                return;
            }
         
    
        }
    P.S. The EyeLocation thing is just something I tried. getLocation().getPitch() etc. doesn't work either.

    Also, one more thing. I have a command that sets a warp location. When the game starts, I want all players to be randomly teleported to one of the locations set in the config without teleporting multiple players to the same warp. How would I do it?
     
    Last edited: May 12, 2016
  2. Offline

    Zombie_Striker

    These methods do not affect the player. You would need to set the yaw and pitch of the teleportation location in order for this to take affect.

    I.E
     
  3. Offline

    OhYes

    Thankyou so much
     
  4. Offline

    Zombie_Striker

    @OhYes
    Mark this thread as solved if your problem has been solved.
     
  5. Offline

    OhYes

    Also, one more thing. I have a command that sets a warp location. When the game starts, I want all players to be randomly teleported to one of the locations set in the config without teleporting multiple players to the same warp. How would I do it? So in my case, 4 players join, so I would set 4 warps. When the runGame() method runs, how would I teleport each player indivisualy to a warp without letting two+ players being teleported to the same warp?
     
  6. Offline

    bennie3211

    Loop through the set of locations, and get the next player that is online and teleport it to the location. If a player is teleported to the last location, get the first location and teleport the next player to it.

    EDIT:

    Code:
    Block block = p.getTargetBlock((HashSet<Byte>)null, 100);
            Location vis = block.getLocation();
    Btw this can throw a NullPointerException if there is no block within 100 blocks! To prevent this check if the block != null before getting it's location!
     
  7. Offline

    OhYes

    Could I get any example code please? I'm not too good with looping. And alright, I'll change it now. Thanks.

    EDIT:
    endGame() also doesn't seem to work.
    Code:
    public void runGame() {
                for(Player all : Bukkit.getOnlinePlayers()){
                    playersInGame.remove(all);
                    all.getInventory().clear();
                    ItemMeta endermeta = enderpearl.getItemMeta();
                    endermeta.setDisplayName(ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + "Ender Shift");
                    endermeta.addEnchant(Enchantment.DURABILITY, 10, true);
                    enderpearl.setItemMeta(endermeta);
                    ItemMeta whackmeta = whacker.getItemMeta();
                    whackmeta.setDisplayName(ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + "End Blade");
                    whackmeta.addEnchant(Enchantment.DURABILITY, 10, true);
                    whacker.setItemMeta(whackmeta);
                    all.getInventory().addItem(enderpearl);
                    all.getInventory().addItem(whacker);
                }
               
                wingame = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    
                    @Override
                    public void run() {
                       
                        remaining = (config.getInt("Maxplayers") - deadPlayers.size());
                       
                        if(remaining == 1){
                            gameState = GAMESTATE.PostGame;
                            endGame();
                            Bukkit.getScheduler().cancelTask(wingame);
                        }
                       
                    }
                   
                }, 0L, 20L);
           
        }
       
        //Ends the game
        protected void endGame() {
    
           
            for(Player all : Bukkit.getOnlinePlayers()){
                if(!(deadPlayers.contains(all))) return;
                Bukkit.broadcastMessage(prefix + ChatColor.AQUA + all.getName() + " is Victorious! Thanks for Playing EnderChase!");
                all.setGameMode(GameMode.SPECTATOR);
            }
           
            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    
                @Override
                public void run() {
                   
                    for(Player all2 : Bukkit.getOnlinePlayers()){
                        all2.setGameMode(Bukkit.getDefaultGameMode());
                    }
                   
                    playersInGame.clear();
                    cooldown.clear();
                    deadPlayers.clear();
                   
                    Bukkit.getServer().shutdown();
                   
                }
               
            }, 100L);
    
        }
     
    Last edited: May 13, 2016
  8. Offline

    bennie3211

    Sure.

    Code:
    public void teleportToLocationsInOrderOrNameItSomethingElse() {
       //Not tested, typed in reply, but this will do the job I think
            ArrayList<String> playersInGame = new ArrayList<String>();
            ArrayList<Location> loc = new ArrayList<Location>();
            int pos = 0;
    
            if (playersInGame.isEmpty())
                return;
           
            for (String s : playersInGame) {
                Player p = Bukkit.getPlayer(s);
               
                p.teleport(loc.get(pos));
                pos++;
               
                if (pos >= loc.size())
                    pos = 0;
            }
        }
    
     
  9. Offline

    OhYes

    Where would the config come in all this? Uh.. would I do a for loop adding all of the positions into the loc arraylist? I don't quite understand how it works.
     
  10. Offline

    bennie3211

    Most of the time you place all information into a config file that will load on startup in variables. When editing values from a game (like adding a new spawnpoint) you add it to the variable and you edit the current config. That way you don't need to load everything all the time from the config itself but loop through the variable in the main/side classes.
     
  11. Offline

    OhYes

    I don't understand. So, I have the setwarp command thing, so.. this method will automatically know to take the locations from the config?? Or do I need to edit the code
    ?
     
  12. Offline

    bennie3211

    Okay, you have a config.yml file where you store all the data like the locations and other things. When the server starts it will get all data from the config and stores it in variables that you have declared. After all data is loaded from the config you can just get the variable where the data is stored.

    An other way is, when you want to get everything from the config file, you can replace the arrayList by a getConfig().getStringList(path).
     
  13. Offline

    OhYes

    Right okay.. I think I understand.. Let me try to test it.

    Nope, I still don't get it. ;-; Sorry. This is my config:
    Code:
    # EnderChase Config
    Locations:
      memes:
        X: 117
        Y: 110
        Z: 199
    Maxplayers: 2
    and this is the command that sets the warps:
    Code:
    else if(cmd.getName().equalsIgnoreCase("ecwarp")){
                if(args.length == 0){
                    p.sendMessage(ChatColor.RED + "Specify a name. /ecwarp name");
                }else if(args.length == 1){
                    config.set("Locations." + args[0] + ".World", p.getWorld());
                    config.set("Locations." + args[0] + ".X", p.getLocation().getX());
                    config.set("Locations." + args[0] + ".Y", p.getLocation().getY());
                    config.set("Locations." + args[0] + ".Z", p.getLocation().getZ());
                    p.sendMessage(ChatColor.YELLOW + args[0] + " warp set.");
                    saveConfig();
                }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
    bennie3211 likes this.
  14. Offline

    bennie3211

    p.getWorld().getName(), don't use p.getWorld() else it will place the object world and not the name. And don't forget to save the yaw and pitch if it's a player location

    Edit:

    Maybe you can use this to store and get locations to and from config:
    Code:
    //when the boolean 'yp' is true, it will return the yaw and pitch too
    public String locToStr(Location l, boolean yp) {
            if (yp)
                return l.getWorld().getName() + "," + l.getX() + "," + l.getY() + "," + l.getZ() + "," + l.getYaw() + ","
                        + l.getPitch();
            return l.getWorld().getName() + "," + l.getX() + "," + l.getY() + "," + l.getZ();
        }
    Code:
    public Location strToLoc(String str) {
            //This will return a location
            String[] l = str.split("\\,");
    
            if (l.length == 4)
                return new Location(getPlugin().getServer().getWorld(l[0]), Double.parseDouble(l[1]),
                        Double.parseDouble(l[2]), Double.parseDouble(l[3]));
            if (l.length == 6)
                return new Location(getPlugin().getServer().getWorld(l[0]), Double.parseDouble(l[1]),
                        Double.parseDouble(l[2]), Double.parseDouble(l[3]), Float.parseFloat(l[4]), Float.parseFloat(l[5]));
            return null;
        }
     
  15. Offline

    OhYes

    I really dont get it! D:
    I have no idea what to do.
    Code:
    public void teleportToLocations() {
               //Not tested, typed in reply, but this will do the job I think
                    ArrayList<String> playersInGame = new ArrayList<String>();
                    List<String> loc = getConfig().getStringList("Locations");
                    int pos = 0;
                   
                  
                   
                    for(String locations : loc){
                        strToLoc(locations);
                    }
            
                    if (playersInGame.isEmpty())
                        return;
                  
                    for (String s : playersInGame) {
                        Player p = Bukkit.getPlayer(s);
                      
                        p.teleport(loc.get(pos));
                        pos++;
                      
                        if (pos >= loc.size())
                            pos = 0;
                    }
                }
     
  16. Offline

    bennie3211

    Try this:
    Code:
    public void teleportToLocations() {
               //Not tested, typed in reply, but this will do the job I think
                    ArrayList<String> playersInGame = new ArrayList<String>();
                    List<String> loc = getConfig().getStringList("Locations");
                    int pos = 0;
           
                    if (playersInGame.isEmpty())
                        return;
                 
                    for (String s : playersInGame) {
                        Player p = Bukkit.getPlayer(s);
                     
                        p.teleport(strToLoc(loc.get(pos)));
                        pos++;
                     
                        if (pos >= loc.size())
                            pos = 0;
                    }
                }
    EDIT:

    edit the playerInGame with the players you want to teleport. Because now you teleport no one

    EDIT 2:

    sorry for double post, didn't know that I pressed the wrong button D:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  17. Offline

    OhYes

    Okay, so when creating new warps. It should work when the game starts?
     
  18. Offline

    bennie3211

    If the locations are set as strings with the locToStr(Location loc) function in the config then you can use it like this, but you need to edit the arraylist that contains the player to all online players if you want to do it to all online players
     
  19. Offline

    OhYes

    Hey I tried it but it doesn't teleport me to the location I set. Instead it freezes me in place. Also,
    this method doesn't work for some reason. It doesn't broadcast the winner, or end the game.
    Code:
    //Ends the game
        protected void endGame() {
    
           
            for(Player all : Bukkit.getOnlinePlayers()){
                if(!(deadPlayers.contains(all))) return;
                Bukkit.broadcastMessage(prefix + ChatColor.AQUA + all.getName() + " is Victorious! Thanks for Playing EnderChase!");
                all.setGameMode(GameMode.SPECTATOR);
            }
           
            Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    
                @Override
                public void run() {
                   
                    for(Player all2 : Bukkit.getOnlinePlayers()){
                        all2.setGameMode(Bukkit.getDefaultGameMode());
                        all2.setHealth(0);
                        all2.kickPlayer(ChatColor.WHITE + "Disconnected: The Game Has Ended.");
                        Bukkit.getServer().reload();
                    }
                   
                    playersInGame.clear();
                    cooldown.clear();
                    deadPlayers.clear();
                   
                   
                }
               
            }, 100L);
    
        }
    Anyone know why endGame() isn't activating and choosing a victor?

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

    bennie3211

    I think your game
    End() doesnt work becuase you reload the sever for the amount of players online if you run the function. (Bukkit.getServer().reload();)
     
  21. Offline

    OhYes

    What would I do instead? Also, do you know why the teleporting doesn't work? You know what, I'll send you the entire code.

    http://pastebin.com/sCdaCpcK
     
  22. Offline

    bennie3211

    Okay, try to split your JavaPlugin class into seperate classes, that will make it easier to read your code. And try to create one main runnable that will have if, else if and else statement where you check the game state. And don't use static if not needed. You can all turn it to public or private and use getters and setters to get the value from the other classes.

    For the teleporting:
    remove the arraylist in the methode and change it so it will loop through the online players that are alive. For the reload part, place the reload methode behind the for loop through the players in the end runnable.
     
  23. Offline

    OhYes

    I did the thing in endGame() but it still doesn't work.
     
  24. Offline

    bennie3211

    try to add debug messages, check where it hangs to find the problem. I thought it was the reload part, but I'm not sure anymore
     
  25. Offline

    OhYes

    Do you think I could get any raw code for the teleporting, I'm getting stuck.
    Code:
    public void teleportToLocations() {
            //Not tested, typed in reply, but this will do the job I think
                 @SuppressWarnings("unchecked")
                ArrayList<Player> onlinePlayers = (ArrayList<Player>) Bukkit.getOnlinePlayers();
                 List<String> loc = getConfig().getStringList("Locations");
                 int pos = 0;
       
                 if (gameState != GAMESTATE.InGame)
                     return;
             
             
                 for (Player onlinePlayers : Bukkit.getOnlinePlayers()) {
                    
                 
                     all.teleport(strToLoc(loc.get(pos)));
                     pos++;
                 
                     if (pos >= loc.size())
                         pos = 0;
                 }
             }
     
  26. Offline

    bennie3211

    raw code?
     
  27. Offline

    OhYes

    Code. Just code. I'm getting stuck, so it'd be nice if I could get some code for me. <3
     
  28. Offline

    bennie3211

    What is not working for now? Did you try adding debug messages? The code I gave you before is for getting a location from and to a String. I would suggest to clean up your code with the tips I gave you before and try to debug it first before you ask for code, else you won't learn it for the next time.
     
  29. Offline

    OhYes

    I'm just going to stop the project. I don't know what to do and I cant seem to fix anything.
     
Thread Status:
Not open for further replies.

Share This Page