Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
Fixing commands and adding basic previewer
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkKronicle committed Jul 15, 2022
1 parent ef00bfb commit c83ad91
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ yarn_mappings=1.19+build.4
loader_version=0.14.8
fabric_api_version=0.57.0+1.19

mod_version=1.5.1
mod_version=1.5.2
maven_group=io.github.darkkronicle
archives_base_name=AdvancedChatCore

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
package io.github.darkkronicle.advancedchatcore.chat;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.brigadier.tree.CommandNode;
import fi.dy.masa.malilib.gui.GuiBase;
import fi.dy.masa.malilib.gui.button.ButtonBase;
import fi.dy.masa.malilib.util.KeyCodes;
Expand All @@ -24,12 +26,20 @@
import lombok.Getter;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.ChatHud;
import net.minecraft.client.network.ChatPreviewer;
import net.minecraft.client.network.ServerInfo;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.option.ServerList;
import net.minecraft.client.toast.SystemToast;
import net.minecraft.client.util.InputUtil;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.command.CommandSource;
import net.minecraft.command.argument.DecoratableArgumentType;
import net.minecraft.text.MutableText;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;

Expand Down Expand Up @@ -57,6 +67,14 @@ public class AdvancedChatScreen extends GuiBase {
@Getter
private final RowList<ButtonBase> leftSideButtons = new RowList<>();

// TODO chat preview somewhere else
private static final Text CHAT_PREVIEW_WARNING_TOAST_TITLE = Text.translatable("chatPreview.warning.toast.title");
private static final Text CHAT_PREVIEW_WARNING_TOAST_TEXT = Text.translatable("chatPreview.warning.toast");
private static final Text CHAT_PREVIEW_PLACEHOLDER_TEXT = Text.translatable("chat.preview").formatted(Formatting.DARK_GRAY);

@Getter
private ChatPreviewer chatPreviewer;

@Override
protected void closeGui(boolean showParent) {
if (ConfigStorage.ChatScreen.PERSISTENT_TEXT.config.getBooleanValue()) {
Expand Down Expand Up @@ -101,6 +119,7 @@ public void resetCurrentMessage() {

public void initGui() {
super.initGui();
this.chatPreviewer = new ChatPreviewer(this.client);
this.rightSideButtons.clear();
this.leftSideButtons.clear();
this.client.keyboard.setRepeatEvents(true);
Expand Down Expand Up @@ -173,6 +192,13 @@ protected MutableText getNarrationMessage() {
if (startHistory >= 0) {
setChatFromHistory(-startHistory - 1);
}
ServerInfo serverInfo = this.client.getCurrentServerEntry();
if (serverInfo != null && this.client.options.getChatPreview().getValue()) {
ServerInfo.ChatPreview chatPreview = serverInfo.getChatPreview();
if (chatPreview != null && serverInfo.shouldPreviewChat() && chatPreview.showToast()) {
ServerList.updateServerListEntry(serverInfo);
}
}
}

public void resize(MinecraftClient client, int width, int height) {
Expand All @@ -194,6 +220,44 @@ public void removed() {

public void tick() {
this.chatField.tick();
this.chatPreviewer.tryRequestPending();
}

private void updatePreviewer(String string) {
String text = string.trim();
if (this.shouldPreviewChat()) {
this.tryRequestPreview(text);
} else {
this.chatPreviewer.disablePreview();
}
}

private void tryRequestPreview(String string) {
if (string.startsWith("/")) {
this.tryRequestCommandPreview(string);
} else {
this.tryRequestChatPreview(string);
}
}

private void tryRequestChatPreview(String string) {
this.chatPreviewer.tryRequest(string);
}

private void tryRequestCommandPreview(String chatText) {
// TODO fix this
this.chatPreviewer.disablePreview();
}

private boolean shouldPreviewChat() {
if (this.client.player == null) {
return false;
} else if (!this.client.options.getChatPreview().getValue()) {
return false;
} else {
ServerInfo serverInfo = this.client.getCurrentServerEntry();
return serverInfo != null && serverInfo.shouldPreviewChat();
}
}

private void onChatFieldUpdate(String chatText) {
Expand Down Expand Up @@ -231,7 +295,8 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (keyCode == KeyCodes.KEY_ENTER || keyCode == KeyCodes.KEY_KP_ENTER) {
String string = this.chatField.getText().trim();
// Strip message and send
MessageSender.getInstance().sendMessage(string);
Text text = this.chatPreviewer.tryConsumeResponse(this.chatField.getText());
MessageSender.getInstance().sendMessage(string, text);
this.chatField.setText("");
last = "";
// Exit
Expand Down Expand Up @@ -384,6 +449,59 @@ public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partia
if (style != null && style.getHoverEvent() != null) {
this.renderTextHoverEffect(matrixStack, style, mouseX, mouseY);
}
if (this.chatPreviewer.shouldRenderPreview()) {
this.renderChatPreview(matrixStack);
}
}

public void renderChatPreview(MatrixStack matrices) {
int i = (int)(255.0 * (this.client.options.getChtOpacity().getValue() * 0.9F + 0.1F));
int j = (int)(255.0 * this.client.options.getTextBackgroundOpacity().getValue());
int k = this.getPreviewWidth();
List<OrderedText> list = this.getPreviewText();
int l = this.getPreviewHeight(list);
RenderSystem.enableBlend();
matrices.push();
matrices.translate((double)this.getPreviewLeft(), (double)this.getPreviewTop(l), 0.0);
fill(matrices, 0, 0, k, l, j << 24);
matrices.translate(2.0, 2.0, 0.0);

for(int m = 0; m < list.size(); ++m) {
OrderedText orderedText = list.get(m);
this.client.textRenderer.drawWithShadow(matrices, orderedText, 0.0F, (float)(m * 9), i << 24 | 16777215);
}

matrices.pop();
RenderSystem.disableBlend();
}

private List<OrderedText> getPreviewText() {
Text text = this.chatPreviewer.getPreviewText();
return text != null ? this.textRenderer.wrapLines(text, this.getPreviewWidth()) : List.of(CHAT_PREVIEW_PLACEHOLDER_TEXT.asOrderedText());
}

private int getPreviewWidth() {
return this.client.currentScreen.width - 4;
}

private int getPreviewHeight(List<OrderedText> lines) {
return Math.max(lines.size(), 1) * 9 + 4;
}

private int getPreviewBottom() {
return this.client.currentScreen.height - 15;
}

private int getPreviewTop(int previewHeight) {
return this.getPreviewBottom() - previewHeight;
}

private int getPreviewLeft() {
return 2;
}

private int getPreviewRight() {
return this.client.currentScreen.width - 2;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import java.util.Optional;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Text;
import org.apache.logging.log4j.Level;

public class MessageSender {
Expand All @@ -37,6 +38,10 @@ public void addFilter(IStringFilter filter, int index) {
}

public void sendMessage(String string) {
sendMessage(string, null);
}

public void sendMessage(String string, Text text) {
String unfiltered = string;
for (IStringFilter filter : filters) {
Optional<String> filtered = filter.filter(string);
Expand All @@ -55,7 +60,11 @@ public void sendMessage(String string) {
}

if (client.player != null) {
this.client.player.sendChatMessage(string);
if (string.startsWith("/")) {
this.client.player.sendCommand(string.substring(1), text);
} else {
this.client.player.sendChatMessage(string, text);
}
}
}
}

0 comments on commit c83ad91

Please sign in to comment.