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

Commit 90d1ed5

Browse files
authored
Merge pull request #18 from PatchworkMC/feature/networking
Patchwork Networking, Part 1
2 parents 328d90e + 838cd0d commit 90d1ed5

40 files changed

+2918
-0
lines changed

checkstyle.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,11 @@
170170
<module name="AtclauseOrder">
171171
<property name="tagOrder" value="@param,@return,@throws,@deprecated"/>
172172
</module>
173+
<!--Some checks are broken (*ahem* lambda returns)-->
174+
<module name="SuppressionCommentFilter">
175+
<property name="offCommentFormat" value="CHECKSTYLE.OFF\: ([\w\|]+)"/>
176+
<property name="onCommentFormat" value="CHECKSTYLE.ON\: ([\w\|]+)"/>
177+
<property name="checkFormat" value="$1"/>
178+
</module>
173179
</module>
174180
</module>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.fml;
21+
22+
import net.minecraftforge.fml.LogicalSidedProvider;
23+
24+
import net.fabricmc.api.ModInitializer;
25+
import net.fabricmc.fabric.api.event.server.ServerStartCallback;
26+
import net.fabricmc.fabric.api.event.server.ServerStopCallback;
27+
28+
public class PatchworkFML implements ModInitializer {
29+
@Override
30+
public void onInitialize() {
31+
ServerStartCallback.EVENT.register(server -> LogicalSidedProvider.setServer(() -> server));
32+
ServerStopCallback.EVENT.register(server -> LogicalSidedProvider.setServer(null));
33+
}
34+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 net.minecraftforge.fml;
21+
22+
import java.util.Optional;
23+
import java.util.function.Function;
24+
import java.util.function.Supplier;
25+
26+
import net.minecraft.client.MinecraftClient;
27+
import net.minecraft.server.MinecraftServer;
28+
import net.minecraft.world.World;
29+
30+
import net.fabricmc.api.EnvType;
31+
import net.fabricmc.loader.api.FabricLoader;
32+
33+
import com.patchworkmc.impl.fml.PatchworkFML;
34+
35+
public enum LogicalSidedProvider {
36+
WORKQUEUE(Supplier::get, Supplier::get),
37+
INSTANCE(Supplier::get, Supplier::get),
38+
/**
39+
* @deprecated "this is pretty dubious" - coderbot
40+
*/
41+
@Deprecated
42+
CLIENTWORLD(c -> Optional.<World>of(c.get().world), s -> Optional.<World>empty());
43+
private static Supplier<MinecraftClient> client;
44+
private static Supplier<MinecraftServer> server;
45+
46+
// Patchwork: since the client never changes we can just set it directly
47+
static {
48+
if (FabricLoader.getInstance().getEnvironmentType().equals(EnvType.CLIENT)) {
49+
client = () -> (MinecraftClient) FabricLoader.getInstance().getGameInstance();
50+
}
51+
}
52+
53+
private final Function<Supplier<MinecraftClient>, ?> clientSide;
54+
private final Function<Supplier<MinecraftServer>, ?> serverSide;
55+
56+
LogicalSidedProvider(Function<Supplier<MinecraftClient>, ?> clientSide, Function<Supplier<MinecraftServer>, ?> serverSide) {
57+
this.clientSide = clientSide;
58+
this.serverSide = serverSide;
59+
}
60+
61+
/**
62+
* Called by callbacks registered in {@link PatchworkFML}.
63+
*/
64+
public static void setServer(Supplier<MinecraftServer> server) {
65+
LogicalSidedProvider.server = server;
66+
}
67+
68+
@SuppressWarnings("unchecked")
69+
public <T> T get(final LogicalSide side) {
70+
return (T) (side == LogicalSide.CLIENT ? clientSide.apply(client) : serverSide.apply(server));
71+
}
72+
}

patchwork-fml/src/main/resources/fabric.mod.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
"depends": {
1616
"fabricloader": ">=0.6.2"
1717
},
18+
"entrypoints": {
19+
"main": [
20+
"com.patchworkmc.impl.fml.PatchworkFML"
21+
]
22+
},
1823
"description": "Implementation of FML's mod-facing interface.",
1924
"custom": {
2025
"modmenu:api": true,

patchwork-networking/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
archivesBaseName = "patchwork-networking"
2+
version = getSubprojectVersion(project, "0.1.0")
3+
4+
dependencies {
5+
compile project(path: ':patchwork-fml', configuration: 'dev')
6+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.List;
23+
import java.util.function.BiConsumer;
24+
import java.util.function.Consumer;
25+
26+
import net.minecraftforge.fml.network.ICustomPacket;
27+
import net.minecraftforge.fml.network.NetworkEvent;
28+
import net.minecraftforge.fml.network.NetworkRegistry;
29+
30+
public interface ListenableChannel {
31+
void setPacketListener(BiConsumer<ICustomPacket<?>, NetworkEvent.Context> listener);
32+
void setRegistrationChangeListener(Consumer<NetworkEvent.ChannelRegistrationChangeEvent> listener);
33+
void setGatherLoginPayloadsListener(BiConsumer<List<NetworkRegistry.LoginPayload>, Boolean> listener);
34+
}
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: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.List;
23+
import java.util.function.BiConsumer;
24+
import java.util.function.Consumer;
25+
import java.util.function.Predicate;
26+
import java.util.function.Supplier;
27+
28+
import net.minecraftforge.fml.network.ICustomPacket;
29+
import net.minecraftforge.fml.network.NetworkEvent;
30+
import net.minecraftforge.fml.network.NetworkRegistry;
31+
32+
import net.minecraft.util.Identifier;
33+
34+
public class NetworkChannel implements NamedChannel, ListenableChannel, VersionedChannel {
35+
private final Identifier name;
36+
37+
private BiConsumer<ICustomPacket<?>, NetworkEvent.Context> packetListener;
38+
private Consumer<NetworkEvent.ChannelRegistrationChangeEvent> registrationChangeListener;
39+
private BiConsumer<List<NetworkRegistry.LoginPayload>, Boolean> gatherLoginPayloadsListener;
40+
41+
private final String networkProtocolVersion;
42+
private final Predicate<String> clientAcceptedVersions;
43+
private final Predicate<String> serverAcceptedVersions;
44+
45+
public NetworkChannel(Identifier name, Supplier<String> networkProtocolVersion, Predicate<String> clientAcceptedVersions, Predicate<String> serverAcceptedVersions) {
46+
this.name = name;
47+
this.networkProtocolVersion = networkProtocolVersion.get();
48+
this.clientAcceptedVersions = clientAcceptedVersions;
49+
this.serverAcceptedVersions = serverAcceptedVersions;
50+
}
51+
52+
@Override
53+
public Identifier getChannelName() {
54+
return name;
55+
}
56+
57+
@Override
58+
public void setPacketListener(BiConsumer<ICustomPacket<?>, NetworkEvent.Context> listener) {
59+
this.packetListener = listener;
60+
}
61+
62+
@Override
63+
public void setRegistrationChangeListener(Consumer<NetworkEvent.ChannelRegistrationChangeEvent> listener) {
64+
this.registrationChangeListener = listener;
65+
}
66+
67+
@Override
68+
public void setGatherLoginPayloadsListener(BiConsumer<List<NetworkRegistry.LoginPayload>, Boolean> listener) {
69+
this.gatherLoginPayloadsListener = listener;
70+
}
71+
72+
public void onPacket(ICustomPacket<?> packet, NetworkEvent.Context context) {
73+
if (packetListener != null) {
74+
packetListener.accept(packet, context);
75+
}
76+
}
77+
78+
public void onRegistrationChange(NetworkEvent.ChannelRegistrationChangeEvent event) {
79+
if (registrationChangeListener != null) {
80+
registrationChangeListener.accept(event);
81+
}
82+
}
83+
84+
public void onGatherLoginPayloads(List<NetworkRegistry.LoginPayload> payloads, boolean isLocal) {
85+
if (gatherLoginPayloadsListener != null) {
86+
gatherLoginPayloadsListener.accept(payloads, isLocal);
87+
}
88+
}
89+
90+
@Override
91+
public String getNetworkProtocolVersion() {
92+
return networkProtocolVersion;
93+
}
94+
95+
@Override
96+
public boolean tryServerVersionOnClient(final String serverVersion) {
97+
return this.clientAcceptedVersions.test(serverVersion);
98+
}
99+
100+
@Override
101+
public boolean tryClientVersionOnServer(final String clientVersion) {
102+
return this.serverAcceptedVersions.test(clientVersion);
103+
}
104+
}

0 commit comments

Comments
 (0)