Egg Location Break Block

Discussion in 'Plugin Development' started by HyrulesLegend, Dec 3, 2013.

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

    HyrulesLegend

    Hey guys, I'm trying to make a plugin so wherever an egg lands when shot by an iron shovel (code in a sec), it breaks the block it lands on.. Any ideas on how I should go about doing this?

    Code that I'm currently using to launch the projectile egg:
    Code:java
    1. @EventHandler
    2. public void OnPlayerInteract(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4. if(p.getItemInHand().getType().equals(Material.IRON_SPADE)) {
    5. final Item Egg = p.getWorld().dropItem(p.getEyeLocation(), new ItemStack(Material.EGG));
    6. Egg.setVelocity(p.getEyeLocation().getDirection());
    7. new BukkitRunnable() {
    8. public void run() {
    9. //break block here
    10. }
    11. }.runTaskLater(plugin, 60);
     
  2. Offline

    rocket138

    You could try something like
    Code:
     public void onProjectileHit(ProjectileHitEvent event) {
            if(event.getEntity() instanceof Egg && event.getEntity().getShooter() instanceof Player) {
                Player player = (Player) event.getEntity().getShooter();
                if(player.hasPermission("egg.explode")) {
                    event.getEntity().getLocation().getBlock().setType(Material.AIR);
       
                }
            }
        }
    I don't know if it will work, but something like that should
    You could just add a test world line or like test if that player that shot it is in the match line

    You wouldn't really need the bukkit scheduler if you used this

    It'd be nice if you responded and told me whether or not that worked...

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

    HyrulesLegend

  4. Offline

    rocket138

    Did you try it without the permission?
     
  5. Offline

    HyrulesLegend

  6. Offline

    rocket138

    You could make it blow up any blocks within a 1 block radius using
    Code:
    event.getEntity().getWorld().createExplosion(event.getEntity().getLocation(), 1.0f, false);
    If that would work for whatever you're going for.
    NOTE: that doesn't break every type of block.

    Also, did you register the event on my first one, and put @EventHandler before it? because it should work.

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

    maxben34

    Use a projectile hit event. Check that the projectile is an egg. Check that the shooter is the player. Get the location of the block it hits and getBlock().setType(material.AIR);

    Shouldn't be too hard. If you need help ask though.

    Max_The_Link_Fan
     
  8. Offline

    CrazyGuy3000

    Dangit, you beat me there.
     
  9. Offline

    HyrulesLegend

    maxben34 It didn't work, I'm sure I probably did something wrong, or maybe it's the way I'm launching the egg, but here's my current code.
    Code:java
    1. @EventHandler
    2. public void OnPlayerInteract(PlayerInteractEvent e) {
    3. final Player p = e.getPlayer();
    4. if(p.getItemInHand().getType().equals(Material.IRON_SPADE)) {
    5. final Item Egg = p.getWorld().dropItem(p.getEyeLocation(), new ItemStack(Material.EGG));
    6. Egg.setVelocity(p.getEyeLocation().getDirection());
    7.  
    8.  
    9. }
    10.  
    11.  
    12. }
    13.  
    14. @EventHandler
    15. public void onEggHit(ProjectileHitEvent e) {
    16. Projectile p = e.getEntity();
    17. EntityType s = e.getEntityType();
    18. if(s.equals(EntityType.PLAYER)) {
    19. if(e.equals(EntityType.EGG)) {
    20. p.getLocation().getBlock().setType(Material.AIR);
    21.  
    22. }
    23. }
    24.  
    25. }
    26.  
    27. }
    28.  
     
  10. Offline

    M0n0clink

    Max_The_Link_Fan I think you should put
    Code:java
    1. if(s instanceof EntityType.PLAYER){

    This was not tested btw.
     
  11. Offline

    HyrulesLegend

  12. Offline

    maxben34

    The egg will never break as you are just spawning an egg entity (not a thrown projectile) and giving it a set velocity vector.

    Use player.shootEgg or something like that and then set the velocity and you should get better results.
     
  13. Offline

    HyrulesLegend

    maxben34 I tried using p.launchProjectile, but egg never worked.
     
  14. Offline

    maxben34

    Well is there a p.shootEgg? I would check the doxygen but I'm busy ATM and I can't.
     
  15. Offline

    HyrulesLegend

    maxben34 There is not sadly, there's only p.shootArrow.
     
  16. Offline

    maxben34

    You gotta do a little bit here since I'm not on a computer... Try p.throwEgg or just do launchprojectile

    Edit: I did some research. throwEgg is depreciated. Use launchProjectile(egg.class) instead. Max_The_Link_Fan

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

    rocket138

    Just for the record, I said this first :p
     
  18. Offline

    maxben34

    Just for a record, this isn't a matter of stating who said what first. It is a matter of helping the original poster solve their problem. :p
     
  19. Offline

    rocket138

    Just for the record, I know :p
     
  20. Offline

    HyrulesLegend

    Just for the record, testing
    that now. :3

    maxben34 The launching worked with that, but the issue still stands of the Block not breaking. Current code:
    Code:java
    1. @EventHandler
    2. public void onEggHit(ProjectileHitEvent e) {
    3. Entity en = e.getEntity();
    4. if(en instanceof Egg) {
    5. en.getLocation().getBlock().setType(Material.AIR);
    6. }
    7. }
    8. }


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

    maxben34

    You have to check if it's an instance of EntityType.Egg i think... or projectile etc.

    The if statement isn't being called because en instanceof Egg doesn't mean anything. Java doesn't know what "Egg" is.
     
  22. Offline

    HyrulesLegend

    maxben34 Java doesn't know, but there's an import in Bukkit for Egg. Also, if(en instanceof EntityType.EGG) just returns a "Fix Project Set up" error.
     
  23. Offline

    DevVersion

    Or use the class BlockIterator
     
  24. Offline

    nickjryan95

    Not sure if you fixed this or not, but try getting the block it hits instead of the block it lands on. If I am thinking this through correctly, you are getting the coordinates of the air space that it is above the block that you want to remove, and the code is just setting the air above the block back to air.
     
Thread Status:
Not open for further replies.

Share This Page