38
38
import java .net .http .HttpResponse ;
39
39
import java .util .List ;
40
40
import java .util .Scanner ;
41
+ import java .util .concurrent .CompletableFuture ;
41
42
42
43
@ EventBusSubscriber (modid = References .MODID , bus = EventBusSubscriber .Bus .GAME )
43
44
public class Events {
44
45
45
- /** Update- Checker **/
46
+ /** Update Checker **/
46
47
private static boolean hasShownUp = false ;
47
48
48
49
@ SubscribeEvent
49
50
public static void onClientTick (ClientTickEvent .Pre event ) {
50
51
if (Config .UPDATE_CHECKER != null && Config .UPDATE_CHECKER .get ()) {
51
52
52
53
if (!hasShownUp && Minecraft .getInstance ().screen == null ) {
54
+ var player = Minecraft .getInstance ().player ;
55
+ if (player == null ) return ; // Added null check
56
+
53
57
var modContainer = ModList .get ().getModContainerById (References .MODID ).orElse (null );
54
58
55
59
if (modContainer != null ) {
56
60
var versionCheckResult = VersionChecker .getResult (modContainer .getModInfo ());
57
61
58
62
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 )));
59
65
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 );
65
68
66
69
hasShownUp = true ;
67
70
@@ -75,59 +78,56 @@ public static void onClientTick(ClientTickEvent.Pre event) {
75
78
}
76
79
77
80
78
-
79
81
private static final HttpClient HTTP_CLIENT = HttpClient .newHttpClient ();
80
82
81
83
/**
82
- * Distributes the supporter rewards on first join .
84
+ * Distributes supporter rewards on first login .
83
85
*/
84
86
@ SubscribeEvent
85
87
public static void SupporterRewards (PlayerEvent .PlayerLoggedInEvent event ) {
86
88
Player player = event .getEntity ();
87
89
Level level = player .level ();
88
90
89
91
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
100
96
if (serverPlayer .getStats ().getValue (Stats .CUSTOM , Stats .PLAY_TIME ) < 5 ) {
101
97
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
+ });
116
110
}
117
111
}
118
- } catch (Exception e ) {
119
- AdditionalStructures .LOGGER .error ("An unexpected error occurred while getting the Supporter URI lists" , e );
120
112
}
121
113
}
122
114
}
123
115
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
+ */
124
124
private static boolean SupporterCheck (URI uri , Player player ) {
125
125
try {
126
126
HttpRequest request = HttpRequest .newBuilder ().uri (uri ).GET ().build ();
127
127
HttpResponse <String > response = HTTP_CLIENT .send (request , HttpResponse .BodyHandlers .ofString ());
128
128
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
131
131
return supporterList .contains (player .getName ().getString ());
132
132
133
133
} catch (Exception e ) {
@@ -136,6 +136,7 @@ private static boolean SupporterCheck(URI uri, Player player) {
136
136
}
137
137
}
138
138
139
+
139
140
private static void giveSupporterReward (Player player , Level level ) {
140
141
ItemStack certificate = new ItemStack (Items .PAPER );
141
142
certificate .set (DataComponents .CUSTOM_NAME , Component .literal ("Thank you for supporting me in my work!" ).withStyle (ChatFormatting .GOLD )
0 commit comments