Skip to content

Commit b4f5eaa

Browse files
committed
fix: sync custom claim problems to client as well as standard ones
1 parent b3047c2 commit b4f5eaa

File tree

4 files changed

+30
-40
lines changed

4 files changed

+30
-40
lines changed

common/src/main/java/dev/ftb/mods/ftbchunks/api/ClaimResult.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ default boolean isSuccess() {
3232
*
3333
* @return the message to be displayed
3434
*/
35-
MutableComponent getMessage();
35+
default MutableComponent getMessage() {
36+
return Component.translatable(getResultId());
37+
}
3638

3739
/**
3840
* Create a custom claim failure result. This may be of use to mods which add extra checks to claiming/forcing/etc.
@@ -74,23 +76,19 @@ enum StandardProblem implements ClaimResult {
7476

7577
public static final NameMap<StandardProblem> NAME_MAP = NameMap.of(NOT_OWNER, values()).baseNameKey("ftbchunks.standard_problem").create();
7678

77-
private final String resultName;
79+
private final String resultId;
7880

79-
StandardProblem(String resultName) {
80-
this.resultName = resultName;
81+
StandardProblem(String resultId) {
82+
this.resultId = "ftbchunks.claim_result." + resultId;
8183
}
8284

8385
public static Optional<StandardProblem> forName(String name) {
8486
return Optional.ofNullable(NAME_MAP.get(name));
8587
}
8688

87-
public MutableComponent getMessage() {
88-
return Component.translatable("ftbchunks.claim_result." + getResultId());
89-
}
90-
9189
@Override
9290
public String getResultId() {
93-
return resultName;
91+
return resultId;
9492
}
9593
}
9694

@@ -100,20 +98,15 @@ public String getResultId() {
10098
* Use {@link ClaimResult#customProblem(String)} to create instances of this class.
10199
*/
102100
class CustomProblem implements ClaimResult {
103-
private final String name;
101+
private final String translationKey;
104102

105-
private CustomProblem(String name) {
106-
this.name = name;
103+
private CustomProblem(String translationKey) {
104+
this.translationKey = translationKey;
107105
}
108106

109107
@Override
110108
public String getResultId() {
111-
return name;
112-
}
113-
114-
@Override
115-
public MutableComponent getMessage() {
116-
return Component.translatable(name);
109+
return translationKey;
117110
}
118111
}
119112
}

common/src/main/java/dev/ftb/mods/ftbchunks/client/gui/ChunkScreenPanel.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@
3838
import net.minecraft.client.player.LocalPlayer;
3939
import net.minecraft.commands.Commands;
4040
import net.minecraft.network.chat.Component;
41+
import net.minecraft.network.chat.MutableComponent;
4142
import net.minecraft.world.entity.player.Player;
4243
import net.minecraft.world.level.ChunkPos;
4344
import org.lwjgl.glfw.GLFW;
4445

4546
import java.util.ArrayList;
4647
import java.util.Date;
47-
import java.util.EnumMap;
4848
import java.util.HashSet;
4949
import java.util.List;
5050
import java.util.Map;
@@ -80,7 +80,7 @@ public ChunkScreenPanel(ChunkScreen panel) {
8080
alignWidgets();
8181
}
8282

83-
public static void notifyChunkUpdates(int totalChunks, int changedChunks, EnumMap<ClaimResult.StandardProblem, Integer> problems) {
83+
public static void notifyChunkUpdates(int totalChunks, int changedChunks, Map<String, Integer> problems) {
8484
if (Minecraft.getInstance().screen instanceof ScreenWrapper sw && sw.getGui() instanceof ChunkScreen cs) {
8585
cs.getChunkScreen().updateInfo = new ChunkUpdateInfo(totalChunks, changedChunks, problems, Minecraft.getInstance().level.getGameTime());
8686
}
@@ -200,10 +200,10 @@ public void drawBackground(GuiGraphics graphics, Theme theme, int x, int y, int
200200
if (updateInfo != null && updateInfo.shouldDisplay()) {
201201
theme.drawString(graphics, updateInfo.summary(), sx + 2, sy + 2, Theme.SHADOW);
202202
int line = 1;
203-
for (Map.Entry<ClaimResult.StandardProblem, Integer> entry : updateInfo.problems.entrySet()) {
204-
ClaimResult.StandardProblem problem = entry.getKey();
203+
for (Map.Entry<String, Integer> entry : updateInfo.problems.entrySet()) {
204+
MutableComponent problem = Component.translatable(entry.getKey());
205205
int count = entry.getValue();
206-
theme.drawString(graphics, problem.getMessage().append(": " + count), sx + 2, sy + 5 + theme.getFontHeight() * line++, Theme.SHADOW);
206+
theme.drawString(graphics, problem.append(": " + count), sx + 2, sy + 5 + theme.getFontHeight() * line++, Theme.SHADOW);
207207
}
208208
}
209209
}
@@ -355,7 +355,7 @@ public XZ getChunkPos() {
355355
}
356356
}
357357

358-
public record ChunkUpdateInfo(int totalChunks, int changedChunks, EnumMap<ClaimResult.StandardProblem, Integer> problems, long timestamp) {
358+
public record ChunkUpdateInfo(int totalChunks, int changedChunks, Map<String, Integer> problems, long timestamp) {
359359
public boolean shouldDisplay() {
360360
// display for 3 seconds + 1 second per line of problem data
361361
long timeout = 60L + 20L * problems.size();

common/src/main/java/dev/ftb/mods/ftbchunks/net/ChunkChangeResponsePacket.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
package dev.ftb.mods.ftbchunks.net;
22

33
import dev.architectury.networking.NetworkManager;
4-
import dev.ftb.mods.ftbchunks.api.ClaimResult.StandardProblem;
54
import dev.ftb.mods.ftbchunks.api.FTBChunksAPI;
65
import dev.ftb.mods.ftbchunks.client.gui.ChunkScreenPanel;
7-
import dev.ftb.mods.ftblibrary.util.NetworkHelper;
86
import net.minecraft.network.FriendlyByteBuf;
97
import net.minecraft.network.codec.ByteBufCodecs;
108
import net.minecraft.network.codec.StreamCodec;
119
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
1210

13-
import java.util.EnumMap;
11+
import java.util.HashMap;
12+
import java.util.Map;
1413

15-
public record ChunkChangeResponsePacket(int totalChunks, int changedChunks, EnumMap<StandardProblem,Integer> problems) implements CustomPacketPayload {
14+
public record ChunkChangeResponsePacket(
15+
int totalChunks, int changedChunks, Map<String,Integer> problems
16+
) implements CustomPacketPayload
17+
{
1618
public static final Type<ChunkChangeResponsePacket> TYPE = new Type<>(FTBChunksAPI.rl("chunk_change_response_packet"));
1719

1820
public static final StreamCodec<FriendlyByteBuf, ChunkChangeResponsePacket> STREAM_CODEC = StreamCodec.composite(
1921
ByteBufCodecs.VAR_INT, ChunkChangeResponsePacket::totalChunks,
2022
ByteBufCodecs.VAR_INT, ChunkChangeResponsePacket::changedChunks,
21-
ByteBufCodecs.map(i -> new EnumMap<>(StandardProblem.class), NetworkHelper.enumStreamCodec(StandardProblem.class), ByteBufCodecs.VAR_INT), ChunkChangeResponsePacket::problems,
23+
ByteBufCodecs.map(HashMap::new, ByteBufCodecs.STRING_UTF8, ByteBufCodecs.VAR_INT), ChunkChangeResponsePacket::problems,
2224
ChunkChangeResponsePacket::new
2325
);
2426

common/src/main/java/dev/ftb/mods/ftbchunks/net/RequestChunkChangePacket.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@
2020
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
2121
import net.minecraft.server.level.ServerPlayer;
2222

23-
import java.util.EnumMap;
24-
import java.util.HashSet;
25-
import java.util.Optional;
26-
import java.util.Set;
27-
import java.util.UUID;
23+
import java.util.*;
2824
import java.util.function.Function;
2925

3026
public record RequestChunkChangePacket(ChunkChangeOp action, Set<XZ> chunks, boolean tryAdminChanges, Optional<UUID> teamId) implements CustomPacketPayload {
@@ -68,16 +64,15 @@ public static void handle(RequestChunkChangePacket message, NetworkManager.Packe
6864
case UNLOAD -> pos -> data.unForceLoad(source, pos.dim(player.level()), false, message.tryAdminChanges);
6965
};
7066

71-
EnumMap<ClaimResult.StandardProblem, Integer> problems = new EnumMap<>(ClaimResult.StandardProblem.class);
67+
Map<String,Integer> problems = new HashMap<>();
68+
7269
int changed = 0;
7370
for (XZ pos : message.chunks) {
7471
ClaimResult r = consumer.apply(pos);
7572
if (!r.isSuccess()) {
76-
FTBChunks.LOGGER.debug(String.format("%s tried to %s @ %s:%d:%d but got result %s", player.getScoreboardName(),
77-
message.action.name, player.level().dimension().location(), pos.x(), pos.z(), r));
78-
if (r instanceof ClaimResult.StandardProblem cr) {
79-
problems.put(cr, problems.getOrDefault(cr, 0) + 1);
80-
}
73+
FTBChunks.LOGGER.debug("{} tried to {} @ {}:{}:{} but got result {}",
74+
player.getScoreboardName(), message.action.name, player.level().dimension().location(), pos.x(), pos.z(), r);
75+
problems.put(r.getResultId(), problems.getOrDefault(r.getResultId(), 0) + 1);
8176
} else {
8277
changed++;
8378
}

0 commit comments

Comments
 (0)