Skip to content
Open
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
80 changes: 45 additions & 35 deletions src/client/java/com/padbro/greeterbro/client/GreeterBroClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,58 @@
import com.padbro.greeterbro.client.managers.AfkManager;
import com.padbro.greeterbro.client.managers.MigrationManager;
import com.padbro.greeterbro.client.managers.TickManager;
import com.padbro.greeterbro.config.GreeterBroServerConfig;
import com.padbro.greeterbro.records.ConfigS2CPayload;
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.ConfigHolder;
import me.shedaniel.autoconfig.serializer.Toml4jConfigSerializer;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;

public class GreeterBroClient implements ClientModInitializer {

private static ConfigHolder<GreeterBroConfig> config;
private static JoinCache joinCache;
public static boolean isJoining = false;
public static final String MOD_ID = "GreeterBro";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

public static GreeterBroConfig getConfig() {
config.save();
return config.get();
}

public static JoinCache getJoinCache() {
return joinCache;
}

public static void saveConfig() {
config.save();
}

@Override
public void onInitializeClient() {
config = AutoConfig.register(GreeterBroConfig.class, Toml4jConfigSerializer::new);
MigrationManager.migrate();

joinCache = JoinCache.loadCache();

ClientTickEvents.END_CLIENT_TICK.register(
client -> {
TickManager.onTick();
AfkManager.onTick();
public static boolean isJoining = false;
public static GreeterBroServerConfig serverConfig;
private static ConfigHolder<GreeterBroConfig> config;
private static JoinCache joinCache;

public static GreeterBroConfig getConfig() {
config.save();
return config.get();
}

public static JoinCache getJoinCache() {
return joinCache;
}

public static void saveConfig() {
config.save();
}

@Override
public void onInitializeClient() {
config = AutoConfig.register(GreeterBroConfig.class, Toml4jConfigSerializer::new);
MigrationManager.migrate();

joinCache = JoinCache.loadCache();

ClientTickEvents.END_CLIENT_TICK.register(
client -> {
TickManager.onTick();
AfkManager.onTick();
});

ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> {
serverConfig = null;
});

CommandManager.register();
}
CommandManager.register();

ClientPlayNetworking.registerGlobalReceiver(
ConfigS2CPayload.ID,
(payload, context) -> {
serverConfig = payload.config();
});
}
}
207 changes: 105 additions & 102 deletions src/client/java/com/padbro/greeterbro/client/JoinCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.padbro.greeterbro.GreeterBro;
import com.padbro.greeterbro.client.config.CacheClearType;
import com.padbro.greeterbro.client.config.ReturningPlayerConfig;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
Expand All @@ -19,121 +21,122 @@
import java.util.Optional;

public class JoinCache {
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private ArrayList<PlayerCacheEntry> joins;
private String date;
private static final File file = new File("join_cache.json");

public static JoinCache loadCache() {
String currentDate = getCurrentDate();
if (!file.exists()) {
return new JoinCache(currentDate, new ArrayList<>());
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private static final File file = new File("join_cache.json");
private ArrayList<PlayerCacheEntry> joins;
private String date;

public JoinCache(String date, ArrayList<PlayerCacheEntry> joins) {
this.joins = joins;
this.date = date;
}
try (FileReader reader = new FileReader(file)) {
JoinCache joinCache = gson.fromJson(reader, new TypeToken<>() {});
ReturningPlayerConfig config = GreeterBroClient.getConfig().returningPlayerConfig;

// Don`t clear the cache if cacheClearType is Never
// Clear the cache if it`s a new day or if cacheClearType is OnNewSession
if (config.cacheClearType != CacheClearType.Never
&& (!Objects.equals(joinCache.date, currentDate)
|| config.cacheClearType == CacheClearType.OnNewSession)) {
joinCache.joins = new ArrayList<>();
joinCache.date = currentDate;
writeToFile(joinCache);
}
return joinCache;
} catch (JsonSyntaxException exception) {
return new JoinCache("", new ArrayList<>());
} catch (Exception exception) {
throw new RuntimeException(
"Cannot load player cache (%s)".formatted(file.getAbsolutePath()), exception);

public static JoinCache loadCache() {
String currentDate = getCurrentDate();
if (!file.exists()) {
return new JoinCache(currentDate, new ArrayList<>());
}
try (FileReader reader = new FileReader(file)) {
JoinCache joinCache = gson.fromJson(reader, new TypeToken<>() {
});
ReturningPlayerConfig config = GreeterBroClient.getConfig().returningPlayerConfig;

// Don`t clear the cache if cacheClearType is Never
// Clear the cache if it`s a new day or if cacheClearType is OnNewSession
if (config.cacheClearType != CacheClearType.Never
&& (!Objects.equals(joinCache.date, currentDate)
|| config.cacheClearType == CacheClearType.OnNewSession)) {
joinCache.joins = new ArrayList<>();
joinCache.date = currentDate;
writeToFile(joinCache);
}
return joinCache;
} catch (JsonSyntaxException exception) {
return new JoinCache("", new ArrayList<>());
} catch (Exception exception) {
throw new RuntimeException(
"Cannot load player cache (%s)".formatted(file.getAbsolutePath()), exception);
}
}
}

public JoinCache(String date, ArrayList<PlayerCacheEntry> joins) {
this.joins = joins;
this.date = date;
}
private static String getCurrentDate() {
return Instant.now()
.atZone(ZoneId.systemDefault())
.toLocalDate()
.format(DateTimeFormatter.ISO_LOCAL_DATE);
}

public void add(String player) {
Optional<PlayerCacheEntry> playerCacheEntry = getPlayerCacheEntry(player);
private static void writeToFile(JoinCache joinCache) {
try (FileWriter writer = new FileWriter(file)) {
gson.toJson(joinCache, writer);
} catch (IOException e) {
GreeterBro.LOGGER.warn("Failed to save cache data to file");
}
}

if (playerCacheEntry.isEmpty()) {
this.joins.add(new PlayerCacheEntry(player, Instant.now()));
} else {
playerCacheEntry.get().setJoinedAt(Instant.now());
public void add(String player) {
Optional<PlayerCacheEntry> playerCacheEntry = getPlayerCacheEntry(player);

if (playerCacheEntry.isEmpty()) {
this.joins.add(new PlayerCacheEntry(player, Instant.now()));
} else {
playerCacheEntry.get().setJoinedAt(Instant.now());
}
writeToFile(this);
}
writeToFile(this);
}

public boolean hasRecentlyJoined(String player) {
Optional<PlayerCacheEntry> playerCacheEntry = this.getPlayerCacheEntry(player);
if (playerCacheEntry.isEmpty()) {
return false;
public boolean hasRecentlyJoined(String player) {
Optional<PlayerCacheEntry> playerCacheEntry = this.getPlayerCacheEntry(player);
if (playerCacheEntry.isEmpty()) {
return false;
}

Instant joinedAt = playerCacheEntry.get().getJoinedAt();

return joinedAt.isAfter(
Instant.now()
.minus(
GreeterBroClient.getConfig().returningPlayerConfig.ignoreForMin,
ChronoUnit.MINUTES));
}

Instant joinedAt = playerCacheEntry.get().getJoinedAt();

return joinedAt.isAfter(
Instant.now()
.minus(
GreeterBroClient.getConfig().returningPlayerConfig.ignoreForMin,
ChronoUnit.MINUTES));
}

public boolean hasJoined(String player) {
return this.getPlayerCacheEntry(player).isPresent();
}

public void clear() {
joins = new ArrayList<>();
writeToFile(this);
}

private static String getCurrentDate() {
return Instant.now()
.atZone(ZoneId.systemDefault())
.toLocalDate()
.format(DateTimeFormatter.ISO_LOCAL_DATE);
}

private Optional<PlayerCacheEntry> getPlayerCacheEntry(String player) {
return this.joins.stream()
.filter(playerCacheEntry -> player.equals(playerCacheEntry.name))
.findFirst();
}

private static void writeToFile(JoinCache joinCache) {
try (FileWriter writer = new FileWriter(file)) {
gson.toJson(joinCache, writer);
} catch (IOException e) {
GreeterBroClient.LOGGER.warn("Failed to save cache data to file");
public boolean hasJoined(String player) {
return this.getPlayerCacheEntry(player).isPresent();
}
}

public boolean shouldClearOnJoin() {
ReturningPlayerConfig config = GreeterBroClient.getConfig().returningPlayerConfig;
return config.cacheClearType == CacheClearType.OnJoin ||
(config.cacheClearType == CacheClearType.OnNewDay &&
!Objects.equals(this.date, getCurrentDate()));
}

public static class PlayerCacheEntry {
private final String name;
private long joinedAt;

public PlayerCacheEntry(String name, Instant joinedAt) {
this.name = name;
this.joinedAt = joinedAt.toEpochMilli();

public void clear() {
joins = new ArrayList<>();
writeToFile(this);
}

private Optional<PlayerCacheEntry> getPlayerCacheEntry(String player) {
return this.joins.stream()
.filter(playerCacheEntry -> player.equals(playerCacheEntry.name))
.findFirst();
}

public Instant getJoinedAt() {
return Instant.ofEpochMilli(joinedAt);
public boolean shouldClearOnJoin() {
ReturningPlayerConfig config = GreeterBroClient.getConfig().returningPlayerConfig;
return config.cacheClearType == CacheClearType.OnJoin ||
(config.cacheClearType == CacheClearType.OnNewDay &&
!Objects.equals(this.date, getCurrentDate()));
}

public void setJoinedAt(Instant instant) {
this.joinedAt = instant.toEpochMilli();
public static class PlayerCacheEntry {
private final String name;
private long joinedAt;

public PlayerCacheEntry(String name, Instant joinedAt) {
this.name = name;
this.joinedAt = joinedAt.toEpochMilli();
}

public Instant getJoinedAt() {
return Instant.ofEpochMilli(joinedAt);
}

public void setJoinedAt(Instant instant) {
this.joinedAt = instant.toEpochMilli();
}
}
}
}
Loading