Errors with Gamemode Checking

Discussion in 'Plugin Development' started by TheLonelyIsland, Jan 13, 2013.

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

    TheLonelyIsland

    Hey Guys,
    I've been making a plugin for my Survival server, it's going to add loads of features that will add aspects to gameplay. One thing I wanted was Blood when a player or mob is hurt. Now... I got it working, the only issue is that even if a player is in creative, something which would have casued damage in survival still causes the blood effect. Now, this is quite an issue.

    I attempted to write some code to rectify it, which can be found on lines 27 through to 29.

    Here's a copy of my current listener. Feel free to point out all my mistakes, and possible reply with a fixed version. If you can help; I will be very grateful.

    Code:
    package com.island219.tgm.bleed;
     
    import org.bukkit.Effect;
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
     
    public class BleedListener
            implements Listener {
     
        Player player;
        GameMode mode = GameMode.SURVIVAL;
     
        @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
        public void onPlayerDamage(EntityDamageEvent event) {
            Entity entity = event.getEntity();
            Location entityLoc = entity.getLocation();
            World world = entity.getWorld();
            if ((world != null) && (entity instanceof Player)) {
                if ((player.getPlayer().getGameMode() != GameMode.CREATIVE)) {
                world.playEffect(entityLoc, Effect.STEP_SOUND, Material.REDSTONE_WIRE);
                }
         
            }
        }
     
        @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
        public void onMobDamage(EntityDamageEvent event) {
            Entity entity = event.getEntity();
            Location entityLoc = entity.getLocation();
            if ((entity instanceof Player)) {
            } else {
                World world = entity.getWorld();
                world.playEffect(entityLoc, Effect.STEP_SOUND, Material.REDSTONE_WIRE);
            }
        }
    }
     
  2. Offline

    Ugleh

    player is null and not being used. You should also not need to do player.getPlayer() as that is redundant.

    Also, you should use EntityDamageByEntityEvent if you are trying to get a player on player attack seeing as your trying to do instanceof player
     
  3. Offline

    TheLonelyIsland

    The thing is, it needs to be if the player is hurt by anything, I just need to exclude players in creative mode.
     
  4. Offline

    Ugleh

    Then use the same event your using, but do this
    player = (Player)event.getEntity();

    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    3. public void onPlayerDamage(EntityDamageEvent event) {
    4. Entity entity = event.getEntity();
    5. player = (Player)event.getEntity();
    6. Location entityLoc = entity.getLocation();
    7. World world = entity.getWorld();
    8. if ((world != null) && (entity instanceof Player)) {
    9. if ((player.getGameMode() != GameMode.CREATIVE)) {
    10. world.playEffect(entityLoc, Effect.STEP_SOUND, Material.REDSTONE_WIRE);
    11. }
    12.  
    13. }
    14. }
    15.  
     
    TheLonelyIsland likes this.
  5. Offline

    TheLonelyIsland

    Thanks so much dude, Much appreciated. :)
     
  6. Offline

    DJSanderrr

    Lol, made the same plugin for my server, but more advanced
     
Thread Status:
Not open for further replies.

Share This Page