Solved Help please, code wont reach what it should.

Discussion in 'Plugin Development' started by MineoxFX, Aug 30, 2013.

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

    MineoxFX

    Hi there, im trying to make a minigame and i coded this

    Code:java
    1. package com.devrosemberg.passthevirus.utils;
    2.  
    3. import java.io.ByteArrayOutputStream;
    4. import java.io.DataOutputStream;
    5. import java.io.IOException;
    6. import java.util.List;
    7. import java.util.Random;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.Color;
    11. import org.bukkit.FireworkEffect;
    12. import org.bukkit.GameMode;
    13. import org.bukkit.Location;
    14. import org.bukkit.Material;
    15. import org.bukkit.World;
    16. import org.bukkit.entity.Entity;
    17. import org.bukkit.entity.EntityType;
    18. import org.bukkit.entity.Firework;
    19. import org.bukkit.entity.Player;
    20. import org.bukkit.inventory.ItemStack;
    21. import org.bukkit.inventory.meta.FireworkMeta;
    22. import org.bukkit.potion.PotionEffect;
    23. import org.bukkit.potion.PotionEffectType;
    24. import org.bukkit.scheduler.BukkitRunnable;
    25.  
    26. import com.devrosemberg.passthevirus.PassTheVirus;
    27.  
    28. public class GameUtil {
    29.  
    30. private PassTheVirus plugin;
    31. private int time;
    32.  
    33. public GameUtil(PassTheVirus instance)
    34. {
    35. this.plugin = instance;
    36. this.time = this.plugin.getConfig().getInt("AutostartCountdown");
    37. }
    38.  
    39. public void startGame() {
    40. if ((!this.plugin.progress.contains("On")) &&
    41. (Bukkit.getOnlinePlayers().length > 1)) {
    42. ChatUtils.broadcast("WHY");
    43. for (Player s : Bukkit.getOnlinePlayers()) {
    44. if(ListUtils.hasvirus.contains(s)) {
    45. ChatUtils.broadcast("Got Here");
    46. prepVirus(s);
    47. Location hasvirusl = plugin.getLocation("hasvirusspawn");
    48. s.teleport(hasvirusl);
    49. ChatUtils.send(s, "You are now the Virus Carrier");
    50. ChatUtils.send(s, "hit Someone to Pass the Virus to Him!");
    51. } else if (ListUtils.novirus.contains(s)){
    52. ChatUtils.broadcast("Now here");
    53. prepPlayer(s);
    54. Location novirusl = plugin.getLocation("novirusspawn");
    55. s.teleport(novirusl);
    56. ChatUtils.broadcast(ListUtils.hasvirus.toString() + " is now the Virus Carrier!");
    57. }
    58. }
    59. }
    60. this.plugin.progress.clear();
    61. this.plugin.progress.add("On");
    62. this.plugin.countdownInProgress.clear();
    63. this.plugin.countdownInProgress.add("Off");
    64. }
    65.  
    66.  
    67. public void endGame(){
    68. if (this.gameInProgress()) {
    69. this.plugin.progress.clear();
    70. this.plugin.progress.add("Off");
    71. for (final Player s : Bukkit.getOnlinePlayers()) {
    72. Location lobby = plugin.getLocation("lobby");
    73. unprepPlayer(s);
    74. s.teleport(lobby);
    75. this.createFirework(lobby);
    76. this.createFirework(s.getLocation());
    77. }
    78. ChatUtils.broadcast("Server will Restart in 15 seconds...");
    79. this.plugin.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable() {
    80. public void run() {
    81. try {
    82. out.writeUTF("Connect");
    83. out.writeUTF("hub");
    84. } catch (IOException e) {
    85. }
    86. for (Player s : Bukkit.getOnlinePlayers())
    87. s.sendPluginMessage(GameUtil.this.plugin, "BungeeCord", b.toByteArray());
    88. try
    89. {
    90. b.close();
    91. out.close();
    92. }
    93. catch (IOException e)
    94. {
    95. }
    96. }
    97. }
    98. , 300L);
    99. this.plugin.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, new Runnable() {
    100. public void run() {
    101. GameUtil.this.rollback();
    102. }
    103. }
    104. , 5L);
    105. }
    106. }
    107.  
    108. public void prepVirus(Player p) {
    109. p.getInventory().clear();
    110. p.getInventory().setArmorContents(null);
    111. p.setHealth(20.0D);
    112. p.setFoodLevel(20);
    113. p.setFireTicks(0);
    114. p.setVelocity(p.getVelocity().multiply(0));
    115. p.setGameMode(GameMode.SURVIVAL);
    116. p.setAllowFlight(false);
    117. p.setFlying(false);
    118. for (PotionEffect effect : p.getActivePotionEffects()) {
    119. p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, Integer.MAX_VALUE ,1));
    120. p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE ,0));
    121. }
    122. }
    123.  
    124. public void prepPlayer(Player p)
    125. {
    126. p.getInventory().clear();
    127. p.getInventory().setArmorContents(null);
    128. p.setHealth(20.0D);
    129. p.setFoodLevel(20);
    130. p.setFireTicks(0);
    131. p.setVelocity(p.getVelocity().multiply(0));
    132. p.setGameMode(GameMode.SURVIVAL);
    133. p.setAllowFlight(false);
    134. p.setFlying(false);
    135. for (PotionEffect effect : p.getActivePotionEffects()) {
    136. p.removePotionEffect(effect.getType());
    137. }
    138. }
    139.  
    140. public void unprepPlayer(Player p) {
    141. p.getInventory().clear();
    142. p.getInventory().setArmorContents(null);
    143. p.setHealth(20.0D);
    144. p.setFoodLevel(20);
    145. p.setFireTicks(0);
    146. p.setVelocity(p.getVelocity().multiply(0));
    147. p.setGameMode(GameMode.SURVIVAL);
    148. p.setAllowFlight(false);
    149. p.setFlying(false);
    150. for (PotionEffect effect : p.getActivePotionEffects()) {
    151. p.removePotionEffect(effect.getType());
    152. }
    153. for (Player s : Bukkit.getOnlinePlayers()) {
    154. p.showPlayer(s);
    155. }
    156. if (p.isInsideVehicle())
    157. p.getVehicle().remove();
    158. }
    159.  
    160. public boolean gameInProgress()
    161. {
    162. if (this.plugin.progress.contains("On")) {
    163. return true;
    164. }
    165. return false;
    166. }
    167.  
    168. public void createFirework(Location loc) {
    169. Firework f1 = (Firework)loc.getWorld().spawn(loc, Firework.class);
    170. Firework f2 = (Firework)loc.getWorld().spawn(loc, Firework.class);
    171. Firework f3 = (Firework)loc.getWorld().spawn(loc, Firework.class);
    172. Firework f4 = (Firework)loc.getWorld().spawn(loc, Firework.class);
    173. Firework f5 = (Firework)loc.getWorld().spawn(loc, Firework.class);
    174. FireworkMeta fMeta = f1.getFireworkMeta();
    175. fMeta.setPower(2);
    176. fMeta.addEffects(new FireworkEffect[] { FireworkEffect.builder().withColor(Color.AQUA).withFade(Color.GREEN).with(FireworkEffect.Type.BALL_LARGE).build() });
    177. f1.setFireworkMeta(fMeta);
    178. f2.setFireworkMeta(fMeta);
    179. f3.setFireworkMeta(fMeta);
    180. f4.setFireworkMeta(fMeta);
    181. f5.setFireworkMeta(fMeta);
    182. }
    183.  
    184. public void rollback() {
    185. this.plugin.getServer().dispatchCommand(this.plugin.getServer().getConsoleSender(), "mv delete ActualWorld");
    186. this.plugin.getServer().dispatchCommand(this.plugin.getServer().getConsoleSender(), "mvconfirm");
    187. this.plugin.getServer().dispatchCommand(this.plugin.getServer().getConsoleSender(), "mv clone world ActualWorld");
    188. this.plugin.getServer().dispatchCommand(this.plugin.getServer().getConsoleSender(), "mvconfirm");
    189. }
    190.  
    191. public void checkForCountdownOnStart() {
    192. if ((!this.plugin.progress.contains("On")) &&
    193. (Bukkit.getOnlinePlayers().length == this.plugin.getConfig().getInt("PlayerAmountForCountdown")))
    194. new BukkitRunnable()
    195. {
    196. public void run() {
    197. int auto = GameUtil.this.plugin.getConfig().getInt("AutostartCountdown");
    198. int pCount = Bukkit.getOnlinePlayers().length;
    199. GameUtil.this.time -= 1;
    200. if (pCount >= GameUtil.this.plugin.getConfig().getInt("PlayerAmountForCountdown")) {
    201. GameUtil.this.plugin.countdownInProgress.clear();
    202. GameUtil.this.plugin.countdownInProgress.add("On");
    203. if (GameUtil.this.time == auto) {
    204. ChatUtils.broadcast("A game will start in " + GameUtil.this.time + " seconds!");
    205. } else if (GameUtil.this.time == 30) {
    206. ChatUtils.broadcast("A game will start in " + GameUtil.this.time + " seconds!");
    207. } else if (GameUtil.this.time == 15) {
    208. ChatUtils.broadcast("A game will start in " + GameUtil.this.time + " seconds!");
    209. } else if (GameUtil.this.time == 10) {
    210. ChatUtils.broadcast("A game will start in " + GameUtil.this.time + " seconds!");
    211. } else if ((GameUtil.this.time < 6) && (GameUtil.this.time > 0)) {
    212. ChatUtils.broadcast("A game will start in " + GameUtil.this.time + " seconds!");
    213. } else if (GameUtil.this.time == 0) {
    214. ListUtils.hasvirus.add(plugin.getServer().getOnlinePlayers()[new Random().nextInt(plugin.getServer().getOnlinePlayers().length)].getName());
    215. GameUtil.this.plugin.startTimer.clear();
    216. ChatUtils.broadcast("Starting Game");
    217. GameUtil.this.plugin.gameUtil.startGame();
    218. cancel();
    219. }
    220. } else {
    221. ChatUtils.broadcast("Countdown stopped due to players leaving");
    222. GameUtil.this.plugin.countdownInProgress.clear();
    223. GameUtil.this.plugin.countdownInProgress.add("Off");
    224. GameUtil.this.plugin.startTimer.clear();
    225. cancel();
    226. }
    227. }
    228. }
    229. .runTaskTimer(this.plugin, 20L, 20L);
    230. }
    231.  
    232.  
    233.  
    234. }
    235.  


    But i added some debug messages because some parts didnt work and i see that the code only reaches the part where it broadcasts "WHY".
    I alredy tried a lot of things and not one single of them worked. Please help me fix this.
    What i mean that it dosent reach something it is that simply it dosent reach this part:

    Code:java
    1. for (Player s : Bukkit.getOnlinePlayers()) {
    2. if(ListUtils.hasvirus.contains(s)) {
    3. ChatUtils.broadcast("Got Here");
    4. prepVirus(s);
    5. Location hasvirusl = plugin.getLocation("hasvirusspawn");
    6. s.teleport(hasvirusl);
    7. ChatUtils.send(s, "You are now the Virus Carrier");
    8. ChatUtils.send(s, "hit Someone to Pass the Virus to Him!");
    9. } else if (ListUtils.novirus.contains(s)){
    10. ChatUtils.broadcast("Now here");
    11. prepPlayer(s);
    12. Location novirusl = plugin.getLocation("novirusspawn");
    13. s.teleport(novirusl);
    14. ChatUtils.broadcast(ListUtils.hasvirus.toString() + " is now the Virus Carrier!");
    15. }


    Regards and Thanks in Adavnce
     
  2. Offline

    hawkfalcon

    Bukkit.getServer().getOnlinePlayers()
     
  3. Offline

    MineoxFX

  4. Offline

    vemacs

    tommycake50 likes this.
  5. Offline

    xTrollxDudex

    vemacs
    Not as long as you handle the list correctly
     
  6. Offline

    MineoxFX

    xTrollxDudex
    Then what is the error? i am doing all correctly,
     
  7. Offline

    vemacs

    xTrollxDudex: I've seen otherwise.

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

    hawkfalcon

    for future reference:
    [8/30/13 4:35:26 PM] hawkfalcon: You do this:
    [8/30/13 4:35:27 PM] hawkfalcon: for (Player s : Bukkit.getOnlinePlayers()) {
    if(ListUtils.hasvirus.contains(s)) {
    [8/30/13 4:35:37 PM] hawkfalcon: You are comparing two player objects
    [8/30/13 4:35:47 PM] hawkfalcon: Don't store player objects
    [8/30/13 4:35:48 PM] hawkfalcon: They change.
    [8/30/13 4:36:02 PM] hawkfalcon: Meaning when you compare them, they will be different
    [8/30/13 4:36:03 PM] DevRosemberg: How do i fix it?
    [8/30/13 4:36:07 PM] hawkfalcon: Compare names rather than player objects
     
  9. Offline

    MineoxFX

Thread Status:
Not open for further replies.

Share This Page