From 822ee7b1522a5c46f6189a0acaa29d5233264a48 Mon Sep 17 00:00:00 2001 From: t3hk0d3 Date: Fri, 27 Jan 2012 23:51:38 +0400 Subject: [PATCH] Fixed new entities classification. issue PermissionsEx/#182 --- .../tehkode/modifyworld/EntityCategory.java | 74 +++++++++++++++++++ .../modifyworld/ModifyworldListener.java | 37 +++------- 2 files changed, 84 insertions(+), 27 deletions(-) create mode 100644 src/main/java/ru/tehkode/modifyworld/EntityCategory.java diff --git a/src/main/java/ru/tehkode/modifyworld/EntityCategory.java b/src/main/java/ru/tehkode/modifyworld/EntityCategory.java new file mode 100644 index 0000000..cb619b8 --- /dev/null +++ b/src/main/java/ru/tehkode/modifyworld/EntityCategory.java @@ -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 classes[]; + + private final static Map, EntityCategory> map = new HashMap, EntityCategory>(); + + static { + for (EntityCategory cat : EntityCategory.values()) { + for (Class catClass : cat.getClasses()) { + map.put(catClass, cat); + } + } + } + + private EntityCategory(String name, Class... classes) { + this.name = name; + this.classes = classes; + } + + public String getName() { + return this.name; + } + + public String getNameDot() { + return this.getName() + "."; + } + + public Class[] getClasses() { + return this.classes; + } + + public static EntityCategory fromEntity(Entity entity) { + for (Class entityClass : map.keySet()) { + if (entityClass.isAssignableFrom(entity.getClass())) { + return map.get(entityClass); + } + } + + return null; + } + +} diff --git a/src/main/java/ru/tehkode/modifyworld/ModifyworldListener.java b/src/main/java/ru/tehkode/modifyworld/ModifyworldListener.java index 642bac4..173ee4c 100644 --- a/src/main/java/ru/tehkode/modifyworld/ModifyworldListener.java +++ b/src/main/java/ru/tehkode/modifyworld/ModifyworldListener.java @@ -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; }