Need help on - "If a player moves it will teleport them back to there original location"

Discussion in 'Plugin Development' started by DeFlavLemon, Aug 8, 2016.

Thread Status:
Not open for further replies.
  1. Hello, thank you for wasting your time on reading this thread.

    So I'm making a Survival Games plugin for a friend of mynes server, and its going great so far! But that's besides the point, what I want to do is make it so whenever a player moves forward it will teleport them to there original location, but when the minimum player amount joins (4) it will allow them to move once 60 seconds have passed. If you could help that could be great! Here is my code at the moment:
    Code:
    package me.deflavlemonyt.sg;
    
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class SurvivalGames extends JavaPlugin implements Listener {
    
        private Material[] items;
    
        @Override
        public void onEnable() {
           
                Bukkit.getServer().getPluginManager().registerEvents(new SignListeners(), this);
            System.out.println(Arrays.toString(items));
        }
       
        public boolean onCommand(CommandSender sender, Player player, Command cmd, Block block, Material material,
                String label, String[] args) {
    
            if (cmd.getName().equalsIgnoreCase("sg")) {
                player.setHealth(20);
                player.setFoodLevel(20);
                player.getPlayer()
                        .sendMessage(ChatColor.GREEN + "You have succesfully teleported to the survival games arena!");
                return true;
            }
            return false;
        }
    
        @EventHandler
            public void onPlayerMove(PlayerMoveEvent event) {
            Player player = event.getPlayer();
           
            }
        }
    Thanks! Keep on coding! Goodbye for now!
     
    Last edited by a moderator: Aug 8, 2016
  2. @DeFlavLemon
    All you need to do is cancel the PlayerMoveEvent, unless there are 4 players.
     
  3. How would I do that? Would I make a if statement? If so then what would I put in it? Sorry, I'm a bit newby. xD
     
  4. @DeFlavLemon
    Well I assume you have some sort of List/array for all the players in your arena, just check the size of this list/array and if it is 4, stop canceling the events.
     
  5. I understand that, but what would the line of code be? Please can you write it? I have my player array already setup but I just need to know "how" to do it not "what" happens. I already know what happens.
     
  6. Offline

    MordorKing78

    @AlvinB That will not be "smooth" at all
     
  7. What would be? Can you help me too?
     
  8. @DeFlavLemon
    Alright so, we're not going to simply give you the code here, you're the one coding the plugin.

    So, let's say you have a List<UUID> with all your players in it. All you need to check if the ".size()" method of the list, and if it is equal to or greater than four, cancel the event.

    @MordorKing78
    Of course it won't be smooth, but there is absolutely nothing you can do about it, unless you modify the client.
     
  9. Offline

    ItsMas_

    @AlvinB

    You shouldn't cancel a MoveEvent, instead use e.setTo(e.getFrom());
     
  10. @ItsMas_
    Would you like to enlighten me as to why you shouldn't cancel it?
     
  11. Ok cool! I'll try that. Thanks :)
     
  12. Offline

    MordorKing78

    @AlvinB I think he actually wants players to be able to look around and not getting a seizure while waiting for the game to start ;) ( Example: Video/Gif )

    EDIT: @DeFlavLemon I've created a piece of code which should properly do what you requested without canceling the event.

    Steps:
    1. Check if the player has moved a whole block
    2. If the player has moved a whole block get his old location
    3. Add his Pitch and Yaw
    4. Teleport the player back

    PHP:
        @EventHandler
        
    public void playerMovement(PlayerMoveEvent e){
            if(
    e.getPlayer().isOp()){return;} //Checks if player is a Operator, if so it will return and not read any further. Remove if you do not want Operators to be able to move.
     
            
    double X e.getFrom().getBlockX(); //Get player his old X coordinate
            
    double Y e.getFrom().getBlockY(); //Get player his old Y coordinate
            
    double Z e.getFrom().getBlockZ(); //Get player his old Z coordinate
            
    World w e.getPlayer().getWorld(); //Get player his world
     
            
    if(e.getFrom().getBlockX() == e.getTo().getBlockX() && e.getFrom().getBlockY() == e.getTo().getBlockY() && e.getFrom().getBlockZ() == e.getTo().getBlockZ()) return; //check if player has moved a whole block
            
    Location oldBlock = new Location(wXYZ); //Get the whole location of the block the player stand on before he moved

            
    oldBlock.setPitch(e.getPlayer().getLocation().getPitch()); //set the pitch of the player
            
    oldBlock.setYaw(e.getPlayer().getLocation().getYaw()); //set the yaw of the player
            
    e.setTo(oldBlock); //move the player back to his position
        
    }
    I did not actually test the code but it should work.

    Steps if you want to make it work with your SG:
    1. Check if the game hasn't started yet.
    2. If the game hasn't started yet launch the code (if it has started just return the code)
    3. Once a new player joins the arena check if the arena has exceeded 4 players, if it has start a countdown from 60 seconds.

    This would be the basics of how it should work.
     
    Last edited: Aug 8, 2016
  13. @MordorKing78
    If that's what he wants to do, then doing something like this would save him a fair amount of lines:
    Code:java
    1. event.setTo(event.getPlayer().getLocation().setDirection(event.getTo().getDirection()))
    What we're doing here is, set the location the player is moving to, to be the same location he is at, but with the Direction of where the new location should be.
     
  14. Offline

    MordorKing78

    @AlvinB Wouldn't that be exact the same as cancelling the event?
     
  15. @MordorKing78
    Yes, but you can look around, since the Direction is carried over.
     
  16. I'll try both of your techniques, thanks for your help.

    I'm getting an error on line "50" could you help me? I just wanted to mention that I moved it over to my SignListeners file so when you clicked on the Join SG sign it would do the method instead.

    Code:
    package me.deflavlemonyt.sg;
    
    import java.util.ArrayList;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    
    import net.minecraft.server.v1_8_R3.World;
    
    public class SignListeners implements Listener {
     
        ArrayList<String> player = new ArrayList<String>();
     
        @EventHandler
        public void onSignChange(SignChangeEvent event) {
         
            if (event.getLine(0).equalsIgnoreCase("[SG]")) {
                event.setLine(0, "§3Join");
                event.setLine(1, "§bSG Arena");
            }
        }
     
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event, PlayerMoveEvent event1) {
            Player player = event.getPlayer();
         
            if (!(event.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            if (event.getClickedBlock().getState() instanceof Sign) {
                Sign sign = (Sign) event.getClickedBlock().getState();
                if (sign.getLine(0).equalsIgnoreCase("§3Join")) {
                 
                    event.getPlayer().setHealth(20);
                    event.getPlayer().setFoodLevel(20);
                    event.getPlayer().getPlayer().sendMessage(ChatColor.GREEN + "You have succesfully teleported to the survival games arena!");
                 
                    double X = event1.getFrom().getBlockX();
                   double Y = event1.getFrom().getBlockY();
                   double Z = event1.getFrom().getBlockZ();
                   World world = (World) event.getPlayer().getWorld();
               
                   if(event1.getFrom().getBlockX() == event1.getTo().getBlockX() && event1.getFrom().getBlockY() == event1.getTo().getBlockY() && event1.getFrom().getBlockZ() == event1.getTo().getBlockZ()) return;
                   Location oldBlock = new Location(world, X, Y, Z);
    
                   oldBlock.setPitch(event.getPlayer().getLocation().getPitch());
                   oldBlock.setYaw(event.getPlayer().getLocation().getYaw());
                   event1.setTo(oldBlock);
                }
            }
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 9, 2016
  17. Offline

    Zombie_Striker

    Why can't you just use "oldBlock = event1.getFrom().getLocation()"

    Also, YOU CANNOT HAVE MULTIPLE EVENTS IN ONE METHOD! This is bold and uppercase because this is really important. For this, you will have to check if a player clicked a sign in one event, store that the player clicked that sign, and then in another event check if the player moved.
     
  18. Ah okay, thanks.

    Shall I put that in my main class because it wont work in the SignListeners class.

    getLocation wont work for some reason.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 9, 2016
  19. Offline

    Zombie_Striker

    @DeFlavLemon
    Just realized that "getFrom()" is already a location. Remove the "getLocation"
     
  20. Ok cool.
     
  21. Offline

    MordorKing78

    If you haven't tried @AlvinB his method yet, try that, it might work.
     
  22. Can I just point out that you may want to do a bit of research into classes and events in both java and Bukkit so you can make your minigame a LOT easier
     
Thread Status:
Not open for further replies.

Share This Page