fabric: Remove unused message callbacks and switch asComponent calls to use registry-aware conversion

fixes #533
This commit is contained in:
Jason Penilla committed 2024-08-20 12:48:24 -07:00
1 parent 2faaa2dcda
commit 6254a4b65a
6 files changed
+4 -357

No files matched your search

@@ -1,106 +0,0 @@
/*
* CarbonChat
*
* Copyright (c) 2024 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.fabric.callback;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.kyori.adventure.text.Component;
import net.minecraft.server.level.ServerPlayer;
import org.checkerframework.checker.nullness.qual.Nullable;
public final class PlayerStatusMessageEvents {
public static final Event<MessageEventListener> JOIN_MESSAGE = EventFactory.createArrayBacked(
MessageEventListener.class,
callbacks -> messageEvent -> {
for (final MessageEventListener callback : callbacks) {
callback.onMessage(messageEvent);
}
}
);
public static final Event<MessageEventListener> QUIT_MESSAGE = EventFactory.createArrayBacked(
MessageEventListener.class,
callbacks -> messageEvent -> {
for (final MessageEventListener callback : callbacks) {
callback.onMessage(messageEvent);
}
}
);
public static final Event<MessageEventListener> DEATH_MESSAGE = EventFactory.createArrayBacked(
MessageEventListener.class,
callbacks -> messageEvent -> {
for (final MessageEventListener callback : callbacks) {
callback.onMessage(messageEvent);
}
}
);
private PlayerStatusMessageEvents() {
}
public interface MessageEvent {
@Nullable Component message();
void message(@Nullable Component message);
default void disableMessage() {
this.message(null);
}
ServerPlayer player();
static MessageEvent of(final ServerPlayer player, final Component message) {
return new MessageEventImpl(player, message);
}
}
@FunctionalInterface
public interface MessageEventListener {
void onMessage(final MessageEvent event);
}
private static final class MessageEventImpl implements MessageEvent {
private final ServerPlayer player;
private @Nullable Component message;
MessageEventImpl(final ServerPlayer player, final @Nullable Component message) {
this.player = player;
this.message = message;
}
@Override
public @Nullable Component message() {
return this.message;
}
@Override
public void message(final @Nullable Component message) {
this.message = message;
}
@Override
public ServerPlayer player() {
return this.player;
}
}
}
@@ -1,83 +0,0 @@
/*
* CarbonChat
*
* Copyright (c) 2024 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.fabric.mixin;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.draycia.carbon.fabric.callback.PlayerStatusMessageEvents;
import net.kyori.adventure.platform.fabric.FabricServerAudiences;
import net.minecraft.network.Connection;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.CommonListenerCookie;
import net.minecraft.server.players.PlayerList;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.incendo.cloud.type.tuple.Pair;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(PlayerList.class)
abstract class PlayerListMixin {
@Shadow @Final private MinecraftServer server;
@Shadow
public abstract void broadcastSystemMessage(Component component, boolean bool);
@Unique private Map<Thread, Pair<Component, Boolean>> joinMsg = new ConcurrentHashMap<>();
@Redirect(
method = "placeNewPlayer",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/players/PlayerList;broadcastSystemMessage(Lnet/minecraft/network/chat/Component;Z)V"
)
)
public void redirectJoinMessage(final PlayerList instance, final Component component, final boolean bool) {
// move to after player is added to playerlist and world
this.joinMsg.put(Thread.currentThread(), Pair.of(component, bool));
}
@Inject(
method = "placeNewPlayer",
at = @At("RETURN")
)
public void injectJoin(final Connection connection, final ServerPlayer serverPlayer, final CommonListenerCookie cookie, final CallbackInfo ci) {
final @Nullable Pair<Component, Boolean> remove = this.joinMsg.remove(Thread.currentThread());
if (remove != null) {
final PlayerStatusMessageEvents.MessageEvent event = PlayerStatusMessageEvents.MessageEvent.of(
serverPlayer, remove.first().asComponent()
);
PlayerStatusMessageEvents.JOIN_MESSAGE.invoker().onMessage(event);
final net.kyori.adventure.text.@Nullable Component message = event.message();
if (message != null) {
this.broadcastSystemMessage(FabricServerAudiences.of(this.server).toNative(message), remove.second());
}
}
}
}
@@ -1,66 +0,0 @@
/*
* CarbonChat
*
* Copyright (c) 2024 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.fabric.mixin;
import net.draycia.carbon.fabric.callback.PlayerStatusMessageEvents;
import net.kyori.adventure.platform.fabric.FabricServerAudiences;
import net.minecraft.network.Connection;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ServerGamePacketListener;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.CommonListenerCookie;
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.server.players.PlayerList;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(ServerGamePacketListenerImpl.class)
abstract class ServerGamePacketListenerImplMixin extends ServerCommonPacketListenerImpl implements ServerGamePacketListener {
@Shadow public ServerPlayer player;
ServerGamePacketListenerImplMixin(final MinecraftServer minecraftServer, final Connection connection, final CommonListenerCookie commonListenerCookie) {
super(minecraftServer, connection, commonListenerCookie);
}
@Redirect(
method = "removePlayerFromWorld()V",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/players/PlayerList;broadcastSystemMessage(Lnet/minecraft/network/chat/Component;Z)V"
)
)
public void redirectQuitMessage(final PlayerList instance, final Component component, final boolean bool) {
final PlayerStatusMessageEvents.MessageEvent event = PlayerStatusMessageEvents.MessageEvent.of(
this.player, component.asComponent()
);
PlayerStatusMessageEvents.QUIT_MESSAGE.invoker().onMessage(event);
final net.kyori.adventure.text.@Nullable Component message = event.message();
if (message != null) {
instance.broadcastSystemMessage(FabricServerAudiences.of(this.server).toNative(message), bool);
}
}
}
@@ -1,95 +0,0 @@
/*
* CarbonChat
*
* Copyright (c) 2024 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.fabric.mixin;
import net.draycia.carbon.fabric.callback.PlayerStatusMessageEvents;
import net.kyori.adventure.platform.fabric.FabricServerAudiences;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.PlayerList;
import net.minecraft.world.entity.player.Player;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(ServerPlayer.class)
abstract class ServerPlayerMixin {
@Shadow @Final public MinecraftServer server;
@Redirect(
method = "die(Lnet/minecraft/world/damagesource/DamageSource;)V",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/players/PlayerList;broadcastSystemMessage(Lnet/minecraft/network/chat/Component;Z)V"
)
)
public void redirectBroadcastDeath(final PlayerList instance, final Component component, final boolean bool) {
final @Nullable Component msg = this.carbon$fireDeathMessageEvent(component);
if (msg != null) {
instance.broadcastSystemMessage(msg, bool);
}
}
@Redirect(
method = "die(Lnet/minecraft/world/damagesource/DamageSource;)V",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/players/PlayerList;broadcastSystemToTeam(Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/network/chat/Component;)V"
)
)
public void redirectBroadcastDeathToTeam(final PlayerList instance, final Player player, final Component component) {
final @Nullable Component msg = this.carbon$fireDeathMessageEvent(component);
if (msg != null) {
instance.broadcastSystemToTeam(player, msg);
}
}
@Redirect(
method = "die(Lnet/minecraft/world/damagesource/DamageSource;)V",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/players/PlayerList;broadcastSystemToAllExceptTeam(Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/network/chat/Component;)V"
)
)
public void redirectBroadcastDeathToAllExceptTeam(final PlayerList instance, final Player player, final Component component) {
final @Nullable Component msg = this.carbon$fireDeathMessageEvent(component);
if (msg != null) {
instance.broadcastSystemToAllExceptTeam(player, msg);
}
}
private @Nullable Component carbon$fireDeathMessageEvent(final Component message) {
final PlayerStatusMessageEvents.MessageEvent event = PlayerStatusMessageEvents.MessageEvent.of(
(ServerPlayer) (Object) this, message.asComponent()
);
PlayerStatusMessageEvents.DEATH_MESSAGE.invoker().onMessage(event);
final net.kyori.adventure.text.@Nullable Component msg = event.message();
if (msg == null) {
return null;
}
return FabricServerAudiences.of(this.server).toNative(msg);
}
}
@@ -36,6 +36,7 @@ import net.draycia.carbon.fabric.MinecraftServerHolder;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.platform.fabric.FabricServerAudiences;
import net.kyori.adventure.text.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.EquipmentSlot;
@@ -156,7 +157,7 @@ public class CarbonPlayerFabric extends WrappedCarbonPlayer implements Forwardin
return null;
}
return item.getDisplayName().asComponent();
return FabricServerAudiences.of(player.server).toAdventure(item.getDisplayName());
}
@Override
@@ -2,12 +2,8 @@
"required": true,
"minVersion": "0.8.4",
"package": "net.draycia.carbon.fabric.mixin",
"compatibilityLevel": "JAVA_16",
"mixins": [
"PlayerListMixin",
"ServerGamePacketListenerImplMixin",
"ServerPlayerMixin"
],
"compatibilityLevel": "JAVA_21",
"mixins": [],
"client": [
],
"injectors": {