Solved Taser

Discussion in 'Plugin Development' started by TheMcScavenger, Jul 14, 2013.

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

    TheMcScavenger

    Basically, I want to have a player paralysed when I right click them with shears. So I wrote this quick little code, denying them from walking and jumping for 5 seconds, and blinding them for 2. I love how it works, I just can't get it to work so it adds it to the player I right click.

    No errors, nothing, just won't give the player the desired effects.

    Code: http://pastebin.com/4McDRCWE
    Code:
    @EventHandler
    public void onPlayerInteractEntityEvent(final Player player, final Entity clickedPlayer){
    if (clickedPlayer instanceof Player) {
    Player targetPlayer = (Player) clickedPlayer;
    Location targetLocation = targetPlayer.getLocation();
    targetPlayer.getWorld().strikeLightning(targetLocation);
    player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 100, 100));
    player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 100, -100));
    player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 40, 100));
    }else{
    player.sendMessage(ChatColor.DARK_RED + "You must click a player!");
    }
    }
    
     
  2. Offline

    ZeusAllMighty11

    That's not even an event.
     
    microgeek likes this.
  3. Offline

    TheMcScavenger

  4. Offline

    etaxi341

    Code:java
    1. @EventHandler
    2. public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent ev){
    3. Entity clickedPlayer = ev.getRightClicked();
    4. Player player = ev.getPlayer();
    5. if (clickedPlayer instanceof Player) {
    6. Player targetPlayer = (Player) clickedPlayer;
    7. Location targetLocation = targetPlayer.getLocation();
    8. targetPlayer.getWorld().strikeLightning(targetLocation);
    9. player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 100, 100));
    10. player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 100, -100));
    11. player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 40, 100));
    12. }else{
    13. player.sendMessage(ChatColor.DARK_RED + "You must click a player!");
    14. }
    15. }
     
  5. Offline

    TheMcScavenger

    Got an error on .getEntity(): The method is undefined.
     
  6. Offline

    etaxi341

    Wait a moment I start my pc and look what it's called

    .getRightClicked(); Is the right one :) Good Luck with it!

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

    TheMcScavenger

    I just managed to work around it, so I made this, and it doesn't work:

    Code:
    @EventHandler
      public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent ev, final Entity clickedPlayer){
      Player player = ev.getPlayer();
      if (clickedPlayer instanceof Player) {
      Player targetPlayer = (Player) clickedPlayer;
      Location targetLocation = targetPlayer.getLocation();
      targetPlayer.getWorld().strikeLightning(targetLocation);
      targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 100, 100));
      targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 100, -100));
      targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 40, 100));
      }else{
      player.sendMessage(ChatColor.DARK_RED + "You must click a player!");
      }
      }
    
    I'll check your code now.

    EDIT: No, still doesn't work:
    Code:
     @EventHandler
      public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent ev){
      Entity clickedPlayer = ev.getRightClicked();
      Player player = ev.getPlayer();
      if (clickedPlayer instanceof Player) {
      Player targetPlayer = (Player) clickedPlayer;
      Location targetLocation = targetPlayer.getLocation();
      targetPlayer.getWorld().strikeLightning(targetLocation);
      player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 100, 100));
      player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 100, -100));
      player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 40, 100));
      }else{
      player.sendMessage(ChatColor.DARK_RED + "You must click a player!");
      }
      }
    
     
  8. Don't do this
    Code:
    Player player = ev.getPlayer();
    Before you've checked if its actually a player because it will give you an error if your not right clicking a player.
    Also get rid of this
    Code:
    final Entity clickedPlayer
     
  9. Offline

    etaxi341

    BorisTheTerrible But only a Player can trigger this Event! so ev.getPlayer is definitelya Player. But the RightClicked Person can be a Entity so you only need to check ev.getRightClicked

    There is no
    Code:java
    1. final Entity clickedPlayer

    in his new Code I've posted
     
  10. Offline

    TheMcScavenger

    Not working:

    Code:
    @EventHandler
      public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent ev){
      Entity clickedPlayer = ev.getRightClicked();
      if (clickedPlayer instanceof Player) {
      Player targetPlayer = (Player) clickedPlayer;
      Location targetLocation = targetPlayer.getLocation();
      targetPlayer.getWorld().strikeLightning(targetLocation);
      targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 100, 100));
      targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 100, -100));
      targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 40, 100));
      }else{
      Bukkit.getServer().broadcastMessage(ChatColor.DARK_BLUE + "[Server] You must right-click a player!");
      }
      }
    
     
  11. Offline

    etaxi341

    TheMcScavenger Is there an Error? If yes could you tell me which line it is? Or what is happening? Is nothing happening? If this is the case did you imported the Listener in your Main class?
     
  12. Offline

    TheMcScavenger

    As since the first code: No error.
    Whole Main.class:
    Code:
    package com.McScavenger.TaserShears;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    import com.McScavenger.TaserShears.Main;
     
    public class Main extends JavaPlugin implements Listener{
     
    public final Logger logger = Logger.getLogger("Minecraft");
    public static Main plugin;
     
      public void onDisable()
      {
        PluginDescriptionFile pdfFile = getDescription();
        logger.info(pdfFile.getName() + " has been disabled!");
      }
     
      public void onEnable()
      {
        PluginDescriptionFile pdfFile = getDescription();
        logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " has been enabled!");
      }
     
      @EventHandler
      public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent ev){
      Entity clickedPlayer = ev.getRightClicked();
      if (clickedPlayer instanceof Player) {
      Player targetPlayer = (Player) clickedPlayer;
      Location targetLocation = targetPlayer.getLocation();
      targetPlayer.getWorld().strikeLightning(targetLocation);
      targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 100, 100));
      targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 100, -100));
      targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 40, 100));
      }else{
      Bukkit.getServer().broadcastMessage(ChatColor.DARK_BLUE + "[Server] You must right-click a player!");
      }
      }  
    }
    
     
  13. Maybe registring your listener? It is obvious you are new to this, do you even know what you are doing there? My guess is that you just copy pasted around/or just were thinking, let's skip the whole "let's take a look at how to use bukkit" part, that's not how it works, first follow the tutorials provided by bukkit itself and then go look around for some other tutorials on youtube/other places. Then, my young padawan, we will be able to help you.

    Edit: taking a look at your plugins doesn't confirm anything of what I just said above, so tell me, what happend?
     
    etaxi341 likes this.
  14. Yah sorry didn't pay attention to the type of event it was.
     
  15. Offline

    etaxi341

  16. Offline

    TheMcScavenger

    I have been following some tutorials, and I am kinda new to this. I just wanted this on a server, and thought I'd make it. Turns out I ran into a problem. So if you could possibly tell me how, it'd be great.
     
  17. Add this: Bukkit.getPluginManager().registerEvents(this, this);
     
    etaxi341 likes this.
  18. Offline

    TheMcScavenger

    Thank you all so much for helping, it works now!
     
Thread Status:
Not open for further replies.

Share This Page