Solved Error with Developed Plugin

Discussion in 'Plugin Development' started by FlareLine, Oct 6, 2013.

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

    FlareLine

    Hi all.
    I'm running into a mess with my plugin.
    I'll give you all the resources first:
    IPCMain:
    Show Spoiler

    Code:java
    1. package dev.flareline.ipcheck;
    2.  
    3. import java.io.File;
    4. import java.io.FileOutputStream;
    5. import java.io.IOException;
    6. import java.io.InputStream;
    7. import java.io.OutputStream;
    8. import java.util.logging.Logger;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.Server;
    13. import org.bukkit.command.Command;
    14. import org.bukkit.command.CommandSender;
    15. import org.bukkit.configuration.file.FileConfiguration;
    16. import org.bukkit.configuration.file.YamlConfiguration;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.event.player.PlayerLoginEvent;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. public class IPCMain extends JavaPlugin {
    22.  
    23. public static final String ANSI_RESET = "\u001B[0m";
    24. public static final String ANSI_CYAN = "\u001B[36m";
    25. public static final String ANSI_GREEN = "\u001B[32m";
    26. public static final String ANSI_RED = "\u001B[31m";
    27.  
    28. static File configFile;
    29. static File loggerFile;
    30.  
    31. static FileConfiguration config;
    32. static FileConfiguration logger;
    33.  
    34. @Override
    35. public void onDisable() {
    36. saveYamls();
    37. }
    38.  
    39. @Override
    40. public void onEnable() {
    41.  
    42. getServer().getPluginManager().registerEvents(new IPCListener(), this);
    43.  
    44. configFile = new File(getDataFolder(), "config.yml");
    45. loggerFile = new File(getDataFolder(), "logger.yml");
    46.  
    47. try {
    48. firstRun();
    49. } catch (Exception e) {
    50. e.printStackTrace();
    51. }
    52. config = new YamlConfiguration();
    53. logger = new YamlConfiguration();
    54. loadYamls();
    55.  
    56. }
    57.  
    58. private void firstRun() throws Exception {
    59. if (!configFile.exists()) {
    60. configFile.getParentFile().mkdirs();
    61. copy(getResource("config.yml"), configFile);
    62. }
    63. if (!loggerFile.exists()) {
    64. loggerFile.getParentFile().mkdirs();
    65. copy(getResource("logger.yml"), loggerFile);
    66. }
    67. }
    68.  
    69. private void copy(InputStream in, File file) {
    70. try {
    71. OutputStream out = new FileOutputStream(file);
    72. byte[] buf = new byte[1024];
    73. int len;
    74. while ((len = in.read(buf)) > 0) {
    75. out.write(buf, 0, len);
    76. }
    77. out.close();
    78. in.close();
    79. } catch (Exception e) {
    80. e.printStackTrace();
    81. }
    82. }
    83.  
    84. public static void loadYamls() {
    85. try {
    86. config.load(configFile);
    87. logger.load(loggerFile);
    88. } catch (Exception e) {
    89. e.printStackTrace();
    90. }
    91. }
    92.  
    93. public static void saveYamls() {
    94. try {
    95. config.save(configFile);
    96. logger.save(loggerFile);
    97. } catch (IOException e) {
    98. e.printStackTrace();
    99. }
    100. }
    101.  
    102. public void writeLogs(Player player) {
    103. String playername = player.getName();
    104. logger.createSection(playername);
    105. saveYamls();
    106. }
    107.  
    108. public boolean onCommand(CommandSender sender, Command cmd, String label,
    109. String[] args) {
    110. if (sender instanceof Player) {
    111. if (cmd.getName().equalsIgnoreCase("ipc")) {
    112. sender.sendMessage(ChatColor.GOLD + "IPC");
    113. return true;
    114. }
    115. } else {
    116. sender.sendMessage(ChatColor.GOLD + "You must be ingame to use IPC");
    117. }
    118. return false;
    119. }
    120.  
    121. public static void checker(String playername, String address,
    122. PlayerLoginEvent event) {
    123. Server server = Bukkit.getServer();
    124. Logger console = server.getLogger();
    125. console.info(ANSI_CYAN + playername + "'s IP Address is: " + address
    126. + ANSI_RESET);
    127. loadYamls();
    128. String playeraddress = logger.getString(playername + ".join.address");
    129. if (playeraddress != null) {
    130. console.info(ANSI_GREEN + playername
    131. + "'s Address already exists: " + playeraddress
    132. + ANSI_RESET);
    133. saveYamls();
    134. } else {
    135. logger.set(playername + ".join.address", address);
    136. console.info(ANSI_RED + "Adding " + playername
    137. + "'s IP in Logger: " + address + ANSI_RESET);
    138. saveYamls();
    139. }
    140. boolean allowed = configurationChecker(playername, address);
    141. Player player = server.getPlayer(playername);
    142. IPCMain.denylogin(player, address, event, allowed, playername);
    143. }
    144.  
    145. public static boolean configurationChecker(String playername, String address) {
    146. loadYamls();
    147. String player = config.getString("exceptions.addresses." + address);
    148. if (player != null) {
    149. if (playername.equals(player)) {
    150. return true;
    151. } else {
    152. return false;
    153. }
    154. }
    155. return false;
    156. }
    157.  
    158. public static void denylogin(Player player, String address,
    159. PlayerLoginEvent event, boolean allowed, String playername) {
    160. Server server = Bukkit.getServer();
    161. Logger console = server.getLogger();
    162. String kickmessage = "Potential Account Theft. Denied Login.";
    163. console.info(ANSI_CYAN + "Testing Validity Of: " + playername
    164. + ANSI_RESET);
    165. String realaddress = logger.getString(playername + ".join.address");
    166. if (allowed = true) {
    167. console.info(ANSI_GREEN + "Address Matches Exception In config.yml"
    168. + ANSI_RESET);
    169. console.info(ANSI_CYAN + "Allowing " + playername + " To Join!"
    170. + ANSI_RESET);
    171. } else {
    172. if (address.equalsIgnoreCase(realaddress)) {
    173. console.info(ANSI_GREEN + "Address Matches!" + ANSI_RESET);
    174. console.info(ANSI_CYAN + "Allowing " + playername + " To Join!"
    175. + ANSI_RESET);
    176. } else {
    177. console.info(ANSI_RED + "Address Does Not Match!" + ANSI_RESET);
    178. console.info(ANSI_RED + "Player: " + playername + ANSI_RESET);
    179. console.info(ANSI_RED + "Address Used: " + address + ANSI_RESET);
    180. console.info(ANSI_RED + "Address In Logger: " + realaddress
    181. + ANSI_RESET);
    182. console.info(ANSI_RED + "Consider Looking Into This Issue!"
    183. + ANSI_RESET);
    184.  
    185. event.disallow(PlayerLoginEvent.Result.KICK_OTHER, kickmessage);
    186. }
    187. }
    188. }
    189.  
    190. }


    IPCListener:
    Show Spoiler

    Code:java
    1. package dev.flareline.ipcheck;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.EventPriority;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.PlayerLoginEvent;
    9.  
    10. public class IPCListener implements Listener {
    11.  
    12. IPCMain ipcheckinstance = new IPCMain();
    13.  
    14. @EventHandler(priority = EventPriority.HIGHEST)
    15. public void onLogin(PlayerLoginEvent event) {
    16.  
    17. Player player = event.getPlayer();
    18. String playername = player.getName();
    19. String address = event.getAddress().toString();
    20.  
    21. Bukkit.getLogger().info(
    22. IPCMain.ANSI_CYAN + playername + " Has Joined The Server!"
    23. + IPCMain.ANSI_RESET);
    24. Bukkit.getLogger().info(
    25. IPCMain.ANSI_CYAN + "Checking Information On: " + playername
    26. + IPCMain.ANSI_RESET);
    27.  
    28. IPCMain.checker(playername, address, event);
    29.  
    30. }
    31. }


    config.yml
    Show Spoiler

    Code:
    # IPC Configuration File
    # Use this file to define exceptions for players.
    # Define them using a YML String format.
    # Every Different IP Address Needs A Separate Entry.
    # Address Must Also Start With A Trailing FowardSlash.
    # eg.
    # exceptions:
    #  addresses:
    #    /999.999.999.999: FlareLine
    #    /123.456.789.012: ExamplePlayer
    exceptions:
      addresses:

    logger.yml
    Show Spoiler

    # IPC Logger File
    # This File Will Contain All Of The Logged Players Who
    # Have Joined The Server Since IPC Was Installed And
    # Any Relevant Information Collected About Them. This
    # Information To Be Collected Will Soon Be Configurable
    # In A Future Update.

    It works, fine for the first time, however when I login a second time, it disregards the changes in logger.yml and states that the player was excepted by the config.yml, even though there are no entries there. Cheers,
    FlareLine.
     
  2. Offline

    1Rogue

    For one, you're using a separate instance of the main class in your listener:

    Code:java
    1. public class IPCListener implements Listener {
    2.  
    3. IPCMain ipcheckinstance = new IPCMain();
    4.  
    5. }
    6.  
    7. // Should be...
    8.  
    9. public class IPCListener implements Listener {
    10.  
    11. private final IPCMain ipcheckinstance;
    12.  
    13. public IPCListener (IPCMain ipcheckinstance) {
    14. this.ipcheckinstance = ipcheckinstance;
    15. }
    16.  
    17. }
    18.  
    19. // And would be called by the main class as...
    20.  
    21. IPCListener listener = new IPCListener(this);
     
  3. Offline

    FlareLine

    1Rogue This wouldn't cause such an error though.
     
  4. Offline

    1Rogue


    It could if you're referencing different instances of the file. Have you fixed your listener yet?
     
  5. Offline

    FlareLine

    1Rogue Yes, however it is still giving me the same error. Mind you it was working before with the opposite instance, and this instantiated class:
    Code:java
    1. IPCMain ipcheckinstance = new IPCMain();

    is never used. Anywhere :p
    Give me a moment and I'll post the specific error.
     
  6. Offline

    1Rogue



    You're getting the same error from before, but it was working before....?

    Do you have a specific error?

    Also as a recommendation, I would say to use a BufferedWriter for the log file, it will take less CPU and you just need to make sure you would called writer.flush(); in onDisable();
     
  7. Offline

    FlareLine

    What I meant was that it was working before I implemented the config.yml

    On first join:
    Show Spoiler

    Code:
    2013-10-07 12:55:10 [INFO] [36mFlareLine Has Joined The Server![0m
    2013-10-07 12:55:10 [INFO] [36mChecking Information On: FlareLine[0m
    2013-10-07 12:55:10 [INFO] [36mFlareLine's IP Address is: /127.0.0.1[0m
    2013-10-07 12:55:10 [INFO] [32mFlareLine's Address already exists: /127.0.0.1[0m
    2013-10-07 12:55:10 [INFO] [36mTesting Validity Of: FlareLine[0m
    2013-10-07 12:55:10 [INFO] [32mAddress Matches Exception In config.yml[0m
    2013-10-07 12:55:10 [INFO] [36mAllowing FlareLine To Join![0m
    2013-10-07 12:55:10 [INFO] FlareLine[/127.0.0.1:52278] logged in with entity id 232 at ([Spawnworld] -4.889269125630935, 68.0, -49.86040489102292)

    config.yml
    Show Spoiler

    Code:
    # IPC Configuration File
    # Use this file to define exceptions for players.
    # Define them using a YML String format.
    # Every Different IP Address Needs A Separate Entry.
    # Address Must Also Start With A Trailing FowardSlash.
    # eg.
    # exceptions:
    #  addresses:
    #    /999.999.999.999: FlareLine
    #    /123.456.789.012: ExamplePlayer
    exceptions: {}
    

    logger.yml
    Show Spoiler

    Code:
    FlareLine:
      join:
        address: /127.0.0.1
    


    After changing the IP in logger.yml to /127.0.0.12 , The console gives me this

    Code:
    2013-10-07 12:59:10 [INFO] [36mFlareLine Has Joined The Server![0m
    2013-10-07 12:59:10 [INFO] [36mChecking Information On: FlareLine[0m
    2013-10-07 12:59:10 [INFO] [36mFlareLine's IP Address is: /127.0.0.1[0m
    2013-10-07 12:59:10 [INFO] [32mFlareLine's Address already exists: /127.0.0.13[0m  <--- Note the difference
    2013-10-07 12:59:10 [INFO] [36mTesting Validity Of: FlareLine[0m
    2013-10-07 12:59:10 [INFO] [32mAddress Matches Exception In config.yml[0m
    2013-10-07 12:59:10 [INFO] [36mAllowing FlareLine To Join![0m
    2013-10-07 12:59:10 [INFO] FlareLine[/127.0.0.1:52311] logged in with entity id 229 at ([Spawnworld] -4.889269125630935, 68.0, -49.86040489102292)
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  8. Offline

    1Rogue

    You have:

    Code:java
    1. if (allowed = true) {
    2. console.info(ANSI_GREEN + "Address Matches Exception In config.yml"
    3. + ANSI_RESET);
    4. console.info(ANSI_CYAN + "Allowing " + playername + " To Join!"
    5. + ANSI_RESET);
    6. }


    Which will always return true. Checks for values need to be == or just the boolean itself:
    Code:java
    1. if (allowed) {
    2. // ...
    3. }
    4. if (allowed == true) {
    5. // ...
    6. }


    That would be why this is triggering each time.

    Edit: This is under your denyLogin method.
     
  9. Offline

    FlareLine

    1Rogue Ah, so a single equals sign will compare 'allowed' to a boolean variable, and return true if allowed is a boolean?
     
  10. Offline

    1Rogue


    No, a single equals sign sets a value, and a double equals sign compares the values and returns a boolean.

    Code:java
    1. boolean test1 = true == true; // Returns true
    2. boolean test2 = true == false; // Returns false
    3. boolean test3 = true != false; // Returns true
    4. boolaen test4 = true != true; // Returns false
     
  11. Offline

    FlareLine

    1Rogue Oh derp. Thankyou for your help anyway kind sir - it's working correctly now :)
     
Thread Status:
Not open for further replies.

Share This Page