Skip to content

Commit

Permalink
simplify testmod with commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Syst3ms committed Sep 24, 2024
1 parent 6e54721 commit d8089a8
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ServerCommandSource> buildCommandForKind(String id, String syncedWith, AttachmentType<Boolean> 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<Boolean> attachment, CommandContext<ServerCommandSource> 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;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
}))
);
});
}
}

0 comments on commit d8089a8

Please sign in to comment.