Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit c731f66

Browse files
committed
Hopefully make checkListPingCompatibilityForClient a bit more readable
1 parent f4f8976 commit c731f66

1 file changed

Lines changed: 61 additions & 32 deletions

File tree

patchwork-networking/src/main/java/com/patchworkmc/impl/networking/NetworkVersionManager.java

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.function.BiPredicate;
3131
import java.util.function.Function;
3232
import java.util.stream.Collectors;
33-
import java.util.stream.StreamSupport;
3433

3534
import net.minecraftforge.fml.network.NetworkRegistry;
3635
import org.apache.commons.lang3.tuple.Pair;
@@ -137,67 +136,97 @@ private List<String> validateChannels(final Function<Identifier, String> incomin
137136
final String incomingVersion = incoming.apply(channel.getChannelName());
138137
final boolean accepted = predicate.test(channel, incomingVersion != null ? incomingVersion : NetworkRegistry.ABSENT);
139138

140-
if (origin == Origin.VANILLA) {
141-
LOGGER.debug(NETREGISTRY, "Channel '{}' : Vanilla acceptance test: {}", channel.getChannelName(), accepted ? "ACCEPTED" : "REJECTED");
142-
} else {
143-
LOGGER.debug(NETREGISTRY, "Channel '{}' : Version test of '{}' from {} : {}", channel.getChannelName(), incomingVersion, origin, accepted ? "ACCEPTED" : "REJECTED");
144-
}
145-
146139
if (!accepted) {
147140
rejected.add(channel.getChannelName().toString());
148141
}
142+
143+
final String status = accepted ? "ACCEPTED" : "REJECTED";
144+
145+
switch (origin) {
146+
case VANILLA:
147+
LOGGER.debug(NETREGISTRY, "Channel '{}' : Vanilla acceptance test: {}", channel.getChannelName(), status);
148+
break;
149+
case PING:
150+
LOGGER.debug(NETREGISTRY, "Channel '{}' : Version test of '{}' during listping : {}", channel.getChannelName(), incomingVersion, status);
151+
break;
152+
case SERVER:
153+
case CLIENT:
154+
LOGGER.debug(NETREGISTRY, "Channel '{}' : Version test of '{}' from {} : {}", channel.getChannelName(), incomingVersion, origin, status);
155+
}
149156
}
150157

151158
if (!rejected.isEmpty()) {
152-
if (origin == Origin.VANILLA) {
159+
switch (origin) {
160+
case VANILLA:
153161
LOGGER.error(NETREGISTRY, "Channels {} rejected vanilla connections", rejected);
154-
} else {
162+
break;
163+
case PING:
164+
LOGGER.error(NETREGISTRY, "Channels {} rejected their server side version number during listping", rejected);
165+
break;
166+
case SERVER:
167+
case CLIENT:
155168
LOGGER.error(NETREGISTRY, "Channels {} rejected their {} version number", rejected, origin);
156169
}
157170

158171
return rejected;
159172
}
160173

161-
LOGGER.debug(NETREGISTRY, "Accepting channel list from {}", origin);
174+
if (origin != Origin.PING) {
175+
LOGGER.debug(NETREGISTRY, "Accepting channel list from {}", origin);
176+
}
162177

163178
return Collections.emptyList();
164179
}
165180

166181
public boolean checkListPingCompatibilityForClient(Map<Identifier, Pair<String, Boolean>> incoming) {
167-
Set<Identifier> handled = new HashSet<>();
168-
final List<Pair<Identifier, Boolean>> results = StreamSupport.stream(channels.spliterator(), false)
169-
.filter(p -> !p.getChannelName().getNamespace().equals("fml"))
170-
.map(ni -> {
171-
final Pair<String, Boolean> incomingVersion = incoming.getOrDefault(ni.getChannelName(), Pair.of(ABSENT, true));
172-
final boolean test = ni.tryServerVersionOnClient(incomingVersion.getLeft());
173-
handled.add(ni.getChannelName());
174-
LOGGER.debug(NETREGISTRY, "Channel '{}' : Version test of '{}' during listping : {}", ni.getChannelName(), incomingVersion, test ? "ACCEPTED" : "REJECTED");
175-
return Pair.of(ni.getChannelName(), test);
176-
}).filter(p -> !p.getRight()).collect(Collectors.toList());
177-
final List<Identifier> missingButRequired = incoming.entrySet().stream()
178-
.filter(p -> !p.getKey().getNamespace().equals("fml"))
179-
.filter(p -> !p.getValue().getRight())
180-
.filter(p -> !handled.contains(p.getKey()))
181-
.map(Map.Entry::getKey)
182-
.collect(Collectors.toList());
183-
184-
if (!results.isEmpty()) {
185-
LOGGER.error(NETREGISTRY, "Channels [{}] rejected their server side version number during listping",
186-
results.stream().map(Pair::getLeft).map(Object::toString).collect(Collectors.joining(",")));
182+
// TODO: don't mutate map here, modify the call site instead
183+
incoming.keySet().removeIf(name -> name.getNamespace().equals("fml"));
184+
185+
List<String> rejected = validateChannels(identifier -> {
186+
Pair<String, Boolean> entry = incoming.get(identifier);
187+
188+
return entry != null ? entry.getLeft() : null;
189+
}, Origin.PING, VersionedChannel::tryServerVersionOnClient);
190+
191+
if (!rejected.isEmpty()) {
187192
return false;
188193
}
189194

195+
Set<Identifier> handled = new HashSet<>();
196+
List<Identifier> missingButRequired = new ArrayList<>();
197+
198+
for (NamedChannel channel: channels) {
199+
handled.add(channel.getChannelName());
200+
}
201+
202+
for (Map.Entry<Identifier, Pair<String, Boolean>> entry : incoming.entrySet()) {
203+
Identifier channelName = entry.getKey();
204+
boolean required = entry.getValue().getRight();
205+
206+
if (!required || handled.contains(channelName)) {
207+
continue;
208+
}
209+
210+
missingButRequired.add(channelName);
211+
}
212+
190213
if (!missingButRequired.isEmpty()) {
191-
LOGGER.error(NETREGISTRY, "The server is likely to require channel [{}] to be present, yet we don't have it",
214+
LOGGER.error(NETREGISTRY, "The server is likely to require the channels {} to be present, yet we don't have them",
192215
missingButRequired);
193216
return false;
194217
}
195218

196219
LOGGER.debug(NETREGISTRY, "Accepting channel list during listping");
220+
197221
return true;
198222
}
199223

200224
public enum Origin {
201-
CLIENT, SERVER, VANILLA
225+
CLIENT, SERVER, VANILLA,
226+
227+
/**
228+
* Server list ping response from server.
229+
*/
230+
PING
202231
}
203232
}

0 commit comments

Comments
 (0)