From 1cf59bca37ac74c671a843da764119b6247f5c64 Mon Sep 17 00:00:00 2001 From: xkball <45330674+xkball@users.noreply.github.com> Date: Sat, 23 Nov 2024 23:51:31 +0800 Subject: [PATCH] Improve: holo sign color setter, dynamic change raw json sign block content. Took 1 hour 8 minutes --- .../RawJsonHolographicSignBlockEntity.java | 5 ++++ .../block/holo_sign/HolographicSignBlock.java | 24 ++++++++++++++++++- .../BaseHolographicSignEditingScreen.java | 19 +++++++-------- .../client/gui/widget/ObjectInputBox.java | 11 +++++++++ ...sonHolographicSignBlockEntityRenderer.java | 4 ++++ .../assets/powertool/lang/en_us.json | 3 ++- .../assets/powertool/lang/zh_cn.json | 3 ++- 7 files changed, 56 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/teacon/powertool/block/entity/RawJsonHolographicSignBlockEntity.java b/src/main/java/org/teacon/powertool/block/entity/RawJsonHolographicSignBlockEntity.java index b44e35c..3346c47 100644 --- a/src/main/java/org/teacon/powertool/block/entity/RawJsonHolographicSignBlockEntity.java +++ b/src/main/java/org/teacon/powertool/block/entity/RawJsonHolographicSignBlockEntity.java @@ -1,12 +1,14 @@ package org.teacon.powertool.block.entity; import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.minecraft.commands.ParserUtils; import net.minecraft.core.BlockPos; import net.minecraft.core.HolderLookup; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.ComponentSerialization; +import net.minecraft.network.chat.ComponentUtils; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockState; @@ -54,6 +56,9 @@ public void filterMessage(ServerPlayer player) { } else { this.forRender = forFilter; } + try { + this.forRender = ComponentUtils.updateForEntity(player.createCommandSourceStack(),forRender,null,0); + } catch (CommandSyntaxException ignored) {} this.setChanged(); if (level != null) { var state = this.getBlockState(); diff --git a/src/main/java/org/teacon/powertool/block/holo_sign/HolographicSignBlock.java b/src/main/java/org/teacon/powertool/block/holo_sign/HolographicSignBlock.java index e13681d..5804faa 100644 --- a/src/main/java/org/teacon/powertool/block/holo_sign/HolographicSignBlock.java +++ b/src/main/java/org/teacon/powertool/block/holo_sign/HolographicSignBlock.java @@ -10,12 +10,15 @@ import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.network.chat.ClickEvent; +import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.ItemInteractionResult; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.LevelAccessor; @@ -40,12 +43,14 @@ import org.teacon.powertool.block.entity.LinkHolographicSignBlockEntity; import org.teacon.powertool.block.entity.RawJsonHolographicSignBlockEntity; import org.teacon.powertool.network.client.OpenHolographicSignEditor; +import org.teacon.powertool.network.server.UpdateBlockEntityData; import org.teacon.powertool.utils.VanillaUtils; import javax.annotation.ParametersAreNonnullByDefault; import java.io.File; import java.net.URI; import java.net.URISyntaxException; +import java.util.List; @MethodsReturnNonnullByDefault @ParametersAreNonnullByDefault @@ -95,6 +100,12 @@ protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Lev return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; } + @Override + protected void neighborChanged(BlockState state, Level level, BlockPos pos, Block neighborBlock, BlockPos neighborPos, boolean movedByPiston) { + super.neighborChanged(state, level, pos, neighborBlock, neighborPos, movedByPiston); + + } + @Override public boolean hasDynamicShape() { return true; @@ -134,9 +145,20 @@ public BlockState updateShape(BlockState state, Direction facing, BlockState fac if (state.getValue(WATERLOGGED)) { level.scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickDelay(level)); } + if(this.type == SignType.RAW_JSON && level.isClientSide() && level.getBlockEntity(currentPos) instanceof RawJsonHolographicSignBlockEntity sign){ + PacketDistributor.sendToServer(UpdateBlockEntityData.create(sign)); + } return super.updateShape(state, facing, facingState, level, currentPos, facingPos); } - + + @Override + public void appendHoverText(ItemStack stack, Item.TooltipContext context, List tooltipComponents, TooltipFlag tooltipFlag) { + super.appendHoverText(stack, context, tooltipComponents, tooltipFlag); + if(type == SignType.RAW_JSON){ + tooltipComponents.add(Component.translatable("")); + } + } + @Override public FluidState getFluidState(BlockState state) { return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state); diff --git a/src/main/java/org/teacon/powertool/client/gui/holo_sign/BaseHolographicSignEditingScreen.java b/src/main/java/org/teacon/powertool/client/gui/holo_sign/BaseHolographicSignEditingScreen.java index c32872b..5c0e44f 100644 --- a/src/main/java/org/teacon/powertool/client/gui/holo_sign/BaseHolographicSignEditingScreen.java +++ b/src/main/java/org/teacon/powertool/client/gui/holo_sign/BaseHolographicSignEditingScreen.java @@ -12,7 +12,6 @@ import net.minecraft.client.gui.screens.Screen; import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.TextColor; import net.minecraft.world.level.block.entity.BlockEntity; import net.neoforged.neoforge.network.PacketDistributor; import org.jetbrains.annotations.NotNull; @@ -21,8 +20,10 @@ import org.teacon.powertool.block.entity.LinkHolographicSignBlockEntity; import org.teacon.powertool.block.entity.RawJsonHolographicSignBlockEntity; import org.teacon.powertool.block.holo_sign.SignType; +import org.teacon.powertool.client.gui.widget.ObjectInputBox; import org.teacon.powertool.network.server.UpdateBlockEntityData; +import java.util.Optional; import java.util.function.Supplier; public class BaseHolographicSignEditingScreen extends Screen { @@ -40,7 +41,7 @@ public class BaseHolographicSignEditingScreen colorInput; protected Button zOffsetToggle; protected Button shadowToggle; @@ -113,14 +114,12 @@ protected void init() { .size(80, 20) .createNarration(displayed -> Component.translatable("powertool.gui.holographic_sign.narration.shadow", displayed.get())) .build(); - - this.colorInput = new EditBox(this.minecraft.font, 200 + innerPadding * 2, 0, 50, 20, Component.empty()); - this.colorInput.setValue("#" + Integer.toHexString(this.colorInARGB)); - this.colorInput.setResponder(string -> { - TextColor color = TextColor.parseColor(this.colorInput.getValue()).result().orElse(null); - this.colorInARGB = color == null ? 0xFFFFFFFF : color.getValue() | 0xFF000000; - }); + + if(colorInARGB < 0) colorInARGB = 0xffffff; + this.colorInput = new ObjectInputBox<>(font, 200 + innerPadding * 2, 0, 50, 20, Component.empty(),ObjectInputBox.RGB_COLOR_VALIDATOR,ObjectInputBox.RGB_COLOR_RESPONDER); + this.colorInput.setValue(Integer.toHexString(this.colorInARGB).toUpperCase()); this.colorInput.setFocused(false); + this.colorInput.setMaxLength(6); this.colorInput.setCanLoseFocus(true); this.zOffsetToggle = new Button.Builder(this.layerArrange.displayName, btn -> { @@ -219,7 +218,7 @@ private void rotate(int degree){ } protected void writeBackToBE(){ - this.sign.colorInARGB = this.colorInARGB; + this.sign.colorInARGB = Optional.ofNullable(colorInput.get()).orElse(0xffffff); this.sign.scale = this.scale; this.sign.align = this.textAlign; this.sign.shadow = this.shadowType; diff --git a/src/main/java/org/teacon/powertool/client/gui/widget/ObjectInputBox.java b/src/main/java/org/teacon/powertool/client/gui/widget/ObjectInputBox.java index 32e66c1..beef6c3 100644 --- a/src/main/java/org/teacon/powertool/client/gui/widget/ObjectInputBox.java +++ b/src/main/java/org/teacon/powertool/client/gui/widget/ObjectInputBox.java @@ -76,10 +76,21 @@ public class ObjectInputBox extends EditBox implements Renderable { (str) -> Float.parseFloat(str) >= 0.0f && Float.parseFloat(str) <= 1.0f ); + public static final Predicate RGB_COLOR_VALIDATOR = (str) -> { + if(str.length() != 6 && str.length() != 8) return false; + try { + Integer.parseInt(str,16); + return true; + }catch (NumberFormatException e){ + return false; + } + }; + public static final Function PASS_RESPONDER = str -> str; public static final Function INT_RESPONDER = Integer::parseInt; public static final Function LONG_RESPONDER = Long::parseLong; public static final Function FLOAT_RESPONDER = Float::parseFloat; + public static final Function RGB_COLOR_RESPONDER = (str) -> Integer.parseInt(str,16); protected final Predicate validator; protected final Function responder; diff --git a/src/main/java/org/teacon/powertool/client/renders/holo_sign/RawJsonHolographicSignBlockEntityRenderer.java b/src/main/java/org/teacon/powertool/client/renders/holo_sign/RawJsonHolographicSignBlockEntityRenderer.java index 2a54371..0227ccb 100644 --- a/src/main/java/org/teacon/powertool/client/renders/holo_sign/RawJsonHolographicSignBlockEntityRenderer.java +++ b/src/main/java/org/teacon/powertool/client/renders/holo_sign/RawJsonHolographicSignBlockEntityRenderer.java @@ -1,6 +1,7 @@ package org.teacon.powertool.client.renders.holo_sign; import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Font; import net.minecraft.client.gui.screens.Screen; @@ -9,8 +10,11 @@ import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider; import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.ComponentUtils; import net.minecraft.network.chat.HoverEvent; import net.minecraft.world.phys.BlockHitResult; +import net.neoforged.neoforge.client.ClientCommandHandler; +import net.neoforged.neoforge.client.ClientCommandSourceStack; import org.teacon.powertool.block.entity.RawJsonHolographicSignBlockEntity; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/src/main/resources/assets/powertool/lang/en_us.json b/src/main/resources/assets/powertool/lang/en_us.json index fd6cee8..a1f0e2e 100644 --- a/src/main/resources/assets/powertool/lang/en_us.json +++ b/src/main/resources/assets/powertool/lang/en_us.json @@ -148,5 +148,6 @@ "tooltip.powertool.marting": "MalayP asked me to write a karting car", "powertool.gui.register.rev": "Require Item:", "powertool.gui.register.sup": "Supply Item (Optional):", - "powertool.gui.register.display_supply": "Display Supply Item On HUD" + "powertool.gui.register.display_supply": "Display Supply Item On HUD", + "tooltip.powertool.raw_json_holo_sign_block":"Dynamic update content when block shape update.(eg. scoreboard)" } \ No newline at end of file diff --git a/src/main/resources/assets/powertool/lang/zh_cn.json b/src/main/resources/assets/powertool/lang/zh_cn.json index d1ecce5..50f7abc 100644 --- a/src/main/resources/assets/powertool/lang/zh_cn.json +++ b/src/main/resources/assets/powertool/lang/zh_cn.json @@ -149,5 +149,6 @@ "tooltip.powertool.marting": "马雷让写的卡丁车,简称马丁车", "powertool.gui.register.rev": "需求物品:", "powertool.gui.register.sup": "提供物品(可选):", - "powertool.gui.register.display_supply": "在HUD显示提供的物品" + "powertool.gui.register.display_supply": "在HUD显示提供的物品", + "tooltip.powertool.raw_json_holo_sign_block":"在方块形状更新时动态更新内容(例: 计分板)." } \ No newline at end of file