Skip to content

Commit 9c7b424

Browse files
committed
Add lastExecution and lastOutput to command block dump
Changed logic for creator/updater handling
1 parent 4b497b2 commit 9c7b424

File tree

6 files changed

+101
-8
lines changed

6 files changed

+101
-8
lines changed

src/main/java/net/modfest/fireblanket/command/CmdFindReplaceCommand.java

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
import net.minecraft.server.command.ServerCommandSource;
1010
import net.minecraft.server.world.ChunkHolder;
1111
import net.minecraft.server.world.ServerWorld;
12+
import net.minecraft.text.HoverEvent;
1213
import net.minecraft.text.Text;
1314
import net.minecraft.util.Formatting;
1415
import net.minecraft.util.math.BlockPos;
1516
import net.minecraft.world.CommandBlockExecutor;
1617
import net.minecraft.world.chunk.WorldChunk;
1718
import net.modfest.fireblanket.compat.roles.Roles;
19+
import net.modfest.fireblanket.mixin.accessor.CommandBlockExecutorAccessor;
1820
import net.modfest.fireblanket.mixinsupport.CommandBE;
1921
import net.modfest.fireblanket.util.TextUtil;
2022

2123
import java.util.Map;
24+
import java.util.Objects;
2225
import java.util.Optional;
2326
import java.util.UUID;
2427
import java.util.function.ToIntBiFunction;
@@ -192,16 +195,59 @@ static Optional<Text> toText(
192195
UUID owner = cbe.fireblanket$getOwner();
193196
UUID lastUpdate = cbe.fireblanket$getLastUpdate();
194197

195-
Text ownerName = TextUtil.getPlayerName(server, owner, "Unknown owner!!");
196-
Text lastUpdateName = TextUtil.getPlayerName(server, lastUpdate, "Unknown last update!!");
198+
final CommandBlockExecutor executor = cbe.fireblanket$getCommandExecutor();
199+
200+
Text ownerName = TextUtil.getPlayerName(server, owner, "Unknown")
201+
.copy().formatted(Formatting.YELLOW);
202+
Text commandText = Text.literal(command).formatted(Formatting.GRAY)
203+
.styled(style -> style.withHoverEvent(new HoverEvent.ShowText(executor.getLastOutput())));
204+
205+
final Text lastExecuted;
206+
207+
final long lastExecution = ((CommandBlockExecutorAccessor) executor).getLastExecution();
208+
209+
if (lastExecution == -1) {
210+
lastExecuted = TextUtil.unknown.copy().formatted(Formatting.GRAY);
211+
} else {
212+
final long ticks = executor.getWorld().getTime() - lastExecution;
213+
214+
final Formatting formatting;
215+
if (ticks == 0) {
216+
formatting = Formatting.RED;
217+
} else if (ticks <= 20) {
218+
formatting = Formatting.GOLD;
219+
} else if (ticks <= 200) {
220+
formatting = Formatting.YELLOW;
221+
} else {
222+
formatting = Formatting.GREEN;
223+
}
224+
225+
lastExecuted = ExecutorUtils.buildDuration(ticks)
226+
.formatted(formatting);
227+
}
228+
229+
if (Objects.equals(owner, lastUpdate)) {
230+
return Optional.of(Text.translatableWithFallback(
231+
"fireblanket.commands.command.grep.entry.same",
232+
"[%s] [%s] [%s]: %s",
233+
name,
234+
ownerName,
235+
lastExecuted,
236+
commandText
237+
));
238+
}
239+
240+
Text lastUpdateName = TextUtil.getPlayerName(server, lastUpdate, "Unknown")
241+
.copy().formatted(Formatting.YELLOW);
197242

198243
return Optional.of(Text.translatableWithFallback(
199244
"fireblanket.commands.command.grep.entry",
200-
"[%s] [Owner: %s] [Last updated: %s]: %s",
245+
"[%s] [%s / %s] [%s]: %s",
201246
name,
202247
ownerName,
203248
lastUpdateName,
204-
command
249+
lastExecuted,
250+
commandText
205251
));
206252
}
207253

src/main/java/net/modfest/fireblanket/command/ExecutorUtils.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static Text toBlame(final BlockEntity blockEntity) {
142142
final MutableText location = TextUtil.ofLocationWithTeleport(
143143
blockEntity.getWorld(),
144144
blockEntity.getPos()
145-
);
145+
).formatted(Formatting.YELLOW);
146146

