RPG Mining Plugin - NOT WORKING?

Discussion in 'Plugin Development' started by HiveHD, Jul 12, 2013.

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

    HiveHD

    I am creating a plugin to increase the time it takes for players to mine ores (and certain other blocks eventually).

    The code I am using to do this is:
    Code:java
    1. //Package & Imports are above this
    2. public final class RPGMining implements Listener {
    3.  
    4. @EventHandler
    5.  
    6. public void addMiningFatigue(PlayerInteractEvent event){
    7. Player p = event.getPlayer();
    8. if (event.getClickedBlock().getType() == Material.DIAMOND_ORE){
    9. p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 300, 1));
    10. }
    11. }
    12. @EventHandler
    13.  
    14. public void onBlockBreak(BlockBreakEvent event) {
    15. Player player = event.getPlayer();
    16. if (event.getBlock().getType() == Material.DIAMOND_ORE){
    17. if(player.hasPotionEffect(PotionEffectType.SLOW_DIGGING)){
    18. player.removePotionEffect(PotionEffectType.SLOW_DIGGING);
    19.  
    20. }
    21. }
    22. }
    23. @EventHandler
    24.  
    25. public void addMiningFatigue1(PlayerInteractEvent event){
    26. Player p = event.getPlayer();
    27. if (event.getClickedBlock().getType() == Material.IRON_ORE){
    28. p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 300, 1));
    29. }
    30. }
    31. @EventHandler
    32.  
    33. public void onBlockBreak1(BlockBreakEvent event) {
    34. Player player = event.getPlayer();
    35. if (event.getBlock().getType() == Material.IRON_ORE){
    36. if(player.hasPotionEffect(PotionEffectType.SLOW_DIGGING)){
    37. player.removePotionEffect(PotionEffectType.SLOW_DIGGING);
    38. }
    39. }
    40. }
    41. @EventHandler
    42.  
    43. public void addMiningFatigue2(PlayerInteractEvent event){
    44. Player p = event.getPlayer();
    45. if (event.getClickedBlock().getType() == Material.GOLD_ORE){
    46. p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 300, 1));
    47. }
    48. }
    49. @EventHandler
    50.  
    51. public void onBlockBreak2(BlockBreakEvent event) {
    52. Player player = event.getPlayer();
    53. if (event.getBlock().getType() == Material.GOLD_ORE){
    54. if(player.hasPotionEffect(PotionEffectType.SLOW_DIGGING)){
    55. player.removePotionEffect(PotionEffectType.SLOW_DIGGING);
    56. }
    57. }
    58.  
    59. }
    60. @EventHandler
    61.  
    62. public void addMiningFatigue3(PlayerInteractEvent event){
    63. Player p = event.getPlayer();
    64. if (event.getClickedBlock().getType() == Material.COAL_ORE){
    65. p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 300, 1));
    66. }
    67. }
    68. @EventHandler
    69.  
    70. public void onBlockBreak3(BlockBreakEvent event) {
    71. Player player = event.getPlayer();
    72. if (event.getBlock().getType() == Material.COAL_ORE){
    73. if(player.hasPotionEffect(PotionEffectType.SLOW_DIGGING)){
    74. player.removePotionEffect(PotionEffectType.SLOW_DIGGING);
    75. }
    76. }
    77. }
    78. @EventHandler
    79.  
    80. public void addMiningFatigue4(PlayerInteractEvent event){
    81. Player p = event.getPlayer();
    82. if (event.getClickedBlock().getType() == Material.EMERALD_ORE){
    83. p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 300, 1));
    84. }
    85. }
    86. @EventHandler
    87.  
    88. public void onBlockBreak4(BlockBreakEvent event) {
    89. Player player = event.getPlayer();
    90. if (event.getBlock().getType() == Material.EMERALD_ORE){
    91. if(player.hasPotionEffect(PotionEffectType.SLOW_DIGGING)){
    92. player.removePotionEffect(PotionEffectType.SLOW_DIGGING);
    93. }
    94. }
    95.  
    96. }
    97. @EventHandler
    98.  
    99. public void addMiningFatigue5(PlayerInteractEvent event){
    100. Player p = event.getPlayer();
    101. if (event.getClickedBlock().getType() == Material.QUARTZ_ORE){
    102. p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 300, 1));
    103. }
    104. }
    105. @EventHandler
    106.  
    107. public void onBlockBreak5(BlockBreakEvent event) {
    108. Player player = event.getPlayer();
    109. if (event.getBlock().getType() == Material.QUARTZ_ORE){
    110. if(player.hasPotionEffect(PotionEffectType.SLOW_DIGGING)){
    111. player.removePotionEffect(PotionEffectType.SLOW_DIGGING);
    112. }
    113. }
    114. }
    115. }
    116.  


    Which is basically just a repeat of this code for each individual block (by changing the block type):


    Code:java
    1. @EventHandler
    2.  
    3. public void addMiningFatigue(PlayerInteractEvent event){
    4. Player p = event.getPlayer();
    5. if (event.getClickedBlock().getType() == Material.DIAMOND_ORE){
    6. p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 300, 1));
    7. }
    8. }
    9. @EventHandler
    10.  
    11. public void onBlockBreak(BlockBreakEvent event) {
    12. Player player = event.getPlayer();
    13. if (event.getBlock().getType() == Material.DIAMOND_ORE){
    14. if(player.hasPotionEffect(PotionEffectType.SLOW_DIGGING)){
    15. player.removePotionEffect(PotionEffectType.SLOW_DIGGING);


    But on loading the .jar onto my server, it doesn't seem to be working? (I can't find any errors in console, but I can't find if it is loading at all either, or whether it's just a problem with the actual code)
     
  2. Offline

    ZeusAllMighty11

    You only need a single event to handle these.

    Did you register your events, also?
     
  3. Offline

    HiveHD

    TheGreenGamerHD Im pretty new to coding, this is the first thing I have ever fully coded... How do i register my events? and how would i use a single event to handle all the required blocks?

    If it helps, when i do '/plugins'
    'RPGMining' isn't on the list?

    does that mean the plugin just isn't loading? i can't find evidence of any errors in the console?!

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

    ZeusAllMighty11

    Sorry for the late reply, caught up in coding :3

    To register events statically:
    Code:
    Bukkit.getPluginManager().registerEvents(this, pluginInstance);
    
    if in main class:

    Code:
    getServer().getPluginManager().registerEvents(this, this);
    
    In the static method, you will need a pointer to your plugin instance, via a constructor or static reference.




    To make into a single event method, just add checks to the event. Don't make multiple events, as it can cause extra lag.
     
  5. Offline

    skore87

    Just to clarify the parameters of registerEvents. First parameter is a class that implements Listener, the second is JavaPlugin. I see people mix these up a lot.

    Also, the keyword 'this' refers to the class calling it.
     
  6. Offline

    creatorfromhell

  7. Offline

    HiveHD

    wait, I don't think it is a problem with the code, i just did another export of the plugin and put it in the server agan and found this in console:

    This is my plugin.yml:
    Code:
    name: RPG Mining
    main: me.HiveHD.RPGMining.RPGMining
    version: 1.0
    author: HiveHD
    description: Converts the mining mechanics to give them a more RPG feel
    what is the problem?
     
  8. Offline

    skore87

    I'm willing to guess it is the name field.

    "Must consist of all alphanumeric characters and underscores (a-z,A-Z,0-9, _)"
    Quoted from http://wiki.bukkit.org/Plugin_YAML.
     
  9. Offline

    Ar7ific1al

    skore87 May be right. I don't see any other discrepancies in your plugin.yml. But you might want to try following a bit more standard convention for your packages. Package names are typically lowercase, I believe. Thus, your package should be me.hivehd.rpgmining and the main class (class which extends JavaPlugin) would be RPGMining. This would make the "main" field in your plugin.yml say me.hivehd.rpgmining.RPGMining. Sorry to nitpick with conventions and whatnot, but to be honest, it might be best for someone like me to do it - I've been slapped around pretty hard before for not following "proper" or "common" conventions like this. :/


    After you fix up your plugin.yml, post if you have anymore issues. And without the right code from your main class, we can't tell if you're registering your events.

    Also, checking for each block type all the time can be very messy. You can make an array list and populate it with all of the blocks you want to mine more slowly, then every time a player goes to mine a block, just check if the block material is in the array list. Try something like this (NOTE: Typed in Notepad++, not Eclipse - may have syntax and/or other errors, but the main point is well made):

    Code:java
    1. public class MiningListener implements Listener {
    2.  
    3. private List<Material> blocks = new ArrayList<Material>();
    4.  
    5. MainClass plugin;
    6.  
    7. /* This is the Listener's constructor - this is what is called when you register the events in MainClass */
    8. public MiningListener(MainClass instance) {
    9. plugin = instance;
    10. }
    11.  
    12. private void populateList() {
    13. blocks.add(Material.DIAMOND_ORE);
    14. blocks.add(Material.IRON_ORE);
    15. blocks.add(Material.COAL_ORE);
    16. blocks.add(Material.GOLD_ORE);
    17. blocks.add(Material.LAPIS_ORE); //May be incorrect - not typing this in Eclipse
    18. blocks.add(Material.REDSTONE_ORE);
    19. blocks.add(Material.EMERALD_ORE);
    20. }
    21.  
    22. @EventHandler
    23. public void onMine(PlayerInteractEvent event) {
    24. if(blocks.contains(event.getClickedBlockBlock().getType()) {
    25. Player player = event.getPlayer();
    26. player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 300, 1));
    27. }
    28. }
    29. }
     
Thread Status:
Not open for further replies.

Share This Page