/src/main/java/org/spigotmc/CustomTimingsHandler.java

https://gitlab.com/Glowstone/Glowkit · Java · 76 lines · 37 code · 8 blank · 31 comment · 3 complexity · dcaa87b8f9005317a0f1b38d16816cd1 MD5 · raw file

  1. /*
  2. * This file is licensed under the MIT License (MIT).
  3. *
  4. * Copyright (c) 2014 Daniel Ennis <http://aikar.co>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package org.spigotmc;
  25. import org.bukkit.Bukkit;
  26. import org.bukkit.plugin.AuthorNagException;
  27. import org.bukkit.plugin.Plugin;
  28. import co.aikar.timings.NullTimingHandler;
  29. import co.aikar.timings.Timing;
  30. import co.aikar.timings.Timings;
  31. import co.aikar.timings.TimingsManager;
  32. import sun.reflect.Reflection;
  33. import java.lang.reflect.Method;
  34. import java.util.logging.Level;
  35. /**
  36. * This is here for legacy purposes incase any plugin used it.
  37. *
  38. * If you use this, migrate ASAP as this will be removed in the future!
  39. *
  40. * @deprecated
  41. * @see co.aikar.timings.Timings#of
  42. */
  43. @Deprecated
  44. public final class CustomTimingsHandler {
  45. private final Timing handler;
  46. public CustomTimingsHandler(String name) {
  47. Timing timing;
  48. Plugin plugin = null;
  49. try {
  50. plugin = TimingsManager.getPluginByClassloader(Reflection.getCallerClass(2));
  51. } catch (Exception ignored) {}
  52. new AuthorNagException("Deprecated use of CustomTimingsHandler. Please Switch to Timings.of ASAP").printStackTrace();
  53. if (plugin != null) {
  54. timing = Timings.of(plugin, "(Deprecated API) " + name);
  55. } else {
  56. try {
  57. final Method ofSafe = TimingsManager.class.getMethod("getHandler", String.class, String.class, Timing.class, boolean.class);
  58. timing = (Timing) ofSafe.invoke("Minecraft", "(Deprecated API) " + name, null, true);
  59. } catch (Exception e) {
  60. Bukkit.getLogger().log(Level.SEVERE, "This handler could not be registered");
  61. timing = Timings.NULL_HANDLER;
  62. }
  63. }
  64. handler = timing;
  65. }
  66. public void startTiming() { handler.startTiming(); }
  67. public void stopTiming() { handler.stopTiming(); }
  68. }