Sign GUI rendering fix

This commit is contained in:
paulevsGitch 2022-05-06 21:20:53 +03:00
parent 0d6d24b748
commit b60d20196f
4 changed files with 74 additions and 282 deletions

View file

@ -105,8 +105,7 @@ public class BaseSignBlock extends SignBlock implements BlockModelProvider, Cust
world.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(world)); world.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(world));
} }
if (!canSurvive(state, world, pos)) { if (!canSurvive(state, world, pos)) {
return state.getValue(WATERLOGGED) ? state.getFluidState() return state.getValue(WATERLOGGED) ? state.getFluidState().createLegacyBlock() : Blocks.AIR.defaultBlockState();
.createLegacyBlock() : Blocks.AIR.defaultBlockState();
} }
return super.updateShape(state, facing, neighborState, world, pos, neighborPos); return super.updateShape(state, facing, neighborState, world, pos, neighborPos);
} }
@ -126,10 +125,11 @@ public class BaseSignBlock extends SignBlock implements BlockModelProvider, Cust
public BlockState getStateForPlacement(BlockPlaceContext ctx) { public BlockState getStateForPlacement(BlockPlaceContext ctx) {
if (ctx.getClickedFace() == Direction.UP) { if (ctx.getClickedFace() == Direction.UP) {
FluidState fluidState = ctx.getLevel().getFluidState(ctx.getClickedPos()); FluidState fluidState = ctx.getLevel().getFluidState(ctx.getClickedPos());
return this.defaultBlockState() return this
.setValue(FLOOR, true) .defaultBlockState()
.setValue(ROTATION, Mth.floor((180.0 + ctx.getRotation() * 16.0 / 360.0) + 0.5 - 12) & 15) .setValue(FLOOR, true)
.setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER); .setValue(ROTATION, Mth.floor((180.0 + ctx.getRotation() * 16.0 / 360.0) + 0.5 - 12) & 15)
.setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER);
} }
else if (ctx.getClickedFace() != Direction.DOWN) { else if (ctx.getClickedFace() != Direction.DOWN) {
BlockState blockState = this.defaultBlockState(); BlockState blockState = this.defaultBlockState();
@ -144,8 +144,7 @@ public class BaseSignBlock extends SignBlock implements BlockModelProvider, Cust
int rot = Mth.floor((180.0 + dir.toYRot() * 16.0 / 360.0) + 0.5 + 4) & 15; int rot = Mth.floor((180.0 + dir.toYRot() * 16.0 / 360.0) + 0.5 + 4) & 15;
blockState = blockState.setValue(ROTATION, rot); blockState = blockState.setValue(ROTATION, rot);
if (blockState.canSurvive(worldView, blockPos)) { if (blockState.canSurvive(worldView, blockPos)) {
return blockState.setValue(FLOOR, false) return blockState.setValue(FLOOR, false).setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER);
.setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER);
} }
} }
} }
@ -163,12 +162,12 @@ public class BaseSignBlock extends SignBlock implements BlockModelProvider, Cust
@Override @Override
public BlockState rotate(BlockState state, Rotation rotation) { public BlockState rotate(BlockState state, Rotation rotation) {
return state.setValue(ROTATION, rotation.rotate((Integer) state.getValue(ROTATION), 16)); return state.setValue(ROTATION, rotation.rotate(state.getValue(ROTATION), 16));
} }
@Override @Override
public BlockState mirror(BlockState state, Mirror mirror) { public BlockState mirror(BlockState state, Mirror mirror) {
return state.setValue(ROTATION, mirror.mirror((Integer) state.getValue(ROTATION), 16)); return state.setValue(ROTATION, mirror.mirror(state.getValue(ROTATION), 16));
} }
@Override @Override

View file

