2 Questions - Teleporting and checking against List

Discussion in 'Plugin Development' started by CRAZYxMUNK3Y, Oct 23, 2012.

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

    CRAZYxMUNK3Y

    Just a quick question.

    What would be the best way to teleport a player backwards a certain amount of blocks as defined in a config?
    Eg, Player is walking east, so they are teleported 20 blocks west.

    Would player.teleport() or Vector be better?

    Thanks
     
  2. Offline

    Codex Arcanum

    Get their targeted block (air if need be), get the vector between them and the target, change the magnitude of the vector to 20, and either teleport them to (their original location + that vector), or just do player.setVelocity(vector).
     
  3. Offline

    CRAZYxMUNK3Y

    Thanks, will check that out when i get to it.

    I have another issue though...

    When i try to check if a player is near it works fine, but if the player is in the config as "blacklisted", nothing happens.
    There are no errors in the console either.

    Code:java
    1.  
    2. package me.crazy.restrainingorder;
    3.  
    4. import java.io.File;
    5. import java.io.FileWriter;
    6. import java.util.List;
    7. import java.util.logging.Logger;
    8.  
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.configuration.file.FileConfiguration;
    11. import org.bukkit.entity.Entity;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.EventPriority;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.player.PlayerMoveEvent;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18. import org.bukkit.util.Vector;
    19.  
    20. public class RestrainingOrder extends JavaPlugin implements Listener {
    21.  
    22. FileConfiguration config;
    23. Logger log = Logger.getLogger("Minecraft");
    24. int radius;
    25. List<String> players;
    26. int pb;
    27. File configFile;
    28. boolean debug;
    29.  
    30. public void onEnable() {
    31. try {
    32. configFile = new File(getDataFolder(), "config.yml");
    33. if (!configFile.exists()) {
    34. configFile.getParentFile().mkdir();
    35. FileWriter fw = new FileWriter(configFile);
    36. String nl = System.getProperty("line.separator");
    37. fw.write("debug: false" + nl + "radius: 5" + nl + "pushback: 5" + nl + "players: " + nl
    38. + " - CRAZYxMUNK3Y");
    39. fw.close();
    40. log.info("Config Created!");
    41. }
    42. config = getConfig();
    43. debug = config.getBoolean("debug");
    44. radius = config.getInt("radius");
    45. pb = config.getInt("pushback");
    46. players = config.getStringList("players");
    47.  
    48. getServer().getPluginManager().registerEvents(this, this);
    49. for (String p : config.getStringList("Players")) {
    50. players.add(p);
    51. }
    52. } catch (Exception e) {
    53. log.severe("[RestrainingOrder] Unable to load plugin! Disabling.");
    54. e.printStackTrace();
    55. getServer().getPluginManager().disablePlugin(this);
    56. }
    57.  
    58. }
    59.  
    60. @EventHandler(priority = EventPriority.LOWEST)
    61. public void playerMove(PlayerMoveEvent event) {
    62. Player player = event.getPlayer();
    63. List<Entity> nearby = player.getNearbyEntities(radius, radius, radius);
    64. if (!nearby.isEmpty()) {
    65. if (debug == true) {
    66. log.info("Players nearby");
    67. }
    68. if (nearby.contains(players.get(0))) {
    69. if (debug == true) {
    70. log.info("Players.get(0) is true");
    71. }
    72. if (player.hasPermission("restrainingorder.approach") || player.isOp()) {
    73. return;
    74. }
    75. } else {
    76. Entity p = nearby.get(0);
    77. Vector v = new Vector();
    78. Vector x = v.setX(-10);
    79. event.setCancelled(true);
    80. player.setVelocity(x);
    81. player.sendMessage(ChatColor.RED + "You are not allowed near the player " + p);
    82. player.sendMessage("You have been teleported back " + pb + " blocks");
    83.  
    84. }
    85. } else {
    86. if (debug == true) {
    87. log.info("No players nearby");
    88. }
    89. return;
    90. }
    91. }
    92. }
    93.  
    94.  


    It shows in the console as "Players near/not near", but not Players.get(0) when within range.

    What I am trying to do is;
    OnEnable, add all players in the config to a list, then on the PlayerMoveEvent, check for players around them, if a player near them is in the config, throw the 1st player back x blocks.

    Hopefully that makes sense.

    Thanks in advanced.
     
Thread Status:
Not open for further replies.

Share This Page