Skip to content

Commit

Permalink
Implements player hosts module
Browse files Browse the repository at this point in the history
  • Loading branch information
AstFaster committed Feb 20, 2023
1 parent a98b3e5 commit d2b5bd9
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 4 deletions.
7 changes: 7 additions & 0 deletions API/src/main/java/fr/hyriode/api/player/IHyriPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ public interface IHyriPlayer {
*/
@NotNull IHyriAuthModule getAuth();

/**
* Get the hosts module of the player
*
* @return The {@link IHyriPlayerHostModule} instance
*/
@NotNull IHyriPlayerHostModule getHosts();

/**
* Get the statistics handler of the player
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public class HyriPlayer implements IHyriPlayer, MongoSerializable, DataSerializa
@Expose
private final HyriFriendsModule friends = new HyriFriendsModule(this);
@Expose
private final HyriPlayerHostModule hosts = new HyriPlayerHostModule();
@Expose
private final HyriAuthModule auth = new HyriAuthModule();
@Expose
private final HyriStatisticsModule statistics = new HyriStatisticsModule();
Expand Down Expand Up @@ -111,6 +113,7 @@ public void save(MongoDocument document) {
this.friends.save(document);

document.append("auth", MongoSerializer.serialize(this.auth));
document.append("hosts", MongoSerializer.serialize(this.hosts));
document.append("statistics", MongoSerializer.serialize(this.statistics));

this.data.save(document);
Expand Down Expand Up @@ -140,6 +143,7 @@ public void load(MongoDocument document) {

this.friends.load(document);
this.auth.load(MongoDocument.of(document.get("auth", Document.class)));
this.hosts.load(MongoDocument.of(document.get("hosts", Document.class)));
this.statistics.load(MongoDocument.of(document.get("statistics", Document.class)));
this.data.load(document);
this.transactions.load(MongoDocument.of(document.get("transactions", Document.class)));
Expand Down Expand Up @@ -171,7 +175,8 @@ public void write(ObjectDataOutput output) throws IOException {
output.writeString(this.guild == null ? null : this.guild.toHexString());

this.auth.write(output);
this.settings.write(output);
this.hosts.write(output);
this.statistics.write(output);
this.data.write(output);
this.transactions.write(output);
}
Expand Down Expand Up @@ -205,7 +210,8 @@ public void read(ObjectDataInput input) throws IOException {
this.guild = guildId == null ? null : new ObjectId(guildId);

this.auth.read(input);
this.settings.read(input);
this.hosts.read(input);
this.statistics.read(input);
this.data.read(input);
this.transactions.read(input);
}
Expand Down Expand Up @@ -349,6 +355,11 @@ public void setGuild(@NotNull ObjectId guild) {
return this.auth;
}

@Override
public @NotNull IHyriPlayerHostModule getHosts() {
return this.hosts;
}

@Override
public @NotNull IHyriStatisticsModule getStatistics() {
return this.statistics;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,127 @@
package fr.hyriode.api.impl.common.player.model.modules;

import fr.hyriode.api.mongodb.MongoDocument;
import fr.hyriode.api.mongodb.MongoSerializable;
import fr.hyriode.api.player.model.modules.IHyriPlayerHostModule;
import fr.hyriode.api.serialization.DataSerializable;
import fr.hyriode.api.serialization.ObjectDataInput;
import fr.hyriode.api.serialization.ObjectDataOutput;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
* Created by AstFaster
* on 17/02/2023 at 15:22
*/
public class HyriPlayerHostModule {
public class HyriPlayerHostModule implements IHyriPlayerHostModule, MongoSerializable, DataSerializable {

private int availableHosts;
private final Set<UUID> bannedPlayers = new HashSet<>();
private final Set<String> favoriteConfigs = new HashSet<>();

@Override
public void save(MongoDocument document) {
document.append("available", this.availableHosts);
document.append("banned_players", this.bannedPlayers.stream().map(UUID::toString).collect(Collectors.toList()));
document.append("favorite_configs", this.favoriteConfigs);
}

@Override
public void load(MongoDocument document) {
this.availableHosts = document.getInteger("available");
document.getList("banned_players", String.class).forEach(uuid -> this.bannedPlayers.add(UUID.fromString(uuid)));
this.favoriteConfigs.addAll(document.getList("favorite_configs", String.class));
}

@Override
public void write(ObjectDataOutput output) throws IOException {
output.writeInt(this.availableHosts);
output.writeInt(this.bannedPlayers.size());

for (UUID playerId : this.bannedPlayers) {
output.writeUUID(playerId);
}

output.writeInt(this.favoriteConfigs.size());

for (String config : this.favoriteConfigs) {
output.writeString(config);
}
}

@Override
public void read(ObjectDataInput input) throws IOException {
this.availableHosts = input.readInt();

final int playersSize = input.readInt();

for (int i = 0; i < playersSize; i++) {
this.bannedPlayers.add(input.readUUID());
}

final int configsSize = input.readInt();

for (int i = 0; i < configsSize; i++) {
this.favoriteConfigs.add(input.readString());
}
}

@Override
public int getAvailableHosts() {
return this.availableHosts;
}

@Override
public void addAvailableHosts(int hosts) {
this.availableHosts += hosts;
}

@Override
public void setAvailableHosts(int hosts) {
this.availableHosts = hosts;
}

@Override
public @NotNull Set<UUID> getBannedPlayers() {
return this.bannedPlayers;
}

@Override
public void addBannedPlayer(@NotNull UUID playerId) {
this.bannedPlayers.add(playerId);
}

@Override
public void removeBannedPlayer(@NotNull UUID playerId) {
this.bannedPlayers.remove(playerId);
}

@Override
public boolean hasBannedPlayer(@NotNull UUID playerId) {
return this.bannedPlayers.contains(playerId);
}

@Override
public @NotNull Set<String> getFavoriteConfigs() {
return this.favoriteConfigs;
}

@Override
public void addFavoriteConfig(@NotNull String config) {
this.favoriteConfigs.add(config);
}

@Override
public void removeFavoriteConfig(@NotNull String config) {
this.favoriteConfigs.remove(config);
}

@Override
public boolean hasFavoriteConfig(@NotNull String config) {
return this.favoriteConfigs.contains(config);
}

}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ allprojects {
apply plugin: 'maven-publish'

group = 'fr.hyriode'
version = '1.5.0'
version = '1.5.1'

java {
withSourcesJar()
Expand Down

0 comments on commit d2b5bd9

Please sign in to comment.