From d32ca96239dd9346d486dcb1f0a804dbbc37bc5e Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 5 Jun 2021 09:06:39 +0800 Subject: add time range filter; fix lookup filters --- src/main/java/com/keuin/blame/Blame.java | 13 ++++- .../com/keuin/blame/command/BlameBlockCommand.java | 56 ++++++++++++++++++++-- .../keuin/blame/lookup/AbstractLookupFilter.java | 12 ++--- .../keuin/blame/lookup/BlockPosLookupFilter.java | 11 ++--- .../java/com/keuin/blame/lookup/DummyFilter.java | 11 +++-- .../java/com/keuin/blame/lookup/LookupFilters.java | 20 ++++++++ .../java/com/keuin/blame/lookup/LookupWorker.java | 9 ++-- .../com/keuin/blame/lookup/TimeLookupFilter.java | 20 ++++++++ 8 files changed, 124 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/keuin/blame/lookup/LookupFilters.java create mode 100644 src/main/java/com/keuin/blame/lookup/TimeLookupFilter.java (limited to 'src') diff --git a/src/main/java/com/keuin/blame/Blame.java b/src/main/java/com/keuin/blame/Blame.java index 380807f..389c14c 100644 --- a/src/main/java/com/keuin/blame/Blame.java +++ b/src/main/java/com/keuin/blame/Blame.java @@ -11,10 +11,13 @@ import com.keuin.blame.lookup.LookupManager; import com.keuin.blame.util.DatabaseUtil; import com.keuin.blame.util.PrintUtil; import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.arguments.LongArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.player.*; +import net.minecraft.command.CommandSource; import net.minecraft.command.argument.BlockPosArgumentType; import net.minecraft.command.argument.DimensionArgumentType; import net.minecraft.server.command.CommandManager; @@ -91,7 +94,15 @@ public class Blame implements ModInitializer { .then(CommandManager.argument("pos", BlockPosArgumentType.blockPos()) .executes(BlameBlockCommand::blameGivenBlockPos) .then(CommandManager.argument("world", DimensionArgumentType.dimension()) - .executes(BlameBlockCommand::blameGivenBlockPos)))) + .executes(BlameBlockCommand::blameGivenBlockPos) + .then(CommandManager.literal("last") + .then(CommandManager.argument("time_range", LongArgumentType.longArg()) + .executes(BlameBlockCommand::blameGivenBlockPos) + .then(CommandManager.argument("time_unit", StringArgumentType.word()) + .suggests((ctx, builder) -> CommandSource.suggestMatching(BlameBlockCommand.timeUnits, builder)) + .executes(BlameBlockCommand::blameGivenBlockPos)) + )) + ))) ); commandDispatcher.register( CommandManager.literal("blame").then(CommandManager.literal("limit") diff --git a/src/main/java/com/keuin/blame/command/BlameBlockCommand.java b/src/main/java/com/keuin/blame/command/BlameBlockCommand.java index 0cd70d8..eb1888d 100644 --- a/src/main/java/com/keuin/blame/command/BlameBlockCommand.java +++ b/src/main/java/com/keuin/blame/command/BlameBlockCommand.java @@ -2,12 +2,12 @@ package com.keuin.blame.command; import com.keuin.blame.data.WorldPos; import com.keuin.blame.data.entry.LogEntry; -import com.keuin.blame.lookup.BlockPosLookupFilter; -import com.keuin.blame.lookup.LookupCallback; -import com.keuin.blame.lookup.LookupManager; +import com.keuin.blame.lookup.*; import com.keuin.blame.util.MinecraftUtil; import com.keuin.blame.util.PrettyUtil; import com.keuin.blame.util.PrintUtil; +import com.mojang.brigadier.arguments.LongArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.minecraft.command.argument.BlockPosArgumentType; @@ -18,12 +18,26 @@ import net.minecraft.util.Formatting; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + import static com.keuin.blame.command.Commands.FAILED; import static com.keuin.blame.command.Commands.SUCCESS; @SuppressWarnings("SameReturnValue") public class BlameBlockCommand { + public static final Map timeUnitAmplifierMap = Collections + .unmodifiableMap(new HashMap() {{ + put("second", 1); + put("minute", 60); + put("hour", 3600); + put("day", 86400); + }}); + public static final Set timeUnits = timeUnitAmplifierMap.keySet(); + public static int blameGivenBlockPos(CommandContext context) throws CommandSyntaxException { // pos // world @@ -45,13 +59,47 @@ public class BlameBlockCommand { } catch (IllegalArgumentException e) { world = MinecraftUtil.worldToString(entity.world); } + + long timeRange; + try { + timeRange = LongArgumentType.getLong(context, "time_range"); + if (timeRange < 0) + return FAILED; + } catch (IllegalArgumentException e) { + timeRange = -1; + } + + long amplifier; + try { + final String timeUnit = StringArgumentType.getString(context, "time_unit"); + amplifier = timeUnitAmplifierMap.getOrDefault(timeUnit, -1); + if (amplifier < 0) + return FAILED; + } catch (IllegalArgumentException e) { + amplifier = 1; + } + + if (timeRange >= 0) { + timeRange *= amplifier; + if (timeRange < 0) + return FAILED; + } + // String world = MinecraftUtil.worldToString(playerEntity.world); WorldPos blockPos = new WorldPos(world, x, y, z); + AbstractLookupFilter filter; + if (timeRange >= 0) { + filter = LookupFilters.compoundedFilter(new TimeLookupFilter(timeRange), new BlockPosLookupFilter(blockPos)); + } else { + filter = new BlockPosLookupFilter(blockPos); + } + LookupManager.INSTANCE.lookup( - new BlockPosLookupFilter(blockPos), + filter, new Callback(context), BlameLimitCommand.getLookupLimit(playerEntity.getUuid()) ); + return SUCCESS; } diff --git a/src/main/java/com/keuin/blame/lookup/AbstractLookupFilter.java b/src/main/java/com/keuin/blame/lookup/AbstractLookupFilter.java index 4c18442..f2e45dc 100644 --- a/src/main/java/com/keuin/blame/lookup/AbstractLookupFilter.java +++ b/src/main/java/com/keuin/blame/lookup/AbstractLookupFilter.java @@ -1,13 +1,7 @@ package com.keuin.blame.lookup; -import com.keuin.blame.data.entry.LogEntry; -import com.mongodb.client.FindIterable; +import org.bson.conversions.Bson; -public abstract class AbstractLookupFilter { - // immutable - - AbstractLookupFilter() { - } - - abstract FindIterable find(FindIterable iterable); +public interface AbstractLookupFilter { + abstract Bson filter(); } \ No newline at end of file diff --git a/src/main/java/com/keuin/blame/lookup/BlockPosLookupFilter.java b/src/main/java/com/keuin/blame/lookup/BlockPosLookupFilter.java index d7cec43..7b667bb 100644 --- a/src/main/java/com/keuin/blame/lookup/BlockPosLookupFilter.java +++ b/src/main/java/com/keuin/blame/lookup/BlockPosLookupFilter.java @@ -1,12 +1,11 @@ package com.keuin.blame.lookup; import com.keuin.blame.data.WorldPos; -import com.keuin.blame.data.entry.LogEntry; import com.keuin.blame.data.entry.LogEntryNames; -import com.mongodb.client.FindIterable; import com.mongodb.client.model.Filters; +import org.bson.conversions.Bson; -public class BlockPosLookupFilter extends AbstractLookupFilter { +public class BlockPosLookupFilter implements AbstractLookupFilter { private final WorldPos blockPos; public BlockPosLookupFilter(WorldPos blockPos) { @@ -14,10 +13,10 @@ public class BlockPosLookupFilter extends AbstractLookupFilter { } @Override - FindIterable find(FindIterable iterable) { - return iterable.filter(Filters.and( + public Bson filter() { + return Filters.and( Filters.eq(LogEntryNames.VERSION, 1), Filters.eq(LogEntryNames.OBJECT_POS, blockPos) - )); + ); } } diff --git a/src/main/java/com/keuin/blame/lookup/DummyFilter.java b/src/main/java/com/keuin/blame/lookup/DummyFilter.java index 0932506..55b0283 100644 --- a/src/main/java/com/keuin/blame/lookup/DummyFilter.java +++ b/src/main/java/com/keuin/blame/lookup/DummyFilter.java @@ -1,11 +1,12 @@ package com.keuin.blame.lookup; -import com.keuin.blame.data.entry.LogEntry; -import com.mongodb.client.FindIterable; +import org.bson.BsonDocument; +import org.bson.conversions.Bson; + +public class DummyFilter implements AbstractLookupFilter { -public class DummyFilter extends AbstractLookupFilter { @Override - protected FindIterable find(FindIterable iterable) { - return iterable; + public Bson filter() { + return new BsonDocument(); } } diff --git a/src/main/java/com/keuin/blame/lookup/LookupFilters.java b/src/main/java/com/keuin/blame/lookup/LookupFilters.java new file mode 100644 index 0000000..2f24138 --- /dev/null +++ b/src/main/java/com/keuin/blame/lookup/LookupFilters.java @@ -0,0 +1,20 @@ +package com.keuin.blame.lookup; + +import com.mongodb.client.model.Filters; +import org.bson.conversions.Bson; + +import java.util.ArrayList; +import java.util.List; + +public class LookupFilters { + public static AbstractLookupFilter compoundedFilter(AbstractLookupFilter... filters) { + return () -> { + List list = new ArrayList<>(); + for (AbstractLookupFilter filter : filters) { + Bson bson = filter.filter(); + list.add(bson); + } + return Filters.and(list); + }; + } +} diff --git a/src/main/java/com/keuin/blame/lookup/LookupWorker.java b/src/main/java/com/keuin/blame/lookup/LookupWorker.java index e353e99..d9425b6 100644 --- a/src/main/java/com/keuin/blame/lookup/LookupWorker.java +++ b/src/main/java/com/keuin/blame/lookup/LookupWorker.java @@ -1,6 +1,7 @@ package com.keuin.blame.lookup; import com.keuin.blame.data.entry.LogEntry; +import com.keuin.blame.data.entry.LogEntryNames; import com.keuin.blame.util.DatabaseUtil; import com.mongodb.client.*; import com.mongodb.client.model.Sorts; @@ -42,9 +43,11 @@ public class LookupWorker extends Thread { AbstractLookupFilter filter = item.getFilter(); time = System.currentTimeMillis(); - FindIterable find = filter.find( - collection.find().sort(Sorts.descending("timestamp_millis")).limit(item.getLimit()) - ); + FindIterable find = collection + .find() + .filter(filter.filter()) + .sort(Sorts.descending(LogEntryNames.TIMESTAMP_MILLIS)) + .limit(item.getLimit()); // FindIterable find = collection.find();//.sort(Sorts.descending("timestamp_millis")); time = System.currentTimeMillis() - time; logger.info(String.format("Lookup finished in %d ms.", time)); diff --git a/src/main/java/com/keuin/blame/lookup/TimeLookupFilter.java b/src/main/java/com/keuin/blame/lookup/TimeLookupFilter.java new file mode 100644 index 0000000..4d891c7 --- /dev/null +++ b/src/main/java/com/keuin/blame/lookup/TimeLookupFilter.java @@ -0,0 +1,20 @@ +package com.keuin.blame.lookup; + +import com.keuin.blame.data.entry.LogEntryNames; +import com.mongodb.client.model.Filters; +import org.bson.conversions.Bson; + +public class TimeLookupFilter implements AbstractLookupFilter { + private final long seconds; + + public TimeLookupFilter(long seconds) { + this.seconds = seconds; + } + + @Override + public Bson filter() { + return Filters.gte( + LogEntryNames.TIMESTAMP_MILLIS, + System.currentTimeMillis() - seconds * 1000); + } +} -- cgit v1.2.3