java: no suitable constructor found for ...

Leventeand1

New member
Jan 17, 2023
4
0
1
Hello! I made a plugin and it worked fine. I wanted to make another one, an event listener. I managed to do it until I entered the event in my Main class. I wrote: new EntityDamageEvent(this); and i got an error! It wrote: java: no suitable constructor found for EntityDamageEvent(survival.survival.Survival) What should I do? I am attaching the whole code. Thank you very much in advance for your help.
 

Attachments

  • Listener.txt
    694 bytes · Views: 3
  • Main class2.txt
    563 bytes · Views: 1
Solution
Hello! I made a plugin and it worked fine. I wanted to make another one, an event listener. I managed to do it until I entered the event in my Main class. I wrote: new EntityDamageEvent(this); and i got an error! It wrote: java: no suitable constructor found for EntityDamageEvent(survival.survival.Survival) What should I do? I am attaching the whole code. Thank you very much in advance for your help.

Java:
package survival.survival.Handlers;

import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;

public class EntityDamageEvent implements Listener {
    public EntityDamageEvent(Plugin...

Zymo

New member
Jan 16, 2023
4
1
0
1
Hello! I made a plugin and it worked fine. I wanted to make another one, an event listener. I managed to do it until I entered the event in my Main class. I wrote: new EntityDamageEvent(this); and i got an error! It wrote: java: no suitable constructor found for EntityDamageEvent(survival.survival.Survival) What should I do? I am attaching the whole code. Thank you very much in advance for your help.

Java:
package survival.survival.Handlers;

import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;

public class EntityDamageEvent implements Listener {
    public EntityDamageEvent(Plugin plugin)
    {
        Bukkit.getPluginManager().registerEvents(this, plugin);
    }

    @EventHandler
    public void onEntityDamage(org.bukkit.event.entity.EntityDamageEvent event)
    {
        Entity entity = event.getEntity();
        if ((entity instanceof Player)) {
            event.setCancelled(true);
        }
    }
}

Ok, I've corrected your code, but next time just look for what you're doing wrong or see some tutorials. These are the basics, so you shouldn't have any problem finding a good tutorial.

First of all before you cast you must be sure that the object you want to cast is similar or the same object to which you want to cast, e.g. In this case, you wanted to cast the entity to the player without first checking whether the entity is a player. (you checked if entity is a player after casting)

Second, the class name should not be the same as in the event method. (idk, my IDE gives me many warnings)

Third, some things in this code could be a lot more readable and better.

Fourth, the name of package: survival.survival is at least strange, usually something like this is used: com.example.project

You have a lot to learn, so instead of making more posts, I recommend to fiddle around with code before posting your next question, or you can go step by step in guides, over time you should learn something.

That's it, please take my answer seriously. Check if it works, though it works for me, but you know you'll never know.
 
Solution

Leventeand1

New member
Jan 17, 2023
4
0
1
Java:
package survival.survival.Handlers;

import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;

public class EntityDamageEvent implements Listener {
    public EntityDamageEvent(Plugin plugin)
    {
        Bukkit.getPluginManager().registerEvents(this, plugin);
    }

    @EventHandler
    public void onEntityDamage(org.bukkit.event.entity.EntityDamageEvent event)
    {
        Entity entity = event.getEntity();
        if ((entity instanceof Player)) {
            event.setCancelled(true);
        }
    }
}

Ok, I've corrected your code, but next time just look for what you're doing wrong or see some tutorials. These are the basics, so you shouldn't have any problem finding a good tutorial.

First of all before you cast you must be sure that the object you want to cast is similar or the same object to which you want to cast, e.g. In this case, you wanted to cast the entity to the player without first checking whether the entity is a player. (you checked if entity is a player after casting)

Second, the class name should not be the same as in the event method. (idk, my IDE gives me many warnings)

Third, some things in this code could be a lot more readable and better.

Fourth, the name of package: survival.survival is at least strange, usually something like this is used: com.example.project

You have a lot to learn, so instead of making more posts, I recommend to fiddle around with code before posting your next question, or you can go step by step in guides, over time you should learn something.

That's it, please take my answer seriously. Check if it works, though it works for me, but you know you'll never know.
You help me a lot! And it works!!! You saved my life! (As if my life depended on it) whatever. Thank you very much!!!!!!!