ThrownEXP directly into inv

Discussion in 'Plugin Development' started by HuppdoDev, Feb 18, 2016.

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

    HuppdoDev

    Hey guys,

    I'm new to Java and I need help with something. I'm trying to write a small plugin that when you throw a EXP bottle it doesn't emit XP. Instead it just gives the user a level. I can't get it to work right, My code is below. Please don't call me a noob, I'm trying to learn. Also, I know I have the event handling up correctly. Its just this part I'm having trouble with.

    Thanks,
    HuppdoDev
    Code:
        @EventHandler
        public void xpThrow(ExpBottleEvent event){
           
            Player player = (Player) event.getEntity();
            event.getEntity().setLevel(player.getLevel() + 50);
           
        }
    }
    
     
  2. Offline

    Zombie_Striker

    @HuppdoDev
    Unless you meant "new to bukkit", it is highly recommend that you learn Java first before even attempting Bukkit. You cannot jump into working with bukkit without fully understanding the way Java and all of it's objects works. Here's the link on where to learn Java:
    https://bukkit.org/threads/plugin-dev-sticky-learning-java-where-to-learn.395662/


    Event.getEntity() returns the Bottle's instance, not the player's instance. You would have to link the exp bottle thrown with the player who threw it if you want to get who threw the bottle.
     
    DarkestCodes likes this.
  3. Offline

    HuppdoDev

    @Zombie_Striker
    I did mean new to bukkit. I have a good understanding of Java. Good enough to write plugins
     
  4. Offline

    Xerox262

    @HuppdoDev To control how much exp it drops you need to do ExpBottleEvent#setExperience(int amount); just set it to 0 to cancel it, because apparently ProjectileHitEvent doesn't implement cancellable, which is not what I remember at all.
     
  5. Offline

    nuno1212sss

    You can just remove the entity on projectilehitevent, I think. (Don't quote me on that)
     
  6. Offline

    HuppdoDev

    @Xerox262
    I'm confused. Bukkit confuses me... Where am I adding this? xD Sorry...
     
  7. Offline

    Xerox262

    @HuppdoDev anywhere in that method body. Normally people (Or at least I do) put cancellation stuff as close to the end as possible, however it will still carry out the rest of your code so long as you don't return.
     
  8. Offline

    HuppdoDev

    @Xerox262
    Using "ExpBottleEvent.setExperience(int 0)" or "ExpBottleEvent.setExperience(exp)" in which I set exp to 0. both times I get this:
    "Cannot make a static reference to the non-static method setExperience(int) from the type ExpBottleEvent"
    Wut?
     
  9. Offline

    Xerox262

    @HuppdoDev Yea, you need to learn Java, I'll help you with this first though, don't expect you to stop half way through.

    The # meant it's an instance method, you need an object of that class to call the method, in your case you have an instance of the class called event.

    Do event.setExperience(amount);
     
  10. Offline

    HuppdoDev

    @Xerox262
    I probably should continue learning Java. Thanks for the help, it really did help a lot. I tried jumping into this too quickly though.

    @Xerox262
    One more question, how do I figure out what player was the one to throw the bottle?
     
    Last edited: Feb 18, 2016
  11. Offline

    Zombie_Striker

    @HuppdoDev
    Listen to ProjectileSpawnEvent. When the project is an XP bottle, save the XP bottle and the player to a Hashmap. Then, in the event you have, get the player who threw the bottle by using the Xp bottle and the hashmap. You can either google how to set up hashmaps or follow one of the tutorials in the link from one of my original posts about where to learn Java.
     
  12. Offline

    Xerox262

    @Zombie_Striker It's a projectile, projectiles have a method to get who threw them.


    @HuppdoDev use your event to get the exp bottle, then use getShooter(); <- Check it's not null, a plugin might spawn one and not set the shooter.
     
  13. Offline

    HuppdoDev

    @Xerox262
    Its coming back as undefined in Eclipse. Does it not have that? I would assume not
     
  14. Offline

    Zombie_Striker

    @HuppdoDev
    Can you post what you tried? It should look like the following:
    Code:
    event.getEntity().getShooter()
    Note that you will have to check if the shooter is an instanceof Player before casting.
     
  15. Offline

    HuppdoDev

    @Zombie_Striker
    I didn't do that. That may have been my problem. I tried
    Code:
    Player player = event.getShooter()
    But when I input your code, it tells me to change something.
    I used
    Code:
    Player player = event.getEntity().getShooter();
    When I do that, it tells me to change it to:
    Code:
    ProjectileSource player = event.getEntity().getShooter();
    
    But then this is invalid:
    Code:
    player.setLevel(player.getLevel() + 50);
    
     
  16. Offline

    Xerox262

    @HuppdoDev Yea, you have to get the exp bottle then get the shooter from that, should be something like event.getEntity(); (Took me a second to realize that's what you were doing in your original post, however you were casing it to player which must've caused some problems.) to get the exp bottle. If you're gonna check that the shooter is an instanceof a player then you don't need to do a null check, they are both eliminated at once.

    Edit: The reason it's asking you to change it to ProjectileSource instead of player is because a projectile can be launched by any of these Ageable, Ambient, Animals, ArmorStand, Bat, Blaze, BlockProjectileSource, CaveSpider, Chicken, ComplexLivingEntity, Cow, Creature, Creeper, EnderDragon, Enderman, Endermite, Flying, Ghast, Giant, Golem, Guardian, Horse, HumanEntity, IronGolem, LivingEntity,MagmaCube, Monster, MushroomCow, NPC, Ocelot, Pig, PigZombie, Player, Rabbit, Sheep, Silverfish, Skeleton, Slime, Snowman, Spider, Squid, Villager, WaterMob, Witch, Wither, Wolf, Zombie

    You need to actually check that it's a player with instanceof, then you can cast it.
     
  17. Offline

    HuppdoDev

    @Xerox262
    So I have to get the entity of the EXP bottle and then check who the shooter of the bottle was?
     
  18. Offline

    Xerox262

    @HuppdoDev You need to check that they were a player, then you can cast them as such then add the levels.
     
  19. Offline

    HuppdoDev

    @Xerox262
    Isn't the thing I would use be:
    Code:
    Player player = event.getEntity().getShooter() && <word> instanceof Player;
    What would I put in <word>?
     
  20. Offline

    Zombie_Striker

    @HuppdoDev
    You would have to create a separate if statement for that.
     
  21. Offline

    MaTaMoR_

    Code:
    if entity istanceof Projectile
     if projectile//getShooter instanceof Player
      Player player : [Player] projectile//getShooter
    [code]
     
  22. Offline

    Xerox262

    @MaTaMoR_ Why would you check if an exp bottle was a projectile?
     
  23. Offline

    MaTaMoR_

    Just change it for the entity you want.
     
  24. Offline

    Xerox262

    @MaTaMoR_What? I mean why would you check if the Exp bottle is a projectile, there's no other entity that can trigger the event, and exp bottle implments Projectile.
     
  25. Offline

    MaTaMoR_

    Weren't you trying to remove the Entity on ProjectileLaunchEvent ?
     
  26. Offline

    Xerox262

    @MaTaMoR_ No, he's just trying to add the exp to a player rather than drop it when they use an xp bottle.
     
Thread Status:
Not open for further replies.

Share This Page