mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-20 04:09:51 +00:00
1.21.6 - New EventBus 7
A full rewrite and redesign of the library that offers much better performance, new features and fixes all previous design flaws See the migration guide here: https://gist.github.com/PaintNinja/ad82c224aecee25efac1ea3e2cf19b91 Co-authored-by: Paint_Ninja <[email protected]>
This commit is contained in:
642 files changed
+7258
-6187
No files matched your search
@@ -27,20 +27,11 @@ public class EventNetworkChannel extends Channel<FriendlyByteBuf> {
|
||||
super(instance);
|
||||
}
|
||||
|
||||
public <T extends CustomPayloadEvent> EventNetworkChannel addListener(Consumer<T> eventListener) {
|
||||
public EventNetworkChannel addListener(Consumer<CustomPayloadEvent> eventListener) {
|
||||
instance.addListener(eventListener);
|
||||
return this;
|
||||
}
|
||||
|
||||
public EventNetworkChannel registerObject(Object object) {
|
||||
instance.registerObject(object);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void unregisterObject(Object object) {
|
||||
instance.unregisterObject(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(FriendlyByteBuf out, FriendlyByteBuf message) {
|
||||
out.writeBytes(message.slice());
|
||||
|
||||
@@ -8,12 +8,10 @@ package net.minecraftforge.network;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraftforge.event.network.CustomPayloadEvent;
|
||||
import net.minecraftforge.eventbus.api.BusBuilder;
|
||||
import net.minecraftforge.eventbus.api.Event;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.eventbus.api.IEventListener;
|
||||
import net.minecraftforge.network.Channel.VersionTest;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -32,7 +30,8 @@ import io.netty.util.AttributeKey;
|
||||
public final class NetworkInstance {
|
||||
// We use an event bus here so that we don't have to have a handle(event) public function on Channel.
|
||||
// Should this be changed so that modders can fire other channel's handlers?
|
||||
private final IEventBus networkEventBus;
|
||||
|
||||
private Collection<Consumer<CustomPayloadEvent>> listeners = new ArrayList<>();
|
||||
private final ResourceLocation channelName;
|
||||
private final int networkProtocolVersion;
|
||||
final VersionTest clientAcceptedVersions;
|
||||
@@ -52,27 +51,16 @@ public final class NetworkInstance {
|
||||
this.serverAcceptedVersions = serverAcceptedVersions;
|
||||
this.attributes = attributes;
|
||||
this.channelHandler = channelHandler;
|
||||
this.networkEventBus = BusBuilder.builder().setExceptionHandler(this::handleError).useModLauncher().build();
|
||||
this.pingData = new ServerStatusPing.ChannelData(channelName, networkProtocolVersion, this.clientAcceptedVersions.accepts(VersionTest.Status.MISSING, -1));
|
||||
}
|
||||
|
||||
private void handleError(IEventBus iEventBus, Event event, IEventListener[] iEventListeners, int i, Throwable throwable) {
|
||||
}
|
||||
|
||||
public <T extends CustomPayloadEvent> void addListener(Consumer<T> eventListener) {
|
||||
this.networkEventBus.addListener(eventListener);
|
||||
}
|
||||
|
||||
public void registerObject(final Object object) {
|
||||
this.networkEventBus.register(object);
|
||||
}
|
||||
|
||||
public void unregisterObject(final Object object) {
|
||||
this.networkEventBus.unregister(object);
|
||||
public void addListener(Consumer<CustomPayloadEvent> eventListener) {
|
||||
this.listeners.add(eventListener);
|
||||
}
|
||||
|
||||
public boolean dispatch(CustomPayloadEvent event) {
|
||||
this.networkEventBus.post(event);
|
||||
for (var listener : listeners)
|
||||
listener.accept(event);
|
||||
return event.getSource().getPacketHandled();
|
||||
}
|
||||
|
||||
|
||||
@@ -158,11 +158,11 @@ public record PacketDistributor<T>(BiFunction<PacketDistributor<T>, T, Consumer<
|
||||
}
|
||||
|
||||
private Consumer<Packet<?>> trackingEntity(Entity entity) {
|
||||
return p -> ((ServerChunkCache)entity.getCommandSenderWorld().getChunkSource()).broadcast(entity, p);
|
||||
return p -> ((ServerChunkCache)entity.level().getChunkSource()).broadcast(entity, p);
|
||||
}
|
||||
|
||||
private Consumer<Packet<?>> trackingEntityAndSelf(Entity entity) {
|
||||
return p -> ((ServerChunkCache)entity.getCommandSenderWorld().getChunkSource()).broadcastAndSend(entity, p);
|
||||
return p -> ((ServerChunkCache)entity.level().getChunkSource()).broadcastAndSend(entity, p);
|
||||
}
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.network.filters;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import com.mojang.brigadier.builder.ArgumentBuilder;
|
||||
import com.mojang.brigadier.tree.ArgumentCommandNode;
|
||||
import com.mojang.brigadier.tree.CommandNode;
|
||||
import com.mojang.brigadier.tree.RootCommandNode;
|
||||
|
||||
class CommandTreeCleaner
|
||||
{
|
||||
|
||||
/**
|
||||
* Cleans the command tree starting at the given root node from any argument types that do not match the given predicate.
|
||||
* Any {@code ArgumentCommandNode}s that have an unmatched argument type will be stripped from the tree.
|
||||
* @return A new command tree, stripped of any unmatched argument types
|
||||
*/
|
||||
public static <S> RootCommandNode<S> cleanArgumentTypes(RootCommandNode<S> root, Predicate<ArgumentType<?>> argumentTypeFilter)
|
||||
{
|
||||
Predicate<CommandNode<?>> nodeFilter = node -> !(node instanceof ArgumentCommandNode<?, ?>) || argumentTypeFilter.test(((ArgumentCommandNode<?, ?>)node).getType());
|
||||
return (RootCommandNode<S>)processCommandNode(root, nodeFilter, new HashMap<>());
|
||||
}
|
||||
|
||||
private static <S> CommandNode<S> processCommandNode(CommandNode<S> node, Predicate<CommandNode<?>> nodeFilter, Map<CommandNode<S>, CommandNode<S>> newNodes)
|
||||
{
|
||||
CommandNode<S> existingNode = newNodes.get(node);
|
||||
if (existingNode == null)
|
||||
{
|
||||
CommandNode<S> newNode = cloneNode(node, nodeFilter, newNodes);
|
||||
newNodes.put(node, newNode);
|
||||
node.getChildren().stream()
|
||||
.filter(nodeFilter)
|
||||
.map(child -> processCommandNode(child, nodeFilter, newNodes))
|
||||
.forEach(newNode::addChild);
|
||||
return newNode;
|
||||
}
|
||||
else
|
||||
{
|
||||
return existingNode;
|
||||
}
|
||||
}
|
||||
|
||||
private static <S> CommandNode<S> cloneNode(CommandNode<S> node, Predicate<CommandNode<?>> nodeFilter, Map<CommandNode<S>, CommandNode<S>> newNodes)
|
||||
{
|
||||
if (node instanceof RootCommandNode<?>)
|
||||
{
|
||||
return new RootCommandNode<>();
|
||||
}
|
||||
else
|
||||
{
|
||||
ArgumentBuilder<S, ?> builder = node.createBuilder();
|
||||
if (node.getRedirect() != null)
|
||||
{
|
||||
if (nodeFilter.test(node.getRedirect()))
|
||||
{
|
||||
builder.forward(processCommandNode(node.getRedirect(), nodeFilter, newNodes), node.getRedirectModifier(), node.isFork());
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.redirect(null);
|
||||
}
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,16 +9,8 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import net.minecraft.commands.CommandBuildContext;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.minecraft.commands.SharedSuggestionProvider;
|
||||
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
|
||||
import net.minecraft.commands.synchronization.ArgumentTypeInfos;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.data.registries.VanillaRegistries;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.network.protocol.game.ClientboundCommandsPacket;
|
||||
import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraftforge.network.ConnectionType;
|
||||
@@ -27,7 +19,6 @@ import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.mojang.brigadier.tree.RootCommandNode;
|
||||
|
||||
/**
|
||||
* A filter for impl packets, used to filter/modify parts of vanilla impl messages that
|
||||
@@ -39,7 +30,6 @@ public class VanillaConnectionNetworkFilter extends VanillaPacketFilter {
|
||||
super(
|
||||
ImmutableMap.<Class<? extends Packet<?>>, BiConsumer<Packet<?>, List<? super Packet<?>>>>builder()
|
||||
.put(handler(ClientboundUpdateAttributesPacket.class, VanillaConnectionNetworkFilter::filterEntityProperties))
|
||||
.put(handler(ClientboundCommandsPacket.class, VanillaConnectionNetworkFilter::filterCommandList))
|
||||
// TODO Filter tags
|
||||
//.put(handler(ClientboundUpdateTagsPacket.class, VanillaConnectionNetworkFilter::filterCustomTagTypes))
|
||||
.build()
|
||||
@@ -67,22 +57,6 @@ public class VanillaConnectionNetworkFilter extends VanillaPacketFilter {
|
||||
return newPacket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter for SCommandListPacket. Uses {@link CommandTreeCleaner} to filter out any ArgumentTypes that are not in the "minecraft" or "brigadier" namespace.
|
||||
* A vanilla client would fail to deserialize the packet and disconnect with an error message if these were sent.
|
||||
*/
|
||||
@NotNull
|
||||
private static ClientboundCommandsPacket filterCommandList(ClientboundCommandsPacket packet) {
|
||||
CommandBuildContext commandBuildContext = Commands.createValidationContext(VanillaRegistries.createLookup());
|
||||
RootCommandNode<SharedSuggestionProvider> root = packet.getRoot(commandBuildContext);
|
||||
RootCommandNode<SharedSuggestionProvider> newRoot = CommandTreeCleaner.cleanArgumentTypes(root, argType -> {
|
||||
ArgumentTypeInfo<?, ?> info = ArgumentTypeInfos.byClass(argType);
|
||||
ResourceLocation id = BuiltInRegistries.COMMAND_ARGUMENT_TYPE.getKey(info);
|
||||
return id != null && (id.getNamespace().equals("minecraft") || id.getNamespace().equals("brigadier"));
|
||||
});
|
||||
return new ClientboundCommandsPacket(newRoot);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters out custom tag types that the vanilla client won't recognize.
|
||||
* It prevents a rare error from logging and reduces the packet size
|
||||
|
||||
+4
-4
@@ -8,14 +8,14 @@ package net.minecraftforge.network.tasks;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
import net.minecraftforge.event.network.GatherLoginConfigurationTasksEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.network.ConnectionType;
|
||||
import net.minecraftforge.network.NetworkContext;
|
||||
|
||||
@ApiStatus.Internal
|
||||
public class ForgeNetworkConfigurationHandler {
|
||||
@SubscribeEvent
|
||||
public void gatherInit(GatherLoginConfigurationTasksEvent event) {
|
||||
public final class ForgeNetworkConfigurationHandler {
|
||||
private ForgeNetworkConfigurationHandler() {}
|
||||
|
||||
public static void gatherInit(GatherLoginConfigurationTasksEvent event) {
|
||||
var ctx = NetworkContext.get(event.getConnection());
|
||||
if (ctx.getType() != ConnectionType.MODDED)
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user