Add AFTER_CLIENT_WORLD_CHANGE (#4173)

* add AFTER_CLIENT_WORLD_CHANGE

* fix

* move

* add description to README and change class to final with a private constructor

* revert the event name
This commit is contained in:
fishshi 2024-11-13 02:16:14 +08:00 committed by modmuss50
parent e9d2cfc835
commit 6da5ef698d
4 changed files with 72 additions and 0 deletions

View file

@ -62,6 +62,11 @@ Currently, this contains events related to when the Minecraft Client is starting
Events related to ticking of a Minecraft client.
There are events that indicate the beginning and end of the tick for the client and the `ClientWorld` if in game.
## `ClientWorldEvents`
Events related to the lifecycle a `ClientWorld`.
Currently, this contains an event which is called after `ClientWorld` has been changed.
## `ClientChunkEvents`
Events related to the lifecycle of chunks on a Minecraft client.

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.api.client.event.lifecycle.v1;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.world.ClientWorld;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
public final class ClientWorldEvents {
private ClientWorldEvents() {
}
/**
* An event which is called after the client world has been changed.
*/
public static final Event<AfterClientWorldChange> AFTER_CLIENT_WORLD_CHANGE = EventFactory.createArrayBacked(AfterClientWorldChange.class, callbacks -> (client, world) -> {
for (AfterClientWorldChange callback : callbacks) {
callback.afterWorldChange(client, world);
}
});
@FunctionalInterface
public interface AfterClientWorldChange {
/**
* Called after the client world has been changed.
*
* @param client the client instance
* @param world the new world instance
*/
void afterWorldChange(MinecraftClient client, ClientWorld world);
}
}

View file

@ -22,9 +22,11 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.world.ClientWorld;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientWorldEvents;
@Mixin(MinecraftClient.class)
public abstract class MinecraftClientMixin {
@ -48,4 +50,12 @@ public abstract class MinecraftClientMixin {
private void onStart(CallbackInfo ci) {
ClientLifecycleEvents.CLIENT_STARTED.invoker().onClientStarted((MinecraftClient) (Object) this);
}
@Inject(method = "setWorld", at = @At("TAIL"))
private void afterClientWorldChange(ClientWorld world, CallbackInfo ci) {
if (world != null) {
MinecraftClient client = (MinecraftClient) (Object) this;
ClientWorldEvents.AFTER_CLIENT_WORLD_CHANGE.invoker().afterWorldChange(client, world);
}
}
}

View file

@ -16,10 +16,15 @@
package net.fabricmc.fabric.test.event.lifecycle.client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientWorldEvents;
public final class ClientLifecycleTests implements ClientModInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger(ClientLifecycleTests.class);
private boolean startCalled;
private boolean stopCalled;
@ -44,5 +49,9 @@ public final class ClientLifecycleTests implements ClientModInitializer {
stopCalled = true;
System.out.println("Client has started stopping!");
});
ClientWorldEvents.AFTER_CLIENT_WORLD_CHANGE.register((client, world) -> {
LOGGER.info("Client world changed to {}", world.getRegistryKey().getValue());
});
}
}