DebugEvents.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * This file is part of architectury.
  3. * Copyright (C) 2020, 2021 shedaniel
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 3 of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. package me.shedaniel.architectury.test.events;
  20. import com.mojang.blaze3d.platform.InputConstants;
  21. import me.shedaniel.architectury.event.events.*;
  22. import me.shedaniel.architectury.event.events.client.*;
  23. import me.shedaniel.architectury.hooks.ExplosionHooks;
  24. import me.shedaniel.architectury.platform.Platform;
  25. import me.shedaniel.architectury.utils.Env;
  26. import net.fabricmc.api.EnvType;
  27. import net.fabricmc.api.Environment;
  28. import net.minecraft.core.Position;
  29. import net.minecraft.core.Vec3i;
  30. import net.minecraft.network.chat.TranslatableComponent;
  31. import net.minecraft.world.InteractionHand;
  32. import net.minecraft.world.InteractionResult;
  33. import net.minecraft.world.InteractionResultHolder;
  34. import net.minecraft.world.entity.Entity;
  35. import net.minecraft.world.entity.player.Player;
  36. import net.minecraft.world.level.Level;
  37. import java.util.Optional;
  38. import static me.shedaniel.architectury.test.TestMod.SINK;
  39. public class DebugEvents {
  40. public static void initialize() {
  41. debugEvents();
  42. if (Platform.getEnvironment() == Env.CLIENT)
  43. debugEventsClient();
  44. }
  45. public static void debugEvents() {
  46. ChatEvent.SERVER.register((player, message, component) -> {
  47. SINK.accept("Server chat received: " + message);
  48. return InteractionResultHolder.pass(component);
  49. });
  50. CommandPerformEvent.EVENT.register(event -> {
  51. SINK.accept("Server command performed: " + event.getResults().getReader().getString());
  52. return InteractionResult.PASS;
  53. });
  54. CommandRegistrationEvent.EVENT.register((dispatcher, selection) -> {
  55. SINK.accept("Server commands registers");
  56. });
  57. EntityEvent.LIVING_DEATH.register((entity, source) -> {
  58. if (entity instanceof Player) {
  59. SINK.accept(entity.getScoreboardName() + " died to " + source.getMsgId() + logSide(entity.level));
  60. }
  61. return InteractionResult.PASS;
  62. });
  63. EntityEvent.LIVING_ATTACK.register((entity, source, amount) -> {
  64. if (source.getDirectEntity() instanceof Player) {
  65. SINK.accept(source.getDirectEntity().getScoreboardName() + " deals %.2f damage" + logSide(entity.level), amount);
  66. }
  67. return InteractionResult.PASS;
  68. });
  69. EntityEvent.ADD.register((entity, level) -> {
  70. if (entity instanceof Player) {
  71. SINK.accept(entity.getScoreboardName() + " was added to " + level.dimension().location().toString() + logSide(level));
  72. }
  73. return InteractionResult.PASS;
  74. });
  75. EntityEvent.PLACE_BLOCK.register((world, pos, state, placer) -> {
  76. SINK.accept(Optional.ofNullable(placer).map(Entity::getScoreboardName).orElse("null") + " places block at " + toShortString(pos) + logSide(world));
  77. return InteractionResult.PASS;
  78. });
  79. ExplosionEvent.DETONATE.register((world, explosion, affectedEntities) -> {
  80. SINK.accept(world.dimension().location() + " explodes at " + toShortString(ExplosionHooks.getPosition(explosion)) + logSide(world));
  81. });
  82. InteractionEvent.LEFT_CLICK_BLOCK.register((player, hand, pos, face) -> {
  83. SINK.accept(player.getScoreboardName() + " left clicks " + toShortString(pos) + logSide(player.level));
  84. return InteractionResult.PASS;
  85. });
  86. InteractionEvent.RIGHT_CLICK_BLOCK.register((player, hand, pos, face) -> {
  87. SINK.accept(player.getScoreboardName() + " right clicks " + toShortString(pos) + logSide(player.level));
  88. return InteractionResult.PASS;
  89. });
  90. InteractionEvent.RIGHT_CLICK_ITEM.register((player, hand) -> {
  91. SINK.accept(player.getScoreboardName() + " uses " + (hand == InteractionHand.MAIN_HAND ? "main hand" : "off hand") + logSide(player.level));
  92. return InteractionResultHolder.pass(player.getItemInHand(hand));
  93. });
  94. InteractionEvent.INTERACT_ENTITY.register((player, entity, hand) -> {
  95. SINK.accept(player.getScoreboardName() + " interacts with " + entity.getScoreboardName() + " using " + (hand == InteractionHand.MAIN_HAND ? "main hand" : "off hand") + logSide(player.level));
  96. return InteractionResult.PASS;
  97. });
  98. LifecycleEvent.SERVER_BEFORE_START.register(instance -> {
  99. SINK.accept("Server ready to start");
  100. });
  101. LifecycleEvent.SERVER_STARTING.register(instance -> {
  102. SINK.accept("Server starting");
  103. });
  104. LifecycleEvent.SERVER_STARTED.register(instance -> {
  105. SINK.accept("Server started");
  106. });
  107. LifecycleEvent.SERVER_STOPPING.register(instance -> {
  108. SINK.accept("Server stopping");
  109. });
  110. LifecycleEvent.SERVER_STOPPED.register(instance -> {
  111. SINK.accept("Server stopped");
  112. });
  113. LifecycleEvent.SERVER_WORLD_LOAD.register(instance -> {
  114. SINK.accept("Server world loaded: " + instance.dimension().location());
  115. });
  116. LifecycleEvent.SERVER_WORLD_UNLOAD.register(instance -> {
  117. SINK.accept("Server world unloaded: " + instance.dimension().location());
  118. });
  119. LifecycleEvent.SERVER_WORLD_SAVE.register(instance -> {
  120. SINK.accept("Server world saved: " + instance.dimension().location());
  121. });
  122. PlayerEvent.PLAYER_JOIN.register(player -> {
  123. SINK.accept(player.getScoreboardName() + " joined" + logSide(player.level));
  124. });
  125. PlayerEvent.PLAYER_QUIT.register(player -> {
  126. SINK.accept(player.getScoreboardName() + " quit" + logSide(player.level));
  127. });
  128. PlayerEvent.PLAYER_RESPAWN.register((player, conqueredEnd) -> {
  129. if (!conqueredEnd) {
  130. SINK.accept(player.getScoreboardName() + " respawns " + logSide(player.level));
  131. }
  132. });
  133. PlayerEvent.PLAYER_CLONE.register((oldPlayer, newPlayer, wonGame) -> {
  134. SINK.accept("Player cloned: " + newPlayer.getScoreboardName() + logSide(newPlayer.level));
  135. });
  136. PlayerEvent.PLAYER_ADVANCEMENT.register((player, advancement) -> {
  137. SINK.accept(player.getScoreboardName() + " was awarded with %s" + logSide(player.level), advancement.getChatComponent().getString());
  138. });
  139. PlayerEvent.CRAFT_ITEM.register((player, constructed, inventory) -> {
  140. SINK.accept(player.getScoreboardName() + " crafts " + new TranslatableComponent(constructed.getDescriptionId()).getString() + logSide(player.level));
  141. });
  142. PlayerEvent.SMELT_ITEM.register((player, smelted) -> {
  143. SINK.accept(player.getScoreboardName() + " smelts " + new TranslatableComponent(smelted.getDescriptionId()).getString() + logSide(player.level));
  144. });
  145. PlayerEvent.PICKUP_ITEM_POST.register((player, entity, stack) -> {
  146. SINK.accept(player.getScoreboardName() + " picks up " + new TranslatableComponent(stack.getDescriptionId()).getString() + logSide(player.level));
  147. });
  148. PlayerEvent.DROP_ITEM.register((player, entity) -> {
  149. SINK.accept(player.getScoreboardName() + " drops " + new TranslatableComponent(entity.getItem().getDescriptionId()).getString() + logSide(player.level));
  150. return InteractionResult.PASS;
  151. });
  152. PlayerEvent.BREAK_BLOCK.register((world, pos, state, player, xp) -> {
  153. SINK.accept(player.getScoreboardName() + " breaks " + toShortString(pos) + logSide(player.level));
  154. return InteractionResult.PASS;
  155. });
  156. PlayerEvent.OPEN_MENU.register((player, menu) -> {
  157. SINK.accept(player.getScoreboardName() + " opens " + toSimpleName(menu) + logSide(player.level));
  158. });
  159. PlayerEvent.CLOSE_MENU.register((player, menu) -> {
  160. SINK.accept(player.getScoreboardName() + " closes " + toSimpleName(menu) + logSide(player.level));
  161. });
  162. PlayerEvent.CHANGE_DIMENSION.register((player, oldLevel, newLevel) -> {
  163. SINK.accept(player.getScoreboardName() + " switched from " + oldLevel.location() + " to " + newLevel.location() + logSide(player.level));
  164. });
  165. }
  166. public static String toShortString(Vec3i pos) {
  167. return pos.getX() + ", " + pos.getY() + ", " + pos.getZ();
  168. }
  169. public static String toShortString(Position pos) {
  170. return pos.x() + ", " + pos.y() + ", " + pos.z();
  171. }
  172. public static String logSide(Level level) {
  173. if (level.isClientSide())
  174. return " (client)";
  175. return " (server)";
  176. }
  177. @Environment(EnvType.CLIENT)
  178. public static void debugEventsClient() {
  179. ClientChatEvent.CLIENT.register(message -> {
  180. SINK.accept("Client chat sent: " + message);
  181. return InteractionResultHolder.pass(message);
  182. });
  183. ClientChatEvent.CLIENT_RECEIVED.register((type, message, sender) -> {
  184. SINK.accept("Client chat received: " + message.getString());
  185. return InteractionResultHolder.pass(message);
  186. });
  187. ClientLifecycleEvent.CLIENT_WORLD_LOAD.register(world -> {
  188. SINK.accept("Client world loaded: " + world.dimension().location().toString());
  189. });
  190. ClientPlayerEvent.CLIENT_PLAYER_JOIN.register(player -> {
  191. SINK.accept(player.getScoreboardName() + " joined (client)");
  192. });
  193. ClientPlayerEvent.CLIENT_PLAYER_QUIT.register(player -> {
  194. if (player != null) {
  195. SINK.accept(player.getScoreboardName() + " quit (client)");
  196. }
  197. });
  198. ClientPlayerEvent.CLIENT_PLAYER_RESPAWN.register((oldPlayer, newPlayer) -> {
  199. SINK.accept(newPlayer.getScoreboardName() + " respawned (client)");
  200. });
  201. GuiEvent.SET_SCREEN.register((screen -> {
  202. SINK.accept("Screen has been changed to " + toSimpleName(screen));
  203. return InteractionResultHolder.pass(screen);
  204. }));
  205. GuiEvent.INIT_PRE.register((screen, widgets, children) -> {
  206. SINK.accept(toSimpleName(screen) + " initializes");
  207. return InteractionResult.PASS;
  208. });
  209. InteractionEvent.CLIENT_LEFT_CLICK_AIR.register((player, hand) -> {
  210. SINK.accept(player.getScoreboardName() + " left clicks air" + logSide(player.level));
  211. });
  212. InteractionEvent.CLIENT_RIGHT_CLICK_AIR.register((player, hand) -> {
  213. SINK.accept(player.getScoreboardName() + " right clicks air" + logSide(player.level));
  214. });
  215. RecipeUpdateEvent.EVENT.register(recipeManager -> {
  216. SINK.accept("Client recipes received");
  217. });
  218. TextureStitchEvent.POST.register(atlas -> {
  219. SINK.accept("Client texture stitched: " + atlas.location());
  220. });
  221. ClientScreenInputEvent.MOUSE_SCROLLED_PRE.register((client, screen, mouseX, mouseY, amount) -> {
  222. SINK.accept("Screen Mouse scrolled: %.2f distance", amount);
  223. return InteractionResult.PASS;
  224. });
  225. ClientScreenInputEvent.MOUSE_CLICKED_PRE.register((client, screen, mouseX, mouseY, button) -> {
  226. SINK.accept("Screen Mouse clicked: " + button);
  227. return InteractionResult.PASS;
  228. });
  229. ClientScreenInputEvent.MOUSE_RELEASED_PRE.register((client, screen, mouseX, mouseY, button) -> {
  230. SINK.accept("Screen Mouse released: " + button);
  231. return InteractionResult.PASS;
  232. });
  233. ClientScreenInputEvent.MOUSE_DRAGGED_PRE.register((client, screen, mouseX1, mouseY1, button, mouseX2, mouseY2) -> {
  234. SINK.accept("Screen Mouse dragged: %d (%d,%d) by (%d,%d)", button, (int) mouseX1, (int) mouseY1, (int) mouseX2, (int) mouseY2);
  235. return InteractionResult.PASS;
  236. });
  237. ClientScreenInputEvent.CHAR_TYPED_PRE.register((client, screen, character, keyCode) -> {
  238. SINK.accept("Screen Char typed: " + character);
  239. return InteractionResult.PASS;
  240. });
  241. ClientScreenInputEvent.KEY_PRESSED_PRE.register((client, screen, keyCode, scanCode, modifiers) -> {
  242. SINK.accept("Screen Key pressed: " + InputConstants.getKey(keyCode, scanCode).getDisplayName().getString());
  243. return InteractionResult.PASS;
  244. });
  245. ClientScreenInputEvent.KEY_RELEASED_PRE.register((client, screen, keyCode, scanCode, modifiers) -> {
  246. SINK.accept("Screen Key released: " + InputConstants.getKey(keyCode, scanCode).getDisplayName().getString());
  247. return InteractionResult.PASS;
  248. });
  249. ClientRawInputEvent.MOUSE_SCROLLED.register((client, amount) -> {
  250. SINK.accept("Raw Mouse scrolled: %.2f distance", amount);
  251. return InteractionResult.PASS;
  252. });
  253. ClientRawInputEvent.MOUSE_CLICKED_PRE.register((client, button, action, mods) -> {
  254. SINK.accept("Raw Mouse clicked: " + button);
  255. return InteractionResult.PASS;
  256. });
  257. ClientRawInputEvent.KEY_PRESSED.register((client, keyCode, scanCode, action, modifiers) -> {
  258. SINK.accept("Raw Key pressed: " + InputConstants.getKey(keyCode, scanCode).getDisplayName().getString());
  259. return InteractionResult.PASS;
  260. });
  261. }
  262. private static String toSimpleName(Object o) {
  263. return o.getClass().getSimpleName() + "@" + Integer.toHexString(o.hashCode());
  264. }
  265. }