Skip to content

Commit

Permalink
📝 FIxs
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxlego08 committed Apr 4, 2024
1 parent 0cb281b commit 73d2cc2
Showing 1 changed file with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import fr.maxlego08.essentials.api.EssentialsPlugin;
import fr.maxlego08.essentials.api.database.dto.CooldownDTO;
import fr.maxlego08.essentials.api.database.dto.OptionDTO;
import fr.maxlego08.essentials.api.user.Option;
import fr.maxlego08.essentials.api.user.User;
import fr.maxlego08.essentials.user.ZUser;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

public class UserTypeAdapter extends TypeAdapter<User> {

Expand Down Expand Up @@ -54,38 +58,56 @@ public void write(JsonWriter out, User value) throws IOException {
@Override
public User read(JsonReader in) throws IOException {
String name = null;
User user = new ZUser(this.plugin, UUID.randomUUID());
UUID uniqueId = null; // Temporary storage for the UUID
Map<Option, Boolean> options = new HashMap<>();
Map<String, Long> cooldowns = new HashMap<>();
Map<String, BigDecimal> balances = new HashMap<>();

in.beginObject();
while (in.hasNext()) {
switch (in.nextName()) {
case "uniqueId" -> new ZUser(this.plugin, UUID.fromString(in.nextString()));
case "uniqueId" -> uniqueId = UUID.fromString(in.nextString());
case "name" -> name = in.nextString();
case "options" -> {
in.beginObject();
while (in.hasNext()) user.setOption(Option.valueOf(in.nextName()), in.nextBoolean());
while (in.hasNext()) {
options.put(Option.valueOf(in.nextName()), in.nextBoolean());
}
in.endObject();
}
case "cooldowns" -> {
in.beginObject();
while (in.hasNext()) user.setCooldown(in.nextName(), in.nextLong());
while (in.hasNext()) {
cooldowns.put(in.nextName(), in.nextLong());
}
in.endObject();
}
case "balances" -> {
in.beginObject();
while (in.hasNext()) {
String key = in.nextName();
BigDecimal value = new BigDecimal(in.nextString());
user.setBalance(key, value);
balances.put(key, value);
}
in.endObject();
}

}
}
in.endObject();

// Ensure that uniqueId is not null before creating a ZUser
if (uniqueId == null) {
throw new IOException("UniqueId is missing from the JSON input.");
}
User user = new ZUser(this.plugin, uniqueId); // Create the ZUser here

// Now, set the other properties
user.setName(name);
user.setOptions(options.entrySet().stream().map(e -> new OptionDTO(e.getKey(), e.getValue())).collect(Collectors.toList()));
user.setCooldowns(cooldowns.entrySet().stream().map(e -> new CooldownDTO(e.getKey(), e.getValue())).collect(Collectors.toList()));
balances.forEach(user::setBalance);

return user;
}

}

0 comments on commit 73d2cc2

Please sign in to comment.