forked from FabricMC/fabric
Add checkstyle, format existing code accordingly.
This commit is contained in:
parent
d1753d4af9
commit
dfdb52d6e5
301 changed files with 3209 additions and 2520 deletions
fabric-networking-v0/src/main/java/net/fabricmc/fabric
api
event/network
network
server
impl
network
ClientSidePacketRegistryImpl.javaPacketDebugOptions.javaPacketRegistryImpl.javaServerSidePacketRegistryImpl.java
server
mixin/network
|
@ -16,12 +16,13 @@
|
|||
|
||||
package net.fabricmc.fabric.api.event.network;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import java.util.Collection;
|
||||
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
import java.util.Collection;
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
|
||||
/**
|
||||
* Event for listening to packet type registrations and unregistrations
|
||||
|
@ -29,22 +30,22 @@ import java.util.Collection;
|
|||
* in the client -> server direction.
|
||||
*/
|
||||
public interface C2SPacketTypeCallback {
|
||||
static final Event<C2SPacketTypeCallback> REGISTERED = EventFactory.createArrayBacked(
|
||||
C2SPacketTypeCallback.class,
|
||||
(callbacks) -> (client, types) -> {
|
||||
for (C2SPacketTypeCallback callback : callbacks) {
|
||||
callback.accept(client, types);
|
||||
Event<C2SPacketTypeCallback> REGISTERED = EventFactory.createArrayBacked(
|
||||
C2SPacketTypeCallback.class,
|
||||
(callbacks) -> (client, types) -> {
|
||||
for (C2SPacketTypeCallback callback : callbacks) {
|
||||
callback.accept(client, types);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
static final Event<C2SPacketTypeCallback> UNREGISTERED = EventFactory.createArrayBacked(
|
||||
C2SPacketTypeCallback.class,
|
||||
(callbacks) -> (client, types) -> {
|
||||
for (C2SPacketTypeCallback callback : callbacks) {
|
||||
callback.accept(client, types);
|
||||
Event<C2SPacketTypeCallback> UNREGISTERED = EventFactory.createArrayBacked(
|
||||
C2SPacketTypeCallback.class,
|
||||
(callbacks) -> (client, types) -> {
|
||||
for (C2SPacketTypeCallback callback : callbacks) {
|
||||
callback.accept(client, types);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,11 +16,12 @@
|
|||
|
||||
package net.fabricmc.fabric.api.event.network;
|
||||
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
import java.util.Collection;
|
||||
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
import java.util.Collection;
|
||||
import net.fabricmc.fabric.api.event.Event;
|
||||
import net.fabricmc.fabric.api.event.EventFactory;
|
||||
|
||||
/**
|
||||
* Event for listening to packet type registrations and unregistrations
|
||||
|
@ -28,22 +29,22 @@ import java.util.Collection;
|
|||
* in the server -> client direction.
|
||||
*/
|
||||
public interface S2CPacketTypeCallback {
|
||||
static final Event<S2CPacketTypeCallback> REGISTERED = EventFactory.createArrayBacked(
|
||||
S2CPacketTypeCallback.class,
|
||||
(callbacks) -> (types) -> {
|
||||
for (S2CPacketTypeCallback callback : callbacks) {
|
||||
callback.accept(types);
|
||||
Event<S2CPacketTypeCallback> REGISTERED = EventFactory.createArrayBacked(
|
||||
S2CPacketTypeCallback.class,
|
||||
(callbacks) -> (types) -> {
|
||||
for (S2CPacketTypeCallback callback : callbacks) {
|
||||
callback.accept(types);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
static final Event<S2CPacketTypeCallback> UNREGISTERED = EventFactory.createArrayBacked(
|
||||
S2CPacketTypeCallback.class,
|
||||
(callbacks) -> (types) -> {
|
||||
for (S2CPacketTypeCallback callback : callbacks) {
|
||||
callback.accept(types);
|
||||
Event<S2CPacketTypeCallback> UNREGISTERED = EventFactory.createArrayBacked(
|
||||
S2CPacketTypeCallback.class,
|
||||
(callbacks) -> (types) -> {
|
||||
for (S2CPacketTypeCallback callback : callbacks) {
|
||||
callback.accept(types);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,21 +18,23 @@ package net.fabricmc.fabric.api.network;
|
|||
|
||||
import io.netty.util.concurrent.Future;
|
||||
import io.netty.util.concurrent.GenericFutureListener;
|
||||
import net.fabricmc.fabric.impl.network.ClientSidePacketRegistryImpl;
|
||||
|
||||
import net.minecraft.network.Packet;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.PacketByteBuf;
|
||||
|
||||
import net.fabricmc.fabric.impl.network.ClientSidePacketRegistryImpl;
|
||||
|
||||
/**
|
||||
* The client-side packet registry.
|
||||
* <p>
|
||||
* It is used for:
|
||||
* <p>
|
||||
* - registering client-side packet receivers (server -> client packets)
|
||||
* - sending packets to the server (client -> server packets).
|
||||
*
|
||||
* <p>It is used for:
|
||||
*
|
||||
* <ul><li>registering client-side packet receivers (server -> client packets)
|
||||
* <li>sending packets to the server (client -> server packets).</ul>
|
||||
*/
|
||||
public interface ClientSidePacketRegistry extends PacketRegistry {
|
||||
static final ClientSidePacketRegistry INSTANCE = new ClientSidePacketRegistryImpl();
|
||||
ClientSidePacketRegistry INSTANCE = new ClientSidePacketRegistryImpl();
|
||||
|
||||
/**
|
||||
* Check if the server declared the ability to receive a given packet ID
|
||||
|
|
|
@ -26,11 +26,11 @@ public interface PacketConsumer {
|
|||
/**
|
||||
* Receive a CustomPayload-based packet.
|
||||
*
|
||||
* The PacketByteBuf received will be released as soon as the method exits,
|
||||
* <p>The PacketByteBuf received will be released as soon as the method exits,
|
||||
* meaning that you have to call .retain()/.release() on it if you want to
|
||||
* keep it around after that.
|
||||
*
|
||||
* Please keep in mind that this CAN be called OUTSIDE of the main thread!
|
||||
* <p>Please keep in mind that this CAN be called OUTSIDE of the main thread!
|
||||
* Most game operations are not thread-safe, so you should look into using
|
||||
* the thread task queue ({@link PacketContext#getTaskQueue()}) to split
|
||||
* the "reading" (which should happen within this method's execution)
|
||||
|
|
|
@ -16,10 +16,11 @@
|
|||
|
||||
package net.fabricmc.fabric.api.network;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.util.ThreadExecutor;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
|
||||
/**
|
||||
* Interface defining a context used during packet processing. Allows access
|
||||
* to additional information, such as the source/target of the player, or
|
||||
|
@ -36,8 +37,8 @@ public interface PacketContext {
|
|||
|
||||
/**
|
||||
* Get the player associated with the packet.
|
||||
* <p>
|
||||
* On the client side, this always returns the client-side player instance.
|
||||
*
|
||||
* <p>On the client side, this always returns the client-side player instance.
|
||||
* On the server side, it returns the player belonging to the client this
|
||||
* packet was sent by.
|
||||
*
|
||||
|
@ -47,15 +48,15 @@ public interface PacketContext {
|
|||
|
||||
/**
|
||||
* Get the task queue for a given side.
|
||||
* <p>
|
||||
* As Minecraft networking I/O is asynchronous, but a lot of its logic is
|
||||
*
|
||||
* <p>As Minecraft networking I/O is asynchronous, but a lot of its logic is
|
||||
* not thread-safe, it is recommended to do the following:
|
||||
* <p>
|
||||
* - read and parse the PacketByteBuf,
|
||||
* - run the packet response logic through the main thread task queue via
|
||||
*
|
||||
* <ul><li>read and parse the PacketByteBuf,
|
||||
* <li>run the packet response logic through the main thread task queue via
|
||||
* ThreadTaskQueue.execute(). The method will check if it's not already
|
||||
* on the main thread in order to avoid unnecessary delays, so don't
|
||||
* worry about that!
|
||||
* worry about that!</ul>
|
||||
*
|
||||
* @return The thread task queue.
|
||||
*/
|
||||
|
|
|
@ -18,25 +18,27 @@ package net.fabricmc.fabric.api.network;
|
|||
|
||||
import io.netty.util.concurrent.Future;
|
||||
import io.netty.util.concurrent.GenericFutureListener;
|
||||
import net.fabricmc.fabric.api.server.PlayerStream;
|
||||
import net.fabricmc.fabric.impl.network.ServerSidePacketRegistryImpl;
|
||||
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.network.Packet;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.PacketByteBuf;
|
||||
|
||||
import net.fabricmc.fabric.api.server.PlayerStream;
|
||||
import net.fabricmc.fabric.impl.network.ServerSidePacketRegistryImpl;
|
||||
|
||||
/**
|
||||
* The server-side packet registry.
|
||||
* <p>
|
||||
* It is used for:
|
||||
* <p>
|
||||
* - registering server-side packet receivers (client -> server packets)
|
||||
* - sending packets to clients (server -> client packets).
|
||||
* <p>
|
||||
* For iterating over clients in a server, see {@link PlayerStream}.
|
||||
*
|
||||
* <p>It is used for:
|
||||
*
|
||||
* <ul><li>registering server-side packet receivers (client -> server packets)
|
||||
* <li>sending packets to clients (server -> client packets).</ul>
|
||||
*
|
||||
* <p>For iterating over clients in a server, see {@link PlayerStream}.
|
||||
*/
|
||||
public interface ServerSidePacketRegistry extends PacketRegistry {
|
||||
static final ServerSidePacketRegistry INSTANCE = new ServerSidePacketRegistryImpl();
|
||||
ServerSidePacketRegistry INSTANCE = new ServerSidePacketRegistryImpl();
|
||||
|
||||
/**
|
||||
* Check if a given client declared the ability to receive a given packet ID
|
||||
|
|
|
@ -16,7 +16,8 @@
|
|||
|
||||
package net.fabricmc.fabric.api.server;
|
||||
|
||||
import net.fabricmc.fabric.impl.server.EntityTrackerStorageAccessor;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import net.minecraft.block.entity.BlockEntity;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
|
@ -31,17 +32,15 @@ import net.minecraft.util.math.Vec3d;
|
|||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.ChunkManager;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
import net.fabricmc.fabric.impl.server.EntityTrackerStorageAccessor;
|
||||
|
||||
/**
|
||||
* Helper streams for looking up players on a server.
|
||||
* <p>
|
||||
* In general, most of these methods will only function with a {@link ServerWorld} instance.
|
||||
*
|
||||
* <p>In general, most of these methods will only function with a {@link ServerWorld} instance.
|
||||
*/
|
||||
public final class PlayerStream {
|
||||
private PlayerStream() {
|
||||
|
||||
}
|
||||
private PlayerStream() { }
|
||||
|
||||
public static Stream<ServerPlayerEntity> all(MinecraftServer server) {
|
||||
if (server.getPlayerManager() != null) {
|
||||
|
@ -54,7 +53,7 @@ public final class PlayerStream {
|
|||
public static Stream<PlayerEntity> world(World world) {
|
||||
if (world instanceof ServerWorld) {
|
||||
// noinspection unchecked
|
||||
return ((Stream<PlayerEntity>) (Stream) ((ServerWorld) world).getPlayers().stream());
|
||||
return ((Stream) ((ServerWorld) world).getPlayers().stream());
|
||||
} else {
|
||||
throw new RuntimeException("Only supported on ServerWorld!");
|
||||
}
|
||||
|
@ -62,11 +61,12 @@ public final class PlayerStream {
|
|||
|
||||
public static Stream<PlayerEntity> watching(World world, ChunkPos pos) {
|
||||
ChunkManager manager = world.getChunkManager();
|
||||
|
||||
if (!(manager instanceof ServerChunkManager)) {
|
||||
throw new RuntimeException("Only supported on ServerWorld!");
|
||||
} else {
|
||||
//noinspection unchecked
|
||||
return ((Stream<PlayerEntity>) (Stream) ((ServerChunkManager) manager).threadedAnvilChunkStorage.getPlayersWatchingChunk(pos, false));
|
||||
return ((Stream) ((ServerChunkManager) manager).threadedAnvilChunkStorage.getPlayersWatchingChunk(pos, false));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,9 +81,10 @@ public final class PlayerStream {
|
|||
|
||||
if (manager instanceof ServerChunkManager) {
|
||||
ThreadedAnvilChunkStorage storage = ((ServerChunkManager) manager).threadedAnvilChunkStorage;
|
||||
|
||||
if (storage instanceof EntityTrackerStorageAccessor) {
|
||||
//noinspection unchecked
|
||||
return ((Stream<PlayerEntity>) (Stream) ((EntityTrackerStorageAccessor) storage).fabric_getTrackingPlayers(entity));
|
||||
return ((Stream) ((EntityTrackerStorageAccessor) storage).fabric_getTrackingPlayers(entity));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,11 +16,13 @@
|
|||
|
||||
package net.fabricmc.fabric.impl.network;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
|
||||
import io.netty.util.concurrent.Future;
|
||||
import io.netty.util.concurrent.GenericFutureListener;
|
||||
import net.fabricmc.fabric.api.event.network.S2CPacketTypeCallback;
|
||||
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
|
||||
import net.fabricmc.fabric.api.network.PacketContext;
|
||||
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.network.ClientPlayNetworkHandler;
|
||||
import net.minecraft.network.Packet;
|
||||
|
@ -28,9 +30,9 @@ import net.minecraft.server.network.packet.CustomPayloadC2SPacket;
|
|||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.PacketByteBuf;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import net.fabricmc.fabric.api.event.network.S2CPacketTypeCallback;
|
||||
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
|
||||
import net.fabricmc.fabric.api.network.PacketContext;
|
||||
|
||||
public class ClientSidePacketRegistryImpl extends PacketRegistryImpl implements ClientSidePacketRegistry {
|
||||
private final Collection<Identifier> serverPayloadIds = new HashSet<>();
|
||||
|
@ -47,6 +49,7 @@ public class ClientSidePacketRegistryImpl extends PacketRegistryImpl implements
|
|||
@Override
|
||||
public void sendToServer(Packet<?> packet, GenericFutureListener<? extends Future<? super Void>> completionListener) {
|
||||
ClientPlayNetworkHandler handler = MinecraftClient.getInstance().getNetworkHandler();
|
||||
|
||||
if (handler != null) {
|
||||
if (completionListener == null) {
|
||||
// stay closer to the vanilla codepath
|
||||
|
@ -67,6 +70,7 @@ public class ClientSidePacketRegistryImpl extends PacketRegistryImpl implements
|
|||
@Override
|
||||
protected void onRegister(Identifier id) {
|
||||
ClientPlayNetworkHandler handler = MinecraftClient.getInstance().getNetworkHandler();
|
||||
|
||||
if (handler != null) {
|
||||
createRegisterTypePacket(PacketTypes.REGISTER, Collections.singleton(id)).ifPresent(handler::sendPacket);
|
||||
}
|
||||
|
@ -75,6 +79,7 @@ public class ClientSidePacketRegistryImpl extends PacketRegistryImpl implements
|
|||
@Override
|
||||
protected void onUnregister(Identifier id) {
|
||||
ClientPlayNetworkHandler handler = MinecraftClient.getInstance().getNetworkHandler();
|
||||
|
||||
if (handler != null) {
|
||||
createRegisterTypePacket(PacketTypes.UNREGISTER, Collections.singleton(id)).ifPresent(handler::sendPacket);
|
||||
}
|
||||
|
|
|
@ -19,7 +19,5 @@ package net.fabricmc.fabric.impl.network;
|
|||
public final class PacketDebugOptions {
|
||||
public static final boolean DISABLE_BUFFER_RELEASES = System.getProperty("fabric.networking.broken.disableBufferReleases", "false").equalsIgnoreCase("true");
|
||||
|
||||
private PacketDebugOptions() {
|
||||
|
||||
}
|
||||
private PacketDebugOptions() { }
|
||||
}
|
||||
|
|
|
@ -16,20 +16,26 @@
|
|||
|
||||
package net.fabricmc.fabric.impl.network;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.fabricmc.fabric.api.network.PacketConsumer;
|
||||
import net.fabricmc.fabric.api.network.PacketContext;
|
||||
import net.fabricmc.fabric.api.network.PacketRegistry;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import net.minecraft.network.Packet;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.InvalidIdentifierException;
|
||||
import net.minecraft.util.PacketByteBuf;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.function.Supplier;
|
||||
import net.fabricmc.fabric.api.network.PacketConsumer;
|
||||
import net.fabricmc.fabric.api.network.PacketContext;
|
||||
import net.fabricmc.fabric.api.network.PacketRegistry;
|
||||
|
||||
public abstract class PacketRegistryImpl implements PacketRegistry {
|
||||
protected static final Logger LOGGER = LogManager.getLogger();
|
||||
|
@ -47,6 +53,7 @@ public abstract class PacketRegistryImpl implements PacketRegistry {
|
|||
@Override
|
||||
public void register(Identifier id, PacketConsumer consumer) {
|
||||
boolean isNew = true;
|
||||
|
||||
if (consumerMap.containsKey(id)) {
|
||||
LOGGER.warn("Registered duplicate packet " + id + "!");
|
||||
LOGGER.trace(new Throwable());
|
||||
|
@ -54,6 +61,7 @@ public abstract class PacketRegistryImpl implements PacketRegistry {
|
|||
}
|
||||
|
||||
consumerMap.put(id, consumer);
|
||||
|
||||
if (isNew) {
|
||||
onRegister(id);
|
||||
}
|
||||
|
@ -86,14 +94,17 @@ public abstract class PacketRegistryImpl implements PacketRegistry {
|
|||
|
||||
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
|
||||
boolean first = true;
|
||||
|
||||
for (Identifier a : ids) {
|
||||
if (!first) {
|
||||
buf.writeByte(0);
|
||||
} else {
|
||||
first = false;
|
||||
}
|
||||
|
||||
buf.writeBytes(a.toString().getBytes(StandardCharsets.US_ASCII));
|
||||
}
|
||||
|
||||
return Optional.of(toPacket(id, buf));
|
||||
}
|
||||
|
||||
|
@ -108,8 +119,10 @@ public abstract class PacketRegistryImpl implements PacketRegistry {
|
|||
try {
|
||||
while (buf.readerIndex() < buf.writerIndex()) {
|
||||
c = (char) buf.readByte();
|
||||
|
||||
if (c == 0) {
|
||||
String s = sb.toString();
|
||||
|
||||
if (!s.isEmpty()) {
|
||||
try {
|
||||
ids.add(new Identifier(s));
|
||||
|
@ -118,6 +131,7 @@ public abstract class PacketRegistryImpl implements PacketRegistry {
|
|||
LOGGER.trace(e);
|
||||
}
|
||||
}
|
||||
|
||||
sb = new StringBuilder();
|
||||
} else {
|
||||
sb.append(c);
|
||||
|
@ -128,6 +142,7 @@ public abstract class PacketRegistryImpl implements PacketRegistry {
|
|||
}
|
||||
|
||||
String s = sb.toString();
|
||||
|
||||
if (!s.isEmpty()) {
|
||||
try {
|
||||
ids.add(new Identifier(s));
|
||||
|
@ -139,6 +154,7 @@ public abstract class PacketRegistryImpl implements PacketRegistry {
|
|||
}
|
||||
|
||||
Collection<Identifier> target = getIdCollectionFor(context);
|
||||
|
||||
if (id.equals(PacketTypes.UNREGISTER)) {
|
||||
target.removeAll(ids);
|
||||
onReceivedUnregisterPacket(context, ids);
|
||||
|
@ -146,13 +162,14 @@ public abstract class PacketRegistryImpl implements PacketRegistry {
|
|||
target.addAll(ids);
|
||||
onReceivedRegisterPacket(context, ids);
|
||||
}
|
||||
|
||||
return false; // continue execution for other mods
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for accepting packets used in Fabric mixins.
|
||||
*
|
||||
* As PacketByteBuf getters in vanilla create a copy (to allow releasing the original packet buffer without
|
||||
* <p>As PacketByteBuf getters in vanilla create a copy (to allow releasing the original packet buffer without
|
||||
* breaking other, potentially delayed accesses), we use a Supplier to generate those copies and release them
|
||||
* when needed.
|
||||
*
|
||||
|
@ -167,8 +184,10 @@ public abstract class PacketRegistryImpl implements PacketRegistry {
|
|||
}
|
||||
|
||||
PacketConsumer consumer = consumerMap.get(id);
|
||||
|
||||
if (consumer != null) {
|
||||
PacketByteBuf buf = bufSupplier.get();
|
||||
|
||||
try {
|
||||
consumer.accept(context, buf);
|
||||
} catch (Throwable t) {
|
||||
|
@ -178,6 +197,7 @@ public abstract class PacketRegistryImpl implements PacketRegistry {
|
|||
buf.release();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
|
|
@ -16,11 +16,18 @@
|
|||
|
||||
package net.fabricmc.fabric.impl.network;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import io.netty.util.concurrent.Future;
|
||||
import io.netty.util.concurrent.GenericFutureListener;
|
||||
import net.fabricmc.fabric.api.event.network.C2SPacketTypeCallback;
|
||||
import net.fabricmc.fabric.api.network.PacketContext;
|
||||
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
|
||||
|
||||
import net.minecraft.client.network.packet.CustomPayloadS2CPacket;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.network.Packet;
|
||||
|
@ -30,9 +37,9 @@ import net.minecraft.server.network.packet.LoginQueryResponseC2SPacket;
|
|||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.PacketByteBuf;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import net.fabricmc.fabric.api.event.network.C2SPacketTypeCallback;
|
||||
import net.fabricmc.fabric.api.network.PacketContext;
|
||||
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
|
||||
|
||||
public class ServerSidePacketRegistryImpl extends PacketRegistryImpl implements ServerSidePacketRegistry {
|
||||
private final WeakHashMap<PlayerEntity, Collection<Identifier>> playerPayloadIds = new WeakHashMap<>();
|
||||
|
@ -47,8 +54,10 @@ public class ServerSidePacketRegistryImpl extends PacketRegistryImpl implements
|
|||
|
||||
protected void forEachHandler(Consumer<ServerPlayNetworkHandler> consumer) {
|
||||
Iterator<WeakReference<ServerPlayNetworkHandler>> it = handlers.iterator();
|
||||
|
||||
while (it.hasNext()) {
|
||||
ServerPlayNetworkHandler server = it.next().get();
|
||||
|
||||
if (server != null) {
|
||||
consumer.accept(server);
|
||||
} else {
|
||||
|
@ -60,6 +69,7 @@ public class ServerSidePacketRegistryImpl extends PacketRegistryImpl implements
|
|||
@Override
|
||||
public boolean canPlayerReceive(PlayerEntity player, Identifier id) {
|
||||
Collection<Identifier> ids = playerPayloadIds.get(player);
|
||||
|
||||
if (ids != null) {
|
||||
return ids.contains(id);
|
||||
} else {
|
||||
|
|
|
@ -16,11 +16,11 @@
|
|||
|
||||
package net.fabricmc.fabric.impl.server;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public interface EntityTrackerStorageAccessor {
|
||||
Stream<ServerPlayerEntity> fabric_getTrackingPlayers(Entity entity);
|
||||
}
|
||||
|
|
|
@ -16,10 +16,10 @@
|
|||
|
||||
package net.fabricmc.fabric.impl.server;
|
||||
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
|
||||
public interface EntityTrackerStreamAccessor {
|
||||
Stream<ServerPlayerEntity> fabric_getTrackingPlayers();
|
||||
}
|
||||
|
|
|
@ -16,12 +16,15 @@
|
|||
|
||||
package net.fabricmc.fabric.mixin.network;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
|
||||
import net.fabricmc.fabric.api.network.PacketContext;
|
||||
import net.fabricmc.fabric.impl.network.ClientSidePacketRegistryImpl;
|
||||
import net.fabricmc.fabric.impl.network.PacketRegistryImpl;
|
||||
import net.fabricmc.fabric.impl.network.PacketTypes;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
|
||||
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.network.ClientPlayNetworkHandler;
|
||||
import net.minecraft.client.network.packet.CustomPayloadS2CPacket;
|
||||
|
@ -31,14 +34,13 @@ import net.minecraft.network.Packet;
|
|||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.PacketByteBuf;
|
||||
import net.minecraft.util.ThreadExecutor;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
|
||||
|
||||
import java.util.Optional;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
|
||||
import net.fabricmc.fabric.api.network.PacketContext;
|
||||
import net.fabricmc.fabric.impl.network.ClientSidePacketRegistryImpl;
|
||||
import net.fabricmc.fabric.impl.network.PacketRegistryImpl;
|
||||
import net.fabricmc.fabric.impl.network.PacketTypes;
|
||||
|
||||
@Mixin(ClientPlayNetworkHandler.class)
|
||||
public abstract class MixinClientPlayNetworkHandler implements PacketContext {
|
||||
|
@ -51,6 +53,7 @@ public abstract class MixinClientPlayNetworkHandler implements PacketContext {
|
|||
@Inject(at = @At("RETURN"), method = "onGameJoin")
|
||||
public void onGameJoin(GameJoinS2CPacket packet, CallbackInfo info) {
|
||||
Optional<Packet<?>> optionalPacket = PacketRegistryImpl.createInitialRegisterPacket(ClientSidePacketRegistry.INSTANCE);
|
||||
|
||||
//noinspection OptionalIsPresent
|
||||
if (optionalPacket.isPresent()) {
|
||||
sendPacket(optionalPacket.get());
|
||||
|
|
|
@ -16,12 +16,14 @@
|
|||
|
||||
package net.fabricmc.fabric.mixin.network;
|
||||
|
||||
import net.fabricmc.fabric.impl.network.CustomPayloadC2SPacketAccessor;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
|
||||
import net.minecraft.server.network.packet.CustomPayloadC2SPacket;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.PacketByteBuf;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
|
||||
import net.fabricmc.fabric.impl.network.CustomPayloadC2SPacketAccessor;
|
||||
|
||||
@Mixin(CustomPayloadC2SPacket.class)
|
||||
public class MixinCustomPayloadC2SPacket implements CustomPayloadC2SPacketAccessor {
|
||||
|
|
|
@ -16,14 +16,16 @@
|
|||
|
||||
package net.fabricmc.fabric.mixin.network;
|
||||
|
||||
import net.fabricmc.fabric.impl.server.EntityTrackerStreamAccessor;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
|
||||
import net.fabricmc.fabric.impl.server.EntityTrackerStreamAccessor;
|
||||
|
||||
@Mixin(targets = "net.minecraft.server.world.ThreadedAnvilChunkStorage$EntityTracker")
|
||||
public class MixinEntityTracker implements EntityTrackerStreamAccessor {
|
||||
|
|
|
@ -16,14 +16,16 @@
|
|||
|
||||
package net.fabricmc.fabric.mixin.network;
|
||||
|
||||
import net.fabricmc.fabric.impl.network.ClientSidePacketRegistryImpl;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
|
||||
import net.fabricmc.fabric.impl.network.ClientSidePacketRegistryImpl;
|
||||
|
||||
@Mixin(MinecraftClient.class)
|
||||
public class MixinMinecraftClient {
|
||||
@Inject(at = @At("RETURN"), method = "disconnect(Lnet/minecraft/client/gui/screen/Screen;)V")
|
||||
|
|
|
@ -16,25 +16,28 @@
|
|||
|
||||
package net.fabricmc.fabric.mixin.network;
|
||||
|
||||
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
|
||||
import net.fabricmc.fabric.impl.network.PacketRegistryImpl;
|
||||
import net.fabricmc.fabric.impl.network.ServerSidePacketRegistryImpl;
|
||||
import net.minecraft.network.ClientConnection;
|
||||
import net.minecraft.network.Packet;
|
||||
import net.minecraft.server.PlayerManager;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import java.util.Optional;
|
||||
import net.minecraft.network.ClientConnection;
|
||||
import net.minecraft.network.Packet;
|
||||
import net.minecraft.server.PlayerManager;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
|
||||
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
|
||||
import net.fabricmc.fabric.impl.network.PacketRegistryImpl;
|
||||
import net.fabricmc.fabric.impl.network.ServerSidePacketRegistryImpl;
|
||||
|
||||
@Mixin(priority = 500, value = PlayerManager.class)
|
||||
public abstract class MixinPlayerManager {
|
||||
@Inject(method = "onPlayerConnect", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/packet/DifficultyS2CPacket;<init>(Lnet/minecraft/world/Difficulty;Z)V"))
|
||||
public void onPlayerConnect(ClientConnection connection, ServerPlayerEntity player, CallbackInfo info) {
|
||||
Optional<Packet<?>> optionalPacket = PacketRegistryImpl.createInitialRegisterPacket(ServerSidePacketRegistry.INSTANCE);
|
||||
|
||||
//noinspection OptionalIsPresent
|
||||
if (optionalPacket.isPresent()) {
|
||||
player.networkHandler.sendPacket(optionalPacket.get());
|
||||
|
|
|
@ -16,11 +16,12 @@
|
|||
|
||||
package net.fabricmc.fabric.mixin.network;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.fabric.api.network.PacketContext;
|
||||
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
|
||||
import net.fabricmc.fabric.impl.network.CustomPayloadC2SPacketAccessor;
|
||||
import net.fabricmc.fabric.impl.network.ServerSidePacketRegistryImpl;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.network.ServerPlayNetworkHandler;
|
||||
|
@ -28,11 +29,12 @@ import net.minecraft.server.network.ServerPlayerEntity;
|
|||
import net.minecraft.server.network.packet.CustomPayloadC2SPacket;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.ThreadExecutor;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.fabric.api.network.PacketContext;
|
||||
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
|
||||
import net.fabricmc.fabric.impl.network.CustomPayloadC2SPacketAccessor;
|
||||
import net.fabricmc.fabric.impl.network.ServerSidePacketRegistryImpl;
|
||||
|
||||
@Mixin(ServerPlayNetworkHandler.class)
|
||||
public class MixinServerPlayNetworkHandler implements PacketContext {
|
||||
|
|
|
@ -16,17 +16,19 @@
|
|||
|
||||
package net.fabricmc.fabric.mixin.network;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import net.fabricmc.fabric.impl.server.EntityTrackerStorageAccessor;
|
||||
import net.fabricmc.fabric.impl.server.EntityTrackerStreamAccessor;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.server.world.ThreadedAnvilChunkStorage;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.server.world.ThreadedAnvilChunkStorage;
|
||||
|
||||
import net.fabricmc.fabric.impl.server.EntityTrackerStorageAccessor;
|
||||
import net.fabricmc.fabric.impl.server.EntityTrackerStreamAccessor;
|
||||
|
||||
@Mixin(ThreadedAnvilChunkStorage.class)
|
||||
public class MixinThreadedAnvilChunkStorage implements EntityTrackerStorageAccessor {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue