Skip to content

Commit

Permalink
guidebook interaction packet handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DaFuqs committed Jan 7, 2025
1 parent 07c8f18 commit 3ab2eb0
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package de.dafuqs.spectrum.compat.modonomicon.client.pages;

import com.klikli_dev.modonomicon.book.BookTextHolder;
import com.klikli_dev.modonomicon.book.page.BookTextPage;
import com.klikli_dev.modonomicon.client.gui.BookGuiManager;
import com.klikli_dev.modonomicon.client.gui.book.entry.BookEntryScreen;
import com.klikli_dev.modonomicon.client.render.page.BookTextPageRenderer;
import de.dafuqs.revelationary.api.advancements.AdvancementHelper;
import de.dafuqs.spectrum.compat.modonomicon.pages.BookConfirmationButtonPage;
import de.dafuqs.spectrum.networking.SpectrumC2SPacketSender;
import net.minecraft.client.gui.widget.ButtonWidget;
import com.klikli_dev.modonomicon.book.*;
import com.klikli_dev.modonomicon.book.page.*;
import com.klikli_dev.modonomicon.client.gui.*;
import com.klikli_dev.modonomicon.client.gui.book.entry.*;
import com.klikli_dev.modonomicon.client.render.page.*;
import de.dafuqs.revelationary.api.advancements.*;
import de.dafuqs.spectrum.compat.modonomicon.pages.*;
import de.dafuqs.spectrum.networking.packet.*;
import net.fabricmc.fabric.api.client.networking.v1.*;
import net.minecraft.client.gui.widget.*;

