Skip to content

Commit

Permalink
Port to MC 1.20.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnuecke committed Nov 15, 2023
1 parent aa2bec9 commit e70216b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 42 deletions.
10 changes: 0 additions & 10 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,8 @@ base {

java.toolchain.languageVersion.set(JavaLanguageVersion.of(17))

repositories {
exclusiveContent {
forRepository { maven("https://cursemaven.com") }
filter { includeGroup("curse.maven") }
}
}

dependencies {
minecraft(libs.forge.platform)

// Just for in-dev convenience. Mod doesn't use any JEI APIs.
runtimeOnly(fg.deobf(libs.forge.jei.get()))
}

minecraft {
Expand Down
9 changes: 4 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
[versions]
minecraft = "1.20.1"
parchment = "2023.08.13"
minecraft = "1.20.2"
parchment = "1.20.1-2023.09.03"

forge-platform = "1.20.1-47.1.0"
forge-platform = "1.20.2-48.0.39"

[plugins]
forgegradle = { id = "net.minecraftforge.gradle", version = "[6.0,6.2)" }
parchment = { id = "org.parchmentmc.librarian.forgegradle", version = "1.+" }
spotless = { id = "com.diffplug.spotless", version = "6.20.0" }
spotless = { id = "com.diffplug.spotless", version = "6.22.0" }

[libraries]
minecraft = { group = "com.mojang", name = "minecraft", version.ref = "minecraft" }

forge-platform = { group = "net.minecraftforge", name = "forge", version.ref = "forge-platform" }
forge-jei = { group = "curse.maven", name = "jei-238222", version = "4690097" }
23 changes: 12 additions & 11 deletions src/main/java/li/cil/bedrockores/common/network/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
import li.cil.bedrockores.common.network.message.InfoResponseMessage;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.network.ChannelBuilder;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.simple.SimpleChannel;
import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.network.SimpleChannel;

import java.util.function.Function;

public final class Network {
private static final String PROTOCOL_VERSION = "1";
private static final int PROTOCOL_VERSION = 1;

public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
new ResourceLocation(Constants.MOD_ID, "main"),
() -> PROTOCOL_VERSION,
PROTOCOL_VERSION::equals,
PROTOCOL_VERSION::equals
);
private static final PacketDistributor.PacketTarget TO_SERVER = PacketDistributor.SERVER.noArg();

public static final SimpleChannel INSTANCE = ChannelBuilder
.named(new ResourceLocation(Constants.MOD_ID, "main"))
.networkProtocolVersion(PROTOCOL_VERSION)
.simpleChannel();

// --------------------------------------------------------------------- //

Expand All @@ -34,7 +35,7 @@ public static void initialize() {
}

public static <T extends AbstractMessage> void sendToServer(final T message) {
Network.INSTANCE.sendToServer(message);
Network.INSTANCE.send(message, TO_SERVER);
}

// --------------------------------------------------------------------- //
Expand All @@ -43,7 +44,7 @@ private static <T extends AbstractMessage> void registerMessage(final Class<T> t
INSTANCE.messageBuilder(type, getNextPacketId(), direction)
.encoder(AbstractMessage::toBytes)
.decoder(decoder)
.consumerNetworkThread(AbstractMessage::handleMessage)
.consumerNetworkThread(AbstractMessage::handleMessageAsync)
.add();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.event.network.CustomPayloadEvent;
import org.apache.commons.lang3.NotImplementedException;

import javax.annotation.Nullable;
import java.util.function.Supplier;

public abstract class AbstractMessage {
protected AbstractMessage() {
Expand All @@ -21,8 +20,8 @@ protected AbstractMessage(final FriendlyByteBuf buffer) {

// --------------------------------------------------------------------- //

public static boolean handleMessage(final AbstractMessage message, final Supplier<NetworkEvent.Context> contextSupplied) {
message.handleMessage(contextSupplied);
public static boolean handleMessageAsync(final AbstractMessage message, final CustomPayloadEvent.Context context) {
message.handleMessageAsync(context);
return true;
}

Expand All @@ -32,17 +31,16 @@ public static boolean handleMessage(final AbstractMessage message, final Supplie

// --------------------------------------------------------------------- //

protected void handleMessage(final Supplier<NetworkEvent.Context> contextSupplier) {
final NetworkEvent.Context context = contextSupplier.get();
protected void handleMessageAsync(final CustomPayloadEvent.Context context) {
context.enqueueWork(() -> handleMessage(context));
}

protected void handleMessage(final NetworkEvent.Context context) {
protected void handleMessage(final CustomPayloadEvent.Context context) {
throw new NotImplementedException("Message implements neither asynchronous nor synchronous handleMessage() method.");
}

@Nullable
protected static Level getLevel(final NetworkEvent.Context context) {
protected static Level getLevel(final CustomPayloadEvent.Context context) {
if (context.getDirection().getReceptionSide().isClient()) {
return getClientLevel();
} else {
Expand All @@ -51,7 +49,7 @@ protected static Level getLevel(final NetworkEvent.Context context) {
}

@Nullable
private static Level getServerLevel(final NetworkEvent.Context context) {
private static Level getServerLevel(final CustomPayloadEvent.Context context) {
final var sender = context.getSender();
return sender != null ? sender.level() : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.event.network.CustomPayloadEvent;

import java.util.function.Consumer;

Expand All @@ -22,7 +22,7 @@ protected AbstractMessageWithPosition(final FriendlyByteBuf buffer) {

// --------------------------------------------------------------------- //

protected <T extends BlockEntity> void withBlockEntity(final NetworkEvent.Context context, final Class<T> type, final Consumer<T> callback) {
protected <T extends BlockEntity> void withBlockEntity(final CustomPayloadEvent.Context context, final Class<T> type, final Consumer<T> callback) {
final Level level = getLevel(context);
if (level != null) {
withBlockEntity(level, type, callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import li.cil.bedrockores.common.network.Network;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.event.network.CustomPayloadEvent;

public final class InfoRequestMessage extends AbstractMessageWithPosition {
public InfoRequestMessage(final BlockPos position) {
Expand All @@ -19,7 +19,7 @@ public InfoRequestMessage(final FriendlyByteBuf buffer) {
// AbstractMessage

@Override
public void handleMessage(final NetworkEvent.Context context) {
public void handleMessage(final CustomPayloadEvent.Context context) {
withBlockEntity(context, BlockEntityWithInfo.class, blockEntity ->
Network.INSTANCE.reply(new InfoResponseMessage(blockEntity.getBlockPos(), blockEntity.getLookAtInfo()), context));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.event.network.CustomPayloadEvent;

import javax.annotation.Nullable;

public final class InfoResponseMessage extends AbstractMessageWithPosition {
@Nullable private Component info;
@Nullable
private Component info;

// --------------------------------------------------------------------- //

Expand Down Expand Up @@ -47,7 +48,7 @@ public void toBytes(final FriendlyByteBuf buffer) {
}

@Override
public void handleMessage(final NetworkEvent.Context context) {
public void handleMessage(final CustomPayloadEvent.Context context) {
withBlockEntity(context, BlockEntityWithInfo.class, blockEntity -> blockEntity.setInfoClient(info));
}
}

0 comments on commit e70216b

Please sign in to comment.