refactor: use Guava methods in various places

feat/data-edit-commands
William278 1 year ago
parent 530b3ef24d
commit 2e3db2fffa

@ -250,21 +250,13 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync, BukkitTask.S
this.dataSyncer = dataSyncer; this.dataSyncer = dataSyncer;
} }
@NotNull
@Override
@SuppressWarnings("unchecked")
public Map<Identifier, Serializer<? extends Data>> getSerializers() {
return serializers;
}
@NotNull @NotNull
@Override @Override
public Map<Identifier, Data> getPlayerCustomDataStore(@NotNull OnlineUser user) { public Map<Identifier, Data> getPlayerCustomDataStore(@NotNull OnlineUser user) {
if (playerCustomDataStore.containsKey(user.getUuid())) { if (playerCustomDataStore.containsKey(user.getUuid())) {
return playerCustomDataStore.get(user.getUuid()); return playerCustomDataStore.get(user.getUuid());
} }
final Map<Identifier, Data> data = new HashMap<>(); final Map<Identifier, Data> data = Maps.newHashMap();
playerCustomDataStore.put(user.getUuid(), data); playerCustomDataStore.put(user.getUuid(), data);
return data; return data;
} }

@ -19,6 +19,8 @@
package net.william278.husksync.data; package net.william278.husksync.data;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import de.tr7zw.changeme.nbtapi.NBTCompound; import de.tr7zw.changeme.nbtapi.NBTCompound;
import de.tr7zw.changeme.nbtapi.NBTPersistentDataContainer; import de.tr7zw.changeme.nbtapi.NBTPersistentDataContainer;
@ -313,10 +315,10 @@ public abstract class BukkitData implements Data {
// Iterate through the server advancement set and add all advancements to the list // Iterate through the server advancement set and add all advancements to the list
@NotNull @NotNull
public static BukkitData.Advancements adapt(@NotNull Player player) { public static BukkitData.Advancements adapt(@NotNull Player player) {
final List<Advancement> advancements = new ArrayList<>(); final List<Advancement> advancements = Lists.newArrayList();
forEachAdvancement(advancement -> { forEachAdvancement(advancement -> {
final AdvancementProgress advancementProgress = player.getAdvancementProgress(advancement); final AdvancementProgress advancementProgress = player.getAdvancementProgress(advancement);
final Map<String, Date> awardedCriteria = new HashMap<>(); final Map<String, Date> awardedCriteria = Maps.newHashMap();
advancementProgress.getAwardedCriteria().forEach(criteriaKey -> awardedCriteria.put(criteriaKey, advancementProgress.getAwardedCriteria().forEach(criteriaKey -> awardedCriteria.put(criteriaKey,
advancementProgress.getDateAwarded(criteriaKey))); advancementProgress.getDateAwarded(criteriaKey)));

@ -19,6 +19,8 @@
package net.william278.husksync.migrator; package net.william278.husksync.migrator;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import me.william278.husksync.bukkit.data.DataSerializer; import me.william278.husksync.bukkit.data.DataSerializer;
import net.william278.hslmigrator.HSLConverter; import net.william278.hslmigrator.HSLConverter;
@ -91,7 +93,7 @@ public class LegacyMigrator extends Migrator {
connectionPool.setPoolName((getIdentifier() + "_migrator_pool").toUpperCase(Locale.ENGLISH)); connectionPool.setPoolName((getIdentifier() + "_migrator_pool").toUpperCase(Locale.ENGLISH));
plugin.log(Level.INFO, "Downloading raw data from the legacy database (this might take a while)..."); plugin.log(Level.INFO, "Downloading raw data from the legacy database (this might take a while)...");
final List<LegacyData> dataToMigrate = new ArrayList<>(); final List<LegacyData> dataToMigrate = Lists.newArrayList();
try (final Connection connection = connectionPool.getConnection()) { try (final Connection connection = connectionPool.getConnection()) {
try (final PreparedStatement statement = connection.prepareStatement(""" try (final PreparedStatement statement = connection.prepareStatement("""
SELECT `uuid`, `username`, `inventory`, `ender_chest`, `health`, `max_health`, `health_scale`, `hunger`, `saturation`, `saturation_exhaustion`, `selected_slot`, `status_effects`, `total_experience`, `exp_level`, `exp_progress`, `game_mode`, `statistics`, `is_flying`, `advancements`, `location` SELECT `uuid`, `username`, `inventory`, `ender_chest`, `health`, `max_health`, `health_scale`, `hunger`, `saturation`, `saturation_exhaustion`, `selected_slot`, `status_effects`, `total_experience`, `exp_level`, `exp_progress`, `game_mode`, `statistics`, `is_flying`, `advancements`, `location`
@ -342,7 +344,7 @@ public class LegacyMigrator extends Migrator {
} }
private Map<String, Integer> convertStatisticMap(@NotNull HashMap<Statistic, Integer> rawMap) { private Map<String, Integer> convertStatisticMap(@NotNull HashMap<Statistic, Integer> rawMap) {
final HashMap<String, Integer> convertedMap = new HashMap<>(); final HashMap<String, Integer> convertedMap = Maps.newHashMap();
for (Map.Entry<Statistic, Integer> entry : rawMap.entrySet()) { for (Map.Entry<Statistic, Integer> entry : rawMap.entrySet()) {
convertedMap.put(entry.getKey().getKey().toString(), entry.getValue()); convertedMap.put(entry.getKey().getKey().toString(), entry.getValue());
} }
@ -350,7 +352,7 @@ public class LegacyMigrator extends Migrator {
} }
private Map<String, Map<String, Integer>> convertMaterialStatisticMap(@NotNull HashMap<Statistic, HashMap<Material, Integer>> rawMap) { private Map<String, Map<String, Integer>> convertMaterialStatisticMap(@NotNull HashMap<Statistic, HashMap<Material, Integer>> rawMap) {
final Map<String, Map<String, Integer>> convertedMap = new HashMap<>(); final Map<String, Map<String, Integer>> convertedMap = Maps.newHashMap();
for (Map.Entry<Statistic, HashMap<Material, Integer>> entry : rawMap.entrySet()) { for (Map.Entry<Statistic, HashMap<Material, Integer>> entry : rawMap.entrySet()) {
for (Map.Entry<Material, Integer> materialEntry : entry.getValue().entrySet()) { for (Map.Entry<Material, Integer> materialEntry : entry.getValue().entrySet()) {
convertedMap.computeIfAbsent(entry.getKey().getKey().toString(), k -> new HashMap<>()) convertedMap.computeIfAbsent(entry.getKey().getKey().toString(), k -> new HashMap<>())
@ -361,7 +363,7 @@ public class LegacyMigrator extends Migrator {
} }
private Map<String, Map<String, Integer>> convertEntityStatisticMap(@NotNull HashMap<Statistic, HashMap<EntityType, Integer>> rawMap) { private Map<String, Map<String, Integer>> convertEntityStatisticMap(@NotNull HashMap<Statistic, HashMap<EntityType, Integer>> rawMap) {
final Map<String, Map<String, Integer>> convertedMap = new HashMap<>(); final Map<String, Map<String, Integer>> convertedMap = Maps.newHashMap();
for (Map.Entry<Statistic, HashMap<EntityType, Integer>> entry : rawMap.entrySet()) { for (Map.Entry<Statistic, HashMap<EntityType, Integer>> entry : rawMap.entrySet()) {
for (Map.Entry<EntityType, Integer> materialEntry : entry.getValue().entrySet()) { for (Map.Entry<EntityType, Integer> materialEntry : entry.getValue().entrySet()) {
convertedMap.computeIfAbsent(entry.getKey().getKey().toString(), k -> new HashMap<>()) convertedMap.computeIfAbsent(entry.getKey().getKey().toString(), k -> new HashMap<>())

@ -19,6 +19,7 @@
package net.william278.husksync.migrator; package net.william278.husksync.migrator;
import com.google.common.collect.Lists;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import net.william278.husksync.BukkitHuskSync; import net.william278.husksync.BukkitHuskSync;
import net.william278.husksync.HuskSync; import net.william278.husksync.HuskSync;
@ -98,7 +99,7 @@ public class MpdbMigrator extends Migrator {
connectionPool.setPoolName((getIdentifier() + "_migrator_pool").toUpperCase(Locale.ENGLISH)); connectionPool.setPoolName((getIdentifier() + "_migrator_pool").toUpperCase(Locale.ENGLISH));
plugin.log(Level.INFO, "Downloading raw data from the MySQLPlayerDataBridge database (this might take a while)..."); plugin.log(Level.INFO, "Downloading raw data from the MySQLPlayerDataBridge database (this might take a while)...");
final List<MpdbData> dataToMigrate = new ArrayList<>(); final List<MpdbData> dataToMigrate = Lists.newArrayList();
try (final Connection connection = connectionPool.getConnection()) { try (final Connection connection = connectionPool.getConnection()) {
try (final PreparedStatement statement = connection.prepareStatement(""" try (final PreparedStatement statement = connection.prepareStatement("""
SELECT `%source_inventory_table%`.`player_uuid`, `%source_inventory_table%`.`player_name`, `inventory`, `armor`, `enderchest`, `exp_lvl`, `exp`, `total_exp` SELECT `%source_inventory_table%`.`player_uuid`, `%source_inventory_table%`.`player_name`, `inventory`, `armor`, `enderchest`, `exp_lvl`, `exp`, `total_exp`

@ -19,6 +19,7 @@
package net.william278.husksync.util; package net.william278.husksync.util;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import net.william278.husksync.HuskSync; import net.william278.husksync.HuskSync;
import net.william278.husksync.adapter.DataAdapter; import net.william278.husksync.adapter.DataAdapter;
@ -54,7 +55,7 @@ public class BukkitLegacyConverter extends LegacyConverter {
@NotNull @NotNull
@Override @Override
public DataSnapshot.Packed convert(@NotNull byte[] data, @NotNull UUID id, public DataSnapshot.Packed convert(byte[] data, @NotNull UUID id,
@NotNull OffsetDateTime timestamp) throws DataAdapter.AdaptionException { @NotNull OffsetDateTime timestamp) throws DataAdapter.AdaptionException {
final JSONObject object = new JSONObject(plugin.getDataAdapter().bytesToString(data)); final JSONObject object = new JSONObject(plugin.getDataAdapter().bytesToString(data));
final int version = object.getInt("format_version"); final int version = object.getInt("format_version");
@ -82,7 +83,7 @@ public class BukkitLegacyConverter extends LegacyConverter {
} }
final JSONObject status = object.getJSONObject("status_data"); final JSONObject status = object.getJSONObject("status_data");
final HashMap<Identifier, Data> containers = new HashMap<>(); final HashMap<Identifier, Data> containers = Maps.newHashMap();
if (shouldImport(Identifier.HEALTH)) { if (shouldImport(Identifier.HEALTH)) {
containers.put(Identifier.HEALTH, BukkitData.Health.from( containers.put(Identifier.HEALTH, BukkitData.Health.from(
status.getDouble("health"), status.getDouble("health"),
@ -166,7 +167,7 @@ public class BukkitLegacyConverter extends LegacyConverter {
} }
final JSONArray advancements = object.getJSONArray("advancements"); final JSONArray advancements = object.getJSONArray("advancements");
final List<Data.Advancements.Advancement> converted = new ArrayList<>(); final List<Data.Advancements.Advancement> converted = Lists.newArrayList();
advancements.iterator().forEachRemaining(o -> { advancements.iterator().forEachRemaining(o -> {
final JSONObject advancement = (JSONObject) JSONObject.wrap(o); final JSONObject advancement = (JSONObject) JSONObject.wrap(o);
final String key = advancement.getString("key"); final String key = advancement.getString("key");
@ -213,7 +214,7 @@ public class BukkitLegacyConverter extends LegacyConverter {
final Map<Statistic, Map<EntityType, Integer>> entityStats = Maps.newHashMap(); final Map<Statistic, Map<EntityType, Integer>> entityStats = Maps.newHashMap();
entities.keys().forEachRemaining(stat -> { entities.keys().forEachRemaining(stat -> {
final JSONObject entityStat = entities.getJSONObject(stat); final JSONObject entityStat = entities.getJSONObject(stat);
final Map<EntityType, Integer> entityMap = new HashMap<>(); final Map<EntityType, Integer> entityMap = Maps.newHashMap();
entityStat.keys().forEachRemaining(entity -> entityMap.put(matchEntityType(entity), entityStat.getInt(entity))); entityStat.keys().forEachRemaining(entity -> entityMap.put(matchEntityType(entity), entityStat.getInt(entity)));
entityStats.put(matchStatistic(stat), entityMap); entityStats.put(matchStatistic(stat), entityMap);
}); });
@ -269,6 +270,7 @@ public class BukkitLegacyConverter extends LegacyConverter {
} }
// Deserialize a single legacy item stack // Deserialize a single legacy item stack
@SuppressWarnings("unchecked")
@Nullable @Nullable
private static ItemStack deserializeLegacyItemStack(@Nullable Object serializedItemStack) { private static ItemStack deserializeLegacyItemStack(@Nullable Object serializedItemStack) {
return serializedItemStack != null ? ItemStack.deserialize((Map<String, Object>) serializedItemStack) : null; return serializedItemStack != null ? ItemStack.deserialize((Map<String, Object>) serializedItemStack) : null;

@ -19,6 +19,7 @@
package net.william278.husksync.util; package net.william278.husksync.util;
import com.google.common.collect.Lists;
import de.tr7zw.changeme.nbtapi.NBT; import de.tr7zw.changeme.nbtapi.NBT;
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT; import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
import de.tr7zw.changeme.nbtapi.iface.ReadableNBT; import de.tr7zw.changeme.nbtapi.iface.ReadableNBT;
@ -416,7 +417,7 @@ public interface BukkitMapPersister {
*/ */
@NotNull @NotNull
private MapData extractMapData() { private MapData extractMapData() {
final List<MapBanner> banners = new ArrayList<>(); final List<MapBanner> banners = Lists.newArrayList();
final String BANNER_PREFIX = "banner_"; final String BANNER_PREFIX = "banner_";
for (int i = 0; i < getCursors().size(); i++) { for (int i = 0; i < getCursors().size(); i++) {
final MapCursor cursor = getCursors().getCursor(i); final MapCursor cursor = getCursors().getCursor(i);

@ -27,9 +27,10 @@ dependencies {
compileOnly "org.mariadb.jdbc:mariadb-java-client:$mariadb_driver_version" compileOnly "org.mariadb.jdbc:mariadb-java-client:$mariadb_driver_version"
compileOnly "org.xerial.snappy:snappy-java:$snappy_version" compileOnly "org.xerial.snappy:snappy-java:$snappy_version"
testImplementation 'com.github.plan-player-analytics:Plan:5.5.2272'
testImplementation "redis.clients:jedis:$jedis_version" testImplementation "redis.clients:jedis:$jedis_version"
testImplementation "org.xerial.snappy:snappy-java:$snappy_version" testImplementation "org.xerial.snappy:snappy-java:$snappy_version"
testImplementation 'com.google.guava:guava:33.0.0-jre'
testImplementation 'com.github.plan-player-analytics:Plan:5.5.2272'
testCompileOnly 'com.github.Exlll.ConfigLib:configlib-yaml:v4.3.0' testCompileOnly 'com.github.Exlll.ConfigLib:configlib-yaml:v4.3.0'
testCompileOnly 'org.jetbrains:annotations:24.1.0' testCompileOnly 'org.jetbrains:annotations:24.1.0'

@ -19,11 +19,11 @@
package net.william278.husksync.command; package net.william278.husksync.command;
import com.google.common.collect.Maps;
import net.william278.husksync.HuskSync; import net.william278.husksync.HuskSync;
import net.william278.husksync.user.CommandUser; import net.william278.husksync.user.CommandUser;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -36,7 +36,7 @@ public abstract class Command extends Node {
@NotNull HuskSync plugin) { @NotNull HuskSync plugin) {
super(name, aliases, plugin); super(name, aliases, plugin);
this.usage = usage; this.usage = usage;
this.additionalPermissions = new HashMap<>(); this.additionalPermissions = Maps.newHashMap();
} }
@Override @Override

@ -19,6 +19,7 @@
package net.william278.husksync.config; package net.william278.husksync.config;
import com.google.common.collect.Maps;
import de.exlll.configlib.Configuration; import de.exlll.configlib.Configuration;
import de.themoep.minedown.adventure.MineDown; import de.themoep.minedown.adventure.MineDown;
import lombok.AccessLevel; import lombok.AccessLevel;
@ -30,7 +31,6 @@ import org.jetbrains.annotations.NotNull;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.TreeMap;
/** /**
* Plugin locale configuration * Plugin locale configuration
@ -54,7 +54,7 @@ public class Locales {
protected static final String DEFAULT_LOCALE = "en-gb"; protected static final String DEFAULT_LOCALE = "en-gb";
// The raw set of locales loaded from yaml // The raw set of locales loaded from yaml
Map<String, String> locales = new TreeMap<>(); Map<String, String> locales = Maps.newTreeMap();
/** /**
* Returns a raw, un-formatted locale loaded from the locales file * Returns a raw, un-formatted locale loaded from the locales file

@ -19,6 +19,7 @@
package net.william278.husksync.config; package net.william278.husksync.config;
import com.google.common.collect.Lists;
import de.exlll.configlib.Comment; import de.exlll.configlib.Comment;
import de.exlll.configlib.Configuration; import de.exlll.configlib.Configuration;
import lombok.AccessLevel; import lombok.AccessLevel;
@ -162,7 +163,7 @@ public class Settings {
@Comment("The master set name for the Redis sentinel.") @Comment("The master set name for the Redis sentinel.")
private String master = ""; private String master = "";
@Comment("List of host:port pairs") @Comment("List of host:port pairs")
private List<String> nodes = new ArrayList<>(); private List<String> nodes = Lists.newArrayList();
private String password = ""; private String password = "";
} }

@ -19,6 +19,7 @@
package net.william278.husksync.data; package net.william278.husksync.data;
import com.google.common.collect.Maps;
import com.google.gson.annotations.Expose; import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import de.themoep.minedown.adventure.MineDown; import de.themoep.minedown.adventure.MineDown;
@ -426,7 +427,7 @@ public class DataSnapshot {
private Builder(@NotNull HuskSync plugin) { private Builder(@NotNull HuskSync plugin) {
this.plugin = plugin; this.plugin = plugin;
this.pinned = false; this.pinned = false;
this.data = new HashMap<>(); this.data = Maps.newHashMap();
this.timestamp = OffsetDateTime.now(); this.timestamp = OffsetDateTime.now();
this.id = UUID.randomUUID(); this.id = UUID.randomUUID();
this.serverName = plugin.getServerName(); this.serverName = plugin.getServerName();

@ -19,6 +19,7 @@
package net.william278.husksync.database; package net.william278.husksync.database;
import com.google.common.collect.Lists;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import net.william278.husksync.HuskSync; import net.william278.husksync.HuskSync;
import net.william278.husksync.adapter.DataAdapter; import net.william278.husksync.adapter.DataAdapter;
@ -250,7 +251,7 @@ public class MySqlDatabase extends Database {
@Override @Override
@NotNull @NotNull
public List<DataSnapshot.Packed> getAllSnapshots(@NotNull User user) { public List<DataSnapshot.Packed> getAllSnapshots(@NotNull User user) {
final List<DataSnapshot.Packed> retrievedData = new ArrayList<>(); final List<DataSnapshot.Packed> retrievedData = Lists.newArrayList();
try (Connection connection = getConnection()) { try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
SELECT `version_uuid`, `timestamp`, `data` SELECT `version_uuid`, `timestamp`, `data`

Loading…
Cancel
Save