Solved Help with codes

Discussion in 'Plugin Development' started by Reynergodoy, Apr 23, 2015.

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

    Reynergodoy

    Hi again guys,
    as you know, I'm a newbie at coding in java

    I'm wondering if you can tell me a code to do the following things (with @EventHandler ( thats logic :p )):
    -If a player is with a coal in the hand
    -If he right-clicks the coal
    -All players in a 6 blocks radius become poisoned


    I want it to be like this:
    Code:
       
        @EventHandler
        public void Interact(EntityDamageByEntityEvent e)
    {
    if ((e.getEntity() instanceof LivingEntity) && (e.getDamager() instanceof Player))
    {
      Player damager = (Player)e.getDamager();
      if (KitAPI.Darkmage.contains(damager.getName()) &&
        (damager.getInventory().getItemInHand() != null) &&
        (damager.getInventory().getItemInHand().getType() == Material.IRON_SWORD) &&
        (Math.random() < 0.5D)) {
        ((LivingEntity)e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.POISON, 120, 1));
      }
    }
    }
    As you can see, the code above means that :
    If the player is with a iron sword in the hand, and attacks another PlayerEntity
    The other player will become poisoned


    But i don't know to to put the radius, and the right click! ( return to the start of the thread to read my specifications, it's red )


    FIXED
    New Code
    New Code (open)

    Code:
        
    @EventHandler
        public void onUseCoal(PlayerInteractEvent e)
        {
            final Player p = e.getPlayer(); //get the player from the event
            ItemStack i = e.getItem(); //get the item being interacted with
            if (KitAPI.Gutin.contains(p.getName()))
            if(e.getAction() == Action.RIGHT_CLICK_AIR||e.getAction() == Action.RIGHT_CLICK_BLOCK) //get the click type
            {
                if(i != null && i.getType().equals(Material.COAL)) //make sure the item is coal
                {
                    p.sendMessage("Poisoning players around you");
                    List<Entity> nearby = p.getNearbyEntities(6.0D, 6.0D, 6.0D); //get nearby entities
                      for (Entity en : nearby) {
                        if (en.getType() == EntityType.PLAYER) { //if they are a player
                            Player p2 = (Player) en; //cast a player
                            p2.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 50, 1)); //poison them
                            p2.sendMessage("All your health are belong to us");
                        }
                      }
                }
            }
        }
    
    }
           @EventHandler
           public void Interact(PlayerDeathEvent e) {
               {
                   Player p = e.getEntity();
               KitAPI.Gutin.remove(p.getName());
    }
    }}}
    
     
    Last edited by a moderator: Apr 23, 2015
  2. Offline

    Koobaczech

    Maybe this?
    Code:
        @EventHandler
          public void onUseCoal(PlayerInteractEvent e) //Interact(click) event
          {
              final Player p = e.getPlayer(); //get the player from the event
              ItemStack i = e.getItem(); //get the item being interacted with
              if(e.getAction() == Action.RIGHT_CLICK_AIR||e.getAction() == Action.RIGHT_CLICK_BLOCK) //get the click type
              {
                  if(i != null && i.getType().equals(Material.COAL)) //make sure the item is coal
                  {
                      p.sendMessage("Poisoning players around you");
                      List<Entity> nearby = p.getNearbyEntities(6.0D, 6.0D, 6.0D); //get nearby entities
                        for (Entity en : nearby) {
                          if (en.getType() == EntityType.PLAYER) { //if they are a player
                              Player p2 = (Player) en; //cast a player
                              p2.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 50, 1)); //poison them
                              p2.sendMessage("All your health are belong to us");
                          }
                        }
                  }
              }
          }
    
     
    Last edited: Apr 23, 2015
    Reynergodoy likes this.
  3. Offline

    ColonelHedgehog

    It's best to learn Java before all else.
     
    Konato_K and teej107 like this.
  4. Offline

    teej107

    Maybe explain the code.
     
    Reynergodoy likes this.
  5. Offline

    Koobaczech

    @teej107 My bad bro! Added some comments
     
    Reynergodoy likes this.
  6. Offline

    Reynergodoy

    oh you're good at this :D
    i will test it, thx

    oh man dont be so rude ;-; i'm only using the bukkit reference for 2 days, i cant now that all

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

    Koobaczech

    Thats general code! Make sure to add checks to not poison the calling player, not poison already poisoned players, change the potion strength and length, and fix any other errors in my code! I write it very fast sorry if it doesn't work lol
     
    Reynergodoy likes this.
  8. Offline

    Reynergodoy

    yes, it doesnt poison me, but i'm without anyone to test it, but i think its ok :)
    thx for your help again :)
     
  9. Offline

    Koobaczech

    Np my friend!! Anything you need lmk
     
    Reynergodoy likes this.
  10. Offline

    ColonelHedgehog

    I didn't intend to be rude. You mentioned you are a "newbie" at Java, so I suggested that you brush up on those skills before tackling Bukkit. :)
     
    Reynergodoy likes this.
  11. Offline

    Reynergodoy

    oh :)
    but i know a bit about java, i was just trying to make a simple plugin of KitPvP, thats logic that i'm not on your level, as i'm only coding on bukkit for 2 days :)
     
  12. Offline

    teej107

    @Reynergodoy Developing with the Bukkit API for 2 days is fine. But......
    Just my advice.
     
Thread Status:
Not open for further replies.

Share This Page