147147
if (blockEntity instanceof CommandBE cbe) {
148148
location.styled(style -> style.withHoverEvent(cbe.fireblanket$getBlame()));
@@ -183,7 +183,8 @@ static MutableText toEmojiSpeak(final Entity e) {
183183

184184
static Text toBlame(final Entity entity) {
185185
final MutableText location = TextUtil.ofLocation(entity.getBlockPos())
186-
.styled(style -> style.withClickEvent(TextUtil.toClickToTeleport(entity)));
186+
.styled(style -> style.withFormatting(Formatting.YELLOW)
187+
.withClickEvent(TextUtil.toClickToTeleport(entity)));
187188

188189
if (entity instanceof CommandBE cbe) {
189190
location.styled(style -> style.withHoverEvent(cbe.fireblanket$getBlame()));
@@ -193,4 +194,34 @@ static Text toBlame(final Entity entity) {
193194
.append(" @ ")
194195
.append(location);
195196
}
197+
198+
// Admittedly this could be better (i.e. off the shelf tooling?)
199+
// But not really sure how you'd do that.
200+
static MutableText buildDuration(long ticks) {
201+
if (ticks == 0) {
202+
return Text.literal("Now");
203+
}
204+
205+
long seconds = ticks / 20;
206+
ticks %= 20;
207+
208+
if (seconds == 0) {
209+
return Text.literal(ticks + " ticks");
210+
}
211+
212+
long minutes = seconds / 60;
213+
seconds %= 60;
214+
215+
long hours = minutes / 60;
216+
minutes %= 60;
217+
218+
long days = hours / 24;
219+
hours %= 24;
220+
221+
if (days == 0) {
222+
return Text.literal(String.format("%02d:%02d:%02d.%02d", hours, minutes, seconds, ticks));
223+
}
224+
225+
return Text.literal(String.format("%d %02d:%02d:%02d.%02d", days, hours, minutes, seconds, ticks));
226+
}
196227
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package net.modfest.fireblanket.mixin.accessor;
2+
3+
import net.minecraft.world.CommandBlockExecutor;
4+
import org.spongepowered.asm.mixin.Mixin;
5+
import org.spongepowered.asm.mixin.gen.Accessor;
6+
7+
/**
8+
* @author Ampflower
9+
**/
10+
@Mixin(CommandBlockExecutor.class)
11+
public interface CommandBlockExecutorAccessor {
12+
@Accessor
13+
long getLastExecution();
14+
}

src/main/java/net/modfest/fireblanket/util/TextUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public final class TextUtil {
4848
.withHoverEvent(new HoverEvent.ShowText(Text.translatable("fireblanket.spoofed.tooltip")))
4949
);
5050

51-
private static final Text unknown = Text.translatableWithFallback("fireblanket.unknown", "???")
51+
public static final Text unknown = Text.translatableWithFallback("fireblanket.unknown", "???")
5252
.formatted(Formatting.GRAY, Formatting.ITALIC);
5353

5454

src/main/resources/assets/fireblanket/lang/en_us.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"gamerule.newEntitiesImmutable": "Whether newly spawned entities should be immutable. (Item frames will be fixed for example)",
55
"commands.gamerule.locked": "You do not have permission to modify this gamerule",
66
"commandsBlock.commandSetByPlayer": "%s set the command at (%s) to: %s",
7-
"fireblanket.commands.command.grep.entry": "[%s] [Owner: %s] [Last updated: %s]: %s",
7+
"fireblanket.commands.command.grep.entry": "[%s] [Creator: %s] [Updater: %s] [%s]\n> %s",
8+
"fireblanket.commands.command.grep.entry.same": "[%s] [%s] [%s]\n> %s",
89
"fireblanket.commands.command.grep.result": "Found %s matches.",
910
"fireblanket.commands.command.sed.result.0": "Replaced %s occurrence across %s block",
1011
"fireblanket.commands.command.sed.result.1": "Replaced %s occurrences across %s block",

src/main/resources/fireblanket.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"accessor.ArmorStandEntityAccessor",
88
"accessor.BlockEntityTypeAccessor",
99
"accessor.ClientConnectionAccessor",
10+
"accessor.CommandBlockExecutorAccessor",
1011
"accessor.CommandBlockMinecartEntityAccessor",
1112
"accessor.EntityTypeAccessor",
1213
"accessor.EntityVelocityUpdateS2CPacketAccessor",

0 commit comments

Comments
 (0)