Checking if a player is clicked

Discussion in 'Plugin Development' started by Unscrewed, May 5, 2011.

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

    Unscrewed

  2. Offline

    xXLupoXx

    At the moment I don't think it's possible but in build 747 they added an entity right click event...
    Maybe you should wait for the next RB
     
  3. Offline

    nickguletskii

    Use the EntityTarget event instead.
     
  4. Offline

    Unscrewed

    Code:
    Player robbedPlayer = event.EntityTarget;
    ?
    I don't get it, could you make an example please? :3
    I know it isnt smart, but I'm building my plugin on Build 755,
    Soo how does it work? :D
     
  5. Offline

    xXLupoXx

    I'm not sure cause I didn't use it yet but I would try Action.PLAYER_INTERACT_ENTITY or something...
    Do you use Eclipse? It should autocomplete the Action
     
  6. Offline

    Unscrewed

    I can choose from:

    LEFT_CLICK_AIR
    LEFT_CLICK_BLOCK
    PHYSICAL
    RIGHT_CLICK_AIR
    RIGHT_CLICK_BLOCK
    valueOf(String name)
    values
    valueOf(class name)
     
  7. Offline

    xXLupoXx

    Ok then I would check:
    @Override
    public void onPlayerInteractEntity(PlayerInteractEntityEvent event){

    }
     
  8. Offline

    nickguletskii

  9. Offline

    Unscrewed

    And what should I put here then?
    Code:
            	Player robbedPlayer = ???;
    Sorry I'm fairly new to Java ;)
     
  10. Offline

    xXLupoXx

    Code:
         public void onPlayerInteractEntity(PlayerInteractEntityEvent event){
            Player player;
            Entity ent = event.getRightClicked();
            if(ent instanceof Player){
                player = (Player)ent;
            }
         }
    And then you have the right clicked player ;)
    And don't forget to register the event
     
  11. Are you making a cops and robbers plugin game or something :p
     
  12. Offline

    Unscrewed

    Yh! :D

    Thank I will try this out now <3

    @xXLupoXx
    I really cant find it out, could you try to make it work with this?
    I even tried to make a new class and everything, please, help me:
    Code:
    package me.Unscrewed.RolePlay;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerListener;
    import com.iConomy.iConomy;
    import com.iConomy.system.Account;
    import com.iConomy.system.Holdings;
    
    public class Robber extends PlayerListener {
    	public void onPlayerInteract(PlayerInteractEvent event){ //Can't change this
    		Player player = event.getPlayer();
        	Holdings account = iConomy.getAccount(player.getName()).getHoldings();
    
            if(event.getAction() == Action.RIGHT_CLICK_AIR && RolePlay.RPRobberUsers.contains(player)){
            	Player robbedPlayer = event.???; //You need to check if you clicked the player - I don't really know how, you need to find player entity nearest to you and check if it's near enough - ask on the development forum, I'm sorry :)
            	Holdings robbedPlayerHoldings = iConomy.getAccount(robbedPlayer.getName()).getHoldings();
    			if(!RolePlay.RPRobberUsers.contains(robbedPlayer) && !RolePlay.RPPoliceUsers.contains(robbedPlayer)){ //You check that your robbedPlayer is not a member of robbers or police
            		//----iConomy Part----
    				int amount = 1; //Configurable amount!
    				player.sendMessage(ChatColor.RED + "[RolePlay] " + ChatColor.AQUA + "You robbed " + iConomy.format(amount) + " from someone!"); //iConomy provides function to automatically format balances!
    	        	account.add(amount);  //Player on the server always has an account :) If he doesn't, iConomy creates one
    				robbedPlayerHoldings.subtract(amount);
    	        	//-----------------Is Dead-----------------\\
    	        	if(RolePlay.isRobber && player.isDead()){
    	        		RolePlay.isCivilian = true;
    	        		RolePlay.isPolice = false;
    	        		RolePlay.isRobber = false;
    	        	}
    	        	else{
    	        		// -- empty -- \\
    	        	}
    	        }
            }
    	}
    }
    
    Can someone help me, please? :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 15, 2016
  13. Offline

    xXLupoXx

    I would try to delete the event.getAction() == Action.RIGHT_CLICK_AIR because the event is fired when you right click an entity​
     
  14. Offline

    Acrobot

  15. Offline

    Unscrewed

  16. Offline

    xXLupoXx

    Code:
    package me.Unscrewed.RolePlay;
     import org.bukkit.ChatColor;
     import org.bukkit.entity.Entity;
     import org.bukkit.entity.Player;
     import org.bukkit.event.block.Action;
     import org.bukkit.event.player.PlayerInteractEntityEvent;
     import org.bukkit.event.player.PlayerInteractEvent;
     import org.bukkit.event.player.PlayerListener;
     import com.iConomy.iConomy;
     import com.iConomy.system.Account;
     import com.iConomy.system.Holdings;
     public class Robber extends PlayerListener {
    
        public void onPlayerInteract(PlayerInteractEvent event){ //Can't change this
    
            Player player = event.getPlayer();
            Holdings account = iConomy.getAccount(player.getName()).getHoldings();
    
            if(RolePlay.RPRobberUsers.contains(player)){
    
                    Player robbedPlayer = null;
                    Entity ent = event.getRightClicked();
                    if(ent instanceof Player){
                    robbedPlayer = (Player)ent;
                    }
                    if(robbedPlayer != null){
                    Holdings robbedPlayerHoldings = iConomy.getAccount(robbedPlayer.getName()).getHoldings();
    
                    if(!RolePlay.RPRobberUsers.contains(robbedPlayer) && !RolePlay.RPPoliceUsers.contains(robbedPlayer)){ //You check that your robbedPlayer is not a member of robbers or police
    
                        //----iConomy Part----
    
                        int amount = 1; //Configurable amount!
    
                        player.sendMessage(ChatColor.RED + "[RolePlay] " + ChatColor.AQUA + "You robbed " + iConomy.format(amount) + " from someone!"); //iConomy provides function to automatically format balances!
    
                        account.add(amount);  //Player on the server always has an account :) If he doesn't, iConomy creates one
    
                        robbedPlayerHoldings.subtract(amount);
    
                        //-----------------Is Dead-----------------\\
    
                        if(RolePlay.isRobber && player.isDead()){
                             RolePlay.isCivilian = true;
                             RolePlay.isPolice = false;
                             RolePlay.isRobber = false;
    
                        }
    
                        else{
    
                            // -- empty -- \\
    
                        }
                     }
                }
             }
         }
    
    }
     
  17. Offline

    fullwall

Thread Status:
Not open for further replies.

Share This Page