diff --git a/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java b/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java index d7bbdd8aa6..77e45514a3 100644 --- a/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java +++ b/fabric-data-attachment-api-v1/src/testmod/java/net/fabricmc/fabric/test/attachment/AttachmentTestMod.java @@ -16,16 +16,21 @@ package net.fabricmc.fabric.test.attachment; +import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; import java.io.File; import java.io.IOException; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import com.mojang.serialization.Codec; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import net.minecraft.command.argument.EntityArgumentType; import net.minecraft.network.codec.PacketCodecs; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; @@ -171,56 +176,55 @@ public void onInitialize() { CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { dispatcher.register( literal("attachment").requires(ServerCommandSource::isExecutedByPlayer) - .then(literal("all").executes(context -> { - ServerPlayerEntity player = context.getSource().getPlayerOrThrow(); - boolean current = player.getAttachedOrElse(SYNCED_WITH_ALL, false); - player.setAttached(SYNCED_WITH_ALL, !current); - context.getSource() - .sendFeedback( - () -> Text.literal("Set flag (synced with all) to " + !current), - false - ); - return 1; - })) - .then(literal("self_only").executes(context -> { - ServerPlayerEntity player = context.getSource().getPlayerOrThrow(); - boolean current = player.getAttachedOrElse(SYNCED_WITH_TARGET, false); - player.setAttached(SYNCED_WITH_ALL, !current); - context.getSource() - .sendFeedback( - () -> Text.literal("Set flag (synced with only self) to " + !current), - false - ); - return 1; - })) - .then(literal("others_only").executes(context -> { - ServerPlayerEntity player = context.getSource().getPlayerOrThrow(); - boolean current = player.getAttachedOrElse(SYNCED_EXCEPT_TARGET, false); - player.setAttached(SYNCED_WITH_ALL, !current); - context.getSource() - .sendFeedback( - () -> Text.literal("Set flag (synced with all but self) to " + !current), - false - ); - return 1; - })) - .then(literal("custom").executes(context -> { - ServerPlayerEntity player = context.getSource().getPlayerOrThrow(); - - if (!player.isCreative()) { - throw BAD_GAMEMODE.create(); - } - - boolean current = player.getAttachedOrElse(SYNCED_CUSTOM_RULE, false); - player.setAttached(SYNCED_CUSTOM_RULE, !current); - context.getSource() - .sendFeedback( - () -> Text.literal("Set flag (synced with creative only) to " + !current), - false - ); - return 1; - })) + .then(buildCommandForKind("all", "all", SYNCED_WITH_ALL)) + .then(buildCommandForKind("self_only", "only self", SYNCED_WITH_TARGET)) + .then(buildCommandForKind("others_only", "all but self", SYNCED_EXCEPT_TARGET)) + .then(literal("custom").executes(context -> updateAttachmentFor( + checkCreative(context.getSource().getPlayerOrThrow()), + SYNCED_CUSTOM_RULE, + context, + "Set flag (synced with creative only) to %s" + )).then( + argument("target", EntityArgumentType.player()).executes(context -> updateAttachmentFor( + checkCreative(EntityArgumentType.getPlayer(context, "target")), + SYNCED_CUSTOM_RULE, + context, + "Set flag (synced with creative only) to %s" + )) + ) + ) ); }); } + + private static LiteralArgumentBuilder buildCommandForKind(String id, String syncedWith, AttachmentType type) { + return literal(id).executes(context -> updateAttachmentFor( + context.getSource().getPlayerOrThrow(), + type, + context, + "Set flag (synced with %s) to %%s".formatted(syncedWith) + )).then( + argument("target", EntityArgumentType.player()).executes(context -> updateAttachmentFor( + EntityArgumentType.getPlayer(context, "target"), + type, + context, + "Set flag (synced with %s) to %%s".formatted(syncedWith) + )) + ); + } + + private static int updateAttachmentFor(ServerPlayerEntity player, AttachmentType attachment, CommandContext context, String messageFormat) throws CommandSyntaxException { + boolean current = player.getAttachedOrElse(attachment, false); + player.setAttached(attachment, !current); + context.getSource().sendFeedback(() -> Text.literal(messageFormat.formatted(!current)), false); + return 1; + } + + private static ServerPlayerEntity checkCreative(ServerPlayerEntity player) throws CommandSyntaxException { + if (!player.isCreative()) { + throw BAD_GAMEMODE.create(); + } + + return player; + } } diff --git a/fabric-data-attachment-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/attachment/client/AttachmentDebugFeatureRenderer.java b/fabric-data-attachment-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/attachment/client/AttachmentDebugFeatureRenderer.java deleted file mode 100644 index 1281496a7a..0000000000 --- a/fabric-data-attachment-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/attachment/client/AttachmentDebugFeatureRenderer.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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.test.attachment.client; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.render.VertexConsumerProvider; -import net.minecraft.client.render.debug.DebugRenderer; -import net.minecraft.client.render.entity.feature.FeatureRenderer; -import net.minecraft.client.render.entity.feature.FeatureRendererContext; -import net.minecraft.client.render.entity.model.PlayerEntityModel; -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.util.Colors; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Position; - -import net.fabricmc.fabric.test.attachment.AttachmentTestMod; - -public class AttachmentDebugFeatureRenderer extends FeatureRenderer> { - public AttachmentDebugFeatureRenderer(FeatureRendererContext> context) { - super(context); - } - - private static void drawString(MatrixStack matrices, VertexConsumerProvider vertexConsumers, Position pos, int line, String string, int color) { - BlockPos blockPos = BlockPos.ofFloored(pos); - DebugRenderer.drawString( - matrices, - vertexConsumers, - string, - (double) blockPos.getX() + 0.5, - pos.getY() + 2.4 + (double) line * 0.25, - (double) blockPos.getZ() + 0.5, - color, - 0.02F, - false, - 0.5F, - true - ); - } - - @Override - public void render(MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, T player, float limbAngle, float limbDistance, float tickDelta, float animationProgress, float headYaw, float headPitch) { - int i = 0; - - boolean attAll = player.getAttachedOrCreate(AttachmentTestMod.SYNCED_WITH_ALL); - drawString( - matrices, - vertexConsumers, - player.getPos(), - i++, - "Synced-with-all attachment: " + attAll, - attAll ? Colors.GREEN : Colors.WHITE - ); - - boolean attTarget = player.getAttachedOrCreate(AttachmentTestMod.SYNCED_WITH_TARGET); - drawString( - matrices, - vertexConsumers, - player.getPos(), - i++, - "Synced-with-target attachment: " + attTarget, - attTarget ? player == MinecraftClient.getInstance().player ? Colors.GREEN : Colors.RED : Colors.WHITE - ); - - boolean attOther = player.getAttachedOrCreate(AttachmentTestMod.SYNCED_EXCEPT_TARGET); - drawString( - matrices, - vertexConsumers, - player.getPos(), - i++, - "Synced-with-non-targets attachment: " + attOther, - attOther ? player != MinecraftClient.getInstance().player ? Colors.GREEN : Colors.RED : Colors.WHITE - ); - - boolean attCustom = player.getAttachedOrCreate(AttachmentTestMod.SYNCED_CUSTOM_RULE); - drawString( - matrices, - vertexConsumers, - player.getPos(), - i++, - "Synced-with-creative attachment: " + attCustom, - attCustom ? player.isCreative() ? Colors.GREEN : Colors.RED : Colors.WHITE - ); - } -} diff --git a/fabric-data-attachment-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/attachment/client/AttachmentTestModClient.java b/fabric-data-attachment-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/attachment/client/AttachmentTestModClient.java index d2ce4bf08d..1f4dd48c90 100644 --- a/fabric-data-attachment-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/attachment/client/AttachmentTestModClient.java +++ b/fabric-data-attachment-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/attachment/client/AttachmentTestModClient.java @@ -16,28 +16,77 @@ package net.fabricmc.fabric.test.attachment.client; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; + +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.exceptions.CommandSyntaxException; + import net.minecraft.client.MinecraftClient; -import net.minecraft.client.render.entity.PlayerEntityRenderer; +import net.minecraft.client.network.AbstractClientPlayerEntity; +import net.minecraft.command.argument.EntityArgumentType; +import net.minecraft.text.Text; +import net.minecraft.util.Colors; import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; -import net.fabricmc.fabric.api.client.rendering.v1.LivingEntityFeatureRendererRegistrationCallback; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.fabricmc.fabric.test.attachment.AttachmentTestMod; public class AttachmentTestModClient implements ClientModInitializer { - @Override - public void onInitializeClient() { - // Various test renderers to display attachments clientside - HudRenderCallback.EVENT.register((drawContext, tickCounter) -> { - if (MinecraftClient.getInstance().player.getAttachedOrCreate(AttachmentTestMod.SYNCED_WITH_TARGET)) { - drawContext.fillGradient(10, 10, 60, 60, 0xFFFF0000, 0xFF0000FF); + private static AbstractClientPlayerEntity parseClientPlayer(FabricClientCommandSource source, String name) throws CommandSyntaxException { + if (name.equals("@s")) { + return source.getPlayer(); + } else { + for (AbstractClientPlayerEntity player : source.getWorld().getPlayers()) { + if (name.equals(player.getName().getLiteralString())) { + return player; + } } - }); - LivingEntityFeatureRendererRegistrationCallback.EVENT.register((entityType, entityRenderer, registrationHelper, context) -> { - if (entityRenderer instanceof PlayerEntityRenderer playerRenderer) { - registrationHelper.register(new AttachmentDebugFeatureRenderer<>(playerRenderer)); - } + throw EntityArgumentType.PLAYER_NOT_FOUND_EXCEPTION.create(); + } + } + + @Override + public void onInitializeClient() { + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> { + dispatcher.register(literal("attachment_test").then(argument("target", StringArgumentType.word()).executes( + context -> { + AbstractClientPlayerEntity player = parseClientPlayer( + context.getSource(), + StringArgumentType.getString(context, "target") + ); + context.getSource().sendFeedback( + Text.literal("Attachments for player %s:".formatted(player.getName().getLiteralString())) + ); + boolean attAll = player.getAttachedOrCreate(AttachmentTestMod.SYNCED_WITH_ALL); + context.getSource().sendFeedback( + Text.literal("Synced-with-all attachment: %s".formatted(attAll)).withColor( + attAll ? Colors.GREEN : Colors.WHITE + ) + ); + boolean attTarget = player.getAttachedOrCreate(AttachmentTestMod.SYNCED_WITH_TARGET); + context.getSource().sendFeedback( + Text.literal("Synced-with-target attachment: %s".formatted(attTarget)).withColor( + attTarget ? player == MinecraftClient.getInstance().player ? Colors.GREEN : Colors.RED : Colors.WHITE + ) + ); + boolean attOther = player.getAttachedOrCreate(AttachmentTestMod.SYNCED_EXCEPT_TARGET); + context.getSource().sendFeedback( + Text.literal("Synced-with-non-targets attachment: %s".formatted(attOther)).withColor( + attOther ? player != MinecraftClient.getInstance().player ? Colors.GREEN : Colors.RED : Colors.WHITE + ) + ); + boolean attCustom = player.getAttachedOrCreate(AttachmentTestMod.SYNCED_CUSTOM_RULE); + context.getSource().sendFeedback( + Text.literal("Synced-with-creative attachment: %s".formatted(attCustom)).withColor( + attCustom ? player.isCreative() ? Colors.GREEN : Colors.RED : Colors.WHITE + ) + ); + return 1; + })) + ); }); } }