public class BookConfirmationButtonPageRenderer extends BookTextPageRenderer {

Expand Down Expand Up @@ -43,7 +44,7 @@ public void onBeginDisplayPage(BookEntryScreen parentScreen, int left, int top)

protected void confirmationButtonClicked(ButtonWidget button) {
if (!(page instanceof BookConfirmationButtonPage confirmationPage)) return;
SpectrumC2SPacketSender.sendConfirmationButtonPressedPacket(confirmationPage.getConfirmationString());
ClientPlayNetworking.send(new GuidebookConfirmationButtonPressedPayload(confirmationPage.getConfirmationString()));
button.setMessage(confirmationPage.getConfirmedButtonText().getComponent());
BookGuiManager.get().openEntry(page.getBook().getId(), page.getParentEntry().getId(), page.getPageNumber());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package de.dafuqs.spectrum.compat.modonomicon.client.pages;

import com.klikli_dev.modonomicon.book.*;
import com.klikli_dev.modonomicon.client.gui.book.entry.BookEntryScreen;
import com.klikli_dev.modonomicon.client.gui.book.entry.*;
import com.klikli_dev.modonomicon.client.render.page.*;
import de.dafuqs.revelationary.api.advancements.*;
import de.dafuqs.spectrum.compat.modonomicon.pages.*;
import de.dafuqs.spectrum.helpers.*;
import de.dafuqs.spectrum.networking.*;
import de.dafuqs.spectrum.networking.packet.*;
import de.dafuqs.spectrum.sound.*;
import net.fabricmc.fabric.api.client.networking.v1.*;
import net.minecraft.client.*;
import net.minecraft.client.gui.*;
import net.minecraft.client.gui.widget.*;
Expand Down Expand Up @@ -189,8 +190,8 @@ protected void paymentButtonClicked(ButtonWidget button) {
if (mc.player.isCreative() || InventoryHelper.hasInInventory(List.of(page.getCost()), mc.player.getInventory())) {
soundInstance = new HintRevelationSoundInstance(mc.player);
MinecraftClient.getInstance().getSoundManager().play(soundInstance);

SpectrumC2SPacketSender.sendGuidebookHintBoughtPacket(page.getCompletionAdvancement(), page.getCost());
ClientPlayNetworking.send(new GuidebookHintBoughtPayload(page.getCompletionAdvancement(), page.getCost()));
revealProgress = 1;
lastRevealTime = mc.world.getTime();
mc.player.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,19 @@

import de.dafuqs.spectrum.api.block.*;
import de.dafuqs.spectrum.api.energy.color.*;
import de.dafuqs.spectrum.blocks.chests.*;
import de.dafuqs.spectrum.blocks.particle_spawner.*;
import de.dafuqs.spectrum.helpers.*;
import de.dafuqs.spectrum.inventories.*;
import de.dafuqs.spectrum.inventories.slots.*;
import de.dafuqs.spectrum.items.magic_items.*;
import de.dafuqs.spectrum.items.tools.*;
import de.dafuqs.spectrum.networking.packet.*;
import de.dafuqs.spectrum.progression.*;
import de.dafuqs.spectrum.registries.*;
import net.fabricmc.fabric.api.networking.v1.*;
import net.fabricmc.fabric.api.transfer.v1.item.*;
import net.minecraft.block.entity.*;
import net.minecraft.entity.*;
import net.minecraft.item.*;
import net.minecraft.recipe.*;
import net.minecraft.screen.*;
import net.minecraft.screen.slot.*;
import net.minecraft.server.network.*;
import net.minecraft.server.world.*;
import net.minecraft.sound.*;
import net.minecraft.util.*;

Expand All @@ -32,54 +25,10 @@ public class SpectrumC2SPacketReceiver {
public static void registerC2SReceivers() {
ServerPlayNetworking.registerGlobalReceiver(RenameItemInBedrockAnvilPayload.ID, RenameItemInBedrockAnvilPayload.getPayloadHandler());
ServerPlayNetworking.registerGlobalReceiver(AddLoreBedrockAnvilPayload.ID, AddLoreBedrockAnvilPayload.getPayloadHandler());

ServerPlayNetworking.registerGlobalReceiver(ParticleSpawnerConfigurationC2SPacket.ID, (packet, context) -> {
// receive the client packet...
if (context.player().currentScreenHandler instanceof ParticleSpawnerScreenHandler particleSpawnerScreenHandler) {
ParticleSpawnerBlockEntity blockEntity = particleSpawnerScreenHandler.getBlockEntity();
if (blockEntity != null) {
// ...apply the new settings...
blockEntity.applySettings(packet.configuration());

// ...and distribute it to all clients again
// Iterate over all players tracking a position in the world and send the packet to each player
for (ServerPlayerEntity serverPlayerEntity : PlayerLookup.tracking((ServerWorld) blockEntity.getWorld(), blockEntity.getPos())) {
ServerPlayNetworking.send(serverPlayerEntity, new ParticleSpawnerConfigurationS2CPacket(blockEntity.getPos(), blockEntity.getConfiguration()));
}
}
}
});

ServerPlayNetworking.registerGlobalReceiver(ChangeCompactingChestSettingsPacket.ID, (payload, context) -> {
// receive the client packet...
if (context.player().currentScreenHandler instanceof CompactingChestScreenHandler compactingChestScreenHandler) {
BlockEntity blockEntity = compactingChestScreenHandler.getBlockEntity();
if (blockEntity instanceof CompactingChestBlockEntity compactingChestBlockEntity) {
// apply the new settings
compactingChestBlockEntity.applySettings(payload);
}
}
});

ServerPlayNetworking.registerGlobalReceiver(SpectrumC2SPackets.GUIDEBOOK_HINT_BOUGHT, (server, player, handler, buf, responseSender) -> {
Identifier completionAdvancement = buf.readIdentifier();
Ingredient payment = Ingredient.fromPacket(buf);

for (ItemStack remainder : InventoryHelper.removeFromInventoryWithRemainders(List.of(payment), player.getInventory())) {
InventoryHelper.smartAddToInventory(remainder, player.getInventory(), null);
}

// give the player the hidden "used_tip" advancement and play a sound
Support.grantAdvancementCriterion(player, "hidden/used_tip", "used_tip");
Support.grantAdvancementCriterion(player, completionAdvancement, "hint_purchased");
player.getWorld().playSound(player, player.getX(), player.getY(), player.getZ(), SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 1.0F, 1.0F);
});

ServerPlayNetworking.registerGlobalReceiver(SpectrumC2SPackets.CONFIRMATION_BUTTON_PRESSED, (server, player, handler, buf, responseSender) -> {
String confirmationString = buf.readString();
SpectrumAdvancementCriteria.CONFIRMATION_BUTTON_PRESSED.trigger(player, confirmationString);
player.getWorld().playSound(player, player.getX(), player.getY(), player.getZ(), SoundEvents.UI_BUTTON_CLICK.value(), SoundCategory.PLAYERS, 1.0F, 1.0F);
});
ServerPlayNetworking.registerGlobalReceiver(GuidebookConfirmationButtonPressedPayload.ID, GuidebookConfirmationButtonPressedPayload.getPayloadHandler());
ServerPlayNetworking.registerGlobalReceiver(GuidebookHintBoughtPayload.ID, GuidebookHintBoughtPayload.getPayloadHandler());
ServerPlayNetworking.registerGlobalReceiver(ChangeCompactingChestSettingsPacket.ID, ChangeCompactingChestSettingsPacket.getPayloadHandler());
ServerPlayNetworking.registerGlobalReceiver(ParticleSpawnerConfigurationC2SPacket.ID, ParticleSpawnerConfigurationC2SPacket.getPayloadHandler());

ServerPlayNetworking.registerGlobalReceiver(SpectrumC2SPackets.BIND_ENDER_SPLICE_TO_PLAYER, (server, player, handler, buf, responseSender) -> {
int entityId = buf.readInt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,11 @@
import net.minecraft.entity.player.*;
import net.minecraft.item.*;
import net.minecraft.network.*;
import net.minecraft.recipe.*;
import net.minecraft.util.*;
import org.jetbrains.annotations.*;

@Environment(EnvType.CLIENT)
public class SpectrumC2SPacketSender {

public static void sendGuidebookHintBoughtPacket(Identifier completionAdvancement, Ingredient ingredient) {
PacketByteBuf packetByteBuf = PacketByteBufs.create();
packetByteBuf.writeIdentifier(completionAdvancement);
ingredient.write(packetByteBuf);
ClientPlayNetworking.send(SpectrumC2SPackets.GUIDEBOOK_HINT_BOUGHT, packetByteBuf);
}

public static void sendConfirmationButtonPressedPacket(String queryToTrigger) {
PacketByteBuf packetByteBuf = PacketByteBufs.create();
packetByteBuf.writeString(queryToTrigger);
ClientPlayNetworking.send(SpectrumC2SPackets.CONFIRMATION_BUTTON_PRESSED, packetByteBuf);
}

public static void sendBindEnderSpliceToPlayer(PlayerEntity playerEntity) {
PacketByteBuf packetByteBuf = PacketByteBufs.create();
packetByteBuf.writeInt(playerEntity.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@

public class SpectrumC2SPackets {

public static final Identifier GUIDEBOOK_HINT_BOUGHT = SpectrumCommon.locate("guidebook_tip_used");
public static final Identifier BIND_ENDER_SPLICE_TO_PLAYER = SpectrumCommon.locate("bind_ender_splice_to_player");
public static final Identifier INK_COLOR_SELECTED = SpectrumCommon.locate("ink_color_select");
public static final Identifier WORKSTAFF_TOGGLE_SELECTED = SpectrumCommon.locate("workstaff_toggle_select");
public static final Identifier CONFIRMATION_BUTTON_PRESSED = SpectrumCommon.locate("confirmation_button_pressed");
public static final Identifier SET_SHADOW_SLOT = SpectrumCommon.locate("set_shadow_slot");

public static <T extends CustomPayload> CustomPayload.Id<T> makeId(String id) {
Expand All @@ -24,6 +22,7 @@ public static void register() {
PayloadTypeRegistry.playC2S().register(ParticleSpawnerConfigurationC2SPacket.ID, ParticleSpawnerConfigurationC2SPacket.CODEC);
PayloadTypeRegistry.playC2S().register(AddLoreBedrockAnvilPayload.ID, AddLoreBedrockAnvilPayload.CODEC);
PayloadTypeRegistry.playC2S().register(RenameItemInBedrockAnvilPayload.ID, RenameItemInBedrockAnvilPayload.CODEC);
PayloadTypeRegistry.playC2S().register(GuidebookHintBoughtPayload.ID, GuidebookHintBoughtPayload.CODEC);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package de.dafuqs.spectrum.networking.packet;

import de.dafuqs.spectrum.helpers.CodecHelper;
import de.dafuqs.spectrum.inventories.AutoCompactingInventory;
import de.dafuqs.spectrum.networking.SpectrumC2SPackets;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import de.dafuqs.spectrum.blocks.chests.*;
import de.dafuqs.spectrum.helpers.*;
import de.dafuqs.spectrum.inventories.*;
import de.dafuqs.spectrum.networking.*;
import net.fabricmc.fabric.api.networking.v1.*;
import net.minecraft.block.entity.*;
import net.minecraft.network.*;
import net.minecraft.network.codec.*;
import net.minecraft.network.packet.*;

public record ChangeCompactingChestSettingsPacket(AutoCompactingInventory.AutoCraftingMode mode) implements CustomPayload {

Expand All @@ -15,7 +18,20 @@ public record ChangeCompactingChestSettingsPacket(AutoCompactingInventory.AutoCr
ChangeCompactingChestSettingsPacket::mode,
ChangeCompactingChestSettingsPacket::new
);


public static ServerPlayNetworking.PlayPayloadHandler<ChangeCompactingChestSettingsPacket> getPayloadHandler() {
return (payload, context) -> {
// receive the client packet...
if (context.player().currentScreenHandler instanceof CompactingChestScreenHandler compactingChestScreenHandler) {
BlockEntity blockEntity = compactingChestScreenHandler.getBlockEntity();
if (blockEntity instanceof CompactingChestBlockEntity compactingChestBlockEntity) {
// apply the new settings
compactingChestBlockEntity.applySettings(payload);
}
}
};
}

@Override
public Id<? extends CustomPayload> getId() {
return ID;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.dafuqs.spectrum.networking.packet;

import de.dafuqs.spectrum.networking.*;
import de.dafuqs.spectrum.progression.*;
import net.fabricmc.fabric.api.networking.v1.*;
import net.minecraft.network.*;
import net.minecraft.network.codec.*;
import net.minecraft.network.packet.*;
import net.minecraft.server.network.*;
import net.minecraft.sound.*;

public record GuidebookConfirmationButtonPressedPayload(String confirmationString) implements CustomPayload {

public static final Id<GuidebookConfirmationButtonPressedPayload> ID = SpectrumC2SPackets.makeId("confirmation_button_pressed");
public static final PacketCodec<PacketByteBuf, GuidebookConfirmationButtonPressedPayload> CODEC = PacketCodec.tuple(PacketCodecs.STRING, GuidebookConfirmationButtonPressedPayload::confirmationString, GuidebookConfirmationButtonPressedPayload::new);

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

public static ServerPlayNetworking.PlayPayloadHandler<GuidebookConfirmationButtonPressedPayload> getPayloadHandler() {
return (payload, context) -> {
ServerPlayerEntity player = context.player();
SpectrumAdvancementCriteria.CONFIRMATION_BUTTON_PRESSED.trigger(player, payload.confirmationString);
player.getWorld().playSound(player, player.getX(), player.getY(), player.getZ(), SoundEvents.UI_BUTTON_CLICK.value(), SoundCategory.PLAYERS, 1.0F, 1.0F);
};

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package de.dafuqs.spectrum.networking.packet;

import de.dafuqs.spectrum.helpers.*;
import de.dafuqs.spectrum.networking.*;
import net.fabricmc.fabric.api.networking.v1.*;
import net.minecraft.item.*;
import net.minecraft.network.*;
import net.minecraft.network.codec.*;
import net.minecraft.network.packet.*;
import net.minecraft.recipe.*;
import net.minecraft.server.network.*;
import net.minecraft.sound.*;
import net.minecraft.util.*;

import java.util.*;

public record GuidebookHintBoughtPayload(Identifier completionAdvancement,
Ingredient payment) implements CustomPayload {

public static final Id<GuidebookHintBoughtPayload> ID = SpectrumC2SPackets.makeId("guidebook_hint_bought");
public static final PacketCodec<RegistryByteBuf, GuidebookHintBoughtPayload> CODEC = PacketCodec.tuple(
Identifier.PACKET_CODEC, GuidebookHintBoughtPayload::completionAdvancement,
Ingredient.PACKET_CODEC, GuidebookHintBoughtPayload::payment,
GuidebookHintBoughtPayload::new);

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

public static ServerPlayNetworking.PlayPayloadHandler<GuidebookHintBoughtPayload> getPayloadHandler() {
return (payload, context) -> {
ServerPlayerEntity player = context.player();
for (ItemStack remainder : InventoryHelper.removeFromInventoryWithRemainders(List.of(payload.payment()), player.getInventory())) {
InventoryHelper.smartAddToInventory(remainder, player.getInventory(), null);
}

// give the player the hidden "used_tip" advancement and play a sound
Support.grantAdvancementCriterion(player, "hidden/used_tip", "used_tip");
Support.grantAdvancementCriterion(player, payload.completionAdvancement(), "hint_purchased");
player.getWorld().playSound(player, player.getX(), player.getY(), player.getZ(), SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 1.0F, 1.0F);
};
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package de.dafuqs.spectrum.networking.packet;

import de.dafuqs.spectrum.blocks.particle_spawner.ParticleSpawnerConfiguration;
import de.dafuqs.spectrum.networking.SpectrumC2SPackets;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import de.dafuqs.spectrum.blocks.particle_spawner.*;
import de.dafuqs.spectrum.inventories.*;
import de.dafuqs.spectrum.networking.*;
import net.fabricmc.fabric.api.networking.v1.*;
import net.minecraft.network.*;
import net.minecraft.network.codec.*;
import net.minecraft.network.packet.*;
import net.minecraft.server.network.*;
import net.minecraft.server.world.*;

public record ParticleSpawnerConfigurationC2SPacket(ParticleSpawnerConfiguration configuration) implements CustomPayload {

Expand All @@ -14,7 +18,26 @@ public record ParticleSpawnerConfigurationC2SPacket(ParticleSpawnerConfiguration
ParticleSpawnerConfigurationC2SPacket::configuration,
ParticleSpawnerConfigurationC2SPacket::new
);


public static ServerPlayNetworking.PlayPayloadHandler<ParticleSpawnerConfigurationC2SPacket> getPayloadHandler() {
return (packet, context) -> {
// receive the client packet...
if (context.player().currentScreenHandler instanceof ParticleSpawnerScreenHandler particleSpawnerScreenHandler) {
ParticleSpawnerBlockEntity blockEntity = particleSpawnerScreenHandler.getBlockEntity();
if (blockEntity != null) {
// ...apply the new settings...
blockEntity.applySettings(packet.configuration());

// ...and distribute it to all clients again
// Iterate over all players tracking a position in the world and send the packet to each player
for (ServerPlayerEntity serverPlayerEntity : PlayerLookup.tracking((ServerWorld) blockEntity.getWorld(), blockEntity.getPos())) {
ServerPlayNetworking.send(serverPlayerEntity, new ParticleSpawnerConfigurationS2CPacket(blockEntity.getPos(), blockEntity.getConfiguration()));
}
}
}
};
}

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

0 comments on commit 3ab2eb0

Please sign in to comment.