Fixed new entities classification. issue PermissionsEx/#182

This commit is contained in:
t3hk0d3 2012-01-27 23:51:38 +04:00
parent 913ab76139
commit 822ee7b152
2 changed files with 84 additions and 27 deletions

View File

@ -0,0 +1,74 @@
/*
* Modifyworld - PermissionsEx ruleset plugin for Bukkit
* Copyright (C) 2011 t3hk0d3 http://www.tehkode.ru
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package ru.tehkode.modifyworld;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.entity.*;
public enum EntityCategory {
PLAYER("player", Player.class),
ANIMAL("animal", Animals.class, Squid.class),
MONSTER("monster", Monster.class, Slime.class, EnderDragon.class, Ghast.class ),
NPC("npc", NPC.class),
PROJECTILE("projectile", Projectile.class);
private String name;
private Class<? extends Entity> classes[];
private final static Map<Class<? extends Entity>, EntityCategory> map = new HashMap<Class<? extends Entity>, EntityCategory>();
static {
for (EntityCategory cat : EntityCategory.values()) {
for (Class<? extends Entity> catClass : cat.getClasses()) {
map.put(catClass, cat);
}
}
}
private EntityCategory(String name, Class<? extends Entity>... classes) {
this.name = name;
this.classes = classes;
}
public String getName() {
return this.name;
}
public String getNameDot() {
return this.getName() + ".";
}
public Class<? extends Entity>[] getClasses() {
return this.classes;
}
public static EntityCategory fromEntity(Entity entity) {
for (Class<? extends Entity> entityClass : map.keySet()) {
if (entityClass.isAssignableFrom(entity.getClass())) {
return map.get(entityClass);
}
}
return null;
}
}

View File

@ -85,35 +85,14 @@ public abstract class ModifyworldListener implements Listener {
}
}
// Fixtures for Bukkit dev lazyness
if (entity instanceof Ghast) {
return "monster.ghast";
String entityName = entity.toString().substring(5).toLowerCase();
EntityCategory category = EntityCategory.fromEntity(entity);
if (category == null) {
return entityName; // category unknown (ender crystal)
}
if (entity instanceof Squid) {
return "animal.squid";
}
if (entity instanceof Slime) {
return "monster.slime";
}
String entityName = entity.getClass().getSimpleName();
if (entityName.startsWith("Craft")) {
entityName = entityName.substring(5).toLowerCase();
}
if (Monster.class.isAssignableFrom(entity.getClass())) {
entityName = "monster." + entityName;
} else if (Animals.class.isAssignableFrom(entity.getClass())) {
entityName = "animal." + entityName;
} else if (Projectile.class.isAssignableFrom(entity.getClass())) {
entityName = "projectile." + entityName;
}
return entityName;
return category.getNameDot() + entityName;
}
// Functional programming fuck yeah
@ -143,8 +122,11 @@ public abstract class ModifyworldListener implements Listener {
private void registerEvents(Plugin plugin) {
PluginManager pluginManager = plugin.getServer().getPluginManager();
for (Method method : this.getClass().getMethods()) {
if (!method.isAnnotationPresent(EventHandler.class)) {
continue;
}
@ -152,6 +134,7 @@ public abstract class ModifyworldListener implements Listener {
if (method.isAnnotationPresent(Toggleable.class)) {
Toggleable toggle = method.getAnnotation(Toggleable.class);
if (!config.getBoolean(toggle.value(), toggle.byDefault())) {
continue;
}