fix NPE and codec issues

This commit is contained in:
Taah 2022-05-28 19:12:18 -07:00
parent d0b1d09f38
commit 9f19bd14ca
5 changed files with 74 additions and 24 deletions

View file

@ -37,6 +37,7 @@ import dev.plex.world.CustomWorld;
import java.io.File;
import lombok.Getter;
import lombok.Setter;
import org.bson.conversions.Bson;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;

View file

@ -7,8 +7,10 @@ import dev.plex.storage.StorageType;
import dev.plex.util.PermissionsUtil;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import java.util.Arrays;
import java.util.List;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
@ -34,8 +36,7 @@ public class PlayerListener<T> extends PlexListener
{
player.setOp(true);
PlexLog.debug("Automatically opped " + player.getName() + " since ranks are enabled.");
}
else if (plugin.getSystem().equalsIgnoreCase("permissions"))
} else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{
player.setOp(false);
PlexLog.debug("Automatically deopped " + player.getName() + " since ranks are disabled.");
@ -48,8 +49,7 @@ public class PlayerListener<T> extends PlexListener
plexPlayer.setName(player.getName()); // set the name of the player
plexPlayer.setIps(Arrays.asList(player.getAddress().getAddress().getHostAddress().trim())); // set the arraylist of ips
DataUtils.insert(plexPlayer); // insert data in some wack db
}
else
} else
{
plexPlayer = DataUtils.getPlayer(player.getUniqueId());
List<String> ips = plexPlayer.getIps();
@ -92,15 +92,18 @@ public class PlayerListener<T> extends PlexListener
plexPlayer.loadPunishments();
}
plugin.getSqlNotes().getNotes(plexPlayer.getUuid()).whenComplete((notes, ex) ->
if (plugin.getStorageType() != StorageType.MONGODB)
{
String plural = notes.size() == 1 ? "note." : "notes.";
if (!notes.isEmpty())
plugin.getSqlNotes().getNotes(plexPlayer.getUuid()).whenComplete((notes, ex) ->
{
PlexUtils.broadcastToAdmins(Component.text(plexPlayer.getName() + " has " + notes.size() + " " + plural).color(NamedTextColor.GOLD));
PlexUtils.broadcastToAdmins(Component.text("Click to view their " + plural).clickEvent(ClickEvent.runCommand("/notes " + plexPlayer.getName() + " list")).color(NamedTextColor.GOLD));
}
});
String plural = notes.size() == 1 ? "note." : "notes.";
if (!notes.isEmpty())
{
PlexUtils.broadcastToAdmins(Component.text(plexPlayer.getName() + " has " + notes.size() + " " + plural).color(NamedTextColor.GOLD));
PlexUtils.broadcastToAdmins(Component.text("Click to view their " + plural).clickEvent(ClickEvent.runCommand("/notes " + plexPlayer.getName() + " list")).color(NamedTextColor.GOLD));
}
});
}
}
// saving the player's data

View file

