| 1 | package net.minecraft.client;
|
| 2 |
|
| 3 | import com.google.common.base.MoreObjects;
|
| 4 | import com.mojang.blaze3d.Blaze3D;
|
| 5 | import com.mojang.blaze3d.platform.ClipboardManager;
|
| 6 | import com.mojang.blaze3d.platform.InputConstants;
|
| 7 | import com.mojang.blaze3d.platform.TextureUtil;
|
| 8 | import com.mojang.blaze3d.platform.Window;
|
| 9 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 10 | import com.mojang.logging.LogUtils;
|
| 11 | import java.nio.file.Path;
|
| 12 | import java.util.Locale;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import net.minecraft.ChatFormatting;
|
| 16 | import net.minecraft.CrashReport;
|
| 17 | import net.minecraft.CrashReportCategory;
|
| 18 | import net.minecraft.ReportedException;
|
| 19 | import net.minecraft.SharedConstants;
|
| 20 | import net.minecraft.client.gui.components.EditBox;
|
| 21 | import net.minecraft.client.gui.components.debug.DebugScreenEntries;
|
| 22 | import net.minecraft.client.gui.components.events.GuiEventListener;
|
| 23 | import net.minecraft.client.gui.narration.NarrationTrigger;
|
| 24 | import net.minecraft.client.gui.screens.PauseScreen;
|
| 25 | import net.minecraft.client.gui.screens.Screen;
|
| 26 | import net.minecraft.client.gui.screens.debug.DebugOptionsScreen;
|
| 27 | import net.minecraft.client.gui.screens.debug.GameModeSwitcherScreen;
|
| 28 | import net.minecraft.client.gui.screens.options.controls.KeyBindsScreen;
|
| 29 | import net.minecraft.client.input.CharacterEvent;
|
| 30 | import net.minecraft.client.input.KeyEvent;
|
| 31 | import net.minecraft.client.input.PreeditEvent;
|
| 32 | import net.minecraft.client.player.LocalPlayer;
|
| 33 | import net.minecraft.client.renderer.fog.FogRenderer;
|
| 34 | import net.minecraft.commands.arguments.blocks.BlockStateParser;
|
| 35 | import net.minecraft.core.BlockPos;
|
| 36 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 37 | import net.minecraft.nbt.CompoundTag;
|
| 38 | import net.minecraft.nbt.NbtUtils;
|
| 39 | import net.minecraft.network.chat.CommonComponents;
|
| 40 | import net.minecraft.network.chat.Component;
|
| 41 | import net.minecraft.network.chat.ClickEvent.OpenFile;
|
| 42 | import net.minecraft.network.protocol.game.ServerboundChangeGameModePacket;
|
| 43 | import net.minecraft.resources.Identifier;
|
| 44 | import net.minecraft.server.commands.GameModeCommand;
|
| 45 | import net.minecraft.server.commands.VersionCommand;
|
| 46 | import net.minecraft.server.permissions.Permissions;
|
| 47 | import net.minecraft.util.Mth;
|
| 48 | import net.minecraft.util.NativeModuleLister;
|
| 49 | import net.minecraft.util.Util;
|
| 50 | import net.minecraft.util.ProblemReporter.ScopedCollector;
|
| 51 | import net.minecraft.world.entity.Entity;
|
| 52 | import net.minecraft.world.level.GameType;
|
| 53 | import net.minecraft.world.level.Level;
|
| 54 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 55 | import net.minecraft.world.level.block.state.BlockState;
|
| 56 | import net.minecraft.world.level.levelgen.feature.FeatureCountTracker;
|
| 57 | import net.minecraft.world.level.storage.TagValueOutput;
|
| 58 | import net.minecraft.world.phys.BlockHitResult;
|
| 59 | import net.minecraft.world.phys.EntityHitResult;
|
| 60 | import net.minecraft.world.phys.HitResult;
|
| 61 | import net.minecraft.world.phys.Vec3;
|
| 62 | import org.jspecify.annotations.Nullable;
|
| 63 | import org.slf4j.Logger;
|
| 64 |
|
| 65 | @Environment(EnvType.CLIENT)
|
| 66 | public class KeyboardHandler {
|
| 67 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 68 | public static final int DEBUG_CRASH_TIME = 10000;
|
| 69 | private final Minecraft minecraft;
|
| 70 | private final ClipboardManager clipboardManager = new ClipboardManager();
|
| 71 | private long debugCrashKeyTime = -1L;
|
| 72 | private long debugCrashKeyReportedTime = -1L;
|
| 73 | private long debugCrashKeyReportedCount = -1L;
|
| 74 | private boolean usedDebugKeyAsModifier;
|
| 75 | private @Nullable PreeditEvent lastPreeditEvent;
|
| 76 |
|
| 77 | public KeyboardHandler(final Minecraft minecraft) {
|
| 78 | this.minecraft = minecraft;
|
| 79 | }
|
| 80 |
|
| 81 | private boolean handleChunkDebugKeys(final KeyEvent event) {
|
| 82 | return switch (event.shortcutKey()) {
|
| 83 | case 101 -> {
|
| 84 | if (this.minecraft.player == null) {
|
| 85 | yield false;
|
| 86 | } else {
|
| 87 | boolean chunkSectionPaths = this.minecraft.debugEntries.toggleStatus(DebugScreenEntries.CHUNK_SECTION_PATHS);
|
| 88 | this.debugFeedback("SectionPath: " + (chunkSectionPaths ? "shown" : "hidden"));
|
| 89 | yield true;
|
| 90 | }
|
| 91 | }
|
| 92 | case 102 -> {
|
| 93 | boolean fogEnabled = FogRenderer.toggleFog();
|
| 94 | this.debugFeedbackEnabledStatus("Fog: ", fogEnabled);
|
| 95 | yield true;
|
| 96 | }
|
| 97 | default -> false;
|
| 98 | case 108 -> {
|
| 99 | this.minecraft.smartCull = !this.minecraft.smartCull;
|
| 100 | this.debugFeedbackEnabledStatus("SmartCull: ", this.minecraft.smartCull);
|
| 101 | yield true;
|
| 102 | }
|
| 103 | case 109 -> {
|
| 104 | this.minecraft.multiDrawIndirect = !this.minecraft.multiDrawIndirect;
|
| 105 | this.debugFeedbackEnabledStatus("MultiDrawIndirect: ", this.minecraft.multiDrawIndirect);
|
| 106 | yield true;
|
| 107 | }
|
| 108 | case 111 -> {
|
| 109 | if (this.minecraft.player == null) {
|
| 110 | yield false;
|
| 111 | } else {
|
| 112 | boolean renderOctree = this.minecraft.debugEntries.toggleStatus(DebugScreenEntries.CHUNK_SECTION_OCTREE);
|
| 113 | this.debugFeedbackEnabledStatus("Frustum culling Octree: ", renderOctree);
|
| 114 | yield true;
|
| 115 | }
|
| 116 | }
|
| 117 | case 117 -> {
|
| 118 | if (event.hasShiftDown()) {
|
| 119 | this.minecraft.gameRenderer.mainCamera().killFrustum();
|
| 120 | this.debugFeedback("Killed frustum");
|
| 121 | } else {
|
| 122 | this.minecraft.gameRenderer.mainCamera().captureFrustum();
|
| 123 | this.debugFeedback("Captured frustum");
|
| 124 | }
|
| 125 |
|
| 126 | yield true;
|
| 127 | }
|
| 128 | case 118 -> {
|
| 129 | if (this.minecraft.player == null) {
|
| 130 | yield false;
|
| 131 | } else {
|
| 132 | boolean sectionVisibility = this.minecraft.debugEntries.toggleStatus(DebugScreenEntries.CHUNK_SECTION_VISIBILITY);
|
| 133 | this.debugFeedbackEnabledStatus("SectionVisibility: ", sectionVisibility);
|
| 134 | yield true;
|
| 135 | }
|
| 136 | }
|
| 137 | case 119 -> {
|
| 138 | this.minecraft.wireframe = !this.minecraft.wireframe;
|
| 139 | if (this.minecraft.wireframe && !RenderSystem.isWireframeAvailable()) {
|
| 140 | this.minecraft.wireframe = false;
|
| 141 | this.debugFeedback("Unable to enable WireFrame");
|
| 142 | } else {
|
| 143 | this.debugFeedbackEnabledStatus("WireFrame: ", this.minecraft.wireframe);
|
| 144 | }
|
| 145 |
|
| 146 | yield true;
|
| 147 | }
|
| 148 | };
|
| 149 | }
|
| 150 |
|
| 151 | private void debugFeedbackEnabledStatus(final String prefix, final boolean isEnabled) {
|
| 152 | this.debugFeedback(prefix + (isEnabled ? "enabled" : "disabled"));
|
| 153 | }
|
| 154 |
|
| 155 | private static Component decorateDebugComponent(final ChatFormatting formatting, final Component component) {
|
| 156 | return Component.empty()
|
| 157 | .append(Component.translatable("debug.prefix").withStyle(formatting, ChatFormatting.BOLD))
|
| 158 | .append(CommonComponents.SPACE)
|
| 159 | .append(component);
|
| 160 | }
|
| 161 |
|
| 162 | private void debugWarningComponent(final Component component) {
|
| 163 | this.minecraft.showDebugChat(decorateDebugComponent(ChatFormatting.RED, component));
|
| 164 | }
|
| 165 |
|
| 166 | private void debugFeedbackComponent(final Component component) {
|
| 167 | this.minecraft.showDebugChat(decorateDebugComponent(ChatFormatting.YELLOW, component));
|
| 168 | }
|
| 169 |
|
| 170 | private void debugFeedbackTranslated(final String pattern) {
|
| 171 | this.debugFeedbackComponent(Component.translatable(pattern));
|
| 172 | }
|
| 173 |
|
| 174 | private void debugFeedback(final String message) {
|
| 175 | this.debugFeedbackComponent(Component.literal(message));
|
| 176 | }
|
| 177 |
|
| 178 | private boolean handleDebugKeys(final KeyEvent event) {
|
| 179 | if (this.debugCrashKeyTime > 0L && this.debugCrashKeyTime < Util.getMillis() - 100L) {
|
| 180 | return true;
|
| 181 | }
|
| 182 |
|
| 183 | if (SharedConstants.DEBUG_HOTKEYS && this.handleChunkDebugKeys(event)) {
|
| 184 | return true;
|
| 185 | }
|
| 186 |
|
| 187 | if (SharedConstants.DEBUG_FEATURE_COUNT) {
|
| 188 | switch (event.shortcutKey()) {
|
| 189 | case 108:
|
| 190 | FeatureCountTracker.logCounts();
|
| 191 | return true;
|
| 192 | case 114:
|
| 193 | FeatureCountTracker.clearCounts();
|
| 194 | return true;
|
| 195 | }
|
| 196 | }
|
| 197 |
|
| 198 | Options options = this.minecraft.options;
|
| 199 | boolean debugAction = false;
|
| 200 | if (options.keyDebugReloadChunk.matches(event)) {
|
| 201 | this.minecraft.levelExtractor.allChanged();
|
| 202 | this.debugFeedbackTranslated("debug.reload_chunks.message");
|
| 203 | debugAction = true;
|
| 204 | }
|
| 205 |
|
| 206 | if (options.keyDebugShowHitboxes.matches(event) && this.minecraft.player != null && !this.minecraft.player.isReducedDebugInfo()) {
|
| 207 | boolean renderHitBoxes = this.minecraft.debugEntries.toggleStatus(DebugScreenEntries.ENTITY_HITBOXES);
|
| 208 | this.debugFeedbackTranslated(renderHitBoxes ? "debug.show_hitboxes.on" : "debug.show_hitboxes.off");
|
| 209 | debugAction = true;
|
| 210 | }
|
| 211 |
|
| 212 | if (options.keyDebugClearChat.matches(event)) {
|
| 213 | this.minecraft.gui.hud.getChat().clearMessages(false);
|
| 214 | debugAction = true;
|
| 215 | }
|
| 216 |
|
| 217 | if (options.keyDebugShowChunkBorders.matches(event) && this.minecraft.player != null && !this.minecraft.player.isReducedDebugInfo()) {
|
| 218 | boolean displayChunkborder = this.minecraft.debugEntries.toggleStatus(DebugScreenEntries.CHUNK_BORDERS);
|
| 219 | this.debugFeedbackTranslated(displayChunkborder ? "debug.chunk_boundaries.on" : "debug.chunk_boundaries.off");
|
| 220 | debugAction = true;
|
| 221 | }
|
| 222 |
|
| 223 | if (options.keyDebugShowAdvancedTooltips.matches(event)) {
|
| 224 | options.advancedItemTooltips = !options.advancedItemTooltips;
|
| 225 | this.debugFeedbackTranslated(options.advancedItemTooltips ? "debug.advanced_tooltips.on" : "debug.advanced_tooltips.off");
|
| 226 | options.save();
|
| 227 | debugAction = true;
|
| 228 | }
|
| 229 |
|
| 230 | if (options.keyDebugCopyRecreateCommand.matches(event)) {
|
| 231 | if (this.minecraft.player != null && !this.minecraft.player.isReducedDebugInfo()) {
|
| 232 | this.copyRecreateCommand(this.minecraft.player.permissions().hasPermission(Permissions.COMMANDS_GAMEMASTER), !event.hasShiftDown());
|
| 233 | }
|
| 234 |
|
| 235 | debugAction = true;
|
| 236 | }
|
| 237 |
|
| 238 | if (options.keyDebugSpectate.matches(event)) {
|
| 239 | if (this.minecraft.player == null || !GameModeCommand.PERMISSION_CHECK.check(this.minecraft.player.permissions())) {
|
| 240 | this.debugFeedbackTranslated("debug.creative_spectator.error");
|
| 241 | } else if (!this.minecraft.player.isSpectator()) {
|
| 242 | this.minecraft.player.connection.send(new ServerboundChangeGameModePacket(GameType.SPECTATOR));
|
| 243 | } else {
|
| 244 | GameType newGameType = MoreObjects.firstNonNull(this.minecraft.gameMode.getPreviousPlayerMode(), GameType.CREATIVE);
|
| 245 | this.minecraft.player.connection.send(new ServerboundChangeGameModePacket(newGameType));
|
| 246 | }
|
| 247 |
|
| 248 | debugAction = true;
|
| 249 | }
|
| 250 |
|
| 251 | if (options.keyDebugSwitchGameMode.matches(event) && this.minecraft.level != null && this.minecraft.gui.screen() == null) {
|
| 252 | if (this.minecraft.canSwitchGameMode() && GameModeCommand.PERMISSION_CHECK.check(this.minecraft.player.permissions())) {
|
| 253 | this.minecraft.gui.setScreen(new GameModeSwitcherScreen());
|
| 254 | } else {
|
| 255 | this.debugFeedbackTranslated("debug.gamemodes.error");
|
| 256 | }
|
| 257 |
|
| 258 | debugAction = true;
|
| 259 | }
|
| 260 |
|
| 261 | if (options.keyDebugDebugOptions.matches(event)) {
|
| 262 | if (this.minecraft.gui.screen() instanceof DebugOptionsScreen) {
|
| 263 | this.minecraft.gui.screen().onClose();
|
| 264 | } else if (this.minecraft.canInterruptScreen()) {
|
| 265 | if (this.minecraft.gui.screen() != null) {
|
| 266 | this.minecraft.gui.screen().onClose();
|
| 267 | }
|
| 268 |
|
| 269 | this.minecraft.gui.setScreen(new DebugOptionsScreen());
|
| 270 | }
|
| 271 |
|
| 272 | debugAction = true;
|
| 273 | }
|
| 274 |
|
| 275 | if (options.keyDebugFocusPause.matches(event)) {
|
| 276 | options.pauseOnLostFocus = !options.pauseOnLostFocus;
|
| 277 | options.save();
|
| 278 | this.debugFeedbackTranslated(options.pauseOnLostFocus ? "debug.pause_focus.on" : "debug.pause_focus.off");
|
| 279 | debugAction = true;
|
| 280 | }
|
| 281 |
|
| 282 | if (options.keyDebugDumpDynamicTextures.matches(event)) {
|
| 283 | Path gameDirectory = this.minecraft.gameDirectory.toPath().toAbsolutePath();
|
| 284 | Path debugTexturePath = TextureUtil.getDebugTexturePath(gameDirectory);
|
| 285 | this.minecraft.getTextureManager().dumpAllSheets(debugTexturePath);
|
| 286 | Component pathComponent = Component.literal(gameDirectory.relativize(debugTexturePath).toString())
|
| 287 | .withStyle(ChatFormatting.UNDERLINE)
|
| 288 | .withStyle(s -> s.withClickEvent(new OpenFile(debugTexturePath)));
|
| 289 | this.debugFeedbackComponent(Component.translatable("debug.dump_dynamic_textures", pathComponent));
|
| 290 | debugAction = true;
|
| 291 | }
|
| 292 |
|
| 293 | if (options.keyDebugReloadResourcePacks.matches(event)) {
|
| 294 | this.debugFeedbackTranslated("debug.reload_resourcepacks.message");
|
| 295 | this.minecraft.reloadResourcePacks();
|
| 296 | debugAction = true;
|
| 297 | }
|
| 298 |
|
| 299 | if (options.keyDebugProfiling.matches(event)) {
|
| 300 | if (this.minecraft.debugClientMetricsStart(this::debugFeedbackComponent)) {
|
| 301 | this.debugFeedbackComponent(
|
| 302 | Component.translatable(
|
| 303 | "debug.profiling.start", 10, options.keyDebugModifier.getTranslatedKeyMessage(), options.keyDebugProfiling.getTranslatedKeyMessage()
|
| 304 | )
|
| 305 | );
|
| 306 | }
|
| 307 |
|
| 308 | debugAction = true;
|
| 309 | }
|
| 310 |
|
| 311 | if (options.keyDebugCopyLocation.matches(event) && this.minecraft.player != null && !this.minecraft.player.isReducedDebugInfo()) {
|
| 312 | this.debugFeedbackTranslated("debug.copy_location.message");
|
| 313 | this.setClipboard(
|
| 314 | String.format(
|
| 315 | Locale.ROOT,
|
| 316 | "/execute in %s run tp @s %.2f %.2f %.2f %.2f %.2f",
|
| 317 | this.minecraft.player.level().dimension().identifier(),
|
| 318 | this.minecraft.player.getX(),
|
| 319 | this.minecraft.player.getY(),
|
| 320 | this.minecraft.player.getZ(),
|
| 321 | this.minecraft.player.getYRot(),
|
| 322 | this.minecraft.player.getXRot()
|
| 323 | )
|
| 324 | );
|
| 325 | debugAction = true;
|
| 326 | }
|
| 327 |
|
| 328 | if (options.keyDebugDumpVersion.matches(event)) {
|
| 329 | this.debugFeedbackTranslated("debug.version.header");
|
| 330 | VersionCommand.dumpVersion(this.minecraft::showDebugChat);
|
| 331 | debugAction = true;
|
| 332 | }
|
| 333 |
|
| 334 | if (options.keyDebugPofilingChart.matches(event)) {
|
| 335 | this.minecraft.getDebugOverlay().toggleProfilerChart();
|
| 336 | debugAction = true;
|
| 337 | }
|
| 338 |
|
| 339 | if (options.keyDebugFpsCharts.matches(event)) {
|
| 340 | this.minecraft.getDebugOverlay().toggleFpsCharts();
|
| 341 | debugAction = true;
|
| 342 | }
|
| 343 |
|
| 344 | if (options.keyDebugNetworkCharts.matches(event)) {
|
| 345 | this.minecraft.getDebugOverlay().toggleNetworkCharts();
|
| 346 | debugAction = true;
|
| 347 | }
|
| 348 |
|
| 349 | if (options.keyDebugLightmapTexture.matches(event)) {
|
| 350 | this.minecraft.getDebugOverlay().toggleLightmapTexture();
|
| 351 | debugAction = true;
|
| 352 | }
|
| 353 |
|
| 354 | if (options.keyDebugSwitchTranslucencyMode.matches(event)) {
|
| 355 | options.improvedTransparency().set(!this.minecraft.options.improvedTransparency().get());
|
| 356 | options.save();
|
| 357 | this.debugFeedbackTranslated(options.improvedTransparency().get() ? "debug.improvedTransparency.on" : "debug.improvedTransparency.off");
|
| 358 | debugAction = true;
|
| 359 | }
|
| 360 |
|
| 361 | return debugAction;
|
| 362 | }
|
| 363 |
|
| 364 | private void copyRecreateCommand(final boolean addNbt, final boolean pullFromServer) {
|
| 365 | HitResult hitResult = this.minecraft.hitResult;
|
| 366 | if (hitResult != null) {
|
| 367 | switch (hitResult.getType()) {
|
| 368 | case BLOCK:
|
| 369 | BlockPos blockPos = ((BlockHitResult)hitResult).getBlockPos();
|
| 370 | Level level = this.minecraft.player.level();
|
| 371 | BlockState state = level.getBlockState(blockPos);
|
| 372 | if (addNbt) {
|
| 373 | if (pullFromServer) {
|
| 374 | this.minecraft.player.connection.getDebugQueryHandler().queryBlockEntityTag(blockPos, tagx -> {
|
| 375 | this.copyCreateBlockCommand(state, blockPos, tagx);
|
| 376 | this.debugFeedbackTranslated("debug.inspect.server.block");
|
| 377 | });
|
| 378 | } else {
|
| 379 | BlockEntity blockEntity = level.getBlockEntity(blockPos);
|
| 380 | CompoundTag tag = blockEntity != null ? blockEntity.saveWithoutMetadata(level.registryAccess()) : null;
|
| 381 | this.copyCreateBlockCommand(state, blockPos, tag);
|
| 382 | this.debugFeedbackTranslated("debug.inspect.client.block");
|
| 383 | }
|
| 384 | } else {
|
| 385 | this.copyCreateBlockCommand(state, blockPos, null);
|
| 386 | this.debugFeedbackTranslated("debug.inspect.client.block");
|
| 387 | }
|
| 388 | break;
|
| 389 | case ENTITY:
|
| 390 | Entity entity = ((EntityHitResult)hitResult).getEntity();
|
| 391 | Identifier id = BuiltInRegistries.ENTITY_TYPE.getKey(entity.getType());
|
| 392 | if (addNbt) {
|
| 393 | if (pullFromServer) {
|
| 394 | this.minecraft.player.connection.getDebugQueryHandler().queryEntityTag(entity.getId(), tagx -> {
|
| 395 | this.copyCreateEntityCommand(id, entity.position(), tagx);
|
| 396 | this.debugFeedbackTranslated("debug.inspect.server.entity");
|
| 397 | });
|
| 398 | } else {
|
| 399 | try (ScopedCollector reporter = new ScopedCollector(entity.problemPath(), LOGGER)) {
|
| 400 | TagValueOutput output = TagValueOutput.createWithContext(reporter, entity.registryAccess());
|
| 401 | entity.saveWithoutId(output);
|
| 402 | this.copyCreateEntityCommand(id, entity.position(), output.buildResult());
|
| 403 | }
|
| 404 |
|
| 405 | this.debugFeedbackTranslated("debug.inspect.client.entity");
|
| 406 | }
|
| 407 | } else {
|
| 408 | this.copyCreateEntityCommand(id, entity.position(), null);
|
| 409 | this.debugFeedbackTranslated("debug.inspect.client.entity");
|
| 410 | }
|
| 411 | }
|
| 412 | }
|
| 413 | }
|
| 414 |
|
| 415 | private void copyCreateBlockCommand(final BlockState state, final BlockPos blockPos, final @Nullable CompoundTag entityTag) {
|
| 416 | StringBuilder description = new StringBuilder(BlockStateParser.serialize(state));
|
| 417 | if (entityTag != null) {
|
| 418 | description.append(entityTag);
|
| 419 | }
|
| 420 |
|
| 421 | String command = String.format(Locale.ROOT, "/setblock %d %d %d %s", blockPos.getX(), blockPos.getY(), blockPos.getZ(), description);
|
| 422 | this.setClipboard(command);
|
| 423 | }
|
| 424 |
|
| 425 | private void copyCreateEntityCommand(final Identifier id, final Vec3 pos, final @Nullable CompoundTag entityTag) {
|
| 426 | String command;
|
| 427 | if (entityTag != null) {
|
| 428 | entityTag.remove("UUID");
|
| 429 | entityTag.remove("Pos");
|
| 430 | String snbt = NbtUtils.toPrettyComponent(entityTag).getString();
|
| 431 | command = String.format(Locale.ROOT, "/summon %s %.2f %.2f %.2f %s", id, pos.x, pos.y, pos.z, snbt);
|
| 432 | } else {
|
| 433 | command = String.format(Locale.ROOT, "/summon %s %.2f %.2f %.2f", id, pos.x, pos.y, pos.z);
|
| 434 | }
|
| 435 |
|
| 436 | this.setClipboard(command);
|
| 437 | }
|
| 438 |
|
| 439 | public void keyPress(final long handle, final @KeyEvent.Action int action, final KeyEvent event) {
|
| 440 | Window window = this.minecraft.getWindow();
|
| 441 | if (handle != 0L && handle == window.handle()) {
|
| 442 | this.minecraft.getFramerateLimitTracker().onInputReceived();
|
| 443 | Options options = this.minecraft.options;
|
| 444 | boolean modifierAndOverlayIsSame = options.keyDebugModifier.key.getValue() == options.keyDebugOverlay.key.getValue();
|
| 445 | boolean debugModifierDown = options.keyDebugModifier.isDown();
|
| 446 | boolean debugCrash = !options.keyDebugCrash.isUnbound() && InputConstants.isKeyDown(options.keyDebugCrash.key.getValue());
|
| 447 | if (this.debugCrashKeyTime > 0L) {
|
| 448 | if (!debugCrash || !debugModifierDown) {
|
| 449 | this.debugCrashKeyTime = -1L;
|
| 450 | }
|
| 451 | } else if (debugCrash && debugModifierDown) {
|
| 452 | this.usedDebugKeyAsModifier = modifierAndOverlayIsSame;
|
| 453 | this.debugCrashKeyTime = Util.getMillis();
|
| 454 | this.debugCrashKeyReportedTime = Util.getMillis();
|
| 455 | this.debugCrashKeyReportedCount = 0L;
|
| 456 | }
|
| 457 |
|
| 458 | Screen screen = this.minecraft.gui.screen();
|
| 459 | if (screen != null) {
|
| 460 | switch (event.shortcutKey()) {
|
| 461 | case 9:
|
| 462 | this.minecraft.setLastInputType(InputType.KEYBOARD_TAB);
|
| 463 | break;
|
| 464 | case 1073741903:
|
| 465 | case 1073741904:
|
| 466 | case 1073741905:
|
| 467 | case 1073741906:
|
| 468 | this.minecraft.setLastInputType(InputType.KEYBOARD_ARROW);
|
| 469 | }
|
| 470 | }
|
| 471 |
|
| 472 | if (action != 1
|
| 473 | || screen instanceof KeyBindsScreen keyBindsScreen && keyBindsScreen.lastKeySelection > Util.getMillis() - 20L
|
| 474 | || !this.minecraft.handleGlobalKeyPress(InputConstants.getKey(event), event.hasControlDownWithQuirk())) {
|
| 475 | if (action != 0) {
|
| 476 | boolean hasNoEditboxFocused = screen == null || !(screen.getFocused() instanceof EditBox) || !((EditBox)screen.getFocused()).canConsumeInput();
|
| 477 | if (hasNoEditboxFocused) {
|
| 478 | if (event.hasControlDownWithQuirk() && event.shortcutKey() == 98 && this.minecraft.getNarrator().isActive() && options.narratorHotkey().get()) {
|
| 479 | boolean wasDisabled = options.narrator().get() == NarratorStatus.OFF;
|
| 480 | options.narrator().set(NarratorStatus.byId(options.narrator().get().getId() + 1));
|
| 481 | options.save();
|
| 482 | if (screen != null) {
|
| 483 | screen.updateNarratorStatus(wasDisabled, NarrationTrigger.KEYBOARD);
|
| 484 | }
|
| 485 | }
|
| 486 |
|
| 487 | LocalPlayer var21 = this.minecraft.player;
|
| 488 | }
|
| 489 | }
|
| 490 |
|
| 491 | if (screen != null) {
|
| 492 | try {
|
| 493 | if (action != 1 && action != -1) {
|
| 494 | if (action == 0 && screen.keyReleased(event)) {
|
| 495 | if (options.keyDebugModifier.matches(event)) {
|
| 496 | this.usedDebugKeyAsModifier = false;
|
| 497 | }
|
| 498 |
|
| 499 | return;
|
| 500 | }
|
| 501 | } else {
|
| 502 | screen.afterKeyboardAction();
|
| 503 | if (screen.keyPressed(event)) {
|
| 504 | if (this.minecraft.gui.screen() == null) {
|
| 505 | InputConstants.Key key = InputConstants.getKey(event);
|
| 506 | KeyMapping.set(key, false);
|
| 507 | }
|
| 508 |
|
| 509 | return;
|
| 510 | }
|
| 511 | }
|
| 512 | } catch (Throwable t) {
|
| 513 | CrashReport report = CrashReport.forThrowable(t, "keyPressed event handler");
|
| 514 | CrashReportCategory keyDetails = report.addCategory("Key");
|
| 515 | keyDetails.setDetail("Scancode", event.key());
|
| 516 | keyDetails.setDetail("Keycode", event.keycode());
|
| 517 | keyDetails.setDetail("Mods", event.modifiers());
|
| 518 | throw new ReportedException(report);
|
| 519 | }
|
| 520 | }
|
| 521 |
|
| 522 | InputConstants.Key key = InputConstants.getKey(event);
|
| 523 | boolean handlesGameInput = this.minecraft.gui.screen() == null;
|
| 524 | boolean handlesGlobalInput = handlesGameInput
|
| 525 | || this.minecraft.gui.screen() instanceof PauseScreen pauseScreen && !pauseScreen.showsPauseMenu()
|
| 526 | || this.minecraft.gui.screen() instanceof GameModeSwitcherScreen;
|
| 527 | if (modifierAndOverlayIsSame && options.keyDebugModifier.matches(event) && action == 0) {
|
| 528 | if (this.usedDebugKeyAsModifier) {
|
| 529 | this.usedDebugKeyAsModifier = false;
|
| 530 | } else {
|
| 531 | this.minecraft.debugEntries.toggleDebugOverlay();
|
| 532 | }
|
| 533 | } else if (!modifierAndOverlayIsSame && options.keyDebugOverlay.matches(event) && action == 1) {
|
| 534 | this.minecraft.debugEntries.toggleDebugOverlay();
|
| 535 | }
|
| 536 |
|
| 537 | if (action == 0) {
|
| 538 | KeyMapping.set(key, false);
|
| 539 | } else {
|
| 540 | boolean didDebugAction = false;
|
| 541 | if (handlesGlobalInput && event.isEscape()) {
|
| 542 | this.minecraft.pauseGame(debugModifierDown);
|
| 543 | didDebugAction = debugModifierDown;
|
| 544 | } else if (debugModifierDown) {
|
| 545 | didDebugAction = this.handleDebugKeys(event);
|
| 546 | if (didDebugAction && screen instanceof DebugOptionsScreen debugOptionsScreen) {
|
| 547 | DebugOptionsScreen.OptionList optionList = debugOptionsScreen.getOptionList();
|
| 548 | if (optionList != null) {
|
| 549 | optionList.children().forEach(DebugOptionsScreen.AbstractOptionEntry::refreshEntry);
|
| 550 | }
|
| 551 | }
|
| 552 | }
|
| 553 |
|
| 554 | if (modifierAndOverlayIsSame) {
|
| 555 | this.usedDebugKeyAsModifier |= didDebugAction;
|
| 556 | }
|
| 557 |
|
| 558 | if (this.minecraft.getDebugOverlay().showProfilerChart() && !debugModifierDown) {
|
| 559 | InputConstants.getKey(event)
|
| 560 | .getNumericKeyValue()
|
| 561 | .ifPresent(digit -> this.minecraft.getDebugOverlay().getProfilerPieChart().profilerPieChartKeyPress(digit));
|
| 562 | }
|
| 563 |
|
| 564 | if (handlesGameInput) {
|
| 565 | if (didDebugAction) {
|
| 566 | KeyMapping.set(key, false);
|
| 567 | } else {
|
| 568 | KeyMapping.set(key, true);
|
| 569 | KeyMapping.click(key);
|
| 570 | }
|
| 571 | } else if (key == options.keyDebugModifier.key) {
|
| 572 | options.keyDebugModifier.setDown(!didDebugAction);
|
| 573 | }
|
| 574 | }
|
| 575 | }
|
| 576 | }
|
| 577 | }
|
| 578 |
|
| 579 | public void charTyped(final long handle, final CharacterEvent event) {
|
| 580 | if (handle != 0L && handle == this.minecraft.getWindow().handle()) {
|
| 581 | Screen screen = this.minecraft.gui.screen();
|
| 582 | if (screen != null && this.minecraft.gui.overlay() == null) {
|
| 583 | try {
|
| 584 | screen.charTyped(event);
|
| 585 | } catch (Throwable t) {
|
| 586 | CrashReport report = CrashReport.forThrowable(t, "charTyped event handler");
|
| 587 | CrashReportCategory keyDetails = report.addCategory("Key");
|
| 588 | keyDetails.setDetail("Codepoint", event.codepoint());
|
| 589 | throw new ReportedException(report);
|
| 590 | }
|
| 591 | }
|
| 592 | }
|
| 593 | }
|
| 594 |
|
| 595 | public void textInput(final long handle, final String text) {
|
| 596 | this.textEditing(handle, null);
|
| 597 | text.codePoints().forEach(codepoint -> this.charTyped(handle, new CharacterEvent(codepoint)));
|
| 598 | }
|
| 599 |
|
| 600 | public void textEditing(final long handle, final @Nullable PreeditEvent event) {
|
| 601 | if (handle != 0L && handle == this.minecraft.getWindow().handle()) {
|
| 602 | this.lastPreeditEvent = event;
|
| 603 | Screen screen = this.minecraft.gui.screen();
|
| 604 | if (screen != null && this.minecraft.gui.overlay() == null) {
|
| 605 | submitPreeditEvent(screen, event);
|
| 606 | }
|
| 607 | }
|
| 608 | }
|
| 609 |
|
| 610 | public void resubmitLastPreeditEvent(final GuiEventListener screen) {
|
| 611 | submitPreeditEvent(screen, this.lastPreeditEvent);
|
| 612 | }
|
| 613 |
|
| 614 | public static void submitPreeditEvent(final GuiEventListener element, final @Nullable PreeditEvent event) {
|
| 615 | try {
|
| 616 | element.preeditUpdated(event);
|
| 617 | } catch (Throwable t) {
|
| 618 | CrashReport report = CrashReport.forThrowable(t, "IME pre-edit event handler");
|
| 619 | CrashReportCategory keyDetails = report.addCategory("Event");
|
| 620 | keyDetails.setDetail("Contents", () -> String.valueOf(event));
|
| 621 | throw new ReportedException(report);
|
| 622 | }
|
| 623 | }
|
| 624 |
|
| 625 | public String getClipboard() {
|
| 626 | return this.clipboardManager.getClipboard();
|
| 627 | }
|
| 628 |
|
| 629 | public void setClipboard(final String clipboard) {
|
| 630 | if (!clipboard.isEmpty()) {
|
| 631 | this.clipboardManager.setClipboard(clipboard);
|
| 632 | }
|
| 633 | }
|
| 634 |
|
| 635 | public void tick() {
|
| 636 | if (this.debugCrashKeyTime > 0L) {
|
| 637 | long now = Util.getMillis();
|
| 638 | long remainingTime = 10000L - (now - this.debugCrashKeyTime);
|
| 639 | long reportedTime = now - this.debugCrashKeyReportedTime;
|
| 640 | if (remainingTime < 0L) {
|
| 641 | if (this.minecraft.hasControlDown()) {
|
| 642 | Blaze3D.youJustLostTheGame();
|
| 643 | }
|
| 644 |
|
| 645 | String message = "Manually triggered debug crash";
|
| 646 | CrashReport report = new CrashReport("Manually triggered debug crash", new Throwable("Manually triggered debug crash"));
|
| 647 | CrashReportCategory manualCrashDetails = report.addCategory("Manual crash details");
|
| 648 | NativeModuleLister.addCrashSection(manualCrashDetails);
|
| 649 | throw new ReportedException(report);
|
| 650 | }
|
| 651 |
|
| 652 | if (reportedTime >= 1000L) {
|
| 653 | if (this.debugCrashKeyReportedCount == 0L) {
|
| 654 | this.debugFeedbackComponent(
|
| 655 | Component.translatable(
|
| 656 | "debug.crash.message", this.minecraft.options.keyDebugModifier.getTranslatedKeyMessage(), this.minecraft.options.keyDebugCrash.getTranslatedKeyMessage()
|
| 657 | )
|
| 658 | );
|
| 659 | } else {
|
| 660 | this.debugWarningComponent(Component.translatable("debug.crash.warning", Mth.ceil((float)remainingTime / 1000.0F)));
|
| 661 | }
|
| 662 |
|
| 663 | this.debugCrashKeyReportedTime = now;
|
| 664 | this.debugCrashKeyReportedCount++;
|
| 665 | }
|
| 666 | }
|
| 667 | }
|
| 668 | }
|
| 669 |
|