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

Bug fixes and quality of life features #24

Merged
merged 16 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import net.minecraft.block.BlockState;
import net.minecraft.block.ShapeContext;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ConfirmLinkScreen;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.entity.LivingEntity;
Expand Down Expand Up @@ -61,8 +63,9 @@ protected ActionResult onUse(BlockState state, World world, BlockPos pos, Player
@Override
protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (!(world.getBlockEntity(pos) instanceof HyperlinkBlockEntity be)) return ItemActionResult.CONSUME;
if (world.isClient && player.getStackInHand(hand).isIn(Glowcase.ITEM_TAG) && canEditGlowcase(player, pos)) {
Glowcase.proxy.openHyperlinkBlockEditScreen(pos);
if (player.getStackInHand(hand).isIn(Glowcase.ITEM_TAG) && canEditGlowcase(player, pos)) {
if (world.isClient) { Glowcase.proxy.openHyperlinkBlockEditScreen(pos); }
return ItemActionResult.SUCCESS;
}
return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected ActionResult onUse(BlockState state, World world, BlockPos pos, Player
if(!(world.getBlockEntity(pos) instanceof ItemDisplayBlockEntity be)) return ActionResult.CONSUME;

if (be.canGiveTo(player)) {
if(!world.isClient) be.giveTo(player, player.getStackInHand(Hand.MAIN_HAND).isEmpty() ? Hand.MAIN_HAND : Hand.OFF_HAND);
if (!world.isClient) be.giveTo(player);
return ActionResult.SUCCESS;
}

Expand Down
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 @@ -147,7 +147,8 @@ public ItemStack getDisplayedStack() {
// -> yes, that means the setBlockState call is wacky
public void cycleRotationType(PlayerEntity playerEntity) {
switch (this.rotationType) {
case TRACKING -> {
case TRACKING -> this.rotationType = RotationType.BILLBOARD;
case BILLBOARD -> {
this.rotationType = RotationType.HORIZONTAL;
if (this.world != null) {
this.world.setBlockState(this.pos, this.getCachedState().with(Properties.ROTATION, MathHelper.floor((double) ((playerEntity.getYaw()) * 16.0F / 360.0F) + 0.5D) & 15));
Expand Down Expand Up @@ -190,19 +191,29 @@ else return switch(this.givesItem) {
};
}

public void giveTo(PlayerEntity player, Hand hand) {
player.setStackInHand(hand, getDisplayedStack().copy());
public void giveTo(PlayerEntity player) {
ItemStack itemStack = player.getStackInHand(Hand.MAIN_HAND);
boolean holdingSameAsDisplay = ItemStack.areItemsEqual(getDisplayedStack(), itemStack);
sisby-folk marked this conversation as resolved.
Show resolved Hide resolved

if (itemStack.isEmpty()) {
player.setStackInHand(Hand.MAIN_HAND, getDisplayedStack().copy());
} else if (holdingSameAsDisplay) {
itemStack.increment(getDisplayedStack().getCount());
itemStack.capCount(itemStack.getMaxCount());
player.setStackInHand(Hand.MAIN_HAND, itemStack);
}

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

@Environment(EnvType.CLIENT)
public static Vec2f getPitchAndYaw(Entity camera, BlockPos pos) {
double d = pos.getX() - camera.getPos().x + 0.5;
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;
double f = pos.getZ() - camera.getPos().z + 0.5;
double f = pos.getZ() - camera.getLerpedPos(delta).z + 0.5;
double g = MathHelper.sqrt((float) (d * d + f * f));

float pitch = (float) ((-MathHelper.atan2(e, g)));
Expand All @@ -219,7 +230,7 @@ public static void tick(World world, BlockPos blockPos, BlockState state, ItemDi
}

public enum RotationType {
LOCKED, TRACKING, HORIZONTAL
LOCKED, TRACKING, HORIZONTAL, BILLBOARD
}

public enum GivesItem {
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.urlEntryWidget.setText(this.hyperlinkBlockEntity.getUrl());
this.titleEntryWidget = new TextFieldWidget(this.client.textRenderer, width / 10, height / 2 - 30, 8 * width / 10, 20, Text.empty());
this.titleEntryWidget.setMaxLength(Integer.MAX_VALUE);
sisby-folk marked this conversation as resolved.
Show resolved Hide resolved
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);
sisby-folk marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -225,7 +225,7 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
));
this.textBlockEntity.renderDirty = true;
++this.currentRow;
this.selectionManager.moveCursorToEnd(false);
this.selectionManager.moveCursorToStart(false);
return true;
} else if (keyCode == GLFW.GLFW_KEY_UP) {
this.currentRow = Math.max(this.currentRow - 1, 0);
Expand All @@ -245,7 +245,28 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
return true;
} else {
try {
return this.selectionManager.handleSpecialKey(keyCode) || super.keyPressed(keyCode, scanCode, modifiers);
boolean val = this.selectionManager.handleSpecialKey(keyCode) || super.keyPressed(keyCode, scanCode, modifiers);
int selectionOffset = this.textBlockEntity.getRawLine(this.currentRow).length() - this.selectionManager.getSelectionStart();

// Find line feed characters and create proper newlines
for (int i = 0; i < this.textBlockEntity.lines.size(); ++i) {
int lineFeedIndex = this.textBlockEntity.getRawLine(i).indexOf("\n");

if (lineFeedIndex >= 0) {
this.textBlockEntity.addRawLine(i + 1,
this.textBlockEntity.getRawLine(i).substring(
MathHelper.clamp(lineFeedIndex + 1, 0, this.textBlockEntity.getRawLine(i).length())
));
this.textBlockEntity.setRawLine(i,
this.textBlockEntity.getRawLine(i).substring(0, MathHelper.clamp(lineFeedIndex, 0, this.textBlockEntity.getRawLine(i).length())
));
this.textBlockEntity.renderDirty = true;
++this.currentRow;
this.selectionManager.moveCursorToEnd(false);
this.selectionManager.moveCursor(-selectionOffset);
}
}
return val;
} catch (StringIndexOutOfBoundsException e) {
e.printStackTrace();
MinecraftClient.getInstance().setScreen(null);
Expand Down
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 @@ -39,11 +39,16 @@ public void render(ItemDisplayBlockEntity entity, float tickDelta, MatrixStack m

switch (entity.rotationType) {
case TRACKING -> {
Vec2f pitchAndYaw = ItemDisplayBlockEntity.getPitchAndYaw(camera, entity.getPos());
Vec2f pitchAndYaw = ItemDisplayBlockEntity.getPitchAndYaw(camera, entity.getPos(), tickDelta);
pitch = pitchAndYaw.x;
yaw = pitchAndYaw.y;
matrices.multiply(RotationAxis.POSITIVE_Y.rotation(yaw));
}
case BILLBOARD -> {
pitch = (float) Math.toRadians(camera.getPitch());
yaw = (float) Math.toRadians(-camera.getYaw());
matrices.multiply(RotationAxis.POSITIVE_Y.rotation(yaw));
}
case HORIZONTAL -> {
var rotation = -(entity.getCachedState().get(Properties.ROTATION) * 2 * Math.PI) / 16.0F;
matrices.multiply(RotationAxis.POSITIVE_Y.rotation((float)rotation));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
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.
public static void editItemDisplayBlock(ItemDisplayBlockEntity be, boolean updatePitchAndYaw) {
if (updatePitchAndYaw && MinecraftClient.getInstance().getCameraEntity() != null) {
Vec2f pitchAndYaw = ItemDisplayBlockEntity.getPitchAndYaw(MinecraftClient.getInstance().getCameraEntity(), be.getPos());
Vec2f pitchAndYaw = ItemDisplayBlockEntity.getPitchAndYaw(MinecraftClient.getInstance().getCameraEntity(), be.getPos(), 0);
be.pitch = pitchAndYaw.x;
be.yaw = pitchAndYaw.y;
}
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"
}
Loading