Skip to content

Commit

Permalink
refactor: Preparing configuration phase usage
Browse files Browse the repository at this point in the history
  • Loading branch information
null2264 committed Dec 7, 2023
1 parent 9d1a291 commit f2a3ab8
Show file tree
Hide file tree
Showing 16 changed files with 367 additions and 198 deletions.
73 changes: 38 additions & 35 deletions src/main/java/io/github/null2264/cobblegen/FluidInteraction.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package io.github.null2264.cobblegen;

import com.google.common.collect.ImmutableList;
import io.github.null2264.cobblegen.data.CGRegistryImpl;
import io.github.null2264.cobblegen.data.model.CGRegistry;
import io.github.null2264.cobblegen.data.model.Generator;
import io.github.null2264.cobblegen.network.payload.CGSyncS2CPayload;
import io.github.null2264.cobblegen.util.CGLog;
import io.github.null2264.cobblegen.util.GeneratorType;
import io.github.null2264.cobblegen.util.PluginFinder;
import io.github.null2264.cobblegen.util.Util;
import lombok.val;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
Expand Down Expand Up @@ -57,6 +56,10 @@ public void addGenerator(Fluid fluid, Generator generator) {
count.incrementAndGet();
}

public Map<Fluid, List<Generator>> getLocalGenerators() {
return generatorMap;
}

