Skip to content

Commit 2a81e2d

Browse files
committed
Make highlight duration configurable
1 parent eb6fbd5 commit 2a81e2d

File tree

5 files changed

+148
-2
lines changed

5 files changed

+148
-2
lines changed

src/main/java/dev/xpple/seedmapper/SeedMapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.mojang.blaze3d.platform.InputConstants;
44
import com.mojang.brigadier.CommandDispatcher;
55
import dev.xpple.betterconfig.api.ModConfigBuilder;
6+
import dev.xpple.seedmapper.command.arguments.DurationArgument;
67
import dev.xpple.seedmapper.command.arguments.MapFeatureArgument;
78
import dev.xpple.seedmapper.command.arguments.SeedIdentifierArgument;
89
import dev.xpple.seedmapper.command.arguments.SeedResolutionArgument;
@@ -18,6 +19,7 @@
1819
import dev.xpple.seedmapper.command.commands.SourceCommand;
1920
import dev.xpple.seedmapper.command.commands.StopTaskCommand;
2021
import dev.xpple.seedmapper.config.Configs;
22+
import dev.xpple.seedmapper.config.DurationAdapter;
2123
import dev.xpple.seedmapper.config.MapFeatureAdapter;
2224
import dev.xpple.seedmapper.config.SeedIdentifierAdapter;
2325
import dev.xpple.seedmapper.config.SeedResolutionAdapter;
@@ -43,6 +45,7 @@
4345
import java.nio.file.Files;
4446
import java.nio.file.Path;
4547
import java.nio.file.StandardCopyOption;
48+
import java.time.Duration;
4649

