Solved Mathematical Problem?!

Discussion in 'Plugin Development' started by SourceForums, Dec 25, 2013.

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

    SourceForums

    Hey there, I'm currently developing a certain mini-game. It's a parkour mini-game that gets you faster every #00 z coordinates. Every time a someone clears a NEW stage (Every 100 z blocks are 1 stage/), it's supposed to send a player a message and log their checkpoint. I've coded as below but even though the config is saved properly with the correct checkpoint, etc., the game recognises the player as if it's its first time clearing a stage and spams the message even though the player has already finished the stage before. I believe this is a mathematical problem and I've been trying to fix it for the past couple of weeks but was unable to. Here's what I've got:
    Code:java
    1. @EventHandler
    2. public void onSpeedChange(PlayerMoveEvent event){
    3. SourceCraftManager pluginManager = new SourceCraftManager(plugin);
    4. Player player = event.getPlayer();
    5. FileConfiguration configuration = pluginManager.getUserdata(player);
    6. Location location = player.getLocation();
    7. int maxValue = Integer.MAX_VALUE;
    8. int locationZ = (int) location.getZ();
    9. int checkpointValue = configuration.getInt("Game.FP.Checkpoint");
    10. int creditsValue = configuration.getInt("General.Credits");
    11. int speedValue = configuration.getInt("Games.FP.Checkpoint") - 1;
    12. ScoreboardManager boardManager = Bukkit.getScoreboardManager();
    13. Scoreboard scoreboard = boardManager.getNewScoreboard();
    14. Objective objective = scoreboard.registerNewObjective("scoreboard", "dummy");
    15. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    16. objective.setDisplayName("" + ChatColor.UNDERLINE + ChatColor.RED + "Flash Stats");
    17. Score stage = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Stage: "));
    18. stage.setScore(checkpointValue);
    19. Score speed = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.RED + "Speed (FpS): "));
    20. speed.setScore(speedValue);
    21. if(location.getWorld().getName().equals("FP-Arena")){
    22. if(locationZ % 100 == 0){
    23. if(locationZ > 0){
    24. if(checkpointValue < locationZ / 100 + 2){
    25. player.sendMessage(ChatColor.GOLD + "You have recieved +1 credit for finishing a stage!");
    26. configuration.set("General.Credits", creditsValue + 1);
    27. configuration.set("Games.FP.Checkpoint", locationZ / 100 + 1);
    28. player.playSound(location, Sound.LEVEL_UP, 1F, 10F);
    29. pluginManager.saveUserdata(player);
    30. }
    31. player.getWorld().playEffect(location,Effect.POTION_BREAK, 1);
    32. for(PotionEffect effect : player.getActivePotionEffects()){
    33. player.removePotionEffect(effect.getType());
    34. } //PotionEffect loop
    35. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, maxValue, locationZ / 100 - 1));
    36. pluginManager.saveUserdata(player);
    37. player.setScoreboard(scoreboard);
    38. } //locationZ check
    39. } //locationZ divisibility 100 check
    40. } //Player world check
    41. } //onSpeedChange
    42.  
    43. @EventHandler
    44. public void onFall(PlayerMoveEvent event){
    45. Player player = event.getPlayer();
    46. SourceCraftManager pluginManager = new SourceCraftManager(plugin);
    47. FileConfiguration configuration = pluginManager.getUserdata(player);
    48. Location location = player.getLocation();
    49. World world = location.getWorld();
    50. int locationY = (int) location.getY();
    51. int maxValue = Integer.MAX_VALUE;
    52. int stageValue = configuration.getInt("Games.FP.Checkpoint");
    53. int speedValue = configuration.getInt("Games.FP.Checkpoint") - 1;
    54. ScoreboardManager boardManager = Bukkit.getScoreboardManager();
    55. Scoreboard scoreboard = boardManager.getNewScoreboard();
    56. Objective objective = scoreboard.registerNewObjective("scoreboard", "dummy");
    57. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    58. objective.setDisplayName("" + ChatColor.UNDERLINE + ChatColor.RED + "Flash Stats");
    59. Score stage = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Stage: "));
    60. stage.setScore(stageValue);
    61. Score speed = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.RED + "Speed (FpS): "));
    62. speed.setScore(speedValue);
    63. if(location.getWorld().getName().equals("FP-Arena")){
    64. if(locationY < 40){
    65. if(configuration.getInt("Games.FP.Checkpoint") == 1){
    66. player.teleport(new Location(world, 0, 50, 0));
    67. for(PotionEffect effect : player.getActivePotionEffects()){
    68. player.removePotionEffect(effect.getType());
    69. } //PotionEffect loop
    70. player.setScoreboard(scoreboard);
    71. }else if(configuration.getInt("Games.FP.Checkpoint") > 1){
    72. player.teleport(new Location(world, 0, 50, speedValue * 100));
    73. Location newLocation = player.getLocation();
    74. int locationZ = (int) newLocation.getZ();
    75. player.getWorld().playEffect(newLocation,Effect.POTION_BREAK, 1);
    76. for(PotionEffect effect : player.getActivePotionEffects()){
    77. player.removePotionEffect(effect.getType());
    78. } //PotionEffect loop
    79. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, maxValue, locationZ / 100 - 1));
    80. player.setScoreboard(scoreboard);
    81. } //FP Checkpoint check
    82. } //locationY check
    83. } //Player world check
    84. } //onFall

    Again, everything related to saving and getting the config is working perfectly. Please reply ASAP. Thanks!
     
  2. Offline

    Developing

    SourceForums You are only allowed to bump your thread every 24 hours.
     
  3. Offline

    SourceForums

    Developing
    Yeah I know, that was technically the first time in 24 hours on this thread.
     
  4. Offline

    ThunderWaffeMC

    It's every 12 hours not 24.
     
  5. Offline

    SourceForums

    Guys, I've fixed the code a little bit but it still doesn't work:
    Code:java
    1. @EventHandler
    2. public void onSpeedChange(PlayerMoveEvent event){
    3. SourceCraftManager pluginManager = new SourceCraftManager(plugin);
    4. Player player = event.getPlayer();
    5. FileConfiguration configuration = pluginManager.getUserdata(player);
    6. Location location = player.getLocation();
    7. int maxValue = Integer.MAX_VALUE;
    8. int locationZ = (int) location.getZ();
    9. int checkpointValue = configuration.getInt("Game.FP.Checkpoint");
    10. int creditsValue = configuration.getInt("General.Credits");
    11. int speedValue = configuration.getInt("Games.FP.Checkpoint") - 1;
    12. int newCheckpointValue = locationZ / 100 + 1;
    13. ScoreboardManager boardManager = Bukkit.getScoreboardManager();
    14. Scoreboard scoreboard = boardManager.getNewScoreboard();
    15. Objective objective = scoreboard.registerNewObjective("scoreboard", "dummy");
    16. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    17. objective.setDisplayName("" + ChatColor.UNDERLINE + ChatColor.RED + "Flash Stats");
    18. Score stage = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Stage: "));
    19. stage.setScore(checkpointValue);
    20. Score speed = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.RED + "Speed (FpS): "));
    21. speed.setScore(speedValue);
    22. if(location.getWorld().getName().equals("FP-Arena")){
    23. if(locationZ % 100 == 0){
    24. if(locationZ > 0){
    25. if(checkpointValue < newCheckpointValue){
    26. player.sendMessage(ChatColor.GOLD + "You have recieved +1 credit for finishing a stage!");
    27. configuration.set("General.Credits", creditsValue + 1);
    28. configuration.set("Games.FP.Checkpoint", newCheckpointValue);
    29. player.playSound(location, Sound.LEVEL_UP, 1F, 10F);
    30. pluginManager.saveUserdata(player);
    31. } //Player checkpoint check
    32. player.getWorld().playEffect(location,Effect.POTION_BREAK, 1);
    33. for(PotionEffect effect : player.getActivePotionEffects()){
    34. player.removePotionEffect(effect.getType());
    35. } //PotionEffect loop
    36. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, maxValue, locationZ / 100 - 1));
    37. pluginManager.saveUserdata(player);
    38. player.setScoreboard(scoreboard);
    39. } //locationZ check
    40. } //locationZ divisibility 100 check
    41. } //Player world check
    42. } //onSpeedChange
    43.  
    44. @EventHandler
    45. public void onFall(PlayerMoveEvent event){
    46. Player player = event.getPlayer();
    47. SourceCraftManager pluginManager = new SourceCraftManager(plugin);
    48. FileConfiguration configuration = pluginManager.getUserdata(player);
    49. Location location = player.getLocation();
    50. World world = location.getWorld();
    51. int locationY = (int) location.getY();
    52. int maxValue = Integer.MAX_VALUE;
    53. int stageValue = configuration.getInt("Games.FP.Checkpoint");
    54. int speedValue = configuration.getInt("Games.FP.Checkpoint") - 1;
    55. ScoreboardManager boardManager = Bukkit.getScoreboardManager();
    56. Scoreboard scoreboard = boardManager.getNewScoreboard();
    57. Objective objective = scoreboard.registerNewObjective("scoreboard", "dummy");
    58. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    59. objective.setDisplayName("" + ChatColor.UNDERLINE + ChatColor.RED + "Flash Stats");
    60. Score stage = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Stage: "));
    61. stage.setScore(stageValue);
    62. Score speed = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.RED + "Speed (FpS): "));
    63. speed.setScore(speedValue);
    64. if(location.getWorld().getName().equals("FP-Arena")){
    65. if(locationY < 40){
    66. if(configuration.getInt("Games.FP.Checkpoint") == 1){
    67. player.teleport(new Location(world, 0, 50, 0));
    68. for(PotionEffect effect : player.getActivePotionEffects()){
    69. player.removePotionEffect(effect.getType());
    70. } //PotionEffect loop
    71. player.setScoreboard(scoreboard);
    72. }else if(configuration.getInt("Games.FP.Checkpoint") > 1){
    73. player.teleport(new Location(world, 0, 50, speedValue * 100));
    74. Location newLocation = player.getLocation();
    75. int locationZ = (int) newLocation.getZ();
    76. player.getWorld().playEffect(newLocation,Effect.POTION_BREAK, 1);
    77. for(PotionEffect effect : player.getActivePotionEffects()){
    78. player.removePotionEffect(effect.getType());
    79. } //PotionEffect loop
    80. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, maxValue, locationZ / 100 - 1));
    81. player.setScoreboard(scoreboard);
    82. } //FP Checkpoint check
    83. } //locationY check
    84. } //Player world check
    85. } //onFall

    Can someone please help? ;( Please reply ASAP.
     
  6. Offline

    CeramicTitan

    Any errors?
     
  7. Offline

    SourceForums

    CeramicTitan
    Nope. This is not a coding error I believe. This is the problem:
     
  8. Offline

    xTigerRebornx

    SourceForums Which message is it spamming exactly? I am not sure what "the message" is
     
  9. Offline

    SourceForums

    xTigerRebornx
    This part:
    Code:java
    1. if(checkpointValue < newCheckpointValue){
    2. player.sendMessage(ChatColor.GOLD + "You have recieved +1 credit for finishing a stage!");
    3. configuration.set("General.Credits", creditsValue + 1);
    4. configuration.set("Games.FP.Checkpoint", newCheckpointValue);
    5. player.playSound(location, Sound.LEVEL_UP, 1F, 10F);
    6. pluginManager.saveUserdata(player);
    7. } //Player checkpoint check
     
  10. Offline

    xTigerRebornx

    SourceForums Your math logics confuse me.... :p
    Its most likely a problem with you setting "newCheckpointValue" that causes it to always be greater then checkpointValue
     
  11. Offline

    SourceForums

    xTigerRebornx
    I was able to figure something like that too. I've been working on just these 2 events for weeks now but was unable to find out how to fix it. That's why I'm asking for you guys' help.
     
  12. Offline

    xTigerRebornx

    SourceForums I'd add some debug messages after changing the values, printing the values, seeing what is wrong when they change, and change your math logic to try and fix the values not being what you want them to be
     
  13. Offline

    SourceForums

    xTigerRebornx
    Tried that before but all the values are just how I expect them to be like.
     
  14. Offline

    xTigerRebornx

    SourceForums If they were what you expect them to be, then you wouldn't exactly be asking for help here, would you? XD (Just pointing that out)

    Anyways, they may be like you expect them to be, but they may not be what you math's logic is made to interpret properly, try doing the debug again, and put the new debug code on here, with the results
     
  15. Offline

    xTrollxDudex

  16. Offline

    SourceForums

    xTigerRebornx
    xTrollxDudex
    I've got the result from the debugging and it's not the result I expected. (I logged things when they steps into the 2nd stage from the 1st.) Here's where I've placed in the debug messages:
    Code:java
    1. @EventHandler
    2. public void onSpeedChange(PlayerMoveEvent event){
    3. SourceCraftManager pluginManager = new SourceCraftManager(plugin);
    4. Player player = event.getPlayer();
    5. FileConfiguration configuration = pluginManager.getUserdata(player);
    6. Location location = player.getLocation();
    7. int maxValue = Integer.MAX_VALUE;
    8. int locationZ = (int) location.getZ();
    9. int checkpointValue = configuration.getInt("Game.FP.Checkpoint");
    10. int creditsValue = configuration.getInt("General.Credits");
    11. int speedValue = configuration.getInt("Games.FP.Checkpoint") - 1;
    12. int newCheckpointValue = locationZ / 100 + 1;
    13. ScoreboardManager boardManager = Bukkit.getScoreboardManager();
    14. Scoreboard scoreboard = boardManager.getNewScoreboard();
    15. Objective objective = scoreboard.registerNewObjective("scoreboard", "dummy");
    16. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    17. objective.setDisplayName("" + ChatColor.UNDERLINE + ChatColor.RED + "Flash Stats");
    18. Score stage = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + "Stage: "));
    19. stage.setScore(checkpointValue);
    20. Score speed = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.RED + "Speed (FpS): "));
    21. speed.setScore(speedValue);
    22. if(location.getWorld().getName().equals("FP-Arena")){
    23. player.sendMessage(ChatColor.RED + "locationZ: " + locationZ);
    24. player.sendMessage(ChatColor.RED + "checkpointValue: " + checkpointValue);
    25. player.sendMessage(ChatColor.RED + "speedValue: " + speedValue);
    26. player.sendMessage(ChatColor.RED + "newCheckpointValue: " + newCheckpointValue);
    27. if(locationZ % 100 == 0){
    28. if(locationZ > 0){
    29. if(checkpointValue < newCheckpointValue){
    30. player.sendMessage(ChatColor.YELLOW + "locationZ: " + locationZ);
    31. player.sendMessage(ChatColor.YELLOW + "checkpointValue: " + checkpointValue);
    32. player.sendMessage(ChatColor.YELLOW + "speedValue: " + speedValue);
    33. player.sendMessage(ChatColor.YELLOW + "newCheckpointValue: " + newCheckpointValue);
    34. player.sendMessage(ChatColor.GOLD + "You have recieved +1 credit for finishing a stage!");
    35. configuration.set("General.Credits", creditsValue + 1);
    36. configuration.set("Games.FP.Checkpoint", newCheckpointValue);
    37. player.playSound(location, Sound.LEVEL_UP, 1F, 10F);
    38. pluginManager.saveUserdata(player);
    39. player.sendMessage(ChatColor.GREEN + "locationZ: " + locationZ);
    40. player.sendMessage(ChatColor.GREEN + "checkpointValue: " + checkpointValue);
    41. player.sendMessage(ChatColor.GREEN + "speedValue: " + speedValue);
    42. player.sendMessage(ChatColor.GREEN + "newCheckpointValue: " + newCheckpointValue);
    43. } //Player checkpoint check
    44. player.getWorld().playEffect(location,Effect.POTION_BREAK, 1);
    45. for(PotionEffect effect : player.getActivePotionEffects()){
    46. player.removePotionEffect(effect.getType());
    47. } //PotionEffect loop
    48. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, maxValue, locationZ / 100 - 1));
    49. pluginManager.saveUserdata(player);
    50. player.setScoreboard(scoreboard);
    51. player.sendMessage(ChatColor.BLUE + "locationZ: " + locationZ);
    52. player.sendMessage(ChatColor.BLUE + "checkpointValue: " + checkpointValue);
    53. player.sendMessage(ChatColor.BLUE + "speedValue: " + speedValue);
    54. player.sendMessage(ChatColor.BLUE + "newCheckpointValue: " + newCheckpointValue);
    55. } //locationZ check
    56. } //locationZ divisibility 100 check
    57. } //Player world check
    58. } //onSpeedChange

    And this is the result I've got:
    Code:
    RED:
    locationZ: 100
    checkpointValue: 0
    speedValue: 1
    newCheckpointValue: 2
    YELLOW:
    locationZ: 100
    checkpointValue: 0
    speedValue: 1
    newCheckpointValue: 2
    GREEN:
    locationZ: 100
    checkpointValue: 0
    speedValue: 1
    newCheckpointValue: 2
    BLUE:
    locationZ: 100
    checkpointValue: 0
    speedValue: 1
    newCheckpointValue: 2
    According to the results, the checkpointValue is 0. That's already an error. Here's what the player userdata/config looks like:
    Code:java
    1. General:
    2. Name: SourceForums
    3. Display Name: SourceForums
    4. Rank: High Administrator
    5. Warnings: 0
    6. Credits: 1077
    7. Games:
    8. E-SG:
    9. Wins: 0
    10. Loses: 0
    11. Draws: 0
    12. Kills: 0
    13. Deaths: 0
    14. SCBB:
    15. Wins: 0
    16. Loses: 0
    17. Draws: 0
    18. Kills: 0
    19. Deaths: 0
    20. HTPC:
    21. Wins: 0
    22. Loses: 0
    23. Draws: 0
    24. Kills: 0
    25. Deaths: 0
    26. APVP:
    27. Wins: 0
    28. Loses: 0
    29. Draws: 0
    30. Kills: 0
    31. Deaths: 0
    32. FP:
    33. Checkpoint: 1
    34. Economy:
    35. General: {}
    36. E-SG:
    37. Mutation: 0
    38. Maps: 0
    39. SCBB:
    40. Class Upgrades: 1
    41. Maps: 0
    42. HTPC:
    43. Class Upgrades: 1
    44. Maps: 0
    45. APVP:
    46. Maps: 0
    47.  

    As you can see, the path Games.FP.Checkpoint should be 1 not 0. The config DOES save properly and this have been tested. All the values change and save when they step into the new stage but the code just ignores them pretty much. (It CAN read it. I have a scoreboard that displays the value of the path General.Credits and it just works perfectly, exactly same to the value on the YAML) I appologise if this is WAY to confusing but it's something I MUST get through. Please reply ASAP.
     
  17. Offline

    xTrollxDudex

    SourceForums
    You ciuld be grabbing the balue of the configuration before it is saved, returning 0.
     
  18. Offline

    SourceForums

    xTrollxDudex
    What's wrong with getting values before saving?
     
  19. Offline

    xTrollxDudex

    SourceForums
    The value isn't set to the one you want, here's what's happening:

    Get int(0) -> Save file(1) -> Open file(1)
    KEY: Step(value of the field)

    See what's happening? The file's default value of 0 is being returned before it could save a value of 1; you see it as 1, but that is after the file changed, which came after the the value was read from the file.
     
  20. Offline

    SourceForums

  21. Offline

    xTrollxDudex

    SourceForums
    Wrong path. It's supposed to be "Games.FP.Checkpoint"

    What you're doing is "Game.FP.Checkpoint". See the missing s?
     
  22. Offline

    SourceForums

    xTrollxDudex
    I'll have to test if that's the only problem but would you mind just killing me for that? Wow, I just...yeah. Thanks, I'll test this ASAP.
     
  23. Offline

    xTrollxDudex

  24. Offline

    SourceForums

    xTrollxDudex
    Yay! Everything worked EXACTLY how I wished it to. But since I'm asking something here, I'll ask another another question. Is there a way to make some particles only visible to one player? If there is, how? Again, thank you so much, you're a lifesaver! Please reply ASAP.
     
  25. Offline

    xTrollxDudex

  26. Offline

    SourceForums

    xTrollxDudex
    I've read through your tutorial but I still do not get it really. I get what packets are, etc. But I have no idea on how I would achieve my goal with it. Still, the problem on this thread is solved so I'll ask the question there.
     
Thread Status:
Not open for further replies.

Share This Page