Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AFTER_CLIENT_WORLD_CHANGE #4173

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*

Check failure on line 1 in fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/api/entity/event/client/ClientWorldChangeEvents.java

View workflow job for this annotation

GitHub Actions / build (21-ubuntu)

File does not end with a newline.
* 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.entity.event.client;
modmuss50 marked this conversation as resolved.
Show resolved Hide resolved

import org.jetbrains.annotations.Nullable;

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 ClientWorldChangeEvents {
/**
* 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 or null
*/
void afterWorldChange(MinecraftClient client, @Nullable ClientWorld world);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*

Check failure on line 1 in fabric-entity-events-v1/src/client/java/net/fabricmc/fabric/mixin/client/entity/event/MinecraftClientMixin.java

View workflow job for this annotation

GitHub Actions / build (21-ubuntu)

File does not end with a newline.
* 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.mixin.client.entity.event;

import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
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.entity.event.client.ClientWorldChangeEvents;

@Mixin(MinecraftClient.class)
public class MinecraftClientMixin {
@Inject(method = "setWorld", at = @At("TAIL"))
private void afterClientWorldChange(@Nullable ClientWorld world, CallbackInfo ci) {
MinecraftClient client = (MinecraftClient) (Object) this;
ClientWorldChangeEvents.AFTER_CLIENT_WORLD_CHANGE.invoker().afterWorldChange(client, world);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"package": "net.fabricmc.fabric.mixin.client.entity.event",
"compatibilityLevel": "JAVA_17",
"client": [
"elytra.ClientPlayerEntityMixin"
"elytra.ClientPlayerEntityMixin",
"MinecraftClientMixin"
],
"injectors": {
"defaultRequire": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,29 @@

package net.fabricmc.fabric.test.entity.event.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.minecraft.entity.EquipmentSlot;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.LivingEntityFeatureRenderEvents;
import net.fabricmc.fabric.api.entity.event.client.ClientWorldChangeEvents;
import net.fabricmc.fabric.test.entity.event.EntityEventTests;

public class EntityEventTestsClient implements ClientModInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger(EntityEventTestsClient.class);

@Override
public void onInitializeClient() {
LivingEntityFeatureRenderEvents.ALLOW_CAPE_RENDER.register(player -> {
return !player.getEquippedStack(EquipmentSlot.CHEST).isOf(EntityEventTests.DIAMOND_ELYTRA);
});

ClientWorldChangeEvents.AFTER_CLIENT_WORLD_CHANGE.register((client, world) -> {
if (world != null) {
LOGGER.info("Client world changed to {}", world.getRegistryKey().getValue());
}
});
}
}
Loading