Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
Faboslav committed Dec 18, 2023
1 parent ad387b5 commit 351c00b
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import net.minecraft.world.dimension.DimensionTypes;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.event.AddReloadListenerEvent;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.event.OnDatapackSyncEvent;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.event.level.LevelEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.faboslav.friendsandfoes.platform.TotemHelper;
import net.minecraft.entity.LivingEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraftforge.network.*;
import net.minecraftforge.network.ChannelBuilder;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.network.SimpleChannel;

public final class PacketHandler
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraftforge.network.*;
import net.minecraftforge.network.ChannelBuilder;
import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.network.SimpleChannel;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -41,15 +43,18 @@ public static <T extends Packet<T>> void registerS2CPacket(

++channel.packets;
channel.channel.messageBuilder(packetClass).encoder(handler::encode).decoder(handler::decode).consumerNetworkThread((msg, ctx) -> {
PlayerEntity player = null;
if (ctx.getSender() == null) {
player = PlayerProvider.getClientPlayer();
}
ctx.enqueueWork(() -> {
PlayerEntity player = null;
if (ctx.getSender() == null) {
player = PlayerProvider.getClientPlayer();
}

if (player != null) {
PlayerEntity finalPlayer = player;
handler.handle(msg).apply(finalPlayer, finalPlayer.getWorld());
}
});

if (player != null) {
PlayerEntity finalPlayer = player;
ctx.enqueueWork(() -> handler.handle(msg).apply(finalPlayer, finalPlayer.getWorld()));
}
ctx.setPacketHandled(true);
}).add();
}
Expand Down
2 changes: 1 addition & 1 deletion forge/src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ logoFile = "icon.png"
[[dependencies.${ modId }]]
modId = "forge"
mandatory = true
versionRange="[${forgeVersion},)"
versionRange = "[${forgeVersion},)"

