Solved "Player p" problem

Discussion in 'Plugin Development' started by Uhlala, Feb 4, 2017.

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

    Uhlala

    I am getting an error when I digit "Player p = p.getPlayer()"; http://prntscr.com/e4lsbe ; It says "The local variable p may not have been initialized". Can someone help me please?

    Code:
    package BUPAPABUPAPA;
    
    import java.awt.List;
    import java.util.ArrayList;
    import java.util.Arrays;
    
    import javax.swing.text.html.parser.Entity;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Herobrine
      implements Listener
    {
           
       
       
      @EventHandler
      void rightClick(PlayerInteractEvent e)
      {
    
         
          ItemStack hbsword = new ItemStack(Material.DIAMOND_SWORD);
          ItemMeta im = hbsword.getItemMeta();
          im.setDisplayName(ChatColor.AQUA + "Herobrine Sword");
          im.setLore(Arrays.asList(ChatColor.DARK_PURPLE + "Queime seus amiguinhos"));
          hbsword.setItemMeta(im);
         
        if ((e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction() == Action.RIGHT_CLICK_BLOCK))
        {
           
            ItemStack item = new ItemStack(Material.DIAMOND_SWORD, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.AQUA + "Herobrine Sword");
            java.util.List<String> lore = new ArrayList<String>();
              lore.add(ChatColor.DARK_PURPLE + "HB");
              meta.setLore(lore);
              item.setItemMeta(meta);
             
            Player p = p.getPlayer();
    
              if(p.getItemInHand().hasItemMeta()){
                if(p.getItemInHand().getItemMeta().hasDisplayName()){
                  if(p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.AQUA + "Herobrine Sword")){
             
               e.getClickedBlock().getWorld().strikeLightning(e.getClickedBlock().getLocation());
              
            PotionEffect effect = new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 20, 4, false, false);
            p.addPotionEffect(effect);
            p.setLevel(0);
            p.sendMessage(ChatColor.AQUA + "Boom");
            p.getWorld().playSound(p.getLocation(), Sound.ENDERMAN_DEATH, 20.0F, 20.0F);
            PotionEffect fr = new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 10 * 20, 0, false, false);
            p.addPotionEffect(fr);
          }else{
              return;
         }
        }
       }
      }
    }
    }
     
  2. Offline

    mehboss

    @Uhlala
    Change the variable:
    Code:
    Player p = p.getPlayer();
    
    To the variable:
    Code:
    Player p = e.getPlayer();
     
  3. Offline

    Zombie_Striker

    Plugins with 1 file directorys do not work. Use the format "me.<your name>.<your project>"

    Wrong entity. Use the org.Bukkit import

    Instead of creating this sword for every single time a player clicks anything, only create it once when the class is created.

    Never let a variable reference itself. What you meant was "e.getPlayer()"

    This does nothing. Replace this with just "}"
     
  4. Offline

    Uhlala


    I didn't understand this part : Instead of creating this sword for every single time a player clicks anything, only create it once when the class is created; Sorry I am new with these stuff

    it worked thank you so much, but could you explain me that part that I mentioned last reply?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 4, 2017
  5. Offline

    mehboss

    @Uhlalal
    Give them the sword once in a join event or something instead of giving them it everytime they right click.
     
  6. Offline

    Uhlala

    oh thanks

    Is there a way to modify the lightning damage?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 4, 2017
  7. Offline

    mehboss

    @Uhlala
    There may be another way but here is a way.
    1. send a lightning packet that doesn't damage the player by doing
    Code:
    ((CraftPlayer) (Player) Sender).getHandle().playerConnection.sendPacket(new PacketPlayOutSpawnEntityWeather(new EntityLightning(((CraftPlayer) (Player) Sender).getHandle().getWorld(), ((Player) Sender).getLocation().getX(), ((Player) Sender).getLocation().getY(), ((Player) Sender).getLocation().getZ(), false, false)));
    ((CraftPlayer) (Player) Sender).getHandle().playerConnection.sendPacket(new PacketPlayOutNamedSoundEffect("ambient.weather.thunder", ((Player) Sender).getLocation().getX(), ((Player) Sender).getLocation().getY(), ((Player) Sender).getLocation().getZ(), 10000.0F, 63));
    2. Then get the players health and subtract the amount of health you want the lightning to do.

    There is probably a bukkit implemented way, not sure.
     
  8. Offline

    Disgastings

    Player p = event.getPlayer();
     
  9. Just saying, you don't register your events
     
  10. Online

    timtower Administrator Administrator Moderator

    Do note that this is a listener only class.
    Registering can be done from any class (like the main)
     
Thread Status:
Not open for further replies.

Share This Page