Skip to content

Commit

Permalink
Merge pull request #29 from JR1811/1.21
Browse files Browse the repository at this point in the history
Refactored 1.21 networking
  • Loading branch information
sisby-folk authored Jul 28, 2024
2 parents 5ff5466 + ceab1da commit d4c8529
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 220 deletions.
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 @@ -14,7 +14,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.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;
Expand Down Expand Up @@ -96,7 +97,8 @@ public static <T extends BlockEntity> Supplier<BlockEntityType<T>> registerBlock

@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.fabric.api.client.rendering.v1.HudRenderCallback;
import net.fabricmc.fabric.api.client.rendering.v1.InvalidateRenderStateCallback;
Expand Down Expand Up @@ -33,6 +34,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 Down Expand Up @@ -60,6 +61,10 @@ 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() {
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.EditItemDisplayBlockSettings;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
Expand Down Expand Up @@ -112,6 +113,12 @@ 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();
}
Expand Down Expand Up @@ -200,13 +207,13 @@ public void giveTo(PlayerEntity player) {
itemStack.capCount(itemStack.getMaxCount());
player.setStackInHand(Hand.MAIN_HAND, itemStack);
}

if (!player.isCreative()) {
givenTo.add(player.getUuid());
markDirty();
}
}


public static Vec2f getPitchAndYaw(Entity camera, BlockPos pos, float delta) {
double d = pos.getX() - camera.getLerpedPos(delta).x + 0.5;
double e = pos.getY() - camera.getEyeY() + 0.5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand Down Expand Up @@ -80,6 +81,11 @@ 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);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.hephaestus.glowcase.client.gui.screen.ingame;

import dev.hephaestus.glowcase.block.entity.HyperlinkBlockEntity;
import dev.hephaestus.glowcase.networking.GlowcaseClientNetworking;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;
Expand Down Expand Up @@ -52,7 +51,9 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {

@Override
public void close() {
GlowcaseClientNetworking.editHyperlinkBlock(hyperlinkBlockEntity.getPos(), titleEntryWidget.getText(), urlEntryWidget.getText());
hyperlinkBlockEntity.setUrl(urlEntryWidget.getText());
hyperlinkBlockEntity.setTitle(titleEntryWidget.getText());
hyperlinkBlockEntity.createEditPacket().send();
super.close();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package dev.hephaestus.glowcase.client.gui.screen.ingame;

import dev.hephaestus.glowcase.block.entity.ItemDisplayBlockEntity;
import dev.hephaestus.glowcase.networking.GlowcaseClientNetworking;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text;
import net.minecraft.util.math.Vec2f;

public class ItemDisplayBlockEditScreen extends GlowcaseScreen {
private final ItemDisplayBlockEntity displayBlock;
Expand All @@ -30,25 +31,25 @@ public void init() {
this.givesItemButtom = 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));
GlowcaseClientNetworking.editItemDisplayBlock(displayBlock, true);
editItemDisplayBlock(true);
}).dimensions(centerW - 75, centerH - 40 - individualPadding, 150, 20).build();

this.rotationTypeButton = ButtonWidget.builder(Text.stringifiedTranslatable("gui.glowcase.rotation_type", this.displayBlock.rotationType), (action) -> {
this.displayBlock.cycleRotationType(this.client.player);
this.rotationTypeButton.setMessage(Text.stringifiedTranslatable("gui.glowcase.rotation_type", this.displayBlock.rotationType));
GlowcaseClientNetworking.editItemDisplayBlock(displayBlock, true);
editItemDisplayBlock(true);
}).dimensions(centerW - 75, centerH - 20, 150, 20).build();

this.showNameButton = ButtonWidget.builder(Text.translatable("gui.glowcase.show_name", this.displayBlock.showName), (action) -> {
this.displayBlock.showName = !this.displayBlock.showName;
this.showNameButton.setMessage(Text.translatable("gui.glowcase.show_name", this.displayBlock.showName));
GlowcaseClientNetworking.editItemDisplayBlock(displayBlock, false);
editItemDisplayBlock(false);
}).dimensions(centerW - 75, centerH + individualPadding, 150, 20).build();

this.offsetButton = ButtonWidget.builder(Text.stringifiedTranslatable("gui.glowcase.offset", this.displayBlock.offset), (action) -> {
this.displayBlock.cycleOffset();
this.offsetButton.setMessage(Text.stringifiedTranslatable("gui.glowcase.offset", this.displayBlock.offset));
GlowcaseClientNetworking.editItemDisplayBlock(displayBlock, true);
editItemDisplayBlock(true);
}).dimensions(centerW - 75, centerH + 20 + padding, 150, 20).build();

this.addDrawableChild(this.givesItemButtom);
Expand All @@ -57,4 +58,13 @@ public void init() {
this.addDrawableChild(this.offsetButton);
}
}

private void editItemDisplayBlock(boolean updatePitchAndYaw) {
if (updatePitchAndYaw && MinecraftClient.getInstance().getCameraEntity() != null) {
Vec2f pitchAndYaw = ItemDisplayBlockEntity.getPitchAndYaw(MinecraftClient.getInstance().getCameraEntity(), displayBlock.getPos(), 0);
displayBlock.pitch = pitchAndYaw.x;
displayBlock.yaw = pitchAndYaw.y;
}
displayBlock.createEditPacket().send();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import dev.hephaestus.glowcase.block.entity.TextBlockEntity;
import dev.hephaestus.glowcase.networking.GlowcaseClientNetworking;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.widget.ButtonWidget;
Expand Down Expand Up @@ -123,7 +122,7 @@ public void tick() {

@Override
public void close() {
GlowcaseClientNetworking.editTextBlock(textBlockEntity);
textBlockEntity.createEditPacket().send();
super.close();
}

Expand Down

This file was deleted.

Loading

0 comments on commit d4c8529

Please sign in to comment.