Worldguard pr.getId().startsWith return false

Discussion in 'Plugin Development' started by splint, Jun 22, 2014.

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

    splint

    Hi all, for my plugin, I use a Worldguard listener to check if the player was in a region that start with "music_"

    For that, I use
    Code:java
    1. if (pr.getId().startsWith("music_") == true && regionname == pr.getId()) {

    That work, but only if the player was in one region.

    If the player was in the region (For exemple) Spawn and music_spawn at the same time, that will not work, the condition
    Code:java
    1. (pr.getId().startsWith("music_")
    will return false.


    How can I do to do that ?
    Thank.

    up

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

    mazentheamazin

    splint
    I'm not familiar with WorldGuard, however, if possible iterate through the regions and if the regions id starts with "music_" then do what you want with the player.
     
  3. Offline

    splint

    But I don't know how to do that too
     
  4. Offline

    mazentheamazin

    splint
    You do not know how to iterate over a list? I have pretty much confirmed that you don't know Java. Please learn Java before going into the Bukkit API, or any API for that matter.
     
  5. Offline

    gokarter24

    Please post more of your code so I can help you cause I have no clue what pr is. splint
     
  6. Offline

    splint

    This is all my Listener code...

    Code:java
    1. package com.minecamp.spouties;
    2.  
    3. import java.util.Iterator;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.EventPriority;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerMoveEvent;
    12. import org.getspout.spoutapi.SpoutManager;
    13. import org.getspout.spoutapi.player.SpoutPlayer;
    14. import org.getspout.spoutapi.sound.SoundManager;
    15.  
    16. import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
    17. import com.sk89q.worldguard.protection.ApplicableRegionSet;
    18. import com.sk89q.worldguard.protection.regions.ProtectedRegion;
    19.  
    20. public class EnteringRegion implements Listener {
    21. Spouties plugin;
    22. public EnteringRegion(Spouties instance) {
    23. plugin = instance;
    24. }
    25. public static int c = 0;
    26. public static int spawnonit;
    27. public static String regionname;
    28. public static int loop1;
    29. public static int b = 0;
    30. public static String i = null;
    31. public static int enter = 0;
    32. public static int time;
    33. public static String url;
    34. public static String region = null;
    35. private WorldGuardPlugin getwg() {
    36. WorldGuardPlugin pl = (WorldGuardPlugin) Bukkit.getPluginManager().getPlugin("WorldGuard");
    37. if((WorldGuardPlugin) Bukkit.getPluginManager().getPlugin("WorldGuard") != null){
    38. return ((WorldGuardPlugin)pl);
    39. }
    40. else
    41. {
    42. }
    43. return null;}
    44.  
    45. @SuppressWarnings("deprecation")
    46. @EventHandler(priority = EventPriority.NORMAL)
    47. public void onPlayerMove(PlayerMoveEvent event) {
    48. final Player p = event.getPlayer();
    49. if (p.getWorld().getName().equalsIgnoreCase("world") == true) {
    50. ApplicableRegionSet ar = this.getwg().getRegionManager(p.getWorld()).getApplicableRegions(p.getLocation());
    51. Iterator<ProtectedRegion> prs = ar.iterator();
    52. if(prs.hasNext() == false)
    53. {enter = 0;
    54. b = 0;
    55. c = 0;
    56. if (Spouties.musiccommand == 0){
    57. SoundManager music = SpoutManager.getSoundManager();
    58. music.stopMusic((SpoutPlayer) p, true);
    59. Bukkit.getScheduler().cancelTask(loop1);}}
    60. while (prs.hasNext()) {
    61. ProtectedRegion pr = prs.next();
    62.  
    63. if (pr.getId().startsWith("music_") == true && regionname == pr.getId()) {
    64. String i = pr.getId();
    65. if (i != null){
    66. enter = 1;
    67. url = plugin.getConfig().getString("url." + i);
    68. String timestring = plugin.getConfig().getString("time." + i);
    69. if (timestring != null){
    70. time = Integer.parseInt(timestring);
    71. time = time*20;
    72. }
    73. if (b==0){
    74. b = 1;
    75. if (url != null && timestring !=null) {
    76. regionname = pr.getId();
    77. loop1 = plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {
    78. public void run() {
    79. SoundManager music = SpoutManager.getSoundManager();
    80. music.stopMusic((SpoutPlayer)p, true, 1000);
    81. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    82. public void run() {
    83. SoundManager music = SpoutManager.getSoundManager();
    84. Bukkit.getScheduler().cancelTask(Spouties.loop);
    85. music.playCustomMusic(plugin, (SpoutPlayer)p, url, true);
    86. Spouties.musiccommand = 0;
    87. }
    88. }, 20);
    89.  
    90. }
    91. }
    92. , 0L, time);
    93. }else{
    94. enter = 0;
    95. b = 0;
    96. if (Spouties.musiccommand == 0){
    97. SoundManager music = SpoutManager.getSoundManager();
    98. music.stopMusic((SpoutPlayer)p);
    99. Bukkit.getScheduler().cancelTask(loop1);}}
    100. p.sendMessage(ChatColor.RED + "bad");
    101. }
    102. }
    103. }
    104.  
    105. else {
    106. regionname = pr.getId();
    107. enter = 0;
    108. b = 0;
    109. if (Spouties.musiccommand == 0){
    110. SoundManager music = SpoutManager.getSoundManager();
    111. music.stopMusic((SpoutPlayer)p);
    112. p.sendMessage(ChatColor.RED + "bad1");
    113. Bukkit.getScheduler().cancelTask(loop1);}}
    114.  
    115.  
    116. if (pr.getId().startsWith("music_") == true && spawnonit == 0) {
    117. spawnonit = 1;
    118. String i = pr.getId();
    119. if (i != null){
    120. enter = 1;
    121. url = plugin.getConfig().getString("url." + i);
    122. String timestring = plugin.getConfig().getString("time." + i);
    123. if (timestring != null){
    124. time = Integer.parseInt(timestring);
    125. time = time*20;
    126. }
    127. if (b==0){
    128. b = 1;
    129. if (url != null && timestring !=null) {
    130. regionname = pr.getId();
    131.  
    132. loop1 = plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {
    133. public void run() {
    134. SoundManager music = SpoutManager.getSoundManager();
    135. music.stopMusic((SpoutPlayer)p, true, 1000);
    136. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    137. public void run() {
    138. SoundManager music = SpoutManager.getSoundManager();
    139. Bukkit.getScheduler().cancelTask(Spouties.loop);
    140. music.playCustomMusic(plugin, (SpoutPlayer)p, url, true);
    141. Spouties.musiccommand = 0;
    142. }
    143. }, 20);
    144.  
    145. }
    146. }
    147. , 0L, time);
    148. }
    149. else{
    150. enter = 0;
    151. if (Spouties.musiccommand == 0){
    152. SoundManager music = SpoutManager.getSoundManager();
    153. music.stopMusic((SpoutPlayer)p);
    154. Bukkit.getScheduler().cancelTask(loop1);}}
    155. }
    156. }
    157. }
    158. else if (pr.getId().startsWith("se_")){
    159. String i = pr.getId();
    160. if (i != null){
    161. url = plugin.getConfig().getString("url." + i);
    162. if (c==0){
    163. c = 1;
    164. if (url != null){
    165. SoundManager music = SpoutManager.getSoundManager();
    166. music.playCustomSoundEffect(plugin, (SpoutPlayer)p, url, plugin.getConfig().getBoolean("notify.playeffect"));
    167. }
    168. else{}
    169. }
    170. }
    171. }
    172. else{
    173. c = 0;
    174. }
    175. }
    176. }
    177. }
    178.  
    179. }


    If I ask how to do that and you answer "Learn it", what the utility of the forum ?
     
  7. Offline

    Shevchik

    Stop comparing strings using ==.
     
  8. Offline

    splint

    Now I use .equals() but this will not solve my problem :(
     
  9. splint You're right, there's no point fixing problems, you have a different problem.

    Also, comparing two strings with == can very easily return false when you're expecting true. How do you know that it's not what's causing your problem?

    Either way, as has been said, please learn Java or else you will run into simple problems all the time. Trust me. :)
     
  10. Offline

    splint

    AdamQpzm
    I know why this return false, but I don't know how to fix it.

    When I'm in 2 regions on the same time, pr.getId() will return a list like
    - music_spawn
    - spawn

    and if I check with .startWith, worldguard return false and not true, because they don't have only one region.

    I try with pr.getId().contains, but same result
     
  11. splint But you're iterating through the regions? Also, you don't "know" since you aren't a java runtime environment and you have no debug messages - for all you know the startsWith() check could've been returning true, but the == could have been returning false. Please learn Java, it'll help, and should stop you from using the bad practices you're using at the moment. :)

    splint Look, that's the problem you have. Do you think you're making a plugin? You're not, really. You're mostly copying from other sources without understanding what it means or what it does. To prove that point... you ask if you should iterate over it? Well, what do you think prs is in your code? It's an iterator!

    Please learn Java before using the Bukkit API, because otherwise you will encounter problems that you don't know how to solve, and the extend of your abilities will be mostly copied code.

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

    splint

    AdamQpzm
    I know prs was an iterator, I just see it, and i'm trying to work with it.
    I learn java by doing some stuff, test some fonctions, etc. I do that just for myself, for my server, and I don't really want to learn all the java methods for years, because I work just for this little project. I take some code, here, because I don't know how to make the worldguard listener, and I try to understand how this function work, etc.
    Every time I ask something, everybody say "Go learn java". That s**k.
     
  13. What word is this supposed to be? o.o

    Also, the reason people tell you to learn Java, regardless of whether or not you like it, is because you have to know Java in order to make things work. Let's assume that poems could only be written in English, as Bukkit plugins can only be written in Java - would you learn how to write a poem before learning how to write (at least the basics of) English? No, you wouldn't. Java & Bukkit is no different.

    As for your problem, it might be caused by the fact you never terminate the loop early, even if it finds such a region.
     
Thread Status:
Not open for further replies.

Share This Page