Solved Teleport

Discussion in 'Plugin Development' started by Fred12i12i, Jan 12, 2014.

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

    Fred12i12i

    Hey im trying to make it so when a mob damage you, you get teleported to a set location. This is the code i have:
    Code:java
    1. @EventHandler
    2. public void playerInteract(EntityDamageEvent e){
    3. Entity p = e.getEntity();
    4. if(pg.InGame.containsKey(p)){
    5. ((Player)p).sendMessage("hi");
    6. ((Player)p).teleport(new Location(Bukkit.getWorld(plugin.getConfig().getString("SWorld")), plugin.getConfig().getInt("SpawnX"), plugin.getConfig().getInt("SpawnY"), plugin.getConfig().getInt("SpawnZ")));
    7. }
    8. }

    When i mob hit me i don't get teleported but i get the hi msg. I have the world, x, y and z location in the config file. I use the same code in a command (/pg spawn) and that works fine. Anyone know what i can do to fix it?
     
  2. Offline

    JoTyler

    If I remember correctly . Configs are strings . You should be getting at error . You need to change the type from string to integer or double
     
  3. Offline

    clmcd42

    If JoTyler's solution doesn't work, try substituting p.getWorld() into your code instead of the config thing, just to see if it works. If it does, then that's your problem.
     
  4. Offline

    xXSniperzzXx_SD

    Fred12i12i

    I'd recomend using the EntityDamageByEntiyEvent if your goal is to tp the player when damaged by a mob
     
  5. Offline

    JoTyler

    xXSniperzzXx_SD. Yep forgot that . Use the method and check if the entity is a .... Etc
     
  6. Offline

    Fred12i12i

  7. Offline

    Garris0n

    Are all those values defined properly in the plugin.yml?

    Are there errors?
     
  8. Offline

    Fred12i12i

    @Garris0nlike like i sayed it works when i use the same code in a command (/sg spawn) so yes they should be. and no i don't get any errors.
     
  9. Offline

    Garris0n

    So the teleport code works in a command and the "hi" message is sent in the event handler? That makes no sense, can you post the command handler/the class?
     
  10. Offline

    Fred12i12i

    Garris0n Just got a NullpointerException Erorr when i moved the code to my PlayerListener class. It still send out the "hi" msg in chat but dosn't tp me.
    Must be something wrong with the tp command
     
  11. Offline

    Garris0n

    So there is an error? Post the error and the full class(es).
     
  12. Offline

    Fred12i12i

    Garris0n yes if i have the code in my main class, i get no Errors but when i put it in the playerListener class i get one
     
  13. Offline

    Garris0n

     
  14. Offline

    Fred12i12i

    Garris0n main class:
    Code:java
    1. public class sg extends JavaPlugin implements Listener{
    2. public final Logger logger = Logger.getLogger("Minecraft");
    3. public static Map<Player, Boolean> InGame = new HashMap<Player, Boolean>();
    4. public static String SworldName;
    5.  
    6. public void onDisable() {
    7. PluginDescriptionFile pdffile = getDescription();
    8. this.logger.info(pdffile.getName() + " Has Been Disabled!");
    9. saveConfig();
    10. }
    11.  
    12. public void onEnable() {
    13. PluginDescriptionFile pdffile = getDescription();
    14. this.logger.info(pdffile.getName() + " Version " + pdffile.getVersion() + " Has Been Enabled!");
    15. PluginManager pm = getServer().getPluginManager();
    16. pm.registerEvents(new MyPlayerListener(), this);
    17.  
    18. getConfig().addDefault("MobX", 0);
    19. getConfig().addDefault("MobY", 0);
    20. getConfig().addDefault("MobZ", 0);
    21. getConfig().addDefault("SWorld", "World");
    22.  
    23. getConfig().options().copyDefaults(true);
    24. saveConfig();
    25. }
    26.  
    27. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    28. Player p = (Player)sender;
    29.  
    30. int X1 = this.getConfig().getInt("SpawnX");
    31. int Y1 = this.getConfig().getInt("SpawnY");
    32. int Z1 = this.getConfig().getInt("SpawnZ");
    33. SworldName = getConfig().getString("SWorld");
    34.  
    35. if (commandLabel.equalsIgnoreCase("sg")) {
    36. if (args.length == 1){
    37. if(args[0].equalsIgnoreCase("setspawn")){
    38. this.getConfig().set("SpawnX", x);
    39. this.getConfig().set("SpawnY", y);
    40. this.getConfig().set("SpawnZ", z);
    41. this.getConfig().set("SWorld", w);
    42. saveConfig();
    43. p.sendMessage(ChatColor.GREEN + "Spawn Set!");
    44. }
    45.  
    46. if(args[0].equalsIgnoreCase("spawn")){
    47. p.teleport(new Location( Bukkit.getWorld(sg.SworldName), X1, Y1, Z1));
    48. InGame.put(p, true);
    49. }
    50. }
    51. }
    52. return false;
    53. }
    54. }

    And the MyPlayerListener class
    Code:java
    1. public class MyPlayerListener implements Listener {
    2. public static sg plugin;
    3. public static String SworldName;
    4.  
    5. public void playerGetHit(EntityDamageByEntityEvent e){
    6. int X1 = plugin.getConfig().getInt("SpawnX");
    7. int Y1 = plugin.getConfig().getInt("SpawnY");
    8. int Z1 = plugin.getConfig().getInt("SpawnZ");
    9. SworldName = plugin.getConfig().getString("SWorld");
    10. Entity p = e.getEntity();
    11. if(sg.InGame.containsKey(p)){
    12. ((Player)p).sendMessage("hi");
    13. ((Player)p).teleport(new Location( Bukkit.getWorld(sg.SworldName), X1, Y1, Z1));
    14. }
    15. }
    16. }

    this is the code i have now and it give no errors
     
  15. Offline

    EnderTroll68

    Fred12i12i

    I have never seen onCommand used like that but in theory it should work.

    Also where is PacMan defined??
     
  16. Offline

    Garris0n

    You're not setting the value of "plugin" in the listener, so I'd expect an NPE there. The only reason you're not getting one is because there's no @EventHandler annotation and thus the event isn't ever firing. Also, you need to fix your variable/class/method names as described here.
     
  17. Offline

    Fred12i12i

  18. Offline

    Garris0n

    Just gonna quickly extra-point-out, you really need to fix the naming stuff. It exists for a reason and will mess you up later if you're not in the right habits.
     
  19. Offline

    Fred12i12i

    Garris0n likes this.
Thread Status:
Not open for further replies.

Share This Page