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

Allow testing protection rules without an entity event source #64

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
60 changes: 51 additions & 9 deletions src/main/java/xyz/nucleoid/leukocyte/command/ProtectCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import net.minecraft.command.argument.BlockPosArgumentType;
import net.minecraft.command.argument.DimensionArgumentType;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.GameProfileArgumentType;
import net.minecraft.entity.Entity;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.text.Texts;
import net.minecraft.text.MutableText;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.BlockPos;
import xyz.nucleoid.leukocyte.Leukocyte;
import xyz.nucleoid.leukocyte.authority.Authority;
import xyz.nucleoid.leukocyte.command.argument.AuthorityArgument;
Expand Down Expand Up @@ -120,7 +125,10 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
.executes(ProtectCommand::displayAuthority)
))
.then(literal("list").executes(ProtectCommand::listAuthorities))
.then(literal("test").executes(ProtectCommand::testRulesHere))
.then(literal("test")
.executes(ProtectCommand::testRulesAtSource)
.then(argument("entity", EntityArgumentType.entity())
.executes(ProtectCommand::testRulesForEntity)))
);
// @formatter:on
}
Expand Down Expand Up @@ -327,28 +335,62 @@ private static int listAuthorities(CommandContext<ServerCommandSource> context)
return Command.SINGLE_SUCCESS;
}

private static int testRulesHere(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
private static int testRulesAtSource(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
return testRules(context, null);
}

private static int testRulesForEntity(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var entity = EntityArgumentType.getEntity(context, "entity");
return testRules(context, entity);
}

private static int testRules(CommandContext<ServerCommandSource> context, Entity entity) throws CommandSyntaxException {
var source = context.getSource();
var player = source.getPlayer();

var world = source.getWorld();
var pos = BlockPos.ofFloored(source.getPosition());

var leukocyte = Leukocyte.get(source.getServer());

var authorities = new ArrayList<Authority>();

var eventSource = EventSource.forEntity(player);
for (var authority : leukocyte.getAuthorities()) {
if (authority.getEventFilter().accepts(eventSource)) {
authorities.add(authority);
try (var eventSource = entity == null ? EventSource.at(world, pos) : EventSource.forEntityAt(entity, pos)) {
for (var authority : leukocyte.getAuthorities()) {
if (authority.getEventFilter().accepts(eventSource)) {
authorities.add(authority);
}
}
}

if (authorities.isEmpty()) {
source.sendError(Text.literal("There are no authorities that apply to you at your current location!"));
MutableText error = Text.literal("There are no authorities that apply ");

if (entity != null) {
error = error.append("to ");
error = error.append(entity.getDisplayName());
error = error.append(ScreenTexts.SPACE);
}

error = error.append("at ");
error = error.append(Texts.bracketed(Text.translatable("chat.coordinates", pos.getX(), pos.getY(), pos.getZ())));
error = error.append("!");

source.sendError(error);
return Command.SINGLE_SUCCESS;
}

source.sendFeedback(() -> {
MutableText text = Text.literal("Testing applicable rules at your current location:\n");
MutableText text = Text.literal("Testing applicable rules ");

if (entity != null) {
text = text.append("for ");
text = text.append(entity.getDisplayName());
text = text.append(ScreenTexts.SPACE);
}

text = text.append("at ");
text = text.append(Texts.bracketed(Text.translatable("chat.coordinates", pos.getX(), pos.getY(), pos.getZ())));
text = text.append(":\n");

text = text.append(" from authorities: ");
for (int i = 0; i < authorities.size(); i++) {
Expand Down