Solved Checking mob type from a config list

Discussion in 'Plugin Development' started by damo1995, Oct 24, 2012.

Thread Status:
Not open for further replies.
  1. Hello guys,

    I asked this a while back on the IRC and I believe on the forums at some point too? Unfortunately I didn't get an answer here but did eventually manage to get it sorted till my HDD drive died and I lost about 9 hours of work -_- Anyway, I would like to put the following in my config:
         - PIG
         - IRON_GOLEM
    ect, and then it checks on mobdamageevent the entity type then checks if it is in this list of entity types but I cant remember the process of how I did it before. If anybody would be able to remind me or show me how it is done.

  2. You can do a .getType on the entity. This will return you the EntityType of the entity.

    Using the .getName() on that EntityType will give you a nice string with the name ("PLAYER", "PIG", "PIG_ZOMBIE" etc.)

    You can use that to check if it's a entity in your config.
  3. Offline


    config.set("path", entity.getType().toString());
    EntityType type = EntityType.valueOf(config.get("path"));
  4. Sorry to bump this after a long time, just not had chance to getting round to it.

    Would somebody please be able to tell me why I am getting
    Show Spoiler

    2012-11-07 11:43:06 [SEVERE] Could not pass event EntityDamageByEntityEvent to AnimalProtect v1.3.2
    at org.bukkit.plugin.RegisteredListener.callEvent(
    at org.bukkit.plugin.SimplePluginManager.fireEvent(
    at org.bukkit.plugin.SimplePluginManager.callEvent(
    at org.bukkit.craftbukkit.event.CraftEventFactory.callEvent(
    at org.bukkit.craftbukkit.event.CraftEventFactory.callEntityDamageEvent(
    at org.bukkit.craftbukkit.event.CraftEventFactory.handleEntityDamageEvent(
    at net.minecraft.server.EntityLiving.damageEntity(
    at net.minecraft.server.EntityAnimal.damageEntity(SourceFile:123)
    at net.minecraft.server.EntityHuman.attack(
    at net.minecraft.server.NetServerHandler.a(
    at net.minecraft.server.Packet7UseEntity.handle(SourceFile:36)
    at net.minecraft.server.NetworkManager.b(
    at net.minecraft.server.NetServerHandler.d(
    at net.minecraft.server.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.MinecraftServer.r(
    at net.minecraft.server.DedicatedServer.r(
    at net.minecraft.server.MinecraftServer.q(
    Caused by: java.lang.NullPointerException
    at me.damo1995.AnimalProtect.NewDamageListeners.onAttacked(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    ... 20 more

    With this code:
    package me.damo1995.AnimalProtect;
    import java.util.List;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.*;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    public class NewDamageListeners implements Listener {
    String fail = ChatColor.DARK_RED + "You cannot attack mobs here!";
    public static AnimalProtect plugin;
    long lnt;
    public NewDamageListeners(AnimalProtect instance) {
    DamageListeners.plugin = instance;
    @EventHandler(priority = EventPriority.HIGHEST)
    public void onAttacked(EntityDamageByEntityEvent event){
    if(event.getDamager() instanceof Player){
    String entity = event.getEntity().getType().toString();
    List<String> protect = plugin.getConfig().getStringList("protect-from-player");
    Boolean debug = plugin.getConfig().getBoolean("debug");
    Player player = (Player)event.getDamager();
    Location loc = event.getEntity().getLocation();
    RegionManager rm = plugin.getWorldGuardPlugin().getRegionManager(loc.getWorld());
    if(plugin.getWorldGuardPlugin().canBuild(player, loc) || player.hasPermission("animalprotect.bypass")){
    if(debug == true){
    player.sendMessage(event.getDamager().getType().toString() + " Attacked " + event.getEntity().getType().toString());
    player.sendMessage("Attack Sucessfull");
    if(debug == true){
    player.sendMessage(event.getDamager().getType().toString() + " Attacked " + event.getEntity().getType().toString()); 
    player.sendMessage("Attack Failed");
    player.sendMessage("Is on the List!!");
    public void notifyAdmin(Player player){
    long timesincelastnote = System.currentTimeMillis() - lnt;
    if(timesincelastnote > plugin.getConfig().getInt("notify-interval") * 1000){
    lnt = System.currentTimeMillis();
    if (DamageListeners.plugin.getConfig().getBoolean("notify") == true) {
    for (Player onlinePlayer : Bukkit.getServer().getOnlinePlayers()) {
    // Get a list of online players and check if they have permission/op //
    if (onlinePlayer.hasPermission("animalprotect-notify")
    || onlinePlayer.isOp()) {
    onlinePlayer.sendMessage( + player.getName() + " " + "Attempted to kill protected animals");
    DamageListeners.plugin.logMessage(player.getName() + " " + "Attempted to kill protected animals");
    But yet without all the worldguard stuff it works fine? :S
  5. Offline


    1. public NewDamageListeners(AnimalProtect instance) {
    2. DamageListeners.plugin = instance;
    3. }
    4. // must be
    5. public NewDamageListeners(AnimalProtect instance) {
    6. plugin = instance;
    7. }

    Your plugin was not initialized (you initialized another class's plugin). Btw have you removed a line somewhere? you made it quite confusing as your error line did not match what I saw (because on line 33 plugin.getConfig().getStringList() 'worked')

    And some offtopic stuff:
    Add ignoreCancelled=true in your EventHandler annotation, so you can remove the if(event.isCancelled())
    You know that EventPriority.HIGHEST is actually one of the lowest priorities there is?
  6. Thanks alot!

    I finaly managed to get this working :)

    Topic marked as solved
Thread Status:
Not open for further replies.

Share This Page