Files
MinecraftForge/patches/minecraft/net/minecraft/client/Minecraft.java.patch
T

343 lines
17 KiB
Diff

--- a/net/minecraft/client/Minecraft.java
+++ b/net/minecraft/client/Minecraft.java
@@ -268,7 +_,7 @@
import org.slf4j.Logger;
@OnlyIn(Dist.CLIENT)
-public class Minecraft extends ReentrantBlockableEventLoop<Runnable> implements WindowEventHandler {
+public class Minecraft extends ReentrantBlockableEventLoop<Runnable> implements WindowEventHandler, net.minecraftforge.client.extensions.IForgeMinecraft {
private static Minecraft instance;
private static final Logger LOGGER = LogUtils.getLogger();
private static final int MAX_TICKS_PER_UPDATE = 10;
@@ -432,7 +_,6 @@
}
}, Util.nonCriticalIoPool());
LOGGER.info("Setting user: {}", this.user.getName());
- LOGGER.debug("(Session ID is {})", this.user.getSessionId());
this.demo = gameConfig.game.demo;
this.allowsMultiplayer = !gameConfig.game.disableMultiplayer;
this.allowsChat = !gameConfig.game.disableChat;
@@ -518,14 +_,14 @@
LOGGER.error("Couldn't set icon", (Throwable)var23);
}
+ // FORGE: Move mouse and keyboard handler setup further below
this.mouseHandler = new MouseHandler(this);
- this.mouseHandler.setup(this.window);
this.keyboardHandler = new KeyboardHandler(this);
- this.keyboardHandler.setup(this.window);
this.options.applyGraphicsPreset(this.options.graphicsPreset().get());
LOGGER.info("Using optional rendering extensions: {}", String.join(", ", RenderSystem.getDevice().getEnabledExtensions()));
this.mainRenderTarget = new MainTarget(this.window.getWidth(), this.window.getHeight());
this.resourceManager = new ReloadableResourceManager(PackType.CLIENT_RESOURCES);
+ net.minecraftforge.client.loading.ClientModLoader.begin(this, this.resourcePackRepository, this.resourceManager);
this.resourcePackRepository.reload();
this.options.loadSelectedResourcePacks(this.resourcePackRepository);
this.languageManager = new LanguageManager(this.options.languageCode, languageData -> {
@@ -613,6 +_,7 @@
this.particleResources = new ParticleResources();
this.resourceManager.registerReloadListener(this.particleResources);
this.particleEngine = new ParticleEngine(this.level, this.particleResources);
+ net.minecraftforge.client.ForgeHooksClient.onRegisterParticleProviders(this.particleResources);
this.particleResources.onReload(this.particleEngine::clearParticles);
this.waypointStyles = new WaypointStyleManager();
this.resourceManager.registerReloadListener(this.waypointStyles);
@@ -634,6 +_,9 @@
this.resourceManager.registerReloadListener(this.gpuWarnlistManager);
this.resourceManager.registerReloadListener(this.regionalCompliancies);
this.gui = new Gui(this);
+ // FORGE: Moved keyboard and mouse handler setup below ingame gui creation to prevent NPEs in them.
+ this.mouseHandler.setup(this.window);
+ this.keyboardHandler.setup(this.window);
RealmsClient realmsClient = RealmsClient.getOrCreate(this);
this.realmsDataFetcher = new RealmsDataFetcher(realmsClient);
RenderSystem.setErrorCallback(this::onFullscreenError);
@@ -665,6 +_,7 @@
}
}
+ net.minecraftforge.client.ForgeHooksClient.initClientHooks(this, this.resourceManager);
this.window.updateVsync(this.options.enableVsync().get());
this.window.updateRawMouseInput(this.options.rawMouseInput().get());
this.window.setAllowCursorChanges(this.options.allowCursorChanges().get());
@@ -690,14 +_,15 @@
.createReload(Util.backgroundExecutor().forName("resourceLoad"), this, RESOURCE_RELOAD_INITIAL_TASK, packs);
GameLoadTimesEvent.INSTANCE.beginStep(TelemetryProperty.LOAD_TIME_LOADING_OVERLAY_MS);
Minecraft.GameLoadCookie loadCookie = new Minecraft.GameLoadCookie(realmsClient, gameConfig.quickPlay);
- this.setOverlay(new LoadingOverlay(this, reloadInstance, maybeT -> Util.ifElse(maybeT, t -> this.rollbackResourcePacks(t, loadCookie), () -> {
+ this.setOverlay(net.minecraftforge.fml.loading.ImmediateWindowHandler.<LoadingOverlay>loadingOverlay(() -> this, () ->reloadInstance, maybeT -> Util.ifElse(maybeT, t -> this.rollbackResourcePacks(t, loadCookie), () -> {
if (SharedConstants.IS_RUNNING_IN_IDE) {
this.selfTest();
}
this.reloadStateTracker.finishReload();
this.onResourceLoadFinished(loadCookie);
- }), false));
+ net.minecraftforge.client.loading.ClientModLoader.completeModLoading();
+ }), false).get());
this.quickPlayLog = QuickPlayLog.of(gameConfig.quickPlay.logPath());
this.framerateLimitTracker = new FramerateLimitTracker(this.options, this);
this.fpsPieProfiler = new ContinuousProfiler(Util.timeSource, () -> this.fpsPieRenderTicks, this.framerateLimitTracker::isHeavilyThrottled);
@@ -816,6 +_,7 @@
StringBuilder builder = new StringBuilder("Minecraft");
if (checkModStatus().shouldReportAsModified()) {
builder.append("*");
+ builder.append(" Forge");
}
builder.append(" ");
@@ -839,6 +_,8 @@
}
private UserApiService createUserApiService(final YggdrasilAuthenticationService authService, final GameConfig config) {
+ if ("0".equals(config.user.user.getAccessToken())) // Forge: We use "0" in dev. Short circuit to stop exception spam.
+ return UserApiService.OFFLINE;
return config.game.offlineDeveloperMode ? UserApiService.OFFLINE : authService.createUserApiService(config.user.user.getAccessToken());
}
@@ -851,7 +_,7 @@
}
private void rollbackResourcePacks(final Throwable t, final Minecraft.@Nullable GameLoadCookie loadCookie) {
- if (this.resourcePackRepository.getSelectedIds().size() > 1) {
+ if (this.resourcePackRepository.getSelectedPacks().stream().anyMatch(e -> !e.isRequired())) { //Forge: This caused infinite loop if any resource packs are forced. Such as mod resources. So check if we can disable any.
this.clearResourcePacksOnError(t, null, loadCookie);
} else {
Util.throwAsRuntime(t);
@@ -1128,12 +_,6 @@
LOGGER.error("setScreen called from non-game thread");
}
- if (this.screen != null) {
- this.screen.removed();
- } else {
- this.setLastInputType(InputType.NONE);
- }
-
if (screen == null) {
if (this.clientLevelTeardownInProgress) {
throw new IllegalStateException("Trying to return to in-game GUI during disconnection");
@@ -1152,6 +_,23 @@
}
}
+ net.minecraftforge.client.ForgeHooksClient.clearGuiLayers(this);
+ Screen old = this.screen;
+ if (screen != null) {
+ var event = net.minecraftforge.client.event.ForgeEventFactoryClient.onScreenOpening(old, screen);
+ if (event == null) return;
+ screen = event;
+ }
+
+ if (screen != old) {
+ if (old != null) {
+ net.minecraftforge.client.event.ForgeEventFactoryClient.onScreenClose(old);
+ old.removed();
+ } else {
+ this.setLastInputType(InputType.NONE);
+ }
+ }
+
this.screen = screen;
if (this.screen != null) {
this.screen.added();
@@ -1235,6 +_,7 @@
Util.shutdownExecutors();
RenderSystem.getSamplerCache().close();
RenderSystem.getDevice().close();
+ net.minecraftforge.fml.config.ConfigTracker.forceUnload();
} catch (Throwable var5) {
LOGGER.error("Shutdown failure!", var5);
throw var5;
@@ -1346,7 +_,9 @@
profiler.popPush("gpuAsync");
RenderSystem.executePendingTasks();
profiler.pop();
+ net.minecraftforge.client.event.ForgeEventFactoryClient.onRenderTickStart(this.deltaTracker);
this.gameRenderer.render(this.deltaTracker, advanceGameTime);
+ net.minecraftforge.client.event.ForgeEventFactoryClient.onRenderTickEnd(this.deltaTracker);
profiler.push("present");
if (!this.gameRenderer.getGameRenderState().windowRenderState.isMinimized) {
this.mainRenderTarget.blitToScreen();
@@ -1450,6 +_,7 @@
this.window.setGuiScale(guiScale);
if (this.screen != null) {
this.screen.resize(this.window.getGuiScaledWidth(), this.window.getGuiScaledHeight());
+ net.minecraftforge.client.ForgeHooksClient.resizeGuiLayers(this, this.window.getGuiScaledWidth(), this.window.getGuiScaledHeight());
}
this.mouseHandler.setIgnoreFirstMove();
@@ -1601,6 +_,7 @@
}
public void stop() {
+ if (this.isRunning()) net.minecraftforge.event.ForgeEventFactory.onGameShuttingDown();
this.running = false;
}
@@ -1630,10 +_,18 @@
if (down && this.hitResult != null && this.hitResult.getType() == HitResult.Type.BLOCK) {
BlockHitResult blockHit = (BlockHitResult)this.hitResult;
BlockPos pos = blockHit.getBlockPos();
- if (!this.level.getBlockState(pos).isAir()) {
+ if (!this.level.isEmptyBlock(pos)) {
+ var inputEvent = new net.minecraftforge.client.event.InputEvent.InteractionKeyMappingTriggered(0, this.options.keyAttack, InteractionHand.MAIN_HAND);
+ if (net.minecraftforge.client.event.InputEvent.InteractionKeyMappingTriggered.BUS.post(inputEvent)) {
+ if (inputEvent.shouldSwingHand()) {
+ this.level.addBreakingBlockEffect(pos, blockHit);
+ this.player.swing(InteractionHand.MAIN_HAND);
+ }
+ return;
+ }
Direction direction = blockHit.getDirection();
- if (this.gameMode.continueDestroyBlock(pos, direction)) {
- this.level.addBreakingBlockEffect(pos, direction);
+ if (this.gameMode.continueDestroyBlock(pos, direction) && inputEvent.shouldSwingHand()) {
+ this.level.addBreakingBlockEffect(pos, blockHit);
this.player.swing(InteractionHand.MAIN_HAND);
}
}
@@ -1676,6 +_,8 @@
this.player.swing(InteractionHand.MAIN_HAND);
return true;
} else {
+ var inputEvent = new net.minecraftforge.client.event.InputEvent.InteractionKeyMappingTriggered(0, this.options.keyAttack, InteractionHand.MAIN_HAND);
+ if (!net.minecraftforge.client.event.InputEvent.InteractionKeyMappingTriggered.BUS.post(inputEvent))
switch (this.hitResult.getType()) {
case ENTITY:
AttackRange customItemRange = heldItem.get(DataComponents.ATTACK_RANGE);
@@ -1686,7 +_,7 @@
case BLOCK:
BlockHitResult blockHit = (BlockHitResult)this.hitResult;
BlockPos pos = blockHit.getBlockPos();
- if (!this.level.getBlockState(pos).isAir()) {
+ if (!this.level.isEmptyBlock(pos)) {
this.gameMode.startDestroyBlock(pos, blockHit.getDirection());
if (this.level.getBlockState(pos).isAir()) {
endAttack = true;
@@ -1699,8 +_,10 @@
}
this.player.resetAttackStrengthTicker();
+ net.minecraftforge.event.ForgeEventFactory.onLeftClickEmpty(this.player);
}
+ if (inputEvent.shouldSwingHand())
this.player.swing(InteractionHand.MAIN_HAND);
return endAttack;
}
@@ -1717,6 +_,12 @@
}
for (InteractionHand hand : InteractionHand.values()) {
+ var inputEvent = new net.minecraftforge.client.event.InputEvent.InteractionKeyMappingTriggered(1, this.options.keyUse, hand);
+ if (net.minecraftforge.client.event.InputEvent.InteractionKeyMappingTriggered.BUS.post(inputEvent)) {
+ if (inputEvent.shouldSwingHand()) this.player.swing(hand);
+ return;
+ }
+
ItemStack heldItem = this.player.getItemInHand(hand);
if (!heldItem.isItemEnabled(this.level.enabledFeatures())) {
return;
@@ -1733,7 +_,7 @@
if (this.player.isWithinEntityInteractionRange(entity, 0.0)
&& this.gameMode.interact(this.player, entity, entityHit, hand) instanceof InteractionResult.Success success) {
- if (success.swingSource() == InteractionResult.SwingSource.CLIENT) {
+ if (success.swingSource() == InteractionResult.SwingSource.CLIENT && inputEvent.shouldSwingHand()) {
this.player.swing(hand);
}
@@ -1745,7 +_,7 @@
int oldCount = heldItem.getCount();
InteractionResult useResult = this.gameMode.useItemOn(this.player, hand, blockHit);
if (useResult instanceof InteractionResult.Success success) {
- if (success.swingSource() == InteractionResult.SwingSource.CLIENT) {
+ if (success.swingSource() == InteractionResult.SwingSource.CLIENT && inputEvent.shouldSwingHand()) {
this.player.swing(hand);
if (!heldItem.isEmpty() && (heldItem.getCount() != oldCount || this.player.hasInfiniteMaterials())) {
this.gameRenderer.itemInHandRenderer.itemUsed(hand);
@@ -1761,6 +_,9 @@
}
}
+ if (heldItem.isEmpty() && (this.hitResult == null || this.hitResult.getType() == HitResult.Type.MISS))
+ net.minecraftforge.event.ForgeEventFactory.onRightClickEmpty(this.player, hand);
+
if (!heldItem.isEmpty() && this.gameMode.useItem(this.player, hand) instanceof InteractionResult.Success success) {
if (success.swingSource() == InteractionResult.SwingSource.CLIENT) {
this.player.swing(hand);
@@ -1789,6 +_,8 @@
}
ProfilerFiller profiler = Profiler.get();
+ net.minecraftforge.event.ForgeEventFactory.onPreClientTick();
+
profiler.push("gui");
this.textInputManager.tick();
this.chatListener.tick();
@@ -1871,6 +_,7 @@
this.tutorial.tick();
+ net.minecraftforge.event.ForgeEventFactory.onPreLevelTick(this.level, () -> true);
try {
this.level.tick(() -> true);
} catch (Throwable var6) {
@@ -1884,6 +_,7 @@
throw new ReportedException(report);
}
+ net.minecraftforge.event.ForgeEventFactory.onPostLevelTick(this.level, () -> true);
}
profiler.popPush("animateTick");
@@ -1908,6 +_,7 @@
profiler.popPush("keyboard");
this.keyboardHandler.tick();
profiler.pop();
+ net.minecraftforge.event.ForgeEventFactory.onPostClientTick();
}
private boolean isLevelRunningNormally() {
@@ -2138,6 +_,7 @@
}
public void setLevel(final ClientLevel level) {
+ if (this.level != null) net.minecraftforge.event.ForgeEventFactory.onLevelUnload(this.level);
this.level = level;
this.updateLevelInEngines(level);
}
@@ -2199,6 +_,7 @@
IntegratedServer server = this.singleplayerServer;
this.singleplayerServer = null;
this.gameRenderer.resetData();
+ net.minecraftforge.client.event.ForgeEventFactoryClient.firePlayerLogout(this.gameMode, this.player);
this.gameMode = null;
this.narrator.clear();
this.clientLevelTeardownInProgress = true;
@@ -2206,6 +_,7 @@
try {
if (this.level != null) {
this.gui.onDisconnected();
+ net.minecraftforge.event.ForgeEventFactory.onLevelUnload(this.level);
}
this.level = null;
@@ -2220,6 +_,7 @@
}
profiler.pop();
+ net.minecraftforge.client.ForgeHooksClient.handleClientLevelClosing(this.level);
}
this.setScreenAndShow(screen);
@@ -2372,6 +_,7 @@
private void pickBlockOrEntity() {
if (this.hitResult != null && this.hitResult.getType() != HitResult.Type.MISS) {
+ if (net.minecraftforge.client.event.ForgeEventFactoryClient.onClickInputPickBlock(this.options.keyPickItem)) return;
boolean includeData = this.hasControlDown();
switch (this.hitResult) {
case BlockHitResult blockHitResult: