From 01445b4c92eef7bd2c32f65189eed2f00d4b4767 Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 24 Dec 2020 21:59:23 +0800 Subject: keep up with main stream (1.1.3-beta) --- .gitignore | 2 +- gradle.properties | 8 ++++---- src/main/java/com/keuin/blame/data/entry/LogEntry.java | 2 +- src/main/java/com/keuin/blame/util/MinecraftUtil.java | 5 ++++- src/main/java/com/keuin/blame/util/PrintUtil.java | 18 ++++++++---------- src/main/resources/fabric.mod.json | 2 +- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 5faee2e..f1b1c57 100644 --- a/.gitignore +++ b/.gitignore @@ -31,5 +31,5 @@ bin/ # fabric run/ -run.1152 +run.1152/ logs/ \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index dd908cf..0c58fb8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,13 +2,13 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties # check these on https://fabricmc.net/use -minecraft_version=1.16.4 -yarn_mappings=1.16.4+build.6 -loader_version=0.10.6+build.214 +minecraft_version=1.15.2 +yarn_mappings=1.15.2+build.17 +loader_version=0.10.8 # Mod Properties mod_version=1.1.3-beta maven_group=com.keuin.blame archives_base_name=blame-fabric # Dependencies # currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api -fabric_version=0.25.1+build.416-1.16 +fabric_version=0.28.4+1.15 diff --git a/src/main/java/com/keuin/blame/data/entry/LogEntry.java b/src/main/java/com/keuin/blame/data/entry/LogEntry.java index 090d77c..e47cc92 100644 --- a/src/main/java/com/keuin/blame/data/entry/LogEntry.java +++ b/src/main/java/com/keuin/blame/data/entry/LogEntry.java @@ -44,7 +44,7 @@ public class LogEntry { public int version = 1; @BsonProperty(GAME_VERSION) - public String gameVersion = MinecraftVersion.field_25319.getName(); + public String gameVersion = (new MinecraftVersion()).getName(); @BsonProperty(TIMESTAMP_MILLIS) public long timeMillis = 0; diff --git a/src/main/java/com/keuin/blame/util/MinecraftUtil.java b/src/main/java/com/keuin/blame/util/MinecraftUtil.java index ce8707e..4cb7e2b 100644 --- a/src/main/java/com/keuin/blame/util/MinecraftUtil.java +++ b/src/main/java/com/keuin/blame/util/MinecraftUtil.java @@ -1,11 +1,14 @@ package com.keuin.blame.util; import net.minecraft.world.World; +import net.minecraft.world.dimension.DimensionType; + +import java.util.Objects; public class MinecraftUtil { public static String worldToString(World world) { - return world.getRegistryKey().getValue().toString(); + return Objects.requireNonNull(DimensionType.getId(world.dimension.getType())).toString(); } } diff --git a/src/main/java/com/keuin/blame/util/PrintUtil.java b/src/main/java/com/keuin/blame/util/PrintUtil.java index 75b73d4..07985f9 100644 --- a/src/main/java/com/keuin/blame/util/PrintUtil.java +++ b/src/main/java/com/keuin/blame/util/PrintUtil.java @@ -2,7 +2,6 @@ package com.keuin.blame.util; import com.mojang.brigadier.context.CommandContext; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; -import net.minecraft.network.MessageType; import net.minecraft.server.MinecraftServer; import net.minecraft.server.PlayerManager; import net.minecraft.server.command.ServerCommandSource; @@ -12,19 +11,19 @@ import net.minecraft.util.Formatting; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import static com.keuin.blame.util.UuidUtils.UUID_NULL; - public final class PrintUtil implements ServerLifecycleEvents.ServerStarted { + // version: 1.2 (for 1.15.2) + private static final Object syncMessage = new Object(); private static final Object syncBroadcast = new Object(); - private static final Style broadcastStyle = Style.EMPTY.withColor(Formatting.AQUA); - private static final Style infoStyle = Style.EMPTY.withColor(Formatting.WHITE); - private static final Style stressStyle = Style.EMPTY.withColor(Formatting.AQUA); - private static final Style warnStyle = Style.EMPTY.withColor(Formatting.YELLOW); - private static final Style errorStyle = Style.EMPTY.withColor(Formatting.DARK_RED); + private static final Style broadcastStyle = new Style().setColor(Formatting.AQUA); + private static final Style infoStyle = new Style().setColor(Formatting.WHITE); + private static final Style stressStyle = new Style().setColor(Formatting.AQUA); + private static final Style warnStyle = new Style().setColor(Formatting.YELLOW); + private static final Style errorStyle = new Style().setColor(Formatting.DARK_RED); private static final Logger LOGGER = LogManager.getLogger(); private static final String LOG_HEADING = "[Blame]"; @@ -52,8 +51,7 @@ public final class PrintUtil implements ServerLifecycleEvents.ServerStarted { if (playerManager != null) playerManager.broadcastChatMessage( new LiteralText(message).setStyle(broadcastStyle), - MessageType.SYSTEM, - UUID_NULL + false ); } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index f4d8e92..aa17a7b 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -25,7 +25,7 @@ "depends": { "fabricloader": ">=0.7.4", "fabric": "*", - "minecraft": "1.16.x" + "minecraft": "1.15.x" }, "suggests": { "another-mod": "*" -- cgit v1.2.3 From 623ebbc3f65ec82bcec66de0f213bbe39065a25a Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 25 Dec 2020 15:54:30 +0800 Subject: check dupe when player uses items --- src/main/java/com/keuin/blame/EventHandler.java | 36 ++++++++++++++----------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/keuin/blame/EventHandler.java b/src/main/java/com/keuin/blame/EventHandler.java index 4a28a84..2a68b39 100644 --- a/src/main/java/com/keuin/blame/EventHandler.java +++ b/src/main/java/com/keuin/blame/EventHandler.java @@ -23,7 +23,7 @@ public class EventHandler implements AttackEntityHandler, PlaceBlockHandler, Bre public static final EventHandler INSTANCE = new EventHandler(); - private static final long MAX_DUPE_INTERVAL = 80; // (判定为重复的必要条件)最大间隔毫秒数 + private static final long MAX_DUPE_INTERVAL = 100; // (判定为重复的必要条件)最大间隔毫秒数 private LogEntry lastUseBlockEntry; //去重用 private LogEntry lastUseEntityEntry; @@ -45,7 +45,6 @@ public class EventHandler implements AttackEntityHandler, PlaceBlockHandler, Bre checkDuplication = true; } } - // 不检查重复,直接提交 String worldString = MinecraftUtil.worldToString(world); String blockId = Registry.BLOCK.getId(world.getBlockState(blockHitResult.getBlockPos()).getBlock()).toString(); LogEntry entry = LogEntryFactory.playerWithBlock( @@ -64,9 +63,7 @@ public class EventHandler implements AttackEntityHandler, PlaceBlockHandler, Bre } lastUseBlockEntry = entry; SubmitWorker.INSTANCE.submit(entry); -// PrintUtil.broadcast("use_block; block_id=" + blockId + "; world=" + worldString); // TODO: 增加判断,事件触发的时候用户不一定真正使用了方块(也可能是无效的动作)。放置方块的时候也会触发这个事件 -// PrintUtil.broadcast(String.format("player %s use block %s", playerEntity.getName().getString(), world.getBlockState(blockHitResult.getBlockPos()))); } @Override @@ -82,8 +79,6 @@ public class EventHandler implements AttackEntityHandler, PlaceBlockHandler, Bre ActionType.BLOCK_PLACE ); SubmitWorker.INSTANCE.submit(entry); -// PrintUtil.broadcast(String.format("place_block; world=%s, player_entity=%s, block_pos=%s, block_state=%s, block_entity=%s", -// world, playerEntity, blockPos, blockState, blockEntity)); } @Override @@ -99,8 +94,6 @@ public class EventHandler implements AttackEntityHandler, PlaceBlockHandler, Bre ActionType.BLOCK_BREAK ); SubmitWorker.INSTANCE.submit(entry); -// PrintUtil.broadcast("break_block; block_id=" + blockId + "; world=" + worldString); -// PrintUtil.broadcast(String.format("player %s break block %s", playerEntity.getName().getString(), blockState)); } @Override @@ -116,8 +109,6 @@ public class EventHandler implements AttackEntityHandler, PlaceBlockHandler, Bre ActionType.ENTITY_ATTACK ); SubmitWorker.INSTANCE.submit(entry); -// PrintUtil.broadcast("attack_entity; entity_id=" + entityId); -// PrintUtil.broadcast(String.format("player %s attack entity %s", playerEntity.getName().getString(), entity)); } @Override @@ -155,14 +146,22 @@ public class EventHandler implements AttackEntityHandler, PlaceBlockHandler, Bre } lastUseEntityEntry = entry; SubmitWorker.INSTANCE.submit(entry); -// PrintUtil.broadcast("use_entity; entity_id=" + entityId); // TODO: 增加判断,无效的时候也会触发这个事件 - // TODO: 增加cooldown,过滤掉两个相邻重复事件(时间间隔大概为20ms+) -// PrintUtil.broadcast(String.format("player %s use entity %s", playerEntity.getName().getString(), entity)); } @Override public void onPlayerUseItem(PlayerEntity playerEntity, World world, Hand hand) { + boolean checkDuplication = false; + long ts = System.currentTimeMillis(); + if (lastUseItemEntry != null && (ts - lastUseItemEntry.timeMillis) <= MAX_DUPE_INTERVAL) { + if ( + lastUseItemEntry.subjectPos.getX() == playerEntity.getX() + && lastUseItemEntry.subjectPos.getY() == playerEntity.getY() + && lastUseItemEntry.subjectPos.getZ() == playerEntity.getZ() // 快速判断,减小不相同导致的资源开销 + ) { + checkDuplication = true; + } + } String itemId = Registry.ITEM.getId(playerEntity.getStackInHand(hand).getItem()).toString(); LogEntry entry = LogEntryFactory.playerWithItem( playerEntity, @@ -170,10 +169,15 @@ public class EventHandler implements AttackEntityHandler, PlaceBlockHandler, Bre itemId, ActionType.ITEM_USE ); + if (checkDuplication) { + LogEntry alignedEntry = new LogEntry(lastUseItemEntry); + alignedEntry.timeMillis = entry.timeMillis; + if (Objects.equals(alignedEntry, entry)) { + return; + } + } + lastUseItemEntry = entry; SubmitWorker.INSTANCE.submit(entry); -// PrintUtil.broadcast("use_item; item_id=" + itemId); - // TODO: 增加cooldown,过滤掉两个相邻重复事件(时间间隔大概为20ms+) -// PrintUtil.broadcast(String.format("player %s use item %s", playerEntity.getName().getString(), playerEntity.getStackInHand(hand))); } } -- cgit v1.2.3 From 9de4839c30ca750026b6fbed235a0300c82f67a8 Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 25 Dec 2020 16:22:47 +0800 Subject: Add `/blame limit ` command to change the lookup limit. Improve the implementation of look up limit. --- gradle.properties | 2 +- src/main/java/com/keuin/blame/Blame.java | 29 +++++------ .../com/keuin/blame/command/BlameBlockCommand.java | 22 ++++----- .../com/keuin/blame/command/BlameLimitCommand.java | 56 ++++++++++++++++++++++ .../java/com/keuin/blame/command/Commands.java | 6 +++ .../blame/lookup/LookupFilterWithCallback.java | 16 +++++-- .../java/com/keuin/blame/lookup/LookupManager.java | 6 ++- .../java/com/keuin/blame/lookup/LookupWorker.java | 2 +- .../java/com/keuin/blame/test/TestDatabase.java | 2 +- 9 files changed, 102 insertions(+), 39 deletions(-) create mode 100644 src/main/java/com/keuin/blame/command/BlameLimitCommand.java create mode 100644 src/main/java/com/keuin/blame/command/Commands.java diff --git a/gradle.properties b/gradle.properties index 0c58fb8..2527577 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.15.2 yarn_mappings=1.15.2+build.17 loader_version=0.10.8 # Mod Properties -mod_version=1.1.3-beta +mod_version=1.2.0-alpha maven_group=com.keuin.blame archives_base_name=blame-fabric # Dependencies diff --git a/src/main/java/com/keuin/blame/Blame.java b/src/main/java/com/keuin/blame/Blame.java index b09f23d..ab22590 100644 --- a/src/main/java/com/keuin/blame/Blame.java +++ b/src/main/java/com/keuin/blame/Blame.java @@ -4,6 +4,7 @@ import com.google.gson.Gson; import com.keuin.blame.adapter.*; import com.keuin.blame.adapter.handler.PlaceBlockHandler; import com.keuin.blame.command.BlameBlockCommand; +import com.keuin.blame.command.BlameLimitCommand; import com.keuin.blame.config.BlameConfig; import com.keuin.blame.lookup.LookupManager; import com.keuin.blame.util.PrintUtil; @@ -96,24 +97,16 @@ public class Blame implements ModInitializer { @Override public void register(CommandDispatcher commandDispatcher, boolean b) { commandDispatcher.register( - CommandManager.literal("blame") - .then( - CommandManager.literal("block") - .then( - CommandManager.argument("x", IntegerArgumentType.integer()) - .then( - CommandManager.argument("y", IntegerArgumentType.integer()) - .then( - CommandManager.argument("z", IntegerArgumentType.integer()) - .then( - CommandManager.argument("world", StringArgumentType.greedyString()) - .executes(BlameBlockCommand::run) - ) - ) - ) - ) - ) - ); + CommandManager.literal("blame").then(CommandManager.literal("block") + .then(CommandManager.argument("x", IntegerArgumentType.integer()) + .then(CommandManager.argument("y", IntegerArgumentType.integer()) + .then(CommandManager.argument("z", IntegerArgumentType.integer()) + .then(CommandManager.argument("world", StringArgumentType.greedyString()) + .executes(BlameBlockCommand::blameBlock))))))); + commandDispatcher.register( + CommandManager.literal("blame").then(CommandManager.literal("limit") + .then(CommandManager.argument("limit", IntegerArgumentType.integer(1, 255)) + .executes(BlameLimitCommand::setLimit)))); } }); } diff --git a/src/main/java/com/keuin/blame/command/BlameBlockCommand.java b/src/main/java/com/keuin/blame/command/BlameBlockCommand.java index 7d85e77..578870d 100644 --- a/src/main/java/com/keuin/blame/command/BlameBlockCommand.java +++ b/src/main/java/com/keuin/blame/command/BlameBlockCommand.java @@ -11,14 +11,13 @@ import net.minecraft.entity.Entity; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; -import java.util.Iterator; +import static com.keuin.blame.command.Commands.FAILED; +import static com.keuin.blame.command.Commands.SUCCESS; public class BlameBlockCommand { - private static final int SUCCESS = 1; - private static final int FAILED = -1; - public static int run(CommandContext context) { + public static int blameBlock(CommandContext context) { Entity entity = context.getSource().getEntity(); if (!(entity instanceof ServerPlayerEntity)) { // can only be executed by player @@ -34,7 +33,8 @@ public class BlameBlockCommand { WorldPos blockPos = new WorldPos(world, x, y, z); LookupManager.INSTANCE.lookup( new BlockPosLookupFilter(blockPos), - new Callback(context) + new Callback(context), + BlameLimitCommand.getLookupLimit(playerEntity.getUuid()) ); return SUCCESS; } @@ -50,18 +50,16 @@ public class BlameBlockCommand { @Override public void onLookupFinishes(Iterable logEntries) { StringBuilder printBuilder = new StringBuilder(); - Iterator iterator = logEntries.iterator(); - int printCount; - for (printCount = 0; printCount < 5; ++printCount) { - if (!iterator.hasNext()) - break; - LogEntry logEntry = iterator.next(); + int printCount = 0; + for (LogEntry logEntry : logEntries) { printBuilder.append(logEntry.toString()); printBuilder.append("\n") .append("================") .append("\n"); + ++printCount; } - printBuilder.append(String.format("Displayed the most recent %d items.", printCount)); + printBuilder.append(String.format("Displayed the most recent %d items. " + + "Use `/blame limit` to change your display limit.", printCount)); PrintUtil.msgInfo(context, printBuilder.toString()); } } diff --git a/src/main/java/com/keuin/blame/command/BlameLimitCommand.java b/src/main/java/com/keuin/blame/command/BlameLimitCommand.java new file mode 100644 index 0000000..a31a2f9 --- /dev/null +++ b/src/main/java/com/keuin/blame/command/BlameLimitCommand.java @@ -0,0 +1,56 @@ +package com.keuin.blame.command; + +import com.keuin.blame.util.PrintUtil; +import com.mojang.brigadier.context.CommandContext; +import net.minecraft.entity.Entity; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; + +import java.util.Map; +import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + +import static com.keuin.blame.command.Commands.FAILED; +import static com.keuin.blame.command.Commands.SUCCESS; + +public class BlameLimitCommand { + + private static final Map maxLookupCount = new ConcurrentHashMap<>(); + private static final int DEFAULT_LOOKUP_LIMIT = 5; + + public static int setLimit(CommandContext context) { + Entity entity = context.getSource().getEntity(); + if (!(entity instanceof ServerPlayerEntity)) { + // can only be executed by player + return FAILED; + } + ServerPlayerEntity playerEntity = (ServerPlayerEntity) entity; + int newLimit = context.getArgument("limit", Integer.class); + int previousLimit = setLookupLimit(playerEntity.getUuid(), newLimit); + PrintUtil.msgInfo(context, String.format("Set your lookup limit to %d (%d previously).", newLimit, previousLimit)); + return SUCCESS; + } + + /** + * Set one player's lookup limit. + * + * @param playerUUID the player's uuid. + * @param newLookupLimit the new limit. Must be positive. + * @return the previous (or default if not set) limit. + */ + private static int setLookupLimit(UUID playerUUID, int newLookupLimit) { + return Optional.ofNullable(maxLookupCount.put(playerUUID, newLookupLimit)).orElse(DEFAULT_LOOKUP_LIMIT); + } + + /** + * Get one player's lookup limit. + * + * @param playerUUID the player's uuid. + * @return the limit. + */ + public static int getLookupLimit(UUID playerUUID) { + return maxLookupCount.getOrDefault(playerUUID, DEFAULT_LOOKUP_LIMIT); + } + +} diff --git a/src/main/java/com/keuin/blame/command/Commands.java b/src/main/java/com/keuin/blame/command/Commands.java new file mode 100644 index 0000000..8d15ccc --- /dev/null +++ b/src/main/java/com/keuin/blame/command/Commands.java @@ -0,0 +1,6 @@ +package com.keuin.blame.command; + +public class Commands { + public static final int SUCCESS = 1; + public static final int FAILED = -1; +} diff --git a/src/main/java/com/keuin/blame/lookup/LookupFilterWithCallback.java b/src/main/java/com/keuin/blame/lookup/LookupFilterWithCallback.java index cd6743c..f2b07cb 100644 --- a/src/main/java/com/keuin/blame/lookup/LookupFilterWithCallback.java +++ b/src/main/java/com/keuin/blame/lookup/LookupFilterWithCallback.java @@ -6,14 +6,18 @@ class LookupFilterWithCallback { private final LookupCallback callback; private final AbstractLookupFilter filter; + private final int limit; - LookupFilterWithCallback(LookupCallback callback, AbstractLookupFilter filter) { + LookupFilterWithCallback(LookupCallback callback, AbstractLookupFilter filter, int limit) { if (callback == null) throw new IllegalArgumentException("callback cannot be null"); if (filter == null) throw new IllegalArgumentException("filter cannot be null"); + if (limit <= 0) + throw new IllegalArgumentException("limit must be positive"); this.callback = callback; this.filter = filter; + this.limit = limit; } public LookupCallback getCallback() { @@ -24,18 +28,22 @@ class LookupFilterWithCallback { return filter; } + public int getLimit() { + return limit; + } + @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; LookupFilterWithCallback that = (LookupFilterWithCallback) o; - return callback.equals(that.callback) && + return limit == that.limit && + callback.equals(that.callback) && filter.equals(that.filter); } @Override public int hashCode() { - return Objects.hash(callback, filter); + return Objects.hash(callback, filter, limit); } - } diff --git a/src/main/java/com/keuin/blame/lookup/LookupManager.java b/src/main/java/com/keuin/blame/lookup/LookupManager.java index 2ffa86b..5574dc5 100644 --- a/src/main/java/com/keuin/blame/lookup/LookupManager.java +++ b/src/main/java/com/keuin/blame/lookup/LookupManager.java @@ -25,8 +25,10 @@ public class LookupManager { workers.forEach(LookupWorker::disable); } - public void lookup(AbstractLookupFilter filter, LookupCallback callback) { - queue.add(new LookupFilterWithCallback(callback, filter)); + public void lookup(AbstractLookupFilter filter, LookupCallback callback, int lookUpLimit) { + if (lookUpLimit <= 0) + throw new IllegalArgumentException("lookup limit must be positive"); + queue.add(new LookupFilterWithCallback(callback, filter, lookUpLimit)); } } diff --git a/src/main/java/com/keuin/blame/lookup/LookupWorker.java b/src/main/java/com/keuin/blame/lookup/LookupWorker.java index 1442615..e353e99 100644 --- a/src/main/java/com/keuin/blame/lookup/LookupWorker.java +++ b/src/main/java/com/keuin/blame/lookup/LookupWorker.java @@ -43,7 +43,7 @@ public class LookupWorker extends Thread { time = System.currentTimeMillis(); FindIterable find = filter.find( - collection.find().sort(Sorts.descending("timestamp_millis")) + collection.find().sort(Sorts.descending("timestamp_millis")).limit(item.getLimit()) ); // FindIterable find = collection.find();//.sort(Sorts.descending("timestamp_millis")); time = System.currentTimeMillis() - time; diff --git a/src/main/java/com/keuin/blame/test/TestDatabase.java b/src/main/java/com/keuin/blame/test/TestDatabase.java index 140ca77..84bf4bc 100644 --- a/src/main/java/com/keuin/blame/test/TestDatabase.java +++ b/src/main/java/com/keuin/blame/test/TestDatabase.java @@ -78,7 +78,7 @@ public class TestDatabase { success[0] |= Objects.equals(e, entry); } } - }); + }, 100); Thread.sleep(2000); assertTrue(success[0]); } catch (Exception e) { -- cgit v1.2.3