Right Click Compass Action

Discussion in 'Plugin Development' started by AlpacaKyle, Jun 13, 2020.

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

    AlpacaKyle

    I want to carry out an action when I right click on a compass on a block or in the air. My code doesn't give me any errors at all but it doesn't work as intended.
    My code:
    Code:
    package main;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class MainClassManHunt extends JavaPlugin {
        @Override
        public void onEnable() {
            Player p = (Player) this.getServer().getPlayer("123456789");
            Player p2 = (Player) this.getServer().getPlayer("123456789");
           
            p.addPotionEffect((new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 999999999, 1)));
           
            p2.addPotionEffect((new PotionEffect(PotionEffectType.SPEED, 400, 1)));   
           
        }
        @Override
        public void onDisable() {
        }
    
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Action a = event.getAction();
            ItemStack is = event.getItem();
            if(a == Action.PHYSICAL || is == null || is.getType()== Material.AIR)
                return;
            if(is.getType() == Material.COMPASS);
                event.getPlayer().sendMessage("hehe");
        }
    
    }
    
     
  2. Offline

    Wick

    You need to register events to use them

    This resource shows you how
     
  3. Offline

    AlpacaKyle

    I have updated my code but it still seems to not work properly. here is the updated code:
    Code:
    package main;
    
    import org.bukkit.Material;
    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.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class MainClassManHunt extends JavaPlugin implements Listener{
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(new MainClassManHunt(), this);
            Player p = (Player) this.getServer().getPlayer("Also_AlpacaKyle");
            Player p2 = (Player) this.getServer().getPlayer("Aeromechanics");
           
            p.addPotionEffect((new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 999999999, 1)));
           
            p2.addPotionEffect((new PotionEffect(PotionEffectType.SPEED, 400, 1)));   
           
        }
        @Override
        public void onDisable() {
        }
    
        @EventHandler
        public void onPlayerUse(PlayerInteractEvent event){
            Player p = (Player) this.getServer().getPlayer("Also_AlpacaKyle");
            Player p2 = (Player) this.getServer().getPlayer("Aeromechanics");
        
            if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
                if(p.getItemInHand().getType() == Material.COMPASS){
                    p.setCompassTarget(p2.getLocation());
                    p2.setCompassTarget(p.getLocation());
                }
            }
        }
    }
     
  4. Offline

    Wick

    You can't create more then one instance of your main class, just use the this keyword for an instance of your main class (the first parameter in your constructor)
     
  5. Offline

    AlpacaKyle

    OK I got the code to run when the player right-clicks with the compass, but p2's compass is pointing towards him from my point of view so whenever I move MY head it changes HIS compass according to wherever I am looking. I would like the compasses to point to the other person from their own point of view but for some reason it is backwards in a way. How do I fix this?
     
Thread Status:
Not open for further replies.

Share This Page