From 7e7398f02e224dda47c0fe4c0ca5ca2b39499c87 Mon Sep 17 00:00:00 2001 From: goumo <76694659+goumo@users.noreply.github.com> Date: Wed, 15 Jan 2025 01:46:18 +0800 Subject: [PATCH] =?UTF-8?q?Tip=E7=BC=96=E8=BE=91=E5=99=A8:=201.=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BA=86=E5=B0=86=E5=86=85=E5=AE=B9=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E4=B8=BA=E7=BF=BB=E8=AF=91=E9=94=AE=E5=80=BC=E5=B9=B6=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=E5=88=B0=E5=89=AA=E8=B4=B4=E6=9D=BF=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD;=202.=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=B0=86=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E7=BC=96=E8=BE=91=E7=9A=84=E6=8F=90=E7=A4=BA=E5=8F=91?= =?UTF-8?q?=E9=80=81=E7=BB=99=E6=89=80=E6=9C=89=E7=8E=A9=E5=AE=B6=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/teammoeg/frostedheart/FHNetwork.java | 2 + .../gui/widget/ActionStateIconButton.java | 41 ++++++++++++++ .../base/client/gui/widget/ColorEditbox.java | 2 +- .../base/client/gui/widget/IconButton.java | 11 ++-- .../{TipAPI.java => ServerTipSender.java} | 16 ++++-- .../frostedheart/content/tips/Tip.java | 18 +++---- .../tips/client/gui/TipEditorScreen.java | 32 +++++++---- .../tips/client/gui/widget/TipEditsList.java | 53 +++++++++++++++---- .../DisplayCustomTipRequestPacket.java | 37 +++++++++++++ .../infrastructure/command/TipCommand.java | 4 +- .../util/client/FHColorHelper.java | 2 +- .../assets/frostedheart/lang/en_us/en_us.json | 11 +++- .../assets/frostedheart/lang/zh_cn/zh_cn.json | 12 ++++- 13 files changed, 199 insertions(+), 42 deletions(-) create mode 100644 src/main/java/com/teammoeg/frostedheart/base/client/gui/widget/ActionStateIconButton.java rename src/main/java/com/teammoeg/frostedheart/content/tips/{TipAPI.java => ServerTipSender.java} (68%) create mode 100644 src/main/java/com/teammoeg/frostedheart/content/tips/network/DisplayCustomTipRequestPacket.java diff --git a/src/main/java/com/teammoeg/frostedheart/FHNetwork.java b/src/main/java/com/teammoeg/frostedheart/FHNetwork.java index a3e102043..5d901b044 100644 --- a/src/main/java/com/teammoeg/frostedheart/FHNetwork.java +++ b/src/main/java/com/teammoeg/frostedheart/FHNetwork.java @@ -62,6 +62,7 @@ import com.teammoeg.frostedheart.content.steamenergy.HeatNetworkRequestC2SPacket; import com.teammoeg.frostedheart.content.steamenergy.HeatNetworkResponseS2CPacket; import com.teammoeg.frostedheart.content.tips.network.DisplayCustomTipPacket; +import com.teammoeg.frostedheart.content.tips.network.DisplayCustomTipRequestPacket; import com.teammoeg.frostedheart.content.tips.network.DisplayTipPacket; import com.teammoeg.frostedheart.content.town.TeamTownDataS2CPacket; import com.teammoeg.frostedheart.content.trade.network.BargainRequestPacket; @@ -197,6 +198,7 @@ public static void register() { // Tip Messages registerMessage("single_tip", DisplayTipPacket.class); registerMessage("custom_tip", DisplayCustomTipPacket.class); + registerMessage("display_request", DisplayCustomTipRequestPacket.class); // Waypoint Messages registerMessage("waypoint_remove", WaypointRemovePacket.class); diff --git a/src/main/java/com/teammoeg/frostedheart/base/client/gui/widget/ActionStateIconButton.java b/src/main/java/com/teammoeg/frostedheart/base/client/gui/widget/ActionStateIconButton.java new file mode 100644 index 000000000..bd55ae699 --- /dev/null +++ b/src/main/java/com/teammoeg/frostedheart/base/client/gui/widget/ActionStateIconButton.java @@ -0,0 +1,41 @@ +package com.teammoeg.frostedheart.base.client.gui.widget; + +import lombok.Getter; +import lombok.Setter; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.NotNull; + +/** + * 按下后显示另一条信息的 IconButton + */ +public class ActionStateIconButton extends IconButton { + private final Component originalMessage; + @Getter + @Setter + private Component clickedMessage; + + public ActionStateIconButton(int x, int y, Icon icon, int color, Component message, Component clickedMessage, OnPress pressedAction) { + this(x, y, icon, color, 1, message, clickedMessage, pressedAction); + } + + public ActionStateIconButton(int x, int y, Icon icon, int color, int scale, Component message, Component clickedMessage, OnPress pressedAction) { + super(x, y, icon, color, scale, message, pressedAction); + this.originalMessage = message; + this.clickedMessage = clickedMessage; + } + + @Override + public void renderWidget(@NotNull GuiGraphics graphics, int mouseX, int mouseY, float partialTicks) { + super.renderWidget(graphics, mouseX, mouseY, partialTicks); + if (!isHovered()) { + setMessage(originalMessage); + } + } + + @Override + public void onClick(double pMouseX, double pMouseY) { + super.onClick(pMouseX, pMouseY); + setMessage(clickedMessage); + } +} diff --git a/src/main/java/com/teammoeg/frostedheart/base/client/gui/widget/ColorEditbox.java b/src/main/java/com/teammoeg/frostedheart/base/client/gui/widget/ColorEditbox.java index 154735248..96ec32489 100644 --- a/src/main/java/com/teammoeg/frostedheart/base/client/gui/widget/ColorEditbox.java +++ b/src/main/java/com/teammoeg/frostedheart/base/client/gui/widget/ColorEditbox.java @@ -42,7 +42,7 @@ public void renderWidget(@NotNull GuiGraphics graphics, int mouseX, int mouseY, PoseStack pose = graphics.pose(); pose.pushPose(); pose.translate(1, 1, 0); - graphics.fill(getX()+getWidth()+2, getY(), getX()+getWidth()+getHeight()+2, getY()+getHeight(), FHColorHelper.makeDark(getColorValue(), 0.25F)); + graphics.fill(getX()+getWidth()+2, getY(), getX()+getWidth()+getHeight()+2, getY()+getHeight(), FHColorHelper.makeDark(getColorValue(), 0.75F)); pose.translate(-1, -1, 0); graphics.fill(getX()+getWidth()+2, getY(), getX()+getWidth()+getHeight()+2, getY()+getHeight(), getColorValue()); pose.popPose(); diff --git a/src/main/java/com/teammoeg/frostedheart/base/client/gui/widget/IconButton.java b/src/main/java/com/teammoeg/frostedheart/base/client/gui/widget/IconButton.java index 57a9dabd8..ca9512f16 100644 --- a/src/main/java/com/teammoeg/frostedheart/base/client/gui/widget/IconButton.java +++ b/src/main/java/com/teammoeg/frostedheart/base/client/gui/widget/IconButton.java @@ -40,8 +40,11 @@ public IconButton(int x, int y, Icon icon, int color, int scale, Component title @Override public void renderWidget(@NotNull GuiGraphics graphics, int mouseX, int mouseY, float partialTicks) { + int color = isActive() ? this.color : 0xFF666666; + int backgroundColor = FHColorHelper.makeDark(color, 0.3F); + float alpha = 0.5F; if (isHoveredOrFocused()) { - graphics.fill(getX(), getY(), getX()+getWidth(), getY()+getHeight(), FHColorHelper.setAlpha(color, 50)); + graphics.fill(getX(), getY(), getX()+getWidth(), getY()+getHeight(), FHColorHelper.setAlpha(backgroundColor, alpha)); if (!getMessage().getString().isBlank() && isHovered()) { int textWidth = ClientUtils.font().width(getMessage()); int renderX = getX()-textWidth+8; @@ -50,21 +53,21 @@ public void renderWidget(@NotNull GuiGraphics graphics, int mouseX, int mouseY, getY()-12, getX()+2 + textWidth, getY(), - FHColorHelper.setAlpha(color, 50)); + FHColorHelper.setAlpha(backgroundColor, alpha)); graphics.drawString(ClientUtils.font(), getMessage(), getX()+2, getY()-10, color); } else { graphics.fill(getX()-textWidth+getWidth()-1, getY()-12, getX()+getWidth(), getY(), - FHColorHelper.setAlpha(color, 50)); + FHColorHelper.setAlpha(backgroundColor, alpha)); graphics.drawString(ClientUtils.font(), getMessage(), getX()-textWidth+getWidth(), getY()-10, color); } } } FHGuiHelper.bindTexture(ICON_LOCATION); - FHGuiHelper.blitColored(graphics.pose(), getX(), getY(), getWidth(), getHeight(), icon.x*scale, icon.y*scale, getWidth(), getHeight(), TEXTURE_WIDTH*scale, TEXTURE_HEIGHT*scale, color, alpha); + FHGuiHelper.blitColored(graphics.pose(), getX(), getY(), getWidth(), getHeight(), icon.x*scale, icon.y*scale, getWidth(), getHeight(), TEXTURE_WIDTH*scale, TEXTURE_HEIGHT*scale, color, this.alpha); } public void setScale(int scale) { diff --git a/src/main/java/com/teammoeg/frostedheart/content/tips/TipAPI.java b/src/main/java/com/teammoeg/frostedheart/content/tips/ServerTipSender.java similarity index 68% rename from src/main/java/com/teammoeg/frostedheart/content/tips/TipAPI.java rename to src/main/java/com/teammoeg/frostedheart/content/tips/ServerTipSender.java index e40f0afb6..f320f950f 100644 --- a/src/main/java/com/teammoeg/frostedheart/content/tips/TipAPI.java +++ b/src/main/java/com/teammoeg/frostedheart/content/tips/ServerTipSender.java @@ -6,7 +6,7 @@ import net.minecraft.server.level.ServerPlayer; import net.minecraftforge.network.PacketDistributor; -public class TipAPI { +public class ServerTipSender { /** * 使客户端显示对应 id 的 tip @@ -18,13 +18,23 @@ public static void sendGeneral(String id, ServerPlayer player) { } /** - * 向客户端发送自定义 tip + * 向所有玩家发送自定义 tip + *
+ * 注意:自定义 tip 不会储存任何状态 + * @param tip tip + */ + public static void sendCustomToAll(Tip tip) { + FHNetwork.sendToServer(new DisplayCustomTipPacket(tip)); + } + + /** + * 向玩家发送自定义 tip *
* 注意:自定义 tip 不会储存任何状态
* @param tip tip
* @param player 目标玩家
*/
public static void sendCustom(Tip tip, ServerPlayer player) {
- FHNetwork.send(PacketDistributor.PLAYER.with(() -> player), new DisplayCustomTipPacket(tip));
+ FHNetwork.sendPlayer(player, new DisplayCustomTipPacket(tip));
}
}
diff --git a/src/main/java/com/teammoeg/frostedheart/content/tips/Tip.java b/src/main/java/com/teammoeg/frostedheart/content/tips/Tip.java
index b9d6b99cd..24c34b2fa 100644
--- a/src/main/java/com/teammoeg/frostedheart/content/tips/Tip.java
+++ b/src/main/java/com/teammoeg/frostedheart/content/tips/Tip.java
@@ -114,6 +114,14 @@ protected Tip(Tip.Builder builder) {
this.backgroundColor = builder.BGColor;
}
+ public void display() {
+ TipManager.INSTANCE.display().general(this);
+ }
+
+ public void forceDisplay() {
+ TipManager.INSTANCE.display().force(this);
+ }
+
public boolean hasNext() {
return TipManager.INSTANCE.hasTip(nextTip);
}
@@ -126,7 +134,7 @@ public boolean saveAsFile() {
return true;
} catch (IOException e) {
LOGGER.error("Unable to save file: '{}'", file, e);
- Tip.builder("exception").error(ErrorType.SAVE, e, Lang.str(file.getName())).forceDisplay();
+ Tip.builder("exception").error(ErrorType.SAVE, e, Lang.str(file.getName())).build().forceDisplay();
return false;
}
}
@@ -225,14 +233,6 @@ public Builder(String id) {
setTemporary();
}
- public void display() {
- TipManager.INSTANCE.display().general(build());
- }
-
- public void forceDisplay() {
- TipManager.INSTANCE.display().force(build());
- }
-
public Tip build() {
return new Tip(this);
}
diff --git a/src/main/java/com/teammoeg/frostedheart/content/tips/client/gui/TipEditorScreen.java b/src/main/java/com/teammoeg/frostedheart/content/tips/client/gui/TipEditorScreen.java
index 63b8c8d01..a33f046f1 100644
--- a/src/main/java/com/teammoeg/frostedheart/content/tips/client/gui/TipEditorScreen.java
+++ b/src/main/java/com/teammoeg/frostedheart/content/tips/client/gui/TipEditorScreen.java
@@ -1,8 +1,11 @@
package com.teammoeg.frostedheart.content.tips.client.gui;
+import com.teammoeg.frostedheart.FHNetwork;
+import com.teammoeg.frostedheart.base.client.gui.widget.ActionStateIconButton;
import com.teammoeg.frostedheart.content.tips.Tip;
import com.teammoeg.frostedheart.base.client.gui.widget.IconButton;
import com.teammoeg.frostedheart.content.tips.client.gui.widget.TipEditsList;
+import com.teammoeg.frostedheart.content.tips.network.DisplayCustomTipRequestPacket;
import com.teammoeg.frostedheart.util.client.ClientUtils;
import com.teammoeg.frostedheart.util.client.FHColorHelper;
import com.teammoeg.frostedheart.util.lang.Lang;
@@ -14,18 +17,11 @@
public class TipEditorScreen extends Screen {
private final TipEditsList list;
- private final IconButton saveButton;
public TipEditorScreen() {
super(Lang.str("Tip Editor"));
this.list = new TipEditsList(ClientUtils.mc(), ClientUtils.screenWidth(), ClientUtils.screenHeight(), 10, ClientUtils.screenHeight() - 30, 28);
list.setRenderBackground(false);
- this.saveButton = new IconButton(ClientUtils.screenWidth()/2 - 10, ClientUtils.screenHeight()-25, IconButton.Icon.FOLDER, FHColorHelper.CYAN, 2, Component.literal("Save"), (b) -> {
- var json = list.getJson();
- if (json != null) {
- Tip.builder("").fromJson(json).build().saveAsFile();
- }
- });
}
@Override
@@ -39,14 +35,32 @@ public void render(@NotNull GuiGraphics pGuiGraphics, int pMouseX, int pMouseY,
public void resize(@NotNull Minecraft pMinecraft, int pWidth, int pHeight) {
super.resize(pMinecraft, pWidth, pHeight);
list.updateSize(ClientUtils.screenWidth(), ClientUtils.screenHeight(), 10, ClientUtils.screenHeight() - 30);
- saveButton.setPosition(ClientUtils.screenWidth()/2 - 10, ClientUtils.screenHeight()-25);
}
@Override
protected void init() {
super.init();
addWidget(list);
- addRenderableWidget(saveButton);
+ // 保存按钮
+ addRenderableWidget(new ActionStateIconButton(ClientUtils.screenWidth()/2 - 30, ClientUtils.screenHeight()-25, IconButton.Icon.FOLDER, FHColorHelper.CYAN, 2, Component.translatable("gui.frostedheart.save_as_file"), Component.translatable("gui.frostedheart.saved"), (b) -> {
+ var json = list.getJson();
+ if (json != null) {
+ Tip.builder("").fromJson(json).build().saveAsFile();
+ }
+ }));
+ // 发送按钮
+ ActionStateIconButton sendButton = new ActionStateIconButton(ClientUtils.screenWidth()/2 + 5, ClientUtils.screenHeight()-25, IconButton.Icon.GIVE, FHColorHelper.CYAN, 2, Component.translatable("gui.frostedheart.tip_editor.send"), Component.translatable("gui.frostedheart.sent"), (b) -> {
+ var json = list.getJson();
+ if (json != null) {
+ Tip tip = Tip.builder("").fromJson(json).build();
+ FHNetwork.sendToServer(new DisplayCustomTipRequestPacket(tip));
+ }
+ });
+ if (!ClientUtils.getPlayer().hasPermissions(2)) {
+ sendButton.active = false;
+ sendButton.setMessage(Component.translatable("gui.frostedheart.tip_editor.no_permission"));
+ }
+ addRenderableWidget(sendButton);
}
@Override
diff --git a/src/main/java/com/teammoeg/frostedheart/content/tips/client/gui/widget/TipEditsList.java b/src/main/java/com/teammoeg/frostedheart/content/tips/client/gui/widget/TipEditsList.java
index de24762b2..cc9e1fdbc 100644
--- a/src/main/java/com/teammoeg/frostedheart/content/tips/client/gui/widget/TipEditsList.java
+++ b/src/main/java/com/teammoeg/frostedheart/content/tips/client/gui/widget/TipEditsList.java
@@ -5,13 +5,14 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
+import com.teammoeg.frostedheart.base.client.gui.widget.ActionStateIconButton;
import com.teammoeg.frostedheart.base.client.gui.widget.ColorEditbox;
import com.teammoeg.frostedheart.base.client.gui.widget.IconButton;
import com.teammoeg.frostedheart.base.client.gui.widget.IconCheckbox;
import com.teammoeg.frostedheart.content.tips.Tip;
import com.teammoeg.frostedheart.content.tips.TipRenderer;
+import com.teammoeg.frostedheart.util.client.ClientUtils;
import com.teammoeg.frostedheart.util.client.FHColorHelper;
-import com.teammoeg.frostedheart.util.lang.Lang;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
@@ -28,6 +29,7 @@
public class TipEditsList extends ContainerObjectSelectionList