diff --git a/gradle.properties b/gradle.properties index cb21572..8ae6840 100644 --- a/gradle.properties +++ b/gradle.properties @@ -19,5 +19,5 @@ issues=https://github.com/modfest/glowcase/issues sources=https://github.com/modfest/glowcase license=CC0-1.0 # Mod Version -baseVersion=1.3.3 +baseVersion=1.4.0 branch=1.21 diff --git a/src/main/java/dev/hephaestus/glowcase/Glowcase.java b/src/main/java/dev/hephaestus/glowcase/Glowcase.java index 3025eba..7bfa21d 100644 --- a/src/main/java/dev/hephaestus/glowcase/Glowcase.java +++ b/src/main/java/dev/hephaestus/glowcase/Glowcase.java @@ -14,8 +14,6 @@ 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.NetworkPayloads; -import dev.hephaestus.glowcase.networking.RegisterC2SNetworking; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup; @@ -97,8 +95,7 @@ public static Supplier> registerBlock @Override public void onInitialize() { - NetworkPayloads.initialize(); - RegisterC2SNetworking.initialize(); + GlowcaseNetworking.init(); CommandRegistrationCallback.EVENT.register((dispatcher, access, environment) -> { dispatcher.register( diff --git a/src/main/java/dev/hephaestus/glowcase/GlowcaseNetworking.java b/src/main/java/dev/hephaestus/glowcase/GlowcaseNetworking.java new file mode 100644 index 0000000..8b932a4 --- /dev/null +++ b/src/main/java/dev/hephaestus/glowcase/GlowcaseNetworking.java @@ -0,0 +1,19 @@ +package dev.hephaestus.glowcase; + +import dev.hephaestus.glowcase.packet.C2SEditHyperlinkBlock; +import dev.hephaestus.glowcase.packet.C2SEditItemDisplayBlock; +import dev.hephaestus.glowcase.packet.C2SEditTextBlock; +import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; + +public class GlowcaseNetworking { + public static void init() { + PayloadTypeRegistry.playC2S().register(C2SEditHyperlinkBlock.ID, C2SEditHyperlinkBlock.PACKET_CODEC); + PayloadTypeRegistry.playC2S().register(C2SEditItemDisplayBlock.ID, C2SEditItemDisplayBlock.PACKET_CODEC); + PayloadTypeRegistry.playC2S().register(C2SEditTextBlock.ID, C2SEditTextBlock.PACKET_CODEC); + + ServerPlayNetworking.registerGlobalReceiver(C2SEditHyperlinkBlock.ID, C2SEditHyperlinkBlock::receive); + ServerPlayNetworking.registerGlobalReceiver(C2SEditItemDisplayBlock.ID, C2SEditItemDisplayBlock::receive); + ServerPlayNetworking.registerGlobalReceiver(C2SEditTextBlock.ID, C2SEditTextBlock::receive); + } +} diff --git a/src/main/java/dev/hephaestus/glowcase/block/entity/HyperlinkBlockEntity.java b/src/main/java/dev/hephaestus/glowcase/block/entity/HyperlinkBlockEntity.java index 249a0af..8d99892 100644 --- a/src/main/java/dev/hephaestus/glowcase/block/entity/HyperlinkBlockEntity.java +++ b/src/main/java/dev/hephaestus/glowcase/block/entity/HyperlinkBlockEntity.java @@ -1,7 +1,6 @@ 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; @@ -61,10 +60,6 @@ public void readNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLooku this.url = tag.getString("url"); } - public EditHyperlinkBlock createEditPacket() { - return new EditHyperlinkBlock(pos, url); - } - // standard blockentity boilerplate public void dispatch() { diff --git a/src/main/java/dev/hephaestus/glowcase/block/entity/ItemDisplayBlockEntity.java b/src/main/java/dev/hephaestus/glowcase/block/entity/ItemDisplayBlockEntity.java index 74a6177..664bd25 100644 --- a/src/main/java/dev/hephaestus/glowcase/block/entity/ItemDisplayBlockEntity.java +++ b/src/main/java/dev/hephaestus/glowcase/block/entity/ItemDisplayBlockEntity.java @@ -1,7 +1,6 @@ package dev.hephaestus.glowcase.block.entity; import dev.hephaestus.glowcase.Glowcase; -import dev.hephaestus.glowcase.networking.packet.EditItemDisplayBlockSettings; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.Entity; @@ -113,12 +112,6 @@ public void readNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLooku } } - public EditItemDisplayBlockSettings createEditPacket() { - 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(); } @@ -261,4 +254,4 @@ public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup registryL public Packet toUpdatePacket() { return BlockEntityUpdateS2CPacket.create(this); } -} \ No newline at end of file +} diff --git a/src/main/java/dev/hephaestus/glowcase/block/entity/TextBlockEntity.java b/src/main/java/dev/hephaestus/glowcase/block/entity/TextBlockEntity.java index 0e32b2a..8710037 100644 --- a/src/main/java/dev/hephaestus/glowcase/block/entity/TextBlockEntity.java +++ b/src/main/java/dev/hephaestus/glowcase/block/entity/TextBlockEntity.java @@ -2,7 +2,6 @@ import dev.hephaestus.glowcase.Glowcase; import dev.hephaestus.glowcase.client.render.block.entity.BakedBlockEntityRenderer; -import dev.hephaestus.glowcase.networking.packet.EditTextBlock; import eu.pb4.placeholders.api.ParserContext; import eu.pb4.placeholders.api.parsers.NodeParser; import eu.pb4.placeholders.api.parsers.TagParser; @@ -81,11 +80,6 @@ protected void readNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLo this.renderDirty = true; } - public EditTextBlock createEditPacket() { - var textValues = new EditTextBlock.TextBlockValues(scale, color, lines); - return new EditTextBlock(pos, textAlignment, zOffset, shadowType, textValues); - } - public String getRawLine(int i) { var line = this.lines.get(i); diff --git a/src/main/java/dev/hephaestus/glowcase/GlowcaseClient.java b/src/main/java/dev/hephaestus/glowcase/client/GlowcaseClient.java similarity index 95% rename from src/main/java/dev/hephaestus/glowcase/GlowcaseClient.java rename to src/main/java/dev/hephaestus/glowcase/client/GlowcaseClient.java index b2ff694..b4806e1 100644 --- a/src/main/java/dev/hephaestus/glowcase/GlowcaseClient.java +++ b/src/main/java/dev/hephaestus/glowcase/client/GlowcaseClient.java @@ -1,12 +1,11 @@ -package dev.hephaestus.glowcase; +package dev.hephaestus.glowcase.client; +import dev.hephaestus.glowcase.Glowcase; import dev.hephaestus.glowcase.block.entity.MailboxBlockEntity; -import dev.hephaestus.glowcase.client.GlowcaseClientProxy; import dev.hephaestus.glowcase.client.render.block.entity.BakedBlockEntityRenderer; 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.fabric.api.client.rendering.v1.HudRenderCallback; import net.fabricmc.fabric.api.client.rendering.v1.InvalidateRenderStateCallback; @@ -34,8 +33,6 @@ 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(); diff --git a/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/HyperlinkBlockEditScreen.java b/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/HyperlinkBlockEditScreen.java index dfe581e..9f8f2c8 100644 --- a/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/HyperlinkBlockEditScreen.java +++ b/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/HyperlinkBlockEditScreen.java @@ -1,6 +1,7 @@ package dev.hephaestus.glowcase.client.gui.screen.ingame; import dev.hephaestus.glowcase.block.entity.HyperlinkBlockEntity; +import dev.hephaestus.glowcase.packet.C2SEditHyperlinkBlock; import net.minecraft.client.gui.widget.TextFieldWidget; import net.minecraft.text.Text; import org.lwjgl.glfw.GLFW; @@ -53,7 +54,7 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) { public void close() { hyperlinkBlockEntity.setUrl(urlEntryWidget.getText()); hyperlinkBlockEntity.setTitle(titleEntryWidget.getText()); - hyperlinkBlockEntity.createEditPacket().send(); + C2SEditHyperlinkBlock.of(hyperlinkBlockEntity).send(); super.close(); } } diff --git a/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemDisplayBlockEditScreen.java b/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemDisplayBlockEditScreen.java index 6c77e58..0463453 100644 --- a/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemDisplayBlockEditScreen.java +++ b/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemDisplayBlockEditScreen.java @@ -1,6 +1,7 @@ package dev.hephaestus.glowcase.client.gui.screen.ingame; import dev.hephaestus.glowcase.block.entity.ItemDisplayBlockEntity; +import dev.hephaestus.glowcase.packet.C2SEditItemDisplayBlock; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.text.Text; @@ -9,7 +10,7 @@ public class ItemDisplayBlockEditScreen extends GlowcaseScreen { private final ItemDisplayBlockEntity displayBlock; - private ButtonWidget givesItemButtom; + private ButtonWidget givesItemButton; private ButtonWidget rotationTypeButton; private ButtonWidget showNameButton; private ButtonWidget offsetButton; @@ -28,9 +29,9 @@ public void init() { int centerW = width / 2; int centerH = height / 2; - this.givesItemButtom = ButtonWidget.builder(Text.stringifiedTranslatable("gui.glowcase.gives_item", this.displayBlock.givesItem), (action) -> { + this.givesItemButton = ButtonWidget.builder(Text.stringifiedTranslatable("gui.glowcase.gives_item", this.displayBlock.givesItem), (action) -> { this.displayBlock.cycleGiveType(); - this.givesItemButtom.setMessage(Text.stringifiedTranslatable("gui.glowcase.gives_item", this.displayBlock.givesItem)); + this.givesItemButton.setMessage(Text.stringifiedTranslatable("gui.glowcase.gives_item", this.displayBlock.givesItem)); editItemDisplayBlock(true); }).dimensions(centerW - 75, centerH - 40 - individualPadding, 150, 20).build(); @@ -52,7 +53,7 @@ public void init() { editItemDisplayBlock(true); }).dimensions(centerW - 75, centerH + 20 + padding, 150, 20).build(); - this.addDrawableChild(this.givesItemButtom); + this.addDrawableChild(this.givesItemButton); this.addDrawableChild(this.rotationTypeButton); this.addDrawableChild(this.showNameButton); this.addDrawableChild(this.offsetButton); @@ -65,6 +66,6 @@ private void editItemDisplayBlock(boolean updatePitchAndYaw) { displayBlock.pitch = pitchAndYaw.x; displayBlock.yaw = pitchAndYaw.y; } - displayBlock.createEditPacket().send(); + C2SEditItemDisplayBlock.of(displayBlock).send(); } -} \ No newline at end of file +} diff --git a/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/TextBlockEditScreen.java b/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/TextBlockEditScreen.java index 5c5f58b..c21852d 100644 --- a/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/TextBlockEditScreen.java +++ b/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/TextBlockEditScreen.java @@ -3,6 +3,7 @@ import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.systems.RenderSystem; import dev.hephaestus.glowcase.block.entity.TextBlockEntity; +import dev.hephaestus.glowcase.packet.C2SEditTextBlock; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.widget.ButtonWidget; @@ -122,7 +123,7 @@ public void tick() { @Override public void close() { - textBlockEntity.createEditPacket().send(); + C2SEditTextBlock.of(textBlockEntity).send(); super.close(); } diff --git a/src/main/java/dev/hephaestus/glowcase/networking/NetworkPayloads.java b/src/main/java/dev/hephaestus/glowcase/networking/NetworkPayloads.java deleted file mode 100644 index bb46800..0000000 --- a/src/main/java/dev/hephaestus/glowcase/networking/NetworkPayloads.java +++ /dev/null @@ -1,30 +0,0 @@ -package dev.hephaestus.glowcase.networking; - -import dev.hephaestus.glowcase.networking.packet.EditHyperlinkBlock; -import dev.hephaestus.glowcase.networking.packet.EditItemDisplayBlockSettings; -import dev.hephaestus.glowcase.networking.packet.EditTextBlock; -import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; -import net.minecraft.network.RegistryByteBuf; -import net.minecraft.network.codec.PacketCodec; -import net.minecraft.network.packet.CustomPayload; - -@SuppressWarnings("unused") -public class NetworkPayloads { - static { - registerC2S(EditHyperlinkBlock.PACKET_ID, EditHyperlinkBlock.PACKET_CODEC); - registerC2S(EditItemDisplayBlockSettings.PACKET_ID, EditItemDisplayBlockSettings.PACKET_CODEC); - registerC2S(EditTextBlock.PACKET_ID, EditTextBlock.PACKET_CODEC); - } - - private static void registerS2C(CustomPayload.Id packetIdentifier, PacketCodec codec) { - PayloadTypeRegistry.playS2C().register(packetIdentifier, codec); - } - - private static void registerC2S(CustomPayload.Id packetIdentifier, PacketCodec codec) { - PayloadTypeRegistry.playC2S().register(packetIdentifier, codec); - } - - public static void initialize() { - // static initialisation - } -} diff --git a/src/main/java/dev/hephaestus/glowcase/networking/RegisterC2SNetworking.java b/src/main/java/dev/hephaestus/glowcase/networking/RegisterC2SNetworking.java deleted file mode 100644 index 1542d72..0000000 --- a/src/main/java/dev/hephaestus/glowcase/networking/RegisterC2SNetworking.java +++ /dev/null @@ -1,18 +0,0 @@ -package dev.hephaestus.glowcase.networking; - -import dev.hephaestus.glowcase.networking.packet.EditHyperlinkBlock; -import dev.hephaestus.glowcase.networking.packet.EditItemDisplayBlockSettings; -import dev.hephaestus.glowcase.networking.packet.EditTextBlock; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; - -public class RegisterC2SNetworking { - static { - ServerPlayNetworking.registerGlobalReceiver(EditHyperlinkBlock.PACKET_ID, EditHyperlinkBlock::receive); - ServerPlayNetworking.registerGlobalReceiver(EditItemDisplayBlockSettings.PACKET_ID, EditItemDisplayBlockSettings::receive); - ServerPlayNetworking.registerGlobalReceiver(EditTextBlock.PACKET_ID, EditTextBlock::receive); - } - - public static void initialize() { - // static initialisation - } -} diff --git a/src/main/java/dev/hephaestus/glowcase/networking/RegisterS2CNetworking.java b/src/main/java/dev/hephaestus/glowcase/networking/RegisterS2CNetworking.java deleted file mode 100644 index a046658..0000000 --- a/src/main/java/dev/hephaestus/glowcase/networking/RegisterS2CNetworking.java +++ /dev/null @@ -1,11 +0,0 @@ -package dev.hephaestus.glowcase.networking; - -public class RegisterS2CNetworking { - static { - // ClientPlayNetworking.registerGlobalReceiver(...); - } - - public static void initialize() { - // static initialisation - } -} diff --git a/src/main/java/dev/hephaestus/glowcase/networking/packet/EditHyperlinkBlock.java b/src/main/java/dev/hephaestus/glowcase/networking/packet/EditHyperlinkBlock.java deleted file mode 100644 index c325a6f..0000000 --- a/src/main/java/dev/hephaestus/glowcase/networking/packet/EditHyperlinkBlock.java +++ /dev/null @@ -1,43 +0,0 @@ -package dev.hephaestus.glowcase.networking.packet; - -import dev.hephaestus.glowcase.Glowcase; -import dev.hephaestus.glowcase.block.entity.HyperlinkBlockEntity; -import dev.hephaestus.glowcase.util.NetworkingUtil; -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.minecraft.network.RegistryByteBuf; -import net.minecraft.network.codec.PacketCodec; -import net.minecraft.network.codec.PacketCodecs; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.server.world.ServerWorld; -import net.minecraft.util.math.BlockPos; - -public record EditHyperlinkBlock(BlockPos pos, String url) implements CustomPayload { - private static final int URL_MAX_LENGTH = 1024; - - public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( - BlockPos.PACKET_CODEC, EditHyperlinkBlock::pos, - PacketCodecs.STRING, EditHyperlinkBlock::url, - EditHyperlinkBlock::new - ); - - public static final Id PACKET_ID = new Id<>(Glowcase.id("channel.hyperlink.save")); - - @Override - public Id getId() { - return PACKET_ID; - } - - public void send() { - ClientPlayNetworking.send(this); - } - - public void receive(ServerPlayNetworking.Context context) { - ServerWorld serverWorld = context.player().getServerWorld(); - if (!NetworkingUtil.canEditGlowcase(context.player(), this.pos(), Glowcase.HYPERLINK_BLOCK.get())) return; - if (!(serverWorld.getBlockEntity(this.pos()) instanceof HyperlinkBlockEntity link)) return; - if (this.url().length() <= URL_MAX_LENGTH) { - link.setUrl(this.url()); - } - } -} \ No newline at end of file diff --git a/src/main/java/dev/hephaestus/glowcase/networking/packet/EditItemDisplayBlockSettings.java b/src/main/java/dev/hephaestus/glowcase/networking/packet/EditItemDisplayBlockSettings.java deleted file mode 100644 index 7342702..0000000 --- a/src/main/java/dev/hephaestus/glowcase/networking/packet/EditItemDisplayBlockSettings.java +++ /dev/null @@ -1,72 +0,0 @@ -package dev.hephaestus.glowcase.networking.packet; - -import dev.hephaestus.glowcase.Glowcase; -import dev.hephaestus.glowcase.block.entity.ItemDisplayBlockEntity; -import dev.hephaestus.glowcase.util.NetworkingUtil; -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.minecraft.network.RegistryByteBuf; -import net.minecraft.network.codec.PacketCodec; -import net.minecraft.network.codec.PacketCodecs; -import net.minecraft.network.packet.CustomPayload; -import net.minecraft.server.world.ServerWorld; -import net.minecraft.state.property.Properties; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.RotationPropertyHelper; - -public record EditItemDisplayBlockSettings(BlockPos pos, ItemDisplayBlockEntity.RotationType rotationType, - ItemDisplayBlockEntity.GivesItem givesItem, - ItemDisplayBlockEntity.Offset offset, - ItemDisplayBlockValues values) implements CustomPayload { - - public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( - BlockPos.PACKET_CODEC, EditItemDisplayBlockSettings::pos, - PacketCodecs.BYTE.xmap(index -> ItemDisplayBlockEntity.RotationType.values()[index], rotation -> (byte) rotation.ordinal()), EditItemDisplayBlockSettings::rotationType, - PacketCodecs.BYTE.xmap(index -> ItemDisplayBlockEntity.GivesItem.values()[index], givesItem -> (byte) givesItem.ordinal()), EditItemDisplayBlockSettings::givesItem, - PacketCodecs.BYTE.xmap(index -> ItemDisplayBlockEntity.Offset.values()[index], offset -> (byte) offset.ordinal()), EditItemDisplayBlockSettings::offset, - ItemDisplayBlockValues.PACKET_CODEC, EditItemDisplayBlockSettings::values, - EditItemDisplayBlockSettings::new - ); - - public static final Id PACKET_ID = new Id<>(Glowcase.id("channel.item_display")); - - @Override - public Id getId() { - return PACKET_ID; - } - - public void send() { - ClientPlayNetworking.send(this); - } - - public void receive(ServerPlayNetworking.Context context) { - ServerWorld serverWorld = context.player().getServerWorld(); - if (this.values().rotation() < 0 || this.values().rotation() >= RotationPropertyHelper.getMax()) return; - if (!NetworkingUtil.canEditGlowcase(context.player(), this.pos(), Glowcase.ITEM_DISPLAY_BLOCK.get())) return; - if (!(serverWorld.getBlockEntity(this.pos()) instanceof ItemDisplayBlockEntity be)) return; - - be.givesItem = this.givesItem(); - be.rotationType = this.rotationType(); - be.offset = this.offset(); - be.pitch = this.values().pitch(); - be.yaw = this.values().yaw(); - be.showName = this.values().showName(); - - serverWorld.setBlockState(this.pos(), context.player().getWorld() - .getBlockState(this.pos()).with(Properties.ROTATION, this.values().rotation())); - - be.markDirty(); - be.dispatch(); - } - - // separated for tuple call - public record ItemDisplayBlockValues(int rotation, boolean showName, float pitch, float yaw) { - public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( - PacketCodecs.INTEGER, ItemDisplayBlockValues::rotation, - PacketCodecs.BOOL, ItemDisplayBlockValues::showName, - PacketCodecs.FLOAT, ItemDisplayBlockValues::pitch, - PacketCodecs.FLOAT, ItemDisplayBlockValues::yaw, - ItemDisplayBlockValues::new - ); - } -} \ No newline at end of file diff --git a/src/main/java/dev/hephaestus/glowcase/packet/C2SEditBlockEntity.java b/src/main/java/dev/hephaestus/glowcase/packet/C2SEditBlockEntity.java new file mode 100644 index 0000000..7735e2f --- /dev/null +++ b/src/main/java/dev/hephaestus/glowcase/packet/C2SEditBlockEntity.java @@ -0,0 +1,33 @@ +package dev.hephaestus.glowcase.packet; + +import dev.hephaestus.glowcase.block.GlowcaseBlock; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.ChunkPos; + +public interface C2SEditBlockEntity extends CustomPayload { + + BlockPos pos(); + + void receive(ServerWorld world, BlockEntity blockEntity); + + default void receive(ServerPlayNetworking.Context context) { + if (!canEdit(context.player())) return; + receive(context.player().getServerWorld(), context.player().getServerWorld().getBlockEntity(this.pos())); + } + + default void send() { + ClientPlayNetworking.send(this); + } + + default boolean canEdit(ServerPlayerEntity player) { + if (!player.getServerWorld().isChunkLoaded(ChunkPos.toLong(pos()))) return false; + if (!(player.squaredDistanceTo(pos().toCenterPos()) > 12 * 12)) return false; + return player.getServerWorld().getBlockState(pos()).getBlock() instanceof GlowcaseBlock block && block.canEditGlowcase(player, pos()); + } +} diff --git a/src/main/java/dev/hephaestus/glowcase/packet/C2SEditHyperlinkBlock.java b/src/main/java/dev/hephaestus/glowcase/packet/C2SEditHyperlinkBlock.java new file mode 100644 index 0000000..22b9cfb --- /dev/null +++ b/src/main/java/dev/hephaestus/glowcase/packet/C2SEditHyperlinkBlock.java @@ -0,0 +1,37 @@ +package dev.hephaestus.glowcase.packet; + +import dev.hephaestus.glowcase.Glowcase; +import dev.hephaestus.glowcase.block.entity.HyperlinkBlockEntity; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.network.RegistryByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.codec.PacketCodecs; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.math.BlockPos; + +public record C2SEditHyperlinkBlock(BlockPos pos, String url) implements C2SEditBlockEntity { + public static final Id ID = new Id<>(Glowcase.id("channel.hyperlink.save")); + public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( + BlockPos.PACKET_CODEC, C2SEditHyperlinkBlock::pos, + PacketCodecs.STRING, C2SEditHyperlinkBlock::url, + C2SEditHyperlinkBlock::new + ); + + public static C2SEditHyperlinkBlock of(HyperlinkBlockEntity be) { + return new C2SEditHyperlinkBlock(be.getPos(), be.getUrl()); + } + + @Override + public Id getId() { + return ID; + } + + @Override + public void receive(ServerWorld world, BlockEntity blockEntity) { + if (!(blockEntity instanceof HyperlinkBlockEntity be)) return; + if (this.url().length() <= HyperlinkBlockEntity.URL_MAX_LENGTH) { + be.setUrl(this.url()); + } + } +} diff --git a/src/main/java/dev/hephaestus/glowcase/packet/C2SEditItemDisplayBlock.java b/src/main/java/dev/hephaestus/glowcase/packet/C2SEditItemDisplayBlock.java new file mode 100644 index 0000000..defe890 --- /dev/null +++ b/src/main/java/dev/hephaestus/glowcase/packet/C2SEditItemDisplayBlock.java @@ -0,0 +1,63 @@ +package dev.hephaestus.glowcase.packet; + +import dev.hephaestus.glowcase.Glowcase; +import dev.hephaestus.glowcase.block.entity.ItemDisplayBlockEntity; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.network.RegistryByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.codec.PacketCodecs; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.state.property.Properties; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.RotationPropertyHelper; + +public record C2SEditItemDisplayBlock(BlockPos pos, ItemDisplayBlockEntity.RotationType rotationType, ItemDisplayBlockEntity.GivesItem givesItem, ItemDisplayBlockEntity.Offset offset, ItemDisplayBlockValues values) implements C2SEditBlockEntity { + public static final Id ID = new Id<>(Glowcase.id("channel.item_display")); + public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( + BlockPos.PACKET_CODEC, C2SEditItemDisplayBlock::pos, + PacketCodecs.BYTE.xmap(index -> ItemDisplayBlockEntity.RotationType.values()[index], rotation -> (byte) rotation.ordinal()), C2SEditItemDisplayBlock::rotationType, + PacketCodecs.BYTE.xmap(index -> ItemDisplayBlockEntity.GivesItem.values()[index], givesItem -> (byte) givesItem.ordinal()), C2SEditItemDisplayBlock::givesItem, + PacketCodecs.BYTE.xmap(index -> ItemDisplayBlockEntity.Offset.values()[index], offset -> (byte) offset.ordinal()), C2SEditItemDisplayBlock::offset, + ItemDisplayBlockValues.PACKET_CODEC, C2SEditItemDisplayBlock::values, + C2SEditItemDisplayBlock::new + ); + + public static C2SEditItemDisplayBlock of(ItemDisplayBlockEntity be) { + return new C2SEditItemDisplayBlock(be.getPos(), be.rotationType, be.givesItem, be.offset, new ItemDisplayBlockValues(be.getCachedState().get(Properties.ROTATION), be.showName, be.pitch, be.yaw)); + } + + @Override + public Id getId() { + return ID; + } + + @Override + public void receive(ServerWorld world, BlockEntity blockEntity) { + if (!(blockEntity instanceof ItemDisplayBlockEntity be)) return; + if (this.values().rotation() < 0 || this.values().rotation() >= RotationPropertyHelper.getMax()) return; + + be.givesItem = this.givesItem(); + be.rotationType = this.rotationType(); + be.offset = this.offset(); + be.pitch = this.values().pitch(); + be.yaw = this.values().yaw(); + be.showName = this.values().showName(); + + world.setBlockState(this.pos(), world.getBlockState(this.pos()).with(Properties.ROTATION, this.values().rotation())); + + be.markDirty(); + be.dispatch(); + } + + // separated for tuple call + public record ItemDisplayBlockValues(int rotation, boolean showName, float pitch, float yaw) { + public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( + PacketCodecs.INTEGER, ItemDisplayBlockValues::rotation, + PacketCodecs.BOOL, ItemDisplayBlockValues::showName, + PacketCodecs.FLOAT, ItemDisplayBlockValues::pitch, + PacketCodecs.FLOAT, ItemDisplayBlockValues::yaw, + ItemDisplayBlockValues::new + ); + } +} diff --git a/src/main/java/dev/hephaestus/glowcase/networking/packet/EditTextBlock.java b/src/main/java/dev/hephaestus/glowcase/packet/C2SEditTextBlock.java similarity index 52% rename from src/main/java/dev/hephaestus/glowcase/networking/packet/EditTextBlock.java rename to src/main/java/dev/hephaestus/glowcase/packet/C2SEditTextBlock.java index e08666b..0240e31 100644 --- a/src/main/java/dev/hephaestus/glowcase/networking/packet/EditTextBlock.java +++ b/src/main/java/dev/hephaestus/glowcase/packet/C2SEditTextBlock.java @@ -1,10 +1,8 @@ -package dev.hephaestus.glowcase.networking.packet; +package dev.hephaestus.glowcase.packet; import dev.hephaestus.glowcase.Glowcase; import dev.hephaestus.glowcase.block.entity.TextBlockEntity; -import dev.hephaestus.glowcase.util.NetworkingUtil; -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.block.entity.BlockEntity; import net.minecraft.network.RegistryByteBuf; import net.minecraft.network.codec.PacketCodec; import net.minecraft.network.codec.PacketCodecs; @@ -17,34 +15,29 @@ import java.util.ArrayList; import java.util.List; -public record EditTextBlock(BlockPos pos, TextBlockEntity.TextAlignment alignment, TextBlockEntity.ZOffset offset, - TextBlockEntity.ShadowType shadowType, - TextBlockValues values) implements CustomPayload { - - public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( - BlockPos.PACKET_CODEC, EditTextBlock::pos, - PacketCodecs.BYTE.xmap(index -> TextBlockEntity.TextAlignment.values()[index], textAlignment -> (byte) textAlignment.ordinal()), EditTextBlock::alignment, - PacketCodecs.BYTE.xmap(index -> TextBlockEntity.ZOffset.values()[index], zOffset -> (byte) zOffset.ordinal()), EditTextBlock::offset, - PacketCodecs.BYTE.xmap(index -> TextBlockEntity.ShadowType.values()[index], shadow -> (byte) shadow.ordinal()), EditTextBlock::shadowType, - TextBlockValues.PACKET_CODEC, EditTextBlock::values, - EditTextBlock::new +public record C2SEditTextBlock(BlockPos pos, TextBlockEntity.TextAlignment alignment, TextBlockEntity.ZOffset offset, TextBlockEntity.ShadowType shadowType, TextBlockValues values) implements C2SEditBlockEntity { + public static final Id ID = new Id<>(Glowcase.id("channel.text_block")); + public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( + BlockPos.PACKET_CODEC, C2SEditTextBlock::pos, + PacketCodecs.BYTE.xmap(index -> TextBlockEntity.TextAlignment.values()[index], textAlignment -> (byte) textAlignment.ordinal()), C2SEditTextBlock::alignment, + PacketCodecs.BYTE.xmap(index -> TextBlockEntity.ZOffset.values()[index], zOffset -> (byte) zOffset.ordinal()), C2SEditTextBlock::offset, + PacketCodecs.BYTE.xmap(index -> TextBlockEntity.ShadowType.values()[index], shadow -> (byte) shadow.ordinal()), C2SEditTextBlock::shadowType, + TextBlockValues.PACKET_CODEC, C2SEditTextBlock::values, + C2SEditTextBlock::new ); - public static final Id PACKET_ID = new Id<>(Glowcase.id("channel.text_block")); + public static C2SEditTextBlock of(TextBlockEntity be) { + return new C2SEditTextBlock(be.getPos(), be.textAlignment, be.zOffset, be.shadowType, new TextBlockValues(be.scale, be.color, be.lines)); + } @Override public Id getId() { - return PACKET_ID; + return ID; } - public void send() { - ClientPlayNetworking.send(this); - } - - public void receive(ServerPlayNetworking.Context context) { - ServerWorld serverWorld = context.player().getServerWorld(); - if (!NetworkingUtil.canEditGlowcase(context.player(), this.pos(), Glowcase.TEXT_BLOCK.get())) return; - if (!(serverWorld.getBlockEntity(this.pos()) instanceof TextBlockEntity be)) return; + @Override + public void receive(ServerWorld world, BlockEntity blockEntity) { + if (!(blockEntity instanceof TextBlockEntity be)) return; be.scale = this.values().scale(); be.lines = this.values().lines(); @@ -66,4 +59,4 @@ public record TextBlockValues(float scale, int color, List lines) { TextBlockValues::new ); } -} \ No newline at end of file +} diff --git a/src/main/java/dev/hephaestus/glowcase/util/NetworkingUtil.java b/src/main/java/dev/hephaestus/glowcase/util/NetworkingUtil.java deleted file mode 100644 index 50d452c..0000000 --- a/src/main/java/dev/hephaestus/glowcase/util/NetworkingUtil.java +++ /dev/null @@ -1,20 +0,0 @@ -package dev.hephaestus.glowcase.util; - -import dev.hephaestus.glowcase.block.GlowcaseBlock; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.server.world.ServerWorld; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.ChunkPos; - -public class NetworkingUtil { - private NetworkingUtil() { - } - - @SuppressWarnings("BooleanMethodIsAlwaysInverted") - public static boolean canEditGlowcase(ServerPlayerEntity player, BlockPos pos, GlowcaseBlock glowcase) { - if (!(player.getWorld() instanceof ServerWorld serverWorld)) return false; - if (!serverWorld.isChunkLoaded(ChunkPos.toLong(pos))) return false; - if (!(player.squaredDistanceTo(pos.toCenterPos()) > 12 * 12)) return false; - return glowcase.canEditGlowcase(player, pos); - } -} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 42fb492..eae9b2d 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -22,7 +22,7 @@ "dev.hephaestus.glowcase.Glowcase" ], "client": [ - "dev.hephaestus.glowcase.GlowcaseClient" + "dev.hephaestus.glowcase.client.GlowcaseClient" ] }, "mixins": [