@NotNull
public Map<Fluid, List<Generator>> getGenerators() {
return notNullOr(serverGeneratorMap, generatorMap);
Expand All @@ -70,31 +73,17 @@ public boolean isSync() {
@ApiStatus.Internal
@Deprecated(since = "5.1", forRemoval = true)
public void writeGeneratorsToPacket(FriendlyByteBuf buf) {
write(buf);
write(generatorMap, buf);
}

@ApiStatus.Internal
public void readGeneratorsFromPacket(FriendlyByteBuf buf) {
val _genSize = buf.readInt();
val genMap = new HashMap<Fluid, List<Generator>>(_genSize);

for (int i = 0; i < _genSize; i++) {
val key = Util.getFluid(buf.readResourceLocation());

val _gensSize = buf.readInt();
val gens = new ArrayList<Generator>(_gensSize);
for (int j = 0; j < _gensSize; j++) {
val generator = Generator.fromPacket(buf);
if (generator == null) {
// Shouldn't be possible, but just in case... it's Java Reflection API after all.
CGLog.warn("Failed to retrieve a generator, skipping...");
continue;
}
gens.add(generator);
}
genMap.put(key, gens);
}
serverGeneratorMap = genMap;
serverGeneratorMap = read(buf);
}

@ApiStatus.Internal
public void readGeneratorsFromPayload(CGSyncS2CPayload payload) {
serverGeneratorMap = payload.recipe();
}

@ApiStatus.Internal
Expand Down Expand Up @@ -203,18 +192,32 @@ public boolean interactFromPipe(Level level, BlockPos pos, Fluid fluid1, Fluid f
return false;
}

public void write(FriendlyByteBuf buf) {
buf.writeInt(generatorMap.size());

for (Map.Entry<Fluid, List<Generator>> entry : generatorMap.entrySet()) {
buf.writeResourceLocation(Util.getFluidId(entry.getKey()));

val gens = entry.getValue();
buf.writeInt(gens.size());
public static void write(Map<Fluid, List<Generator>> generatorMap, FriendlyByteBuf buf) {
buf.writeMap(
generatorMap,
(b, o) -> b.writeResourceLocation(Util.getFluidId(o)),
(b, generators) -> b.writeCollection(generators, (p, gen) -> gen.toPacket(p))
);
}

for (Generator generator : gens) {
generator.toPacket(buf);
}
}
@ApiStatus.Internal
public static Map<Fluid, List<Generator>> read(FriendlyByteBuf buf) {
return buf.readMap(
(o) -> Util.getFluid(o.readResourceLocation()),
(o) -> {
val _gensSize = o.readVarInt();
val gens = new ArrayList<Generator>(_gensSize);
for (int j = 0; j < _gensSize; j++) {
val generator = Generator.fromPacket(o);
if (generator == null) {
// Shouldn't be possible, but just in case... it's Java Reflection API after all.
CGLog.warn("Failed to retrieve a generator, skipping...");
continue;
}
gens.add(generator);
}
return gens;
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

//#if MC<1.20.2
@Mixin(net.minecraft.client.multiplayer.ClientPacketListener.class)
//#else
//$$ @Mixin(net.minecraft.client.multiplayer.ClientCommonPacketListenerImpl.class)
//#endif
@Mixin(
//#if MC<1.20.2
net.minecraft.client.multiplayer.ClientPacketListener.class
//#else
//$$ net.minecraft.client.multiplayer.ClientCommonPacketListenerImpl.class
//#endif
)
public abstract class ClientCommonPacketListenerMixin
{
@SuppressWarnings("DataFlowIssue")
Expand All @@ -30,17 +32,16 @@ private net.minecraft.client.multiplayer.ClientPacketListener getListener() {
}

@SuppressWarnings("AmbiguousMixinReference")
//#if MC>=1.20.2
//$$ @Inject(method = "handleCustomPayload(Lnet/minecraft/network/protocol/common/ClientboundCustomPayloadPacket;)V", at = @At("HEAD"), cancellable = true)
//#else
@Inject(method = "handleCustomPayload", at = @At("HEAD"), cancellable = true)
//#endif
private void handleCustomPayload(ClientboundCustomPayloadPacket packet, CallbackInfo ci) {
//#if MC<1.20.2
if (CGClientPlayNetworkHandler.handlePacket(getListener(), packet)) {
//#else
//$$ if (CGClientPlayNetworkHandler.handlePacket(getListener(), packet.payload())) {
//#endif
if (CGClientPlayNetworkHandler.handlePacket(
getListener(),
//#if MC<1.20.2
packet
//#else
//$$ packet.payload()
//#endif
)) {
ci.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package io.github.null2264.cobblegen.mixin.network;

import org.spongepowered.asm.mixin.Mixin;
//#if MC<1.20.2
import net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket;
//#else
//$$ import lombok.val;
//$$ import io.github.null2264.cobblegen.data.CGIdentifier;
//$$ import io.netty.buffer.Unpooled;
//$$ import net.minecraft.network.FriendlyByteBuf;
//$$ import io.github.null2264.cobblegen.network.PacketByteBufPayload;
//$$ import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket;
//$$ import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
//$$ import net.minecraft.resources.ResourceLocation;
//$$ import org.spongepowered.asm.mixin.injection.At;
//$$ import org.spongepowered.asm.mixin.injection.Inject;
//$$ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
//$$
//$$ import static io.github.null2264.cobblegen.util.Constants.KNOWN_CLIENT_PAYLOADS;
//$$ import static io.github.null2264.cobblegen.CobbleGen.MOD_ID;
//#endif
import org.spongepowered.asm.mixin.Debug;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(value = ClientboundCustomPayloadPacket.class, priority = 999)
public abstract class ClientboundCustomPayloadPacketMixin {
Expand All @@ -26,10 +27,10 @@ public abstract class ClientboundCustomPayloadPacketMixin {
//$$ if (!id.getNamespace().equals(MOD_ID))
//$$ return;
//$$
//$$ FriendlyByteBuf newBuf = new FriendlyByteBuf(Unpooled.buffer());
//$$ newBuf.writeBytes(buf.copy());
//$$ buf.skipBytes(buf.readableBytes());
//$$ cir.setReturnValue(new PacketByteBufPayload(id, newBuf));
//$$ val reader = KNOWN_CLIENT_PAYLOADS.get(CGIdentifier.fromMC(id));
//$$ if (reader == null) return;
//$$
//$$ cir.setReturnValue(reader.apply(buf));
//$$ }
//#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
//#if MC<1.20.2
import net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket;
//#else
//$$ import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
//#endif
import io.github.null2264.cobblegen.network.CGServerPlayNetworkHandler;
import io.github.null2264.cobblegen.util.Util;
import lombok.val;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket;
import org.spongepowered.asm.mixin.Final;
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;

//#if MC<1.20.2
@Mixin(net.minecraft.server.network.ServerGamePacketListenerImpl.class)
//#else
//$$ @Mixin(net.minecraft.server.network.ServerCommonPacketListenerImpl.class)
//#endif
@Mixin(
//#if MC<1.20.2
net.minecraft.server.network.ServerGamePacketListenerImpl.class
//#else
//$$ net.minecraft.server.network.ServerCommonPacketListenerImpl.class
//#endif
)
public abstract class ServerCommonPacketListenerMixin
{
@Shadow
Expand Down Expand Up @@ -51,12 +53,15 @@ private void init(CallbackInfo ci) {

@Inject(method = "handleCustomPayload", at = @At("HEAD"), cancellable = true)
private void handleCustomPayload(ServerboundCustomPayloadPacket packet, CallbackInfo ci) {
//#if MC<1.20.2
if (CGServerPlayNetworkHandler.handlePacket(getListener(), packet)) {
//#else
//$$ if (CGServerPlayNetworkHandler.handlePacket(getListener(), packet.payload())) {
//#endif
if (CGServerPlayNetworkHandler.handlePacket(
getListener(),
//#if MC<1.20.2
packet
//#else
//$$ packet.payload()
//#endif
)) {
ci.cancel();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package io.github.null2264.cobblegen.mixin.network;

import org.spongepowered.asm.mixin.Debug;
import org.spongepowered.asm.mixin.Mixin;
//#if MC<1.20.2
import net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket;
//#else
//$$ import net.minecraft.network.FriendlyByteBuf;
//$$ import lombok.val;
//$$ import io.github.null2264.cobblegen.data.CGIdentifier;
//$$ import io.netty.buffer.Unpooled;
//$$ import io.github.null2264.cobblegen.network.PacketByteBufPayload;
//$$ import net.minecraft.network.FriendlyByteBuf;
//$$ import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
//$$ import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
//$$ import net.minecraft.resources.ResourceLocation;
//$$ import org.spongepowered.asm.mixin.injection.At;
//$$ import org.spongepowered.asm.mixin.injection.Inject;
//$$ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
//$$
//$$ import static io.github.null2264.cobblegen.util.Constants.KNOWN_SERVER_PAYLOADS;
//$$ import static io.github.null2264.cobblegen.CobbleGen.MOD_ID;
//#endif

Expand All @@ -26,10 +27,10 @@ public abstract class ServerboundCustomPayloadPacketMixin {
//$$ if (!id.getNamespace().equals(MOD_ID))
//$$ return;
//$$
//$$ FriendlyByteBuf newBuf = new FriendlyByteBuf(Unpooled.buffer());
//$$ newBuf.writeBytes(buf.copy());
//$$ buf.skipBytes(buf.readableBytes());
//$$ cir.setReturnValue(new PacketByteBufPayload(id, newBuf));
//$$ val reader = KNOWN_SERVER_PAYLOADS.get(CGIdentifier.fromMC(id));
//$$ if (reader == null) return;
//$$
//$$ cir.setReturnValue(reader.apply(buf));
//$$ }
//#endif
}
Loading

0 comments on commit f2a3ab8

Please sign in to comment.