Skip to content

Commit b73f774

Browse files
committed
patreon rewards optimized
1 parent 1aef052 commit b73f774

File tree

1 file changed

+37
-36
lines changed
  • src/main/java/xxrexraptorxx/additionalstructures/utils

1 file changed

+37
-36
lines changed

src/main/java/xxrexraptorxx/additionalstructures/utils/Events.java

+37-36
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,33 @@
3838
import java.net.http.HttpResponse;
3939
import java.util.List;
4040
import java.util.Scanner;
41+
import java.util.concurrent.CompletableFuture;
4142

4243
@EventBusSubscriber(modid = References.MODID, bus = EventBusSubscriber.Bus.GAME)
4344
public class Events {
4445

45-
/** Update-Checker **/
46+
/** Update Checker **/
4647
private static boolean hasShownUp = false;
4748

4849
@SubscribeEvent
4950
public static void onClientTick(ClientTickEvent.Pre event) {
5051
if (Config.UPDATE_CHECKER != null && Config.UPDATE_CHECKER.get()) {
5152

5253
if (!hasShownUp && Minecraft.getInstance().screen == null) {
54+
var player = Minecraft.getInstance().player;
55+
if (player == null) return; // Added null check
56+
5357
var modContainer = ModList.get().getModContainerById(References.MODID).orElse(null);
5458

5559
if (modContainer != null) {
5660
var versionCheckResult = VersionChecker.getResult(modContainer.getModInfo());
5761

5862
if (versionCheckResult.status() == VersionChecker.Status.OUTDATED || versionCheckResult.status() == VersionChecker.Status.BETA_OUTDATED) {
63+
MutableComponent url = Component.literal(ChatFormatting.GREEN + "Click here to update!")
64+
.withStyle(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, References.URL)));
5965

60-
MutableComponent url = Component.literal(ChatFormatting.GREEN + "Click here to update!");
61-
url.withStyle(url.getStyle().withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, References.URL)));
62-
63-
Minecraft.getInstance().player.displayClientMessage(Component.literal(ChatFormatting.BLUE + "A newer version of " + ChatFormatting.YELLOW + References.NAME + ChatFormatting.BLUE + " is available!"), false);
64-
Minecraft.getInstance().player.displayClientMessage(url, false);
66+
player.displayClientMessage(Component.literal(ChatFormatting.BLUE + "A newer version of " + ChatFormatting.YELLOW + References.NAME + ChatFormatting.BLUE + " is available!"), false);
67+
player.displayClientMessage(url, false);
6568

6669
hasShownUp = true;
6770

@@ -75,59 +78,56 @@ public static void onClientTick(ClientTickEvent.Pre event) {
7578
}
7679

7780

78-
7981
private static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient();
8082

8183
/**
82-
* Distributes the supporter rewards on first join.
84+
* Distributes supporter rewards on first login.
8385
*/
8486
@SubscribeEvent
8587
public static void SupporterRewards(PlayerEvent.PlayerLoggedInEvent event) {
8688
Player player = event.getEntity();
8789
Level level = player.level();
8890

8991
if (Config.PATREON_REWARDS.get()) {
90-
try {
91-
URI SUPPORTER_URI = URI.create("https://raw.githubusercontent.com/XxRexRaptorxX/Patreons/main/Supporter");
92-
URI PREMIUM_SUPPORTER_URI = URI.create("https://raw.githubusercontent.com/XxRexRaptorxX/Patreons/main/Premium%20Supporter");
93-
URI ELITE_URI = URI.create("https://raw.githubusercontent.com/XxRexRaptorxX/Patreons/main/Elite");
94-
95-
// Test if a player already has rewards
96-
if (!player.getInventory().contains(new ItemStack(Items.PAPER))) {
97-
98-
ServerPlayer serverPlayer = (ServerPlayer) player;
99-
// Test if player joins the first time
92+
// Check if the player already has rewards
93+
if (!player.getInventory().contains(new ItemStack(Items.PAPER))) {
94+
if (player instanceof ServerPlayer serverPlayer) { // Ensure the player is a ServerPlayer
95+
// Check if the player is logging in for the first time
10096
if (serverPlayer.getStats().getValue(Stats.CUSTOM, Stats.PLAY_TIME) < 5) {
10197

102-
// Test if player is supporter
103-
if (SupporterCheck(SUPPORTER_URI, player)) {
104-
giveSupporterReward(player, level);
105-
}
106-
107-
// Test if player is premium supporter
108-
if (SupporterCheck(PREMIUM_SUPPORTER_URI, player)) {
109-
givePremiumSupporterReward(player, level);
110-
}
111-
112-
// Test if player is elite
113-
if (SupporterCheck(ELITE_URI, player)) {
114-
giveEliteReward(player);
115-
}
98+
// Perform supporter checks asynchronously
99+
CompletableFuture.runAsync(() -> {
100+
if (SupporterCheck(URI.create("https://raw.githubusercontent.com/XxRexRaptorxX/Patreons/main/Supporter"), player)) {
101+
giveSupporterReward(player, level);
102+
}
103+
if (SupporterCheck(URI.create("https://raw.githubusercontent.com/XxRexRaptorxX/Patreons/main/Premium%20Supporter"), player)) {
104+
givePremiumSupporterReward(player, level);
105+
}
106+
if (SupporterCheck(URI.create("https://raw.githubusercontent.com/XxRexRaptorxX/Patreons/main/Elite"), player)) {
107+
giveEliteReward(player);
108+
}
109+
});
116110
}
117111
}
118-
} catch (Exception e) {
119-
AdditionalStructures.LOGGER.error("An unexpected error occurred while getting the Supporter URI lists", e);
120112
}
121113
}
122114
}
123115

116+
117+
/**
118+
* Checks if the player is in the supporter list from the given URI.
119+
*
120+
* @param uri URI to a file containing supporter names
121+
* @param player The in-game player
122+
* @return true if the player is a supporter, otherwise false
123+
*/
124124
private static boolean SupporterCheck(URI uri, Player player) {
125125
try {
126126
HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build();
127127
HttpResponse<String> response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
128128

129-
//analyze supporter list
130-
List<String> supporterList = List.of(response.body().split("\\R")); //split lines
129+
// Parse supporter list
130+
List<String> supporterList = List.of(response.body().split("\\R")); // Split lines
131131
return supporterList.contains(player.getName().getString());
132132

133133
} catch (Exception e) {
@@ -136,6 +136,7 @@ private static boolean SupporterCheck(URI uri, Player player) {
136136
}
137137
}
138138

139+
139140
private static void giveSupporterReward(Player player, Level level) {
140141
ItemStack certificate = new ItemStack(Items.PAPER);
141142
certificate.set(DataComponents.CUSTOM_NAME, Component.literal("Thank you for supporting me in my work!").withStyle(ChatFormatting.GOLD)

0 commit comments

Comments
 (0)