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

Implement per-player tag position #2

Merged
merged 3 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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.0.2")
version = System.getenv().getOrDefault("VERSION", "1.0.3")
}

minecraft {
Expand Down
45 changes: 29 additions & 16 deletions core/src/main/java/com/rappytv/globaltags/api/ApiHandler.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.rappytv.globaltags.api;

import com.rappytv.globaltags.util.PlayerInfo;
import com.rappytv.globaltags.util.Util;
import net.labymod.api.Laby;
import net.labymod.api.client.entity.player.tag.PositionType;
import net.labymod.api.util.I18n;
import javax.inject.Singleton;
import java.util.UUID;
Expand All @@ -13,22 +15,20 @@ public String getApiVersion() {
ApiRequest request = new ApiRequest(
"GET",
"/",
"",
null
""
);

return request.getVersion();
}

public String getTag(UUID uuid) {
public PlayerInfo getInfo(UUID uuid) {
ApiRequest request = new ApiRequest(
"GET",
"/players/" + uuid,
Util.getSessionToken(),
null
Util.getSessionToken()
);

return request.getTag();
return new PlayerInfo(request.getTag(), request.getPosition());
}

public void setTag(String tag) {
Expand All @@ -40,39 +40,52 @@ public void setTag(String tag) {
);

if(!request.isSuccessful()) {
Util.notify(I18n.translate("globaltags.notifications.error"), request.getError(), null);
Util.notify(I18n.translate("globaltags.notifications.error"), request.getError(), true);
return;
}
Util.notify(I18n.translate("globaltags.notifications.success"), request.getMessage(), null);
Util.notify(I18n.translate("globaltags.notifications.success"), request.getMessage(), false);
}

public void setPosition(PositionType position) {
ApiRequest request = new ApiRequest(
"POST",
"/players/" + Laby.labyAPI().getUniqueId() + "/position",
Util.getSessionToken(),
position
);

if(!request.isSuccessful()) {
Util.notify(I18n.translate("globaltags.notifications.error"), request.getError(), true);
return;
}
Util.notify(I18n.translate("globaltags.notifications.success"), request.getMessage(), false);
}

public void resetTag() {
ApiRequest request = new ApiRequest(
"DELETE",
"/players/" + Laby.labyAPI().getUniqueId(),
Util.getSessionToken(),
null
Util.getSessionToken()
);

if(!request.isSuccessful()) {
Util.notify(I18n.translate("globaltags.notifications.error"), request.getError(), null);
Util.notify(I18n.translate("globaltags.notifications.error"), request.getError(), true);
return;
}
Util.notify(I18n.translate("globaltags.notifications.success"), request.getMessage(), null);
Util.notify(I18n.translate("globaltags.notifications.success"), request.getMessage(), false);
}

public void reportPlayer(UUID uuid) {
ApiRequest request = new ApiRequest(
"POST",
"/players/" + uuid + "/report",
Util.getSessionToken(),
null
Util.getSessionToken()
);

if(!request.isSuccessful()) {
Util.notify(I18n.translate("globaltags.notifications.error"), request.getError(), null);
Util.notify(I18n.translate("globaltags.notifications.error"), request.getError(), true);
return;
}
Util.notify(I18n.translate("globaltags.notifications.success"), request.getMessage(), null);
Util.notify(I18n.translate("globaltags.notifications.success"), request.getMessage(), false);
}
}
25 changes: 17 additions & 8 deletions core/src/main/java/com/rappytv/globaltags/api/ApiRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.google.gson.Gson;
import com.rappytv.globaltags.GlobalTagAddon;
import net.labymod.api.client.entity.player.tag.PositionType;
import net.labymod.api.util.I18n;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -19,17 +19,22 @@ public class ApiRequest {
private boolean successful;
private String message;
private String tag;
private String position;
private String error;
private String version;

public ApiRequest(String method, String path, String key, @Nullable String tag) {
Gson gson = new Gson();
public ApiRequest(String method, String path, String key) {
this(method, path, key, BodyPublishers.noBody());
}
public ApiRequest(String method, String path, String key, String tag) {
this(method, path, key, BodyPublishers.ofString(new Gson().toJson(new RequestBody(tag))));
}
public ApiRequest(String method, String path, String key, PositionType type) {
this(method, path, key, BodyPublishers.ofString(new Gson().toJson(new RequestBody(type))));
}

private ApiRequest(String method, String path, String key, BodyPublisher bodyPublisher) {
try {
BodyPublisher bodyPublisher = tag == null ?
BodyPublishers.noBody() :
BodyPublishers.ofString(gson.toJson(new RequestBody(tag)));

HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("https://gt.rappytv.com" + path))
.header("Content-Type", "application/json")
Expand All @@ -41,7 +46,7 @@ public ApiRequest(String method, String path, String key, @Nullable String tag)
HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());

ResponseBody responseBody = gson.fromJson(response.body(), ResponseBody.class);
ResponseBody responseBody = new Gson().fromJson(response.body(), ResponseBody.class);

if(responseBody.error != null) {
error = responseBody.error;
Expand All @@ -56,6 +61,7 @@ public ApiRequest(String method, String path, String key, @Nullable String tag)

this.message = responseBody.message;
this.tag = responseBody.tag;
this.position = responseBody.position;
successful = true;
} catch (IOException | InterruptedException | URISyntaxException e) {
e.printStackTrace();
Expand All @@ -73,6 +79,9 @@ public String getMessage() {
public String getTag() {
return tag;
}
public String getPosition() {
return position;
}
public String getError() {
return error;
}
Expand Down
18 changes: 17 additions & 1 deletion core/src/main/java/com/rappytv/globaltags/api/RequestBody.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package com.rappytv.globaltags.api;

public record RequestBody(String tag) {
import net.labymod.api.client.entity.player.tag.PositionType;

public class RequestBody {

public String tag;
public String position;

public RequestBody(String tag) {
this.tag = tag;
}
public RequestBody(PositionType type) {
position = switch (type) {
case ABOVE_NAME -> "ABOVE";
case BELOW_NAME -> "BELOW";
case RIGHT_TO_NAME -> "RIGHT";
case LEFT_TO_NAME -> "LEFT";
};
}
}
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 position;
public String message;

// For errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import com.rappytv.globaltags.config.subconfig.TagSubConfig;
import net.labymod.api.addon.AddonConfig;
import net.labymod.api.client.entity.player.tag.PositionType;
import net.labymod.api.client.gui.screen.widget.widgets.input.SliderWidget.SliderSetting;
import net.labymod.api.client.gui.screen.widget.widgets.input.SwitchWidget.SwitchSetting;
import net.labymod.api.client.gui.screen.widget.widgets.input.dropdown.DropdownWidget.DropdownSetting;
import net.labymod.api.configuration.loader.annotation.ConfigName;
import net.labymod.api.configuration.loader.annotation.SpriteSlot;
import net.labymod.api.configuration.loader.annotation.SpriteTexture;
Expand All @@ -24,9 +22,6 @@ public class GlobalTagConfig extends AddonConfig {
@SliderSetting(min = 5, max = 10)
@SpriteSlot(size = 32, x = 2)
private final ConfigProperty<Integer> tagSize = new ConfigProperty<>(10);
@DropdownSetting
@SpriteSlot(size = 32, x = 3)
private final ConfigProperty<PositionType> position = new ConfigProperty<>(PositionType.ABOVE_NAME);
@SpriteSlot(size = 32, x = 1)
private final TagSubConfig tags = new TagSubConfig();

Expand All @@ -40,7 +35,4 @@ public ConfigProperty<Boolean> showOwnTag() {
public ConfigProperty<Integer> tagSize() {
return tagSize;
}
public ConfigProperty<PositionType> position() {
return position;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.rappytv.globaltags.GlobalTagAddon;
import com.rappytv.globaltags.api.ApiHandler;
import com.rappytv.globaltags.util.Util;
import net.labymod.api.client.entity.player.tag.PositionType;
import net.labymod.api.client.gui.screen.widget.widgets.input.ButtonWidget.ButtonSetting;
import net.labymod.api.client.gui.screen.widget.widgets.input.TextFieldWidget.TextFieldSetting;
import net.labymod.api.client.gui.screen.widget.widgets.input.dropdown.DropdownWidget.DropdownSetting;
import net.labymod.api.configuration.loader.Config;
import net.labymod.api.configuration.loader.annotation.SpriteSlot;
import net.labymod.api.configuration.loader.property.ConfigProperty;
Expand All @@ -18,6 +20,7 @@ public class TagSubConfig extends Config {

public TagSubConfig() {
apiHandler = GlobalTagAddon.getAddon().getApiHandler();
position.addChangeListener((property, oldValue, newValue) -> apiHandler.setPosition(newValue));
}

@TextFieldSetting
Expand All @@ -33,7 +36,11 @@ public void setTag(Setting setting) {
Util.clearCache(false);
}

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

@MethodOrder(after = "position")
@ButtonSetting
@SpriteSlot(size = 32, y = 1, x = 2)
public void resetTag(Setting setting) {
Expand All @@ -49,7 +56,7 @@ public void clearCache(Setting setting) {
Util.notify(
I18n.translate("globaltags.notifications.success"),
I18n.translate("globaltags.notifications.cacheCleared"),
null
true
);
}
}
21 changes: 11 additions & 10 deletions core/src/main/java/com/rappytv/globaltags/nametag/CustomTag.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rappytv.globaltags.nametag;

import com.rappytv.globaltags.GlobalTagAddon;
import com.rappytv.globaltags.util.PlayerInfo;
import com.rappytv.globaltags.util.TagCache;
import net.labymod.api.Laby;
import net.labymod.api.client.component.Component;
Expand All @@ -14,11 +15,11 @@
public class CustomTag extends NameTag {

private final GlobalTagAddon addon;
private final PositionType positionType;
private final PositionType position;

public CustomTag(GlobalTagAddon addon, PositionType positionType) {
public CustomTag(GlobalTagAddon addon, PositionType position) {
this.addon = addon;
this.positionType = positionType;
this.position = position;
}

@Override
Expand All @@ -30,21 +31,21 @@ public float getScale() {
protected @Nullable RenderableComponent getRenderableComponent() {
if(!addon.configuration().enabled().get()) return null;
if(entity == null || !(entity instanceof Player)) return null;
if(!addon.configuration().position().get().equals(positionType)) return null;
UUID uuid = entity.getUniqueId();
if(!addon.configuration().showOwnTag().get() && Laby.labyAPI().getUniqueId().equals(uuid)) return null;

String tag;
PlayerInfo info;
if(TagCache.has(uuid))
tag = TagCache.get(uuid);
info = TagCache.get(uuid);
else {
tag = addon.getApiHandler().getTag(uuid);
TagCache.add(uuid, tag);
info = addon.getApiHandler().getInfo(uuid);
TagCache.add(uuid, info);
}
if(tag == null) return null;
if(info.getTag() == null) return null;
if(!position.equals(info.getPosition())) return null;

return RenderableComponent.of(Component.text(
tag.replace('&', '§')
info.getTag().replace('&', '§')
));
}
}
27 changes: 27 additions & 0 deletions core/src/main/java/com/rappytv/globaltags/util/PlayerInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.rappytv.globaltags.util;

import net.labymod.api.client.entity.player.tag.PositionType;

public class PlayerInfo {

private final String tag;
private final String position;

public PlayerInfo(String tag, String position) {
this.tag = tag;
this.position = position;
}

public String getTag() {
return tag;
}

public PositionType getPosition() {
return switch(position) {
default -> PositionType.ABOVE_NAME;
case "BELOW" -> PositionType.BELOW_NAME;
case "RIGHT" -> PositionType.RIGHT_TO_NAME;
case "LEFT" -> PositionType.LEFT_TO_NAME;
};
}
}
8 changes: 4 additions & 4 deletions core/src/main/java/com/rappytv/globaltags/util/TagCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

public class TagCache {

private static final Map<UUID, String> cache = new HashMap<>();
private static final Map<UUID, PlayerInfo> cache = new HashMap<>();

public static void add(UUID uuid, String tag) {
cache.put(uuid, tag);
public static void add(UUID uuid, PlayerInfo info) {
cache.put(uuid, info);
}
public static boolean has(UUID uuid) {
return cache.containsKey(uuid);
}
public static String get(UUID uuid) {
public static PlayerInfo get(UUID uuid) {
return cache.get(uuid);
}
public static boolean isEmpty() {
Expand Down
Loading