Skip to content

Commit

Permalink
Add support for 1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
wifi-left committed Jun 8, 2023
1 parent c4cbf00 commit 499270c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 49 deletions.
16 changes: 8 additions & 8 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ org.gradle.parallel=true

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.19.4
yarn_mappings=1.19.4+build.2
loader_version=0.14.21
minecraft_version=1.20
yarn_mappings=1.20+build.1
loader_version=0.14.21

#Fabric api
fabric_version=0.83.0+1.19.4
fabric_version=0.83.0+1.20
# Mod Properties
mod_version = 1.0.1
mod_version = 1.0.2
maven_group = io.wifi.signgui
archives_base_name = BambooSignEditor

loom_libraries_base=https://download.mcbbs.net/maven/
loom_resources_base=https://download.mcbbs.net/assets/
loom_version_manifests=http://127.0.0.1/api/vm.php
# loom_libraries_base=https://download.mcbbs.net/maven/
# loom_resources_base=https://download.mcbbs.net/assets/
loom_version_manifests=http://127.0.0.1/api/vl_1_20.json
41 changes: 26 additions & 15 deletions src/main/java/io/wifi/signgui/MyGuiScreen.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package io.wifi.signgui;


import io.netty.buffer.Unpooled;
import net.minecraft.block.entity.SignBlockEntity;
import net.minecraft.block.entity.SignText;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.gui.widget.TextWidget;
import net.minecraft.client.gui.widget.AbstractTextWidget;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.text.TextColor;
import net.minecraft.text.ClickEvent.Action;
import net.minecraft.util.math.BlockPos;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;

Expand All @@ -23,6 +25,7 @@ public class MyGuiScreen extends Screen {
private final TextFieldWidget[] textFields = new TextFieldWidget[4];
private final TextFieldWidget[] colorFields = new TextFieldWidget[4];
private final TextFieldWidget[] commandField = new TextFieldWidget[4];

// 创建一个文本框,用来编辑告示牌绑定的命令
// 创建一个告示牌方块实体对象,用来获取和设置告示牌的数据

Expand All @@ -41,9 +44,10 @@ protected void init() {
super.init();
// this.client.setRep(true); // 设置键盘重复事件
// 遍历告示牌的每一行文本
SignText signText = sign.getText(signguiMain.textIsFront);
for (int i = 0; i < 4; ++i) {
// 获取告示牌的文本内容
MutableText line = (MutableText) sign.getTextOnRow(i, false);
MutableText line = (MutableText) signText.getMessage(i, false);
String text = line.getString().replaceAll("§", "&");
String command = "";

Expand Down Expand Up @@ -99,16 +103,15 @@ protected void init() {
this.commandField[i] = commandField;
this.colorFields[i] = colorField;

this.addDrawableChild(this.colorFields[i]); // 添加文本框对象到GUI中
this.addDrawableChild(this.textFields[i]); // 添加文本框对象到GUI中
this.addDrawableChild(this.colorFields[i]); // 添加文本框对象到GUI中
this.addDrawableChild(this.commandField[i]); // 添加文本框对象到GUI中

}

confirmButton = ButtonWidget.builder(Text.translatable("gui.ok"), button -> {
// 确认按钮的点击事件,发送数据包给服务器,更新告示牌的文本和命令
BlockPos pos = sign.getPos();


PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeBlockPos(pos);
Expand All @@ -123,7 +126,7 @@ protected void init() {
buf.writeString(ColorName);
buf.writeString(cmd);
}
ClientPlayNetworking.send(signgui.UPDATE_SIGN_PACKET_ID, buf);
ClientPlayNetworking.send(signguiMain.UPDATE_SIGN_PACKET_ID, buf);
// 关闭 GUI & 修改文本
this.close();
}).position(this.width / 2 - 104, 48 + 4 * 48).size(100, 20).build();
Expand All @@ -133,9 +136,10 @@ protected void init() {
{
// 取消按钮的点击事件,关闭 GUI
this.close();
}).position(this.width / 2 + 4, 4 * 48 + 48).size(100,20).build();
}).position(this.width / 2 + 4, 4 * 48 + 48).size(100, 20).build();

this.addDrawableChild(confirmButton); // 添加确认按钮对象到GUI中
// this.addDrawableChild(titletip); // 添加取消按钮对象到GUI中
this.addDrawableChild(cancelButton); // 添加取消按钮对象到GUI中
// 创建一个文本框对象,并设置其位置、大小、最大长度等属性

Expand Down Expand Up @@ -165,19 +169,26 @@ public void removed() {
// sign.markDirty(); // 标记告示牌方块实体为脏数据,以便同步到服务器端
}

private void drawCenteredTextWithShadow(DrawContext matrices, TextRenderer textRenderer, Text text, int x, int y,
int color, boolean shadow) {
matrices.drawText(textRenderer, text, x, y, color, shadow);
}

@Override
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
public void render(DrawContext matrices, int mouseX, int mouseY, float delta) {
this.renderBackground(matrices); // 渲染背景
// this.dra(this.title, this.width / 2, 20, 0xFFFFFFFF, false, , null, null,
// 0x00FFFFFF, 0x00FFFFFF);

drawCenteredTextWithShadow(matrices, this.textRenderer, this.title, this.width / 2, 20, 16777215); // 渲染标题
drawCenteredTextWithShadow(matrices, this.textRenderer, this.title, this.width / 2 - 30, 20, 0xffffff, true); // 渲染标题0xAARRGGBB
for (int i = 0; i < 4; ++i) {
// 20 + i * 48
drawCenteredTextWithShadow(matrices, this.textRenderer,
Text.translatable("gui.wifi.signgui.signtext", i + 1), this.width / 2 - 170, 52 + i * 48,
16777215); // 渲染文本标签
Text.translatable("gui.wifi.signgui.signtext", i + 1), this.width / 2 - 220, 52 + i * 48,
0xffffff, true); // 渲染文本标签
drawCenteredTextWithShadow(matrices, this.textRenderer,
Text.translatable("gui.wifi.signgui.signcmd", i + 1), this.width / 2 - 170, 72 + i * 48,
16777215); // 渲染命令标签
Text.translatable("gui.wifi.signgui.signcmd", i + 1), this.width / 2 - 220, 72 + i * 48,
0xffffff, true); // 渲染命令标签

}

