Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored 1.21 networking #29

Merged
merged 5 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/dev/hephaestus/glowcase/Glowcase.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import dev.hephaestus.glowcase.block.entity.MailboxBlockEntity;
import dev.hephaestus.glowcase.block.entity.TextBlockEntity;
import dev.hephaestus.glowcase.compat.PolydexCompatibility;
import dev.hephaestus.glowcase.networking.GlowcaseCommonNetworking;
import dev.hephaestus.glowcase.networking.RegisterC2SNetworking;
import dev.hephaestus.glowcase.networking.NetworkPayloads;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
Expand Down Expand Up @@ -73,7 +74,8 @@ public static Identifier id(String... path) {

@Override
public void onInitialize() {
GlowcaseCommonNetworking.onInitialize();
NetworkPayloads.initialize();
RegisterC2SNetworking.initialize();

CommandRegistrationCallback.EVENT.register((dispatcher, access, environment) -> {
dispatcher.register(
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/dev/hephaestus/glowcase/GlowcaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.hephaestus.glowcase.client.render.block.entity.HyperlinkBlockEntityRenderer;
import dev.hephaestus.glowcase.client.render.block.entity.ItemDisplayBlockEntityRenderer;
import dev.hephaestus.glowcase.client.render.block.entity.TextBlockEntityRenderer;
import dev.hephaestus.glowcase.networking.RegisterS2CNetworking;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand Down Expand Up @@ -36,6 +37,8 @@ public void onInitializeClient() {
WorldRenderEvents.AFTER_TRANSLUCENT.register(BakedBlockEntityRenderer.Manager::render);
InvalidateRenderStateCallback.EVENT.register(BakedBlockEntityRenderer.Manager::reset);

RegisterS2CNetworking.initialize();

HudRenderCallback.EVENT.register((context, tickDelta) -> {
MinecraftClient client = MinecraftClient.getInstance();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.hephaestus.glowcase.block.entity;

import dev.hephaestus.glowcase.Glowcase;
import dev.hephaestus.glowcase.networking.packet.EditHyperlinkBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.NbtCompound;
Expand All @@ -13,48 +14,52 @@
import org.jetbrains.annotations.Nullable;

public class HyperlinkBlockEntity extends BlockEntity {
private String url = "";

public HyperlinkBlockEntity(BlockPos pos, BlockState state) {
super(Glowcase.HYPERLINK_BLOCK_ENTITY, pos, state);
}

public String getUrl() {
return url;
}

public void setUrl(String newUrl) {
url = newUrl;
markDirty();
dispatch();
}

@Override
public void writeNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
super.writeNbt(tag, registryLookup);
tag.putString("url", this.url);
}

@Override
public void readNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
super.readNbt(tag, registryLookup);
this.url = tag.getString("url");
}

// standard blockentity boilerplate

public void dispatch() {
if (world instanceof ServerWorld sworld) sworld.getChunkManager().markForUpdate(pos);
}

@Override
public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup registryLookup) {
return createNbt(registryLookup);
}

@Nullable
@Override
public Packet<ClientPlayPacketListener> toUpdatePacket() {
return BlockEntityUpdateS2CPacket.create(this);
}
private String url = "";

public HyperlinkBlockEntity(BlockPos pos, BlockState state) {
super(Glowcase.HYPERLINK_BLOCK_ENTITY, pos, state);
}

public String getUrl() {
return url;
}

public void setUrl(String newUrl) {
url = newUrl;
markDirty();
dispatch();
}

@Override
public void writeNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
super.writeNbt(tag, registryLookup);
tag.putString("url", this.url);
}

@Override
public void readNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
super.readNbt(tag, registryLookup);
this.url = tag.getString("url");
}

public EditHyperlinkBlock createPacketData() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might rename this to "createEditPacket" since I think that's more descriptive. But that'd be in a separate codestyle pass, nothing that would block this pr

Copy link
Author

@JR1811 JR1811 Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this would apply for all the other BEs with packets too, then?

I wonder if something like an interface or abstract super class would be good to enforce / abstract this whole structure a bit better.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think I will move from short to byte for the enum packet codec entries too.

If there is ever going to be an enum with more entries than the byte size, there is something definitely wrong with the enum, and it should probably refactored to be something like a list or map...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my suggestion on this - EditPacket<T extends BlockEntity> extends CustomPayload

contains

  • void send() - defaulted c2s send.
  • void receive(context) - abstract for packets to implement
  • EditPacket create(T blockEntity) - construct packet from blockentity.

Anything that's not public from the BE that needs to be, just make it public.

return new EditHyperlinkBlock(pos, url);
}

// standard blockentity boilerplate

public void dispatch() {
if (world instanceof ServerWorld sworld) sworld.getChunkManager().markForUpdate(pos);
}

@Override
public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup registryLookup) {
return createNbt(registryLookup);
}

@Nullable
@Override
public Packet<ClientPlayPacketListener> toUpdatePacket() {
return BlockEntityUpdateS2CPacket.create(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.hephaestus.glowcase.block.entity;

import dev.hephaestus.glowcase.Glowcase;
import dev.hephaestus.glowcase.networking.packet.EditItemDisplayBlockSettings;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.BlockState;
Expand Down Expand Up @@ -114,6 +115,12 @@ public void readNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLooku
}
}

public EditItemDisplayBlockSettings createPacketData() {
var itemValues = new EditItemDisplayBlockSettings.ItemDisplayBlockValues(
getCachedState().get(Properties.ROTATION), showName, pitch, yaw);
return new EditItemDisplayBlockSettings(pos, rotationType, givesItem, offset, itemValues);
}

public boolean hasItem() {
return this.stack != null && !this.stack.isEmpty();
}
Expand Down Expand Up @@ -180,7 +187,7 @@ public void cycleOffset() {
markDirty();
dispatch();
}

public boolean canGiveTo(PlayerEntity player) {
if(!hasItem()) return false;
else return switch(this.givesItem) {
Expand All @@ -189,7 +196,7 @@ else return switch(this.givesItem) {
case ONCE -> player.isCreative() || !givenTo.contains(player.getUuid());
};
}

public void giveTo(PlayerEntity player, Hand hand) {
player.setStackInHand(hand, getDisplayedStack().copy());
if (!player.isCreative()) {
Expand Down
Loading
Loading