Skip to content

Commit

Permalink
Added ModFest#24 title masks for hyperlink blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Chailotl committed Jul 13, 2024
1 parent 2c0b0e9 commit f7f95be
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,27 @@
import org.jetbrains.annotations.Nullable;

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

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

public String getText() {
return !title.isEmpty() ? title : url;
}

public String getTitle() {
return title;
}

public void setTitle(String newTitle) {
title = newTitle;
markDirty();
dispatch();
}

public String getUrl() {
return url;
}
Expand All @@ -32,12 +47,14 @@ public void setUrl(String newUrl) {
@Override
public void writeNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
super.writeNbt(tag, registryLookup);
tag.putString("title", this.title);
tag.putString("url", this.url);
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public class HyperlinkBlockEditScreen extends GlowcaseScreen {
private final HyperlinkBlockEntity hyperlinkBlockEntity;

private TextFieldWidget titleEntryWidget;
private TextFieldWidget urlEntryWidget;

public HyperlinkBlockEditScreen(HyperlinkBlockEntity hyperlinkBlockEntity) {
Expand All @@ -24,10 +25,17 @@ public void init() {

if (this.client == null) return;

this.urlEntryWidget = new TextFieldWidget(this.client.textRenderer, width / 10, height / 2 - 10, 8 * width / 10, 20, Text.empty());
this.titleEntryWidget = new TextFieldWidget(this.client.textRenderer, width / 10, height / 2 - 30, 8 * width / 10, 20, Text.empty());
this.titleEntryWidget.setMaxLength(Integer.MAX_VALUE);
this.titleEntryWidget.setText(this.hyperlinkBlockEntity.getTitle());
this.titleEntryWidget.setPlaceholder(Text.translatable("gui.glowcase.title"));

this.urlEntryWidget = new TextFieldWidget(this.client.textRenderer, width / 10, height / 2 + 10, 8 * width / 10, 20, Text.empty());
this.urlEntryWidget.setMaxLength(Integer.MAX_VALUE);
this.urlEntryWidget.setText(this.hyperlinkBlockEntity.getUrl());
this.urlEntryWidget.setPlaceholder(Text.translatable("gui.glowcase.url"));

this.addDrawableChild(this.titleEntryWidget);
this.addDrawableChild(this.urlEntryWidget);
}

Expand All @@ -36,6 +44,8 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_KP_ENTER || keyCode == GLFW.GLFW_KEY_ESCAPE) {
this.close();
return true;
} else if (this.titleEntryWidget.isActive()) {
return this.titleEntryWidget.keyPressed(keyCode, scanCode, modifiers);
} else if (this.urlEntryWidget.isActive()) {
return this.urlEntryWidget.keyPressed(keyCode, scanCode, modifiers);
}else {
Expand All @@ -45,7 +55,7 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {

@Override
public void close() {
GlowcaseClientNetworking.editHyperlinkBlock(hyperlinkBlockEntity.getPos(), urlEntryWidget.getText());
GlowcaseClientNetworking.editHyperlinkBlock(hyperlinkBlockEntity.getPos(), titleEntryWidget.getText(), urlEntryWidget.getText());
super.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public void render(HyperlinkBlockEntity entity, float f, MatrixStack matrices, V
if (hitResult instanceof BlockHitResult && ((BlockHitResult) hitResult).getBlockPos().equals(entity.getPos())) {
float scale = 0.025F;
matrices.scale(scale, scale, scale);
matrices.translate(-context.getTextRenderer().getWidth(entity.getUrl()) / 2F, -4, scale);
matrices.translate(-context.getTextRenderer().getWidth(entity.getText()) / 2F, -4, scale);
// Fixes shadow being rendered in front of actual text
matrices.scale(1, 1, -1);
context.getTextRenderer().draw(entity.getUrl(), 0, 0, 0xFFFFFF, true, matrices.peek().getPositionMatrix(), vertexConsumers, TextLayerType.NORMAL, 0, LightmapTextureManager.MAX_LIGHT_COORDINATE);
context.getTextRenderer().draw(entity.getText(), 0, 0, 0xFFFFFF, true, matrices.peek().getPositionMatrix(), vertexConsumers, TextLayerType.NORMAL, 0, LightmapTextureManager.MAX_LIGHT_COORDINATE);
}
matrices.pop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import net.minecraft.util.math.Vec2f;

public class GlowcaseClientNetworking {
public static void editHyperlinkBlock(BlockPos pos, String url) {
ClientPlayNetworking.send(new GlowcaseCommonNetworking.EditHyperlinkBlock(pos, url));
public static void editHyperlinkBlock(BlockPos pos, String title, String url) {
ClientPlayNetworking.send(new GlowcaseCommonNetworking.EditHyperlinkBlock(pos, title, url));
}

//TODO: Pretty spicy, copied from the old code. Should maybe break this into more packets, or dispatch off the type of property I'm setting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@

public class GlowcaseCommonNetworking {

public record EditHyperlinkBlock(BlockPos pos, String url) implements CustomPayload {
public record EditHyperlinkBlock(BlockPos pos, String title, String url) implements CustomPayload {
public static final Id<EditHyperlinkBlock> PACKET_ID = new Id<>(Glowcase.id("channel.hyperlink.save"));
public static final PacketCodec<RegistryByteBuf, EditHyperlinkBlock> PACKET_CODEC = PacketCodec.tuple(BlockPos.PACKET_CODEC, EditHyperlinkBlock::pos, PacketCodecs.STRING, EditHyperlinkBlock::url, EditHyperlinkBlock::new);
public static final PacketCodec<RegistryByteBuf, EditHyperlinkBlock> PACKET_CODEC = PacketCodec.tuple(BlockPos.PACKET_CODEC, EditHyperlinkBlock::pos, PacketCodecs.STRING, EditHyperlinkBlock::title, PacketCodecs.STRING, EditHyperlinkBlock::url, EditHyperlinkBlock::new);

public static void receive(EditHyperlinkBlock payload, ServerPlayNetworking.Context context) {
context.player().server.submit(() -> {
if(canEditGlowcase(context.player(), payload.pos(), Glowcase.HYPERLINK_BLOCK) && context.player().getServerWorld().getBlockEntity(payload.pos()) instanceof HyperlinkBlockEntity link && payload.url().length() <= URL_MAX_LENGTH) {
if(canEditGlowcase(context.player(), payload.pos(), Glowcase.HYPERLINK_BLOCK) && context.player().getServerWorld().getBlockEntity(payload.pos()) instanceof HyperlinkBlockEntity link && payload.url().length() <= URL_MAX_LENGTH && payload.title().length() <= TITLE_MAX_LENGTH) {
link.setTitle(payload.title());
link.setUrl(payload.url());
}
});
Expand Down Expand Up @@ -136,6 +137,7 @@ public Id<? extends CustomPayload> getId() {
}
}

private static final int TITLE_MAX_LENGTH = 1024;
private static final int URL_MAX_LENGTH = 1024;

public static void onInitialize() {
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/assets/glowcase/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@
"command.glowcase.failed.no_world": "Failed to send message; sender must be in a world",
"glowcase.mailbox.sender": "From %s",
"glowcase.mailbox.reminder1": "Right click to delete this message",
"glowcase.mailbox.reminder2": "Sneak+Right click to delete all messages from this sender"
"glowcase.mailbox.reminder2": "Sneak+Right click to delete all messages from this sender",
"gui.glowcase.title": "Title",
"gui.glowcase.url": "URL"
}

0 comments on commit f7f95be

Please sign in to comment.