Need help making a custom event

Discussion in 'Plugin Development' started by Sabersamus, Apr 1, 2012.

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

    Sabersamus

    I looked at the custom event tutorial but its not really making sense, could someone help with this?

    The events class:
    Code:java
    1.  
    2. public class TransferMoneyEvent extends Event
    3. {
    4.  
    5. private static final HandlerList handlers = new HandlerList();
    6. private Economy economy;
    7. private Player player = economy.player;
    8.  
    9. @Override
    10. public HandlerList getHandlers()
    11. {
    12. return handlers;
    13. }
    14.  
    15. public static HandlerList getHandlerList()
    16. {
    17. return handlers;
    18. }
    19.  
    20. public int getAmountTransferred()
    21. {
    22. return economy.getTransferedAmount();
    23. }
    24.  
    25. public void setAmountTransferred(int amount)
    26. {
    27. economy.setTransferredAmount(amount);
    28. }
    29.  
    30. public Player getPlayer()
    31. {
    32. return this.player;
    33. }
    34. }


    i tested it and it didnt fire, obviously :3 but i dont understand how to get it to work :/
     
  2. Offline

    nickrak

    Can you post your listener code and the event caller's code as well?
     
  3. Offline

    Cooliojazz

    What about the code catching and throwing the event? Also, I feel like there should be a constructor, as economy is not initialized...
    EDIT: Ninja'd...
     
  4. Offline

    Sabersamus

    i guess i havent made a caller :3

    how/where do i make it?

    just fixed that

    Code:java
    1. private static Basic plugin;
    2. public TransferMoneyEvent(Basic instance){
    3. plugin = instance;
    4. }
    5. private Economy economy = plugin.getEconomyAPI();


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

    nickrak

  6. Offline

    Sabersamus

    that doesnt help at all, it doesnt say where that goes, do i put that where ever money is being transferred? cause that makes sense (To me)
     
  7. Offline

    Cooliojazz

    That seems a bit better. =P You call it whenever you want the event to be fired. Bukkit.getServer().getPluginManager().callEvent(new TransferMoneyEvent(plugin));
     
    Sabersamus likes this.
  8. Offline

    Sabersamus

    ill try that ;) thanks

    i gotta go, ill post later tonight if this works, (i really hope this does D: )

    im getting an NPE on

    pm.callEvent(event);

    heres my event

    Code:java
    1. public class TransferMoneyEvent extends Event
    2. {
    3. public static Basic plugin;
    4. public TransferMoneyEvent(Basic instance)
    5. {
    6. plugin = instance;
    7. }
    8.  
    9. private static final HandlerList handlers = new HandlerList();
    10. private Economy economy = new Economy();
    11. private Player player = economy.player;
    12. private int amount = economy.transferedMoney;
    13.  
    14. @Override
    15. public HandlerList getHandlers()
    16. {
    17. return handlers;
    18. }
    19.  
    20. public static HandlerList getHandlerList()
    21. {
    22. return handlers;
    23. }
    24.  
    25. public int getAmountTransferred()
    26. {
    27. return this.amount;
    28. }
    29.  
    30. public void setAmountTransferred(int amount)
    31. {
    32. economy.setTransferredAmount(amount);
    33. }
    34.  
    35. public Player getPlayer()
    36. {
    37. return this.player;
    38. }
    39. }


    heres where the event is being called

    Code:java
    1. public void transferMoney(Player givingPlayer, Player receivingPlayer, int value){
    2. TransferMoneyEvent event = new TransferMoneyEvent(plugin);
    3. PluginManager pm = plugin.getServer().getPluginManager();
    4. this.player = givingPlayer;
    5. EconomyInfo info = plugin.getEconomyInfo();
    6. if(this.transferedMoney == 0){
    7. this.transferedMoney = value;
    8. if(givingPlayer == null || receivingPlayer == null)return;
    9. if(info.getMoney().getInt(givingPlayer.getName() + ".Balance") - value < 0){
    10. return;
    11. }
    12. this.subtractMoney(givingPlayer, value);
    13. this.addMoney(receivingPlayer, value);
    14. }else{
    15. value = this.transferedMoney;
    16. if(givingPlayer == null || receivingPlayer == null)return;
    17. if(info.getMoney().getInt(givingPlayer.getName() + ".Balance") - value < 0){
    18. return;
    19. }
    20. this.subtractMoney(givingPlayer, value);
    21. this.addMoney(receivingPlayer, value);
    22. }
    23. pm.callEvent(event);
    24. }


    and heres my test listener

    Code:java
    1. @EventHandler
    2. public void onTransfer(TransferMoneyEvent event)
    3. {
    4. Player player = event.getPlayer();
    5. plugin.getLogger().info("test");
    6. player.sendMessage("rawr: ");
    7. }


    :/

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

    Technius

    Your error log would be nice.
     
  10. Offline

    CorrieKay

    Uhm, ive never played around with custom events or anything, but uhm,

    shouldnt you be passing more info into the event than you are? ...and vice versa? from what im seeing, no matter what you do to the event in the listener, it wouldnt affect any of your outcome. Plus, your event fires AFTER all of your code executes :x
     
  11. Offline

    Sabersamus

    Code:
    21:30:11 [INFO] [Basic] test
    21:30:11 [SEVERE] Could not pass event TransferMoneyEvent to Basic
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:303)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:459)
            at com.github.Sabersamus.Basic.Economy.API.Economy.transferMoney(Economy
    .java:94)
            at com.github.Sabersamus.Basic.Economy.Wallet.onCommand(Wallet.java:64)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:16
    6)
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:4
    73)
            at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.
    java:821)
            at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)
    
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
            at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:33)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
            at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:7
    8)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:554)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:452)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:490)
    Caused by: java.lang.NullPointerException
            at com.github.Sabersamus.Basic.Listeners.Test.onTransfer(Test.java:24)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:301)
            ... 17 more
    if you see in there it says [Basic] test

    which means the event is firing (good) its just event.getPlayer(); is null apparently (im just now seeing the [Basic] test for the first time)

    what do you mean?

    edit: i haven't really added much to the event right now i just need to make sure its working

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

    CorrieKay

    well... i dont know exactly what economy is, nor have a dealt with it, but im assuming that it doesnt contain the specific player you need, considering that its a new object you just created. Therefore, the player field in economy would be null, logically, i have no idea how it would know which player to point at.
     
  13. Offline

    Sabersamus

    in the transferMoney() method, i set

    this.player = givingPlayer;
    givingPlayer is the player thats giving money,

    so it *shouldn't* be null :/

    Code:java
    1.  
    2. protected Player player;
    3.  
    4. public void transferMoney(Player givingPlayer, Player receivingPlayer, int value){
    5. TransferMoneyEvent event = new TransferMoneyEvent(plugin);
    6. PluginManager pm = plugin.getServer().getPluginManager();
    7. pm.callEvent(event);
    8. this.player = givingPlayer;
    9. //more code
    10.  
    11.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  14. Offline

    CorrieKay

    yeah but theyre completly different objects :eek:

    edit: i get it! pass the economy object into the event as a parameter :D
     
  15. Offline

    Sabersamus

    not sure what you mean :3

    but in the Event class it pulls that for getPlayer();

    Code:java
    1.  
    2. private Player player = economy.player;
    3.  
    4. public Player getPlayer(){
    5. return this.player;
    6. }
    7.  
     
  16. Offline

    CorrieKay

    er.. okay, lets see.. where is this economy object originating from? i wanna go look at the api for it
     
  17. Offline

    Sabersamus

    i changed it to
    Code:java
    1. private Economy economy = plugin.getEconomyAPI();


    main class >>
    Code:java
    1. public Economy getEconomyAPI(){
    2. return new Economy(this);
    3. }


    eh, that made everything return null and wont even fire the event D:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  18. Offline

    CorrieKay

    Okay dont worry. Do me a favor, and tell me what the economy object youre using is called, in the code (not the event)
     
  19. Offline

    Sabersamus

    what do you mean? :/

    edit: i found why everything broke, if i do this

    Code:java
    1. private Economy economy = new Economy();
    its fine but if i do

    Code:java
    1. private Economy economy = new Economy(plugin);
    it breaks D:
     
  20. Offline

    CorrieKay

    where is the api for this economy thing? lemme take a look at it.
     
  21. Offline

    Sabersamus

    not sure if this is what you mean, but all the things are stored in Economy.java

    Code:java
    1. package com.github.Sabersamus.Basic.Economy.API;
    2.  
    3. import org.bukkit.OfflinePlayer;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.plugin.PluginManager;
    6.  
    7. import com.github.Sabersamus.Basic.Basic;
    8. import com.github.Sabersamus.Basic.EconomyInfo;
    9.  
    10. /**
    11.  * Methods to manage the economy
    12.  */
    13. public class Economy
    14. {
    15. public static Basic plugin;
    16. public Economy(Basic instance){
    17. plugin = instance;
    18. }
    19.  
    20. public Economy()
    21. {
    22.  
    23. }
    24.  
    25. private boolean hasEnough;
    26. protected int transferedMoney = 0;
    27. protected Player player;
    28.  
    29. public boolean hasEnough(){
    30. if(hasEnough){
    31. return true;
    32. }else{
    33. return false;
    34. }
    35. }
    36.  
    37. /**
    38.   * Gets the balance of a player
    39.   * @param - player the player chosen
    40.   * @return - the players balance
    41.   */
    42. public int getBalance(Player player){
    43. EconomyInfo info = plugin.getEconomyInfo();
    44. if(info.getMoney().contains(player.getName())){
    45. return info.getMoney().getInt(player.getName() + ".Balance");
    46. }
    47. return 0;
    48. }
    49.  
    50. /**
    51.   * Gets the balance of an offline player
    52.   * @param player - the offline player whose balance to be checked
    53.   * @return - the amount of money they have
    54.   */
    55. public int getBalance(OfflinePlayer player)
    56. {
    57. EconomyInfo info = plugin.getEconomyInfo();
    58. if(player != null)
    59. {
    60. return info.getMoney().getInt(player.getName() + ".Balance");
    61. }else{
    62. return 0;
    63. }
    64. }
    65.  
    66. /**
    67.   * Transfers money from one player to another
    68.   * @param givingPlayer - the player to give the money
    69.   * @param receivingPlayer - the player to get the money
    70.   * @param value - the amount of money to be transferred
    71.   */
    72. public void transferMoney(Player givingPlayer, Player receivingPlayer, int value){
    73. TransferMoneyEvent event = new TransferMoneyEvent(plugin);
    74. PluginManager pm = plugin.getServer().getPluginManager();
    75. this.player = givingPlayer;
    76. EconomyInfo info = plugin.getEconomyInfo();
    77. if(this.transferedMoney == 0){
    78. this.transferedMoney = value;
    79. if(givingPlayer == null || receivingPlayer == null)return;
    80. if(info.getMoney().getInt(givingPlayer.getName() + ".Balance") - value < 0){
    81. return;
    82. }
    83. this.subtractMoney(givingPlayer, value);
    84. this.addMoney(receivingPlayer, value);
    85. }else{
    86. value = this.transferedMoney;
    87. if(givingPlayer == null || receivingPlayer == null)return;
    88. if(info.getMoney().getInt(givingPlayer.getName() + ".Balance") - value < 0){
    89. return;
    90. }
    91. this.subtractMoney(givingPlayer, value);
    92. this.addMoney(receivingPlayer, value);
    93. }
    94. pm.callEvent(event);
    95. }
    96.  
    97. /**
    98.   * Transfers money between an online player and an offline player
    99.   * @param givingPlayer - the player giving the money
    100.   * @param receivingPlayer - the offline player getting money
    101.   * @param amount - the amount of money to be transferred
    102.   */
    103. public void transferMoney(Player givingPlayer, OfflinePlayer receivingPlayer, int amount)
    104. {
    105. this.player = givingPlayer;
    106. EconomyInfo info = plugin.getEconomyInfo();
    107. if(this.transferedMoney == 0)
    108. {
    109. this.transferedMoney = amount;
    110. if(givingPlayer == null || receivingPlayer == null)return;
    111. if(info.getMoney().getInt(givingPlayer.getName() + ".Balance") - amount < 0)return;
    112. this.subtractMoney(givingPlayer, amount);
    113. this.addMoney(receivingPlayer, amount);
    114. }else{
    115. amount = this.transferedMoney;
    116. if(givingPlayer == null || receivingPlayer == null)return;
    117. if(info.getMoney().getInt(givingPlayer.getName() + ".Balance") - amount < 0)return;
    118. this.subtractMoney(givingPlayer, amount);
    119. this.addMoney(givingPlayer, amount);
    120. }
    121. }
    122.  
    123. /**
    124.   * Sets the amount of money transferred
    125.   * @param amount - the amount of money to be set
    126.   */
    127. public void setTransferredAmount(int amount)
    128. {
    129. this.transferedMoney = amount;
    130. }
    131.  
    132. /**
    133.   * Gets the amount of money transferred
    134.   * @return if a transaction has been set, returns the amount, else 0
    135.   */
    136. public int getTransferedAmount()
    137. {
    138. if(this.transferedMoney > 0)
    139. {
    140. return this.transferedMoney;
    141. }
    142. return 0;
    143. }
    144.  
    145. /**
    146.   * Adds money to a players balance
    147.   * @param - player the player selected
    148.   * @param - amount the amount to be added
    149.   */
    150. public void addMoney(Player player, int amount){
    151. EconomyInfo info = plugin.getEconomyInfo();
    152. this.player = player;
    153. if(this.transferedMoney == 0){
    154. this.transferedMoney = amount;
    155. if(info.getMoney().contains(player.getName())){
    156. info.getMoney().set(player.getName() + ".Balance", plugin.getEconomyInfo().getMoney().getInt(player.getName() + ".Balance") + amount);
    157. info.saveMoney();
    158. }else{
    159. info.getMoney().set(player.getName() + ".Balance", amount);
    160. info.saveMoney();
    161. }
    162. }else{
    163. amount = this.transferedMoney;
    164. if(info.getMoney().contains(player.getName())){
    165. info.getMoney().set(player.getName() + ".Balance", plugin.getEconomyInfo().getMoney().getInt(player.getName() + ".Balance") + amount);
    166. info.saveMoney();
    167. }else{
    168. info.getMoney().set(player.getName() + ".Balance", amount);
    169. info.saveMoney();
    170. }
    171. }
    172. }
    173.  
    174. /**
    175.   * Adds money to an offline players balance
    176.   * @param player - the offline player to get money
    177.   * @param amount - the amount of money to receive
    178.   */
    179. public void addMoney(OfflinePlayer player, int amount)
    180. {
    181. EconomyInfo info = plugin.getEconomyInfo();
    182. if(player == null)return;
    183. info.getMoney().set(player.getName() + ".Balance", info.getMoney().getInt(player.getName() + ".Balance") + amount);
    184. info.saveMoney();
    185. }
    186.  
    187. /**
    188.   * Subtracts money from a players balance
    189.   * @param player - the player chosen
    190.   * @param amount - the money to be removed, can not make a players balance less than 0
    191.   */
    192. public void subtractMoney(Player player, int amount){
    193. this.player = player;
    194. EconomyInfo settings = plugin.getEconomyInfo();
    195. if(this.transferedMoney == 0){
    196. this.transferedMoney = amount;
    197. if(settings.getMoney().contains(player.getName())){
    198. if(settings.getMoney().getInt(player.getName() + ".Balance") - amount < 0){
    199. this.hasEnough = false;
    200. return;
    201. }else{
    202. this.hasEnough = true;
    203. settings.getMoney().set(player.getName() + ".Balance", settings.getMoney().getInt(player.getName() + ".Balance") - amount);
    204. settings.saveMoney();
    205. this.transferedMoney = amount;
    206. }
    207. }
    208. }else{
    209. if(settings.getMoney().contains(player.getName())){
    210. if(settings.getMoney().getInt(player.getName() + ".Balance") - amount < 0){
    211. this.hasEnough = false;
    212. return;
    213. }else{
    214. this.hasEnough = true;
    215. settings.getMoney().set(player.getName() + ".Balance", settings.getMoney().getInt(player.getName() + ".Balance") - amount);
    216. settings.saveMoney();
    217. this.transferedMoney = amount;
    218. }
    219. }
    220. }
    221. }
    222.  
    223. /**
    224.   * Subtracts money from an offline player
    225.   * @param player - the offline player
    226.   * @param value - the amount of money to be subtracted
    227.   */
    228. public void subtractMoney(OfflinePlayer player, int value)
    229. {
    230. EconomyInfo info = plugin.getEconomyInfo();
    231. if(player == null)return;
    232. if(info.getMoney().getInt(player.getName() + ".Balance") - value < 0)return;
    233. info.getMoney().set((player.getName() + ".Balance"), info.getMoney().getInt(player.getName() + ".Balance") - value);
    234. info.saveMoney();
    235. }
    236.  
    237. /**
    238.   * Sets a players balance
    239.   * @param player - the player
    240.   * @param amount - the amount to be set, can not be less than 0
    241.   */
    242. public void setBalance(Player player, int amount){
    243. this.player = player;
    244. EconomyInfo settings = plugin.getEconomyInfo();
    245. if(amount < 0){
    246. return;
    247. }else{
    248. settings.getMoney().set(player.getName() + ".Balance", amount);
    249. settings.saveMoney();
    250. this.transferedMoney = amount;
    251. }
    252. }
    253.  
    254. /**
    255.   * Sets an offline players balance
    256.   * @param player - the offline player
    257.   * @param amount - the amount of money to be set
    258.   */
    259. public void setBalance(OfflinePlayer player, int amount)
    260. {
    261. EconomyInfo info = plugin.getEconomyInfo();
    262. if(amount < 0)return;
    263. info.getMoney().set(player.getName() + ".Balance", amount);
    264. info.saveMoney();
    265. this.transferedMoney = amount;
    266. }
    267. }
    268.  


    theres a method in the main class to access it

    public Economy getEconomyAPI(){
    return new Economy(this);
    }
     
  22. Offline

    CorrieKay

    Knew it. Your player is uninitialized, look at line 27. Youre creating a brand new object of this class, and the player isnt initialized. instead of creating a new instance of Economy, have your event:

    Code:
    public static Basic plugin;
    Economy economy;
    public TransferMoneyEvent(Basic instance, Economy economy)
    {
    plugin = instance;
    this.economy = economy;
    }
    as your constructor (and declared variables)

    and for your event creation, add "this" to the constructor parameters.
     
  23. Offline

    Sabersamus

    ok i added that,

    and now i get this

    Code:
    22:26:38 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'wall
    et' in plugin Basic v0.7
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:16
    6)
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:4
    73)
            at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.
    java:821)
            at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)
    
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
            at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:33)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
            at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:7
    8)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:554)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:452)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:490)
    Caused by: java.lang.NullPointerException
            at com.github.Sabersamus.Basic.Economy.API.TransferMoneyEvent.<init>(Tra
    nsferMoneyEvent.java:20)
            at com.github.Sabersamus.Basic.Economy.API.Economy.transferMoney(Economy
    .java:73)
            at com.github.Sabersamus.Basic.Economy.Wallet.onCommand(Wallet.java:64)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
            ... 12 more

    73 of Economy: TransferMoneyEvent event = new TransferMoneyEvent(plugin, this);

    20 of TransferMoneyEvent: private Player player = economy.player;
     
  24. Offline

    CorrieKay

    did the player get the message?
     
  25. Offline

    dsmyth1915

    Your forgetting something here. You declared a variable "player" as a Player entity. Then you checking the Player getPlayer(){return this.player} shouldn't it be
    Code:java
    1.  
    2. Public player getPlayer(){
    3. return this.player
    4. }
    5.  
    or am I mistaken?
     
  26. nope Sabersamus is correct. First it's public (lower case p) and the return type is Player since the class is names like that.

    BTW: dsmyth1915 your code wouldn't even compile.
     
  27. Offline

    dsmyth1915

    Thanks you, I'm on my phone at the moment and it like to make weird capitalizations for no apparent reason.
     
  28. Offline

    Sabersamus

    ok so i got it working, but i have a temporary work around :/ and its not fun

    Code:java
    1. public Player getPlayer()
    2. {
    3. return this.player;
    4. }
    5.  
    6. protected void setPlayer(Player player)
    7. {
    8. this.player = player;
    9. }
    thats in the event, and RIGHT before i call the event,

    Code:java
    1.  
    2. TransferMoneyEvent event = new TransferMoneyEvent(plugin);
    3. PluginManager pm = plugin.getServer().getPluginManager();
    4. event.setPlayer(givingPlayer);
    5. pm.callEvent(event);


    this works, my test listener is the same as before,

    Code:java
    1.  
    2. public void onTransfer(TransferMoneyEvent event){
    3. Player player = event.getPlayer();
    4. plugin.getLogger().info("test");
    5. player.sendMessage("rawr: ");
    6. }


    it displays both messages, but i dont wanna have to use protected void setPlayer(Player player){ :/
     
  29. use pakage protected setPlayer(), then its not incuded at the java doc by default
     
  30. Offline

    CorrieKay

    then... pass the player in through the event constructor.. :confused:
     
Thread Status:
Not open for further replies.

Share This Page