Trouble with making no hit delay for LivingEntity

Discussion in 'Plugin Development' started by zippyclutch, Mar 27, 2017.

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

    zippyclutch

    Okay, I'm a huge beginner at plugins. But, I found two threads that I thought might help me weave together an idea I've been having:

    Background: I have a gun-pvp server, and I need there to be no hit delay for any type of entity (mobs and players). I need this because shotguns shoot multiple bullets so to speak, and because of the hit delay, only one of the bullets actually hits and does damage. This is the same with fast-firing rifles.

    Problem: What I've coded doesn't work. And, I don't know how to implement a way that works.

    Code:

    Code:
    package com.zippy.GlitchHitDelay;
    
    
    
    import org.bukkit.entity.LivingEntity;
    
    import org.bukkit.entity.Snowball;
    
    import org.bukkit.event.EventHandler;
    
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    
    public class Main extends JavaPlugin {
    
    
    @Override
    
    public void onEnable() {
    
    
    }
    
    
    @Override
    
    public void onDisable() {
    
    
    }
    
    
    
    @EventHandler
    
    public void onDamage(EntityDamageByEntityEvent e) {
    
    if (e.getEntity() instanceof LivingEntity) {
    
    if (e.getDamager() instanceof Snowball) {
    
    Snowball sball = (Snowball) e.getDamager();
    
    LivingEntity victim = (LivingEntity) e.getEntity();
    
    LivingEntity attacker = (LivingEntity) sball.getShooter();
    
    LivingEntity.getMaximumNoDamageTicks();
    
    victim.setNoDamageTicks(0);
    
    attacker.setNoDamageTicks(0);
    
    }
    
    }
    
    }
    
    }
    What have I done wrong here? Any feedback is loved. Please don't reply, "ooh, I have this problem too." and not give possible help.
     
    Last edited by a moderator: Mar 27, 2017
  2. Offline

    mine-care

    What did you forget here? (Other than the code tags)
    *Cough* (open)
    You dont need to read this! You can figure it yourself (open)
    You didn't register the event :D
     
  3. Offline

    zippyclutch

    I put the whole plugin inside of there?
    Sorry, I'm new

    @mine-care How do I register the event? I'd love if you would help me with this, sorry that I don't get it

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.

    @mine-care Okay, I think I got it. But the plugin still doesn't work.
    I have these two codes now in separate files:

    package com.zippy.GlitchHitDelay;



    import org.bukkit.plugin.java.JavaPlugin;



    public class EventHandle extends JavaPlugin {


    @Override

    public void onEnable() {

    new PlayerListener(this);

    }


    @Override

    public void onDisable() {


    }

    }


    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    And
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~

    package com.zippy.GlitchHitDelay;



    import org.bukkit.entity.LivingEntity;

    import org.bukkit.entity.Snowball;

    import org.bukkit.event.EventHandler;

    import org.bukkit.event.Listener;

    import org.bukkit.event.entity.EntityDamageByEntityEvent;



    public class PlayerListener implements Listener {



    public PlayerListener(EventHandle plugin) {

    plugin.getServer().getPluginManager().registerEvents(this, plugin);

    }



    @EventHandler

    public void onDamage(EntityDamageByEntityEvent e) {

    if (e.getEntity() instanceof LivingEntity) {

    if (e.getDamager() instanceof Snowball) {

    Snowball sball = (Snowball) e.getDamager();

    LivingEntity victim = (LivingEntity) e.getEntity();

    LivingEntity attacker = (LivingEntity) sball.getShooter();

    victim.setNoDamageTicks(victim.getMaximumNoDamageTicks());

    victim.setLastDamage(0);

    victim.setNoDamageTicks(0);

    attacker.setNoDamageTicks(0);

    }

    }

    }

    }
     
    Last edited: Mar 27, 2017
  4. Offline

    Zombie_Striker

    @zippyclutch
    1. if the onDisable does nothing, delete it.
    2. Follow Java naming conventions: package names should be all lowercase.
    3. Do you own zippy.com? If not, don't use it in your domain. Either use a domain you own, your email address, or use the format me.<your name>.<your project>
    4. Don't call you main class EventHandle, it is far to easy to confuse this with EventHandler, and it is also not accurate. That class has nothing to do with the events.
    5. Is entity a living entity? Are you sure the damager is a Snowball? Are you sure the attacker is a living entity?
    6. Have you debugged? What exactly does not work?
     
  5. Offline

    zippyclutch

    @Zombie_Striker
    Would you please show me how to make the 'entity' refer to the living entity like you said, and also tell me how to make the attacker a living entity and the damage into a snowball?
    Thanks in advance.
     
  6. Offline

    Zombie_Striker

    @zippyclutch
    if(sball.getShooter instanceof LivingEntity){

    Also,:
     
  7. Offline

    zippyclutch

    @Zombie_Striker
    Well the idea behind the plugin is that when snowballs are launched from a player, the entity being attacked will get hit every single time with the projectile, instead of being limited of being hit every time because of the tick delay. You know, if you spam a sword at a zombie in 1.8 the zombie doesn't get hit every time you hit it, because of the tick delay. The point of the plugin is to remove that.

    And thanks in advance again.

    EDIT: I'm not sure how I'm supposed to implement what you wrote into my code.
     
  8. Offline

    Zombie_Striker

    @zippyclutch
    After creating the sball variable, add that line. Then, encapsulate it by adding an end bracket at the bottom of the method.
     
  9. Offline

    zippyclutch

    @Zombie_Striker
    I don't know how to do it right. Please write it. Sorry about this, I really am new to coding
     
  10. Offline

    Side8StarLite

    @zippyclutch
    Your code seems to work fine, but according to this post setNoDamageTicks only seems to work if you put it through a Bukkit task. If you want some information on tasks, read it here. I've tried it and it ought to work...
     
  11. Offline

    zippyclutch

    @Side8StarLite
    Would you please put my code into a bukkit task and send it here? There's not enough information on the site for me to understand. Thanks in advance
     
  12. Offline

    Zombie_Striker

  13. Offline

    zippyclutch

    @Zombie_Striker
    Listen man, I'm very new to coding, What it shows there means nothing to me. I don't know as much as you, okay? The way you're explaining things to me makes no sense. At least try to explain to me in a way that I can understand knowing that I just started coding today.

    I don't know how to implement what I've wrote in code into a BukkitTask, okay?

    @Zombie_Striker
    @Side8StarLite

    So, would this work?

    Code:
    package me.zippy.GlitchHitDelayF;
    
    
    
    import org.bukkit.entity.LivingEntity;
    
    import org.bukkit.entity.Snowball;
    
    import org.bukkit.event.EventHandler;
    
    import org.bukkit.event.Listener;
    
    import org.bukkit.scheduler.BukkitRunnable;
    
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    
    
    
    class PlayerListener implements Listener {
    
    
    private final myfileone plugin;
    
    
    public PlayerListener(myfileone plugin) {
    
    this.plugin = plugin;
    
    this.plugin.getServer().getPluginManager().registerEvents(this, plugin);
    
    }
    
    
    @EventHandler
    
    public void onDamage(final EntityDamageByEntityEvent e) {
    
    new BukkitRunnable() {
    
    
    
    @Override
    
    public void run() {
    
    if (e.getEntity() instanceof LivingEntity) {
    
    if (e.getDamager() instanceof Snowball) {
    
    Snowball sball = (Snowball) e.getDamager();
    
    LivingEntity victim = (LivingEntity) e.getEntity();
    
    LivingEntity attacker = (LivingEntity) sball.getShooter();
    
    victim.setNoDamageTicks(victim.getMaximumNoDamageTicks());
    
    victim.setLastDamage(0);
    
    victim.setNoDamageTicks(0);
    
    attacker.setNoDamageTicks(0);
    
    }
    
    }
    
    }
    
    
    }.runTaskLater(this.plugin, 0);
    
    }
    
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 27, 2017
  14. Offline

    Side8StarLite

    @zippyclutch
    A Bukkit task is what it is: a task. Specifically, a task that repeats itself over and over again. That being said, you just need to make a task once and run that. If you make a new one everytime some Entity get hurts, then your server will get fried.
    I won't give you the code, but here's how you would do it:

    1 - in the onEnable() method in the main class, create a task
    2 - Inside that task, get all the entities of the world using getEntities and setNoDamageTicks(0) for each of them (remember to check whether Entity is an instance of LivingEntity!)
    3 - run the task

    This might be performance heavy if you have a lot of entities in your world. If you're only doing players, then I'd suggest using the getOnlinePlayers method instead of getEntities.
     
  15. Offline

    zippyclutch

    @Side8StarLite
    @Zombie_Striker

    I've figured it out. Thank you SO much for all of the time and trouble. I really appreciate it. If you feel the need to ask what the code is I'll send it over in case you want to use it for yourselves (But you guys won't because you know how to do it yourselves I think based on what you have said).

    /Closed
     
Thread Status:
Not open for further replies.

Share This Page