Setting the player location?

Discussion in 'Plugin Development' started by firecombat4, Jul 18, 2012.

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

    firecombat4

    So ive really just started learning plugins, and combining http://wiki.bukkit.org/Plugin_Tutorial and changing the events around using http://jd.bukkit.org/apidocs/ i was able to come up with this.
    Code:
    package com.hotmail.stevenbellamy.test;
     
    import java.util.logging.Logger;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class test extends JavaPlugin{
        public final Logger logger = Logger.getLogger("Minecraft");
       
       
        @Override
        public void onEnable(){
            new PlayerMoveListener(this);   
           
        }
       
            public void onDisable(){
        }
    }
    
    Code:
    package com.hotmail.stevenbellamy.test;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.Plugin;
     
    public class PlayerMoveListener implements Listener{
        private Plugin plugin;
       
        public PlayerMoveListener(test plugin){
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
     
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent evt, Location from) {
            Player player = evt.getPlayer();
            Location loc = evt.getPlayer().getLocation();
            if(loc.getZ() > 50 || loc.getX() > 50 || loc.getZ() < - 50 || loc.getX() < - 50){
                player.sendMessage(ChatColor.GOLD + "You cannot do that!");
                }
            }
        }
    
    And then if your location is X,Z below or above 50 itl message you saying "You cannot do that"
    I am just wondering how i would "bounce" the player back as if it were a barrier. Ive looked but cannot find a source code.

    Thanks!
     
  2. Offline

    tabr

    it need to be X be between -50 AND 50 AND Z between -50 and 50.
    for example 0:y:0.
    try:
    Code:
    player.sendMessage(ChatColor.GOLD + "You cannot do that!["+loc.getX()+":"+loc.getZ()+"]");
     
  3. Offline

    r0306

    tabr
    I think it's something like this:
    Code:
    player.setVelocity(player.getVelocity().getDirection().normalize().multiply(-1));
     
  4. Offline

    firecombat4

    Im getting an error on "getDirection", undefined for the type vector.
    Thanks
     
  5. Offline

    r0306

    firecombat4
    Woops. It's actually:
    Code:
    player.setVelocity(player.getDirection().normalize().multiply(-1));
     
  6. Offline

    Njol

    This would throw him out of the boundary if he's walking backwards.
    Your first code was correct, there was only a getDirection() too much:
    Code:
    player.setVelocity(player.getVelocity().normalize().multiply(-1));
    BTW:
    Remove 'Location from' from the method - event handlers only have one argument which is the event.
     
  7. Offline

    firecombat4

    I tried this before, and it semi works. It spazzes you in and out of the border until you walk away from the border, where it kind of "lets you go"
     
  8. Offline

    Njol

    Cancel the PlayerMoveEvent if event.getTo() is outside of the boundaries (i.e. change your code to use event.getTo() instead of event.getPlayer().getLocation() and cancel the event inside the if statement):
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent evt) {
            Player player = evt.getPlayer();
            Location loc = evt.getTo();
            if(loc.getZ() > 50 || loc.getX() > 50 || loc.getZ() < - 50 || loc.getX() < - 50){
                evt.setCancelled(true);
                player.setVelocity(...);
            }
        }
    
     
  9. Offline

    firecombat4

    That seems to work, thank you. its very buggy but ill play around with it haha :D. thanks a lot

    So i found out something that might work for me, Location from = evt.getFrom();, player.teleport(from);, this seems to work best but to get the bounce effect i need to get the X,Z and then take off say 5, which would teleport them 5 blocks "inside" the border. Im just wondering how i would do that as X,Z can be either negative or positive value.
    Thanks heaps

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

    Njol

    You could use something like this:
    Code:
    Location to = event.getFrom();
    to.setX(to.getX() < 0 ? to.getX() + 5 : to.getX() - 5);
    to.setZ(...);
    player.teleport(to);
    I do not recomment this though, as it's wierd being teleported 5 blocks backwards (and it can cause the player to be teleported into solid blocks, possibly causing them to suffocate).
     
  11. Offline

    firecombat4

    I will use a value such as 2 or do an extra check for blocks. But thanks man!.
    Also does to.setX(to.getX() < 0 ? mean if X is bigger then 0 then do "this"? i saw the question mark in borderguards source code as well.
    Thanks
     
  12. Offline

    Njol

    I don't know that it is called, but it's an inlined if statement:
    Code:
    condition ? true-expression : false-expression
    e.g. 'true ? 1 : 0' returns 1, 'x > 10 ? "yes" : "nope"' returns "yes" if x is greater than 10, otherwise it returns "nope".
     
  13. Offline

    firecombat4

    Thats awesome!, thanks :D
     
  14. Offline

    talmdal

    It's called a 3-ary operator (takes three operators) similar to binary operators (+ - * /) which take two operators or unary operators (++ -- ! ~) which only take one operator
     
Thread Status:
Not open for further replies.

Share This Page