Solved Needs Help, Trying to add permissions to a event

Discussion in 'Plugin Development' started by kevinjss1, Jan 19, 2015.

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

    kevinjss1

    Hello I am trying to add permissions to this event, I will show the code for the class. (This Plugin will not be on bukkit)
    @EventHandler

    public void onProjectileHit(ProjectileHitEvent event) {

    Player player = (Player) event.getEntity();

    if(!(player.hasPermission("DHBalls.use"))){

    Projectile projectile = event.getEntity();

    if (projectile instanceof Snowball) {

    Snowball snowball = (Snowball) projectile;

    snowball.getWorld().createExplosion(snowball.getLocation(), 3F);

    Thanks, Kevin
     
  2. Offline

    SuperOriginal

    You're casting the projectile to a Player and a Snowball...
     
  3. Offline

    Skionz

    @kevinjss1
    1. Check before casting.
    2. Make your code legible.
    3. Since ProjectileHitEvent#getEntity() returns a Projectile and a Player is not a Projectile your code is all wrong resulting in a massive amount of stack-traces. Refer to number 1. as well.
    4. A projectile cannot have permissions.
     
    Konato_K and SuperOriginal like this.
  4. Offline

    kevinjss1

    OK, thanks to both of you, I will try to fix the error. If I need any help I will ask.
    Thanks Again
    ~Kevin

    @Skionz
    May I ask you to give me a short example.
    If it wasn't clear from the code supplied, I am try to restrict if the snowball explodes or not. I have looked on many youtube channels and text tutorials and yet not one shows how to do this.

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

    SuperOriginal

    @kevinjss1
    1. ProjectileHitEvent
    2. if projectile is an instance of a snowball, and
    3. if Projectile#getShooter() is an instance of player, cast it.
    4. If Player has perms, create explosion
     
    Last edited: Jan 20, 2015
  6. Offline

    kevinjss1

    @SuperOriginal I am sorry to continue bothering you but I still am having the problem I have attached the code again

    Code:
    package org.kevinjss1.bukkit.DHFBalls;
    
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
    
    
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }
    
        @EventHandler
        public void onProjectileHit(ProjectileHitEvent event) {
            Projectile projectile = event.getEntity();
                if (projectile instanceof Snowball) {
                    Snowball snowball = (Snowball) projectile;
                    if (Projectile.getShooter instanceof Player){
                    if shooter.hasPermission("DHBalls.use"){
                    snowball.getWorld().createExplosion(snowball.getLocation(), 3F);
                }
            }
    
        }
        }
    }
     
  7. Offline

    Skionz

    @kevinjss1 The 'getShooter()' method is not static and needs an object to be invoked on. 'shooter' also does not exist and your syntax is incorrect.
     
    kevinjss1 likes this.
  8. Offline

    kevinjss1

    @Skionz that fixed one of my issues, I am very thank of it being gone but I still can't fix this I have adjusted my syntax and used every thing that I think would fit in the following:
    Code:
                    if (snowball.getShooter instanceof Player){
                    if Player.hasPermission("DHBalls.use"){
                    snowball.getWorld().createExplosion(snowball.getLocation(), 3F);
    Full code:
    Code:
    package org.kevinjss1.bukkit.DHFBalls;
    
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
    
    
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }
    
        @EventHandler
        public void onProjectileHit(ProjectileHitEvent event) {
            Projectile projectile = event.getEntity();
                if (projectile instanceof Snowball) {
                    Snowball snowball = (Snowball) projectile;
                    if (snowball.getShooter instanceof Player){
                    if Player.hasPermission("DHBalls.use"){
                    snowball.getWorld().createExplosion(snowball.getLocation(), 3F);
                }
            }
    
        }
    }
    }
    
     
  9. Offline

    Skionz

    @kevinjss1 'hasPermission' is not static either... Java basics are an obvious prerequisite to an API written in Java.
     
  10. Offline

    kevinjss1

    I had started to learn Java but had lost interest but then I started with bukkit. Do you know of any good tutorials where I can learn both?

    I have done some research, would this be correct?
    Code:
    if(!player.!PermissionManager.hasPerm(p,"Perm.Node",false))
    Dosent seem to work.

    <posts merged . Please use the edit button instead of spamming new posts. ~eya>
     
    Last edited by a moderator: Jan 20, 2015
  11. Offline

    Skionz

    @kevinjss1 Look on oracle's website. I am pretty sure that wouldn't compile.
     
  12. ...
    Code:java
    1.  
    2. if(event.getEntity().getShooter() instanceof Player) { //With this we can check if the shooter is a player
    3. Player player = event.getEntity().getShooter(); //If the shooter is a player we get the player
    4. //WIth this we can check if the player has the right permission
    5. if(!player.hasPermission("Perm.Node"); { <You can add a no permission message here> return; }
    6. }
     
  13. Offline

    SuperOriginal

  14. What ¿?
     
  15. Offline

    SuperOriginal

    @MaTaMoR_ When you get the shooter, you need to cast it to player
     
    Konato_K likes this.
  16. Player player = event.getEntity().getShooter(); ?
     
  17. Offline

    SuperOriginal

  18. Offline

    1Rogue

     
  19. ah... i forget about it :p i write it here xD
     
  20. Offline

    kevinjss1

    Would this be correct?
    Code:
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onProjectileHit(ProjectileHitEvent event) {
            Projectile projectile = event.getEntity();
                if (projectile instanceof Snowball) {
                    Snowball snowball = (Snowball) projectile;
                    if(event.getEntity().getShooter() instanceof Player) {
                        @SuppressWarnings("deprecation")
                        Player player = (Player) event.getEntity().getShooter();
                        if(!player.hasPermission("Perm.Node"));
                    snowball.getWorld().createExplosion(snowball.getLocation(), 3F);
                }
     
  21. Offline

    CraftCreeper6

    @kevinjss1
    No. You need too add 4 curly braces.
     
  22. Offline

    Skionz

    I don't know why I have to say this so much. Learn Java. You need to understand the syntax before anything else. You can't speak spanish if you don't know how to conjugate.
     
    Konato_K likes this.
  23. Offline

    SuperOriginal

    Man... I learn new analogies every day
     
    Skionz likes this.
Thread Status:
Not open for further replies.

Share This Page