mirror of
https://github.com/filoghost/HolographicDisplays.git
synced 2024-12-24 01:37:35 +01:00
Performance improvement to get the stack trace element (the call is ~8
times faster)
This commit is contained in:
parent
d8955fce02
commit
20acd2fc82
@ -19,6 +19,7 @@ import org.bukkit.craftbukkit.v1_10_R1.entity.CraftEntity;
|
||||
|
||||
import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand;
|
||||
import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine;
|
||||
import com.gmail.filoghost.holographicdisplays.util.ReflectionUtils;
|
||||
import com.gmail.filoghost.holographicdisplays.util.Utils;
|
||||
|
||||
public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand {
|
||||
@ -126,9 +127,8 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
|
||||
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
|
||||
if (elements.length > 2 && elements[2] != null && elements[2].getFileName().equals("EntityTrackerEntry.java") && 158 < elements[2].getLineNumber() && elements[2].getLineNumber() < 168) {
|
||||
StackTraceElement element = ReflectionUtils.getStackTraceElement(2);
|
||||
if (element.getFileName().equals("EntityTrackerEntry.java") && 158 < element.getLineNumber() && element.getLineNumber() < 168) {
|
||||
// Then this method is being called when creating a new movement packet, we return a fake ID!
|
||||
return -1;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import org.bukkit.craftbukkit.v1_11_R1.entity.CraftEntity;
|
||||
|
||||
import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand;
|
||||
import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine;
|
||||
import com.gmail.filoghost.holographicdisplays.util.ReflectionUtils;
|
||||
import com.gmail.filoghost.holographicdisplays.util.Utils;
|
||||
|
||||
import net.minecraft.server.v1_11_R1.AxisAlignedBB;
|
||||
@ -135,9 +136,8 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
|
||||
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
|
||||
if (elements.length > 2 && elements[2] != null && elements[2].getFileName().equals("EntityTrackerEntry.java") && 158 < elements[2].getLineNumber() && elements[2].getLineNumber() < 168) {
|
||||
StackTraceElement element = ReflectionUtils.getStackTraceElement(2);
|
||||
if (element.getFileName().equals("EntityTrackerEntry.java") && 158 < element.getLineNumber() && element.getLineNumber() < 168) {
|
||||
// Then this method is being called when creating a new movement packet, we return a fake ID!
|
||||
return -1;
|
||||
}
|
||||
|
@ -52,8 +52,8 @@ public class EntityNMSItem extends EntityItem implements NMSItem {
|
||||
@Override
|
||||
public ItemStack getItemStack() {
|
||||
// Dirty method to check if the icon is being picked up
|
||||
StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
|
||||
if (stacktrace.length > 2 && stacktrace[2].getClassName().contains("EntityInsentient")) {
|
||||
StackTraceElement element = ReflectionUtils.getStackTraceElement(2);
|
||||
if (element.getClassName().contains("EntityInsentient")) {
|
||||
return null; // Try to pickup this, dear entity ignoring the pickupDelay!
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ import org.bukkit.craftbukkit.v1_7_R1.entity.CraftEntity;
|
||||
|
||||
import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSWitherSkull;
|
||||
import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine;
|
||||
import com.gmail.filoghost.holographicdisplays.util.ReflectionUtils;
|
||||
import com.gmail.filoghost.holographicdisplays.util.Utils;
|
||||
|
||||
import net.minecraft.server.v1_7_R1.EntityWitherSkull;
|
||||
@ -70,9 +71,8 @@ public class EntityNMSWitherSkull extends EntityWitherSkull implements NMSWither
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
|
||||
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
|
||||
if (elements.length > 2 && elements[2] != null && elements[2].getFileName().equals("EntityTrackerEntry.java") && elements[2].getLineNumber() > 134 && elements[2].getLineNumber() < 144) {
|
||||
StackTraceElement element = ReflectionUtils.getStackTraceElement(2);
|
||||
if (element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 134 && element.getLineNumber() < 144) {
|
||||
// Then this method is being called when creating a new packet, we return a fake ID!
|
||||
return -1;
|
||||
}
|
||||
|
@ -52,8 +52,8 @@ public class EntityNMSItem extends EntityItem implements NMSItem {
|
||||
@Override
|
||||
public ItemStack getItemStack() {
|
||||
// Dirty method to check if the icon is being picked up
|
||||
StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
|
||||
if (stacktrace.length > 2 && stacktrace[2].getClassName().contains("EntityInsentient")) {
|
||||
StackTraceElement element = ReflectionUtils.getStackTraceElement(2);
|
||||
if (element.getClassName().contains("EntityInsentient")) {
|
||||
return null; // Try to pickup this, dear entity ignoring the pickupDelay!
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,7 @@ import org.bukkit.craftbukkit.v1_7_R2.entity.CraftEntity;
|
||||
|
||||
import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSWitherSkull;
|
||||
import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine;
|
||||
import com.gmail.filoghost.holographicdisplays.util.ReflectionUtils;
|
||||
import com.gmail.filoghost.holographicdisplays.util.Utils;
|
||||
|
||||
public class EntityNMSWitherSkull extends EntityWitherSkull implements NMSWitherSkull {
|
||||
@ -72,9 +73,8 @@ public class EntityNMSWitherSkull extends EntityWitherSkull implements NMSWither
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
|
||||
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
|
||||
if (elements.length > 2 && elements[2] != null && elements[2].getFileName().equals("EntityTrackerEntry.java") && elements[2].getLineNumber() > 134 && elements[2].getLineNumber() < 144) {
|
||||
StackTraceElement element = ReflectionUtils.getStackTraceElement(2);
|
||||
if (element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 134 && element.getLineNumber() < 144) {
|
||||
// Then this method is being called when creating a new packet, we return a fake ID!
|
||||
return -1;
|
||||
}
|
||||
|
@ -52,8 +52,8 @@ public class EntityNMSItem extends EntityItem implements NMSItem {
|
||||
@Override
|
||||
public ItemStack getItemStack() {
|
||||
// Dirty method to check if the icon is being picked up
|
||||
StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
|
||||
if (stacktrace.length > 2 && stacktrace[2].getClassName().contains("EntityInsentient")) {
|
||||
StackTraceElement element = ReflectionUtils.getStackTraceElement(2);
|
||||
if (element.getClassName().contains("EntityInsentient")) {
|
||||
return null; // Try to pickup this, dear entity ignoring the pickupDelay!
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ import org.bukkit.craftbukkit.v1_7_R3.entity.CraftEntity;
|
||||
|
||||
import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSWitherSkull;
|
||||
import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine;
|
||||
import com.gmail.filoghost.holographicdisplays.util.ReflectionUtils;
|
||||
import com.gmail.filoghost.holographicdisplays.util.Utils;
|
||||
|
||||
import net.minecraft.server.v1_7_R3.EntityWitherSkull;
|
||||
@ -70,9 +71,8 @@ public class EntityNMSWitherSkull extends EntityWitherSkull implements NMSWither
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
|
||||
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
|
||||
if (elements.length > 2 && elements[2] != null && elements[2].getFileName().equals("EntityTrackerEntry.java") && elements[2].getLineNumber() > 134 && elements[2].getLineNumber() < 144) {
|
||||
StackTraceElement element = ReflectionUtils.getStackTraceElement(2);
|
||||
if (element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 134 && element.getLineNumber() < 144) {
|
||||
// Then this method is being called when creating a new packet, we return a fake ID!
|
||||
return -1;
|
||||
}
|
||||
|
@ -48,8 +48,8 @@ public class EntityNMSItem extends EntityItem implements NMSItem {
|
||||
@Override
|
||||
public ItemStack getItemStack() {
|
||||
// Dirty method to check if the icon is being picked up
|
||||
StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
|
||||
if (stacktrace.length > 2 && stacktrace[2].getClassName().contains("EntityInsentient")) {
|
||||
StackTraceElement element = ReflectionUtils.getStackTraceElement(2);
|
||||
if (element.getClassName().contains("EntityInsentient")) {
|
||||
return null; // Try to pickup this, dear entity ignoring the pickupDelay!
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ import org.bukkit.craftbukkit.v1_7_R4.entity.CraftEntity;
|
||||
|
||||
import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSWitherSkull;
|
||||
import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine;
|
||||
import com.gmail.filoghost.holographicdisplays.util.ReflectionUtils;
|
||||
import com.gmail.filoghost.holographicdisplays.util.Utils;
|
||||
|
||||
import net.minecraft.server.v1_7_R4.EntityWitherSkull;
|
||||
@ -69,9 +70,8 @@ public class EntityNMSWitherSkull extends EntityWitherSkull implements NMSWither
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
|
||||
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
|
||||
if (elements.length > 2 && elements[2] != null && elements[2].getFileName().equals("EntityTrackerEntry.java") && elements[2].getLineNumber() > 134 && elements[2].getLineNumber() < 144) {
|
||||
StackTraceElement element = ReflectionUtils.getStackTraceElement(2);
|
||||
if (element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 134 && element.getLineNumber() < 144) {
|
||||
// Then this method is being called when creating a new packet, we return a fake ID!
|
||||
return -1;
|
||||
}
|
||||
|
@ -112,9 +112,8 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
|
||||
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
|
||||
if (elements.length > 2 && elements[2] != null && elements[2].getFileName().equals("EntityTrackerEntry.java") && elements[2].getLineNumber() > 137 && elements[2].getLineNumber() < 147) {
|
||||
StackTraceElement element = ReflectionUtils.getStackTraceElement(2);
|
||||
if (element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 137 && element.getLineNumber() < 147) {
|
||||
// Then this method is being called when creating a new packet, we return a fake ID!
|
||||
return -1;
|
||||
}
|
||||
|
@ -112,9 +112,8 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
|
||||
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
|
||||
if (elements.length > 2 && elements[2] != null && elements[2].getFileName().equals("EntityTrackerEntry.java") && elements[2].getLineNumber() > 137 && elements[2].getLineNumber() < 147) {
|
||||
StackTraceElement element = ReflectionUtils.getStackTraceElement(2);
|
||||
if (element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 137 && element.getLineNumber() < 147) {
|
||||
// Then this method is being called when creating a new packet, we return a fake ID!
|
||||
return -1;
|
||||
}
|
||||
|
@ -112,9 +112,8 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
|
||||
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
|
||||
if (elements.length > 2 && elements[2] != null && elements[2].getFileName().equals("EntityTrackerEntry.java") && elements[2].getLineNumber() > 137 && elements[2].getLineNumber() < 147) {
|
||||
StackTraceElement element = ReflectionUtils.getStackTraceElement(2);
|
||||
if (element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 137 && element.getLineNumber() < 147) {
|
||||
// Then this method is being called when creating a new packet, we return a fake ID!
|
||||
return -1;
|
||||
}
|
||||
|
@ -131,9 +131,8 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
|
||||
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
|
||||
if (elements.length > 2 && elements[2] != null && elements[2].getFileName().equals("EntityTrackerEntry.java") && elements[2].getLineNumber() > 142 && elements[2].getLineNumber() < 152) {
|
||||
StackTraceElement element = ReflectionUtils.getStackTraceElement(2);
|
||||
if (element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 142 && element.getLineNumber() < 152) {
|
||||
// Then this method is being called when creating a new packet, we return a fake ID!
|
||||
return -1;
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ import org.bukkit.craftbukkit.v1_9_R2.entity.CraftEntity;
|
||||
|
||||
import com.gmail.filoghost.holographicdisplays.nms.interfaces.entity.NMSArmorStand;
|
||||
import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine;
|
||||
import com.gmail.filoghost.holographicdisplays.util.ReflectionUtils;
|
||||
import com.gmail.filoghost.holographicdisplays.util.Utils;
|
||||
|
||||
public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorStand {
|
||||
@ -126,9 +127,8 @@ public class EntityNMSArmorStand extends EntityArmorStand implements NMSArmorSta
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
|
||||
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
|
||||
if (elements.length > 2 && elements[2] != null && elements[2].getFileName().equals("EntityTrackerEntry.java") && elements[2].getLineNumber() > 142 && elements[2].getLineNumber() < 152) {
|
||||
StackTraceElement element = ReflectionUtils.getStackTraceElement(2);
|
||||
if (element.getFileName().equals("EntityTrackerEntry.java") && element.getLineNumber() > 142 && element.getLineNumber() < 152) {
|
||||
// Then this method is being called when creating a new packet, we return a fake ID!
|
||||
return -1;
|
||||
}
|
||||
|
@ -1,9 +1,12 @@
|
||||
package com.gmail.filoghost.holographicdisplays.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
public class ReflectionUtils {
|
||||
|
||||
private static Method getStackTraceElementMethod;
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public static void putInPrivateStaticMap(Class<?> clazz, String fieldName, Object key, Object value) throws Exception {
|
||||
@ -24,4 +27,22 @@ public class ReflectionUtils {
|
||||
field.setAccessible(true);
|
||||
return field.get(handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* If you only need one stack trace element this is faster than Throwable.getStackTrace()[element],
|
||||
* it doesn't generate the full stack trace.
|
||||
*/
|
||||
public static StackTraceElement getStackTraceElement(int index) {
|
||||
try {
|
||||
if (getStackTraceElementMethod == null) {
|
||||
getStackTraceElementMethod = Throwable.class.getDeclaredMethod("getStackTraceElement", int.class);
|
||||
getStackTraceElementMethod.setAccessible(true);
|
||||
}
|
||||
|
||||
return (StackTraceElement) getStackTraceElementMethod.invoke(new Throwable(), index);
|
||||
} catch (Exception e) {
|
||||
// Fallback
|
||||
return new Throwable().getStackTrace()[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user