Expand Down
11 changes: 0 additions & 11 deletions src/main/java/io/wifi/signgui/signgui.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,23 @@
import org.lwjgl.glfw.GLFW;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.SignBlockEntity;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.text.TextColor;
import net.minecraft.text.ClickEvent.Action;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.ServerWorldAccess;

public class signgui implements ClientModInitializer {
// 定义一个键绑定
private static final KeyBinding keyBinding = new KeyBinding("key.signeditorgui.open_gui", InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_V, "category.signeditorgui");
// 定义一个数据包标识符,用于更新告示牌的文本和命令
public static final Identifier UPDATE_SIGN_PACKET_ID = new Identifier("signeditorgui", "update_sign");

@Override
public void onInitializeClient() {
Expand Down
31 changes: 17 additions & 14 deletions src/main/java/io/wifi/signgui/signguiMain.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package io.wifi.signgui;
// MyMod.java

import org.lwjgl.glfw.GLFW;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.SignBlockEntity;
import net.minecraft.block.entity.SignText;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.MutableText;
Expand All @@ -20,14 +16,13 @@
import net.minecraft.text.ClickEvent.Action;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.ServerWorldAccess;

public class signguiMain implements ModInitializer {
// 定义一个数据包标识符,用于更新告示牌的文本和命令
public static final Identifier UPDATE_SIGN_PACKET_ID = new Identifier("signeditorgui", "update_sign");
public static final boolean textIsFront = true;

@Override
public void onInitialize() {
Expand Down Expand Up @@ -58,6 +53,7 @@ public void onInitialize() {
// 检查方块是否是告示牌
if (be instanceof SignBlockEntity) {
SignBlockEntity sign = (SignBlockEntity) be;
SignText signText = sign.getText(signguiMain.textIsFront);
for (int i = 0; i < 4; ++i) {
// 获取文本框中输入的内容,并解析颜色代码(如果有的话)
String text = textCache[i];
Expand All @@ -76,21 +72,28 @@ public void onInitialize() {
} else {
literalText.setStyle(literalText.getStyle().withColor(textColor));
}
sign.setTextOnRow(i, literalText); // 设置告示牌方块实体的文本内容
// signText.withMessage
signText = signText.withMessage(i, literalText); // 设置告示牌方块实体的文本内容
}
boolean res = sign.setText(signText, signguiMain.textIsFront);
// System.out.print("Modify result: "+res);
sign.markDirty();
// player.openEditSignScreen(sign);
player.networkHandler.sendPacket(new BlockUpdateS2CPacket(world, sign.getPos()));
player.networkHandler.sendPacket(sign.toUpdatePacket());
// world.updateNeighbors(signPos, signState.getBlock());
// world.syncWorldEvent(client, 0, signPos, 0);
// world.getChunk(signPos).markBlockForPostProcessing(signPos);
style.withColor((TextColor.fromFormatting(Formatting.GREEN)));
client.sendMessage(Text.translatable("msg.signgui.success").setStyle(style));
if (res) {
client.sendMessage(Text.translatable("msg.signgui.success").setStyle(style));
} else {
client.sendMessage(
Text.translatable("msg.signgui.unexpected", "Cannot modify the sign block").setStyle(style));
}
} else {
String out = (client == null ? "NULL" : client.getWorld()) + ":" + signPos.getX() + " "
+ signPos.getY() + " " + signPos.getZ();
style.withColor(TextColor.fromFormatting(Formatting.RED));
client.sendMessage(Text.translatable("msg.signgui.unexpected", out).setStyle(style));
// String out = (client == null ? "NULL" : client.()) + ":" + signPos.getX() + "
// "
// + signPos.getY() + " " + signPos.getZ();
return;
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"depends": {
"fabricloader": ">=0.14.16",
"fabric-api": "*",
"minecraft": ">=1.19.3",
"minecraft": ">=1.20",
"java": ">=17"
}
}

0 comments on commit 499270c

Please sign in to comment.