4750
public class SeedMapper implements ClientModInitializer {
4851

@@ -69,6 +72,7 @@ public void onInitializeClient() {
6972
.registerType(SeedIdentifier.class, new SeedIdentifierAdapter(), SeedIdentifierArgument::seedIdentifier)
7073
.registerType(SeedResolutionArgument.SeedResolution.class, new SeedResolutionAdapter(), SeedResolutionArgument::seedResolution)
7174
.registerTypeHierarchy(MapFeature.class, new MapFeatureAdapter(), MapFeatureArgument::mapFeature)
75+
.registerType(Duration.class, new DurationAdapter(), DurationArgument::duration)
7276
.registerGlobalChangeHook(event -> {
7377
if (event.config().equals("DevMode")) {
7478
try {
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package dev.xpple.seedmapper.command.arguments;
2+
3+
import com.mojang.brigadier.StringReader;
4+
import com.mojang.brigadier.arguments.ArgumentType;
5+
import com.mojang.brigadier.context.CommandContext;
6+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
7+
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
8+
import com.mojang.brigadier.suggestion.Suggestions;
9+
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
10+
import dev.xpple.seedmapper.command.CustomClientCommandSource;
11+
import it.unimi.dsi.fastutil.objects.Object2IntMap;
12+
import it.unimi.dsi.fastutil.objects.Object2IntMaps;
13+
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
14+
import net.minecraft.commands.SharedSuggestionProvider;
15+
import net.minecraft.network.chat.Component;
16+
import net.minecraft.util.TimeUtil;
17+
import net.minecraft.util.Util;
18+
19+
import java.time.Duration;
20+
import java.util.Collection;
21+
import java.util.List;
22+
import java.util.concurrent.CompletableFuture;
23+
import java.util.function.Consumer;
24+
25+
public class DurationArgument implements ArgumentType<Duration> {
26+
27+
private static final Collection<String> EXAMPLES = List.of("5s", "10m", "3h");
28+
29+
private static final SimpleCommandExceptionType INVALID_UNIT_EXCEPTION = new SimpleCommandExceptionType(Component.translatable("argument.time.invalid_unit"));
30+
31+
public static DurationArgument duration() {
32+
return new DurationArgument();
33+
}
34+
35+
public static DurationArgument getDuration(CommandContext<CustomClientCommandSource> context, String name) {
36+
return context.getArgument(name, DurationArgument.class);
37+
}
38+
39+
@Override
40+
public Duration parse(StringReader reader) throws CommandSyntaxException {
41+
return new Parser(reader).parse();
42+
}
43+
44+
@Override
45+
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
46+
StringReader reader = new StringReader(builder.getInput());
47+
reader.setCursor(builder.getStart());
48+
49+
Parser parser = new Parser(reader);
50+
51+
try {
52+
parser.parse();
53+
} catch (CommandSyntaxException ignored) {
54+
}
55+
56+
if (parser.suggester != null) {
57+
parser.suggester.accept(builder);
58+
}
59+
60+
return builder.buildFuture();
61+
}
62+
63+
@Override
64+
public Collection<String> getExamples() {
65+
return EXAMPLES;
66+
}
67+
68+
private static final class Parser {
69+
70+
private static final Object2IntMap<String> UNITS = Util.make(() -> {
71+
Object2IntMap<String> temp = new Object2IntOpenHashMap<>();
72+
73+
temp.put("s", 1);
74+
temp.put("m", TimeUtil.SECONDS_PER_MINUTE);
75+
temp.put("h", (int) TimeUtil.SECONDS_PER_HOUR);
76+
77+
return Object2IntMaps.unmodifiable(temp);
78+
});
79+
80+
private final StringReader reader;
81+
private Consumer<SuggestionsBuilder> suggester;
82+
83+
private Parser(StringReader reader) {
84+
this.reader = reader;
85+
}
86+
87+
private Duration parse() throws CommandSyntaxException {
88+
float durationRaw = reader.readFloat();
89+
int cursor = reader.getCursor();
90+
suggester = builder -> {
91+
SuggestionsBuilder newBuilder = builder.createOffset(cursor);
92+
SharedSuggestionProvider.suggest(UNITS.keySet(), newBuilder);
93+
builder.add(newBuilder);
94+
};
95+
String string = reader.readUnquotedString();
96+
int unit = UNITS.getOrDefault(string, -1);
97+
if (unit == -1) {
98+
throw INVALID_UNIT_EXCEPTION.create();
99+
}
100+
int durationSeconds = Math.round(durationRaw * unit);
101+
return Duration.ofSeconds(durationSeconds);
102+
}
103+
}
104+
}

src/main/java/dev/xpple/seedmapper/config/Configs.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import dev.xpple.betterconfig.api.ModConfig;
77
import dev.xpple.seedmapper.SeedMapper;
88
import dev.xpple.seedmapper.command.arguments.SeedResolutionArgument;
9+
import dev.xpple.seedmapper.render.RenderManager;
910
import dev.xpple.seedmapper.seedmap.MapFeature;
1011
import dev.xpple.seedmapper.seedmap.SeedMapScreen;
1112
import dev.xpple.seedmapper.util.ComponentUtils;
@@ -15,6 +16,7 @@
1516
import net.minecraft.network.chat.Component;
1617
import net.minecraft.util.Util;
1718

19+
import java.time.Duration;
1820
import java.util.EnumSet;
1921
import java.util.HashMap;
2022
import java.util.Map;
@@ -132,4 +134,10 @@ private static Component listToggledFeatures() {
132134
private static Component getDevModeComment() {
133135
return Component.translatable("config.devMode.comment");
134136
}
137+
138+
@Config(onChange = "updateHighlightDuration")
139+
public static Duration HighlightDuration = Duration.ofMinutes(5);
140+
private static void updateHighlightDuration(Duration oldValue, Duration newValue) {
141+
RenderManager.rebuildLineSet();
142+
}
135143
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dev.xpple.seedmapper.config;
2+
3+
import com.google.gson.TypeAdapter;
4+
import com.google.gson.stream.JsonReader;
5+
import com.google.gson.stream.JsonWriter;
6+
7+
import java.io.IOException;
8+
import java.time.Duration;
9+
10+
public class DurationAdapter extends TypeAdapter<Duration> {
11+
@Override
12+
public void write(JsonWriter writer, Duration duration) throws IOException {
13+
writer.value(duration.toString());
14+
}
15+
16+
@Override
17+
public Duration read(JsonReader reader) throws IOException {
18+
return Duration.parse(reader.nextString());
19+
}
20+
}

src/main/java/dev/xpple/seedmapper/render/RenderManager.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.cache.CacheBuilder;
44
import com.mojang.blaze3d.vertex.PoseStack;
55
import com.mojang.blaze3d.vertex.VertexConsumer;
6+
import dev.xpple.seedmapper.config.Configs;
67
import net.fabricmc.fabric.api.client.rendering.v1.RenderStateDataKey;
78
import net.fabricmc.fabric.api.client.rendering.v1.world.WorldExtractionContext;
89
import net.fabricmc.fabric.api.client.rendering.v1.world.WorldRenderContext;
@@ -15,7 +16,6 @@
1516
import net.minecraft.world.level.chunk.status.ChunkStatus;
1617
import net.minecraft.world.phys.Vec3;
1718

18-
import java.time.Duration;
1919
import java.util.Collection;
2020
import java.util.Collections;
2121
import java.util.HashSet;
@@ -28,7 +28,17 @@ private RenderManager() {
2828

2929
private static final RenderStateDataKey<Set<Line>> LINES_SET_KEY = RenderStateDataKey.create(() -> "SeedMapper lines set");
3030

31-
private static final Set<Line> lines = Collections.newSetFromMap(CacheBuilder.newBuilder().expireAfterWrite(Duration.ofMinutes(5)).<Line, Boolean>build().asMap());
31+
private static Set<Line> lines = Collections.emptySet();
32+
static {
33+
rebuildLineSet();
34+
}
35+
36+
public static void rebuildLineSet() {
37+
Set<Line> temp = lines;
38+
lines = Collections.newSetFromMap(CacheBuilder.newBuilder().expireAfterWrite(Configs.HighlightDuration).<Line, Boolean>build().asMap());
39+
lines.addAll(temp);
40+
temp.clear();
41+
}
3242

3343
public static void drawBoxes(Collection<BlockPos> posBatch, int colour) {
3444
Set<Line> lines = new HashSet<>();

0 commit comments

Comments
 (0)