diff --git a/src/main/java/lol/bai/badpackets/impl/registry/ChannelRegistry.java b/src/main/java/lol/bai/badpackets/impl/registry/ChannelRegistry.java index f161dce..b9c3748 100644 --- a/src/main/java/lol/bai/badpackets/impl/registry/ChannelRegistry.java +++ b/src/main/java/lol/bai/badpackets/impl/registry/ChannelRegistry.java @@ -9,6 +9,7 @@ import java.util.function.Consumer; import java.util.function.Supplier; +import com.google.common.base.Suppliers; import lol.bai.badpackets.api.config.ClientConfigPacketReceiver; import lol.bai.badpackets.api.config.ServerConfigPacketReceiver; import lol.bai.badpackets.api.play.ClientPlayPacketReceiver; @@ -28,8 +29,8 @@ public class ChannelRegistry { Constants.MC_REGISTER_CHANNEL, Constants.MC_UNREGISTER_CHANNEL); - private static final ReaderMapHolder C2S_READERS = new ReaderMapHolder(AccessServerboundCustomPayloadPacket::badpackets_getPacketReaders, AccessServerboundCustomPayloadPacket::badpackets_setPacketReaders); - private static final ReaderMapHolder S2C_READERS = new ReaderMapHolder(AccessClientboundCustomPayloadPacket::badpackets_getPacketReaders, AccessClientboundCustomPayloadPacket::badpackets_setPacketReaders); + private static final Supplier C2S_READERS = Suppliers.memoize(() -> new ReaderMapHolder(AccessServerboundCustomPayloadPacket::badpackets_getPacketReaders, AccessServerboundCustomPayloadPacket::badpackets_setPacketReaders)); + private static final Supplier S2C_READERS = Suppliers.memoize(() -> new ReaderMapHolder(AccessClientboundCustomPayloadPacket::badpackets_getPacketReaders, AccessClientboundCustomPayloadPacket::badpackets_setPacketReaders)); public static final ChannelRegistry> CONFIG_S2C = new ChannelRegistry<>(RESERVED_CHANNELS, S2C_READERS); public static final ChannelRegistry> CONFIG_C2S = new ChannelRegistry<>(RESERVED_CHANNELS, C2S_READERS); @@ -40,11 +41,11 @@ public class ChannelRegistry { private final Map channels = new HashMap<>(); private final Set reservedChannels; private final Set> handlers = new HashSet<>(); - private final ReaderMapHolder readersHolder; + private final Supplier readersHolder; private final ReentrantReadWriteLock locks = new ReentrantReadWriteLock(); - private ChannelRegistry(Set reservedChannels, ReaderMapHolder readersHolder) { + private ChannelRegistry(Set reservedChannels, Supplier readersHolder) { this.reservedChannels = reservedChannels; this.readersHolder = readersHolder; } @@ -58,10 +59,10 @@ public void register(ResourceLocation id, FriendlyByteBuf.Reader> readers = readersHolder.getter.get(); + Map> readers = readersHolder.get().getter.get(); if (!(readers instanceof HashMap)) { readers = new HashMap<>(readers); - readersHolder.setter.accept(readers); + readersHolder.get().setter.accept(readers); } readers.put(id, reader);