Can't get onPlayerJoin working

Discussion in 'Plugin Development' started by bittiez, May 14, 2012.

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

    bittiez

    I've read many other topics and "fixed" my code to what they did, however I still can't get it working..
    Does anyone see anything wrong with this code? (The rest of the plugin works fine, its just the onplayerjoin that doesn't do anything)
    Code:java
    1. package com.cubiceffect.bittiez.rentslots;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.EventPriority;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerJoinEvent;
    12. import org.bukkit.plugin.RegisteredServiceProvider;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. import org.bukkit.ChatColor;
    15. import java.io.BufferedReader;
    16. import java.io.InputStreamReader;
    17. import java.io.OutputStreamWriter;
    18. import java.net.URL;
    19. import java.net.URLConnection;
    20. import java.net.URLEncoder;
    21. import net.milkbowl.vault.economy.Economy;
    22.  
    23. public class rentslots extends JavaPlugin implements Listener {
    24. Logger log;
    25. public static Economy economy = null;
    26. public void onEnable(){
    27. log = this.getLogger();
    28. getServer().getPluginManager().registerEvents(this, this);
    29. if (!setupEconomy() ) {
    30. log.info(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    31. getServer().getPluginManager().disablePlugin(this);
    32. return;
    33. }
    34. }
    35.  
    36. public void onDisable(){
    37. log.info("Rent slots disabled.");
    38. }
    39.  
    40. private boolean setupEconomy()
    41. {
    42. RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    43. if (economyProvider != null) {
    44. economy = economyProvider.getProvider();
    45. }
    46.  
    47. return (economy != null);
    48. }
    49. @EventHandler(priority = EventPriority.NORMAL)
    50. public void onPlayerJoin(PlayerJoinEvent event) {
    51. Player player = event.getPlayer();
    52. if(player.hasPermission("rentslots.notifyupdate")){
    53. String updates = senddata("upd", "1", "", ""); //Need to check updates?
    54. if (updates == "true") {
    55. player.sendMessage(ChatColor.RED + "[RENT SLOTS] Rent has come commands that need to be ran!");
    56. player.sendMessage(ChatColor.RED + "[RENT SLOTS] [url]http://cubiceffect.com/shops[/url]");
    57. player.sendMessage(ChatColor.RED + "[RENT SLOTS] Please click that link and follow the directions!");
    58. }
    59. }
    60.  
    61. }
    62.  
    63.  
    64. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    65. if (sender instanceof Player) {
    66. } else {return false;}
    67. if (cmd.getName().equalsIgnoreCase("rent")){ // If the player typed /basic then do the following...
    68. if(!sender.hasPermission("rentslots.rent")) {sender.sendMessage(ChatColor.RED + "You do not have the required permissions"); return true;}
    69. if (args.length > 0) {} else {
    70. sender.sendMessage(ChatColor.RED + "Avilable commands are:");
    71. sender.sendMessage(ChatColor.GOLD + "/rent list <-- A list of all available slots");
    72. sender.sendMessage(ChatColor.RED + "/rent buy <loc#> <-- Buy a slot(Get the loc# from /rent list");
    73. sender.sendMessage(ChatColor.GOLD + "/rent price <-- Get the current price of rentals");
    74. sender.sendMessage(ChatColor.RED + "/rent owned <-- See all currently owned shops");
    75. sender.sendMessage(ChatColor.GOLD + "/rent pay <loc#> <-- Pay for the next rental period of <loc#>(Get the loc# of your owned shops with /rent owned)");
    76. return true;
    77. }
    78. String cprice = senddata("gprice", "1", "", ""); //Current price
    79. double bala = economy.getBalance(sender.getName());
    80. int aInt = Integer.parseInt(cprice);
    81.  
    82. if(args[0].equalsIgnoreCase("list")) {
    83. sender.sendMessage(ChatColor.GOLD + senddata(args[0], "checklist", "", ""));
    84. return true;
    85. }
    86.  
    87. if(args[0].equalsIgnoreCase("buy") && args.length > 1) {
    88. if(bala > aInt){
    89. String[] gCom = senddata(args[0], sender.getName(), "loc", args[1]).split("////");
    90. if(gCom[0] == "false") {sender.sendMessage(ChatColor.RED + "An error occured"); return true;}
    91. economy.withdrawPlayer(sender.getName(), aInt);
    92. String[] dtn = gCom[1].split("<br>");
    93. for (String s : dtn) {
    94. sender.sendMessage(ChatColor.GOLD + s);
    95. }
    96. return true;
    97.  
    98. } else {sender.sendMessage(ChatColor.RED + "You do not have enough funds!"); return true;}
    99. }
    100.  
    101. if(args[0].equalsIgnoreCase("price")) {
    102. sender.sendMessage(ChatColor.GOLD + senddata(args[0], "1", "", ""));
    103. return true;
    104. }
    105.  
    106. if(args[0].equalsIgnoreCase("owned")) {
    107. String[] dtn = senddata(args[0], sender.getName(), "", "").split("<br>");
    108. for (String s : dtn) {
    109. sender.sendMessage(ChatColor.GOLD + s);
    110. }
    111. return true;
    112. }
    113.  
    114. if(args[0].equalsIgnoreCase("pay") && args.length > 1) {
    115. if(bala > aInt){
    116. String[] dtn1 = senddata(args[0], args[1], "", "").split("////");
    117.  
    118.  
    119. if(dtn1[0] == "false") {sender.sendMessage(ChatColor.RED + "An error occured"); return true;}
    120. String[] dtn = dtn1[1].split("<br>");
    121.  
    122.  
    123. economy.withdrawPlayer(sender.getName(), aInt);
    124.  
    125. for (String s : dtn) {
    126. sender.sendMessage(ChatColor.GOLD + s);
    127. }
    128. } else {sender.sendMessage(ChatColor.RED + "You do not have enough funds!"); return true;}
    129. return true;
    130. }
    131.  
    132. sender.sendMessage(ChatColor.RED + "Avilable commands are:");
    133. sender.sendMessage(ChatColor.GOLD + "/rent list <-- A list of all available slots");
    134. sender.sendMessage(ChatColor.RED + "/rent buy <loc#> <-- Buy a slot(Get the loc# from /rent list)");
    135. sender.sendMessage(ChatColor.GOLD + "/rent price <-- Get the current price of rentals");
    136. sender.sendMessage(ChatColor.RED + "/rent owned <-- See all currently owned shops");
    137. sender.sendMessage(ChatColor.GOLD + "/rent pay <loc#> <-- Pay for the next rental period of <loc#>(Get the loc# of your owned shops with /rent owned)");
    138. return true;
    139.  
    140.  
    141. }
    142.  
    143.  
    144. return false;
    145. }
    146.  
    147.  
    148.  
    149.  
    150. public String senddata( String param1, String param2, String p3, String p4 )
    151. {
    152. String dta = "";
    153.  
    154.  
    155. try {
    156.  
    157. // Construct data
    158. String data = URLEncoder.encode(param1, "UTF-8") + "=" + URLEncoder.encode(param2, "UTF-8");
    159. data += "&" + URLEncoder.encode(p3, "UTF-8") + "=" + URLEncoder.encode(p4, "UTF-8");
    160.  
    161. // Send data
    162. URL url = new URL("#####Hidden:)#####");
    163. URLConnection conn = url.openConnection();
    164. conn.setDoOutput(true);
    165. OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    166. wr.write(data);
    167. wr.flush();
    168.  
    169. // Get the response
    170. BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    171. String line;
    172. while ((line = rd.readLine()) != null) {
    173. dta+= line;
    174. //System.out.println(line);
    175. }
    176. wr.close();
    177. rd.close();
    178. } catch (Exception e) {
    179. }
    180.  
    181.  
    182.  
    183. return dta;
    184. }
    185. }
    186.  
     
  2. Offline

    Flash619

    Code:java
    1.  
    2. if(player.hasPermission("rentslots.notifyupdate")){
    3.  


    Could it have to do with the player not having the permission? Possibly try to
    comment it out or add a few log statements to see how far it gets before it stops
    working?
     
  3. Offline

    bittiez

    I added a player.sendMessage right before the permission check, it didn't send any message =/
     
  4. Offline

    Flash619

    Could you instead of using the [ code] brackets in your main post, switch it to
    [syntax = java]
    code here
    [/syntax]
    without spaces so I can read it a bit easier?

    Do you have the listener initialized properly? I usually use separate classes for my listeners, but I tend to be very object orientated.

    I usually do it like this tho:
    Code:java
    1.  
    2. SpawnEggThrow EggListener = new SpawnEggThrow(this); //First I tell it that the class SpawnEggThrow will be known as EggListener
    3.  
    4. //Then I punch this out
    5. Bukkit.getServer().getPluginManager().registerEvents(EggListener, this);
    6. //Mind you this is all within the onEnable() method.
    7.  
     
  5. Offline

    bittiez

    Okay, never mind it's not the onplayerjoin anymore, its something to do with
    String updates = senddata("upd", "1", "", ""); //Need to check updates?
    if (updates == "true") { <--- is not coming out true for some reason


    Ill figure it out, thanks guys!

    Hmm, okay so I have tested over and over again, double and triple checked, the string updates is indeed returning "true" any ideas why it seems to think otherwise?

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

    LucasEmanuel

    Is it returning a string true or a boolean true?
     
  7. Offline

    dsmyth1915

    What is the "String updates = senddata " part supposed to do?

    Also, its a void, it can't return a boolean true unless you state that a variable is a boolean.
     
  8. Offline

    bittiez

    It returns a string true, the senddata part posts data to a url and returns whatever the url returned(in this case, "true")
     
  9. Offline

    dsmyth1915

    try taking that out entirely and see if it works? if it doesn't, then that's not your actuall problem.

    //String ~yadayada~

    Just noticed, your checking to see if update returns a String with the value of "true"
     
Thread Status:
Not open for further replies.

Share This Page