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:
    Code:
    protect-from-player:
         - 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.

    Thanks.
     
  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

    Cybermaxke

    Code:
    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

    Code:
    2012-11-07 11:43:06 [SEVERE] Could not pass event EntityDamageByEntityEvent to AnimalProtect v1.3.2
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:341)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at org.bukkit.craftbukkit.event.CraftEventFactory.callEvent(CraftEventFactory.java:80)
    at org.bukkit.craftbukkit.event.CraftEventFactory.callEntityDamageEvent(CraftEventFactory.java:364)
    at org.bukkit.craftbukkit.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:386)
    at net.minecraft.server.EntityLiving.damageEntity(EntityLiving.java:645)
    at net.minecraft.server.EntityAnimal.damageEntity(SourceFile:123)
    at net.minecraft.server.EntityHuman.attack(EntityHuman.java:765)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:1050)
    at net.minecraft.server.Packet7UseEntity.handle(SourceFile:36)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:282)
    at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:111)
    at net.minecraft.server.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.MinecraftServer.r(MinecraftServer.java:578)
    at net.minecraft.server.DedicatedServer.r(DedicatedServer.java:215)
    at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:495)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:428)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:818)
    Caused by: java.lang.NullPointerException
    at me.damo1995.AnimalProtect.NewDamageListeners.onAttacked(NewDamageListeners.java:34)
    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)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:339)
    ... 20 more
    

    With this code:
    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;
     
    import com.sk89q.worldguard.protection.managers.RegionManager;
     
    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.isCancelled()){
    return;
    }
    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();
    @SuppressWarnings("unused")
    RegionManager rm = plugin.getWorldGuardPlugin().getRegionManager(loc.getWorld());
    if(protect.contains(entity)){
    if(plugin.getWorldGuardPlugin().canBuild(player, loc) || player.hasPermission("animalprotect.bypass")){
    event.setCancelled(false);
    if(debug == true){
    player.sendMessage(event.getDamager().getType().toString() + " Attacked " + event.getEntity().getType().toString());
    player.sendMessage("Attack Sucessfull");
    }
    return;
    }else{
    event.setCancelled(true);
    if(debug == true){
    player.sendMessage(event.getDamager().getType().toString() + " Attacked " + event.getEntity().getType().toString()); 
    player.sendMessage("Attack Failed");
    }
    player.sendMessage(fail);
    this.notifyAdmin(player);
    }
    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(plugin.fail + player.getName() + " " + "Attempted to kill protected animals");
    DamageListeners.plugin.logMessage(player.getName() + " " + "Attempted to kill protected animals");
    }
    }
    }
    }
    return;
    }
    }
    
    But yet without all the worldguard stuff it works fine? :S
    Thanks.
     
  5. Offline

    fireblast709

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

    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