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

Commit

Permalink
Release the demons
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkKronicle committed Nov 17, 2021
1 parent 584edda commit eb7809a
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import lombok.Getter;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.ChatHud;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
Expand All @@ -34,7 +33,7 @@ public class AdvancedChatScreen extends GuiBase {
private int messageHistorySize = -1;

/** Chat field at the bottom of the screen */
@Getter protected TextFieldWidget chatField;
@Getter protected AdvancedTextField chatField;

/** What the chat box started out with */
@Getter private String originalChatText = "";
Expand Down Expand Up @@ -71,7 +70,7 @@ public void initGui() {
this.client.keyboard.setRepeatEvents(true);
this.messageHistorySize = this.client.inGameHud.getChatHud().getMessageHistory().size();
this.chatField =
new TextFieldWidget(
new AdvancedTextField(
this.textRenderer,
4,
this.height - 12,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright (C) 2021 DarkKronicle
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package io.github.darkkronicle.advancedchatcore.chat;

import fi.dy.masa.malilib.util.KeyCodes;
import io.github.darkkronicle.advancedchatcore.util.TextUtil;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;

public class AdvancedTextField extends TextFieldWidget {

/**
* Stores the last saved snapshot of the box. This ensures that not every character update is
* put in, but instead groups.
*/
private String lastSaved = "";

/** Snapshots of chat box */
private final List<String> history = new ArrayList<>();

private int historyIndex = -1;

public AdvancedTextField(
TextRenderer textRenderer, int x, int y, int width, int height, Text text) {
this(textRenderer, x, y, width, height, null, text);
}

public AdvancedTextField(
TextRenderer textRenderer,
int x,
int y,
int width,
int height,
@Nullable TextFieldWidget copyFrom,
Text text) {
super(textRenderer, x, y, width, height, copyFrom, text);
history.add("");
}

public static boolean isUndo(int code) {
// Undo (Ctrl + Z)
return code == KeyCodes.KEY_Z && Screen.hasControlDown() && !Screen.hasAltDown();
}

/** Triggers undo for the text box */
public void undo() {
// Save the current snapshot if it's been edited
if (!this.lastSaved.equals(this.getText()) && historyIndex < 0) {
addToHistory(getText());
}
// History index < 0 means not in the middle of undoing
if (historyIndex < 0) {
historyIndex = history.size() - 1;
}
// Check that we're not at index 0
if (historyIndex != 0) {
historyIndex--;
}
// Set the text but don't update
setText(history.get(historyIndex), false);
}

public void redo() {
if (historyIndex < 0 || historyIndex >= history.size() - 1) {
// No stuff to redo...
return;
}
historyIndex++;
setText(history.get(historyIndex), false);
}

@Override
public void write(String text) {
super.write(text);
updateHistory();
}

@Override
public void eraseCharacters(int characterOffset) {
super.eraseCharacters(characterOffset);
updateHistory();
}

/**
* Sets the text for the text field
*
* @param text Text to set
* @param update Updates the history
*/
public void setText(String text, boolean update) {
// Wrapper class for setText
super.setText(text);
if (update) {
updateHistory();
}
}

@Override
public void setText(String text) {
setText(text, true);
}

private void updateHistory() {
if (historyIndex >= 0) {
// Remove all history after what has gone back
pruneHistory(historyIndex + 1);
historyIndex = -1;
}
// Check to see if it should log
int dif = getText().length() - lastSaved.length();
double sim = TextUtil.similarity(getText(), lastSaved);
if (sim >= .3 && (dif < 5 && dif * -1 < 5) || (sim >= .9)) {
return;
}
addToHistory(getText());
}

private void addToHistory(String text) {
this.lastSaved = text;
this.history.add(text);
while (this.history.size() > 50) {
this.history.remove(0);
}
}

/**
* Remove's all history past a certain index
*
* @param index Index to prune from. Non-inclusive.
*/
private void pruneHistory(int index) {
if (index == 0) {
history.clear();
return;
}
while (history.size() > index) {
history.remove(history.size() - 1);
}
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (!this.isActive()) {
return false;
}
if (!isUndo(keyCode)) {
return super.keyPressed(keyCode, scanCode, modifiers);
}
if (Screen.hasShiftDown()) {
redo();
} else {
undo();
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import fi.dy.masa.malilib.render.RenderUtils;
import fi.dy.masa.malilib.util.StringUtils;
import io.github.darkkronicle.advancedchatcore.interfaces.ConfigRegistryOption;
import io.github.darkkronicle.advancedchatcore.util.ColorUtil;
import io.github.darkkronicle.advancedchatcore.util.Colors;
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand Down Expand Up @@ -85,16 +85,29 @@ public void render(int mouseX, int mouseY, boolean selected, MatrixStack matrixS
this.y,
this.width,
this.height,
ColorUtil.WHITE.withAlpha(150).color());
Colors.getInstance().getColorOrWhite("white").withAlpha(150).color());
} else if (this.isOdd) {
RenderUtils.drawRect(
this.x, this.y, this.width, this.height, ColorUtil.WHITE.withAlpha(70).color());
this.x,
this.y,
this.width,
this.height,
Colors.getInstance().getColorOrWhite("white").withAlpha(70).color());
} else {
RenderUtils.drawRect(
this.x, this.y, this.width, this.height, ColorUtil.WHITE.withAlpha(50).color());
this.x,
this.y,
this.width,
this.height,
Colors.getInstance().getColorOrWhite("white").withAlpha(50).color());
}
String name = this.option.getDisplayName();
this.drawString(this.x + 4, this.y + 7, ColorUtil.WHITE.color(), name, matrixStack);
this.drawString(
this.x + 4,
this.y + 7,
Colors.getInstance().getColorOrWhite("white").color(),
name,
matrixStack);

RenderUtils.color(1f, 1f, 1f, 1f);
RenderSystem.disableBlend();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import fi.dy.masa.malilib.gui.widgets.WidgetListEntryBase;
import fi.dy.masa.malilib.gui.wrappers.TextFieldWrapper;
import fi.dy.masa.malilib.render.RenderUtils;
import io.github.darkkronicle.advancedchatcore.util.ColorUtil;
import io.github.darkkronicle.advancedchatcore.util.Colors;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -69,13 +69,21 @@ public void render(int mouseX, int mouseY, boolean selected, MatrixStack matrixS
this.y,
this.width,
this.height,
ColorUtil.WHITE.withAlpha(150).color());
Colors.getInstance().getColorOrWhite("white").withAlpha(150).color());
} else if (this.odd) {
RenderUtils.drawRect(
this.x, this.y, this.width, this.height, ColorUtil.WHITE.withAlpha(70).color());
this.x,
this.y,
this.width,
this.height,
Colors.getInstance().getColorOrWhite("white").withAlpha(70).color());
} else {
RenderUtils.drawRect(
this.x, this.y, this.width, this.height, ColorUtil.WHITE.withAlpha(50).color());
this.x,
this.y,
this.width,
this.height,
Colors.getInstance().getColorOrWhite("white").withAlpha(50).color());
}

