Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Add Notifications module with management, display, and rendering features #657

Merged
merged 7 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/keystrokesmod/module/ModuleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public void register() {
// this.addModule(new NyaProxy());
this.addModule(new Settings());
this.addModule(new MiddleClick());
this.addModule(new Notifications());

// combat
this.addModule(new AimAssist());
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/keystrokesmod/module/impl/client/Notifications.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package keystrokesmod.module.impl.client;

import keystrokesmod.module.Module;
import keystrokesmod.module.setting.impl.ButtonSetting;
import keystrokesmod.utility.CoolDown;
import keystrokesmod.utility.Utils;
import keystrokesmod.utility.font.Font;
import keystrokesmod.utility.font.FontManager;
import keystrokesmod.utility.font.impl.MinecraftFontRenderer;
import keystrokesmod.utility.render.AnimationUtils;
import keystrokesmod.utility.render.ColorUtils;
import keystrokesmod.utility.render.RRectUtils;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class Notifications extends Module {
public static final List<NotificationTypes> notifs = new ArrayList<>();
private static final List<String> messages = new ArrayList<>();
private static final List<CoolDown> durations = new ArrayList<>();
private static final List<AnimationUtils> animationsX = new ArrayList<>();
private static final List<AnimationUtils> animationsY = new ArrayList<>();
public static ButtonSetting chatNoti;
private final Font fontRegular = FontManager.getRegular(16);
private final Font fontIcon = FontManager.getIcons(20);

public Notifications() {
super("Notifications", category.client);
this.registerSetting(chatNoti = new ButtonSetting("Show in chat", false));
}

public static void sendNotification(NotificationTypes notificationType, String message, long duration) {
if (!chatNoti.isToggled()) {
ScaledResolution sr = new ScaledResolution(mc);
notifs.add(notificationType);
messages.add(message);
durations.add(new CoolDown(duration));
durations.get(notifs.size() - 1).start();
animationsX.add(new AnimationUtils(sr.getScaledWidth()));
animationsY.add(new AnimationUtils(sr.getScaledHeight() - (notifs.size() * 30)));
animationsX.get(notifs.size() - 1).setAnimation(sr.getScaledWidth(), 16);
} else {
Utils.sendMessage("&7[&1LI&7-" + ((notificationType == NotificationTypes.INFO) ? "&1" : notificationType == NotificationTypes.WARN ? "&e" : "&4") + notificationType.toString() + "&7]&r " + message);
}
}

@SubscribeEvent
public void onTick(RenderGameOverlayEvent event) {
ScaledResolution sr = new ScaledResolution(mc);
for (int index = 0; index < notifs.size(); index++) {
animationsY.get(index).setAnimation(sr.getScaledHeight() - ((index + 1) * 30), 16);
RRectUtils.drawRound(animationsX.get(index).getValue(), animationsY.get(index).getValue(), 120, 25, 3, new Color(0, 0, 0, 92));
fontIcon.drawString(notifs.get(index) == NotificationTypes.INFO ? "G" : "R", animationsX.get(index).getValue() + 12.5, animationsY.get(index).getValue() + 15.5, MinecraftFontRenderer.CenterMode.XY, false, ColorUtils.getFontColor(2).getRGB());
fontRegular.wrapText(messages.get(index), animationsX.get(index).getValue() + 25, animationsY.get(index).getValue() + 12.5, MinecraftFontRenderer.CenterMode.Y, false, ColorUtils.getFontColor(2).getRGB(), 95);
if (durations.get(index).hasFinished()) {
notifs.remove(index);
messages.remove(index);
durations.remove(index);
animationsX.remove(index);
animationsY.remove(index);
index--;
} else if (durations.get(index).getTimeLeft() < 500) {
animationsX.get(index).setAnimation(sr.getScaledWidth(), 16);
} else {
animationsX.get(index).setAnimation(sr.getScaledWidth() - 125, 16);
}
}
}

public enum NotificationTypes {
INFO,
WARN,
ERROR
}
}
4 changes: 0 additions & 4 deletions src/main/java/keystrokesmod/module/impl/render/BedPlates.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
import net.minecraft.util.BlockPos;
import net.minecraft.block.Block;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.RenderHandEvent;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
Expand All @@ -30,7 +28,6 @@ public class BedPlates extends Module {
public static final ShaderUtils roundedShader = new ShaderUtils("keystrokesmod:shaders/rrect.frag");

public static SliderSetting updateRate, yShift, layers;
// public static ButtonSetting bedwarsOnly;
private final CoolDown updateCooldown = new CoolDown(0);
private final List<BlockPos> beds = new ArrayList<>();
private final List<List<Block>> bedBlocks = new ArrayList<>();
Expand All @@ -40,7 +37,6 @@ public BedPlates() {
this.registerSetting(yShift = new SliderSetting("Y-shift", 2, -5, 10, 1));
this.registerSetting(updateRate = new SliderSetting("Update rate (ms)", 1000, 250, 5000, 250));
this.registerSetting(layers = new SliderSetting("Layers", 3, 3, 10, 1));
//this.registerSetting(bedwarsOnly = new ButtonSetting("Bedwars only", true));
}

public void onEnable() {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/keystrokesmod/utility/font/Font.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package keystrokesmod.utility.font;

import keystrokesmod.utility.font.impl.MinecraftFontRenderer;

public interface Font {
int drawString(String text, double x, double y, int color, boolean dropShadow);

void drawString(String text, double x, double y, MinecraftFontRenderer.CenterMode centerMode, boolean shadow, int color);
int drawString(final String text, final double x, final double y, final int color);

int drawStringWithShadow(final String text, final double x, final double y, final int color);
Expand All @@ -12,7 +14,7 @@ public interface Font {
int drawCenteredString(final String text, final double x, final double y, final int color);

int drawRightString(final String text, final double x, final double y, final int color);

void wrapText(String text, double x, double y, MinecraftFontRenderer.CenterMode centerMode, boolean shadow, int color, double width);
float height();

default int getStringWidth(String text) {
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/keystrokesmod/utility/font/FontManager.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package keystrokesmod.utility.font;

import keystrokesmod.utility.Utils;
import keystrokesmod.utility.font.impl.FontRenderer;
import keystrokesmod.utility.font.impl.FontUtil;
import keystrokesmod.utility.font.impl.MinecraftFontRenderer;
import net.minecraft.client.Minecraft;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
Expand All @@ -15,7 +15,7 @@ public class FontManager {
private static final HashMap<Integer, FontRenderer> INTERNATIONAL = new HashMap<>();
private static final HashMap<Integer, FontRenderer> MONTSERRAT_MAP = new HashMap<>();
private static final HashMap<Integer, FontRenderer> ROBOTO_MAP = new HashMap<>();

private static final HashMap<Integer, FontRenderer> Regular = new HashMap<>();
private static final HashMap<Integer, FontRenderer> LIGHT_MAP = new HashMap<>();

private static final HashMap<Integer, FontRenderer> NUNITO = new HashMap<>();
Expand All @@ -36,7 +36,7 @@ public class FontManager {
private static final HashMap<Integer, FontRenderer> TAHOMA_BOLD = new HashMap<>();
private static final HashMap<Integer, FontRenderer> TAHOMA = new HashMap<>();

private static final HashMap<Integer, FontRenderer> ICONS_1 = new HashMap<>();
private static final HashMap<Integer, FontRenderer> ICONS = new HashMap<>();
private static final HashMap<Integer, FontRenderer> ICONS_2 = new HashMap<>();
private static final HashMap<Integer, FontRenderer> ICONS_3 = new HashMap<>();

Expand All @@ -62,7 +62,9 @@ public class FontManager {
public static Font getMontserratMedium(final int size) {
return get(MONTSERRAT_MAP, size, "Montserrat-Medium", true, true);
}

public static Font getRegular(final int size) {
return get(Regular, size, "regular", true, true);
}
TejasLamba2006 marked this conversation as resolved.
Show resolved Hide resolved
public static Font getMontserratHairline(final int size) {
return get(MONTSERRAT_HAIRLINE, size, "Montserrat-Hairline", true, true);
}
Expand Down Expand Up @@ -147,8 +149,8 @@ public static Font getDreamscapeNoAA(final int size) {
return get(DREAMSCAPE_NO_AA, size, "Dreamscape", true, false);
}

public static Font getIconsOne(final int size) {
return get(ICONS_1, size, "Icon-1", true, true);
public static Font getIcons(final int size) {
return get(ICONS, size, "icon", true, true);
TejasLamba2006 marked this conversation as resolved.
Show resolved Hide resolved
}

public static Font getIconsThree(final int size) {
Expand Down Expand Up @@ -197,6 +199,8 @@ private static Font get(@NotNull HashMap<Integer, FontRenderer> map, int size, S

if (font != null) {
map.put(size, new FontRenderer(font, fractionalMetrics, AA, international));
} else {
Utils.sendMessage("Failed to load font: " + name);
TejasLamba2006 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
58 changes: 58 additions & 0 deletions src/main/java/keystrokesmod/utility/font/impl/FontRenderer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keystrokesmod.utility.font.impl;

import keystrokesmod.utility.font.FontManager;
import keystrokesmod.utility.render.ColorUtils;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.MathHelper;
import org.lwjgl.BufferUtils;
Expand All @@ -12,6 +13,8 @@
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/**
* @author Patrick, Hazsi
Expand Down Expand Up @@ -176,6 +179,33 @@ public int drawRightString(String text, double x, double y, int color) {
return drawString(text, x - (width(text)), y, color, false);
}

@Override
public void wrapText(String text, double x, double y, MinecraftFontRenderer.CenterMode centerMode, boolean shadow, int color, double width) {
List<String> lines = new ArrayList<>();
String[] words = text.trim().split(" ");
StringBuilder line = new StringBuilder();

for (String word : words) {
double totalWidth = getStringWidth(line + " " + word);

if (x + totalWidth >= x + width) {
lines.add(line.toString());
line = new StringBuilder(word).append(" ");
continue;
}

line.append(word).append(" ");
}
lines.add(line.toString());

double newY = y - (centerMode == MinecraftFontRenderer.CenterMode.XY || centerMode == MinecraftFontRenderer.CenterMode.Y ? ((lines.size() - 1) * (height() + 5)) / 2 : 0);
for (String s : lines) {
ColorUtils.resetColor();
drawString(s, x, newY, centerMode, shadow, color);
newY += height() + 5;
}
}
TejasLamba2006 marked this conversation as resolved.
Show resolved Hide resolved

public int drawStringWithShadow(final String text, final double x, final double y, final int color) {
drawString(text, x + 0.25, y + 0.25, color, true);
return drawString(text, x, y, color, false);
Expand Down Expand Up @@ -242,6 +272,34 @@ public int drawString(String text, double x, double y, final int color, final bo
return (int) (x - givenX);
}

@Override
public void drawString(String text, double x, double y, MinecraftFontRenderer.CenterMode centerMode, boolean shadow, int color) {
switch (centerMode) {
case X:
if (shadow) {
this.drawString(text, x - (double) this.getStringWidth(text) / 2 + 0.5, y + 0.5, color, true);
}
this.drawString(text, x - (double) this.getStringWidth(text) / 2, y, color, false);
return;
case Y:
if (shadow) {
this.drawString(text, x + 0.5, y - this.height() / 2 + 0.5, color, true);
}
this.drawString(text, x, y - this.height() / 2, color, false);
return;
case XY:
if (shadow) {
this.drawString(text, x - (double) this.getStringWidth(text) / 2 + 0.5, y - this.height() / 2 + 0.5, color, true);
}
this.drawString(text, x - (double) this.getStringWidth(text) / 2, y - this.height() / 2, color, false);
return;
case NONE:
if (shadow) {
this.drawString(text, x + 0.5, y + 0.5, color, true);
}
this.drawString(text, x, y, color, false);
}
TejasLamba2006 marked this conversation as resolved.
Show resolved Hide resolved
}
public int width(String text) {
if (!this.international && this.requiresInternationalFont(text)) {
return FontManager.getInternational(this.font.getSize()).width(text);
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/keystrokesmod/utility/font/impl/FontUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,31 @@ public class FontUtil {
private static final IResourceManager RESOURCE_MANAGER = Raven.mc.getResourceManager();

/**
* Method, which gets a font by a resource name
* Enhanced method, which gets a font by a resource name with better error handling.
*
* @param resource resource name
* @param size font size
* @return font by resource
* @return font by resource or null if an error occurs
*/
public static @Nullable Font getResource(final String resource, final int size) {
try {
return Font.createFont(Font.TRUETYPE_FONT, RESOURCE_MANAGER.getResource(new ResourceLocation(resource)).getInputStream()).deriveFont((float) size);
} catch (final FontFormatException | IOException ignored) {
return null;
Font font = Font.createFont(Font.TRUETYPE_FONT, RESOURCE_MANAGER.getResource(new ResourceLocation(resource)).getInputStream()).deriveFont((float) size);
if (font != null) {
System.out.println("Font loaded successfully: " + resource);
return font;
} else {
System.out.println("Font loaded but is null: " + resource);
}
} catch (FontFormatException e) {
TejasLamba2006 marked this conversation as resolved.
Show resolved Hide resolved
System.out.println("Font format is not supported: " + resource);
e.printStackTrace();
} catch (IOException e) {
System.out.println("Error accessing the font file: " + resource);
e.printStackTrace();
} catch (Exception e) {
System.out.println("Unexpected error loading font: " + resource);
e.printStackTrace();
}
return null;
}
}
}
Loading
Loading