Skip to content

Commit

Permalink
Improve: holo sign color setter, dynamic change raw json sign block c…
Browse files Browse the repository at this point in the history
…ontent.

Took 1 hour 8 minutes
  • Loading branch information
xkball committed Nov 23, 2024
1 parent 0c06333 commit 1cf59bc
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Component> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<T extends BaseHolographicSignBlockEntity> extends Screen {
Expand All @@ -40,7 +41,7 @@ public class BaseHolographicSignEditingScreen<T extends BaseHolographicSignBlock
protected boolean bidirectional;

protected Button changeAlignment;
protected EditBox colorInput;
protected ObjectInputBox<Integer> colorInput;
protected Button zOffsetToggle;
protected Button shadowToggle;

Expand Down Expand Up @@ -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 -> {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,21 @@ public class ObjectInputBox<T> extends EditBox implements Renderable {
(str) -> Float.parseFloat(str) >= 0.0f && Float.parseFloat(str) <= 1.0f
);

public static final Predicate<String> 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<String,String> PASS_RESPONDER = str -> str;
public static final Function<String,Integer> INT_RESPONDER = Integer::parseInt;
public static final Function<String,Long> LONG_RESPONDER = Long::parseLong;
public static final Function<String,Float> FLOAT_RESPONDER = Float::parseFloat;
public static final Function<String,Integer> RGB_COLOR_RESPONDER = (str) -> Integer.parseInt(str,16);

protected final Predicate<String> validator;
protected final Function<String, T> responder;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/assets/powertool/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/powertool/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -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":"在方块形状更新时动态更新内容(例: 计分板)."
}

0 comments on commit 1cf59bc

Please sign in to comment.