Solved Token buy sign

Discussion in 'Plugin Development' started by PieMan456, Dec 21, 2013.

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

    PieMan456

    Hello Everyone,

    I made a plugin that gives you tokens when you vote. I am trying to make it so you can buy things with the tokens. Except when you right click on the sign it does nothing. Here is part of my class:
    Code:java
    1. @EventHandler
    2. public void onSignChange(SignChangeEvent e){
    3. Player p = e.getPlayer();
    4. if(!(e.getLines().length > 0)) return;
    5. if(!e.getLine(0).equalsIgnoreCase("[GodToken]")) return;
    6. if(e.getLines().length < 3){
    7. e.setLine(0, ChatColor.RED + "[GodToken]");
    8. p.sendMessage(ChatColor.RED + "A GodToken sign must have at least 4 lines!");
    9. return;
    10. }
    11. e.setLine(0, ChatColor.BLUE + "[GodToken]");
    12. }
    13.  
    14. @EventHandler
    15. public void onPlayerInteract(PlayerInteractEvent e){
    16. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    17. if(e.getClickedBlock().getType() == Material.SIGN){
    18. Sign s = (Sign) e.getClickedBlock().getState();
    19. if(s.getLine(0).equalsIgnoreCase(ChatColor.BLUE + "[GodToken]")){
    20. int id = Integer.parseInt(s.getLine(1));
    21. String lol = s.getLine(2);
    22. int id2 = Integer.parseInt(s.getLine(3));
    23. Player p = e.getPlayer();
    24. Inventory pi = p.getInventory();
    25. Material m = Material.matchMaterial(lol);
    26. pi.addItem(new ItemStack(m, id));
    27. econ.withdrawPlayer(p.getName(), id2);
    28. }
    29. }
    30. if(e.getClickedBlock().getType() == Material.SIGN_POST){
    31. Sign s = (Sign) e.getClickedBlock().getState();
    32. if(s.getLine(0).equalsIgnoreCase(ChatColor.BLUE + "[GodToken]")){
    33. int id = Integer.parseInt(s.getLine(1));
    34. String lol = s.getLine(2);
    35. int id2 = Integer.parseInt(s.getLine(3));
    36. Player p = e.getPlayer();
    37. Inventory pi = p.getInventory();
    38. Material m = Material.matchMaterial(lol);
    39. pi.addItem(new ItemStack(m, id));
    40. econ.withdrawPlayer(p.getName(), id2);
    41. }
    42. }
    43. if(e.getClickedBlock().getType() == Material.WALL_SIGN){
    44. Sign s = (Sign) e.getClickedBlock().getState();
    45. if(s.getLine(0).equalsIgnoreCase(ChatColor.BLUE + "[GodToken]")){
    46. int id = Integer.parseInt(s.getLine(1));
    47. String lol = s.getLine(2);
    48. int id2 = Integer.parseInt(s.getLine(3));
    49. Player p = e.getPlayer();
    50. Inventory pi = p.getInventory();
    51. Material m = Material.matchMaterial(lol);
    52. pi.addItem(new ItemStack(m, id));
    53. econ.withdrawPlayer(p.getName(), id2);
    54. }
    55. }
    56. }
    57. }
     
  2. Offline

    xTigerRebornx

    PieMan456 There are multiple ways of identifying a sign, like Material.SIGN_POST and Material.WALL_SIGN (Not sure about those exact names), check for the other ways instead of just material.sign
     
  3. Offline

    PieMan456

  4. Offline

    xTigerRebornx

    PieMan456 Okay, is there any errors in console? and are your events registered properly?
     
  5. Offline

    PieMan456

    xTigerRebornx
    No there are no errors. And the other event works fine so yes they are registered properly.
     
  6. Offline

    xTigerRebornx

    PieMan456 The only thing I could think of is to add some debug each step that happens, and see what doesn't happen correctly
     
  7. Offline

    PieMan456

  8. Offline

    keensta

    Your problem is "ChatColor.BLUE" you can't have this in the check line. Just remove that and it will work also I shortened you code for you.

    I do however recommend for your code that you do a check to make sure they have the money in the first place.

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerInteract(PlayerInteractEvent e){
    4.  
    5. if(e.isCancelled() || e.getClickedBlock() == null)
    6. return;
    7.  
    8. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    9. Block b = e.getClickedBlock();
    10.  
    11. if(b.getType() == Material.SIGN || b.getType() == Material.SIGN_POST || b.getType() == Material.WALL_SIGN){
    12. Sign s = (Sign) b.getState();
    13.  
    14. if(s.getLine(0).equalsIgnoreCase("[GodToken]")){
    15. String lol = s.getLine(2);
    16. int id = Integer.parseInt(s.getLine(1));
    17. int id2 = Integer.parseInt(s.getLine(3));
    18. Player p = e.getPlayer();
    19. Inventory pi = p.getInventory();
    20. Material m = Material.matchMaterial(lol);
    21.  
    22. econ.withdrawPlayer(p.getName(), id2);
    23. pi.addItem(new ItemStack(m, id));
    24. p.sendMessage("Transaction Complete");
    25. }
    26.  
    27. }
    28.  
    29. }
    30. }
     
  9. Offline

    PieMan456

    keensta xTigerRebornx
    Ok I got it to kind of work now. But there are 2 problems. I have a /gtokens give command. Whenever a player joins and they don't have any tokens it sets their balance to 0. But when I do/gtokens give Pie_Man_456 10 it sets the balance to 010. The other problem is when I try to buy something it glitches and it doesn't give me the thing im buying until I click it multiple times and it does not give me the right amount. Whole Class:
    Code:java
    1. package me.pieman.godtokeneco;
    2.  
    3. import net.milkbowl.vault.economy.Economy;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.block.Sign;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.Action;
    15. import org.bukkit.event.block.SignChangeEvent;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17. import org.bukkit.event.player.PlayerJoinEvent;
    18. import org.bukkit.inventory.Inventory;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.plugin.RegisteredServiceProvider;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23. public class GodTokenEco extends JavaPlugin implements Listener {
    24.  
    25. public static Economy econ = null;
    26.  
    27. public void onEnable(){
    28. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    29. getConfig().options().copyDefaults(true);
    30. saveConfig();
    31. if (!setupEconomy() ) {
    32. getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    33. getServer().getPluginManager().disablePlugin(this);
    34. return;
    35. }
    36. }
    37.  
    38. public void onDisable(){
    39. saveConfig();
    40. }
    41.  
    42. private boolean setupEconomy() {
    43. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    44. return false;
    45. }
    46. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    47. if (rsp == null) {
    48. return false;
    49. }
    50. econ = rsp.getProvider();
    51. return econ != null;
    52. }
    53.  
    54. public void addGodTokens(String player, double amount){
    55. getConfig().set(player, amount);
    56. saveConfig();
    57. }
    58.  
    59. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    60. if(!(sender instanceof Player)){
    61. sender.sendMessage(ChatColor.RED + "Only players can use this command!");
    62. return true;
    63. }
    64.  
    65. Player p = (Player) sender;
    66. if(cmd.getName().equalsIgnoreCase("gtokens")){
    67. if(args.length == 0){
    68. p.sendMessage(ChatColor.RED + "Usage: /gtokens bal");
    69. return true;
    70. }
    71. if(args[0].equalsIgnoreCase("bal")){
    72. if(p.hasPermission("gtokens.bal")){
    73. int cash = getConfig().getInt(p.getName());
    74. p.sendMessage(ChatColor.GREEN + "GodTokens Balance: " + ChatColor.DARK_PURPLE + cash);
    75. return true;
    76. } else {
    77. p.sendMessage(ChatColor.RED + "You do not have permission!");
    78. return true;
    79. }
    80. } if (args[0].equalsIgnoreCase("give")){
    81. if(p.hasPermission("gtokens.give")){
    82. Player target = Bukkit.getServer().getPlayer(args[1]);
    83. if(target == null){
    84. p.sendMessage(ChatColor.RED + "That player is not online!");
    85. return true;
    86. }
    87. int cash = getConfig().getInt(target.getName());
    88. getConfig().set(target.getName(), cash + args[2]);
    89. saveConfig();
    90. p.sendMessage(ChatColor.GREEN + "Added " + args[2] + " to " + target.getName() + "'s account!");
    91. return true;
    92. } else {
    93. p.sendMessage(ChatColor.RED + "You do not have permission!");
    94. return true;
    95. }
    96. }
    97. }
    98.  
    99. return true;
    100. }
    101.  
    102. @EventHandler
    103. public void onSignChange(SignChangeEvent e){
    104. Player p = e.getPlayer();
    105. if(!(e.getLines().length > 0)) return;
    106. if(!e.getLine(0).equalsIgnoreCase("[GodToken]")) return;
    107. if(e.getLines().length < 3){
    108. e.setLine(0, ChatColor.RED + "[GodToken]");
    109. p.sendMessage(ChatColor.RED + "A GodToken sign must have at least 4 lines!");
    110. return;
    111. }
    112. e.setLine(0, ChatColor.BLUE + "[GodToken]");
    113. }
    114.  
    115. @EventHandler
    116. public void onPlayerInteract(PlayerInteractEvent e){
    117. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    118. if(e.getClickedBlock().getType() == Material.SIGN){
    119. Sign s = (Sign) e.getClickedBlock().getState();
    120. if(s.getLine(0).equalsIgnoreCase(ChatColor.BLUE + "[GodToken]")){
    121. int id = Integer.parseInt(s.getLine(1));
    122. String lol = s.getLine(2);
    123. int id2 = Integer.parseInt(s.getLine(3));
    124. Player p = e.getPlayer();
    125. Inventory pi = p.getInventory();
    126. if(getConfig().getInt(p.getName()) < id2){
    127. p.sendMessage(ChatColor.RED + "Sorry you do not have enough GodTokens!");
    128. return;
    129. }
    130. Material m = Material.matchMaterial(lol);
    131. pi.addItem(new ItemStack(m, id));
    132. int cash = getConfig().getInt(p.getName());
    133. getConfig().set(p.getName(), cash - id2);
    134. saveConfig();
    135. }
    136. }
    137. if(e.getClickedBlock().getType() == Material.SIGN_POST){
    138. Sign s = (Sign) e.getClickedBlock().getState();
    139. if(s.getLine(0).equalsIgnoreCase(ChatColor.BLUE + "[GodToken]")){
    140. int id = Integer.parseInt(s.getLine(1));
    141. String lol = s.getLine(2);
    142. int id2 = Integer.parseInt(s.getLine(3));
    143. Player p = e.getPlayer();
    144. Inventory pi = p.getInventory();
    145. if(getConfig().getInt(p.getName()) < id2){
    146. p.sendMessage(ChatColor.RED + "Sorry you do not have enough GodTokens!");
    147. return;
    148. }
    149. Material m = Material.matchMaterial(lol);
    150. pi.addItem(new ItemStack(m, id));
    151. int cash = getConfig().getInt(p.getName());
    152. getConfig().set(p.getName(), cash - id2);
    153. saveConfig();
    154. }
    155. }
    156. if(e.getClickedBlock().getType() == Material.WALL_SIGN){
    157. Sign s = (Sign) e.getClickedBlock().getState();
    158. if(s.getLine(0).equalsIgnoreCase("[GodToken]")){
    159. int id = Integer.parseInt(s.getLine(1));
    160. String lol = s.getLine(2);
    161. int id2 = Integer.parseInt(s.getLine(3));
    162. Player p = e.getPlayer();
    163. Inventory pi = p.getInventory();
    164. if(getConfig().getInt(p.getName()) < id2){
    165. p.sendMessage(ChatColor.RED + "Sorry you do not have enough GodTokens!");
    166. return;
    167. }
    168. Material m = Material.matchMaterial(lol);
    169. pi.addItem(new ItemStack(m, id));
    170. int cash = getConfig().getInt(p.getName());
    171. getConfig().set(p.getName(), cash - id2);
    172. saveConfig();
    173. }
    174. }
    175. }
    176. }
    177.  
    178. @EventHandler
    179. public void onPlayerJoin(PlayerJoinEvent e){
    180. Player p = e.getPlayer();
    181. if(getConfig().get(p.getName()) == null){
    182. getConfig().set(p.getName(), 1);
    183. saveConfig();
    184. }
    185. }
    186. }
    187.  
     
  10. Offline

    xTigerRebornx

    First off, you are getting the value of the player's tokens as a int, but setting it to a double. That might be causing some problems

    Code:
    int cash = getConfig().getInt(target.getName())
    Code:
    public void addGodTokens(String player, double amount)
    PieMan456
     
  11. Offline

    PieMan456

    xTigerRebornx
    Yes but I don't use that method at all so it doesn't matter.
     
  12. Offline

    xTigerRebornx

    PieMan456 Doing what I said should fix both problems, or at least somewhat fix them, and I'd reccomend you store it as an Int too, as you are using parseInt when you click the signs

    PieMan456 You had it, so I figured you used it. Sorry :p

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

    PieMan456

    xTigerRebornx
    Class:
    Code:java
    1. package me.pieman.godtokeneco;
    2.  
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.block.Sign;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.block.Action;
    14. import org.bukkit.event.block.SignChangeEvent;
    15. import org.bukkit.event.player.PlayerInteractEvent;
    16. import org.bukkit.event.player.PlayerJoinEvent;
    17. import org.bukkit.inventory.Inventory;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. public class GodTokenEco extends JavaPlugin implements Listener {
    22.  
    23. public void onEnable(){
    24. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    25. getConfig().options().copyDefaults(true);
    26. saveConfig();
    27. }
    28.  
    29. public void onDisable(){
    30. saveConfig();
    31. }
    32.  
    33. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    34. if(!(sender instanceof Player)){
    35. sender.sendMessage(ChatColor.RED + "Only players can use this command!");
    36. return true;
    37. }
    38.  
    39. Player p = (Player) sender;
    40. if(cmd.getName().equalsIgnoreCase("gtokens")){
    41. if(args.length == 0){
    42. p.sendMessage(ChatColor.RED + "Usage: /gtokens bal");
    43. return true;
    44. }
    45. if(args[0].equalsIgnoreCase("bal")){
    46. if(p.hasPermission("gtokens.bal")){
    47. int cash = getConfig().getInt(p.getName());
    48. p.sendMessage(ChatColor.GREEN + "GodTokens Balance: " + ChatColor.DARK_PURPLE + cash);
    49. return true;
    50. } else {
    51. p.sendMessage(ChatColor.RED + "You do not have permission!");
    52. return true;
    53. }
    54. } if (args[0].equalsIgnoreCase("give")){
    55. if(p.hasPermission("gtokens.give")){
    56. Player target = Bukkit.getServer().getPlayer(args[1]);
    57. if(target == null){
    58. p.sendMessage(ChatColor.RED + "That player is not online!");
    59. return true;
    60. }
    61. int cash = getConfig().getInt(target.getName());
    62. getConfig().set(target.getName(), cash + args[2]);
    63. saveConfig();
    64. p.sendMessage(ChatColor.GREEN + "Added " + args[2] + " GodTokens to " + target.getName() + "'s account!");
    65. return true;
    66. } else {
    67. p.sendMessage(ChatColor.RED + "You do not have permission!");
    68. return true;
    69. }
    70. }
    71. }
    72.  
    73. return true;
    74. }
    75.  
    76. @EventHandler
    77. public void onSignChange(SignChangeEvent e){
    78. Player p = e.getPlayer();
    79. if(!(e.getLines().length > 0)) return;
    80. if(!e.getLine(0).equalsIgnoreCase("[GodToken]")) return;
    81. if(e.getLines().length < 3){
    82. e.setLine(0, ChatColor.RED + "[GodToken]");
    83. p.sendMessage(ChatColor.RED + "A GodToken sign must have at least 4 lines!");
    84. return;
    85. }
    86. e.setLine(0, ChatColor.BLUE + "[GodToken]");
    87. }
    88.  
    89. @EventHandler
    90. public void onPlayerInteract(PlayerInteractEvent e){
    91. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    92. if(e.getClickedBlock().getType() == Material.SIGN){
    93. Sign s = (Sign) e.getClickedBlock().getState();
    94. if(s.getLine(0).equalsIgnoreCase(ChatColor.BLUE + "[GodToken]")){
    95. int id = Integer.parseInt(s.getLine(1));
    96. String lol = s.getLine(2);
    97. int id2 = Integer.parseInt(s.getLine(3));
    98. Player p = e.getPlayer();
    99. Inventory pi = p.getInventory();
    100. if(getConfig().getInt(p.getName()) < id2){
    101. p.sendMessage(ChatColor.RED + "Sorry you do not have enough GodTokens!");
    102. return;
    103. }
    104. Material m = Material.matchMaterial(lol);
    105. pi.addItem(new ItemStack(m, id));
    106. int cash = getConfig().getInt(p.getName());
    107. getConfig().set(p.getName(), cash - id2);
    108. saveConfig();
    109. }
    110. }
    111. if(e.getClickedBlock().getType() == Material.SIGN_POST){
    112. Sign s = (Sign) e.getClickedBlock().getState();
    113. if(s.getLine(0).equalsIgnoreCase(ChatColor.BLUE + "[GodToken]")){
    114. int id = Integer.parseInt(s.getLine(1));
    115. String lol = s.getLine(2);
    116. int id2 = Integer.parseInt(s.getLine(3));
    117. Player p = e.getPlayer();
    118. Inventory pi = p.getInventory();
    119. if(getConfig().getInt(p.getName()) < id2){
    120. p.sendMessage(ChatColor.RED + "Sorry you do not have enough GodTokens!");
    121. return;
    122. }
    123. Material m = Material.matchMaterial(lol);
    124. pi.addItem(new ItemStack(m, id));
    125. int cash = getConfig().getInt(p.getName());
    126. getConfig().set(p.getName(), cash - id2);
    127. saveConfig();
    128. }
    129. }
    130. if(e.getClickedBlock().getType() == Material.WALL_SIGN){
    131. Sign s = (Sign) e.getClickedBlock().getState();
    132. if(s.getLine(0).equalsIgnoreCase("[GodToken]")){
    133. int id = Integer.parseInt(s.getLine(1));
    134. String lol = s.getLine(2);
    135. int id2 = Integer.parseInt(s.getLine(3));
    136. Player p = e.getPlayer();
    137. Inventory pi = p.getInventory();
    138. if(getConfig().getInt(p.getName()) < id2){
    139. p.sendMessage(ChatColor.RED + "Sorry you do not have enough GodTokens!");
    140. return;
    141. }
    142. Material m = Material.matchMaterial(lol);
    143. pi.addItem(new ItemStack(m, id));
    144. int cash = getConfig().getInt(p.getName());
    145. getConfig().set(p.getName(), cash - id2);
    146. saveConfig();
    147. }
    148. }
    149. }
    150. }
    151.  
    152. @EventHandler
    153. public void onPlayerJoin(PlayerJoinEvent e){
    154. Player p = e.getPlayer();
    155. if(getConfig().get(p.getName()) == null){
    156. int cash = 0;
    157. getConfig().set(p.getName(), cash);
    158. saveConfig();
    159. }
    160. }
    161. }
    162.  


    Does it look better now?
     
  14. Offline

    xTigerRebornx

    When you set it for your give command, I believe that it is storing it as a string, since you never checked it args[2] is an integer. Add a check to see if args[2] is an int, then see if it fixes the first problem PieMan456
     
  15. Offline

    PieMan456

  16. Offline

    xTigerRebornx

    Do the same thing you do with the signs.
    use a try/catch, and parseInt args[2]
    PieMan456
     
  17. Offline

    PieMan456

  18. Offline

    xTigerRebornx

    It might help with that too, as it could of had problems when doing parseInt and subtracting/checking the points when you click
     
  19. Offline

    PieMan456

    xTigerRebornx
    The currency now works and everything but the buying is not working.
     
  20. Offline

    xTigerRebornx

    Do what I said for you to do before, if its not working, and there are no errors, put debug messages each step, and see whats messing up
     
  21. Offline

    PieMan456

    xTigerRebornx
    I did everything goes through fine. I think it's because of like lag or somethine. When I click it once it does nothing, 5 times it gives me like 16 when it is supposed to give me 10 for each click.
     
  22. Offline

    xTigerRebornx

  23. Offline

    PieMan456

    xTigerRebornx
    Code:java
    1. package me.pieman.godtokeneco;
    2.  
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.block.Sign;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.block.Action;
    14. import org.bukkit.event.block.SignChangeEvent;
    15. import org.bukkit.event.player.PlayerInteractEvent;
    16. import org.bukkit.event.player.PlayerJoinEvent;
    17. import org.bukkit.inventory.Inventory;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. public class GodTokenEco extends JavaPlugin implements Listener {
    22.  
    23. public void onEnable(){
    24. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    25. getConfig().options().copyDefaults(true);
    26. saveConfig();
    27. }
    28.  
    29. public void onDisable(){
    30. saveConfig();
    31. }
    32.  
    33. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    34. if(!(sender instanceof Player)){
    35. sender.sendMessage(ChatColor.RED + "Only players can use this command!");
    36. return true;
    37. }
    38.  
    39. Player p = (Player) sender;
    40. if(cmd.getName().equalsIgnoreCase("gtokens")){
    41. if(args.length == 0){
    42. p.sendMessage(ChatColor.RED + "Usage: /gtokens bal");
    43. return true;
    44. }
    45. if(args[0].equalsIgnoreCase("bal")){
    46. if(p.hasPermission("gtokens.bal")){
    47. int cash = getConfig().getInt(p.getName());
    48. p.sendMessage(ChatColor.GREEN + "GodTokens Balance: " + ChatColor.DARK_PURPLE + cash);
    49. return true;
    50. } else {
    51. p.sendMessage(ChatColor.RED + "You do not have permission!");
    52. return true;
    53. }
    54. } if (args[0].equalsIgnoreCase("give")){
    55. if(p.hasPermission("gtokens.give")){
    56. Player target = Bukkit.getServer().getPlayer(args[1]);
    57. if(target == null){
    58. p.sendMessage(ChatColor.RED + "That player is not online!");
    59. return true;
    60. }
    61. int dood = Integer.parseInt(args[2]);
    62. int cash = getConfig().getInt(target.getName());
    63. getConfig().set(target.getName(), cash + dood);
    64. saveConfig();
    65. p.sendMessage(ChatColor.GREEN + "Added " + dood + " GodTokens to " + target.getName() + "'s account!");
    66. return true;
    67. } else {
    68. p.sendMessage(ChatColor.RED + "You do not have permission!");
    69. return true;
    70. }
    71. }
    72. }
    73.  
    74. return true;
    75. }
    76.  
    77. @EventHandler
    78. public void onSignChange(SignChangeEvent e){
    79. Player p = e.getPlayer();
    80. if(!(e.getLines().length > 0)) return;
    81. if(!e.getLine(0).equalsIgnoreCase("[GodToken]")) return;
    82. if(e.getLines().length < 3){
    83. e.setLine(0, ChatColor.RED + "[GodToken]");
    84. p.sendMessage(ChatColor.RED + "A GodToken sign must have at least 4 lines!");
    85. return;
    86. }
    87. e.setLine(0, ChatColor.BLUE + "[GodToken]");
    88. }
    89.  
    90. @EventHandler
    91. public void onPlayerInteract(PlayerInteractEvent e){
    92. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    93. if(e.getClickedBlock().getType() == Material.SIGN){
    94. Sign s = (Sign) e.getClickedBlock().getState();
    95. if(s.getLine(0).equalsIgnoreCase(ChatColor.BLUE + "[GodToken]")){
    96. int id = Integer.parseInt(s.getLine(1));
    97. String lol = s.getLine(2);
    98. int id2 = Integer.parseInt(s.getLine(3));
    99. Player p = e.getPlayer();
    100. Inventory pi = p.getInventory();
    101. if(getConfig().getInt(p.getName()) < id2){
    102. p.sendMessage(ChatColor.RED + "Sorry you do not have enough GodTokens!");
    103. return;
    104. }
    105. Material m = Material.matchMaterial(lol);
    106. pi.addItem(new ItemStack(m, id));
    107. int cash = getConfig().getInt(p.getName());
    108. getConfig().set(p.getName(), cash - id2);
    109. saveConfig();
    110. }
    111. }
    112. if(e.getClickedBlock().getType() == Material.SIGN_POST){
    113. Sign s = (Sign) e.getClickedBlock().getState();
    114. if(s.getLine(0).equalsIgnoreCase(ChatColor.BLUE + "[GodToken]")){
    115. int id = Integer.parseInt(s.getLine(1));
    116. String lol = s.getLine(2);
    117. int id2 = Integer.parseInt(s.getLine(3));
    118. Player p = e.getPlayer();
    119. Inventory pi = p.getInventory();
    120. if(getConfig().getInt(p.getName()) < id2){
    121. p.sendMessage(ChatColor.RED + "Sorry you do not have enough GodTokens!");
    122. return;
    123. }
    124. Material m = Material.matchMaterial(lol);
    125. pi.addItem(new ItemStack(m, id));
    126. int cash = getConfig().getInt(p.getName());
    127. getConfig().set(p.getName(), cash - id2);
    128. saveConfig();
    129. }
    130. }
    131. if(e.getClickedBlock().getType() == Material.WALL_SIGN){
    132. Sign s = (Sign) e.getClickedBlock().getState();
    133. if(s.getLine(0).equalsIgnoreCase("[GodToken]")){
    134. int id = Integer.parseInt(s.getLine(1));
    135. String lol = s.getLine(2);
    136. int id2 = Integer.parseInt(s.getLine(3));
    137. Player p = e.getPlayer();
    138. Inventory pi = p.getInventory();
    139. if(getConfig().getInt(p.getName()) < id2){
    140. p.sendMessage(ChatColor.RED + "Sorry you do not have enough GodTokens!");
    141. return;
    142. }
    143. Material m = Material.matchMaterial(lol);
    144. pi.addItem(new ItemStack(m, id));
    145. int cash = getConfig().getInt(p.getName());
    146. getConfig().set(p.getName(), cash - id2);
    147. saveConfig();
    148. }
    149. }
    150. }
    151. }
    152.  
    153. @EventHandler
    154. public void onPlayerJoin(PlayerJoinEvent e){
    155. Player p = e.getPlayer();
    156. if(getConfig().get(p.getName()) == null){
    157. int cash = 0;
    158. getConfig().set(p.getName(), cash);
    159. saveConfig();
    160. }
    161. }
    162. }
    163.  
     
  24. Offline

    xTigerRebornx

    After you give them the ItemStack and save, put a return;
    It might help with the lag, since it wont go through the rest of the code (I believe)

    And when you give them the ItemStack, i'd print a message of the details of what they got (I.E. "you have got [ammount [item] for [price]" PieMan456
     
  25. Offline

    PieMan456

    xTigerRebornx
    Ok it sends the message and everything but it still only gives you the item after you click the sign like 10 times anit only gives you like 1/3 of the amount you are supposed to get.
     
  26. Offline

    xTigerRebornx

    PieMan456 I will compile and test it on my computer, and see if I can find the problem

    How do the signs work?
     
  27. Offline

    PieMan456

    xTigerRebornx
    Almost exactly like essentials signs. Except [Buy] is [GodToken]
     
  28. Offline

    xTigerRebornx

    PieMan456 Okay, so somehow, I got it to work, and what I did and how it works makes no sense whatsoever.
    I set Material m to a random Material instead of Material.matchMaterial(lol), and that somehow fixed it.....
    I am confused

    PieMan456 I have figured out how I fixed it. I did what I mentioned above, but only for the code that checks if it is a Material.SIGN

    xTigerRebornx My guess is that it was freezing up somewhere in that section.
    Oh, and I would add a cooldown to clicking the signs, holding it down will lag the hell out of your server

    PieMan456 Tagged myself....seems legit

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

    keensta

    PieMan456
    To fix the sign glitching and not giving you the stuff I forgot to mention IT DOES give you stuff but you inventory doesn't update properly so you don't see it until you move the stacks so just do "player.updateInventory();" that will fix the problem

    Secondly what xTigerRebornx the server will lag if you hold down the button well if you take a look here army interact event https://github.com/keensta/XpBank/blob/master/src/com/andi/xpbank/listeners/XpBankInteract.java you'll notice I have put all the sign types on one line like I showed you to do. Doing this cuts down on the checks especially when all the checks have the same code you have repeated code for no reason.

    You'll see I have that one event and check that handles xp taking and giving updating the signs display and so on this block of code runs smooth even when you hold you right click down to withdraw loads.
     
  30. Offline

    PieMan456

Thread Status:
Not open for further replies.

Share This Page