Paintball Plugin Help

Discussion in 'Plugin Development' started by Nick_Titan, Jun 27, 2015.

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

    Nick_Titan

    Hi so i am making a paintball plugin and what it does is when you click with a stick it throws an egg and then whoever it hits does no damage but instead blinds them like the paint is covering there eyes. But my problem is when i coded it the eggs come out even when its not a stick in their hand like its suposed to. Also it doesnt matter what damage type it is the blindness still occurs. Here is my code:
    Code:
    package me.runefist;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class PlayerListener implements Listener {
       
        public PlayerListener(EventHandle plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);   
        }
       
        @EventHandler
        public void blank(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            if (player.getItemInHand().equals(Material.STICK));
            player.throwEgg();
        }
       
        @EventHandler
        public void dmg(EntityDamageEvent event) {
            Entity e = event.getEntity();
            if (e instanceof Player) event.setCancelled(true);
            Player player = (Player)e;
            if (DamageCause.PROJECTILE != null);
            player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 90, 1));
        }
    }
     
  2. Offline

    Tecno_Wizard

    @Nick_Titan, well, this will likely break a server...
    Get the damaged entity and check to see if it is an egg first of all. Only then should you cancel the event.
    You're casting the damaged entity to a player without checking to see if it is truly a player.
    You ended the line if damaged is projectile instead of leaving it as a condition for the following line.
     
  3. Offline

    Nick_Titan

    @Tecno_Wizard Thanks for the help but i'm a nooby coder and i dont get what your saying could you possibly touch up my code and repaste it to show me?
     
  4. Offline

    Tecno_Wizard

    @Nick_Titan, I apologize for not responding. I never got a notification.

    The problem.with that is that you wouldn't be learning how to fix it, but I will tell you what you need to do.

    I believe the damage event has a get damager method. See what the damager is.
    You should only cast the damaged entity to a player inside an if statement that won't run if the entity isn't a player. Use instanceof.
    Remove the semicolon at the end of the projectile check.
     
  5. @Nick_Titan

    Yes the first mistake is you have a semicolon after your "if" statement. This makes the statement do nothing. Put brackets in place of the semicolon. Inside those brackets, put the player.throwEgg(); method.

    Proper if statement:

    Code:
    if (true) {
    
    }
    This means if whats inside the parenthesis equals true, it will run the code inside the brackets.

    Do the same for the if statements in the other event listener.

    https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
     
  6. Offline

    The_BloodHound

    For throwing the egg, on stick click:
    Code:
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
    Player p = e.getPlayer();
    if(p.getAction().equals(Action.RIGHT_CLICK_AIR) && p.getItemInHand().equals(Material.STICK)){
    p.throwEgg();
    }
    }
    Basically, what this does, when the player right clicks and the item that's in his hand is a Stick, it throws and egg.
    For the blinding effect part, I'm very confused with your code so here's mine:
    Code:
        @EventHandler
    
        public void onEntityDamage(EntityDamageEvent e){
    
            Player p = (Player) e.getEntity();
    
            if (DamageCause.PROJECTILE != null){
    
                p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 90, 1));
    
            }
    
        }
    
    If this code doesn't work, tell me because I couldn't test this as I was tired when I made this xD
     
Thread Status:
Not open for further replies.

Share This Page