How do I add OP-only to my plugin?

Discussion in 'Plugin Development' started by SneakyToe, Jul 5, 2011.

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

    SneakyToe

    Hello, I'm currently developing a plugin that makes so that snowballs explode.
    And I'm wondering if anyone can help me get the plugin to OP only, or perhaps a config.txt where you put all the admin names in.

    I use a ProjectileListener for this.
     
  2. Offline

    xZise

    You could test if the thrower is an op? All CommandSenders (Player is also a CommandSender) have a "isOp()" function. If you have the snowball, you can get with the "getShooter()" method the shooter. Then you only have to test, if the shooter is a player. And if it is a player you can test if the player is an op:
    Code:
    Snowball ball;
    if (ball.getShooter() instanceof Player) {
        boolean isOp = ((Player) ball.getShooter()).isOp();
    }
    Fabian
     
  3. Offline

    Jaker232

    Then you can use the explosion API to create a explosion.

    TNT has a power of 12, creepers have 3, so you can pick which distance you want. I recommend the power of 3 which is approximately 1/4 of a power of TNT.
     
  4. Offline

    SneakyToe


    As said.. I'm pretty new to Java, a RL friend helped me with some stuff so if you could show me where in my code to put this code I would appreciate it.

    Code:
    package haias;
    
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.event.entity.EntityListener;
    
    public class MyProjectileListener extends EntityListener {
     
        public void onProjectileHit(ProjectileHitEvent event)
        {
            System.out.println(event.getEntity().getClass().getName());
    
            if (event.getEntity().getClass().getName()=="org.bukkit.craftbukkit.entity.CraftSnowball"){
                event.getEntity().getWorld().createExplosion(event.getEntity().getLocation(), 4);
            }
        }
    
        public MyProjectileListener(Main p){}
    }
    
     
  5. Offline

    Jaker232

    If you can just place it somewhere towards the bottom before the last curly bracket and test it, it might work.
     
  6. Offline

    SneakyToe

    Lol.. Funny that the code he sent me didn't even work.

    Doesn't work..

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

    Jaker232

    You could just remove the public MyProjectileListener(Main p){} if you are not going to use it.
    I'll look up the documents and try to find how to create explosions, because I don't want you wasting your time looking for one simple code.

    EDIT: Can't find it, sorry!
     
  8. Offline

    SneakyToe

    It's alright, I think I figured it out..
     
  9. Offline

    Jaker232

    Have you make sure the action is done, then it checks to see if it's ops or using permission node, and do the explosion event?
     
  10. Offline

    SneakyToe

    I'm working on creating a custom Admin list for the plugin, aswell as putting together a blast radius configure.
     
  11. Offline

    Jaker232

    This will require extending your code, adding a new .java file that adds an admin file or config file (you can pick which one, not my decision, but admin file is the best way to go.) I heard there's a tutorial in "Resources" for that.

    It could load the config and plugin, load adminlist.txt or config.yml and then look up admins and grant them it.
     
  12. Offline

    SneakyToe

    Yup, going to work on this tonight, just chillin' and browsing the forums atm.
     
  13. Offline

    Jaker232

    Speaking of forums, I should go back to work. See you around.
     
  14. Offline

    SneakyToe

    Back to work? What do you mean?
     
  15. Offline

    Jaker232

    Working on my plugins.
     
  16. Offline

    SneakyToe

    Oh, add me up on Skype / Steam / MSN / whatever.

    Skype: lydarii
    Steam: TheFaded
    MSN: [email protected]
     
  17. Offline

    Jaker232

    I don't have any contact information, only e-mail and Meebo (IM) but I can create a channel on a IRC if you want to chat with me.
     
  18. Offline

    SneakyToe

    Sure, do that. :)
     
  19. Offline

    krinsdeath

    Code:
        public void onProjectileHit(ProjectileHitEvent event) {
        	Snowball ball = (Snowball) event.getEntity();
        	if (event.getEntity() instanceof Snowball) {
        		Player player = (Player) ball.getShooter();
        		if (player.isOp()) {
        				event.getEntity().getWorld().createExplosion(event.getEntity().getLocation(), 4);
            	}
        	}
        }
     
  20. Offline

    SneakyToe

    Thanks man, lots of bro love to you.
     
  21. Offline

    xZise

    I doubt that this is working!
    Code:
    public void onProjectileHit(ProjectileHitEvent event) {
      if (event.getEntity() instanceof Snowball) {
        Snowball ball = (Snowball) event.getEntity();
        if (event.getShooter() instanceof Player) {
          Player player = (Player) ball.getShooter();
          if (player.isOp()) {
            event.getEntity().getWorld().createExplosion(event.getEntity().getLocation(), 4);
          }
        }
      }
    }
    Fabian
     
  22. Offline

    SneakyToe

    It is working just fine

    Lol.. You were right, it's not working.. When I tried it my server must have lagged or what so ever..

    Please someone post something that works!

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

    xZise

    Actually the code I posted should work. The post from krinsdeath should create errors, if you shoot something else a snowball or somebody (but not players) shoot anything.

    This should work?! Why doesn't it work?

    Fabian
     
  24. Offline

    SneakyToe

    Could you please add your code into my code? I'm not 100% sure where to add it.
     
  25. Offline

    xZise

    Replace your method?
    Code:
    package haias;
    
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Snowball;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.event.entity.EntityListener;
    
    public class MyProjectileListener extends EntityListener {
        public void onProjectileHit(ProjectileHitEvent event) {
            if (event.getEntity() instanceof Snowball) {
                Snowball ball = (Snowball) event.getEntity();
                if (event.getShooter() instanceof Player) {
                    Player player = (Player) ball.getShooter();
                    if (player.isOp()) {
                        event.getEntity().getWorld().createExplosion(event.getEntity().getLocation(), 4);
                    }
                }
            }
        }
    
        public MyProjectileListener(Main p){}
    }
    
     
  26. Offline

    krinsdeath

    Code:
    public void onProjectileHit(ProjectileHitEvent event) {
      Snowball ball = (event.getEntity() instanceof Snowball) ? (Snowball) event.getEntity() : null;
      Player player = (event.getShooter() instanceof Player) ? (Player) event.getShooter() : null;
      if (player == null || ball == null) { return; }
      if (player.isOp()) {
        event.getEntity().getWorld().createExplosion(event.getEntity().getLocation(), 4);
      }
    }
    This is massively condensed, but xZise was right, my first "solution" would have thrown errors had the shooter been anything other than a player.
     
  27. Offline

    xZise

    Ehr sorry, but this code is working (maybe, didn't tested it) but far complexer than the previous versions.

    Fabian
     
  28. Offline

    SneakyToe

    This code is neither working.. Hmm
    When I toss the snowballs the console just spams me errors..

    Code:
    13:56:44 [SEVERE] Could not pass event PROJECTILE_HIT to Explosive Snowballs java.lang.ClassCastException: org.bukkit.event.entity.ProjectileHitEvent cannot be cast to org.bukkit.entity.Projectile         at haias.MyProjectileListener.onProjectileHit(MyProjectileListener.java: 14)         at org.bukkit.plugin.java.JavaPluginLoader$69.execute(JavaPluginLoader.j ava:726)         at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav a:58)         at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j ava:321)         at net.minecraft.server.Entity.die(Entity.java:147)         at net.minecraft.server.EntitySnowball.m_(EntitySnowball.java:181)         at net.minecraft.server.World.entityJoinedWorld(World.java:1190)         at net.minecraft.server.WorldServer.entityJoinedWorld(WorldServer.java:4 8)         at net.minecraft.server.World.playerJoinedWorld(World.java:1172)         at net.minecraft.server.World.cleanUp(World.java:1102)         at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:447)         at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:361)         at net.minecraft.server.ThreadServerApplication.run(SourceFile:422) >
    
     
  29. Offline

    xZise

    Look in the exception:
    Code:
    13:56:44 [SEVERE] Could not pass event PROJECTILE_HIT to Explosive Snowballs java.lang.ClassCastException: org.bukkit.event.entity.ProjectileHitEvent cannot be cast to org.bukkit.entity.Projectile
    at haias.MyProjectileListener.onProjectileHit(MyProjectileListener.java: 14)
    at org.bukkit.plugin.java.JavaPluginLoader$69.execute(JavaPluginLoader.j ava:726)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav a:58)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j ava:321)
    at net.minecraft.server.Entity.die(Entity.java:147)
    at net.minecraft.server.EntitySnowball.m_(EntitySnowball.java:181)
    [...]
    First line is a description about the problem: A class cast exception occur, if you try to cast something to something other, but it isn't possible (in your case ProjectileHitEvent to Projectile). A cast is this with the brackets:
    Code:
    Object x = new String("Hello world");
    String s = (String) x
    If now the variable x couldn't be converted to a String the error occur.

    The rest is a Stack trace: So the method "onProjectileHit()" was called by "execute" which was called by "callEvent" and so on.

    So the location where the error occurs is in the first line on the stack trace:
    Code:
    at haias.MyProjectileListener.onProjectileHit(MyProjectileListener.java: 14)
    This means: In the Package "haias" in the file "MyProjectileListener.java" in the method "onProjectileHit". And in specific in the file "MyProjectileListener.java" in line 14. I don't know what there is, but no code here does a cast to Projectile.

    Fabian
     
Thread Status:
Not open for further replies.

Share This Page