Skip to content

Commit

Permalink
Merge pull request #50 from Global-Tags/feat/referrals
Browse files Browse the repository at this point in the history
Implement referral bullet point
  • Loading branch information
RappyTV authored Jul 17, 2024
2 parents e684811 + a54f5bb commit 4ab5d7c
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 7 deletions.
29 changes: 24 additions & 5 deletions api/src/main/java/com/rappytv/globaltags/api/ApiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
@SuppressWarnings("unused")
public class ApiHandler {

// https://github.com/elysiajs/elysia/issues/495
private static final Map<String, Object> emptyBody = Map.of("body", "placeholder body");

private ApiHandler() {}
Expand Down Expand Up @@ -61,6 +62,8 @@ public Map<String, Object> getBody() {
request.responseBody.tag,
request.responseBody.position,
request.responseBody.icon,
request.responseBody.referred,
request.responseBody.referrals,
request.responseBody.roles,
request.responseBody.ban
));
Expand Down Expand Up @@ -154,7 +157,27 @@ public static void resetTag(UUID uuid, Consumer<ApiResponse> consumer) {
) {
@Override
public Map<String, Object> getBody() {
// https://github.com/elysiajs/elysia/issues/495
return emptyBody;
}
};
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 referPlayer(UUID uuid, Consumer<ApiResponse> consumer) {
ApiRequest request = new ApiRequest(
Method.POST,
"/players/" + uuid + "/referral",
Util.getSessionToken()
) {
@Override
public Map<String, Object> getBody() {
return emptyBody;
}
};
Expand Down Expand Up @@ -217,7 +240,6 @@ public static void unbanPlayer(UUID uuid, Consumer<ApiResponse> consumer) {
) {
@Override
public Map<String, Object> getBody() {
// https://github.com/elysiajs/elysia/issues/495
return emptyBody;
}
};
Expand Down Expand Up @@ -281,7 +303,6 @@ public static void toggleAdmin(UUID uuid, Consumer<ApiResponse> consumer) {
) {
@Override
public Map<String, Object> getBody() {
// https://github.com/elysiajs/elysia/issues/495
return emptyBody;
}
};
Expand All @@ -303,7 +324,6 @@ public static void linkDiscord(Consumer<ApiResponse> consumer) {
) {
@Override
public Map<String, Object> getBody() {
// https://github.com/elysiajs/elysia/issues/495
return emptyBody;
}
};
Expand All @@ -325,7 +345,6 @@ public static void unlinkDiscord(Consumer<ApiResponse> consumer) {
) {
@Override
public Map<String, Object> getBody() {
// https://github.com/elysiajs/elysia/issues/495
return emptyBody;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class ResponseBody {
public String tag;
public String position;
public String icon;
public boolean referred;
public int referrals;
public String[] roles;
public String message;
public Ban ban;
Expand Down
29 changes: 28 additions & 1 deletion api/src/main/java/com/rappytv/globaltags/types/PlayerInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,28 @@ public class PlayerInfo {
private final String plainTag;
private final String position;
private final String icon;
private final boolean referred;
private final int referrals;
private final List<GlobalRole> roles;
private final Suspension suspension;

public PlayerInfo(UUID uuid, String tag, String position, String icon, String[] roles, Ban ban) {
public PlayerInfo(
UUID uuid,
String tag,
String position,
String icon,
boolean referred,
int referrals,
String[] roles,
Ban ban
) {
this.uuid = uuid;
this.tag = Util.translateColorCodes(tag);
this.plainTag = tag != null ? tag : "";
this.position = position;
this.icon = icon;
this.referred = referred;
this.referrals = referrals;
this.roles = new ArrayList<>();
for(String role : roles) {
try {
Expand Down Expand Up @@ -101,6 +114,20 @@ public boolean isAdmin() {
return roles.contains(GlobalRole.ADMIN);
}

/**
* Returns if the player has referred to another player for inviting them
*/
public boolean hasReferred() {
return referred;
}

/**
* Returns how many other players were invited by the player
*/
public int getReferrals() {
return referrals;
}

/**
* Returns all of the players roles
*/
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.1")
version = System.getenv().getOrDefault("VERSION", "1.2.2")
}

minecraft {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/com/rappytv/globaltags/GlobalTagAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.rappytv.globaltags.command.GlobalTagCommand;
import com.rappytv.globaltags.config.GlobalTagConfig;
import com.rappytv.globaltags.interaction.EditBanInfoBulletPoint;
import com.rappytv.globaltags.interaction.ReferPlayerBulletPoint;
import com.rappytv.globaltags.interaction.ToggleBanBulletPoint;
import com.rappytv.globaltags.interaction.ChangeTagBulletPoint;
import com.rappytv.globaltags.interaction.ClearTagBulletPoint;
Expand Down Expand Up @@ -57,6 +58,7 @@ protected void enable() {
labyAPI().interactionMenuRegistry().register(new ChangeTagBulletPoint());
labyAPI().interactionMenuRegistry().register(new ClearTagBulletPoint());
labyAPI().interactionMenuRegistry().register(new EditBanInfoBulletPoint());
labyAPI().interactionMenuRegistry().register(new ReferPlayerBulletPoint());
labyAPI().interactionMenuRegistry().register(new ReportBulletPoint());
labyAPI().interactionMenuRegistry().register(new ToggleAdminBulletPoint());
labyAPI().interactionMenuRegistry().register(new ToggleBanBulletPoint());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.rappytv.globaltags.interaction;

import com.rappytv.globaltags.GlobalTagAddon;
import com.rappytv.globaltags.api.ApiHandler;
import com.rappytv.globaltags.types.PlayerInfo;
import com.rappytv.globaltags.util.TagCache;
import net.labymod.api.Laby;
import net.labymod.api.client.component.Component;
import net.labymod.api.client.entity.player.Player;
import net.labymod.api.client.entity.player.interaction.BulletPoint;
import net.labymod.api.client.gui.icon.Icon;

public class ReferPlayerBulletPoint implements BulletPoint {

@Override
public Component getTitle() {
return Component.translatable("globaltags.context.referral.name");
}

@Override
public Icon getIcon() {
return null;
}

@Override
public void execute(Player player) {
ApiHandler.referPlayer(player.getUniqueId(), (response) -> Laby.references().chatExecutor().displayClientMessage(
Component.empty()
.append(GlobalTagAddon.prefix)
.append(response.getMessage())
));
}

@Override
public boolean isVisible(Player player) {
PlayerInfo executer = TagCache.get(Laby.labyAPI().getUniqueId());
return executer != null && !executer.hasReferred();
}
}
3 changes: 3 additions & 0 deletions core/src/main/resources/assets/globaltags/i18n/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@
"context": {
"reason": "Reason",
"placeholder": "Please enter a reason...",
"referral": {
"name": "This player invited me to GlobalTags"
},
"report": {
"name": "Report GlobalTag",
"title": "Report %s",
Expand Down

0 comments on commit 4ab5d7c

Please sign in to comment.