-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
141 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 120 additions & 1 deletion
121
...n/src/main/java/fr/hyriode/api/impl/common/player/model/modules/HyriPlayerHostModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters