Permalink
Browse files

Added hint type to wrap.

There is no way for the wrap method to reliably work across the board
without the hint type, because of the fact that we may be searching for
a particular interface type, of which the concrete item may implement
several reasonable options for. If Java had runtime generic reflection,
we could get away with this as is, but unfortunately, as it does not,
we have to provide the type hint ourselves. This will still eliminate the
few casts that were in some places, and is generally better practice than
returning <T> T anyways.

I still have to go through and update all the classes that used the wrap
method, but that will be included in a separate commit.
  • Loading branch information...
1 parent de8c8af commit 2cfdd16d88f161ed8ded5aa9ccfc3842003a25e3 @LadyCailin LadyCailin committed May 8, 2013
@@ -4,14 +4,11 @@
import com.laytonsmith.PureUtilities.ClassDiscovery;
import com.laytonsmith.PureUtilities.ReflectionUtils;
import com.laytonsmith.annotations.WrappedItem;
-import com.sk89q.util.ReflectionUtil;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.List;
import java.util.Map;
-import java.util.Set;
/**
* This class provides various utilities for managing the abstraction layer
@@ -38,11 +35,11 @@ private AbstractionUtils(){}
* in the abstraction layer.
* @return
*/
- public static <T extends AbstractionObject> T wrap(Object item) throws AbstractionException {
+ public static <T extends AbstractionObject> T wrap(Class<T> clazz, Object item) throws AbstractionException {
if(item == null){
return null;
}
- return instantiate(doLookup(item.getClass()), item);
+ return (T) instantiate(doLookup(clazz, item.getClass()), item);
}
/**
@@ -52,12 +49,12 @@ private AbstractionUtils(){}
* @return
* @throws com.laytonsmith.abstraction.AbstractionUtils.AbstractionException
*/
- public static Class<? extends AbstractionObject> doLookup(Class<?> clazz) throws AbstractionException {
+ public static Class<? extends AbstractionObject> doLookup(Class<? extends AbstractionObject> hint, Class<?> clazz) throws AbstractionException {
if(abstractionTypes.containsKey(clazz)){
return abstractionTypes.get(clazz);
} else {
- Class found = null;
//It's not directly included. This may not be a problem though, we need to walk through the superclasses.
+ Class found = null;
if(found == null){
Class c = clazz;
while((c = c.getSuperclass()) != null){
@@ -72,12 +69,20 @@ private AbstractionUtils(){}
//no awesome way to do this, because if two interfaces are implemented, it's equally likely we want either,
//so it's arbitrary which one we return. However, the getSuperInterfaces class will return them in the order
//from left to right of most likely, so while undefined, which is returned is consistent, and reasonable.
- for(Class c : getAllSuperInterfaces(clazz, new ArrayList<Class>())){
- if(abstractionTypes.containsKey(c)){
- found = c;
+ Class c = clazz;
+ outer: do{
+ boolean breakOut = false;
+ for(Class cc : getAllSuperInterfaces(c, new ArrayList<Class>())){
+ if(hint.isAssignableFrom(cc) && abstractionTypes.containsKey(cc)){
+ found = cc;
+ breakOut = true;
+ break;
+ }
+ }
+ if(breakOut){
break;
}
- }
+ } while((c = c.getSuperclass()) != null);
}
if(found != null){
//Cool, it's in here. Let's add this type though, so future lookups are faster
@@ -97,7 +102,7 @@ private AbstractionUtils(){}
if(f.getAnnotation(WrappedItem.class) != null){
//This is it
if(f.getType().isAssignableFrom(instance.getClass())){
- ReflectionUtils.set(wrapper.getClass(), wrapper, f.getName(), instance);
+ ReflectionUtils.set(c, wrapper, f.getName(), instance);
} else {
//This is unit tested for, but just in case
throw new RuntimeException("There is an error in the abstraction layer, with the " + c.getName()
@@ -45,7 +45,7 @@ public MCLocation GetLocation(MCWorld w, double x, double y, double z, float yaw
if(w != null){
w2 = w.getHandle();
}
- return AbstractionUtils.wrap(new Location(w2, x, y, z, yaw, pitch));
+ return AbstractionUtils.wrap(MCLocation.class, new Location(w2, x, y, z, yaw, pitch));
}
public Class GetServerEventMixin() {
@@ -56,7 +56,7 @@ public Class GetServerEventMixin() {
MCEnchantment[] ea = new MCEnchantment[Enchantment.values().length];
Enchantment[] oea = Enchantment.values();
for (int i = 0; i < ea.length; i++) {
- ea[i] = AbstractionUtils.wrap(oea[i]);
+ ea[i] = AbstractionUtils.wrap(MCEnchantment.class, oea[i]);
}
return ea;
@@ -66,29 +66,31 @@ public MCEnchantment GetEnchantmentByName(String name) {
try{
//If they are looking it up by number, we can support that
int i = Integer.valueOf(name);
- return AbstractionUtils.wrap(Enchantment.getById(i));
+ return AbstractionUtils.wrap(MCEnchantment.class, Enchantment.getById(i));
} catch(NumberFormatException e){
try{
- return AbstractionUtils.wrap(Enchantment.getByName(name));
+ return AbstractionUtils.wrap(MCEnchantment.class, Enchantment.getByName(name));
} catch(NullPointerException ee){
return null;
}
}
}
public MCServer GetServer() {
- return AbstractionUtils.wrap(Bukkit.getServer());
+ return AbstractionUtils.wrap(MCServer.class, Bukkit.getServer());
}
public MCMaterial getMaterial(int id) {
- return AbstractionUtils.wrap(Material.getMaterial(id));
+ return AbstractionUtils.wrap(MCMaterial.class, Material.getMaterial(id));
}
public MCItemStack GetItemStack(int type, int qty) {
- return AbstractionUtils.wrap(new ItemStack(type, qty));
+ return AbstractionUtils.wrap(MCItemStack.class, new ItemStack(type, qty));
}
+
+ @Deprecated
public MCItemStack GetItemStack(int type, int data, int qty) {
- return AbstractionUtils.wrap(new ItemStack(type, qty, (short)0, (byte)data));
+ return AbstractionUtils.wrap(MCItemStack.class, new ItemStack(type, qty, (short)0, (byte)data));
}
public static final BukkitBlockListener BlockListener = new BukkitBlockListener();
@@ -28,7 +28,7 @@ public MCPlugin getPlugin(String name) {
if(p.getPlugin(name) == null){
return null;
}
- return AbstractionUtils.wrap(p.getPlugin(name));
+ return AbstractionUtils.wrap(MCPlugin.class, p.getPlugin(name));
}
public PluginManager __PluginManager(){
@@ -55,7 +55,7 @@ public int hashCode() {
Plugin[] plugs = p.getPlugins();
for (Plugin plug : plugs) {
- retn.add((MCPlugin) AbstractionUtils.wrap(plug));
+ retn.add(AbstractionUtils.wrap(MCPlugin.class, plug));
}
return retn;
@@ -17,6 +17,7 @@
import com.laytonsmith.abstraction.enums.MCMobs;
import com.laytonsmith.abstraction.enums.MCSpawnReason;
import com.laytonsmith.abstraction.enums.MCTargetReason;
+import com.laytonsmith.abstraction.enums.bukkit.BukkitMCEntityType;
import com.laytonsmith.abstraction.enums.bukkit.BukkitMCSpawnReason;
import com.laytonsmith.abstraction.events.*;
import com.laytonsmith.annotations.abstraction;
@@ -62,15 +63,15 @@ public Object _GetObject() {
public MCEntity getEntity() {
if (e.getEntity() != null) {
- return AbstractionUtils.wrap(e.getEntity());
+ return AbstractionUtils.wrap(MCEntity.class, e.getEntity());
}
return null;
}
public List<MCBlock> getBlocks() {
List<MCBlock> ret = new ArrayList<MCBlock>();
for (Block b : e.blockList()) {
- ret.add((MCBlock) AbstractionUtils.wrap(b));
+ ret.add(AbstractionUtils.wrap(MCBlock.class, b));
}
return ret;
}
@@ -83,7 +84,7 @@ public void setBlocks(List<MCBlock> blocks) {
}
public MCLocation getLocation() {
- return AbstractionUtils.wrap(e.getLocation());
+ return AbstractionUtils.wrap(MCLocation.class, e.getLocation());
}
public float getYield() {
@@ -108,7 +109,7 @@ public Object _GetObject() {
}
public MCProjectile getEntity() {
- return AbstractionUtils.wrap(phe.getEntity());
+ return AbstractionUtils.wrap(MCProjectile.class, phe.getEntity());
}
public MCEntityType getEntityType() {
@@ -140,7 +141,7 @@ public Object _GetObject() {
public Set<MCLivingEntity> getAffectedEntities() {
Set<MCLivingEntity> ret = new HashSet<MCLivingEntity>();
for (LivingEntity le : pse.getAffectedEntities()) {
- ret.add((MCLivingEntity) AbstractionUtils.wrap(le));
+ ret.add(AbstractionUtils.wrap(MCLivingEntity.class, le));
}
return ret;
}
@@ -176,7 +177,7 @@ public int getDroppedExp() {
List<MCItemStack> drops = new ArrayList<MCItemStack>();
for(ItemStack is : islist){
- drops.add((MCItemStack) AbstractionUtils.wrap(is));
+ drops.add(AbstractionUtils.wrap(MCItemStack.class, is));
}
return drops;
@@ -191,7 +192,7 @@ public void addDrop(MCItemStack is){
}
public MCLivingEntity getEntity() {
- return AbstractionUtils.wrap(e.getEntity());
+ return AbstractionUtils.wrap(MCLivingEntity.class, e.getEntity());
}
public void setDroppedExp(int exp) {
@@ -213,11 +214,11 @@ public Object _GetObject() {
}
public MCLivingEntity getEntity() {
- return AbstractionUtils.wrap(e.getEntity());
+ return AbstractionUtils.wrap(MCLivingEntity.class, e.getEntity());
}
public MCLocation getLocation() {
- return AbstractionUtils.wrap(e.getLocation());
+ return AbstractionUtils.wrap(MCLocation.class, e.getLocation());
}
public MCSpawnReason getSpawnReason() {
@@ -244,7 +245,7 @@ public Object _GetObject() {
}
public MCEntity getEntity() {
- return AbstractionUtils.wrap(e.getRightClicked());
+ return AbstractionUtils.wrap(MCEntity.class, e.getRightClicked());
}
public boolean isCancelled() {
@@ -256,7 +257,7 @@ public void setCancelled(boolean cancelled) {
}
public MCPlayer getPlayer() {
- return AbstractionUtils.wrap(e.getPlayer());
+ return AbstractionUtils.wrap(MCPlayer.class, e.getPlayer());
}
}
@@ -270,7 +271,7 @@ public BukkitMCPlayerDropItemEvent(PlayerDropItemEvent e) {
}
public MCItem getItemDrop() {
- return AbstractionUtils.wrap(e.getItemDrop());
+ return AbstractionUtils.wrap(MCItem.class, e.getItemDrop());
}
public void setItemStack(MCItemStack stack) {
@@ -291,7 +292,7 @@ public void setCancelled(boolean cancelled) {
}
public MCPlayer getPlayer() {
- return AbstractionUtils.wrap(e.getPlayer());
+ return AbstractionUtils.wrap(MCPlayer.class, e.getPlayer());
}
public Object _GetObject() {
@@ -311,7 +312,7 @@ public int getRemaining() {
}
public MCItem getItem() {
- return AbstractionUtils.wrap(e.getItem());
+ return AbstractionUtils.wrap(MCItem.class, e.getItem());
}
public void setItemStack(MCItemStack stack) {
@@ -336,7 +337,7 @@ public void setCancelled(boolean cancelled) {
}
public MCPlayer getPlayer() {
- return AbstractionUtils.wrap(e.getPlayer());
+ return AbstractionUtils.wrap(MCPlayer.class, e.getPlayer());
}
public Object _GetObject() {
@@ -362,7 +363,7 @@ public MCDamageCause getCause() {
}
public MCEntity getEntity() {
- return AbstractionUtils.wrap(event.getEntity());
+ return AbstractionUtils.wrap(MCEntity.class, event.getEntity());
}
public int getDamage() {
@@ -385,7 +386,7 @@ public BukkitMCEntityDamageByEntityEvent(EntityDamageByEntityEvent e) {
}
public MCEntity getDamager() {
- return AbstractionUtils.wrap(event.getDamager());
+ return AbstractionUtils.wrap(MCEntity.class, event.getDamager());
}
}
@@ -408,7 +409,7 @@ public Object _GetObject() {
}
public MCEntity getTarget() {
- return AbstractionUtils.wrap(pie.getTarget());
+ return AbstractionUtils.wrap(MCEntity.class, pie.getTarget());
}
public void setTarget(MCEntity target) {
@@ -420,11 +421,11 @@ public void setTarget(MCEntity target) {
}
public MCEntity getEntity() {
- return AbstractionUtils.wrap(pie.getEntity());
+ return AbstractionUtils.wrap(MCEntity.class, pie.getEntity());
}
public MCEntityType getEntityType() {
- return AbstractionUtils.wrap(pie.getEntity().getType());
+ return BukkitMCEntityType.getConvertor().getAbstractedEnum(pie.getEntity().getType());
}
public MCTargetReason getReason() {
@@ -444,11 +445,11 @@ public Object _GetObject() {
}
public MCEntity getEntity() {
- return AbstractionUtils.wrap(epe.getEntity());
+ return AbstractionUtils.wrap(MCEntity.class, epe.getEntity());
}
public MCLocation getLocation() {
- return AbstractionUtils.wrap(epe.getLocation());
+ return AbstractionUtils.wrap(MCLocation.class, epe.getLocation());
}
}
}
@@ -112,13 +112,6 @@ public void onEnable() {
//Metrics
try {
org.mcstats.Metrics m = new Metrics(this);
- m.addCustomData(new Metrics.Plotter("Player count") {
-
- @Override
- public int getValue() {
- return Static.getServer().getOnlinePlayers().length;
- }
- });
m.start();
} catch (IOException e) {
// Failed to submit the stats :-(
@@ -250,7 +243,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,
&& (cmd.getName().equals("reloadaliases") || cmd.getName().equals("reloadalias") || cmd.getName().equals("recompile"))) {
MCPlayer player = null;
if (sender instanceof Player) {
- player = AbstractionUtils.wrap((Player) sender);
+ player = AbstractionUtils.wrap(MCPlayer.class, sender);
}
ac.reload(player);
// if(ac.reload(player)){
@@ -280,14 +273,14 @@ public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,
ServerCommandEvent sce = new ServerCommandEvent((ConsoleCommandSender) sender, cmd2);
serverListener.onServerCommand(sce);
} else if(sender instanceof BlockCommandSender){
- MCCommandSender s = AbstractionUtils.wrap((BlockCommandSender)sender);
+ MCCommandSender s = AbstractionUtils.wrap(MCCommandSender.class, sender);
String cmd2 = StringUtils.Join(args, " ");
Static.getAliasCore().alias(cmd2, s, new ArrayList<Script>());
}
return true;
} else if (sender instanceof Player) {
try {
- return runCommand((MCPlayer)AbstractionUtils.wrap(sender), cmd.getName(), args);
+ return runCommand(AbstractionUtils.wrap(MCPlayer.class, sender), cmd.getName(), args);
} catch (DataSourceException ex) {
Logger.getLogger(CommandHelperPlugin.class.getName()).log(Level.SEVERE, null, ex);
} catch (ReadOnlyException ex) {
Oops, something went wrong.

0 comments on commit 2cfdd16

Please sign in to comment.