@ -1,270 +0,0 @@
package ru.bclib.client.gui;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.platform.Lighting;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.math.Matrix4f;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.Util;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.font.TextFieldHelper;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.model.geom.ModelLayers;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.blockentity.SignRenderer;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.network.protocol.game.ServerboundSignUpdatePacket;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.WoodType;
import ru.bclib.blockentities.BaseSignBlockEntity;
import ru.bclib.blocks.BaseSignBlock;
import ru.bclib.client.render.BaseSignBlockEntityRenderer;
import java.util.Arrays;
@Environment(EnvType.CLIENT)
public class BlockSignEditScreen extends Screen {
private final BaseSignBlockEntity sign;
private int ticksSinceOpened;
private int currentRow;
private TextFieldHelper selectionManager;
private final String[] text = (String[]) Util.make(new String[4], (strings) -> {
Arrays.fill(strings, "");
});
private SignRenderer.SignModel model;
public BlockSignEditScreen(BaseSignBlockEntity sign) {
super(new TranslatableComponent("sign.edit"));
this.sign = sign;
}
protected void init() {
//set up a default model
model = new SignRenderer.SignModel(this.minecraft.getEntityModels()
.bakeLayer(ModelLayers.createSignModelName(WoodType.OAK)));
minecraft.keyboardHandler.setSendRepeatsToGui(true);
this.addRenderableWidget(new Button(
this.width / 2 - 100,
this.height / 4 + 120,
200,
20,
CommonComponents.GUI_DONE,
(buttonWidget) -> {
this.finishEditing();
}
));
this.sign.setEditable(false);
this.selectionManager = new TextFieldHelper(
() -> {
return this.text[this.currentRow];
},
(string) -> {
this.text[this.currentRow] = string;
this.sign.setMessage(this.currentRow, new TextComponent(string));
},
TextFieldHelper.createClipboardGetter(this.minecraft),
TextFieldHelper.createClipboardSetter(this.minecraft),
(string) -> {
return this.minecraft.font.width(string) <= 90;
}
);
}
public void removed() {
minecraft.keyboardHandler.setSendRepeatsToGui(false);
ClientPacketListener clientPlayNetworkHandler = this.minecraft.getConnection();
if (clientPlayNetworkHandler != null) {
clientPlayNetworkHandler.send(new ServerboundSignUpdatePacket(
this.sign.getBlockPos(),
this.text[0],
this.text[1],
this.text[2],
this.text[3]
));
}
this.sign.setEditable(true);
}
public void tick() {
++this.ticksSinceOpened;
if (!this.sign.getType().isValid(this.sign.getBlockState())) {
this.finishEditing();
}
}
private void finishEditing() {
this.sign.setChanged();
this.minecraft.setScreen((Screen) null);
}
public boolean charTyped(char chr, int keyCode) {
this.selectionManager.charTyped(chr);
return true;
}
public void onClose() {
this.finishEditing();
}
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (keyCode == 265) {
this.currentRow = this.currentRow - 1 & 3;
this.selectionManager.setCursorToEnd();
return true;
}
else if (keyCode != 264 && keyCode != 257 && keyCode != 335) {
return selectionManager.keyPressed(keyCode) || super.keyPressed(keyCode, scanCode, modifiers);
}
else {
this.currentRow = this.currentRow + 1 & 3;
this.selectionManager.setCursorToEnd();
return true;
}
}
public void render(PoseStack matrices, int mouseX, int mouseY, float delta) {
Lighting.setupForFlatItems();
this.renderBackground(matrices);
GuiComponent.drawCenteredString(matrices, this.font, this.title, this.width / 2, 40, 16777215);
matrices.pushPose();
matrices.translate((double) (this.width / 2), 0.0D, 50.0D);
matrices.scale(93.75F, -93.75F, 93.75F);
matrices.translate(0.0D, -1.3125D, 0.0D);
BlockState blockState = this.sign.getBlockState();
boolean bl = blockState.getValue(BaseSignBlock.FLOOR);
if (!bl) {
matrices.translate(0.0D, -0.3125D, 0.0D);
}
boolean bl2 = this.ticksSinceOpened / 6 % 2 == 0;
matrices.pushPose();
matrices.scale(0.6666667F, -0.6666667F, -0.6666667F);
MultiBufferSource.BufferSource immediate = minecraft.renderBuffers().bufferSource();
VertexConsumer vertexConsumer = BaseSignBlockEntityRenderer.getConsumer(immediate, blockState.getBlock());
model.root.getChild("sign").render(matrices, vertexConsumer, 15728880, OverlayTexture.NO_OVERLAY);
if (bl) {
model.stick.render(matrices, vertexConsumer, 15728880, OverlayTexture.NO_OVERLAY);
}
matrices.popPose();
matrices.translate(0.0D, 0.3333333432674408D, 0.046666666865348816D);
matrices.scale(0.010416667F, -0.010416667F, 0.010416667F);
int i = this.sign.getColor().getTextColor();
int j = this.selectionManager.getCursorPos();
int k = this.selectionManager.getSelectionPos();
int l = this.currentRow * 10 - this.text.length * 5;
Matrix4f matrix4f = matrices.last().pose();
int m;
String string2;
int s;
int t;
for (m = 0; m < this.text.length; ++m) {
string2 = this.text[m];
if (string2 != null) {
if (this.font.isBidirectional()) {
string2 = this.font.bidirectionalShaping(string2);
}
float n = (float) (-this.minecraft.font.width(string2) / 2);
this.minecraft.font.drawInBatch(
string2,
n,
(float) (m * 10 - this.text.length * 5),
i,
false,
matrix4f,
immediate,
false,
0,
15728880,
false
);
if (m == this.currentRow && j >= 0 && bl2) {
s = this.minecraft.font.width(string2.substring(0, Math.max(Math.min(j, string2.length()), 0)));
t = s - this.minecraft.font.width(string2) / 2;
if (j >= string2.length()) {
this.minecraft.font.drawInBatch(
"_",
(float) t,
(float) l,
i,
false,
matrix4f,
immediate,
false,
0,
15728880,
false
);
}
}
}
}
immediate.endBatch();
for (m = 0; m < this.text.length; ++m) {
string2 = this.text[m];
if (string2 != null && m == this.currentRow && j >= 0) {
int r = this.minecraft.font.width(string2.substring(0, Math.max(Math.min(j, string2.length()), 0)));
s = r - this.minecraft.font.width(string2) / 2;
if (bl2 && j < string2.length()) {
int var31 = l - 1;
int var10003 = s + 1;
this.minecraft.font.getClass();
fill(matrices, s, var31, var10003, l + 9, -16777216 | i);
}
if (k != j) {
t = Math.min(j, k);
int u = Math.max(j, k);
int v = this.minecraft.font.width(string2.substring(0, t)) - this.minecraft.font.width(string2) / 2;
int w = this.minecraft.font.width(string2.substring(0, u)) - this.minecraft.font.width(string2) / 2;
int x = Math.min(v, w);
int y = Math.max(v, w);
Tesselator tessellator = Tesselator.getInstance();
BufferBuilder bufferBuilder = tessellator.getBuilder();
RenderSystem.disableTexture();
RenderSystem.enableColorLogicOp();
RenderSystem.logicOp(GlStateManager.LogicOp.OR_REVERSE);
bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
float var32 = (float) x;
bufferBuilder.vertex(matrix4f, var32, (float) (l + 9), 0.0F).color(0, 0, 255, 255).endVertex();
var32 = (float) y;
bufferBuilder.vertex(matrix4f, var32, (float) (l + 9), 0.0F).color(0, 0, 255, 255).endVertex();
bufferBuilder.vertex(matrix4f, (float) y, (float) l, 0.0F).color(0, 0, 255, 255).endVertex();
bufferBuilder.vertex(matrix4f, (float) x, (float) l, 0.0F).color(0, 0, 255, 255).endVertex();
bufferBuilder.end();
BufferUploader.end(bufferBuilder);
RenderSystem.disableColorLogicOp();
RenderSystem.enableTexture();
}
}
}
matrices.popPose();
Lighting.setupFor3DItems();
super.render(matrices, mouseX, mouseY, delta);
}
}

