Solved Causing an event with a material

Discussion in 'Plugin Development' started by TheYamsMan, May 23, 2014.

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

    TheYamsMan

    I am trying to make a plugin to cause lightning to strike where ever a player right clicks, but only if they are holding a gold axe. This is my code:

    Code:
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            World world = player.getWorld();
            if(event.getAction() == (Action.RIGHT_CLICK_BLOCK)){
                if(player.getItemInHand().getType() == Material.GOLD_AXE){
                    Block block = event.getClickedBlock();
                    world.strikeLightning(block.getLocation());
                }
            }
        }
    I also added a listener, but it still doesn't work:

    Code:
    public class superaxe extends JavaPlugin implements Listener{
     
        public final Logger logger = Logger.getLogger("Minecraft");
     
        public static superaxe plugin;
     
        @Override
        public void onDisable(){
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Disabled");
            getServer().getPluginManager().registerEvents(this, this);
        }
    Everytime I do this, whether I change it to right of left clicking, the lightning will not strike. I have changed the code a few times, but nothing works. Am I doing something wrong? Can someone please tell me the write code?
    Also, please use my tag so that I can come back easily, thanks! :)
     
  2. Offline

    Bram0101

    Where you check what the action is you must also check if you right click air. So
    Code:java
    1.  
    2. if(event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR)
    3.  
     
  3. Offline

    fireblast709

    TheYamsMan you register your listener in on disable, while you should register it in onEnable.

    A general note:
    - don't use Logger.getLogger("Minecraft"), use the one provided by JavaPlugin#getLogger()
    - remove that static plugin instance, since you won't need it.

    Bram0101 do note that when you use RIGHT_CLICK_AIR, getClickedBlock() will return null.
     
  4. Offline

    TheYamsMan

    fireblast709
    So...um...ok just to recap.
    Remove the static plugin. Totally or should I replace it with something?
    What logger should I use? Can you show me the code? I don't know what you are talking about.
    Do you recommend something else other than right_click_air?

    Bram0101
    Thanks for the help!
     
  5. Offline

    fireblast709

    TheYamsMan If you so desperately need a static instance, you can always use
    Code:java
    1. JavaPlugin.getPlugin(<yourmainclass>.class)
    Or pass the instance via constructors
    Code:java
    1. public class SomeRandomClass
    2. {
    3. private final YourMainClass plugin;
    4.  
    5. public SomeRandomClass(YourMainClass plugin)
    6. {
    7. this.plugin = plugin;
    8. }
    9.  
    10. // Other code
    11. }
    12.  
    13. // Now from your main class, it would look like this
    14. new SomeRandomClass(this);
    The logger I am talking about resides in JavaPlugin, which is the class that your main class extends. So in your main class, just call
    Code:java
    1. this.getLogger();
    which gives you a Logger instance specially created for your plugin.

    You can still use RIGHT_CLICK_AIR, especially if you would like a greater range than just the blocks around the player. In that case you would have to manually find the target block, since event.getClickedBlock() would return null. Here I recommend a BlockIterator, a class found in the util package of Bukkit.

    Example usage:
    Code:java
    1. public Block getTarget(Player player, int range)
    2. {
    3. BlockIterator bi = new BlockIterator(player, range);
    4. Block target = null;
    5. while(bi.hasNext())
    6. {
    7. target = bi.next();
    8. // In this example we only consider AIR blocks as
    9. // transparent.
    10. if(target.getType() != Material.AIR)
    11. break; // Since we found our block, no need to continue looking
    12. target = null;
    13. // Make sure that if we don't find a block in the loop, that we set it to null
    14. // This is used to indicate that we didn't find the block when we return
    15. }
    16. return target;
    17. }
     
  6. Offline

    TheYamsMan

    fireblast709 Thank you soo much!

    fireblast709 When I put the other logger in, tons of errors pop up. Lots of misplaced constructs and also it says "The field JavaPlugin.logger is not visible." DO I need to change my onEnable and onDisable things? EDIT: Where do I put in the right clicking part into the blockIterator? When I simply add the rightclick.AIR thing, I strike lightnig, but on myself. ???
    Code:
    Code:java
    1. public void onPlayerInteract(PlayerInteractEvent event){
    2. Player p = event.getPlayer();
    3. World world = p.getWorld();
    4. if(event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction == Action.RIGHT_CLICK_AIR){
    5. If(p.getItemInHand().getType() == Material.GOLD_AXE){
    6. Location loc = p.getEyeLocation();
    7. world.strikeLightning(loc);
    8. }
    9. }
    10. }


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

    itzrobotix

    Or Player.getTargetBlock(null to ignore air, range as int);
     
  8. Offline

    fireblast709

    itzrobotix deprecated. My code would pretty much do the same, but evades the 'magic values' (material IDs)

    TheYamsMan You use this.getLogger(), for example in onEnable(). Also, the right clicking part goes into the event handling method (the one annotated with @EventHandler and with the first (and only) parameter PlayerInteractEvent). Then you basically check for event.getAction() == Action.RIGHT_CLICK_AIR, and use the method I provided (and I do suggest to read the documentation to make sure you fully understand it), which will either return the target Block, or null if there was no in the specified radius.
     
  9. Offline

    TheYamsMan

    fireblast709
    It still tells me that "The field JavaPlugin.logger is not visible." Do you know why this is?
    This is my onEnable code right now:
    Code:java
    1. @Override
    2. Public void onEnable(){
    3. PluginDescriptionFile pdfFile = this.getDescription();
    4. this.getLogger();
    5. this.logger.info(pdfFile.getName() + "Version " + pdfFile.getVersion() + " has been enabled");
    6. //this is where my problem is, it has the error on logger in this.logger.info
    7. getServer().getPluginManager().registerEvents(this, this);
    8. //this is for my listener, it works just fine
    9. }

    The same thing happens for my onDisable:
    Code:java
    1. @Override
    2. public void onDisable(){
    3. PluginDescriptionFile pdfFile = this.getDescription();
    4. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has been enabled.");
    5. }

    The logger fit in the right place though. Thanks.
     
  10. Offline

    fireblast709

    TheYamsMan instead of this.logger, use this.getLogger()
     
  11. Offline

    TheYamsMan

    fireblast709
    no .info after?

    fireblast709 When I change it to this.getLogger.info(...) or this.getLogger(...) It says it can't be resolved or is not a field.

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

    fireblast709

    TheYamsMan this.getLogger().info("Hello World")
     
  13. Offline

    TheYamsMan

    fireblast709
    Thank you so much for your help!

    fireblast709
    Sorry, one more thing.
    Can you show me how it fits together? My player interact event wont fit with the publick Block getTarget(....... stuff.

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

    itzrobotix

    Doesn't mean it won't work. I'm pretty sure it has been deprecated for a while now.
     
  15. Offline

    TheYamsMan

    fireblast709
    Can you show me how to fit it together? I cant fit them because they are both public and stuff.
     
  16. Offline

    TheYamsMan

    @fireblast709
    Can you please respond?

    This is what I have for the event check, but I still get errors.
    Code:java
    1. @EventHandler
    2. public void onClick(PlayerInteractEvent event){
    3. if(event.getAction()==Action.RIGHT_CLICK_AIR){
    4. public Block getTarget(Player player, int range){
    5. //get target gives me an error, so does the comma and last parentheses before the curly bracket
    6. BlockIterator bi = new BlockIterator(player, range);
    7. Block target = null;
    8. World world = player.getWorld();
    9. while(bi.hasNext()){
    10. target = bi.next();
    11. if(target.getType() != Material.AIR){
    12. Location loc = target.getLocation();
    13. world.strikeLightning(loc);
    14. }
    15. target = null;
    16. }
    17. return target;
    18. //This give me an error, change to
    19. }
    20. }
    21. }


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

    fireblast709

    TheYamsMan that is because it isn't even valid Java, you cannot just paste a function inside another function.
    Code:java
    1. @EventHandler
    2. public void onClick(PlayerInteractEvent event)
    3. {
    4. if(event.getAction() == Action.RIGHT_CLICK_AIR)
    5. {
    6. BlockIterator bi = new BlockIterator(player, range);
    7. Block target = null;
    8. World world = player.getWorld();
    9. while(bi.hasNext())
    10. {
    11. target = bi.next();
    12. if(target.getType() != Material.AIR)
    13. {
    14. Location loc = target.getLocation();
    15. world.strikeLightning(loc);
    16. break;
    17. }
    18. }
    19. }
    20. }
     
  18. Offline

    TheYamsMan

    fireblast709 OK thanks, sorry for sending you messages and whatnot. i was just a bit anxious...XD

    ALSO, I know that isn't valid, I just wasn't sure what to take out or what to move and whatever else...

    EDIT: I added the player as event.getPlayer(); ya know, normal, but it still gives me an error on range. Since the declaring the block getTarget(...) thing was taken out, range is not defined as anything. Could you tell me how to get it back in?
     
  19. Offline

    fireblast709

    TheYamsMan it should be defined by you. Like, what do you want the range to be
     
  20. Offline

    TheYamsMan

    fireblast709 I dont know. I am unfamiliar with the block iterator. If I wanted it to be the same as before (with public in it) it would be nested functions. What do you think it should be?
     
  21. Offline

    fireblast709

    TheYamsMan just replace range with any number you want it to be
     
  22. Offline

    TheYamsMan

    fireblast709 I knew that wouldn't be what it sounded like. XD Thank you so much for all your help! I am officially done with this thread!
     
Thread Status:
Not open for further replies.

Share This Page