Solved Disableing Player-Only PvP

Discussion in 'Plugin Development' started by CaptainGoobla, Jun 14, 2014.

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

    CaptainGoobla

    I was woundering how to disable player-only PvP.
    This is for my Minecraft server's lobby.
    Anyways, please help
     
  2. Offline

    Kilovice

    This cancels all damage
    Code:java
    1. public void damage(EntityDamageEvent event){
    2. event.setCancelled(true);
    3. }
     
  3. Offline

    CaptainGoobla

    I want to have all damages but fire damage.
    I put this:
    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageEvent event) {
    3. if (!(event.getEntity() instanceof Player)) {
    4. return;
    5. }
    6. if ((event.getCause().equals(EntityDamageEvent.DamageCause.FALL)) ||
    7. ((event.getCause().equals(EntityDamageEvent.DamageCause.STARVATION)) ||
    8. ((event.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_ATTACK)) ||
    9. ((event.getCause().equals(EntityDamageEvent.DamageCause.DROWNING)) ||
    10. ((event.getCause().equals(EntityDamageEvent.DamageCause.BLOCK_EXPLOSION)) ||
    11. ((event.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION)))))))) {
    12. event.setCancelled(true);
     
  4. Offline

    Deleted user


    Instead of all those else statements why don't you just use..

    Code:java
    1.  
    2. if (event.getCause != DamageCause.FIRE_OR_WHATEVER_ITS_CALLED) {
    3.  


    also it's best to compare enums with '==' instead of equals() as it is null safe, however equals() is still technically correct as it defers to ==.
     
  5. Offline

    CaptainGoobla

    Thanks Eballer48

    By the way, you put
    Code:java
    1. if (event.getCause != DamageCause.FIRE_OR_WHATEVER_ITS_CALLED) {

    Its suppose to be
    Code:java
    1. if (event.getCause() != DamageCause.FIRE_OR_WHATEVER_ITS_CALLED) {
     
  6. Offline

    Deleted user

    Was just writing in GChrome, it happens lol. Didn't have my IDE open.
     
  7. Offline

    CaptainGoobla

    Last edited by a moderator: Jun 8, 2016
  8. Offline

    CaptainGoobla

    Its still not working. I'm still getting damages.
     
  9. Offline

    xTigerRebornx

  10. 1. Check on the EntityDamageByEntityEvent if the hitted Entity and the damager is a Player:
    Code:Java
    1.  
    2. @EventHandler
    3. public void onPvP(EntityDamageByEntityEvent e) {
    4. if (e.getEntity() instanceof Player && e.getEntity().getDamager() instanceof Player() {
    5.  
    6. }
    7. }
    8.  

    2. Cancel the Event:
    Code:Java
    1.  
    2. @EventHandler
    3. public void onPvP(EntityDamageByEntityEvent e) {
    4. if (e.getEntity() instanceof Player && e.getEntity().getDamager() instanceof Player() {
    5.  
    6. e.setCancelled(true);
    7. }
    8. }
    9.  
     
  11. Offline

    CaptainGoobla

    This is my code from using FisheyLP's idea.
    Code:java
    1. @EventHandler
    2. public void onPvP(EntityDamageByEntityEvent event) {
    3. if (event.getEntity() instanceof Player && event.getEntity().getDamager() instanceof Player() {
    4. event.sendCanceelled(true)
    5. }
    6. }

    The code used before
    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageEvent event) {
    3. if (!(event.getEntity() instanceof Player)) {
    4. return;
    5. if(event.getCause() != DamageCause.FIRE) {
    6. event.sentCancelled(true);
    7. }
    8. }
     
  12. oh you want that the player only gets fire damage... then do it like this:
    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageEvent event) {
    3. if (event.getEntity() instanceof Player) {
    4. if (event.getCause() != DamageCause.FIRE || event.getCause() != DamageCause.FIRE_TICK) {
    5. event.sentCancelled(true);
    6. }
    7. }
     
  13. Offline

    CaptainGoobla

    Gosh, why is coding so complicated... Its not working.
    Code:java
    1. package me.CaptainGoobla.TestPlugin;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.entity.EntityDamageEvent;
    7. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class TestPlugin extends JavaPlugin implements Listener{
    11.  
    12. @EventHandler
    13. public void onEntityDamage(EntityDamageEvent event) {
    14. if(event.getEntity() instanceof Player) {
    15. if(event.getCause() != DamageCause.FIRE || event.getCause() != DamageCause.FIRE_TICK) {
    16. event.setCancelled(true);
    17. }
    18. }
    19. }
    20. }
    21.  


    I forgot to add:
    Code:java
    1. public void onEnable() {
    2. getServer().getPluginManager().registerEvents(this, this);
    3. }

    Except that fire damage is disabled now.

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

    DrTURTLE2


    Well if that is your full plugin, you didnt register your events.
     
  15. Offline

    CaptainGoobla

    Read the message above that:

    I don't get any more damages, but fire isn't damaging me.
     
  16. Offline

    DrTURTLE2


    Oh sorry I think you ninja'd me. Anyways yeah thats what the code says, that you it's cancelling fire damage. What do you want?
     
  17. Offline

    Necrodoom

  18. Offline

    CaptainGoobla

    What do you mean by that?
    If you want to know what happened, this is what happened:
    I got no damages on anything. If I go in fire, I don't burn up or get damaged.
     
  19. Offline

    CaptainGoobla

    Someone, please post the code. its been like 2 days
     
  20. Offline

    Graysoldier

    This should work:

    Code:java
    1. @EventHandler
    2. public void onPvp(EntityDamageEvent e) {
    3. if (e.getEntity() instanceof Player) {
    4. if (e.getCause() != DamageCause.FIRE && e.getCause() != DamageCause.FIRE_TICK) {
    5. e.setCancelled(true);
    6. }
    7. }
    8. }
     
  21. Offline

    CaptainGoobla

Thread Status:
Not open for further replies.

Share This Page