renderEntry(mouseX, mouseY, selected, matrixStack);
Expand All @@ -96,7 +104,12 @@ public void render(int mouseX, int mouseY, boolean selected, MatrixStack matrixS
*/
public void renderEntry(int mouseX, int mouseY, boolean selected, MatrixStack matrixStack) {
String name = getName();
this.drawString(this.x + 4, this.y + 7, ColorUtil.WHITE.color(), name, matrixStack);
this.drawString(
this.x + 4,
this.y + 7,
Colors.getInstance().getColorOrWhite("white").color(),
name,
matrixStack);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import lombok.Getter;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand Down Expand Up @@ -52,6 +53,33 @@ public int size() {
return matches.size();
}

/**
* Get's a group from the result. This only works if it's a {@link RegexFinder}, otherwise it
* returns the entire string.
*
* @param num Group number
* @return StringMatch from group. It references the original string.
*/
public StringMatch getGroup(StringMatch match, int num) {
if (!matches.contains(match)) {
return null;
}
if (!(finder instanceof RegexFinder)) {
return match;
}
try {
// TODO abstract in some way to make it not cast a ton
RegexFinder regex = (RegexFinder) finder;
Matcher matcher = regex.getPattern(input).matcher(match.match);
String group = matcher.group(num);
int start = matcher.start(num);
int end = matcher.start(num);
return new StringMatch(group, start, end);
} catch (Exception e) {
return null;
}
}

/**
* Replaces the groups with a specified match
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
package io.github.darkkronicle.advancedchatcore.util;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;

/**
* A class to store data about a match.
*
* <p>This class is comparable based on where it starts.
*/
@EqualsAndHashCode
@AllArgsConstructor
public class StringMatch implements Comparable<StringMatch> {

Expand Down
Loading

0 comments on commit eb7809a

Please sign in to comment.