Skip to content

Commit

Permalink
remove Guava as much as possible as it is not needed anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
FireInstall committed Jan 29, 2024
1 parent efb6b67 commit cd3cf0f
Show file tree
Hide file tree
Showing 35 changed files with 165 additions and 266 deletions.
38 changes: 12 additions & 26 deletions Minigames/src/main/java/au/com/mineauz/minigames/Minigames.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
import au.com.mineauz.minigames.stats.MinigameStats;
import au.com.mineauz.minigames.stats.StatValueField;
import au.com.mineauz.minigames.stats.StoredGameStats;
import com.google.common.io.Closeables;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.ListenableFuture;
import io.papermc.lib.PaperLib;
import net.milkbowl.vault.economy.Economy;
import org.bstats.bukkit.Metrics;
Expand All @@ -40,13 +37,13 @@
import org.bukkit.plugin.java.JavaPluginLoader;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -405,15 +402,11 @@ public Economy getEconomy() {
}

private int checkVersion() {
final InputStream stream = this.getResource("minigame.properties");
final Properties p = new Properties();
try {
try (final InputStream stream = this.getResource("minigame.properties")) {
p.load(stream);
} catch (final NullPointerException | IOException e) {
this.getLogger().warning(e.getMessage());
} finally {
//noinspection UnstableApiUsage
Closeables.closeQuietly(stream);
}

if (p.containsKey("version")) {
Expand Down Expand Up @@ -507,27 +500,20 @@ public FileConfiguration getLang() {
public void queueStatSave(final StoredGameStats saveData, final boolean winner) {
MinigameUtils.debugMessage("Scheduling SQL data save for " + saveData);

final ListenableFuture<Long> winCountFuture = this.backend.loadSingleStat(saveData.getMinigame(), MinigameStats.Wins, StatValueField.Total, saveData.getPlayer().getUUID());
final CompletableFuture<Long> winCountFuture = this.backend.loadSingleStat(saveData.getMinigame(), MinigameStats.Wins, StatValueField.Total, saveData.getPlayer().getUUID());
this.backend.saveStats(saveData);

this.backend.addServerThreadCallback(winCountFuture, new FutureCallback<>() {
@Override
public void onFailure(final @NotNull Throwable t) {
}

@Override
public void onSuccess(final Long winCount) {
final Minigame minigame = saveData.getMinigame();
final MinigamePlayer player = saveData.getPlayer();
winCountFuture.thenApply(winCount -> Bukkit.getScheduler().runTask(Minigames.getPlugin(), () -> {
final Minigame minigame = saveData.getMinigame();
final MinigamePlayer player = saveData.getPlayer();

// Do rewards
if (winner) {
RewardsModule.getModule(minigame).awardPlayer(player, saveData, minigame, winCount == 0);
} else {
RewardsModule.getModule(minigame).awardPlayerOnLoss(player, saveData, minigame);
}
// Do rewards
if (winner) {
RewardsModule.getModule(minigame).awardPlayer(player, saveData, minigame, winCount == 0);
} else {
RewardsModule.getModule(minigame).awardPlayerOnLoss(player, saveData, minigame);
}
});
}));
}

public void toggleDebug() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,26 @@
import au.com.mineauz.minigames.minigame.Minigame;
import au.com.mineauz.minigames.minigame.ScoreboardOrder;
import au.com.mineauz.minigames.stats.*;
import com.google.common.util.concurrent.*;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;

public class BackendManager {
private final Logger logger;
private boolean debug;

private Backend backend;
private final ListeningExecutorService executorService;
private final Executor bukkitThreadExecutor;

public BackendManager(Logger logger) {
this.logger = logger;
this.debug = false;

bukkitThreadExecutor = command -> Bukkit.getScheduler().runTask(Minigames.getPlugin(), command);

executorService = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
}

private Backend makeBackend(String type) {
Expand Down Expand Up @@ -145,10 +137,10 @@ public void shutdown() {
* @param order The order to retrieve in
* @param offset The offset to retrieve from
* @param length The number of stats to retrieve
* @return A ListenableFuture that returns the list of StoredStats loaded
* @return A CompletableFuture that returns the list of StoredStats loaded
*/
public ListenableFuture<List<StoredStat>> loadStats(final Minigame minigame, final MinigameStat stat, final StatValueField field, final ScoreboardOrder order, final int offset, final int length) {
return executorService.submit(() -> backend.loadStats(minigame, stat, field, order, offset, length));
public CompletableFuture<List<StoredStat>> loadStats(final Minigame minigame, final MinigameStat stat, final StatValueField field, final ScoreboardOrder order, final int offset, final int length) {
return CompletableFuture.supplyAsync(() -> backend.loadStats(minigame, stat, field, order, offset, length));
}

/**
Expand All @@ -160,18 +152,18 @@ public ListenableFuture<List<StoredStat>> loadStats(final Minigame minigame, fin
* @param playerId The player that owns the stat
* @return The value of the stat. If it is not set, 0 will be returned
*/
public ListenableFuture<Long> loadSingleStat(final Minigame minigame, final MinigameStat stat, final StatValueField field, final UUID playerId) {
return executorService.submit(() -> backend.getStat(minigame, playerId, stat, field));
public CompletableFuture<Long> loadSingleStat(final Minigame minigame, final MinigameStat stat, final StatValueField field, final UUID playerId) {
return CompletableFuture.supplyAsync(() -> backend.getStat(minigame, playerId, stat, field));
}

/**
* Queues a task to save the stats to the backend. The stats will be saved asynchronously
*
* @param stats The stats to be saved
* @return A ListenableFuture that returns the inputted stats for chaining.
* @return A CompletableFuture that returns the inputted stats for chaining.
*/
public ListenableFuture<StoredGameStats> saveStats(final StoredGameStats stats) {
return executorService.submit(() -> {
public CompletableFuture<StoredGameStats> saveStats(final StoredGameStats stats) {
return CompletableFuture.supplyAsync(() -> {
backend.saveGameStatus(stats);
return stats;
});
Expand All @@ -181,11 +173,10 @@ public ListenableFuture<StoredGameStats> saveStats(final StoredGameStats stats)
* Retrieves the settings for all stats in a minigame. This is loaded asynchronously
*
* @param minigame The minigame to load settings for
* @return A ListenableFuture that returns a map of minigame stats and their settings
* @return A CompletableFuture that returns a map of minigame stats and their settings
*/
public ListenableFuture<Map<MinigameStat, StatSettings>> loadStatSettings(final Minigame minigame) {

return executorService.submit(() -> backend.loadStatSettings(minigame));
public CompletableFuture<Map<MinigameStat, StatSettings>> loadStatSettings(final Minigame minigame) {
return CompletableFuture.supplyAsync(() -> backend.loadStatSettings(minigame));
}

/**
Expand All @@ -195,27 +186,17 @@ public ListenableFuture<Map<MinigameStat, StatSettings>> loadStatSettings(final
* @param settings The collection of settings to save
* @return A ListenableFuture to get the status of the save
*/
public ListenableFuture<Void> saveStatSettings(final Minigame minigame, final Collection<StatSettings> settings) {
public @Nullable CompletableFuture<Void> saveStatSettings(final Minigame minigame, final Collection<StatSettings> settings) {
if (backend instanceof TestBackEnd) {
backend.saveStatSettings(minigame, settings);
return null;
}
return executorService.submit(() -> {
return CompletableFuture.supplyAsync(() -> {
backend.saveStatSettings(minigame, settings);
return null;
});
}

/**
* Adds a callback to the ListenableFuture that will be executed on the Minecraft server thread
*
* @param future The future to add the callback to
* @param callback The callback to be added
*/
public <T> void addServerThreadCallback(ListenableFuture<T> future, FutureCallback<T> callback) {
Futures.addCallback(future, callback, bukkitThreadExecutor);
}

/**
* Initializes an export to the target backend type.
* The export can take a while and will prevent all other backend interactions while it goes on
Expand All @@ -226,7 +207,7 @@ public <T> void addServerThreadCallback(ListenableFuture<T> future, FutureCallba
* @return A future to let you know when the process is finished
* @throws IllegalArgumentException Thrown if the backend chosen cannot be used. Reason given in message
*/
public ListenableFuture<Void> exportTo(String type, ConfigurationSection config, final ExportNotifier notifier) throws IllegalArgumentException {
public CompletableFuture<Void> exportTo(String type, ConfigurationSection config, final ExportNotifier notifier) throws IllegalArgumentException {
final Backend destination = makeBackend(type);
if (destination == null) {
throw new IllegalArgumentException("Invalid backend type");
Expand All @@ -240,7 +221,7 @@ public ListenableFuture<Void> exportTo(String type, ConfigurationSection config,
throw new IllegalArgumentException("Failed to initialize destination backend");
}

return executorService.submit(() -> backend.exportTo(destination, notifier), null);
return CompletableFuture.runAsync(() -> backend.exportTo(destination, notifier), null);
}

/**
Expand All @@ -252,7 +233,7 @@ public ListenableFuture<Void> exportTo(String type, ConfigurationSection config,
* @param config The config to load settings from
* @throws IllegalArgumentException Thrown if the backend chosen cannot be used. Reason given in message
*/
public ListenableFuture<Void> switchBackend(final String type, ConfigurationSection config) throws IllegalArgumentException {
public CompletableFuture<Void> switchBackend(final String type, ConfigurationSection config) throws IllegalArgumentException {
final Backend newBackend = makeBackend(type);
if (newBackend == null) {
throw new IllegalArgumentException("Invalid backend type");
Expand All @@ -266,7 +247,7 @@ public ListenableFuture<Void> switchBackend(final String type, ConfigurationSect
throw new IllegalArgumentException("Failed to initialize target backend");
}

return executorService.submit(() -> {
return CompletableFuture.runAsync(() -> {
backend.shutdown();
backend = newBackend;
logger.warning("Backend has been switched to " + type);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package au.com.mineauz.minigames.backend;

import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.IdentityHashMap;
import java.util.Map;

public class ConnectionHandler {
Expand All @@ -18,7 +18,7 @@ public class ConnectionHandler {
public ConnectionHandler(Connection connection) {
this.connection = connection;

preparedStatements = Maps.newIdentityHashMap();
preparedStatements = new IdentityHashMap<>();
}

public boolean lease() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import au.com.mineauz.minigames.minigame.ScoreboardOrder;
import au.com.mineauz.minigames.objects.MinigamePlayer;
import au.com.mineauz.minigames.stats.*;
import com.google.common.collect.Maps;
import org.bukkit.configuration.ConfigurationSection;

import java.sql.ResultSet;
Expand Down Expand Up @@ -202,7 +201,7 @@ public Map<MinigameStat, StatSettings> loadStatSettings(Minigame minigame) {

int minigameId = getMinigameId(handler, minigame);

Map<MinigameStat, StatSettings> settings = Maps.newHashMap();
Map<MinigameStat, StatSettings> settings = new HashMap<>();

try (ResultSet rs = handler.executeQuery(loadStatSettings, minigameId)) {
while (rs.next()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import au.com.mineauz.minigames.stats.MinigameStat;
import au.com.mineauz.minigames.stats.StatValueField;
import au.com.mineauz.minigames.stats.StoredStat;
import com.google.common.collect.Lists;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -91,7 +91,7 @@ private List<StoredStat> loadStats(ConnectionHandler handler, int minigameId, Mi
case DESCENDING -> getSingleDesc;
};

List<StoredStat> stats = Lists.newArrayList();
List<StoredStat> stats = new ArrayList<>();
try (ResultSet rs = handler.executeQuery(statement, minigameId, statName, offset, length)) {
while (rs.next()) {
stats.add(loadStat(rs));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import au.com.mineauz.minigames.backend.BackendImportCallback;
import au.com.mineauz.minigames.backend.ExportNotifier;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.InvalidConfigurationException;
Expand All @@ -14,10 +12,7 @@

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.*;

public class FlatFileExporter {
private final File file;
Expand All @@ -40,7 +35,7 @@ public FlatFileExporter(File file, BackendImportCallback callback, ExportNotifie
this.notifier = notifier;

config = new YamlConfiguration();
minigameIds = Maps.newHashMap();
minigameIds = new HashMap<>();
}

public boolean doExport() {
Expand Down Expand Up @@ -82,7 +77,7 @@ private void loadCompletions() {

private void exportPlayers() {
notifyNext("Exporting players...");
Set<UUID> uniquePlayers = Sets.newHashSet(completions.values());
Set<UUID> uniquePlayers = new HashSet<>(completions.values());

for (UUID playerId : uniquePlayers) {
// Attempt to get information about this player
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import au.com.mineauz.minigames.minigame.ScoreboardOrder;
import au.com.mineauz.minigames.objects.MinigamePlayer;
import au.com.mineauz.minigames.stats.*;
import com.google.common.collect.Maps;
import org.bukkit.configuration.ConfigurationSection;

import java.io.File;
Expand Down Expand Up @@ -244,7 +243,7 @@ public Map<MinigameStat, StatSettings> loadStatSettings(Minigame minigame) {

int minigameId = getMinigameId(handler, minigame);

Map<MinigameStat, StatSettings> settings = Maps.newHashMap();
Map<MinigameStat, StatSettings> settings = new HashMap<>();

try (ResultSet rs = handler.executeQuery(loadStatSettings, minigameId)) {
while (rs.next()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import au.com.mineauz.minigames.stats.MinigameStat;
import au.com.mineauz.minigames.stats.StatValueField;
import au.com.mineauz.minigames.stats.StoredStat;
import com.google.common.collect.Lists;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -90,7 +90,7 @@ private List<StoredStat> loadStats(ConnectionHandler handler, int minigameId, Mi
case DESCENDING -> getSingleDesc;
};

List<StoredStat> stats = Lists.newArrayList();
List<StoredStat> stats = new ArrayList<>();
try (ResultSet rs = handler.executeQuery(statement, minigameId, statName, offset, length)) {
while (rs.next()) {
stats.add(loadStat(rs));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import au.com.mineauz.minigames.minigame.MinigameState;
import au.com.mineauz.minigames.objects.MinigamePlayer;
import au.com.mineauz.minigames.objects.Position;
import com.google.common.collect.Lists;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import io.papermc.lib.PaperLib;
Expand Down Expand Up @@ -248,9 +247,9 @@ public void restoreBlocks(final MinigamePlayer modifier) {
}
Iterator<MgBlockData> it = blockdata.values().iterator();

final List<MgBlockData> baseBlocks = Lists.newArrayList();
final List<MgBlockData> gravityBlocks = Lists.newArrayList();
final List<MgBlockData> attachableBlocks = Lists.newArrayList();
final List<MgBlockData> baseBlocks = new ArrayList<>();
final List<MgBlockData> gravityBlocks = new ArrayList<>();
final List<MgBlockData> attachableBlocks = new ArrayList<>();

//sort the blocks into the three lists above
while (it.hasNext()) {
Expand Down
Loading

0 comments on commit cd3cf0f

Please sign in to comment.