Runnable help.

Discussion in 'Plugin Development' started by Rockindavies21, Jun 11, 2014.

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

    Rockindavies21

    Hi, i have a runnable in my plugins onEnable() and it has p.getLocation() in it. But, p is red because it's not a valid vaiable. That's where the issue arises i can't figure out how to make a player variable global in the class or add one to the onEnable(). All help greatly appreciated! :)
     
  2. Offline

    Tzeentchful

    Try adding the final modifier to the variable you are trying to use.
     
  3. Offline

    1Rogue

    Any variable used in an anonymous context has to be effectively final if it is from outside the class' scope.
     
    Europia79 likes this.
  4. Offline

    Rockindavies21

  5. Could we get the clipping of your code in which you are having problems?
     
  6. Offline

    Rockindavies21

    xYourFreindx
    I changed it up a lot. I fixed the double runnables but now whenever someone types /tceffect <effect> it takes away another players effect and if someone else does it it takes another players away. In short only one person can have it.
    Also happens with /tceffect stop.

    Code:
    Code:java
    1. package me.rockindavies21.tcparticleeffects;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Location;
    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.player.PlayerQuitEvent;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class TCParticleEffects extends JavaPlugin implements Listener{
    18.  
    19. public double radialsPerStep = Math.PI / 8;
    20.  
    21. public float radius = .4f;
    22.  
    23. protected float step = 0;
    24.  
    25. public Player p;
    26.  
    27. public int hearts;
    28.  
    29. List<String> Main = new ArrayList<String>();
    30.  
    31. List<String> effecthearts = new ArrayList<String>();
    32. List<String> effectwater = new ArrayList<String>();
    33. List<String> effectwitch = new ArrayList<String>();
    34. List<String> effectslime = new ArrayList<String>();
    35.  
    36. @Override
    37. public void onEnable() {
    38. getServer().getPluginManager().registerEvents(this, this);
    39.  
    40. hearts = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    41.  
    42. @Override
    43. public void run() {
    44. Location location = p.getLocation();
    45. location.add(0, 1.9f, 0);
    46. location.add(Math.cos(radialsPerStep * step) * radius, 0, Math.sin(radialsPerStep * step) * radius);
    47. if(effecthearts.contains(p.getName())){
    48. ParticleEffect.HEART.display(location, 0, 0, 0, .5f, 1);
    49. step++;
    50. }
    51.  
    52. if(effectwater.contains(p.getName())){
    53. ParticleEffect.DRIP_WATER.display(location, 0, 0, 0, .5f, 1);
    54. step++;
    55. }
    56. if(effectwitch.contains(p.getName())){
    57. ParticleEffect.WITCH_MAGIC.display(location, 0, 0, 0, .5f, 1);
    58. step++;
    59. }
    60. if(effectslime.contains(p.getName())){
    61. ParticleEffect.SLIME.display(location, 0, 0, 0, .5f, 6);
    62. step++;
    63. }
    64.  
    65.  
    66. }
    67.  
    68. }, 0, 2L);
    69.  
    70. }
    71.  
    72. @Override
    73. public void onDisable() {
    74.  
    75. }
    76.  
    77. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    78. p = (Player) sender;
    79. if(commandLabel.equalsIgnoreCase("tceffect")){
    80. if(p.hasPermission("tc.effects")){
    81.  
    82. if(args[0].equalsIgnoreCase("stop")){
    83. effecthearts.remove(p.getName());
    84. effectwater.remove(p.getName());
    85. effectwitch.remove(p.getName());
    86. effectslime.remove(p.getName());
    87. Main.remove(p.getName());
    88. return true;
    89. }
    90. if(Main.contains(p.getName())){
    91. p.sendMessage(ChatColor.GREEN + "You already have an active effect!");
    92. return true;
    93. }
    94.  
    95. if(args.length > 0){
    96. if(args[0].equalsIgnoreCase("hearts")){
    97. p.sendMessage(ChatColor.GREEN + "Heart effect activated!");
    98. effecthearts.add(p.getName());
    99. Main.add(p.getName());
    100. return true;
    101.  
    102. }
    103. }
    104. }
    105.  
    106. if(args[0].equalsIgnoreCase("water")){
    107. p.sendMessage(ChatColor.GREEN + "Water effect activated!");
    108. effectwater.add(p.getName());
    109. Main.add(p.getName());
    110. return true;
    111.  
    112. }
    113.  
    114. if(args[0].equalsIgnoreCase("witch")){
    115. p.sendMessage(ChatColor.GREEN + "Witch effect activated!");
    116. effectwitch.add(p.getName());
    117. Main.add(p.getName());
    118. return true;
    119.  
    120. }
    121.  
    122. if(args[0].equalsIgnoreCase("slime")){
    123. p.sendMessage(ChatColor.GREEN + "Witch effect activated!");
    124. effectslime.add(p.getName());
    125. Main.add(p.getName());
    126. return true;
    127.  
    128. }
    129.  
    130. }
    131. return false;
    132. }
    133.  
    134.  
    135. @EventHandler
    136. public void onQuit(PlayerQuitEvent e){
    137. Player p = e.getPlayer();
    138. Main.remove(p.getName());
    139. if(effecthearts.contains(p.getName())){
    140. effecthearts.remove(p.getName());
    141. }
    142. if(effectwater.contains(p.getName())){
    143. effectwater.remove(p.getName());
    144. }
    145. if(effectwitch.contains(p.getName())){
    146. effectwitch.remove(p.getName());
    147. }
    148. if(effectslime.contains(p.getName())){
    149. effectslime.remove(p.getName());
    150. }
    151.  
    152.  
    153.  
    154. }
    155. }
    156.  
     
  7. Why are you doing all this in your onEnable?
    What exactly are you trying to achieve?
     
  8. Offline

    Garris0n

    The variable 'p' is going to throw plenty of null pointer exceptions until you actually set it to something...
     
    AdamQpzm likes this.
  9. Offline

    drtshock

    xTrollxDudex and Garris0n like this.
  10. Offline

    Rockindavies21

    xYourFreindx
    I'm creating a particle effects plugin.
    Garris0n
    p is set on line 78.
    drtshock
    Fixed. Also, that rubber duck method sounds interesting i'll give it a shot :p
     
    drtshock likes this.
  11. Offline

    Necrodoom

    Rockindavies21 onenable would fire before oncmmand.. I don't think you understand when onenable fires and whats its purpose.
     
    AdamQpzm and Garris0n like this.
  12. Offline

    1Rogue

    Fields qualify as "effectively final" by anonymous contexts

    The real crime here is a global player variable that can be represented by anything and has no real state management.
     
    AdamQpzm and Garris0n like this.
  13. onEnable says *When the plugins gets loaded by the server*, an event that doesn't involve players. So you're casting player to a null object. You've got to describe what players you're talking about to the program. I'd suggest using the onPlayerJoin event. Since its the first thing to trigger when a player joins.
     
  14. Offline

    ever_x

    Instead of doing this all in onEnable, create an object class (getters / setters) to handle actual particle effects for ONE player. When they type a commands (or whatever), create a new instance of the class, and run it that way. Then, the class only works for ONE player, so you can have each player with a separate trail. Welcome to OOP.
     
  15. Offline

    Rockindavies21

    ever_x
    Could you explain in more detail?
    I'm really confused :(
     
  16. Offline

    JaguarJo

  17. Offline

    MeRPG

    Create a new Object by creating a new class.
    <Edit by Moderator: Redacted bit url>
    Every time a player joins, create another instance of your object to store that players data.
     
    Last edited by a moderator: Feb 12, 2017
  18. Offline

    Rockindavies21

    MeRPG
    Read through it a lot. Makes sense.

    Is this the correct way to do it?
    The only issue i have is at "new Player" Player is red because i didn't put the code correctly in the parenthesis...
    Not exactly sure what to put in there.

    Code:
    Code:java
    1. package me.rockindavies21.tcparticleeffects;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.player.PlayerJoinEvent;
    7.  
    8. public class PlayerJoin implements Listener{
    9.  
    10. Player p;
    11.  
    12. @EventHandler
    13. public void onJoin(PlayerJoinEvent e){
    14. Player player = new Player(p.getName());
    15.  
    16. }
    17.  
    18.  
    19. }
    20.  
     
  19. Offline

    ever_x

    Rockindavies21 You don't create a new player instance, you create a new instance of your own class, but pass in a Player object into the constructor.
     
  20. Offline

    Rockindavies21

    ever_x
    I completely recoded the plugin. Now when i type /tceffect hearts i get kicked with internal server error.
    Error:
    Code:
    [18:59:03] [Server thread/WARN]: Failed to handle packet for /xx.xxx.xxxxxxx
    java.lang.NullPointerException
        at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:101) ~[spigot.jar:git-Spigot-1451]
        at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:425) ~[spigot.jar:git-Spigot-1451]
        at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:406) ~[spigot.jar:git-Spigot-1451]
        at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:367) [spigot.jar:git-Spigot-1451]
        at org.apache.logging.log4j.core.Logger.log(Logger.java:110) [spigot.jar:git-Spigot-1451]
        at org.apache.logging.log4j.spi.AbstractLogger.error(AbstractLogger.java:609) [spigot.jar:git-Spigot-1451]
        at org.bukkit.craftbukkit.v1_7_R3.util.ForwardLogHandler.publish(ForwardLogHandler.java:33) ~[spigot.jar:git-Spigot-1451]
        at java.util.logging.Logger.log(Logger.java:610) ~[?:1.7.0_51]
        at java.util.logging.Logger.doLog(Logger.java:631) ~[?:1.7.0_51]
        at java.util.logging.Logger.log(Logger.java:720) ~[?:1.7.0_51]
        at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:991) ~[spigot.jar:git-Spigot-1451]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:830) ~[spigot.jar:git-Spigot-1451]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) ~[spigot.jar:git-Spigot-1451]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:65) ~[spigot.jar:git-Spigot-1451]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:176) ~[spigot.jar:git-Spigot-1451]
        at net.minecraft.server.v1_7_R3.ServerConnection.c(ServerConnection.java:77) [spigot.jar:git-Spigot-1451]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:713) [spigot.jar:git-Spigot-1451]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:283) [spigot.jar:git-Spigot-1451]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:576) [spigot.jar:git-Spigot-1451]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:482) [spigot.jar:git-Spigot-1451]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1451]
     
  21. Offline

    Rockindavies21

  22. Offline

    teej107

    Rockindavies21 I don't know how your re-worked code works, but from your first code, you could have just surrounded the code in your runnable with a for loop that loops through the online players. You wouldn't need to make a global variable for the player. Using a global variable for a player makes me think that there will only be one player that this would apply to. If you want this to be for only one player, forget the for loop and use the getPlayer() method and check to see if the player isn't null before running the rest of the code.
     
  23. Offline

    JaguarJo

    Locked. I'm sorry, but you are not using CraftBukkit software and we don't support unofficial builds. Please seek support from the makers of your server software.
     
Thread Status:
Not open for further replies.

Share This Page