Skip to content

Commit

Permalink
chore: add Icon.STREAM_CODEC, NetworkHelper.sendToAll
Browse files Browse the repository at this point in the history
  • Loading branch information
desht committed May 18, 2024
1 parent 8179d9b commit 00f3c16
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
15 changes: 14 additions & 1 deletion common/src/main/java/dev/ftb/mods/ftblibrary/icon/Icon.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import dev.ftb.mods.ftblibrary.FTBLibrary;
import dev.ftb.mods.ftblibrary.config.ImageResourceConfig;
import dev.ftb.mods.ftblibrary.math.PixelBuffer;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

Expand All @@ -19,6 +20,18 @@ public static Color4I empty() {
return Color4I.EMPTY_ICON;
}

public static final StreamCodec<FriendlyByteBuf,Icon> STREAM_CODEC = new StreamCodec<>() {
@Override
public Icon decode(FriendlyByteBuf buf) {
return Icon.getIcon(buf.readUtf());
}

@Override
public void encode(FriendlyByteBuf buf, Icon icon) {
buf.writeUtf(icon.toString());
}
};

public static Icon getIcon(@Nullable JsonElement json) {
if (json == null || json.isJsonNull()) {
return empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.server.MinecraftServer;

public class NetworkHelper {
public static <T extends CustomPacketPayload> void registerC2S(CustomPacketPayload.Type<T> type, StreamCodec<? super RegistryFriendlyByteBuf, T> codec, NetworkManager.NetworkReceiver<T> handler) {
Expand All @@ -21,6 +22,10 @@ public static <T extends CustomPacketPayload> void registerS2C(CustomPacketPaylo
}
}

public static <T extends CustomPacketPayload> void sendToAll(MinecraftServer server, T packet) {
NetworkManager.sendToPlayers(server.getPlayerList().getPlayers(), packet);
}

public static <B extends FriendlyByteBuf, V extends Enum<V>> StreamCodec<B, V> enumStreamCodec(Class<V> enumClass) {
return new StreamCodec<>() {
@Override
Expand Down

0 comments on commit 00f3c16

Please sign in to comment.