[Help] Plugin Help List!

Discussion in 'Plugin Development' started by nubpro, Jun 8, 2011.

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

    nubpro

    Yesterday, I made a plugin which can warp you to your portals location. I tried my plugin and typed /portal, then it freezed my server. Could any experience plugin dev or players could help me
    ?
    The Source Code !
    Show Spoiler


    • package net.nubpro.project.plugin.TPportals;

      import java.io.File;
      import java.util.logging.Logger;
      import org.bukkit.ChatColor;
      import org.bukkit.Location;
      import org.bukkit.command.Command;
      import org.bukkit.command.CommandSender;
      import org.bukkit.entity.Player;
      import org.bukkit.plugin.java.JavaPlugin;

      public class TPportals extends JavaPlugin
      {
      private static final Logger log = Logger.getLogger("Minecraft");

      Data data = new Data();
      Location loc;
      ChatColor standard = ChatColor.WHITE;
      ChatColor special = ChatColor.RED;

      public void onDisable()
      {
      log.info("HomeX disabled");
      }

      public void onEnable()
      {
      log.info("HomeX enabled");

      if (!new File("plugins/TpPortal").exists()) {
      log.info("TpPortal directory does not exist. Creating new folder...");
      new File("plugins/TpPortal").mkdir();
      log.info("TpPortal direcotry created.");
      }

      this.standard = Data.getStandardColor();
      this.special = Data.getSpecialColor();
      }

      public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
      if ((sender instanceof Player)) {
      Player player = (Player)sender;
      String world = player.getWorld().getName();

      if (cmd.getName().equalsIgnoreCase("setportal")) {
      this.loc = player.getLocation();

      Data.setHome(this.loc, world);

      player.sendMessage(this.standard + "Your " + this.special + "portal" + this.standard + " has been set.");
      }

      if (cmd.getName().equalsIgnoreCase("portal")) {
      if (Data.homeSet( world)) {
      this.loc = player.getLocation();
      this.loc = Data.getHome(this.loc, world);

      player.teleportTo(this.loc);

      player.sendMessage(this.standard + "You have been teleported to your " + this.special + "portal" + this.standard + ".");
      } else {
      player.sendMessage(this.standard + "You have to set a " + this.special + "portal" + this.standard + " first.");
      }
      }
      }

      return true;
      }
      }


    Data Class!
    Show Spoiler


    • package net.nubpro.project.plugin.TPportals;

      import java.io.BufferedReader;
      import java.io.BufferedWriter;
      import java.io.File;
      import java.io.FileReader;
      import java.io.FileWriter;
      import org.bukkit.ChatColor;
      import org.bukkit.Location;

      public class Data
      {
      public static void setHome(Location loc, String world)
      {
      File f = new File("plugins/TpPortal/" + world);
      try
      {
      if (!f.exists()) {
      f.mkdir();
      }

      f = new File("plugins/TpPortal/" + world + "/portal.txt");

      if (!f.exists()) {
      f.createNewFile();
      }

      BufferedReader br = new BufferedReader(new FileReader(f));

      String[] save = new String[1000];
      String s = "";
      int j = 0;

      while ((s = br.readLine()) != null) {
      {
      save[j] = s;
      j++;
      }
      }

      br.close();

      BufferedWriter bw = new BufferedWriter(new FileWriter(f));

      String x = " " + String.valueOf(loc.getX());
      String y = " " + String.valueOf(loc.getY());
      String z = " " + String.valueOf(loc.getZ());
      String yaw = " " + String.valueOf(loc.getYaw());
      String pitch = " " + String.valueOf(loc.getPitch());

      bw.write( x + y + z + yaw + pitch);
      bw.newLine();

      j = 0;

      while (save[j] != null) {
      bw.write(save[j]);
      bw.newLine();
      }

      bw.flush();
      bw.close();
      } catch (Exception e) {
      System.out.println(e);
      }
      }

      public static boolean homeSet(String world) {
      File f = new File("plugins/TpPortal/" + world + "/portal.txt");

      if (f.exists())
      try {
      BufferedReader br = new BufferedReader(new FileReader(f));
      while ((br.readLine()) != null) {
      {
      return true;
      }
      }

      return false;
      } catch (Exception e) {
      System.out.println(e);
      }
      else {
      return false;
      }

      return false;
      }

      public static Location getHome(Location loc , String world) {
      File f = new File("plugins/TpPortal/" + world + "/portal.txt");
      try
      {
      BufferedReader br = new BufferedReader(new FileReader(f));
      String s = "";

      while ((s = br.readLine()) != null)
      if (s.split(" ")[0] != null) {
      Double x = Double.valueOf(s.split(" ")[1]);
      Double y = Double.valueOf(s.split(" ")[2]);
      Double z = Double.valueOf(s.split(" ")[3]);
      Float yaw = Float.valueOf(s.split(" ")[4]);
      Float pitch = Float.valueOf(s.split(" ")[5]);

      loc.setX(x.doubleValue());
      loc.setY(y.doubleValue());
      loc.setZ(z.doubleValue());
      loc.setYaw(yaw.floatValue());
      loc.setPitch(pitch.floatValue());
      }
      }
      catch (Exception e) {
      System.out.println(e);
      }

      return loc;
      }

      public static ChatColor getStandardColor() {
      String path = "plugins/OtherX/data.txt";
      File file = new File(path);

      ChatColor chatcolor = ChatColor.WHITE;

      if (file.exists()) {
      try {
      BufferedReader br = new BufferedReader(new FileReader(file));

      String s = br.readLine();

      while (!s.split("=")[0].equals("standard-color")) {
      s = br.readLine();
      }

      if (s.split("=")[0].equals("standard-color")) {
      s = s.split("=")[1];

      if (s.equals("AQUA"))
      chatcolor = ChatColor.AQUA;
      else if (s.equals("BLACK"))
      chatcolor = ChatColor.BLACK;
      else if (s.equals("BLUE"))
      chatcolor = ChatColor.BLUE;
      else if (s.equals("DARK_AQUA"))
      chatcolor = ChatColor.DARK_AQUA;
      else if (s.equals("DARK_BLUE"))
      chatcolor = ChatColor.DARK_BLUE;
      else if (s.equals("DARK_GRAY"))
      chatcolor = ChatColor.DARK_GRAY;
      else if (s.equals("DARK_GREEN"))
      chatcolor = ChatColor.DARK_GREEN;
      else if (s.equals("DARK_PURPLE"))
      chatcolor = ChatColor.DARK_PURPLE;
      else if (s.equals("DARK_RED"))
      chatcolor = ChatColor.DARK_RED;
      else if (s.equals("GOLD"))
      chatcolor = ChatColor.GOLD;
      else if (s.equals("GRAY"))
      chatcolor = ChatColor.GRAY;
      else if (s.equals("GREEN"))
      chatcolor = ChatColor.GREEN;
      else if (s.equals("LIGHT_PURPLE"))
      chatcolor = ChatColor.LIGHT_PURPLE;
      else if (s.equals("RED"))
      chatcolor = ChatColor.RED;
      else if (s.equals("WHITE"))
      chatcolor = ChatColor.WHITE;
      else if (s.equals("YELLOW")) {
      chatcolor = ChatColor.YELLOW;
      }
      }

      br.close();
      } catch (Exception e) {
      e.printStackTrace();
      }
      }

      return chatcolor;
      }

      public static ChatColor getSpecialColor() {
      String path = "plugins/OtherX/data.txt";
      File file = new File(path);

      ChatColor chatcolor = ChatColor.RED;

      if (file.exists()) {
      try {
      BufferedReader br = new BufferedReader(new FileReader(file));

      String s = br.readLine();

      while (!s.split("=")[0].equals("special-color")) {
      s = br.readLine();
      }

      if (s.split("=")[0].equals("special-color")) {
      s = s.split("=")[1];

      if (s.equals("AQUA"))
      chatcolor = ChatColor.AQUA;
      else if (s.equals("BLACK"))
      chatcolor = ChatColor.BLACK;
      else if (s.equals("BLUE"))
      chatcolor = ChatColor.BLUE;
      else if (s.equals("DARK_AQUA"))
      chatcolor = ChatColor.DARK_AQUA;
      else if (s.equals("DARK_BLUE"))
      chatcolor = ChatColor.DARK_BLUE;
      else if (s.equals("DARK_GRAY"))
      chatcolor = ChatColor.DARK_GRAY;
      else if (s.equals("DARK_GREEN"))
      chatcolor = ChatColor.DARK_GREEN;
      else if (s.equals("DARK_PURPLE"))
      chatcolor = ChatColor.DARK_PURPLE;
      else if (s.equals("DARK_RED"))
      chatcolor = ChatColor.DARK_RED;
      else if (s.equals("GOLD"))
      chatcolor = ChatColor.GOLD;
      else if (s.equals("GRAY"))
      chatcolor = ChatColor.GRAY;
      else if (s.equals("GREEN"))
      chatcolor = ChatColor.GREEN;
      else if (s.equals("LIGHT_PURPLE"))
      chatcolor = ChatColor.LIGHT_PURPLE;
      else if (s.equals("RED"))
      chatcolor = ChatColor.RED;
      else if (s.equals("WHITE"))
      chatcolor = ChatColor.WHITE;
      else if (s.equals("YELLOW")) {
      chatcolor = ChatColor.YELLOW;
      }
      }

      br.close();
      } catch (Exception e) {
      e.printStackTrace();
      }
      }

      return chatcolor;
      }
      }
     
  2. Offline

    CaiusTSM

    I ran your plugin on my server, and it worked fine for me, are you sure its not just your server?
    Did you have any other programs running on it?
    Any other plugins?

    I typed /setportal and it set a portal.
    Then I typed /portal and it teleported me to that location.
    Btw nice plugin. :)
     
  3. Offline

    nubpro

    I ran many plugin for my server.
    When I typed /portal, everything just freeze included my running CMD.
    Could you upload me the plugin?
    Thank you.

    After I tested my plugin, I found the bug.
    If you type /setportal once, it will saved once in the portal.txt file.
    The second time, you type /setportal, everything will freeze and unbale to check portal.txt file because the file had increased it own's size.
    It duplicate so many times into the txt file
    I hope any devs' can help me !

    Okay, Forum moderator please close this. Thanks :D

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

    CaiusTSM

    If you wanted a code fix, here it is.
    Code:
    package net.nubpro.project.plugin.TPportals;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.util.Vector;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    
    public class Data
    {
    public static void setHome(Location loc, String world)
    {
    File f = new File("plugins/TpPortal/" + world);
    try
    {
    if (!f.exists()) {
    f.mkdir();
    }
    
    File portalFile = new File("plugins/TpPortal/" + world + "/portal.txt"); //changed f to portalFile
    
    if (!portalFile.exists()) {
    portalFile.createNewFile();
    }
    BufferedReader br = new BufferedReader(new FileReader(portalFile));
    
    Vector<String> save = new Vector<String>(0); //made this a vector of size 0, this saves memory
    String s = "";
    while ((s = br.readLine()) != null) {
        save.setSize(save.size()+1);    //changed
        save.add(s); //changed
    }
    
    br.close();
    BufferedWriter bw = new BufferedWriter(new FileWriter(portalFile));
    
    String x = " " + String.valueOf(loc.getX());
    String y = " " + String.valueOf(loc.getY());
    String z = " " + String.valueOf(loc.getZ());
    String yaw = " " + String.valueOf(loc.getYaw());
    String pitch = " " + String.valueOf(loc.getPitch());
    
    bw.write((x + y + z + yaw + pitch));
    bw.newLine();
    
    int size = save.size(); //changed
    for(int i = 0; i < size; i++) //changed to for loop, there is no reason to have a while loop
    {
        if(save.get(i) != null) //checks to see if save[i] is null
        {
            bw.write(save.get(i));
            bw.newLine();
        }
    }
    
    bw.flush();
    bw.close();
    } catch (Exception e) {
    System.out.println(e);
    }
    }
    
    public static boolean homeSet(String world) {
    File f = new File("plugins/TpPortal/" + world + "/portal.txt");
    
    if (f.exists())
    try {
    BufferedReader br = new BufferedReader(new FileReader(f));
    while ((br.readLine()) != null) {
    {
    return true;
    }
    }
    
    return false;
    } catch (Exception e) {
    System.out.println(e);
    }
    else {
    return false;
    }
    
    return false;
    }
    
    public static Location getHome(Location loc , String world) {
    File f = new File("plugins/TpPortal/" + world + "/portal.txt");
    try
    {
    BufferedReader br = new BufferedReader(new FileReader(f));
    String s = "";
    
    while ((s = br.readLine()) != null)
    if (s.split(" ")[0] != null) {
    Double x = Double.valueOf(s.split(" ")[1]);
    Double y = Double.valueOf(s.split(" ")[2]);
    Double z = Double.valueOf(s.split(" ")[3]);
    Float yaw = Float.valueOf(s.split(" ")[4]);
    Float pitch = Float.valueOf(s.split(" ")[5]);
    
    loc.setX(x.doubleValue());
    loc.setY(y.doubleValue());
    loc.setZ(z.doubleValue());
    loc.setYaw(yaw.floatValue());
    loc.setPitch(pitch.floatValue());
    }
    }
    catch (Exception e) {
    System.out.println(e);
    }
    
    return loc;
    }
    
    public static ChatColor getStandardColor() {
    String path = "plugins/OtherX/data.txt";
    File file = new File(path);
    
    ChatColor chatcolor = ChatColor.WHITE;
    
    if (file.exists()) {
    try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    
    String s = br.readLine();
    
    while (!s.split("=")[0].equals("standard-color")) {
    s = br.readLine();
    }
    
    if (s.split("=")[0].equals("standard-color")) {
    s = s.split("=")[1];
    
    if (s.equals("AQUA"))
    chatcolor = ChatColor.AQUA;
    else if (s.equals("BLACK"))
    chatcolor = ChatColor.BLACK;
    else if (s.equals("BLUE"))
    chatcolor = ChatColor.BLUE;
    else if (s.equals("DARK_AQUA"))
    chatcolor = ChatColor.DARK_AQUA;
    else if (s.equals("DARK_BLUE"))
    chatcolor = ChatColor.DARK_BLUE;
    else if (s.equals("DARK_GRAY"))
    chatcolor = ChatColor.DARK_GRAY;
    else if (s.equals("DARK_GREEN"))
    chatcolor = ChatColor.DARK_GREEN;
    else if (s.equals("DARK_PURPLE"))
    chatcolor = ChatColor.DARK_PURPLE;
    else if (s.equals("DARK_RED"))
    chatcolor = ChatColor.DARK_RED;
    else if (s.equals("GOLD"))
    chatcolor = ChatColor.GOLD;
    else if (s.equals("GRAY"))
    chatcolor = ChatColor.GRAY;
    else if (s.equals("GREEN"))
    chatcolor = ChatColor.GREEN;
    else if (s.equals("LIGHT_PURPLE"))
    chatcolor = ChatColor.LIGHT_PURPLE;
    else if (s.equals("RED"))
    chatcolor = ChatColor.RED;
    else if (s.equals("WHITE"))
    chatcolor = ChatColor.WHITE;
    else if (s.equals("YELLOW")) {
    chatcolor = ChatColor.YELLOW;
    }
    }
    
    br.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    
    return chatcolor;
    }
    
    public static ChatColor getSpecialColor() {
    String path = "plugins/OtherX/data.txt";
    File file = new File(path);
    
    ChatColor chatcolor = ChatColor.RED;
    
    if (file.exists()) {
    try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    
    String s = br.readLine();
    
    while (!s.split("=")[0].equals("special-color")) {
    s = br.readLine();
    }
    
    if (s.split("=")[0].equals("special-color")) {
    s = s.split("=")[1];
    
    if (s.equals("AQUA"))
    chatcolor = ChatColor.AQUA;
    else if (s.equals("BLACK"))
    chatcolor = ChatColor.BLACK;
    else if (s.equals("BLUE"))
    chatcolor = ChatColor.BLUE;
    else if (s.equals("DARK_AQUA"))
    chatcolor = ChatColor.DARK_AQUA;
    else if (s.equals("DARK_BLUE"))
    chatcolor = ChatColor.DARK_BLUE;
    else if (s.equals("DARK_GRAY"))
    chatcolor = ChatColor.DARK_GRAY;
    else if (s.equals("DARK_GREEN"))
    chatcolor = ChatColor.DARK_GREEN;
    else if (s.equals("DARK_PURPLE"))
    chatcolor = ChatColor.DARK_PURPLE;
    else if (s.equals("DARK_RED"))
    chatcolor = ChatColor.DARK_RED;
    else if (s.equals("GOLD"))
    chatcolor = ChatColor.GOLD;
    else if (s.equals("GRAY"))
    chatcolor = ChatColor.GRAY;
    else if (s.equals("GREEN"))
    chatcolor = ChatColor.GREEN;
    else if (s.equals("LIGHT_PURPLE"))
    chatcolor = ChatColor.LIGHT_PURPLE;
    else if (s.equals("RED"))
    chatcolor = ChatColor.RED;
    else if (s.equals("WHITE"))
    chatcolor = ChatColor.WHITE;
    else if (s.equals("YELLOW")) {
    chatcolor = ChatColor.YELLOW;
    }
    }
    
    br.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    
    return chatcolor;
    }
    }
    
    I hope this helps, with this I was able to use /setportal multiple times without bugs or errors.
     
  5. Offline

    nubpro

    Thanks !
    I already fix this and I uploaded it in bukkit forum.
    http://forums.bukkit.org/threads/tp-tpportal-v0-2-teleport-to-your-portals-location-818.20597/
     
  6. Offline

    nubpro

    When I typed /portal, /setportal and /portal help, it shows me the usage in the plugin.yml.
    Is there any problem in the Class?
    I think the main problem is in the TpPortal.class because I added the /portal help list.
    I wonder what's the problem of it, please help!
    :'(
    @CaiusTSM
    Main TpPortal.class File
    Show Spoiler

    Code:
    package net.nubpro.project.plugin.TpPortal;
    
    import java.io.File;
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class TpPortal extends JavaPlugin
    {
     private static final Logger log = Logger.getLogger("Minecraft");
    
          DataListener data = new DataListener();
          Location loc;
    
          public void onDisable()
          {
            log.info("TpPortal v0.3 disabled");
          }
    
          public void onEnable()
          {
            log.info("TpPortal v0.3 enabled");
    
            if (!new File("plugins/TpPortal").exists()) {
              log.info("TpPortal directory creating..");
              new File("plugins/TpPortal").mkdir();
              log.info("TpPortal directory created.");
            }
          }
    
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args) {
            if ((sender instanceof Player))return false; {
              Player player = (Player)sender;
              String world = player.getWorld().getName();
    
              if (CommandLabel.equalsIgnoreCase("portal"))
              {
                if ((args.length < 1) || (args[0].equalsIgnoreCase("help"))) {
                  player.sendMessage(ChatColor.GREEN + "Test");
                }
    
              if (cmd.getName().equalsIgnoreCase("portal")) {
                if (DataListener.homeSet( world, world)) {
                  this.loc = player.getLocation();
                  this.loc = DataListener.getHome(this.loc, world, world);
    
                  player.teleportTo(this.loc);
    
                  player.sendMessage(ChatColor.AQUA + "Destination Arrived (Portal)");
                } else {
                  player.sendMessage(ChatColor.RED + "Set a portal location first.");
                }
              }
     
              if (cmd.getName().equalsIgnoreCase("setportal")) {
                  if (!player.isOp()) {
                        player.sendMessage(ChatColor.RED + "Can be only performed by OP");
                        return true;}
                  this.loc = player.getLocation();
    
                DataListener.setHome(this.loc, world, world);
    
                player.sendMessage(ChatColor.DARK_AQUA + "Portal had been set at current your position.");
              }
    
              }
            return true;
            }
        }
    }
          

    Show Spoiler

    DataListener
    Code:
    package net.nubpro.project.plugin.TpPortal;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import org.bukkit.Location;
    
    
    public class DataListener
    {
      public static void setHome(Location loc, String name, String world)
      {
        File f = new File("plugins/TpPortal/" + world);
        try
        {
          if (!f.exists()) {
            f.mkdir();
          }
    
          f = new File("plugins/TpPortal/" + world + "/portal.txt");
    
          if (!f.exists()) {
            f.createNewFile();
          }
    
          BufferedReader br = new BufferedReader(new FileReader(f));
    
          String[] save = new String[1000];
          String s = "";
          int j = 0;
    
          while ((s = br.readLine()) != null) {
            if (!s.split(" ")[0].equalsIgnoreCase(name)) {
              save[j] = s;
              j++;
            }
          }
    
          br.close();
    
          BufferedWriter bw = new BufferedWriter(new FileWriter(f));
    
          String x = " " + String.valueOf(loc.getX());
          String y = " " + String.valueOf(loc.getY());
          String z = " " + String.valueOf(loc.getZ());
          String yaw = " " + String.valueOf(loc.getYaw());
          String pitch = " " + String.valueOf(loc.getPitch());
    
          bw.write(name + x + y + z + yaw + pitch);
          bw.newLine();
    
          j = 0;
    
          while (save[j] != null) {
            bw.write(save[j]);
            bw.newLine();
          }
    
          bw.flush();
          bw.close();
        } catch (Exception e) {
          System.out.println(e);
        }
      }
      
    
    public static boolean homeSet(String name, String world) {
        File f = new File("plugins/TpPortal/" + world + "/portal.txt");
    
        if (f.exists())
          try {
            BufferedReader br = new BufferedReader(new FileReader(f));
            String s = "";
    
            while ((s = br.readLine()) != null) {
              if (s.split(" ")[0].equalsIgnoreCase(name)) {
                return true;
              }
            }
    
            return false;
          } catch (Exception e) {
            System.out.println(e);
          }
        else {
          return false;
        }
    
        return false;
      }
    
      public static Location getHome(Location loc, String name, String world) {
        File f = new File("plugins/TpPortal/" + world + "/portal.txt");
        try
        {
          BufferedReader br = new BufferedReader(new FileReader(f));
          String s = "";
    
          while ((s = br.readLine()) != null)
            if (s.split(" ")[0].equalsIgnoreCase(name)) {
              Double x = Double.valueOf(s.split(" ")[1]);
              Double y = Double.valueOf(s.split(" ")[2]);
              Double z = Double.valueOf(s.split(" ")[3]);
              Float yaw = Float.valueOf(s.split(" ")[4]);
              Float pitch = Float.valueOf(s.split(" ")[5]);
    
              loc.setX(x.doubleValue());
              loc.setY(y.doubleValue());
              loc.setZ(z.doubleValue());
              loc.setYaw(yaw.floatValue());
              loc.setPitch(pitch.floatValue());
            }
        }
        catch (Exception e) {
          System.out.println(e);
        }
    
        return loc;
      }
    }
    
     
  7. Offline

    alta189

    You need to return true
     
  8. Offline

    nubpro

    I think I may get it work myself >< :D

    bumb

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

    alta189

    I cant read your code, because of formatting stuff like:
    Code:
    [COLOR=rgb(255, 0, 0)] 
    You can not change the color of things in a code tag. Please repost it with the full source for that class. Also, don't bump your thread.
     
  10. Offline

    nubpro

    @alta189
    Don't mean to bumb my thread xD
    TpPortal.java < Main
    Show Spoiler

    Code:
    package net.nubpro.project.plugin.TpPortal;
    
    import java.io.File;
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class TpPortal extends JavaPlugin
    {
     private static final Logger log = Logger.getLogger("Minecraft");
    
          DataListener data = new DataListener();
          Location loc;
    
          public void onDisable()
          {
            log.info("TpPortal v0.3 disabled");
          }
    
          public void onEnable()
          {
            log.info("TpPortal v0.3 enabled");
    
            if (!new File("plugins/TpPortal").exists()) {
              log.info("TpPortal directory creating..");
              new File("plugins/TpPortal").mkdir();
              log.info("TpPortal directory created.");
            }
          }
    
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args) {
            if ((sender instanceof Player)); {
              Player player = (Player)sender;
              String world = player.getWorld().getName();
              
              if (CommandLabel.equalsIgnoreCase("portal"))
              {
                if ((args.length < 1) || (args[0].equalsIgnoreCase("help"))) {
                  player.sendMessage(ChatColor.GREEN + "Test");
                  return true;
                }
             
              if (cmd.getName().equalsIgnoreCase("portal")) {
                if (DataListener.homeSet( world, world)) {
                  this.loc = player.getLocation();
                  this.loc = DataListener.getHome(this.loc, world, world);
    
                  player.teleportTo(this.loc);
    
                  player.sendMessage(ChatColor.AQUA + "Destination Arrived (Portal)");
                } else {
                  player.sendMessage(ChatColor.RED + "Set a portal location first.");
                }
              }
            
                
              if (cmd.getName().equalsIgnoreCase("setportal")) {
                  if (!player.isOp()) {
                        player.sendMessage(ChatColor.RED + "Can be only performed by OP");
                        return true;}
                  this.loc = player.getLocation();
    
                DataListener.setHome(this.loc, world, world);
    
                player.sendMessage(ChatColor.DARK_AQUA + "Portal had been set at current your position.");
              }
              
              }
            return true;
            }
        }
    }
            
        
            
    
    
    

    Data Listener
    Show Spoiler

    Code:
    package net.nubpro.project.plugin.TpPortal;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import org.bukkit.Location;
    
    
    public class DataListener
    {
      public static void setHome(Location loc, String name, String world)
      {
        File f = new File("plugins/TpPortal/" + world);
        try
        {
          if (!f.exists()) {
            f.mkdir();
          }
    
          f = new File("plugins/TpPortal/" + world + "/portal.txt");
    
          if (!f.exists()) {
            f.createNewFile();
          }
    
          BufferedReader br = new BufferedReader(new FileReader(f));
    
          String[] save = new String[1000];
          String s = "";
          int j = 0;
    
          while ((s = br.readLine()) != null) {
            if (!s.split(" ")[0].equalsIgnoreCase(name)) {
              save[j] = s;
              j++;
            }
          }
    
          br.close();
    
          BufferedWriter bw = new BufferedWriter(new FileWriter(f));
    
          String x = " " + String.valueOf(loc.getX());
          String y = " " + String.valueOf(loc.getY());
          String z = " " + String.valueOf(loc.getZ());
          String yaw = " " + String.valueOf(loc.getYaw());
          String pitch = " " + String.valueOf(loc.getPitch());
    
          bw.write(name + x + y + z + yaw + pitch);
          bw.newLine();
    
          j = 0;
    
          while (save[j] != null) {
            bw.write(save[j]);
            bw.newLine();
          }
    
          bw.flush();
          bw.close();
        } catch (Exception e) {
          System.out.println(e);
        }
      }
      
    
    public static boolean homeSet(String name, String world) {
        File f = new File("plugins/TpPortal/" + world + "/portal.txt");
    
        if (f.exists())
          try {
            BufferedReader br = new BufferedReader(new FileReader(f));
            String s = "";
    
            while ((s = br.readLine()) != null) {
              if (s.split(" ")[0].equalsIgnoreCase(name)) {
                return true;
              }
            }
    
            return false;
          } catch (Exception e) {
            System.out.println(e);
          }
        else {
          return false;
        }
    
        return false;
      }
    
      public static Location getHome(Location loc, String name, String world) {
        File f = new File("plugins/TpPortal/" + world + "/portal.txt");
        try
        {
          BufferedReader br = new BufferedReader(new FileReader(f));
          String s = "";
    
          while ((s = br.readLine()) != null)
            if (s.split(" ")[0].equalsIgnoreCase(name)) {
              Double x = Double.valueOf(s.split(" ")[1]);
              Double y = Double.valueOf(s.split(" ")[2]);
              Double z = Double.valueOf(s.split(" ")[3]);
              Float yaw = Float.valueOf(s.split(" ")[4]);
              Float pitch = Float.valueOf(s.split(" ")[5]);
    
              loc.setX(x.doubleValue());
              loc.setY(y.doubleValue());
              loc.setZ(z.doubleValue());
              loc.setYaw(yaw.floatValue());
              loc.setPitch(pitch.floatValue());
            }
        }
        catch (Exception e) {
          System.out.println(e);
        }
    
        return loc;
      }
    }
    
     
  11. Offline

    CaiusTSM

    nubpro, you only return true in the onCommand function when a player has said a command, if not then return false:
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args) {
            if ((sender instanceof Player)){
              Player player = (Player)sender;
              String world = player.getWorld().getName();
    
              if (cmd.getName().equalsIgnoreCase("portal"))
              {
                if ((args.length < 1) || (args[0].equalsIgnoreCase("help"))) {
                  player.sendMessage(ChatColor.GREEN + "-portal help-");
                }
                if (DataListener.homeSet(world, world)) { //strange function I do not understand why you put world twice.
                  this.loc = player.getLocation();
                  this.loc = DataListener.getHome(this.loc, world, world);
                  player.teleportTo(this.loc);
                  player.sendMessage(ChatColor.AQUA + "Destination Arrived (Portal)");
                } else {
                  player.sendMessage(ChatColor.RED + "Set a portal location first.");
                }
                return true; //return true when a command was said
                }
    
              if (cmd.getName().equalsIgnoreCase("setportal")) {
                  if (!player.isOp()) {
                        player.sendMessage(ChatColor.RED + "Can be only performed by OP");
                    } else {
                    this.loc = player.getLocation();
                    DataListener.setHome(this.loc, world, world); //strange function why do you put world twice?
                    player.sendMessage(ChatColor.DARK_AQUA + "Portal had been set at current your position.");
              }
              return true; //return true when a command was said
              }
            }
            return false; //the function returns false when no command was said
            }
    btw, your code is messy, I had trouble trying to fix it, and whats up with the setHome() function taking the exact same string twice?.
     
  12. Offline

    nubpro

    I am still learning xD
    Should be fix in this weekend :rolleyes:

    I think I can get it work myself :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 16, 2016
Thread Status:
Not open for further replies.

Share This Page