[[dependencies.${ modId }]]
modId = "minecraft"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,73 +22,88 @@
* @author ThatGravyBoat
* <a href="https://github.com/Team-Resourceful/ResourcefulLib">https://github.com/Team-Resourceful/ResourcefulLib</a>
*/
public class PacketChannelManagerImpl {
public static final Map<Identifier, Channel> CHANNELS = new HashMap<>();
public class PacketChannelManagerImpl
{
public static final Map<Identifier, Channel> CHANNELS = new HashMap<>();

public static void registerChannel(Identifier name) {
public static void registerChannel(Identifier name) {
String protocolVersion = ModVersion.getModVersion();
Channel channel = new Channel(0, NetworkRegistry.newSimpleChannel(name, () -> protocolVersion, protocolVersion::equals, protocolVersion::equals));
CHANNELS.put(name, channel);
}
Channel channel = new Channel(0, NetworkRegistry.newSimpleChannel(name, () -> protocolVersion, protocolVersion::equals, protocolVersion::equals));
CHANNELS.put(name, channel);
}

public static <T extends Packet<T>> void registerS2CPacket(Identifier name, Identifier id, PacketHandler<T> handler, Class<T> packetClass) {
Channel channel = CHANNELS.get(name);
if (channel == null) {
throw new IllegalStateException("Channel " + name + " not registered");
}
channel.channel.registerMessage(++channel.packets, packetClass, handler::encode, handler::decode, (msg, ctx) -> {
PlayerEntity player = null;
if (ctx.getSender() == null) {
player = PlayerProvider.getClientPlayer();
}
public static <T extends Packet<T>> void registerS2CPacket(
Identifier name,
Identifier id,
PacketHandler<T> handler,
Class<T> packetClass
) {
Channel channel = CHANNELS.get(name);
if (channel == null) {
throw new IllegalStateException("Channel " + name + " not registered");
}
channel.channel.registerMessage(++channel.packets, packetClass, handler::encode, handler::decode, (msg, ctx) -> {
ctx.enqueueWork(() -> {
PlayerEntity player = null;
if (ctx.getSender() == null) {
player = PlayerProvider.getClientPlayer();
}

if (player != null) {
PlayerEntity finalPlayer = player;
ctx.enqueueWork(() -> handler.handle(msg).apply(finalPlayer, finalPlayer.getWorld()));
}
ctx.setPacketHandled(true);
});
}
if (player != null) {
PlayerEntity finalPlayer = player;
ctx.enqueueWork(() -> handler.handle(msg).apply(finalPlayer, finalPlayer.getWorld()));
}
});

public static <T extends Packet<T>> void registerC2SPacket(Identifier name, Identifier id, PacketHandler<T> handler, Class<T> packetClass) {
Channel channel = CHANNELS.get(name);
if (channel == null) {
throw new IllegalStateException("Channel " + name + " not registered");
}
channel.channel.registerMessage(++channel.packets, packetClass, handler::encode, handler::decode, (msg, ctx) -> {
PlayerEntity player = ctx.getSender();
if (player != null) {
ctx.enqueueWork(() -> handler.handle(msg).apply(player, player.getWorld()));
}
ctx.setPacketHandled(true);
});
}
ctx.setPacketHandled(true);
});
}

public static <T extends Packet<T>> void sendToServer(Identifier name, T packet) {
Channel channel = CHANNELS.get(name);
if (channel == null) {
throw new IllegalStateException("Channel " + name + " not registered");
}
channel.channel.sendToServer(packet);
}
public static <T extends Packet<T>> void registerC2SPacket(
Identifier name,
Identifier id,
PacketHandler<T> handler,
Class<T> packetClass
) {
Channel channel = CHANNELS.get(name);
if (channel == null) {
throw new IllegalStateException("Channel " + name + " not registered");
}
channel.channel.registerMessage(++channel.packets, packetClass, handler::encode, handler::decode, (msg, ctx) -> {
PlayerEntity player = ctx.getSender();
if (player != null) {
ctx.enqueueWork(() -> handler.handle(msg).apply(player, player.getWorld()));
}
ctx.setPacketHandled(true);
});
}

public static <T extends Packet<T>> void sendToPlayer(Identifier name, T packet, PlayerEntity player) {
Channel channel = CHANNELS.get(name);
if (channel == null) {
throw new IllegalStateException("Channel " + name + " not registered");
}
if (player instanceof ServerPlayerEntity serverPlayer) {
channel.channel.send(PacketDistributor.PLAYER.with(() -> serverPlayer), packet);
}
}
public static <T extends Packet<T>> void sendToServer(Identifier name, T packet) {
Channel channel = CHANNELS.get(name);
if (channel == null) {
throw new IllegalStateException("Channel " + name + " not registered");
}
channel.channel.sendToServer(packet);
}

private static final class Channel {
private int packets;
private final SimpleChannel channel;
public static <T extends Packet<T>> void sendToPlayer(Identifier name, T packet, PlayerEntity player) {
Channel channel = CHANNELS.get(name);
if (channel == null) {
throw new IllegalStateException("Channel " + name + " not registered");
}
if (player instanceof ServerPlayerEntity serverPlayer) {
channel.channel.send(PacketDistributor.PLAYER.with(() -> serverPlayer), packet);
}
}

private Channel(int packets, SimpleChannel channel) {
this.packets = packets;
this.channel = channel;
}
}
private static final class Channel
{
private int packets;
private final SimpleChannel channel;

private Channel(int packets, SimpleChannel channel) {
this.packets = packets;
this.channel = channel;
}
}
}
2 changes: 1 addition & 1 deletion neoforge/src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ displayName = "${modName}"
authors = "${modAuthor}"
description = "${modDescription}"
logoFile = "icon.png"
logoBlur=false
logoBlur = false

[[mixins]]
config = "friendsandfoes-neoforge.mixins.json"
Expand Down

0 comments on commit 351c00b

Please sign in to comment.