View file

@ -0,0 +1,62 @@
package ru.bclib.mixin.client;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.SignEditScreen;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.blockentity.SignRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.SignBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import ru.bclib.blocks.BaseSignBlock;
import ru.bclib.client.render.BaseSignBlockEntityRenderer;
@Mixin(SignEditScreen.class)
public abstract class SignEditScreenMixin extends Screen {
@Shadow @Final private SignBlockEntity sign;
@Shadow private SignRenderer.SignModel signModel;
@Unique private boolean bclib_renderStick;
@Unique private boolean bclib_isSign;
protected SignEditScreenMixin(Component component) {
super(component);
}
@Inject(method = "render(Lcom/mojang/blaze3d/vertex/PoseStack;IIF)V", at = @At(
value = "INVOKE",
target = "Lcom/mojang/blaze3d/vertex/PoseStack;pushPose()V",
shift = Shift.BEFORE
), locals = LocalCapture.CAPTURE_FAILSOFT)
private void bclib_checkOffset(PoseStack poseStack, int i, int j, float f, CallbackInfo info, float g, BlockState blockState, boolean bl, boolean bl2, float h) {
bclib_isSign = blockState.getBlock() instanceof BaseSignBlock;
if (bclib_isSign) {
bclib_renderStick = blockState.getValue(BaseSignBlock.FLOOR);
if (bclib_renderStick) {
poseStack.translate(0.0, 0.3125, 0.0);
}
}
}
@ModifyArg(method = "render(Lcom/mojang/blaze3d/vertex/PoseStack;IIF)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/model/geom/ModelPart;render(Lcom/mojang/blaze3d/vertex/PoseStack;Lcom/mojang/blaze3d/vertex/VertexConsumer;II)V"), index = 1)
private VertexConsumer bclib_signRender(VertexConsumer consumer) {
if (bclib_isSign) {
signModel.stick.visible = bclib_renderStick;
Block block = sign.getBlockState().getBlock();
MultiBufferSource.BufferSource bufferSource = this.minecraft.renderBuffers().bufferSource();
return BaseSignBlockEntityRenderer.getConsumer(bufferSource, block);
}
return consumer;
}
}

View file

@ -5,6 +5,7 @@
"compatibilityLevel": "JAVA_17", "compatibilityLevel": "JAVA_17",
"client": [ "client": [
"ClientRecipeBookMixin", "ClientRecipeBookMixin",
"SignEditScreenMixin",
"ModelManagerMixin", "ModelManagerMixin",
"TextureAtlasMixin", "TextureAtlasMixin",
"AnvilScreenMixin", "AnvilScreenMixin",
@ -12,8 +13,8 @@
"ModelBakeryMixin", "ModelBakeryMixin",
"WorldPresetMixin", "WorldPresetMixin",
"MinecraftMixin", "MinecraftMixin",
"GameMixin", "BlockMixin",
"BlockMixin" "GameMixin"
], ],
"injectors": { "injectors": {
"defaultRequire": 1 "defaultRequire": 1