Skip to content
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
3 changes: 3 additions & 0 deletions src/main/java/de/rettichlp/ucutils/UCUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class UCUtils implements ModInitializer {
public void onInitialize() {
// This entrypoint is suitable for setting up client-specific logic, such as rendering.

syncService.syncFactionMembers();

this.registry.registerSounds();

ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> {
Expand All @@ -64,6 +66,7 @@ public void onInitialize() {
client.execute(() -> {
this.registry.registerListeners();
renderService.initializeWidgets();
syncService.syncFactionSpecificData();
syncService.checkForUpdates();
});
});
Expand Down
63 changes: 4 additions & 59 deletions src/main/java/de/rettichlp/ucutils/command/ModCommand.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
package de.rettichlp.ucutils.command;

import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import de.rettichlp.ucutils.common.registry.CommandBase;
import de.rettichlp.ucutils.common.registry.UCUtilsCommand;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.metadata.Person;
import net.minecraft.client.network.PlayerListEntry;
import net.minecraft.text.ClickEvent;
import org.jetbrains.annotations.NotNull;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.StringJoiner;

import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static de.rettichlp.ucutils.UCUtils.MOD_ID;
import static de.rettichlp.ucutils.UCUtils.commandService;
import static de.rettichlp.ucutils.UCUtils.messageService;
import static de.rettichlp.ucutils.UCUtils.networkHandler;
import static de.rettichlp.ucutils.UCUtils.player;
import static de.rettichlp.ucutils.UCUtils.storage;
import static de.rettichlp.ucutils.UCUtils.syncService;
import static de.rettichlp.ucutils.UCUtils.utilService;
import static java.net.URI.create;
import static java.time.LocalDateTime.MIN;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;
import static net.minecraft.command.CommandSource.suggestMatching;
import static net.minecraft.text.Text.empty;
import static net.minecraft.text.Text.of;
import static net.minecraft.util.Formatting.DARK_GRAY;
Expand All @@ -42,56 +32,18 @@ public class ModCommand extends CommandBase {
@Override
public LiteralArgumentBuilder<FabricClientCommandSource> execute(@NotNull LiteralArgumentBuilder<FabricClientCommandSource> node) {
return node
.then(literal("userinfo")
.requires(fabricClientCommandSource -> commandService.isSuperUser())
.then(argument("player", word())
.suggests((context, builder) -> {
List<String> list = networkHandler.getPlayerList().stream()
.map(PlayerListEntry::getProfile)
.map(GameProfile::name)
.toList();
return suggestMatching(list, builder);
})
.executes(context -> {
String playerName = context.getArgument("player", String.class);
// api.getUserInfo(playerName, response -> {
//
// player.sendMessage(empty(), false);
//
// messageService.sendModMessage("UCUtils User Information - " + playerName, false);
//
// messageService.sendModMessage(empty()
// .append(of("Version").copy().formatted(GRAY))
// .append(of(":").copy().formatted(DARK_GRAY)).append(" ")
// .append(of(response.version()).copy().formatted(WHITE)), false);
//
// messageService.sendModMessage(empty()
// .append(of("Aktivitäten").copy().formatted(GRAY))
// .append(of(":").copy().formatted(DARK_GRAY)).append(" ")
// .append(of("Klick ↗").copy().styled(style -> style
// .withColor(WHITE)
// .withClickEvent(new ClickEvent.SuggestCommand("/activity player " + playerName)))), false);
//
// player.sendMessage(empty(), false);
// });

return 1;
})))
.then(literal("sync")
.then(literal("faction")
.executes(context -> {
syncService.syncFactionMembersWithCommand(() -> {});
return 1;
}))
.executes(context -> {
syncService.syncFactionMembers();
syncService.checkForUpdates();
syncService.syncFactionSpecificData();

utilService.delayedAction(syncService::syncFactionSpecificData, 2000);

return 1;
}))
.executes(context -> {
String version = utilService.getVersion();
String authors = getAuthors();
LocalDateTime lastSyncTimestamp = syncService.getLastSyncTimestamp();

player.sendMessage(empty(), false);

Expand All @@ -116,13 +68,6 @@ public LiteralArgumentBuilder<FabricClientCommandSource> execute(@NotNull Litera
.withColor(WHITE)
.withClickEvent(new ClickEvent.OpenUrl(create("https://github.com/UnicacityAddon/ucutils"))))), false);

messageService.sendModMessage(empty()
.append(of("Letzte Synchronisierung").copy().formatted(GRAY))
.append(of(":").copy().formatted(DARK_GRAY)).append(" ")
.append(of(lastSyncTimestamp.equals(MIN)
? "Nie"
: messageService.dateTimeToFriendlyString(lastSyncTimestamp)).copy().formatted(WHITE)), false);

player.sendMessage(empty(), false);

storage.print();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/rettichlp/ucutils/common/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public Faction getCachedFaction(String playerName) {
public Faction getFaction(String playerName) {
Faction faction = this.factionEntries.stream()
.filter(factionEntry -> factionEntry.members().stream()
.anyMatch(factionMember -> factionMember.playerName().equalsIgnoreCase(playerName)))
.anyMatch(factionMember -> factionMember.username().equalsIgnoreCase(playerName)))
.findFirst()
.map(FactionEntry::faction)
.orElse(NULL);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/de/rettichlp/ucutils/common/api/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.google.gson.reflect.TypeToken;
import com.mojang.authlib.GameProfile;
import de.rettichlp.ucutils.common.api.response.ErrorResponse;
import de.rettichlp.ucutils.common.models.Faction;
import de.rettichlp.ucutils.common.models.FactionMember;
import lombok.Getter;
import net.minecraft.client.MinecraftClient;
import org.jetbrains.annotations.Contract;
Expand Down Expand Up @@ -69,6 +71,10 @@ public void getModrinthVersions(Consumer<List<Map<String, Object>>> callback) {
get("https://api.modrinth.com/v2/project/ucutils/version", new TypeToken<>() {}, callback);
}

public void getFactionMembers(@NotNull Faction faction, Consumer<List<FactionMember>> callback) {
get("https://api.unicacity.eu/api/factions/" + faction.getApiKey() + "/members", new TypeToken<>() {}, callback);
}

private <T> void get(@NotNull String uri, TypeToken<T> typeToken, Consumer<T> callback) {
HttpRequest httpRequest = this.requestBuilder.copy()
.uri(uri.startsWith("https://") ? create(uri) : create(this.baseUrl + uri))
Expand Down
31 changes: 16 additions & 15 deletions src/main/java/de/rettichlp/ucutils/common/models/Faction.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,26 @@
@AllArgsConstructor
public enum Faction {

NULL("", false, GRAY, ""),
POLIZEI("Polizei", false, BLUE, "✯"),
FBI("FBI", false, DARK_BLUE, "✯"),
RETTUNGSDIENST("Rettungsdienst", false, DARK_RED, "✚"),
NULL("", "", false, GRAY, ""),
POLIZEI("Polizei", "police", false, BLUE, "✯"),
FBI("FBI", "fbi", false, DARK_BLUE, "✯"),
RETTUNGSDIENST("Rettungsdienst", "medic", false, DARK_RED, "✚"),

LA_COSA_NOSTRA("La Cosa Nostra", true, DARK_AQUA, "⚜"),
WESTSIDE_BALLAS("Westside Ballas", true, DARK_PURPLE, "☠"),
CALDERON_KARTELL("Calderón Kartell", true, GOLD, "☀"),
KERZAKOV_FAMILIE("Kerzakov Familie", true, RED, "✮"),
HAYAT_KARTELL("Hayat Kartell", true, DARK_GREEN, "Ħ"),
YAKUZA("Yakuza", true, GREEN, "☯"),
VELENTZAS("Velentzas", true, WHITE, "δ"),
LA_COSA_NOSTRA("La Cosa Nostra", "mafia", true, DARK_AQUA, "⚜"),
WESTSIDE_BALLAS("Westside Ballas", "gang", true, DARK_PURPLE, "☠"),
CALDERON_KARTELL("Calderón Kartell", "mexican", true, GOLD, "☀"),
KERZAKOV_FAMILIE("Kerzakov Familie", "kerzakov", true, RED, "✮"),
HAYAT_KARTELL("Hayat Kartell", "hayat_kartell", true, DARK_GREEN, "Ħ"),
YAKUZA("Yakuza", "yakuza", true, GREEN, "☯"),
VELENTZAS("Velentzas", "velentzas", true, WHITE, "δ"),

HITMAN("Hitman", false, AQUA, "➹"),
TERRORISTEN("Terroristen", false, GRAY, "❇"),
KIRCHE("Kirche", false, LIGHT_PURPLE, "†"),
NEWS("News", false, YELLOW, "✉");
HITMAN("Hitman", "hitman", false, AQUA, "➹"),
TERRORISTEN("Terroristen", "terror", false, GRAY, "❇"),
KIRCHE("Kirche", "church", false, LIGHT_PURPLE, "†"),
NEWS("News", "news", false, YELLOW, "✉");

private final String displayName;
private final String apiKey;
private final boolean isBadFaction;
private final Formatting color;
private final String icon;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.rettichlp.ucutils.common.models;

public record FactionMember(String playerName, int rank) {
import java.util.UUID;

public record FactionMember(int id, String username, UUID uuid, int rankNumber, String rankName, boolean isLeader, String gender) {

}
35 changes: 15 additions & 20 deletions src/main/java/de/rettichlp/ucutils/common/services/SyncService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package de.rettichlp.ucutils.common.services;

import de.rettichlp.ucutils.common.models.Faction;
import de.rettichlp.ucutils.common.models.FactionEntry;
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import static de.rettichlp.ucutils.UCUtils.LOGGER;
import static de.rettichlp.ucutils.UCUtils.api;
import static de.rettichlp.ucutils.UCUtils.commandService;
import static de.rettichlp.ucutils.UCUtils.notificationService;
Expand All @@ -16,9 +16,6 @@
import static de.rettichlp.ucutils.common.models.Faction.NULL;
import static de.rettichlp.ucutils.common.services.CommandService.COMMAND_COOLDOWN_MILLIS;
import static java.awt.Color.MAGENTA;
import static java.time.LocalDateTime.MIN;
import static java.time.LocalDateTime.now;
import static java.util.Arrays.stream;
import static java.util.Objects.nonNull;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.TimeUnit.MINUTES;
Expand All @@ -31,31 +28,29 @@

public class SyncService {

@Getter
private LocalDateTime lastSyncTimestamp = MIN;
@Getter
private boolean gameSyncProcessActive = false;

public void syncFactionMembersWithCommand(Runnable runAfter) {
this.gameSyncProcessActive = true;
public void syncFactionMembers() {
for (Faction faction : Faction.values()) {
if (faction == NULL) {
continue;
}

List<String> factionMemberInfoCommands = stream(Faction.values())
.filter(faction -> faction != NULL)
.map(faction -> "memberinfoall " + faction.getDisplayName())
.toList();
storage.getFactionEntries().clear();

commandService.sendCommands(factionMemberInfoCommands, 1000);
api.getFactionMembers(faction, factionMembers -> {
// to faction entry
FactionEntry factionEntry = new FactionEntry(faction, factionMembers);

utilService.delayedAction(() -> {
this.gameSyncProcessActive = false;
notificationService.sendSuccessNotification("Fraktionsmitglieder synchronisiert");
runAfter.run();
}, Faction.values().length * 1000L + 1000);
storage.getFactionEntries().add(factionEntry);
LOGGER.info("Faction members for faction {} synced ({} members)", faction, factionMembers.size());
});
}
}

public void syncFactionSpecificData() {
this.gameSyncProcessActive = true;
this.lastSyncTimestamp = now();

// parse from faction-related init commands after all faction members are synced
utilService.delayedAction(() -> {
Expand Down
52 changes: 0 additions & 52 deletions src/main/java/de/rettichlp/ucutils/listener/impl/SyncListener.java

This file was deleted.

Loading