geforkt von Mirrors/Paper
Cache material data constructors. Fixes BUKKIT-2980
Reobtaining a constructor is not a trivial operation, this change makes the Material enum store the respective constructors for each MaterialData. Additionally 'fixed' the material tests to use proper generics. By: Darth Android <darthandroid@gmail.com>
Dieser Commit ist enthalten in:
Ursprung
f70c5fcd4c
Commit
d2e8c21941
@ -1,10 +1,7 @@
|
|||||||
package org.bukkit;
|
package org.bukkit;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import org.apache.commons.lang.Validate;
|
import org.apache.commons.lang.Validate;
|
||||||
import org.bukkit.map.MapView;
|
import org.bukkit.map.MapView;
|
||||||
@ -331,7 +328,7 @@ public enum Material {
|
|||||||
;
|
;
|
||||||
|
|
||||||
private final int id;
|
private final int id;
|
||||||
private final Class<? extends MaterialData> data;
|
private final Constructor<? extends MaterialData> ctor;
|
||||||
private static Material[] byId = new Material[383];
|
private static Material[] byId = new Material[383];
|
||||||
private final static Map<String, Material> BY_NAME = Maps.newHashMap();
|
private final static Map<String, Material> BY_NAME = Maps.newHashMap();
|
||||||
private final int maxStack;
|
private final int maxStack;
|
||||||
@ -342,11 +339,11 @@ public enum Material {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Material(final int id, final int stack) {
|
private Material(final int id, final int stack) {
|
||||||
this(id, stack, null);
|
this(id, stack, MaterialData.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Material(final int id, final int stack, final int durability) {
|
private Material(final int id, final int stack, final int durability) {
|
||||||
this(id, stack, durability, null);
|
this(id, stack, durability, MaterialData.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Material(final int id, final Class<? extends MaterialData> data) {
|
private Material(final int id, final Class<? extends MaterialData> data) {
|
||||||
@ -361,7 +358,14 @@ public enum Material {
|
|||||||
this.id = id;
|
this.id = id;
|
||||||
this.durability = (short) durability;
|
this.durability = (short) durability;
|
||||||
this.maxStack = stack;
|
this.maxStack = stack;
|
||||||
this.data = data == null ? MaterialData.class : data;
|
// try to cache the constructor for this material
|
||||||
|
try {
|
||||||
|
this.ctor = data.getConstructor(int.class, byte.class);
|
||||||
|
} catch (NoSuchMethodException ex) {
|
||||||
|
throw new AssertionError(ex);
|
||||||
|
} catch (SecurityException ex) {
|
||||||
|
throw new AssertionError(ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -397,7 +401,7 @@ public enum Material {
|
|||||||
* @return MaterialData associated with this Material
|
* @return MaterialData associated with this Material
|
||||||
*/
|
*/
|
||||||
public Class<? extends MaterialData> getData() {
|
public Class<? extends MaterialData> getData() {
|
||||||
return data;
|
return ctor.getDeclaringClass();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -409,24 +413,19 @@ public enum Material {
|
|||||||
*/
|
*/
|
||||||
public MaterialData getNewData(final byte raw) {
|
public MaterialData getNewData(final byte raw) {
|
||||||
try {
|
try {
|
||||||
Constructor<? extends MaterialData> ctor = data.getConstructor(int.class, byte.class);
|
|
||||||
|
|
||||||
return ctor.newInstance(id, raw);
|
return ctor.newInstance(id, raw);
|
||||||
} catch (InstantiationException ex) {
|
} catch (InstantiationException ex) {
|
||||||
Logger.getLogger(Material.class.getName()).log(Level.SEVERE, null, ex);
|
final Throwable t = ex.getCause();
|
||||||
} catch (IllegalAccessException ex) {
|
if (t instanceof RuntimeException) {
|
||||||
Logger.getLogger(Material.class.getName()).log(Level.SEVERE, null, ex);
|
throw (RuntimeException) t;
|
||||||
} catch (IllegalArgumentException ex) {
|
}
|
||||||
Logger.getLogger(Material.class.getName()).log(Level.SEVERE, null, ex);
|
if (t instanceof Error) {
|
||||||
} catch (InvocationTargetException ex) {
|
throw (Error) t;
|
||||||
Logger.getLogger(Material.class.getName()).log(Level.SEVERE, null, ex);
|
}
|
||||||
} catch (NoSuchMethodException ex) {
|
throw new AssertionError(t);
|
||||||
Logger.getLogger(Material.class.getName()).log(Level.SEVERE, null, ex);
|
} catch (Throwable t) {
|
||||||
} catch (SecurityException ex) {
|
throw new AssertionError(t);
|
||||||
Logger.getLogger(Material.class.getName()).log(Level.SEVERE, null, ex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
package org.bukkit;
|
package org.bukkit;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.hamcrest.CoreMatchers.isA;
|
import static org.junit.Assert.*;
|
||||||
import static org.hamcrest.CoreMatchers.nullValue;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
|
import org.bukkit.material.MaterialData;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class MaterialTest {
|
public class MaterialTest {
|
||||||
@ -42,14 +40,12 @@ public class MaterialTest {
|
|||||||
assertThat(Material.getMaterial(null), is(nullValue()));
|
assertThat(Material.getMaterial(null), is(nullValue()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// [EB]: gawd we need better code >.>
|
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
|
||||||
@Test
|
@Test
|
||||||
public void getData() {
|
public void getData() {
|
||||||
for (Material material : Material.values()) {
|
for (Material material : Material.values()) {
|
||||||
Class clazz = material.getData();
|
Class<? extends MaterialData> clazz = material.getData();
|
||||||
|
|
||||||
assertThat(material.getNewData((byte) 0), isA(clazz));
|
assertThat(material.getNewData((byte) 0), is(instanceOf(clazz)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren