Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add customizable global fonts #45

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0fa93b7
Parse font field from InfoGet json responses
RappyTV Jul 7, 2024
bd09c9d
Add own GlobalFont enum, add PlayerInfo#getFont
RappyTV Jul 7, 2024
beaa321
Rename default font
RappyTV Jul 7, 2024
d0c2f24
Add ApiHandler#setFont
RappyTV Jul 7, 2024
2adbdf9
Add font option to update global font
RappyTV Jul 7, 2024
bc5d219
Add new missing translations
RappyTV Jul 7, 2024
5082837
Bump version, add version to revisionRegistry
RappyTV Jul 7, 2024
d469712
Update german translations, set indent size to 4
RappyTV Jul 7, 2024
4568b04
Merge branch 'master' into feat/customFonts
RappyTV Jul 7, 2024
969e103
Improve getting font styles, add default and unicode style
RappyTV Jul 8, 2024
47a7bba
Improve getting font styles, add default and unicode style
RappyTV Jul 8, 2024
88a46cd
Improve getting font styles, add default and unicode style
RappyTV Jul 8, 2024
405a5fc
Improve getting font styles, add default and unicode style
RappyTV Jul 8, 2024
725190d
Add dark souls font
RappyTV Jul 8, 2024
288c999
Remove test command
RappyTV Jul 8, 2024
782a4f7
Add Alagard, Glacial Indifference and Super RPG font
RappyTV Jul 8, 2024
e7beb43
Fix updateSettings button not showing up
RappyTV Jul 10, 2024
a27b6cd
Fix fontResponse not being respected in result popup
RappyTV Jul 10, 2024
72d512a
Implement custom extra left-margin for the staff icon per font
RappyTV Jul 10, 2024
50363d5
Adjust alagard to fit
RappyTV Jul 10, 2024
a05fa7e
Reorder custom fonts by alphabet
RappyTV Jul 10, 2024
eb2ee23
Fix not being able to use negative values in GlobalFont constructor
RappyTV Jul 10, 2024
02b2de9
Set the staff margins for all GlobalFonts
RappyTV Jul 10, 2024
bc3bbb2
Adjust the dark souls font shifting
RappyTV Jul 10, 2024
9d51a92
Adjust the super rpg font shifting
RappyTV Jul 10, 2024
33c4d0f
Add IntroducedIn annotation to join discord button
RappyTV Jul 10, 2024
9779238
Support vanilla theme
RappyTV Jul 10, 2024
e6448e8
Add sprite texture to font config option
RappyTV Jul 10, 2024
670a4da
Update dates on the new revisions
RappyTV Jul 10, 2024
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
27 changes: 27 additions & 0 deletions api/src/main/java/com/rappytv/globaltags/api/ApiHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rappytv.globaltags.api;

import com.rappytv.globaltags.types.GlobalFont;
import com.rappytv.globaltags.types.GlobalIcon;
import com.rappytv.globaltags.types.PlayerInfo;
import com.rappytv.globaltags.types.PlayerInfo.Suspension;
Expand Down Expand Up @@ -59,6 +60,7 @@ public Map<String, Object> getBody() {
consumer.accept(new PlayerInfo(
uuid,
request.responseBody.tag,
request.responseBody.font,
request.responseBody.position,
request.responseBody.icon,
request.responseBody.admin,
Expand Down Expand Up @@ -92,6 +94,31 @@ public Map<String, Object> getBody() {
}));
}

public static void setFont(GlobalFont font, Consumer<ApiResponse> consumer) {
setFont(Laby.labyAPI().getUniqueId(), font, consumer);
}

public static void setFont(UUID uuid, GlobalFont font, Consumer<ApiResponse> consumer) {
ApiRequest request = new ApiRequest(
Method.POST,
"/players/" + uuid + "/font",
Util.getSessionToken()
) {
@Override
public Map<String, Object> getBody() {
return Map.of("font", font.name());
}
};
request.sendAsyncRequest((response) -> {
if(!request.isSuccessful()) {
consumer.accept(new ApiResponse(false, request.getError()));
return;
}
TagCache.clear();
TagCache.resolveSelf((info) -> consumer.accept(new ApiResponse(true, request.getMessage())));
});
}

public static void setPosition(PositionType position, Consumer<ApiResponse> consumer) {
setPosition(Laby.labyAPI().getUniqueId(), position, consumer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class ResponseBody {

// For success
public String tag;
public String font;
public String position;
public String icon;
public boolean admin;
Expand Down
56 changes: 56 additions & 0 deletions api/src/main/java/com/rappytv/globaltags/types/GlobalFont.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.rappytv.globaltags.types;

import net.labymod.api.Laby;
import net.labymod.api.client.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;

public enum GlobalFont {
DEFAULT(0, 0),
UNICODE(-1, -10),
ALAGARD(1, 3),
DARK_SOULS(1, 22),
GLACIAL_INDIFFERENCE(3, 18),
SUPER_RPG(2, 10);

private static final Map<GlobalFont, ResourceLocation> locations = new HashMap<>();

private final double vanillaMargin;
private final double fancyMargin;

GlobalFont(double vanillaMargin, double fancyMargin) {
this.vanillaMargin = vanillaMargin;
this.fancyMargin = fancyMargin;
}

public double getStaffMargin() {
return switch (Laby.labyAPI().themeService().currentTheme().getId()) {
case "vanilla" -> vanillaMargin;
case "fancy" -> fancyMargin;
default -> 0;
};
}

@Nullable
public ResourceLocation getCachedLocation() {
if(locations.containsKey(this)) return locations.get(this);
locations.put(this, getResourceLocation());
return getCachedLocation();
}

@Nullable
private ResourceLocation getResourceLocation() {
return switch (this) {
case DEFAULT -> null;
case UNICODE -> ResourceLocation.create(
"minecraft",
"uniform"
);
default -> ResourceLocation.create(
"globaltags",
this.name().toLowerCase()
);
};
}
}
18 changes: 16 additions & 2 deletions api/src/main/java/com/rappytv/globaltags/types/PlayerInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ public class PlayerInfo {
private final UUID uuid;
private final Component tag;
private final String plainTag;
private final String font;
private final String position;
private final String icon;
private final boolean admin;
private final Suspension suspension;

public PlayerInfo(UUID uuid, String tag, String position, String icon, boolean admin, Ban ban) {
public PlayerInfo(UUID uuid, String tag, String font, String position, String icon, boolean admin, Ban ban) {
this.uuid = uuid;
this.tag = Util.translateColorCodes(tag);
this.font = font;
this.tag = Util.formatTag(tag, getFont());
this.plainTag = tag != null ? tag : "";
this.position = position;
this.icon = icon;
Expand Down Expand Up @@ -54,6 +56,18 @@ public String getPlainTag() {
return plainTag;
}

/**
* Returns the {@link GlobalFont} enum value which the player has selected
*/
@NotNull
public GlobalFont getFont() {
try {
return GlobalFont.valueOf(font);
} catch (Exception ignored) {
return GlobalFont.DEFAULT;
}
}

/**
* Returns the player's GlobalTag position
*/
Expand Down
23 changes: 18 additions & 5 deletions api/src/main/java/com/rappytv/globaltags/util/Util.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rappytv.globaltags.util;

import com.rappytv.globaltags.types.GlobalFont;
import net.labymod.api.Laby;
import net.labymod.api.client.component.Component;
import net.labymod.api.client.component.format.NamedTextColor;
Expand All @@ -20,6 +21,7 @@ public class Util {
NamedTextColor.DARK_GRAY
);
private static Component tagResponse = null;
private static Component fontResponse = null;
private static Component positionResponse = null;
private static Component iconResponse = null;

Expand All @@ -41,16 +43,22 @@ public static void notify(Component title, Component description) {
public static void update(ResultType type, Component component) {
switch (type) {
case TAG -> tagResponse = component;
case FONT -> fontResponse = component;
case POSITION -> positionResponse = component;
case ICON -> iconResponse = component;
}
if(tagResponse == null || positionResponse == null || iconResponse == null) return;
if(tagResponse == null
|| fontResponse == null
|| positionResponse == null
|| iconResponse == null
) return;
SimpleAdvancedPopup popup = SimpleAdvancedPopup
.builder()
.title(Component.text("Update result", NamedTextColor.AQUA))
.description(Component.translatable(
"globaltags.settings.tags.updateSettings.result",
tagResponse,
fontResponse,
positionResponse,
iconResponse
))
Expand All @@ -61,23 +69,28 @@ public static void update(ResultType type, Component component) {
TagCache.clear();
TagCache.resolveSelf();
tagResponse = null;
fontResponse = null;
positionResponse = null;
iconResponse = null;
});
}

public enum ResultType {
TAG,
FONT,
POSITION,
ICON
}

@NotNull
public static Component translateColorCodes(String string) {
if(string == null) string = "";
return LegacyComponentSerializer
public static Component formatTag(String tag, GlobalFont font) {
if(tag == null) tag = "";
Component component = LegacyComponentSerializer
.legacyAmpersand()
.deserialize(string);
.deserialize(tag);
if(font.getCachedLocation() != null)
component.style(component.style().font(font.getCachedLocation()));
return component;
}

public static @Nullable String getSessionToken() {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ labyMod {
author = "RappyTV"
description = "Get yourself a custom Globaltag that's publicly visible to anyone using this addon."
minecraftVersion = "*"
version = System.getenv().getOrDefault("VERSION", "1.2.0")
version = System.getenv().getOrDefault("VERSION", "1.2.1")
}

minecraft {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ protected void preConfigurationLoad() {
Laby.references().revisionRegistry().register(new SimpleRevision("globaltags", new SemanticVersion("1.1.0"), "2023-11-24"));
Laby.references().revisionRegistry().register(new SimpleRevision("globaltags", new SemanticVersion("1.1.7"), "2024-02-27"));
Laby.references().revisionRegistry().register(new SimpleRevision("globaltags", new SemanticVersion("1.1.9"), "2024-06-01"));
Laby.references().revisionRegistry().register(new SimpleRevision("globaltags", new SemanticVersion("1.2.0"), "2024-07-06"));
Laby.references().revisionRegistry().register(new SimpleRevision("globaltags", new SemanticVersion("1.2.0"), "2024-07-10"));
Laby.references().revisionRegistry().register(new SimpleRevision("globaltags", new SemanticVersion("1.2.1"), "2024-07-10"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public GlobalTagConfig() {
@SpriteSlot(size = 32, y = 2, x = 2)
@SwitchSetting
private final ConfigProperty<Boolean> localizedResponses = new ConfigProperty<>(true);
@IntroducedIn(namespace = "globaltags", value = "1.2.0")
@MethodOrder(after = "localizedResponses")
@SpriteSlot(size = 32, y = 2, x = 3)
@ButtonSetting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.rappytv.globaltags.api.ApiHandler;
import com.rappytv.globaltags.config.widget.TagPreviewWidget;
import com.rappytv.globaltags.config.widget.TagPreviewWidget.TagPreviewSetting;
import com.rappytv.globaltags.types.GlobalFont;
import com.rappytv.globaltags.types.GlobalIcon;
import com.rappytv.globaltags.util.TagCache;
import com.rappytv.globaltags.util.Util;
Expand Down Expand Up @@ -30,8 +31,9 @@ public TagSubConfig() {
TagPreviewWidget::change
);
tag.addChangeListener(runnable);
font.addChangeListener(runnable);
position.addChangeListener(runnable);
globalIcon.addChangeListener(runnable);
icon.addChangeListener(runnable);
}

@SpriteSlot(size = 32, x = 1)
Expand All @@ -43,15 +45,20 @@ public TagSubConfig() {
@SpriteSlot(size = 32, y = 1)
private final ConfigProperty<String> tag = new ConfigProperty<>("");

@IntroducedIn(namespace = "globaltags", value = "1.2.1")
@SpriteSlot(size = 32, y = 3)
@DropdownSetting
private final ConfigProperty<GlobalFont> font = new ConfigProperty<>(GlobalFont.DEFAULT);

@DropdownSetting
@SpriteSlot(size = 32, x = 3)
private final ConfigProperty<PositionType> position = new ConfigProperty<>(PositionType.ABOVE_NAME);

@DropdownSetting
@SpriteSlot(size = 32, y = 1, x = 2)
private final ConfigProperty<GlobalIcon> globalIcon = new ConfigProperty<>(GlobalIcon.NONE);
private final ConfigProperty<GlobalIcon> icon = new ConfigProperty<>(GlobalIcon.NONE);

@MethodOrder(after = "globalIcon")
@MethodOrder(after = "icon")
@ButtonSetting
@SpriteSlot(size = 32, y = 1, x = 1)
@SuppressWarnings("ConstantConditions")
Expand All @@ -74,12 +81,17 @@ else if(info == null)
else Util.update(ResultType.TAG, response.getMessage());
});
else Util.update(ResultType.TAG, Util.unchanged);
if(!info.getFont().equals(font.get())) ApiHandler.setFont(font.get(), (response) -> {
if(response.isSuccessful()) Util.update(ResultType.FONT, Component.text("✔", NamedTextColor.GREEN));
else Util.update(ResultType.FONT, response.getMessage());
});
else Util.update(ResultType.FONT, Util.unchanged);
if(!info.getPosition().equals(position.get())) ApiHandler.setPosition(position.get(), (response) -> {
if(response.isSuccessful()) Util.update(ResultType.POSITION, Component.text("✔", NamedTextColor.GREEN));
else Util.update(ResultType.POSITION, response.getMessage());
});
else Util.update(ResultType.POSITION, Util.unchanged);
if(!info.getGlobalIcon().equals(globalIcon.get())) ApiHandler.setIcon(globalIcon.get(), (response) -> {
if(!info.getGlobalIcon().equals(icon.get())) ApiHandler.setIcon(icon.get(), (response) -> {
if(response.isSuccessful()) Util.update(ResultType.ICON, Component.text("✔", NamedTextColor.GREEN));
else Util.update(ResultType.ICON, response.getMessage());
});
Expand All @@ -106,11 +118,13 @@ public void resetTag(Setting setting) {
public ConfigProperty<String> tag() {
return tag;
}
public ConfigProperty<GlobalFont> font() {
return font;
}
public ConfigProperty<PositionType> position() {
return position;
}

public ConfigProperty<GlobalIcon> icon() {
return globalIcon;
return icon;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ public void initialize(boolean refetched) {
} else {
if(refetched) {
config.tag().set(info.getPlainTag());
config.font().set(info.getFont());
config.position().set(info.getPosition());
config.icon().set(info.getGlobalIcon());
}
boolean updated = !config.tag().get().equals(info.getPlainTag())
|| !config.font().get().equals(info.getFont())
|| !config.position().get().equals(info.getPosition())
|| !config.icon().get().equals(info.getGlobalIcon());
if(changed && updated) {
Expand All @@ -87,13 +89,14 @@ public void initialize(boolean refetched) {
I18n.translate("globaltags.settings.tags.staged.description")
);
}
setVariable("--staff-margin", 2 + config.font().get().getStaffMargin());
ComponentWidget tag = ComponentWidget.component(
config.tag().get().isBlank()
? Component.translatable(
"globaltags.settings.tags.tagPreview.empty",
NamedTextColor.RED
)
: Util.translateColorCodes(config.tag().get())
: Util.formatTag(config.tag().get(), config.font().get())
).addId("text");
if (config.icon().get() != GlobalIcon.NONE)
this.addEntryInitialized(new IconWidget(config.icon().get().getIcon()).addId("icon"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ public void render(Stack stack, Entity entity) {

Laby.labyAPI().renderPipeline().renderSeeThrough(entity, () -> {
if(info.getIcon() != null) info.getIcon().render(stack, -11, 0, 9, 9);
if(info.isAdmin()) admin.render(stack, getWidth() + 0.9F, -1.2F, 11, 11);
if(info.isAdmin()) admin.render(
stack,
getWidth() + 0.9F + (float) info.getFont().getStaffMargin(),
-1.2F,
11,
11
);
});
}

Expand Down
14 changes: 14 additions & 0 deletions core/src/main/resources/assets/globaltags/font/alagard.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"providers": [
{
"type": "ttf",
"file": "globaltags:alagard.ttf",
"shift": [
0,
1.6
],
"size": 8.0,
"oversample": 10.0
}
]
}
Binary file not shown.
14 changes: 14 additions & 0 deletions core/src/main/resources/assets/globaltags/font/dark_souls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"providers": [
{
"type": "ttf",
"file": "globaltags:dark_souls.ttf",
"shift": [
0,
0.8
],
"size": 11.0,
"oversample": 11.0
}
]
}
Binary file not shown.
Loading
Loading