package de.tr7zw.changeme.nbtapi.utils.nmsmappings;

import java.lang.reflect.Constructor;
import java.util.logging.Level;

import de.tr7zw.changeme.nbtapi.NbtApiException;
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;

import static de.tr7zw.changeme.nbtapi.utils.MinecraftVersion.logger;

/**
 * This Enum wraps Constructors for NMS classes
 * 
 * @author tr7zw
 *
 */
@SuppressWarnings("javadoc")
public enum ObjectCreator {
    NMS_NBTTAGCOMPOUND(null, null, ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()),
    NMS_BLOCKPOSITION(null, null, ClassWrapper.NMS_BLOCKPOSITION.getClazz(), int.class, int.class, int.class),
    NMS_COMPOUNDFROMITEM(MinecraftVersion.MC1_11_R1, null, ClassWrapper.NMS_ITEMSTACK.getClazz(), ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()),
    ;

    private Constructor<?> construct;
    private Class<?> targetClass;

    ObjectCreator(MinecraftVersion from, MinecraftVersion to, Class<?> clazz, Class<?>... args){
       if(from != null && MinecraftVersion.getVersion().getVersionId() < from.getVersionId())
    	   return;
       if(to != null && MinecraftVersion.getVersion().getVersionId() > to.getVersionId())
    	   return;
    	try{
        	this.targetClass = clazz;
            construct = clazz.getDeclaredConstructor(args);
            construct.setAccessible(true);
        }catch(Exception ex){
            logger.log(Level.SEVERE, "Unable to find the constructor for the class '" + clazz.getName() + "'", ex);
        }
    }
    
    /**
     * Creates an Object instance with given args
     * 
     * @param args
     * @return Object created
     */
    public Object getInstance(Object... args){
        try{
            return construct.newInstance(args);
        }catch(Exception ex){
        	throw new NbtApiException("Exception while creating a new instance of '" + targetClass + "'", ex);
        }
    }
    
}

@@ -431,20 +431,17 @@
 eator %7B%0A
-    
+%09
 NMS_NBTT
@@ -508,20 +508,17 @@
 azz()),%0A
-    
+%09
 NMS_BLOC
@@ -616,20 +616,17 @@
 class),%0A
-    
+%09
 NMS_COMP
@@ -759,20 +759,12 @@
 ()),
-%0A    ;%0A%0A    
+;%0A%0A%09
 priv
@@ -793,20 +793,17 @@
 struct;%0A
-    
+%09
 private 
@@ -825,20 +825,17 @@
 Class;%0A%0A
-    
+%09
 ObjectCr
@@ -921,19 +921,48 @@
 rgs)
+ 
 %7B%0A
-       
+%09%09if (clazz == null)%0A%09%09%09return;%0A%09%09
 if
+ 
 (fro
@@ -1042,32 +1042,27 @@
 onId())%0A
-    %09   
+%09%09%09
 return;%0A
        i
@@ -1057,17 +1057,13 @@
 rn;%0A
-       
+%09%09
 if
+ 
 (to 
@@ -1143,24 +1143,19 @@
 ())%0A
-    %09   
+%09%09%09
 return;%0A
     
@@ -1154,26 +1154,18 @@
 rn;%0A
-    
+%09
 %09try
+ 
 %7B%0A
-        
+%09%09
 %09thi
@@ -1187,28 +1187,19 @@
  clazz;%0A
-            
+%09%09%09
 construc
@@ -1238,28 +1238,19 @@
 (args);%0A
-            
+%09%09%09
 construc
@@ -1272,30 +1272,26 @@
 (true);%0A
-        %7D
+%09%09%7D 
 catch
+ 
 (Excepti
@@ -1296,30 +1296,22 @@
 tion ex)
+ 
 %7B%0A
-            
+%09%09%09
 logger.l
@@ -1410,41 +1410,22 @@
 x);%0A
-        %7D%0A    %7D%0A    %0A    /**%0A    
+%09%09%7D%0A%09%7D%0A%0A%09/**%0A%09
  * C
@@ -1470,20 +1470,14 @@
 rgs%0A
-     * %0A    
+%09 * %0A%09
  * @
@@ -1487,20 +1487,17 @@
 am args%0A
-    
+%09
  * @retu
@@ -1518,20 +1518,14 @@
 ted%0A
-     */%0A    
+%09 */%0A%09
 publ
@@ -1565,35 +1565,22 @@
 rgs)
+ 
 %7B%0A
-        
+%09%09
 try
+ 
 %7B%0A
-            
+%09%09%09
 retu
@@ -1615,22 +1615,18 @@
 s);%0A
-        %7D
+%09%09%7D 
 catch
+ 
 (Exc
@@ -1639,18 +1639,13 @@
  ex)
+ 
 %7B%0A
-        
+%09%09
 %09thr
@@ -1744,27 +1744,14 @@
 x);%0A
-        %7D%0A    %7D%0A    
+%09%09%7D%0A%09%7D%0A
 %0A%7D%0A

Fix potential issue with 1.7.10