[Closed] Getting UUID of player from sign

Discussion in 'Plugin Development' started by gvlfm78, Apr 15, 2014.

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

    gvlfm78

    I am trying to get the UUID of a player from the player's name on a sign:

    Code:java
    1. Block block = event.getClickedBlock(); //Getting the block the player clicked
    2. org.bukkit.block.Sign sign = (org.bukkit.block.Sign) block.getState(); //Getting the sign state
    3. UUID PlayerOnSignVariable = Bukkit.getPlayer(sign.getLine(1)).getUniqueId();


    And the plugin stops there it gives me a NUllPointerException.

    The variable I am returning as an UUID variable in a method and using it as such.
     
  2. Offline

    Onlineids

    Make sure line 1 has something on it if(sign.getLine(1) != null){
     
  3. Offline

    St3venAU

    Are you sure that there is the name of a currently online player (and ONLY the name of a player) on the 2nd line of the sign? (Remember, that getLine(0) is the 1st line and getLine(1) is the 2nd line)
     
  4. Offline

    Onlineids

    ahh your right forgot that, gvlfm78 insert the null check and remember that line 1 is actually the second line
     
  5. Offline

    breezeyboy

    Would the full UUID fit on one line?
     
  6. Offline

    St3venAU

    Read the OP again. The player's name is on the sign, not their UUID.
     
  7. Offline

    breezeyboy

  8. Offline

    gvlfm78

    I know that line 1 is the second line, and by adding the null check I still get a nullpointer
     
  9. Offline

    Onlineids

    line number of error?
    and code on that line
     
  10. Offline

    gvlfm78

    Onlineids

    It's on this line:
    Code:java
    1. UUID PlayerOnSignVariable = Bukkit.getPlayer(sign.getLine(1)).getUniqueId();


    Giving me this error (Only pasted part of it):
    Code:
    Caused by: java.lang.NullPointerException
            at gvlfm78.plugin.InactiveLockette.InactiveLocketteListener.playerOnSign
    (InactiveLocketteListener.java:57) ~[?:?]
            at gvlfm78.plugin.InactiveLockette.InactiveLocketteListener.signHasPriva
    te(InactiveLocketteListener.java:243) ~[?:?]
            at gvlfm78.plugin.InactiveLockette.InactiveLocketteListener.onPlayerInte
    ract(InactiveLocketteListener.java:47) ~[?:?]
     
  11. Offline

    Onlineids

    add toString() to the end
    EDIT that wont work xD nvm change it to Player instead of UUID and add toString()
     
  12. Offline

    gvlfm78

    If I change it to player and then give it a String it won't work...
     
  13. Offline

    Onlineids

    yea it will its getting the player with that string make it getPlayerExact
     
  14. Offline

    gvlfm78

    I'm sorry, I don't understand what you mean by "make it getPlayerExact"
     
  15. Offline

    Onlineids

    nvm then
     
  16. Offline

    gvlfm78

    Fine, let me find somebody more helpful.

    [Bump]

    Can anybody else help me with this?
     
  17. Offline

    Onlineids

    I told you exactly what to do and you say what do you mean -_-
     
  18. Offline

    gvlfm78

    Make what getPlayerExact?
     
  19. Offline

    Onlineids

    Bukkit.getPlayerExact()
     
  20. Offline

    gvlfm78

    You mean doing this:
    Code:java
    1. Player PlayerOnSignVariable = Bukkit.getPlayerExact(sign.getLine(1)).getUniqueId().toString();


    Which then tells me I have to give it a Player instead of a String?
     
  21. Offline

    Onlineids

    use the method Bukkit.getPlayer(UUID id) and drop the toString
     
  22. Onlineids I have no idea what you're trying to suggest he does, so don't be too hard on him. :)

    gvlfm78 You need to make sure that the following things aren't null, because any of them could be: The sign variable, the String that getLine(1) returns, or the Player that getPlayer() or getPlayerExact() returns. But go back to what you had before the whole Player and toString() thing.

    Whether you should use getPlayer() or getPlayerExact() always depends. Use getPlayer() if you want the person who is 'close' to the name you're giving it (i.e. 'Adam' might return AdamQpzm) , and use getPlayerExact() if you want a player whose name is exactly what you're giving it (i.e. 'Adam' wouldn't return AdamQpzm, it would only return Adam or null).
     
  23. Offline

    gvlfm78

    That way we're going back to the start of this thread...

    And nothing at all happens when a playe punches a block...

    Now I have to go, coming back tomorrow

    I'll paste my whole class in the meantime

    Code:java
    1. package gvlfm78.plugin.InactiveLockette;
    2.  
    3. import org.bukkit.Bukkit;
    4.  
    5. import org.bukkit.ChatColor;
    6.  
    7. import org.bukkit.Location;
    8.  
    9. import org.bukkit.Material;
    10.  
    11. import org.bukkit.block.*;
    12.  
    13. import org.bukkit.material.Sign;
    14.  
    15. import org.bukkit.entity.Player;
    16.  
    17. import org.bukkit.event.EventHandler;
    18.  
    19. import org.bukkit.event.EventPriority;
    20.  
    21. import org.bukkit.event.Listener;
    22.  
    23. import org.bukkit.event.block.Action;
    24.  
    25. import org.bukkit.event.player.PlayerInteractEvent;
    26.  
    27. import org.bukkit.material.Attachable;
    28.  
    29. import java.util.UUID;
    30. import java.util.logging.Level;
    31.  
    32. public class InactiveLocketteListener implements Listener {
    33.  
    34. private InactiveLockette plugin;
    35. public InactiveLocketteListener(InactiveLockette plugin) {this.plugin = plugin;}
    36.  
    37.  
    38. @EventHandler(ignoreCancelled=true,priority = EventPriority.HIGH)
    39. public void onPlayerInteract(PlayerInteractEvent event) {
    40. if(event.getAction() == Action.LEFT_CLICK_BLOCK){
    41. Block block = event.getClickedBlock();
    42.  
    43. if ((block.getState() instanceof org.bukkit.block.Sign)&&(block != null)&&(block.getType() == Material.WALL_SIGN)){ //Player Left Clicked a wall sign
    44. org.bukkit.block.Sign sign = (org.bukkit.block.Sign) block.getState();
    45. if (sign.getLine(0).equalsIgnoreCase("[" + plugin.getConfig().getString("settingsChat.firstLine") + "]")){ //The first line of the sign contains [Private]
    46.  
    47. signHasPrivate(event);
    48. }
    49. }
    50. }
    51. }
    52.  
    53. private UUID playerOnSign(PlayerInteractEvent event){
    54. Block block = event.getClickedBlock(); //Getting the block the player clicked
    55. org.bukkit.block.Sign sign = (org.bukkit.block.Sign) block.getState(); //Getting the sign state
    56. if(sign.getLine(1) != null){
    57. UUID PlayerOnSignVariable = Bukkit.getPlayer(sign.getLine(1)).getUniqueId();
    58. return PlayerOnSignVariable;
    59. }else
    60. plugin.getLogger().severe("Could not get player UUID from sign");
    61. return null;
    62. }
    63.  
    64. public Block getAttachedBlock(Block sb) {
    65. if (sb.getType() == Material.WALL_SIGN) {
    66. Sign s = (Sign) sb.getState().getData();
    67. return sb.getRelative(s.getAttachedFace());
    68. } else {
    69. return null;
    70. }
    71. }
    72.  
    73. private int inactivityDays(PlayerInteractEvent event){
    74.  
    75. UUID PlayerOnSign = playerOnSign(event);
    76.  
    77. long lastseen = Bukkit.getOfflinePlayer(PlayerOnSign).getLastPlayed();
    78.  
    79. //Last time they were seen online (milliseconds)
    80.  
    81. long current = System.currentTimeMillis();
    82.  
    83. //Current system time (milliseconds)
    84.  
    85. long difference = current - lastseen;
    86.  
    87. //Difference in time (milliseconds)
    88.  
    89. int inactivedays = (int) (difference/1000/60/60/24);
    90.  
    91. //Changing the difference in time in milliseconds to days
    92.  
    93. return inactivedays;
    94.  
    95. }
    96.  
    97. private void chestContainerOpening(PlayerInteractEvent event,Block signBlock){
    98.  
    99. Block attachedBlock = getAttachedBlock(signBlock);
    100. if(attachedBlock == null){
    101. plugin.getLogger().log(Level.SEVERE,"The attachedBlock was null!");;
    102. return;
    103. }
    104. else{
    105.  
    106. if(attachedBlock.getType().equals(Material.CHEST)){
    107.  
    108. Player player = event.getPlayer(); //Getting the player who punched the sign
    109.  
    110. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating useful variable
    111.  
    112. if(plugin.getConfig().getBoolean ("ClearItems")){
    113.  
    114. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString("onUnlock.messageChest"));
    115.  
    116. Chest chest = (Chest) attachedBlock.getState();
    117.  
    118. chest.getInventory().clear();
    119.  
    120. BlockFace[] chestFaces = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH};
    121.  
    122. for (BlockFace bf : chestFaces) {
    123.  
    124. Block faceBlock = attachedBlock.getRelative(bf);
    125.  
    126. if (faceBlock.getType() == Material.CHEST)
    127.  
    128. {
    129.  
    130. ((Chest) faceBlock.getState()).getInventory().clear();
    131.  
    132. }
    133.  
    134. }
    135.  
    136. }
    137.  
    138. }
    139. }
    140. }
    141.  
    142. private void containerOpening(Material Container, PlayerInteractEvent event){
    143.  
    144. Block attachedBlock = getAttachedBlock(event.getClickedBlock());
    145.  
    146. if(attachedBlock == null){
    147. plugin.getLogger().log(Level.SEVERE,"The attachedBlock was null!");;
    148. return;
    149. }
    150. else{
    151. if(attachedBlock.getType() == Container){
    152.  
    153. if(plugin.getConfig().getBoolean ("ClearItems")){
    154.  
    155. Player player = event.getPlayer(); //Getting the player who clicked the sign
    156.  
    157. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating useful variable
    158.  
    159. if(Container == Material.FURNACE){
    160.  
    161. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString ("onUnlock.messageFurnace"));
    162.  
    163. Furnace furnace = (Furnace) attachedBlock.getState();
    164.  
    165. furnace.getInventory().clear();
    166.  
    167. }
    168.  
    169. else if(Container == Material.DISPENSER){
    170.  
    171. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString ("onUnlock.messageDispenser"));
    172.  
    173. Dispenser dispenser = (Dispenser) attachedBlock.getState();
    174.  
    175. dispenser.getInventory().clear();
    176.  
    177. }
    178.  
    179. else if(Container == Material.HOPPER){
    180.  
    181. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString ("onUnlock.messageHopper"));
    182.  
    183. Hopper hopper = (Hopper) attachedBlock.getState();
    184.  
    185. hopper.getInventory().clear();
    186.  
    187. }
    188.  
    189. if(Container == Material.DROPPER){
    190.  
    191. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString ("onUnlock.messageDropper"));
    192.  
    193. Dropper dropper = (Dropper) attachedBlock.getState();
    194.  
    195. dropper.getInventory().clear();
    196.  
    197. }
    198.  
    199. }
    200.  
    201. }
    202.  
    203. }
    204. }
    205.  
    206. private void broadcast(PlayerInteractEvent event){
    207.  
    208. if(plugin.getConfig().getBoolean("settings.broadcast")){
    209.  
    210. Block block = event.getClickedBlock(); //Getting the block the player clicked
    211.  
    212. Block attachedBlock = getAttachedBlock(event.getClickedBlock());
    213.  
    214. if(attachedBlock == null){
    215. plugin.getLogger().log(Level.SEVERE,"The attachedBlock was null!");;
    216. return;
    217. }
    218. else{
    219. String freedblock = attachedBlock.getType().name();
    220.  
    221. Location l = block.getLocation();
    222.  
    223. int x = (int) l.getX();
    224.  
    225. int y = (int) l.getY();
    226.  
    227. int z = (int) l.getZ();
    228.  
    229. String location = x + ", " + y + ", " + z;
    230.  
    231. String PlayerOnSign = playerOnSign(event).toString();
    232.  
    233. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating useful variable
    234.  
    235. Bukkit.broadcastMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString("messages.messageBroadcast").replace("%block%", freedblock).replace("%owner%", PlayerOnSign).replace("%coordinates%", location));
    236.  
    237. }
    238. }
    239. }
    240.  
    241. private void signHasPrivate(PlayerInteractEvent event){
    242. Player player = event.getPlayer(); //Getting the player who clicked the sign
    243. String PlayerOnSign = playerOnSign(event).toString();
    244. int lengthPlayerOnSign = PlayerOnSign.length();
    245. if(lengthPlayerOnSign<14){
    246. int inactivedays = inactivityDays(event); //Inactivedays variable
    247.  
    248. if(inactivedays == 0 && plugin.getConfig().getBoolean("settings.onClickDisplayDays")){
    249.  
    250. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating chat prefix variable
    251.  
    252. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString("onPunch.messageActive"));
    253.  
    254. //Player is still active
    255.  
    256. }
    257.  
    258. else if(plugin.getConfig().getBoolean("settings.onClickDisplayDays")){
    259.  
    260. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating prefix variable
    261.  
    262. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString("onPunch.messageInactive").replace("%inactivedays%", Integer.toString(inactivedays)));
    263.  
    264. if(plugin.getConfig().getBoolean("settings.onClickDisplayDaysToWait") && !((plugin.getConfig().getInt("settings.daysOfInactivity")) - (inactivedays) < 0)){
    265.  
    266. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString("onPunch.messageDaysToWait").replace("%daystowait%", Integer.toString((plugin.getConfig().getInt("settings.daysOfInactivity")) - (inactivedays))));
    267.  
    268. }
    269. else{}
    270. //No need to display days to wait
    271.  
    272. }
    273.  
    274. if((inactivedays > plugin.getConfig().getInt("settings.daysOfInactivity"))) {
    275.  
    276. //If player has been inactive for at least set amount of days
    277.  
    278. if(plugin.getConfig().getBoolean("settings.permissionToOpenLocks")){
    279.  
    280. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating prefix variable
    281.  
    282. if(player.isOp()||player.hasPermission("inactivelockette.player")||player.hasPermission("inactivelockette.*")){ //If player who clicked has permission to open locks
    283. if(plugin.getConfig().getBoolean("settings.useEconomy")){ //If economy interface is used
    284. String playerName = player.getName(); //Player name String variable
    285. if((InactiveLockette.econ.isEnabled()) && (InactiveLockette.econ.hasAccount(playerName))){
    286.  
    287. int cost = plugin.getConfig().getInt("settings.costToOpenLocks");//Cost variable
    288. double balance = InactiveLockette.econ.getBalance(playerName);//Balance of player
    289.  
    290. if(InactiveLockette.econ.has(playerName, cost)){
    291. InactiveLockette.econ.withdrawPlayer(playerName, cost);
    292. player.sendMessage(ChatColor.GREEN + prefix + " " + plugin.getConfig().getString("messages.messageMoneyWithdraw").replace("%cost%", Integer.toString(cost)).replace("%balance%", Double.toString(InactiveLockette.econ.getBalance(playerName))));
    293. }
    294. else if(!(InactiveLockette.econ.has(playerName, plugin.getConfig().getInt("settings.costToOpenLocks")))){
    295. double moneyneeded = cost-balance;
    296. player.sendMessage(ChatColor.RED + prefix + " " + plugin.getConfig().getString("messages.messageMoneyTransactionFailed1"));
    297. player.sendMessage(ChatColor.RED + prefix + " " + plugin.getConfig().getString("messages.messageMoneyTransactionFailed2").replace("%cost%", Integer.toString(cost)));
    298. player.sendMessage(ChatColor.RED + prefix + " " + plugin.getConfig().getString("messages.messageMoneyTransactionFailed3").replace("%balance%", Double.toString(balance)).replace("%moneyneeded%", Double.toString(moneyneeded)));
    299. return;
    300. }
    301. }
    302. }
    303. //Economy isn't used
    304.  
    305. chestContainerOpening(event, event.getClickedBlock()); //Passing to the checks to remove items
    306.  
    307. containerOpening(Material.FURNACE, event);
    308.  
    309. containerOpening(Material.DISPENSER, event);
    310.  
    311. containerOpening(Material.HOPPER, event);
    312.  
    313. containerOpening(Material.DROPPER, event);
    314.  
    315. event.getClickedBlock().breakNaturally(); //Breaking the sign
    316.  
    317. broadcast(event);
    318.  
    319. }
    320. else{
    321. player.sendMessage(ChatColor.DARK_RED + prefix + " " + plugin.getConfig().getString("onPunch.messageNoPermission"));
    322. }
    323. }
    324.  
    325. }
    326.  
    327. }
    328. //Player name on sign is longer than 14 chars, cannot act further.
    329. }
    330. }


    Now I don't get any error at all, just nothing happens.
    That means the Line(1) on the sign isn't null, or the error message I put it would appear.

    Could the problem be elsewhere in the class?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  24. gvlfm78 Can I see your updated code?
     
  25. Offline

    tomudding

    gvlfm78, Check if the clicked block is a Sign.
    Code:java
    1. if(block instance of Sign) { //block is your event.getClickedBlock()
     
  26. Offline

    gvlfm78

    Of which sign class?

    org.bukkit.block.Sign

    OR

    org.bukkit.Material.Sign?

    Currently this is the check I have at the top of the class, and have had for ages:

    Code:java
    1. if ((block.getState() instanceof org.bukkit.block.Sign)&&(block != null)&&(block.getType() == Material.WALL_SIGN)){ //Player Left Clicked a wall sign



    Do you mean this:
    Code:java
    1. package gvlfm78.plugin.InactiveLockette;
    2.  
    3. import org.bukkit.Bukkit;
    4.  
    5. import org.bukkit.ChatColor;
    6.  
    7. import org.bukkit.Location;
    8.  
    9. import org.bukkit.Material;
    10.  
    11. import org.bukkit.block.*;
    12.  
    13. import org.bukkit.material.Sign;
    14.  
    15. import org.bukkit.entity.Player;
    16.  
    17. import org.bukkit.event.EventHandler;
    18.  
    19. import org.bukkit.event.EventPriority;
    20.  
    21. import org.bukkit.event.Listener;
    22.  
    23. import org.bukkit.event.block.Action;
    24.  
    25. import org.bukkit.event.player.PlayerInteractEvent;
    26.  
    27. import org.bukkit.material.Attachable;
    28.  
    29. import java.util.UUID;
    30. import java.util.logging.Level;
    31.  
    32. public class InactiveLocketteListener implements Listener {
    33.  
    34. private InactiveLockette plugin;
    35. public InactiveLocketteListener(InactiveLockette plugin) {this.plugin = plugin;}
    36.  
    37.  
    38. @EventHandler(ignoreCancelled=true,priority = EventPriority.HIGH)
    39. public void onPlayerInteract(PlayerInteractEvent event) {
    40. if(event.getAction() == Action.LEFT_CLICK_BLOCK){
    41. Block block = event.getClickedBlock();
    42.  
    43. if ((block.getState() instanceof org.bukkit.block.Sign)&&(block != null)&&(block.getType() == Material.WALL_SIGN)){ //Player Left Clicked a wall sign
    44. org.bukkit.block.Sign sign = (org.bukkit.block.Sign) block.getState();
    45. if (sign.getLine(0).equalsIgnoreCase("[" + plugin.getConfig().getString("settingsChat.firstLine") + "]")){ //The first line of the sign contains [Private]
    46.  
    47. signHasPrivate(event);
    48. }
    49. }
    50. }
    51. }
    52.  
    53. private UUID playerOnSign(PlayerInteractEvent event){
    54. Block block = event.getClickedBlock(); //Getting the block the player clicked
    55. org.bukkit.block.Sign sign = (org.bukkit.block.Sign) block.getState(); //Getting the sign state
    56. if(sign.getLine(1) != null){
    57. UUID PlayerOnSignVariable = Bukkit.getPlayer(sign.getLine(1)).getUniqueId();
    58. return PlayerOnSignVariable;
    59. }else
    60. plugin.getLogger().severe("Could not get player UUID from sign");
    61. return null;
    62. }
    63.  
    64. public Block getAttachedBlock(Block sb) {
    65. if (sb.getType() == Material.WALL_SIGN) {
    66. Sign s = (Sign) sb.getState().getData();
    67. return sb.getRelative(s.getAttachedFace());
    68. } else {
    69. return null;
    70. }
    71. }
    72.  
    73. private int inactivityDays(PlayerInteractEvent event){
    74.  
    75. UUID PlayerOnSign = playerOnSign(event);
    76.  
    77. long lastseen = Bukkit.getOfflinePlayer(PlayerOnSign).getLastPlayed();
    78.  
    79. //Last time they were seen online (milliseconds)
    80.  
    81. long current = System.currentTimeMillis();
    82.  
    83. //Current system time (milliseconds)
    84.  
    85. long difference = current - lastseen;
    86.  
    87. //Difference in time (milliseconds)
    88.  
    89. int inactivedays = (int) (difference/1000/60/60/24);
    90.  
    91. //Changing the difference in time in milliseconds to days
    92.  
    93. return inactivedays;
    94.  
    95. }
    96.  
    97. private void chestContainerOpening(PlayerInteractEvent event,Block signBlock){
    98.  
    99. Block attachedBlock = getAttachedBlock(signBlock);
    100. if(attachedBlock == null){
    101. plugin.getLogger().log(Level.SEVERE,"The attachedBlock was null!");;
    102. return;
    103. }
    104. else{
    105.  
    106. if(attachedBlock.getType().equals(Material.CHEST)){
    107.  
    108. Player player = event.getPlayer(); //Getting the player who punched the sign
    109.  
    110. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating useful variable
    111.  
    112. if(plugin.getConfig().getBoolean ("ClearItems")){
    113.  
    114. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString("onUnlock.messageChest"));
    115.  
    116. Chest chest = (Chest) attachedBlock.getState();
    117.  
    118. chest.getInventory().clear();
    119.  
    120. BlockFace[] chestFaces = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH};
    121.  
    122. for (BlockFace bf : chestFaces) {
    123.  
    124. Block faceBlock = attachedBlock.getRelative(bf);
    125.  
    126. if (faceBlock.getType() == Material.CHEST)
    127.  
    128. {
    129.  
    130. ((Chest) faceBlock.getState()).getInventory().clear();
    131.  
    132. }
    133.  
    134. }
    135.  
    136. }
    137.  
    138. }
    139. }
    140. }
    141.  
    142. private void containerOpening(Material Container, PlayerInteractEvent event){
    143.  
    144. Block attachedBlock = getAttachedBlock(event.getClickedBlock());
    145.  
    146. if(attachedBlock == null){
    147. plugin.getLogger().log(Level.SEVERE,"The attachedBlock was null!");;
    148. return;
    149. }
    150. else{
    151. if(attachedBlock.getType() == Container){
    152.  
    153. if(plugin.getConfig().getBoolean ("ClearItems")){
    154.  
    155. Player player = event.getPlayer(); //Getting the player who clicked the sign
    156.  
    157. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating useful variable
    158.  
    159. if(Container == Material.FURNACE){
    160.  
    161. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString ("onUnlock.messageFurnace"));
    162.  
    163. Furnace furnace = (Furnace) attachedBlock.getState();
    164.  
    165. furnace.getInventory().clear();
    166.  
    167. }
    168.  
    169. else if(Container == Material.DISPENSER){
    170.  
    171. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString ("onUnlock.messageDispenser"));
    172.  
    173. Dispenser dispenser = (Dispenser) attachedBlock.getState();
    174.  
    175. dispenser.getInventory().clear();
    176.  
    177. }
    178.  
    179. else if(Container == Material.HOPPER){
    180.  
    181. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString ("onUnlock.messageHopper"));
    182.  
    183. Hopper hopper = (Hopper) attachedBlock.getState();
    184.  
    185. hopper.getInventory().clear();
    186.  
    187. }
    188.  
    189. if(Container == Material.DROPPER){
    190.  
    191. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString ("onUnlock.messageDropper"));
    192.  
    193. Dropper dropper = (Dropper) attachedBlock.getState();
    194.  
    195. dropper.getInventory().clear();
    196.  
    197. }
    198.  
    199. }
    200.  
    201. }
    202.  
    203. }
    204. }
    205.  
    206. private void broadcast(PlayerInteractEvent event){
    207.  
    208. if(plugin.getConfig().getBoolean("settings.broadcast")){
    209.  
    210. Block block = event.getClickedBlock(); //Getting the block the player clicked
    211.  
    212. Block attachedBlock = getAttachedBlock(event.getClickedBlock());
    213.  
    214. if(attachedBlock == null){
    215. plugin.getLogger().log(Level.SEVERE,"The attachedBlock was null!");;
    216. return;
    217. }
    218. else{
    219. String freedblock = attachedBlock.getType().name();
    220.  
    221. Location l = block.getLocation();
    222.  
    223. int x = (int) l.getX();
    224.  
    225. int y = (int) l.getY();
    226.  
    227. int z = (int) l.getZ();
    228.  
    229. String location = x + ", " + y + ", " + z;
    230.  
    231. String PlayerOnSign = playerOnSign(event).toString();
    232.  
    233. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating useful variable
    234.  
    235. Bukkit.broadcastMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString("messages.messageBroadcast").replace("%block%", freedblock).replace("%owner%", PlayerOnSign).replace("%coordinates%", location));
    236.  
    237. }
    238. }
    239. }
    240.  
    241. private void signHasPrivate(PlayerInteractEvent event){
    242. Player player = event.getPlayer(); //Getting the player who clicked the sign
    243. String PlayerOnSign = playerOnSign(event).toString();
    244. int lengthPlayerOnSign = PlayerOnSign.length();
    245. if(lengthPlayerOnSign<14){
    246. int inactivedays = inactivityDays(event); //Inactivedays variable
    247.  
    248. if(inactivedays == 0 && plugin.getConfig().getBoolean("settings.onClickDisplayDays")){
    249.  
    250. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating chat prefix variable
    251.  
    252. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString("onPunch.messageActive"));
    253.  
    254. //Player is still active
    255.  
    256. }
    257.  
    258. else if(plugin.getConfig().getBoolean("settings.onClickDisplayDays")){
    259.  
    260. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating prefix variable
    261.  
    262. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString("onPunch.messageInactive").replace("%inactivedays%", Integer.toString(inactivedays)));
    263.  
    264. if(plugin.getConfig().getBoolean("settings.onClickDisplayDaysToWait") && !((plugin.getConfig().getInt("settings.daysOfInactivity")) - (inactivedays) < 0)){
    265.  
    266. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString("onPunch.messageDaysToWait").replace("%daystowait%", Integer.toString((plugin.getConfig().getInt("settings.daysOfInactivity")) - (inactivedays))));
    267.  
    268. }
    269. else{}
    270. //No need to display days to wait
    271.  
    272. }
    273.  
    274. if((inactivedays > plugin.getConfig().getInt("settings.daysOfInactivity"))) {
    275.  
    276. //If player has been inactive for at least set amount of days
    277.  
    278. if(plugin.getConfig().getBoolean("settings.permissionToOpenLocks")){
    279.  
    280. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating prefix variable
    281.  
    282. if(player.isOp()||player.hasPermission("inactivelockette.player")||player.hasPermission("inactivelockette.*")){ //If player who clicked has permission to open locks
    283. if(plugin.getConfig().getBoolean("settings.useEconomy")){ //If economy interface is used
    284. String playerName = player.getName(); //Player name String variable
    285. if((InactiveLockette.econ.isEnabled()) && (InactiveLockette.econ.hasAccount(playerName))){
    286.  
    287. int cost = plugin.getConfig().getInt("settings.costToOpenLocks");//Cost variable
    288. double balance = InactiveLockette.econ.getBalance(playerName);//Balance of player
    289.  
    290. if(InactiveLockette.econ.has(playerName, cost)){
    291. InactiveLockette.econ.withdrawPlayer(playerName, cost);
    292. player.sendMessage(ChatColor.GREEN + prefix + " " + plugin.getConfig().getString("messages.messageMoneyWithdraw").replace("%cost%", Integer.toString(cost)).replace("%balance%", Double.toString(InactiveLockette.econ.getBalance(playerName))));
    293. }
    294. else if(!(InactiveLockette.econ.has(playerName, plugin.getConfig().getInt("settings.costToOpenLocks")))){
    295. double moneyneeded = cost-balance;
    296. player.sendMessage(ChatColor.RED + prefix + " " + plugin.getConfig().getString("messages.messageMoneyTransactionFailed1"));
    297. player.sendMessage(ChatColor.RED + prefix + " " + plugin.getConfig().getString("messages.messageMoneyTransactionFailed2").replace("%cost%", Integer.toString(cost)));
    298. player.sendMessage(ChatColor.RED + prefix + " " + plugin.getConfig().getString("messages.messageMoneyTransactionFailed3").replace("%balance%", Double.toString(balance)).replace("%moneyneeded%", Double.toString(moneyneeded)));
    299. return;
    300. }
    301. }
    302. }
    303. //Economy isn't used
    304.  
    305. chestContainerOpening(event, event.getClickedBlock()); //Passing to the checks to remove items
    306.  
    307. containerOpening(Material.FURNACE, event);
    308.  
    309. containerOpening(Material.DISPENSER, event);
    310.  
    311. containerOpening(Material.HOPPER, event);
    312.  
    313. containerOpening(Material.DROPPER, event);
    314.  
    315. event.getClickedBlock().breakNaturally(); //Breaking the sign
    316.  
    317. broadcast(event);
    318.  
    319. }
    320. else{
    321. player.sendMessage(ChatColor.DARK_RED + prefix + " " + plugin.getConfig().getString("onPunch.messageNoPermission"));
    322. }
    323. }
    324.  
    325. }
    326.  
    327. }
    328. //Player name on sign is longer than 14 chars, cannot act further.
    329. }
    330. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  27. gvlfm78 Okay, that's quite a bit to look through so what you're going to have to do is put in some debug code to check where the thing turns unexpected. For example, check your if statements and your method returns. Is the block an instance of a Sign? Is the player returned the one you were expecting? Is line 1 null? And so on. When you do that, you should find what's wrong, or at least narrow down some possibilities.
     
  28. Offline

    gvlfm78

    Yes, I was going to do that yesterday but I had to go.

    I will now add some debug checks and re-post with what I have found out.
     
  29. Offline

    BillyGalbreath

    You can never have too many null checks ;)

    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
    3. public UUID getUUIDfromNameOnSign(PlayerInteractEvent event) {
    4. Block block = event.getClickedBlock(); //Getting the block the player clicked
    5. if (block == null)
    6. return null; // no block clicked (air, etc)
    7. if (!(block.getState() instanceof Sign))
    8. return null; // no sign clicked
    9. Sign sign = (Sign) block.getState(); // get the sign
    10. String line1 = sign.getLine(1); // get the sign text on second line
    11. if (line1 == null || line1.equals(""))
    12. return null; // Second line on sign is empty
    13. Player player = Bukkit.getPlayer(line1); // Deprecated; Can also use .getOfflinePlayer() here to check offline players (also deprecated)
    14. if (player == null)
    15. return null; // Cannot find player by name on sign
    16. return player.getUniqueId(); // return the UUID
    17. }
    18.  


    Null check all the things \o/
     
  30. Offline

    gvlfm78

    Now.

    it is getting the UUID from the player from the sign, according to the debug checks. But I tested that with my own username on the sign, and in-game, nothing happens.

    I was getting the NullPointer because on the coversion to UUIDs the spigot server (which doesn't affect this, the code is in Bukkit) deleted the user profiles of the people I was using the inactive locks of, so the player we were trying to get didn't exist and it gave and error.

    In the meantime, here is the class:

    Code:java
    1. package gvlfm78.plugin.InactiveLockette;
    2.  
    3. import org.bukkit.Bukkit;
    4.  
    5. import org.bukkit.ChatColor;
    6.  
    7. import org.bukkit.Location;
    8.  
    9. import org.bukkit.Material;
    10.  
    11. import org.bukkit.block.*;
    12.  
    13. import org.bukkit.material.Sign;
    14.  
    15. import org.bukkit.entity.Player;
    16.  
    17. import org.bukkit.event.EventHandler;
    18.  
    19. import org.bukkit.event.EventPriority;
    20.  
    21. import org.bukkit.event.Listener;
    22.  
    23. import org.bukkit.event.block.Action;
    24.  
    25. import org.bukkit.event.player.PlayerInteractEvent;
    26.  
    27. import org.bukkit.material.Attachable;
    28.  
    29. import java.util.UUID;
    30. import java.util.logging.Level;
    31.  
    32. public class InactiveLocketteListener implements Listener {
    33.  
    34. private InactiveLockette plugin;
    35. public InactiveLocketteListener(InactiveLockette plugin) {this.plugin = plugin;}
    36.  
    37.  
    38. @EventHandler(ignoreCancelled=true,priority = EventPriority.HIGH)
    39. public void onPlayerInteract(PlayerInteractEvent event) {
    40. if(event.getAction() == Action.LEFT_CLICK_BLOCK){
    41. Block block = event.getClickedBlock();
    42. plugin.getLogger().info("DEBUG: Gotten left clicked block");
    43. if ((block.getState() instanceof org.bukkit.block.Sign)&&(block != null)&&(block.getType() == Material.WALL_SIGN)){ //Player Left Clicked a wall sign
    44. plugin.getLogger().info("DEBUG: Got a wall sign");
    45. org.bukkit.block.Sign sign = (org.bukkit.block.Sign) block.getState();
    46. if (sign.getLine(0).equalsIgnoreCase("[" + plugin.getConfig().getString("settingsChat.firstLine") + "]")){ //The first line of the sign contains [Private]
    47. plugin.getLogger().info("DEBUG: Sign contains private");
    48. signHasPrivate(event);
    49. }
    50. }
    51. }
    52. }
    53.  
    54. private UUID playerOnSign(PlayerInteractEvent event) {
    55. Block block = event.getClickedBlock(); //Getting the block the player clicked
    56. org.bukkit.block.Sign sign = (org.bukkit.block.Sign) block.getState(); //Getting the sign state
    57. if (sign.getLine(1) != null && Bukkit.getPlayerExact(sign.getLine(1)) != null) {
    58. plugin.getLogger().info("DEBUG: Line 1 of sign is not null and player isn't null");
    59. Player playerObjectOnSign = Bukkit.getPlayerExact(sign.getLine(1)); //Getting Player from sign
    60. UUID PlayerOnSignVariable = playerObjectOnSign.getUniqueId(); //Getting UUID from Player
    61. plugin.getLogger().info("DEBUG: Got the UUID from the sign!");
    62. return PlayerOnSignVariable;
    63. } else
    64. plugin.getLogger().severe("Could not get player UUID from sign");
    65. return null;
    66. }
    67.  
    68. public Block getAttachedBlock(Block sb) {
    69. if (sb.getType() == Material.WALL_SIGN) {
    70. Sign s = (Sign) sb.getState().getData();
    71. plugin.getLogger().info("DEBUG: Attached block found");
    72. return sb.getRelative(s.getAttachedFace());
    73. } else {
    74. return null;
    75. }
    76. }
    77.  
    78. private int inactivityDays(PlayerInteractEvent event){
    79.  
    80. UUID PlayerOnSign = playerOnSign(event);
    81.  
    82. long lastseen = Bukkit.getOfflinePlayer(PlayerOnSign).getLastPlayed();
    83.  
    84. //Last time they were seen online (milliseconds)
    85.  
    86. long current = System.currentTimeMillis();
    87.  
    88. //Current system time (milliseconds)
    89.  
    90. long difference = current - lastseen;
    91.  
    92. //Difference in time (milliseconds)
    93.  
    94. int inactivedays = (int) (difference/1000/60/60/24);
    95.  
    96. //Changing the difference in time in milliseconds to days
    97.  
    98. return inactivedays;
    99.  
    100. }
    101.  
    102. private void chestContainerOpening(PlayerInteractEvent event,Block signBlock){
    103.  
    104. Block attachedBlock = getAttachedBlock(signBlock);
    105. if(attachedBlock == null){
    106. plugin.getLogger().log(Level.SEVERE,"The attached Block was null!");
    107. return;
    108. }
    109. else{
    110.  
    111. if(attachedBlock.getType().equals(Material.CHEST)){
    112.  
    113. Player player = event.getPlayer(); //Getting the player who punched the sign
    114.  
    115. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating useful variable
    116.  
    117. if(plugin.getConfig().getBoolean ("ClearItems")){
    118.  
    119. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString("onUnlock.messageChest"));
    120.  
    121. Chest chest = (Chest) attachedBlock.getState();
    122.  
    123. chest.getInventory().clear();
    124.  
    125. BlockFace[] chestFaces = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH};
    126.  
    127. for (BlockFace bf : chestFaces) {
    128.  
    129. Block faceBlock = attachedBlock.getRelative(bf);
    130.  
    131. if (faceBlock.getType() == Material.CHEST)
    132.  
    133. {
    134.  
    135. ((Chest) faceBlock.getState()).getInventory().clear();
    136.  
    137. }
    138.  
    139. }
    140.  
    141. }
    142.  
    143. }
    144. }
    145. }
    146.  
    147. private void containerOpening(Material Container, PlayerInteractEvent event){
    148.  
    149. Block attachedBlock = getAttachedBlock(event.getClickedBlock());
    150.  
    151. if(attachedBlock == null){
    152. plugin.getLogger().log(Level.SEVERE,"The attachedBlock was null!");;
    153. return;
    154. }
    155. else{
    156. if(attachedBlock.getType() == Container){
    157.  
    158. if(plugin.getConfig().getBoolean ("ClearItems")){
    159.  
    160. Player player = event.getPlayer(); //Getting the player who clicked the sign
    161.  
    162. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating useful variable
    163.  
    164. if(Container == Material.FURNACE){
    165.  
    166. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString ("onUnlock.messageFurnace"));
    167.  
    168. Furnace furnace = (Furnace) attachedBlock.getState();
    169.  
    170. furnace.getInventory().clear();
    171.  
    172. }
    173.  
    174. else if(Container == Material.DISPENSER){
    175.  
    176. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString ("onUnlock.messageDispenser"));
    177.  
    178. Dispenser dispenser = (Dispenser) attachedBlock.getState();
    179.  
    180. dispenser.getInventory().clear();
    181.  
    182. }
    183.  
    184. else if(Container == Material.HOPPER){
    185.  
    186. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString ("onUnlock.messageHopper"));
    187.  
    188. Hopper hopper = (Hopper) attachedBlock.getState();
    189.  
    190. hopper.getInventory().clear();
    191.  
    192. }
    193.  
    194. if(Container == Material.DROPPER){
    195.  
    196. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString ("onUnlock.messageDropper"));
    197.  
    198. Dropper dropper = (Dropper) attachedBlock.getState();
    199.  
    200. dropper.getInventory().clear();
    201.  
    202. }
    203.  
    204. }
    205.  
    206. }
    207.  
    208. }
    209. }
    210.  
    211. private void broadcast(PlayerInteractEvent event){
    212.  
    213. if(plugin.getConfig().getBoolean("settings.broadcast")){
    214.  
    215. Block block = event.getClickedBlock(); //Getting the block the player clicked
    216.  
    217. Block attachedBlock = getAttachedBlock(event.getClickedBlock());
    218.  
    219. if(attachedBlock == null){
    220. plugin.getLogger().log(Level.SEVERE,"The attachedBlock was null!");;
    221. return;
    222. }
    223. else{
    224. String freedblock = attachedBlock.getType().name();
    225.  
    226. Location l = block.getLocation();
    227.  
    228. int x = (int) l.getX();
    229.  
    230. int y = (int) l.getY();
    231.  
    232. int z = (int) l.getZ();
    233.  
    234. String location = x + ", " + y + ", " + z;
    235.  
    236. String PlayerOnSign = playerOnSign(event).toString();
    237.  
    238. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating useful variable
    239.  
    240. Bukkit.broadcastMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString("messages.messageBroadcast").replace("%block%", freedblock).replace("%owner%", PlayerOnSign).replace("%coordinates%", location));
    241.  
    242. }
    243. }
    244. }
    245.  
    246. private void signHasPrivate(PlayerInteractEvent event){
    247. Player player = event.getPlayer(); //Getting the player who clicked the sign
    248. String PlayerOnSign = playerOnSign(event).toString();
    249. int lengthPlayerOnSign = PlayerOnSign.length();
    250. if(lengthPlayerOnSign<14){
    251. int inactivedays = inactivityDays(event); //Inactivedays variable
    252.  
    253. if(inactivedays == 0 && plugin.getConfig().getBoolean("settings.onClickDisplayDays")){
    254.  
    255. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating chat prefix variable
    256.  
    257. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString("onPunch.messageActive"));
    258.  
    259. //Player is still active
    260.  
    261. }
    262.  
    263. else if(plugin.getConfig().getBoolean("settings.onClickDisplayDays")){
    264.  
    265. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating prefix variable
    266.  
    267. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString("onPunch.messageInactive").replace("%inactivedays%", Integer.toString(inactivedays)));
    268.  
    269. if(plugin.getConfig().getBoolean("settings.onClickDisplayDaysToWait") && !((plugin.getConfig().getInt("settings.daysOfInactivity")) - (inactivedays) < 0)){
    270.  
    271. player.sendMessage(ChatColor.BLUE + prefix + " " + plugin.getConfig().getString("onPunch.messageDaysToWait").replace("%daystowait%", Integer.toString((plugin.getConfig().getInt("settings.daysOfInactivity")) - (inactivedays))));
    272.  
    273. }
    274. else{}
    275. //No need to display days to wait
    276. plugin.getLogger().info("DEBUG: No need to display days to wait");
    277. }
    278.  
    279. if((inactivedays > plugin.getConfig().getInt("settings.daysOfInactivity"))) {
    280.  
    281. //If player has been inactive for at least set amount of days
    282.  
    283. if(plugin.getConfig().getBoolean("settings.permissionToOpenLocks")){
    284.  
    285. String prefix = plugin.getConfig().getString("settingsChat.prefix"); //Creating prefix variable
    286.  
    287. if(player.isOp()||player.hasPermission("inactivelockette.player")||player.hasPermission("inactivelockette.*")){ //If player who clicked has permission to open locks
    288. if(plugin.getConfig().getBoolean("settings.useEconomy")){ //If economy interface is used
    289. String playerName = player.getName(); //Player name String variable
    290. if((InactiveLockette.econ.isEnabled()) && (InactiveLockette.econ.hasAccount(playerName))){
    291.  
    292. int cost = plugin.getConfig().getInt("settings.costToOpenLocks");//Cost variable
    293. double balance = InactiveLockette.econ.getBalance(playerName);//Balance of player
    294.  
    295. if(InactiveLockette.econ.has(playerName, cost)){
    296. InactiveLockette.econ.withdrawPlayer(playerName, cost);
    297. player.sendMessage(ChatColor.GREEN + prefix + " " + plugin.getConfig().getString("messages.messageMoneyWithdraw").replace("%cost%", Integer.toString(cost)).replace("%balance%", Double.toString(InactiveLockette.econ.getBalance(playerName))));
    298. }
    299. else if(!(InactiveLockette.econ.has(playerName, plugin.getConfig().getInt("settings.costToOpenLocks")))){
    300. double moneyneeded = cost-balance;
    301. player.sendMessage(ChatColor.RED + prefix + " " + plugin.getConfig().getString("messages.messageMoneyTransactionFailed1"));
    302. player.sendMessage(ChatColor.RED + prefix + " " + plugin.getConfig().getString("messages.messageMoneyTransactionFailed2").replace("%cost%", Integer.toString(cost)));
    303. player.sendMessage(ChatColor.RED + prefix + " " + plugin.getConfig().getString("messages.messageMoneyTransactionFailed3").replace("%balance%", Double.toString(balance)).replace("%moneyneeded%", Double.toString(moneyneeded)));
    304. return;
    305. }
    306. }
    307. }
    308. //Economy isn't used
    309.  
    310. chestContainerOpening(event, event.getClickedBlock()); //Passing to the checks to remove items
    311.  
    312. containerOpening(Material.FURNACE, event);
    313.  
    314. containerOpening(Material.DISPENSER, event);
    315.  
    316. containerOpening(Material.HOPPER, event);
    317.  
    318. containerOpening(Material.DROPPER, event);
    319.  
    320. event.getClickedBlock().breakNaturally(); //Breaking the sign
    321.  
    322. broadcast(event);
    323.  
    324. }
    325. else{
    326. player.sendMessage(ChatColor.DARK_RED + prefix + " " + plugin.getConfig().getString("onPunch.messageNoPermission"));
    327. }
    328. }
    329.  
    330. }
    331.  
    332. }
    333. //Player name on sign is longer than 14 chars, cannot act further.
    334. }
    335. }
     
Thread Status:
Not open for further replies.

Share This Page