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

Commit cffbedc

Browse files
committed
Merge branch 'feature/networking-messages'
2 parents 9f24aee + 86a4d5e commit cffbedc

File tree

31 files changed

+1070
-246
lines changed

31 files changed

+1070
-246
lines changed

patchwork-events-entity/src/main/java/net/minecraftforge/event/entity/living/LivingAttackEvent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class LivingAttackEvent extends LivingEvent {
4343
private final DamageSource source;
4444
private final float damage;
4545

46+
// For EventBus
4647
public LivingAttackEvent() {
4748
this.source = null;
4849
this.damage = 0f;

patchwork-events-entity/src/main/java/net/minecraftforge/event/entity/living/LivingSpawnEvent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class LivingSpawnEvent extends LivingEvent {
4444
private final double y;
4545
private final double z;
4646

47+
// For EventBus
4748
public LivingSpawnEvent() {
4849
this.world = null;
4950
this.x = 0;

patchwork-extensions/src/main/java/com/patchworkmc/impl/extension/PatchworkEntityTypeBuilderExtensions.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,4 @@ public interface PatchworkEntityTypeBuilderExtensions<T extends Entity> {
3131
EntityType.Builder<T> setTrackingRange(int range);
3232

3333
EntityType.Builder<T> setShouldReceiveVelocityUpdates(boolean value);
34-
35-
// TODO: setCustomClientFactory (waiting on patchwork-networking features)
3634
}

patchwork-extensions/src/main/java/com/patchworkmc/impl/extension/PatchworkEntityTypeExtensions.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
* Forge does this through patching the constructor instead, we just add methods with mixins instead.
2626
*/
2727
public interface PatchworkEntityTypeExtensions<T extends Entity> {
28-
void setUpdateInterval(int interval);
28+
void patchwork$setUpdateInterval(int interval);
2929

30-
void setTrackingRange(int range);
30+
void patchwork$setTrackingRange(int range);
3131

32-
void setShouldReceiveVelocityUpdates(boolean value);
33-
34-
// TODO: setCustomClientFactory (waiting on patchwork-networking features)
32+
void patchwork$setShouldReceiveVelocityUpdates(boolean value);
3533
}

patchwork-extensions/src/main/java/com/patchworkmc/mixin/extension/MixinEntityType.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ private void hookAlwaysUpdateVelocity(CallbackInfoReturnable<Boolean> cir) {
6060
}
6161

6262
@Override
63-
public void setUpdateInterval(int interval) {
63+
public void patchwork$setUpdateInterval(int interval) {
6464
this.updateInterval = interval;
6565
}
6666

6767
@Override
68-
public void setTrackingRange(int range) {
68+
public void patchwork$setTrackingRange(int range) {
6969
this.trackingRange = range;
7070
}
7171

7272
@Override
73-
public void setShouldReceiveVelocityUpdates(boolean value) {
73+
public void patchwork$setShouldReceiveVelocityUpdates(boolean value) {
7474
this.shouldRecieveVelocityUpdates = value;
7575
}
7676
}

patchwork-extensions/src/main/java/com/patchworkmc/mixin/extension/MixinEntityTypeBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ private void onBuildReturn(String id, CallbackInfoReturnable<EntityType> cir) {
4444
PatchworkEntityTypeExtensions type = (PatchworkEntityTypeExtensions) cir.getReturnValue();
4545

4646
if (updateInterval != null) {
47-
type.setUpdateInterval(updateInterval);
47+
type.patchwork$setUpdateInterval(updateInterval);
4848
}
4949

5050
if (trackingRange != null) {
51-
type.setTrackingRange(trackingRange);
51+
type.patchwork$setTrackingRange(trackingRange);
5252
}
5353

5454
if (shouldRecieveVelocityUpdates != null) {
55-
type.setShouldReceiveVelocityUpdates(shouldRecieveVelocityUpdates);
55+
type.patchwork$setShouldReceiveVelocityUpdates(shouldRecieveVelocityUpdates);
5656
}
5757
}
5858

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
archivesBaseName = "patchwork-networking-messages"
2+
version = getSubprojectVersion(project, "0.1.0")
3+
4+
dependencies {
5+
compile project(path: ':patchwork-fml', configuration: 'dev')
6+
compile project(path: ':patchwork-networking', configuration: 'dev')
7+
}

patchwork-networking/src/main/java/com/patchworkmc/impl/networking/VersionedChannel.java renamed to patchwork-networking-messages/src/main/java/com/patchworkmc/impl/networking/ClientEntitySpawner.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@
1919

2020
package com.patchworkmc.impl.networking;
2121

22-
public interface VersionedChannel extends NamedChannel {
23-
String getNetworkProtocolVersion();
24-
boolean tryServerVersionOnClient(String serverVersion);
25-
boolean tryClientVersionOnServer(String clientVersion);
22+
import java.util.function.BiFunction;
23+
24+
import net.minecraftforge.fml.network.FMLPlayMessages;
25+
26+
import net.minecraft.entity.Entity;
27+
import net.minecraft.world.World;
28+
29+
public interface ClientEntitySpawner<T extends Entity> {
30+
T customClientSpawn(FMLPlayMessages.SpawnEntity packet, World world);
31+
void patchwork$setCustomClientFactory(BiFunction<FMLPlayMessages.SpawnEntity, World, T> customClientFactory);
2632
}

patchwork-networking/src/main/java/com/patchworkmc/impl/networking/NamedChannel.java renamed to patchwork-networking-messages/src/main/java/com/patchworkmc/impl/networking/ContainerSyncAccess.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package com.patchworkmc.impl.networking;
2121

22-
import net.minecraft.util.Identifier;
23-
24-
public interface NamedChannel {
25-
Identifier getChannelName();
22+
public interface ContainerSyncAccess {
23+
int patchwork$getNewContainerSyncId();
2624
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
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.Consumer;
23+
24+
import io.netty.buffer.Unpooled;
25+
import net.minecraftforge.fml.network.FMLPlayMessages;
26+
import org.apache.logging.log4j.LogManager;
27+
import org.apache.logging.log4j.Logger;
28+
29+
import net.minecraft.container.Container;
30+
import net.minecraft.container.ContainerType;
31+
import net.minecraft.container.NameableContainerProvider;
32+
import net.minecraft.entity.Entity;
33+
import net.minecraft.network.Packet;
34+
import net.minecraft.server.network.ServerPlayerEntity;
35+
import net.minecraft.text.LiteralText;
36+
import net.minecraft.util.Identifier;
37+
import net.minecraft.util.PacketByteBuf;
38+
39+
import net.fabricmc.api.ModInitializer;
40+
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
41+
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
42+
43+
public class PatchworkPlayNetworkingMessages implements ModInitializer, MessageFactory {
44+
private static final Logger LOGGER = LogManager.getLogger("patchwork-networking");
45+
private static final Identifier IDENTIFIER = new Identifier("fml", "play");
46+
private static final NetworkChannelVersion VERSION = new NetworkChannelVersion("FML2", version -> true, version -> true);
47+
private static final short SPAWN_ENTITY = 0;
48+
private static final short OPEN_CONTAINER = 1;
49+
50+
@Override
51+
public void onInitialize() {
52+
PatchworkNetworking.getVersionManager().createChannel(IDENTIFIER, VERSION);
53+
PatchworkNetworking.setFactory(this);
54+
55+
// TODO: Move to client initializer
56+
ClientSidePacketRegistry.INSTANCE.register(IDENTIFIER, (context, buf) -> {
57+
int id = buf.readUnsignedByte();
58+
59+
if (id == SPAWN_ENTITY) {
60+
FMLPlayMessages.SpawnEntity spawn = FMLPlayMessages.SpawnEntity.decode(buf);
61+
FMLPlayMessages.SpawnEntity.handle(spawn, context);
62+
} else if (id == OPEN_CONTAINER) {
63+
FMLPlayMessages.OpenContainer open = FMLPlayMessages.OpenContainer.decode(buf);
64+
FMLPlayMessages.OpenContainer.handle(open, context);
65+
} else {
66+
LOGGER.warn("Received an unknown fml:play message with an id of {} and a payload of {} bytes", id, buf.readableBytes());
67+
}
68+
});
69+
70+
ServerSidePacketRegistry.INSTANCE.register(IDENTIFIER, (context, buf) -> {
71+
LOGGER.warn("Received an fml:play on the server, this should not happen! Kicking the offending client.");
72+
73+
ServerPlayerEntity entity = (ServerPlayerEntity) context.getPlayer();
74+
75+
entity.networkHandler.disconnect(new LiteralText("fml:play messages should only be sent to the client!"));
76+
});
77+
}
78+
79+
@Override
80+
public Packet<?> getEntitySpawningPacket(Entity entity) {
81+
FMLPlayMessages.SpawnEntity message = new FMLPlayMessages.SpawnEntity(entity);
82+
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
83+
84+
buf.writeByte(SPAWN_ENTITY);
85+
FMLPlayMessages.SpawnEntity.encode(message, buf);
86+
87+
return ServerSidePacketRegistry.INSTANCE.toPacket(IDENTIFIER, buf);
88+
}
89+
90+
@Override
91+
public void sendContainerOpenPacket(ServerPlayerEntity player, NameableContainerProvider provider, Consumer<PacketByteBuf> extraDataWriter) {
92+
if (player.world.isClient) {
93+
return;
94+
}
95+
96+
player.method_14247();
97+
98+
ContainerSyncAccess access = (ContainerSyncAccess) player;
99+
int openContainerId = access.patchwork$getNewContainerSyncId();
100+
101+
PacketByteBuf extraData = new PacketByteBuf(Unpooled.buffer());
102+
extraDataWriter.accept(extraData);
103+
104+
// reset to beginning in case modders read for whatever reason
105+
extraData.readerIndex(0);
106+
107+
PacketByteBuf output = new PacketByteBuf(Unpooled.buffer());
108+
output.writeVarInt(extraData.readableBytes());
109+
output.writeBytes(extraData);
110+
111+
if (output.readableBytes() > 32600 || output.readableBytes() < 1) {
112+
throw new IllegalArgumentException("Invalid PacketByteBuf for openGui, found " + output.readableBytes() + " bytes");
113+
}
114+
115+
Container c = provider.createMenu(openContainerId, player.inventory, player);
116+
ContainerType<?> type = c.getType();
117+
118+
FMLPlayMessages.OpenContainer msg = new FMLPlayMessages.OpenContainer(type, openContainerId, provider.getDisplayName(), output);
119+
Packet<?> packet = PatchworkPlayNetworkingMessages.getOpenContainerPacket(msg);
120+
121+
player.networkHandler.sendPacket(packet);
122+
player.container = c;
123+
player.container.addListener(player);
124+
125+
// TODO MinecraftForge.EVENT_BUS.post(new PlayerContainerEvent.Open(player, c));
126+
}
127+
128+
private static Packet<?> getOpenContainerPacket(FMLPlayMessages.OpenContainer message) {
129+
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
130+
131+
buf.writeByte(OPEN_CONTAINER);
132+
FMLPlayMessages.OpenContainer.encode(message, buf);
133+
134+
return ServerSidePacketRegistry.INSTANCE.toPacket(IDENTIFIER, buf);
135+
}
136+
}

0 commit comments

Comments
 (0)