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

Commit 1ff6d33

Browse files
committed
Gut NetworkInstance into NetworkChannel and subinterfaces, simplify event control flow
1 parent 4ba2d2d commit 1ff6d33

12 files changed

Lines changed: 309 additions & 216 deletions

File tree

patchwork-networking/src/main/java/com/patchworkmc/api/networking/Channel.java

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2019, 2019
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package com.patchworkmc.impl.networking;
21+
22+
import java.util.function.BiConsumer;
23+
import java.util.function.Consumer;
24+
25+
import net.minecraftforge.fml.network.ICustomPacket;
26+
import net.minecraftforge.fml.network.NetworkEvent;
27+
28+
public interface ListenableChannel {
29+
void setPacketListener(BiConsumer<ICustomPacket<?>, NetworkEvent.Context> listener);
30+
void setRegistrationChangeListener(Consumer<NetworkEvent.ChannelRegistrationChangeEvent> listener);
31+
void setGatherLoginPayloadsListener(Consumer<NetworkEvent.GatherLoginPayloadsEvent> listener);
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2019, 2019
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package com.patchworkmc.impl.networking;
21+
22+
import net.minecraft.util.Identifier;
23+
24+
public interface NamedChannel {
25+
Identifier getChannelName();
26+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2019, 2019
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package com.patchworkmc.impl.networking;
21+
22+
import java.util.function.BiConsumer;
23+
import java.util.function.Consumer;
24+
import java.util.function.Predicate;
25+
import java.util.function.Supplier;
26+
27+
import net.minecraftforge.fml.network.ICustomPacket;
28+
import net.minecraftforge.fml.network.NetworkEvent;
29+
30+
import net.minecraft.util.Identifier;
31+
32+
public class NetworkChannel implements NamedChannel, ListenableChannel, VersionedChannel {
33+
private final Identifier name;
34+
35+
private BiConsumer<ICustomPacket<?>, NetworkEvent.Context> packetListener;
36+
private Consumer<NetworkEvent.ChannelRegistrationChangeEvent> registrationChangeListener;
37+
private Consumer<NetworkEvent.GatherLoginPayloadsEvent> gatherLoginPayloadsListener;
38+
39+
private final String networkProtocolVersion;
40+
private final Predicate<String> clientAcceptedVersions;
41+
private final Predicate<String> serverAcceptedVersions;
42+
43+
public NetworkChannel(Identifier name, Supplier<String> networkProtocolVersion, Predicate<String> clientAcceptedVersions, Predicate<String> serverAcceptedVersions) {
44+
this.name = name;
45+
this.networkProtocolVersion = networkProtocolVersion.get();
46+
this.clientAcceptedVersions = clientAcceptedVersions;
47+
this.serverAcceptedVersions = serverAcceptedVersions;
48+
}
49+
50+
@Override
51+
public Identifier getChannelName() {
52+
return name;
53+
}
54+
55+
@Override
56+
public void setPacketListener(BiConsumer<ICustomPacket<?>, NetworkEvent.Context> listener) {
57+
this.packetListener = listener;
58+
}
59+
60+
@Override
61+
public void setRegistrationChangeListener(Consumer<NetworkEvent.ChannelRegistrationChangeEvent> listener) {
62+
this.registrationChangeListener = listener;
63+
}
64+
65+
@Override
66+
public void setGatherLoginPayloadsListener(Consumer<NetworkEvent.GatherLoginPayloadsEvent> listener) {
67+
this.gatherLoginPayloadsListener = listener;
68+
}
69+
70+
public void onPacket(ICustomPacket<?> packet, NetworkEvent.Context context) {
71+
if (packetListener != null) {
72+
packetListener.accept(packet, context);
73+
}
74+
}
75+
76+
public void onRegistrationChange(NetworkEvent.ChannelRegistrationChangeEvent event) {
77+
if (registrationChangeListener != null) {
78+
registrationChangeListener.accept(event);
79+
}
80+
}
81+
82+
public void onGatherLoginPayloads(NetworkEvent.GatherLoginPayloadsEvent event) {
83+
if (gatherLoginPayloadsListener != null) {
84+
gatherLoginPayloadsListener.accept(event);
85+
}
86+
}
87+
88+
@Override
89+
public String getNetworkProtocolVersion() {
90+
return networkProtocolVersion;
91+
}
92+
93+
@Override
94+
public boolean tryServerVersionOnClient(final String serverVersion) {
95+
return this.clientAcceptedVersions.test(serverVersion);
96+
}
97+
98+
@Override
99+
public boolean tryClientVersionOnServer(final String clientVersion) {
100+
return this.serverAcceptedVersions.test(clientVersion);
101+
}
102+
}

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2019, 2019
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
120
package com.patchworkmc.impl.networking;
221

322
import java.util.ArrayList;
@@ -13,7 +32,6 @@
1332
import java.util.stream.Collectors;
1433
import java.util.stream.StreamSupport;
1534

16-
import net.minecraftforge.fml.network.NetworkInstance;
1735
import net.minecraftforge.fml.network.NetworkRegistry;
1836
import org.apache.commons.lang3.tuple.Pair;
1937
import org.apache.logging.log4j.LogManager;
@@ -29,9 +47,9 @@ public final class NetworkVersionManager {
2947
public static final String ABSENT = "ABSENT \uD83E\uDD14";
3048
public static final String ACCEPTVANILLA = "ALLOWVANILLA \uD83D\uDC93\uD83D\uDC93\uD83D\uDC93";
3149

32-
Iterable<NetworkInstance> channels;
50+
Iterable<NetworkChannel> channels;
3351

34-
public NetworkVersionManager(Collection<NetworkInstance> channels) {
52+
public NetworkVersionManager(Collection<NetworkChannel> channels) {
3553
this.channels = channels;
3654
}
3755

@@ -63,7 +81,7 @@ public Map<Identifier, Pair<String, Boolean>> buildChannelVersionsForListPing()
6381
for (VersionedChannel channel: channels) {
6482
Identifier name = channel.getChannelName();
6583

66-
if(name.getNamespace().equals("fml")) {
84+
if (name.getNamespace().equals("fml")) {
6785
continue;
6886
}
6987

@@ -119,19 +137,19 @@ private List<String> validateChannels(final Function<Identifier, String> incomin
119137
final String incomingVersion = incoming.apply(channel.getChannelName());
120138
final boolean accepted = predicate.test(channel, incomingVersion != null ? incomingVersion : NetworkRegistry.ABSENT);
121139

122-
if(origin == Origin.VANILLA) {
140+
if (origin == Origin.VANILLA) {
123141
LOGGER.debug(NETREGISTRY, "Channel '{}' : Vanilla acceptance test: {}", channel.getChannelName(), accepted ? "ACCEPTED" : "REJECTED");
124142
} else {
125143
LOGGER.debug(NETREGISTRY, "Channel '{}' : Version test of '{}' from {} : {}", channel.getChannelName(), incomingVersion, origin, accepted ? "ACCEPTED" : "REJECTED");
126144
}
127145

128-
if(!accepted) {
146+
if (!accepted) {
129147
rejected.add(channel.getChannelName().toString());
130148
}
131149
}
132150

133151
if (!rejected.isEmpty()) {
134-
if(origin == Origin.VANILLA) {
152+
if (origin == Origin.VANILLA) {
135153
LOGGER.error(NETREGISTRY, "Channels {} rejected vanilla connections", rejected);
136154
} else {
137155
LOGGER.error(NETREGISTRY, "Channels {} rejected their {} version number", rejected, origin);
Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1-
package com.patchworkmc.impl.networking;
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2019, 2019
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
219

3-
import com.patchworkmc.api.networking.Channel;
20+
package com.patchworkmc.impl.networking;
421

5-
public interface VersionedChannel extends Channel {
22+
public interface VersionedChannel extends NamedChannel {
623
String getNetworkProtocolVersion();
7-
boolean tryServerVersionOnClient(final String serverVersion);
8-
boolean tryClientVersionOnServer(final String clientVersion);
24+
boolean tryServerVersionOnClient(String serverVersion);
25+
boolean tryClientVersionOnServer(String clientVersion);
926
}

patchwork-networking/src/main/java/net/minecraftforge/fml/network/NetworkHooks.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@
2121

2222
import net.minecraft.network.ClientConnection;
2323

24+
import com.patchworkmc.impl.networking.NetworkChannel;
25+
2426
public class NetworkHooks {
2527
public static boolean onCustomPayload(final ICustomPacket<?> packet, final ClientConnection connection) {
26-
NetworkInstance target = NetworkRegistry.findTarget(packet.getName());
28+
NetworkChannel target = NetworkRegistry.findTarget(packet.getName());
2729

2830
if (target == null) {
2931
return false;
3032
}
3133

32-
return target.dispatch(packet, connection);
34+
final NetworkEvent.Context context = new NetworkEvent.Context(connection, packet.getDirection(), packet.getIndex());
35+
36+
target.onPacket(packet, context);
37+
38+
return context.getPacketHandled();
3339
}
3440
}

0 commit comments

Comments
 (0)