Making a player vanish for 1 second.

Discussion in 'Plugin Development' started by NDUGAR, May 3, 2014.

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

    NDUGAR

    So I have made a plugin which is called the ghost plugin it tps a player to themselves to sync the client-side:

    Here's my whole code:
    Code:java
    1. package me.Schnel.Ghost;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Effect;
    6. import org.bukkit.Location;
    7. import org.bukkit.Sound;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class main extends JavaPlugin{
    14.  
    15. @Override
    16. public void onEnable(){
    17. getLogger().info("Plugin Enabled");
    18. }
    19.  
    20. @Override
    21. public void onDisable(){
    22. getLogger().info("Plugin Disbled");
    23. }
    24. @Override
    25. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    26. if (cmd.getName().equalsIgnoreCase("ghost")){
    27. Player player = (Player) sender;
    28. if(player.hasPermission("ghost.*")){
    29. Location location = player.getLocation();
    30. player.getPlayer().getWorld().playEffect(location, Effect.ENDER_SIGNAL, 7);
    31. player.getPlayer().getWorld().playEffect(location, Effect.MOBSPAWNER_FLAMES, 5);
    32. player.getPlayer().getWorld().playEffect(location, Effect.SMOKE, 3);
    33. player.getPlayer().getWorld().playEffect(location, Effect.EXTINGUISH, 1);
    34. player.getPlayer().getWorld().playSound(location, Sound.EXPLODE, 5, 3);
    35. String ghost = "tp " + sender.getName() + " " + sender.getName();
    36. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), ghost);
    37. sender.sendMessage(ChatColor.DARK_GRAY + "You have been tp'ed back");
    38. return true;
    39. }
    40. }
    41. return false;
    42.  
    43. }
    44. }
    45.  


    So where should I put it? Also what should I do for the only vanish for like 1 second?
     
  2. Offline

    hintss

    have a runnable un-hide them after a second?
     
    NDUGAR likes this.
  3. Offline

    NDUGAR

    Still don't quite understand this, would I be required to create a new class?

    Any help anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  4. Offline

    itzrobotix


    Code:java
    1. for(Player pl : Bukkit.getOnlinePlayers(){
    2. pl.hidePlayer(player);
    3. }
    4. Bukkit.getScheduler().scheduleSyncDelayedTask(*instance of main class*, new Runnable(){
    5. public void run(){
    6. for(Player pl : Bukkit.getOnlinePlayers(){
    7. pl.showPlayer(player);
    8. }
    9. }
    10. }, 20)

    ^ It is 20 because there are 20 ticks in a second.
     
  5. Offline

    mibac138

    Making it a method:
    Code:java
    1. public void HidePlayer ( final Player p, int secs ) {
    2. p.hidePlayer( p );
    3.  
    4. for ( Player p2 : Bukkit.getOnlinePlayers( ) ) {
    5. p2.hidePlayer( p );
    6. }
    7.  
    8. getServer( ).getScheduler( ).scheduleSyncDelayedTask( this,
    9. new Runnable( ) {
    10.  
    11. @Override
    12. public void run ( ) {
    13. p.hidePlayer( p );
    14.  
    15. for ( Player p2 : Bukkit.getOnlinePlayers( ) ) {
    16. p2.showPlayer( p );
    17. }
    18.  
    19. }
    20.  
    21. }, secs * 20 );
    22. }


    Example usage:
    Code:java
    1. @Override
    2. public boolean onCommand ( CommandSender sender, Command cmd, String label,
    3. String[] args ) {
    4. if ( cmd.getName( ).equalsIgnoreCase( "ghost" ) ) {
    5. HidePlayer( (Player) sender, args[ 0 ] );
    6. }
    7. return false;
    8.  
    9. }
    10.  
     
  6. Offline

    NDUGAR

    So would this work:
    Code:java
    1. package me.Schnel.Ghost;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Effect;
    6. import org.bukkit.Location;
    7. import org.bukkit.Sound;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class main extends JavaPlugin{
    14.  
    15. @Override
    16. public void onEnable(){
    17. getLogger().info("Plugin Enabled");
    18. }
    19.  
    20. @Override
    21. public void onDisable(){
    22. getLogger().info("Plugin Disbled");
    23. }
    24. @Override
    25. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    26. if (cmd.getName().equalsIgnoreCase("ghost")){
    27. final Player player = (Player) sender;
    28. if(player.hasPermission("ghost.*")){
    29. Location location = player.getLocation();
    30. player.getPlayer().getWorld().playEffect(location, Effect.ENDER_SIGNAL, 7);
    31. player.getPlayer().getWorld().playEffect(location, Effect.MOBSPAWNER_FLAMES, 5);
    32. player.getPlayer().getWorld().playEffect(location, Effect.SMOKE, 3);
    33. player.getPlayer().getWorld().playEffect(location, Effect.EXTINGUISH, 1);
    34. player.getPlayer().getWorld().playSound(location, Sound.EXPLODE, 5, 3);
    35. String ghost = "tp " + sender.getName() + " " + sender.getName();
    36. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), ghost);
    37. sender.sendMessage(ChatColor.DARK_GRAY + "You have been tp'ed back");
    38. for(Player pl : Bukkit.getOnlinePlayers()){
    39. pl.hidePlayer(player);
    40. }
    41. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    42. public void run(){
    43. for(Player pl : Bukkit.getOnlinePlayers()){
    44. pl.showPlayer(player);
    45. }
    46. }
    47. }, 20);
    48. return true;
    49. }
    50. }
    51. return false;
    52.  
    53. }
    54. }
    55.  


    It came up with this: Unexpected exception while parsing console command "ghost"
    org.bukkit.command.CommandException: Unhandled exception executing command 'ghost' in plugin Ghost v1.0.3
    Anyone know why it comes up with the error?
     
  7. Offline

    Teddinator

    I got the same error as you, it had something to do with my plugin.yml. My guess is that you check your plugin.yml.
     
  8. Offline

    NDUGAR


    Before I added the scheduler it was working fine in game...
     
  9. Offline

    Commander9292

    You literally just told us what the issue was...
     
  10. Offline

    NDUGAR

    Yes but I want the scheduler to hide the player for a second so I was wondering on how to fix the scheduler... :p
     
  11. Offline

    Commander9292

    Oh gotcha. My bad! I'd type up some code but I'm on my phone. I can help you later if you haven't figured it out by then.
     
  12. Offline

    NDUGAR

    I doubt I can figure it out within the next 10 hours xD
     
  13. Offline

    Commander9292

    Lol. Well I'll be home in 10 minutes so..
     
  14. Offline

    NDUGAR

    Oh k, thanks :p Sorry if this is a very nooby mistake :D

    My last piece of code was horribly indented so I decided to fix that :p

    Code:java
    1. package me.Schnel.Ghost;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Effect;
    6. import org.bukkit.Location;
    7. import org.bukkit.Sound;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class main extends JavaPlugin{
    14.  
    15. @Override
    16. public void onEnable(){
    17. getLogger().info("Plugin Enabled");
    18. }
    19.  
    20. @Override
    21. public void onDisable(){
    22. getLogger().info("Plugin Disbled");
    23. }
    24. @Override
    25. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    26. if (cmd.getName().equalsIgnoreCase("ghost")){
    27. final Player player = (Player) sender;
    28. if(player.hasPermission("ghost.*")){
    29. Location location = player.getLocation();
    30. player.getPlayer().getWorld().playEffect(location, Effect.ENDER_SIGNAL, 7);
    31. player.getPlayer().getWorld().playEffect(location, Effect.MOBSPAWNER_FLAMES, 5);
    32. player.getPlayer().getWorld().playEffect(location, Effect.SMOKE, 3);
    33. player.getPlayer().getWorld().playEffect(location, Effect.EXTINGUISH, 1);
    34. player.getPlayer().getWorld().playSound(location, Sound.EXPLODE, 5, 3);
    35. String ghost = "tp " + sender.getName() + " " + sender.getName();
    36. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), ghost);
    37. sender.sendMessage(ChatColor.DARK_GRAY + "You have been tp'ed back");
    38. for(Player pl : Bukkit.getOnlinePlayers()){
    39. pl.hidePlayer(player);
    40. }
    41. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    42. public void run(){
    43. for(Player pl : Bukkit.getOnlinePlayers()){
    44. pl.showPlayer(player);
    45. }
    46. }
    47. }, 20);
    48. return true;
    49. }
    50. }
    51. return false;
    52.  
    53. }
    54. }
    55.  


    And YAML:

    Code:
    name: Ghost
    author: SchnelStudios Inc.
    version: 1.0.3
    main: me.Schnel.Ghost.main
    description: Ever get endlessly tp'ed into a block or just stuck in limbo, using /ghost will make your day
    permissions:
      ghost.*:
        default: op
     
     
    commands:
      ghost:
        usage: /ghost
        description: teleports a player to their server location instead of their client side location.
        permission: ghost.*
        permission-message: You cannot disappear
    Commander9292 I still got no luck, any help :? :D

    Any help from anyone?
    Please help :p


    Bump!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  15. Offline

    raGan.

    Start by pasting full exception to the pastebin or similar site, and providing us with the link.
     
  16. Offline

    NDUGAR

    http://pastebin.com/JFX3NV5N

    Code:java
    1. package me.Schnel.Ghost;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Effect;
    6. import org.bukkit.Location;
    7. import org.bukkit.Sound;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class main extends JavaPlugin{
    14.  
    15. @Override
    16. public void onEnable(){
    17. getLogger().info("Plugin Enabled");
    18. getLogger().severe("Uh oh! The plugin is DEAD!!! (or broken)");
    19. }
    20.  
    21. @Override
    22. public void onDisable(){
    23. getLogger().info("Plugin Disbled");
    24. }
    25. @Override
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    27. if (cmd.getName().equalsIgnoreCase("ghost")){
    28. final Player player = (Player) sender;
    29. if(player.hasPermission("ghost.*")){
    30. Location location = player.getLocation();
    31. player.getPlayer().getWorld().playEffect(location, Effect.ENDER_SIGNAL, 7);
    32. player.getPlayer().getWorld().playEffect(location, Effect.MOBSPAWNER_FLAMES, 5);
    33. player.getPlayer().getWorld().playEffect(location, Effect.SMOKE, 3);
    34. player.getPlayer().getWorld().playEffect(location, Effect.EXTINGUISH, 1);
    35. player.getPlayer().getWorld().playSound(location, Sound.EXPLODE, 5, 3);
    36. String ghost = "tp " + sender.getName() + " " + sender.getName();
    37. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), ghost);
    38. sender.sendMessage(ChatColor.DARK_GRAY + "You have been tp'ed back");
    39. for(Player pl : Bukkit.getOnlinePlayers()){
    40. pl.hidePlayer(player);
    41. }
    42. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    43. public void run(){
    44. for(Player pl : Bukkit.getOnlinePlayers()){
    45. pl.showPlayer(player);
    46. }
    47. }
    48. }, 60);
    49. return true;
    50. }
    51. }
    52. return false;
    53.  
    54. }
    55. }
    56.  


    The vanish doesn't work :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  17. Offline

    Garris0n

    You cast the sender to a player without actually checking if it's a player and then proceeded to run the command from the console?

    Also, instead of using the /tp command, you can use Player#teleport(Location) or Player#teleport(Entity) instead.
     
  18. Offline

    NDUGAR

    I don't really understand the first bit also Player#teleport(location) comes up with errors...

    Code:java
    1. package me.Schnel.Ghost;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Effect;
    6. import org.bukkit.Location;
    7. import org.bukkit.Sound;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class main extends JavaPlugin{
    14.  
    15. @Override
    16. public void onEnable(){
    17. getLogger().info("Plugin Enabled");
    18. getLogger().severe("Uh oh! The plugin is DEAD!!! (or broken)");
    19. }
    20.  
    21. @Override
    22. public void onDisable(){
    23. getLogger().info("Plugin Disbled");
    24. }
    25. @Override
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    27. if (cmd.getName().equalsIgnoreCase("ghost")){
    28. final Player player = (Player) sender;
    29. if(player.hasPermission("ghost.*")){
    30. Location location = player.getLocation();
    31. player.getPlayer().getWorld().playEffect(location, Effect.ENDER_SIGNAL, 7);
    32. player.getPlayer().getWorld().playEffect(location, Effect.MOBSPAWNER_FLAMES, 5);
    33. player.getPlayer().getWorld().playEffect(location, Effect.SMOKE, 3);
    34. player.getPlayer().getWorld().playEffect(location, Effect.EXTINGUISH, 1);
    35. player.getPlayer().getWorld().playSound(location, Sound.EXPLODE, 5, 3);
    36. String ghost = Player#teleport(location);
    37. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), ghost);
    38. sender.sendMessage(ChatColor.DARK_GRAY + "You have been tp'ed back");
    39. for(Player pl : Bukkit.getOnlinePlayers()){
    40. pl.hidePlayer(player);
    41. }
    42. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    43. public void run(){
    44. for(Player pl : Bukkit.getOnlinePlayers()){
    45. pl.showPlayer(player);
    46. }
    47. }
    48. }, 60);
    49. return true;
    50. }
    51. }
    52. return false;
    53.  
    54. }
    55. }
    56.  
     
  19. Offline

    Garris0n

    NDUGAR That is the method, not something you just type in to make it work. The method is teleport, it accepts either an entity or location, and it is in the class Player.
     
  20. Offline

    NDUGAR

    Thanks, I finally understood that, and also why isn't the vanish working?

    raGan. any help on the vanishing mechanism?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  21. Offline

    NDUGAR

    Any help on the vanish not working?

    Anyone, help on vanishing method?

    Here's my code:

    Code:java
    1. package me.Schnel.Ghost;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Effect;
    6. import org.bukkit.Location;
    7. import org.bukkit.Sound;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class main extends JavaPlugin{
    14.  
    15. @Override
    16. public void onEnable(){
    17. getLogger().info("Plugin Enabled");
    18. getLogger().severe("Uh oh! The plugin is DEAD!!! (or broken)");
    19. }
    20.  
    21. @Override
    22. public void onDisable(){
    23. getLogger().info("Plugin Disbled");
    24. }
    25. @Override
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    27. if (cmd.getName().equalsIgnoreCase("ghost")){
    28. final Player player = (Player) sender;
    29. if(player.hasPermission("ghost.*")){
    30. Location location = player.getLocation();
    31. player.getPlayer().getWorld().playEffect(location, Effect.ENDER_SIGNAL, 7);
    32. player.getPlayer().getWorld().playEffect(location, Effect.MOBSPAWNER_FLAMES, 5);
    33. player.getPlayer().getWorld().playEffect(location, Effect.SMOKE, 3);
    34. player.getPlayer().getWorld().playEffect(location, Effect.EXTINGUISH, 1);
    35. player.getPlayer().getWorld().playSound(location, Sound.EXPLODE, 5, 3);
    36. String ghost = "tp " + sender.getName() + " " + location;
    37. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), ghost);
    38. sender.sendMessage(ChatColor.DARK_GRAY + "You have been tp'ed back");
    39. for(Player pl : Bukkit.getOnlinePlayers()){
    40. pl.hidePlayer(player);
    41. }
    42. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    43. public void run(){
    44. for(Player pl : Bukkit.getOnlinePlayers()){
    45. pl.showPlayer(player);
    46. }
    47. }
    48. }, 60);
    49. return true;
    50. }
    51. }
    52. return false;
    53.  
    54. }
    55. }
    56.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  22. Offline

    AoH_Ruthless

    NDUGAR
    Well you have to tell us what isn't working and any errors you get..
     
  23. Offline

    NDUGAR

    The command runs perfectly fine apart from the fact that the player doesn't vanish for 3 seconds :D -no errors in the console...

    So in short, it works; but when I use the scheduler for creating the vanishing effect... Any help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  24. Offline

    Jaaakee224

    NDUGAR Remember the rule about bumping.
     
  25. Offline

    NDUGAR

    Sorry but do you have any help for me? Instead of reminding me the rules, you could help and then add that helpful suggestion that is very off-topic... Thanks anyway... But I just really need help with an explanation, unless you can do that, please don't say anything that isn't useful mediocrely and I understand that rules are rules, but instead of saying this to me send your inquiry to a mod.
     
Thread Status:
Not open for further replies.

Share This Page