Skip to content

Commit

Permalink
wip s2c packets
Browse files Browse the repository at this point in the history
  • Loading branch information
DaFuqs committed Jan 12, 2025
1 parent f681ef6 commit 1f589fd
Show file tree
Hide file tree
Showing 32 changed files with 264 additions and 398 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,11 @@ public void addTransmission(PastelTransmission transmission, int travelTime) {
}

public int getColor() {
return ColorHelper.getRandomColor(this.uuid.hashCode());
return getColor(this.uuid);
}

public static int getColor(UUID uuid) {
return ColorHelper.getRandomColor(uuid.hashCode());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import de.dafuqs.spectrum.api.item.*;
import de.dafuqs.spectrum.blocks.chests.*;
import de.dafuqs.spectrum.blocks.particle_spawner.*;
import de.dafuqs.spectrum.networking.*;
import de.dafuqs.spectrum.registries.*;
import net.fabricmc.api.*;
Expand All @@ -15,55 +14,47 @@
import net.minecraft.util.math.*;
import org.jetbrains.annotations.*;

public record BlackHoleChestStatusUpdatePayload(BlockPos pos,
ParticleSpawnerConfiguration configuration) implements CustomPayload {
import java.util.*;

public record BlackHoleChestStatusUpdatePayload(BlockPos pos, boolean isFull, boolean canStoreExperience,
long storedExperience,
long maxStoredExperience) implements CustomPayload {

public static final Id<BlackHoleChestStatusUpdatePayload> ID = SpectrumC2SPackets.makeId("black_hole_chest_status_update");
public static final PacketCodec<PacketByteBuf, BlackHoleChestStatusUpdatePayload> CODEC = PacketCodec.tuple(
BlockPos.PACKET_CODEC,
BlackHoleChestStatusUpdatePayload::pos,
ParticleSpawnerConfiguration.PACKET_CODEC,
BlackHoleChestStatusUpdatePayload::configuration,
BlockPos.PACKET_CODEC, BlackHoleChestStatusUpdatePayload::pos,
PacketCodecs.BOOL, BlackHoleChestStatusUpdatePayload::isFull,
PacketCodecs.BOOL, BlackHoleChestStatusUpdatePayload::canStoreExperience,
PacketCodecs.VAR_LONG, BlackHoleChestStatusUpdatePayload::storedExperience,
PacketCodecs.VAR_LONG, BlackHoleChestStatusUpdatePayload::maxStoredExperience,
BlackHoleChestStatusUpdatePayload::new
);

public static void sendBlackHoleChestUpdate(BlackHoleChestBlockEntity chest) {
var xpStack = chest.getStack(BlackHoleChestBlockEntity.EXPERIENCE_STORAGE_PROVIDER_ITEM_SLOT);

long storedXP = 0;
long maxStoredXP = 0;

PacketByteBuf buf = PacketByteBufs.create();
buf.writeBlockPos(chest.getPos());
buf.writeBoolean(chest.isFullServer());
buf.writeBoolean(chest.canStoreExperience());
if (xpStack.getItem() instanceof ExperienceStorageItem experienceStorageItem) {
buf.writeLong(ExperienceStorageItem.getStoredExperience(xpStack));
buf.writeLong(experienceStorageItem.getMaxStoredExperience(xpStack));
} else {
buf.writeLong(0);
buf.writeLong(0);
storedXP = ExperienceStorageItem.getStoredExperience(xpStack);
maxStoredXP = experienceStorageItem.getMaxStoredExperience(xpStack);
}

for (ServerPlayerEntity player : PlayerLookup.tracking(chest)) {
ServerPlayNetworking.send(player, SpectrumS2CPackets.BLACK_HOLE_CHEST_STATUS_UPDATE, buf);
ServerPlayNetworking.send(player, new BlackHoleChestStatusUpdatePayload(chest.getPos(), chest.isFullServer(), chest.canStoreExperience(), storedXP, maxStoredXP));
}
}

@Environment(EnvType.CLIENT)
public static ClientPlayNetworking.@NotNull PlayPayloadHandler<BlackHoleChestStatusUpdatePayload> getPayloadHandler() {
return (client, handler, buf, responseSender) -> {
var pos = buf.readBlockPos();
var isFull = buf.readBoolean();
var canStoreXP = buf.readBoolean();
var xp = buf.readLong();
var max = buf.readLong();

client.execute(() -> {
var entity = client.world.getBlockEntity(pos, SpectrumBlockEntities.BLACK_HOLE_CHEST);

return (payload, context) -> {
context.client().execute(() -> {
Optional<BlackHoleChestBlockEntity> entity = context.client().world.getBlockEntity(payload.pos, SpectrumBlockEntities.BLACK_HOLE_CHEST);
entity.ifPresent(chest -> {
chest.setFull(isFull);
chest.setHasXPStorage(canStoreXP);
chest.setXPData(xp, max);
chest.setFull(payload.isFull);
chest.setHasXPStorage(payload.canStoreExperience);
chest.setXPData(payload.storedExperience, payload.maxStoredExperience);
});
});
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.dafuqs.spectrum.networking.s2c_payloads;

import de.dafuqs.spectrum.blocks.particle_spawner.*;
import de.dafuqs.spectrum.networking.*;
import de.dafuqs.spectrum.particle.effect.*;
import net.fabricmc.api.*;
Expand All @@ -14,36 +13,32 @@
import net.minecraft.util.math.*;
import org.jetbrains.annotations.*;

public record ColorTransmissionPayload(BlockPos pos,
ParticleSpawnerConfiguration configuration) implements CustomPayload {
public record ColorTransmissionPayload(BlockPos pos, ColoredTransmission transmission) implements CustomPayload {

public static final Id<ColorTransmissionPayload> ID = SpectrumC2SPackets.makeId("color_transmission");
public static final PacketCodec<PacketByteBuf, ColorTransmissionPayload> CODEC = PacketCodec.tuple(
BlockPos.PACKET_CODEC,
ColorTransmissionPayload::pos,
ParticleSpawnerConfiguration.PACKET_CODEC,
ColorTransmissionPayload::configuration,
BlockPos.PACKET_CODEC, ColorTransmissionPayload::pos,
ColoredTransmission.PACKET_CODEC, ColorTransmissionPayload::transmission,
ColorTransmissionPayload::new
);

public static void playColorTransmissionParticle(ServerWorld world, @NotNull ColoredTransmission transfer) {
BlockPos blockPos = BlockPos.ofFloored(transfer.getOrigin());
public static void playColorTransmissionParticle(ServerWorld world, @NotNull ColoredTransmission transmission) {
BlockPos pos = BlockPos.ofFloored(transmission.getOrigin());

PacketByteBuf buf = PacketByteBufs.create();
ColoredTransmission.writeToBuf(buf, transfer);
ColoredTransmission.writeToBuf(buf, transmission);

for (ServerPlayerEntity player : PlayerLookup.tracking(world, blockPos)) {
ServerPlayNetworking.send(player, SpectrumS2CPackets.COLOR_TRANSMISSION, buf);
for (ServerPlayerEntity player : PlayerLookup.tracking(world, pos)) {
ServerPlayNetworking.send(player, new ColorTransmissionPayload(pos, transmission));
}
}

@Environment(EnvType.CLIENT)
public static ClientPlayNetworking.@NotNull PlayPayloadHandler<ColorTransmissionPayload> getPayloadHandler() {
return (client, handler, buf, responseSender) -> {
ColoredTransmission transmission = ColoredTransmission.readFromBuf(buf);
client.execute(() -> {
// Everything in this lambda is running on the render thread
client.world.addImportantParticle(new ColoredTransmissionParticleEffect(transmission.getDestination(), transmission.getArrivalInTicks(), transmission.getDyeColor()), true, transmission.getOrigin().getX(), transmission.getOrigin().getY(), transmission.getOrigin().getZ(), 0.0D, 0.0D, 0.0D);
return (payload, context) -> {
context.client().execute(() -> {
ColoredTransmission transmission = payload.transmission;
context.client().world.addImportantParticle(new ColoredTransmissionParticleEffect(transmission.getDestination(), transmission.getArrivalInTicks(), transmission.getDyeColor()), true, transmission.getOrigin().getX(), transmission.getOrigin().getY(), transmission.getOrigin().getZ(), 0.0D, 0.0D, 0.0D);
});
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.dafuqs.spectrum.networking.s2c_payloads;

import de.dafuqs.spectrum.blocks.chests.*;
import de.dafuqs.spectrum.blocks.particle_spawner.*;
import de.dafuqs.spectrum.networking.*;
import de.dafuqs.spectrum.registries.*;
import net.fabricmc.api.*;
Expand All @@ -14,41 +13,30 @@
import net.minecraft.util.math.*;
import org.jetbrains.annotations.*;

public record CompactingChestStatusUpdatePayload(BlockPos pos,
ParticleSpawnerConfiguration configuration) implements CustomPayload {
public record CompactingChestStatusUpdatePayload(BlockPos pos, boolean hasToCraft) implements CustomPayload {

public static final Id<CompactingChestStatusUpdatePayload> ID = SpectrumC2SPackets.makeId("compacting_chest_status_update");
public static final PacketCodec<PacketByteBuf, CompactingChestStatusUpdatePayload> CODEC = PacketCodec.tuple(
BlockPos.PACKET_CODEC,
CompactingChestStatusUpdatePayload::pos,
ParticleSpawnerConfiguration.PACKET_CODEC,
CompactingChestStatusUpdatePayload::configuration,
BlockPos.PACKET_CODEC, CompactingChestStatusUpdatePayload::pos,
PacketCodecs.BOOL, CompactingChestStatusUpdatePayload::hasToCraft,
CompactingChestStatusUpdatePayload::new
);

public static void sendCompactingChestStatusUpdate(CompactingChestBlockEntity chest) {
PacketByteBuf buf = PacketByteBufs.create();
buf.writeBlockPos(chest.getPos());
buf.writeBoolean(chest.hasToCraft());

for (ServerPlayerEntity player : PlayerLookup.tracking(chest)) {
ServerPlayNetworking.send(player, SpectrumS2CPackets.COMPACTING_CHEST_STATUS_UPDATE, buf);
ServerPlayNetworking.send(player, new CompactingChestStatusUpdatePayload(chest.getPos(), chest.hasToCraft()));
}
}

@Environment(EnvType.CLIENT)
public static ClientPlayNetworking.@NotNull PlayPayloadHandler<CompactingChestStatusUpdatePayload> getPayloadHandler() {
return (client, handler, buf, responseSender) -> {
var pos = buf.readBlockPos();
var hasToCraft = buf.readBoolean();
return (payload, context) -> {
var pos = payload.pos;
var hasToCraft = payload.hasToCraft;

client.execute(() -> {
var entity = client.world.getBlockEntity(pos, SpectrumBlockEntities.COMPACTING_CHEST);

if (entity.isEmpty())
return;

entity.get().shouldCraft(hasToCraft);
context.client().execute(() -> {
var entity = context.client().world.getBlockEntity(pos, SpectrumBlockEntities.COMPACTING_CHEST);
entity.ifPresent(compactingChestBlockEntity -> compactingChestBlockEntity.shouldCraft(hasToCraft));
});
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.dafuqs.spectrum.networking.s2c_payloads;

import de.dafuqs.spectrum.blocks.chests.*;
import de.dafuqs.spectrum.blocks.particle_spawner.*;
import de.dafuqs.spectrum.networking.*;
import de.dafuqs.spectrum.registries.*;
import net.fabricmc.api.*;
Expand All @@ -17,52 +16,43 @@

import java.util.*;

public record FabricationChestStatusUpdatePayload(BlockPos pos,
ParticleSpawnerConfiguration configuration) implements CustomPayload {
public record FabricationChestStatusUpdatePayload(BlockPos pos, boolean isFull, boolean hasValidRecipes,
List<ItemStack> stacks) implements CustomPayload {

public static final Id<FabricationChestStatusUpdatePayload> ID = SpectrumC2SPackets.makeId("fabrication_chest_status_update");
public static final PacketCodec<PacketByteBuf, FabricationChestStatusUpdatePayload> CODEC = PacketCodec.tuple(
BlockPos.PACKET_CODEC,
FabricationChestStatusUpdatePayload::pos,
ParticleSpawnerConfiguration.PACKET_CODEC,
FabricationChestStatusUpdatePayload::configuration,
public static final PacketCodec<RegistryByteBuf, FabricationChestStatusUpdatePayload> CODEC = PacketCodec.tuple(
BlockPos.PACKET_CODEC, FabricationChestStatusUpdatePayload::pos,
PacketCodecs.BOOL, FabricationChestStatusUpdatePayload::isFull,
PacketCodecs.BOOL, FabricationChestStatusUpdatePayload::hasValidRecipes,
ItemStack.LIST_PACKET_CODEC, FabricationChestStatusUpdatePayload::stacks,
FabricationChestStatusUpdatePayload::new
);

public static void sendFabricationChestStatusUpdate(FabricationChestBlockEntity chest) {
PacketByteBuf buf = PacketByteBufs.create();
buf.writeBlockPos(chest.getPos());
buf.writeBoolean(chest.isFullServer());
buf.writeBoolean(chest.hasValidRecipes());
buf.writeInt(chest.getRecipeOutputs().size());
for (ItemStack recipeOutput : chest.getRecipeOutputs()) {
buf.writeItemStack(recipeOutput);
}
BlockPos pos = chest.getPos();
boolean isFull = chest.isFullServer();
boolean hasValidRecipes = chest.hasValidRecipes();
List<ItemStack> stacks = new ArrayList<>();
stacks.addAll(chest.getRecipeOutputs());

for (ServerPlayerEntity player : PlayerLookup.tracking(chest)) {
ServerPlayNetworking.send(player, SpectrumS2CPackets.FABRICATION_CHEST_STATUS_UPDATE, buf);
ServerPlayNetworking.send(player, new FabricationChestStatusUpdatePayload(pos, isFull, hasValidRecipes, stacks));
}
}

@Environment(EnvType.CLIENT)
public static ClientPlayNetworking.@NotNull PlayPayloadHandler<FabricationChestStatusUpdatePayload> getPayloadHandler() {
return (client, handler, buf, responseSender) -> {
var pos = buf.readBlockPos();
var isFull = buf.readBoolean();
var hasValidRecipes = buf.readBoolean();
var outputCount = buf.readInt();
final var cachedOutputs = new ArrayList<ItemStack>(4);
for (int i = 0; i < outputCount; i++) {
cachedOutputs.add(buf.readItemStack());
}
return (payload, context) -> {
var pos = payload.pos;
var isFull = payload.isFull;
var hasValidRecipes = payload.hasValidRecipes;
List<ItemStack> outputs = payload.stacks;

client.execute(() -> {
var entity = client.world.getBlockEntity(pos, SpectrumBlockEntities.FABRICATION_CHEST);

if (entity.isEmpty())
return;

entity.get().updateState(isFull, hasValidRecipes, cachedOutputs);
context.client().execute(() -> {
Optional<FabricationChestBlockEntity> entity = context.client().world.getBlockEntity(pos, SpectrumBlockEntities.FABRICATION_CHEST);
if (entity.isPresent()) {
entity.get().updateState(isFull, hasValidRecipes, outputs);
}
});
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,45 @@

import de.dafuqs.spectrum.api.block.*;
import de.dafuqs.spectrum.api.energy.color.*;
import de.dafuqs.spectrum.blocks.particle_spawner.*;
import de.dafuqs.spectrum.networking.*;
import de.dafuqs.spectrum.registries.*;
import net.fabricmc.api.*;
import net.fabricmc.fabric.api.client.networking.v1.*;
import net.fabricmc.fabric.api.networking.v1.*;
import net.minecraft.network.*;
import net.minecraft.network.codec.*;
import net.minecraft.network.packet.*;
import net.minecraft.registry.entry.*;
import net.minecraft.screen.*;
import net.minecraft.server.network.*;
import net.minecraft.util.math.*;
import org.jetbrains.annotations.*;

import java.util.*;

public record InkColorSelectedPayload(BlockPos pos,
ParticleSpawnerConfiguration configuration) implements CustomPayload {
public record InkColorSelectedPayload(Optional<RegistryEntry<InkColor>> color) implements CustomPayload {

public static final Id<InkColorSelectedPayload> ID = SpectrumC2SPackets.makeId("ink_color_selected");
public static final PacketCodec<PacketByteBuf, InkColorSelectedPayload> CODEC = PacketCodec.tuple(
BlockPos.PACKET_CODEC,
InkColorSelectedPayload::pos,
ParticleSpawnerConfiguration.PACKET_CODEC,
InkColorSelectedPayload::configuration,
public static final PacketCodec<RegistryByteBuf, InkColorSelectedPayload> CODEC = PacketCodec.tuple(
PacketCodecs.optional(PacketCodecs.registryEntry(SpectrumRegistries.INK_COLORS_KEY)), InkColorSelectedPayload::color,
InkColorSelectedPayload::new
);

public static void sendInkColorSelected(@Nullable InkColor color, ServerPlayerEntity player) {
PacketByteBuf packetByteBuf = PacketByteBufs.create();
if (color == null) {
packetByteBuf.writeBoolean(false);
} else {
packetByteBuf.writeBoolean(true);
packetByteBuf.writeIdentifier(color.getID());
}
ServerPlayNetworking.send(player, SpectrumS2CPackets.INK_COLOR_SELECTED, packetByteBuf);
}

@Override
public Id<? extends CustomPayload> getId() {
return ID;
public static void sendInkColorSelected(Optional<RegistryEntry<InkColor>> color, ServerPlayerEntity player) {
ServerPlayNetworking.send(player, new InkColorSelectedPayload(color));
}

@Environment(EnvType.CLIENT)
public static ClientPlayNetworking.PlayPayloadHandler<InkColorSelectedPayload> getPayloadHandler() {
return (client, handler, buf, responseSender) -> {
ScreenHandler screenHandler = client.player.currentScreenHandler;
return (payload, context) -> {
ScreenHandler screenHandler = context.player().currentScreenHandler;
if (screenHandler instanceof InkColorSelectedPacketReceiver inkColorSelectedPacketReceiver) {
boolean isSelection = buf.readBoolean();

InkColor color = null;
if (isSelection) {
Optional<InkColor> optionalInkColor = InkColor.ofId(buf.readIdentifier());
if (optionalInkColor.isPresent()) {
color = optionalInkColor.get();
}
}
inkColorSelectedPacketReceiver.onInkColorSelectedPacket(color);
inkColorSelectedPacketReceiver.onInkColorSelectedPacket(payload.color);
}
};
}

@Override
public Id<? extends CustomPayload> getId() {
return ID;
}

}
Loading

0 comments on commit 1f589fd

Please sign in to comment.