Skip to content

Commit

Permalink
merge: 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
itIsMaku authored Mar 23, 2024
2 parents 6d09c91 + 1335906 commit 64fe6a1
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class MommonsPluginBungee extends BungeeWorkerPlugin {
@Getter
private static MommonsPluginBungee plugin;

@Getter
private Configuration configuration;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.jetbrains.annotations.Nullable;

public final class WorkerReceiver {
public final class BungeeWorkerReceiver {

@Nullable
public static <T> T getService(Worker worker, Class<T> clazz) {
Expand Down
4 changes: 4 additions & 0 deletions mommons-shared/src/main/java/cz/maku/mommons/Mommons.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import okhttp3.OkHttpClient;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -14,4 +15,7 @@ public class Mommons {
.create();

public static final ExecutorService ES = Executors.newCachedThreadPool();

public static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder()
.build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void publish(LogRecord record) {
} else {
className = clazz.getName();
}
record.setLoggerName(String.format("%s%s%s - %s%s%s", ConsoleColors.CYAN_BRIGHT, Thread.currentThread().getName(), ConsoleColors.WHITE_BRIGHT, ConsoleColors.CYAN_BRIGHT, className, ConsoleColors.WHITE_BRIGHT));
record.setLoggerName(String.format("%s%s%s - %s%s%s", ConsoleColors.CYAN_BRIGHT, Thread.currentThread().getName(), ConsoleColors.WHITE_BRIGHT, ConsoleColors.CYAN_BRIGHT, className, ConsoleColors.RESET));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cz.maku.mommons.storage.database.type;

import cz.maku.mommons.storage.database.SQLRow;
import org.jetbrains.annotations.NotNull;

import java.sql.*;
import java.util.ArrayList;
Expand All @@ -22,6 +23,15 @@ public MySQL(Connection connection) {
instance = this;
}

@NotNull
public static Connection connection() {
if (instance == null) {
throw new RuntimeException("MySQL is not initialized!");
}

return instance.getConnection();
}

public static MySQL getApi() {
return instance;
}
Expand Down
24 changes: 24 additions & 0 deletions mommons-shared/src/main/java/cz/maku/mommons/utils/Cooldown.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cz.maku.mommons.utils;

import java.util.concurrent.TimeUnit;

public class Cooldown {

private final long cooldown;
private final TimeUnit timeUnit;
private final long executionTime;

public Cooldown(long cooldown, TimeUnit timeUnit) {
this.cooldown = cooldown;
this.timeUnit = timeUnit;
executionTime = System.currentTimeMillis();
}

public boolean isExpired() {
return System.currentTimeMillis() - executionTime >= timeUnit.toMillis(cooldown);
}

public static Cooldown of(long cooldown, TimeUnit timeUnit) {
return new Cooldown(cooldown, timeUnit);
}
}
44 changes: 44 additions & 0 deletions mommons-shared/src/main/java/cz/maku/mommons/utils/Rests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cz.maku.mommons.utils;

import com.google.common.collect.Maps;
import cz.maku.mommons.Mommons;
import okhttp3.*;

import java.io.IOException;
import java.util.Map;
import java.util.Optional;

public final class Rests {

private static <RES> Optional<RES> perform(Request request, Class<RES> clazz) throws IOException {
Call call = Mommons.HTTP_CLIENT.newCall(request);
Response response = call.execute();

Check warning on line 15 in mommons-shared/src/main/java/cz/maku/mommons/utils/Rests.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

AutoCloseable used without 'try'-with-resources

'Response' used without 'try'-with-resources statement
ResponseBody responseBody = response.body();
if (responseBody == null) return Optional.empty();
return Optional.of(Mommons.GSON.fromJson(responseBody.string(), clazz));
}

public static <RES> Optional<RES> get(String url, Class<RES> clazz, Map<String, String> headers) throws IOException {
Request request = new Request.Builder()
.url(url)
.headers(Headers.of(headers))
.build();

return perform(request, clazz);
}

public static <REQ, RES> Optional<RES> post(String url, REQ req, Class<RES> clazz, Map<String, String> headers) throws IOException {
RequestBody body = RequestBody.create(Mommons.GSON.toJson(req), MediaType.get("application/json"));
Request request = new Request.Builder()
.url(url)
.headers(Headers.of(headers))
.post(body)
.build();

return perform(request, clazz);
}

public static <T> Optional<T> get(String url, Class<T> clazz) throws IOException {
return get(url, clazz, Maps.newHashMap());
}
}
13 changes: 13 additions & 0 deletions mommons-shared/src/main/java/cz/maku/mommons/utils/Texts.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cz.maku.mommons.utils;

import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.List;
Expand All @@ -10,6 +11,13 @@
public final class Texts {

public static final char ARROW = '➤';
public static final char ARROW_RIGHT = '➜';
public static final char ARROW_LEFT = '⬅';
public static final char ARROW_UP = '⬆';
public static final char ARROW_DOWN = '⬇';
public static final char CORRECT = '✓';
public static final char INCORRECT = '✗';
public static final char STAR = '★';

public static List<String> createTextBlock(int averageLineLength, String text) {
List<String> lines = Lists.newArrayList();
Expand Down Expand Up @@ -102,4 +110,9 @@ public static String underscore(String s) {
}
return temp.toString();
}

public static String capitalize(@NotNull String s) {
return s.substring(0, 1).toUpperCase() + s.substring(1);
}

}

0 comments on commit 64fe6a1

Please sign in to comment.