@ -11,6 +11,7 @@ import dev.plex.storage.StorageType;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import dev.plex.util.TimeUtils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@ -22,6 +23,7 @@ import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import lombok.Data;
import lombok.Getter;
import org.apache.commons.io.FileUtils;
@ -105,8 +107,7 @@ public class PunishmentManager implements PlexBase
{
DataUtils.update(plexPlayer);
});
}
else
} else
{
Plex.get().getSqlPunishment().insertPunishment(punishment);
}
@ -117,8 +118,7 @@ public class PunishmentManager implements PlexBase
try
{
return !FileUtils.readFileToString(file, StandardCharsets.UTF_8).trim().isEmpty();
}
catch (IOException e)
} catch (IOException e)
{
e.printStackTrace();
}
@ -129,6 +129,11 @@ public class PunishmentManager implements PlexBase
{
return CompletableFuture.supplyAsync(() ->
{
if (!DataUtils.hasPlayedBefore(uuid))
{
return false;
}
PlexPlayer player = DataUtils.getPlayer(uuid);
player.loadPunishments();
return player.getPunishments().stream().anyMatch(punishment -> (punishment.getType() == PunishmentType.BAN || punishment.getType() == PunishmentType.TEMPBAN) && punishment.isActive());
@ -138,10 +143,10 @@ public class PunishmentManager implements PlexBase
public boolean isBanned(UUID uuid)
{
// TODO: If a person is using MongoDB, this will error out because it is checking for bans on a player that doesn't exist yet
/*if (!DataUtils.hasPlayedBefore(uuid))
if (!DataUtils.hasPlayedBefore(uuid))
{
return false;
}*/
}
return DataUtils.getPlayer(uuid).getPunishments().stream().anyMatch(punishment -> (punishment.getType() == PunishmentType.BAN || punishment.getType() == PunishmentType.TEMPBAN) && punishment.isActive());
}
@ -159,8 +164,7 @@ public class PunishmentManager implements PlexBase
List<PlexPlayer> players = Plex.get().getMongoPlayerData().getPlayers();
return players.stream().map(PlexPlayer::getPunishments).flatMap(Collection::stream).filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN || punishment.getType() == PunishmentType.TEMPBAN).toList();
});
}
else
} else
{
//PlexLog.debug("Checking active bans mysql");
CompletableFuture<List<Punishment>> future = new CompletableFuture<>();
@ -191,8 +195,7 @@ public class PunishmentManager implements PlexBase
.peek(punishment -> punishment.setActive(false)).collect(Collectors.toList()));
DataUtils.update(plexPlayer);
});
}
else
} else
{
return Plex.get().getSqlPunishment().removeBan(uuid);
}
@ -220,8 +223,7 @@ public class PunishmentManager implements PlexBase
Bukkit.broadcast(PlexUtils.messageComponent("unfrozePlayer", "Plex", Bukkit.getOfflinePlayer(player.getUuid()).getName()));
}
}.runTaskLater(Plex.get(), 20 * seconds);
}
else if (punishment.getType() == PunishmentType.MUTE)
} else if (punishment.getType() == PunishmentType.MUTE)
{
player.setMuted(true);
ZonedDateTime now = ZonedDateTime.now(ZoneId.of(TimeUtils.TIMEZONE));

View file

@ -1,5 +1,8 @@
package dev.plex.storage;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import dev.morphia.Datastore;
@ -7,7 +10,9 @@ import dev.morphia.Morphia;
import dev.morphia.mapping.MapperOptions;
import dev.plex.PlexBase;
import dev.plex.player.PlexPlayer;
import dev.plex.storage.codec.ZonedDateTimeCodec;
import dev.plex.util.PlexLog;
import org.bson.codecs.configuration.CodecRegistries;
public class MongoConnection implements PlexBase
{
@ -42,7 +47,7 @@ public class MongoConnection implements PlexBase
connectionString = "mongodb://" + host + ":" + port + "/?uuidRepresentation=STANDARD";
}
PlexLog.debug("Using mongo connection string: " + connectionString);
MongoClient client = MongoClients.create(connectionString);
MongoClient client = MongoClients.create(MongoClientSettings.builder().codecRegistry(CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), CodecRegistries.fromCodecs(new ZonedDateTimeCodec()))).applyConnectionString(new ConnectionString(connectionString)).build());
Datastore datastore = Morphia.createDatastore(client, database == null ? "admin" : database, MapperOptions.DEFAULT);
datastore.getMapper().map(PlexPlayer.class);
datastore.ensureIndexes();

View file

@ -0,0 +1,39 @@
package dev.plex.storage.codec;
import org.bson.BsonReader;
import org.bson.BsonWriter;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import static dev.plex.util.TimeUtils.TIMEZONE;
/**
* @author Taah
* @project Plex
* @since 6:53 PM [28-05-2022]
*/
public class ZonedDateTimeCodec implements Codec<ZonedDateTime>
{
@Override
public ZonedDateTime decode(BsonReader reader, DecoderContext decoderContext)
{
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(reader.readDateTime()), ZoneId.of(TIMEZONE));
}
@Override
public void encode(BsonWriter writer, ZonedDateTime value, EncoderContext encoderContext)
{
writer.writeDateTime(value.toInstant().toEpochMilli());
}
@Override
public Class<ZonedDateTime> getEncoderClass()
{
return ZonedDateTime.class;
}
}