Skip to content

Commit

Permalink
introduced facade interface PlayerInformationProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandollase committed Jun 1, 2017
1 parent cf3f7c7 commit bc895dc
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 70 deletions.
10 changes: 5 additions & 5 deletions src/main/java/amidst/PerApplicationInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
import amidst.mojangapi.MojangApi;
import amidst.mojangapi.MojangApiBuilder;
import amidst.mojangapi.file.DotMinecraftDirectoryNotFoundException;
import amidst.mojangapi.file.facade.PlayerInformationCache;
import amidst.mojangapi.file.facade.PlayerInformationProvider;
import amidst.mojangapi.minecraftinterface.local.LocalMinecraftInterfaceCreationException;
import amidst.mojangapi.world.SeedHistoryLogger;
import amidst.mojangapi.world.World;
import amidst.mojangapi.world.WorldBuilder;
import amidst.mojangapi.world.player.PlayerInformationCache;
import amidst.mojangapi.world.player.PlayerInformationCacheImpl;
import amidst.settings.biomeprofile.BiomeProfileDirectory;
import amidst.threading.ThreadMaster;

@NotThreadSafe
public class PerApplicationInjector {
private final AmidstMetaData metadata;
private final AmidstSettings settings;
private final PlayerInformationCache playerInformationCache;
private final PlayerInformationProvider playerInformationProvider;
private final SeedHistoryLogger seedHistoryLogger;
private final WorldBuilder worldBuilder;
private final MojangApi mojangApi;
Expand All @@ -50,9 +50,9 @@ public PerApplicationInjector(CommandLineParameters parameters, AmidstMetaData m
LocalMinecraftInterfaceCreationException {
this.metadata = metadata;
this.settings = settings;
this.playerInformationCache = new PlayerInformationCacheImpl();
this.playerInformationProvider = new PlayerInformationCache();
this.seedHistoryLogger = SeedHistoryLogger.from(parameters.seedHistoryFile);
this.worldBuilder = new WorldBuilder(playerInformationCache, seedHistoryLogger);
this.worldBuilder = new WorldBuilder(playerInformationProvider, seedHistoryLogger);
this.mojangApi = new MojangApiBuilder(worldBuilder, parameters).construct();
this.biomeProfileDirectory = BiomeProfileDirectory.create(parameters.biomeProfilesDirectory);
this.threadMaster = new ThreadMaster();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package amidst.mojangapi.file.facade;

import amidst.documentation.Immutable;
import amidst.documentation.NotNull;
import amidst.mojangapi.world.player.PlayerInformation;

@Immutable
public class ImmutablePlayerInformationProvider implements PlayerInformationProvider {
private final PlayerInformation playerInformation;

public ImmutablePlayerInformationProvider(PlayerInformation playerInformation) {
this.playerInformation = playerInformation;
}

@NotNull
@Override
public PlayerInformation getByPlayerUUID(String playerUUID) {
return playerInformation;
}

@NotNull
@Override
public PlayerInformation getByPlayerName(String playerName) {
return playerInformation;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package amidst.mojangapi.world.player;
package amidst.mojangapi.file.facade;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -7,6 +7,7 @@
import amidst.documentation.ThreadSafe;
import amidst.logging.AmidstLogger;
import amidst.mojangapi.file.service.PlayerInformationService;
import amidst.mojangapi.world.player.PlayerInformation;

/**
* Even though this class is thread-safe, it is possible that the same player
Expand All @@ -16,15 +17,15 @@
* problem.
*/
@ThreadSafe
public class PlayerInformationCacheImpl implements PlayerInformationCache {
public class PlayerInformationCache implements PlayerInformationProvider {
private final Map<String, PlayerInformation> byUUID = new ConcurrentHashMap<>();
private final Map<String, PlayerInformation> byName = new ConcurrentHashMap<>();
private final PlayerInformationService playerInformationService = new PlayerInformationService();

@NotNull
@Override
public PlayerInformation getByUUID(String uuid) {
String cleanUUID = getCleanUUID(uuid);
public PlayerInformation getByPlayerUUID(String playerUUID) {
String cleanUUID = getCleanUUID(playerUUID);
PlayerInformation result = byUUID.get(cleanUUID);
if (result != null) {
return result;
Expand All @@ -38,13 +39,13 @@ public PlayerInformation getByUUID(String uuid) {

@NotNull
@Override
public PlayerInformation getByName(String name) {
PlayerInformation result = byName.get(name);
public PlayerInformation getByPlayerName(String playerName) {
PlayerInformation result = byName.get(playerName);
if (result != null) {
return result;
} else {
AmidstLogger.info("requesting player information for name: " + name);
result = playerInformationService.fromName(name);
AmidstLogger.info("requesting player information for name: " + playerName);
result = playerInformationService.fromName(playerName);
put(result);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package amidst.mojangapi.file.facade;

import amidst.documentation.NotNull;
import amidst.documentation.ThreadSafe;
import amidst.mojangapi.world.player.PlayerInformation;

@ThreadSafe
public interface PlayerInformationProvider {
@NotNull
PlayerInformation getByPlayerUUID(String playerUUID);

@NotNull
PlayerInformation getByPlayerName(String playerName);
}
11 changes: 6 additions & 5 deletions src/main/java/amidst/mojangapi/file/facade/SaveGamePlayer.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package amidst.mojangapi.file.facade;

import java.util.function.Function;
import java.util.function.Supplier;

import amidst.documentation.Immutable;
import amidst.mojangapi.file.directory.SaveDirectory;
import amidst.mojangapi.file.nbt.player.PlayerNbt;
import amidst.mojangapi.file.service.SaveDirectoryService;
import amidst.mojangapi.world.player.PlayerCoordinates;
import amidst.mojangapi.world.player.PlayerInformation;

@Immutable
public class SaveGamePlayer {
Expand All @@ -29,7 +27,10 @@ public boolean tryBackupAndWritePlayerCoordinates(PlayerCoordinates coordinates)
&& saveDirectoryService.tryWriteCoordinates(saveDirectory, playerNbt, coordinates);
}

public <R> R map(Supplier<R> ifIsLevelDat, Function<String, R> ifIsPlayerdata, Function<String, R> ifIsPlayers) {
return playerNbt.map(ifIsLevelDat, ifIsPlayerdata, ifIsPlayers);
public PlayerInformation getPlayerInformation(PlayerInformationProvider playerInformationProvider) {
return playerNbt.map(
() -> PlayerInformation.theSingleplayerPlayer(),
playerUUID -> playerInformationProvider.getByPlayerUUID(playerUUID),
playerName -> playerInformationProvider.getByPlayerName(playerName));
}
}
14 changes: 7 additions & 7 deletions src/main/java/amidst/mojangapi/world/WorldBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import amidst.documentation.Immutable;
import amidst.mojangapi.file.MojangApiParsingException;
import amidst.mojangapi.file.facade.ImmutablePlayerInformationProvider;
import amidst.mojangapi.file.facade.PlayerInformationProvider;
import amidst.mojangapi.file.facade.SaveGame;
import amidst.mojangapi.minecraftinterface.MinecraftInterface;
import amidst.mojangapi.minecraftinterface.MinecraftInterfaceException;
Expand All @@ -26,10 +28,8 @@
import amidst.mojangapi.world.oracle.ImmutableWorldSpawnOracle;
import amidst.mojangapi.world.oracle.SlimeChunkOracle;
import amidst.mojangapi.world.oracle.WorldSpawnOracle;
import amidst.mojangapi.world.player.ImmutablePlayerInformationCache;
import amidst.mojangapi.world.player.MovablePlayerList;
import amidst.mojangapi.world.player.PlayerInformation;
import amidst.mojangapi.world.player.PlayerInformationCache;
import amidst.mojangapi.world.player.WorldPlayerType;
import amidst.mojangapi.world.versionfeatures.DefaultVersionFeatures;
import amidst.mojangapi.world.versionfeatures.VersionFeatures;
Expand All @@ -42,15 +42,15 @@ public class WorldBuilder {
*/
public static WorldBuilder createSilentPlayerless() {
return new WorldBuilder(
new ImmutablePlayerInformationCache(PlayerInformation.theSingleplayerPlayer()),
new ImmutablePlayerInformationProvider(PlayerInformation.theSingleplayerPlayer()),
SeedHistoryLogger.createDisabled());
}

private final PlayerInformationCache playerInformationCache;
private final PlayerInformationProvider playerInformationProvider;
private final SeedHistoryLogger seedHistoryLogger;

public WorldBuilder(PlayerInformationCache playerInformationCache, SeedHistoryLogger seedHistoryLogger) {
this.playerInformationCache = playerInformationCache;
public WorldBuilder(PlayerInformationProvider playerInformationProvider, SeedHistoryLogger seedHistoryLogger) {
this.playerInformationProvider = playerInformationProvider;
this.seedHistoryLogger = seedHistoryLogger;
}

Expand Down Expand Up @@ -78,7 +78,7 @@ public World fromSaveGame(MinecraftInterface minecraftInterface, SaveGame saveGa
MojangApiParsingException {
VersionFeatures versionFeatures = DefaultVersionFeatures.create(minecraftInterface.getRecognisedVersion());
MovablePlayerList movablePlayerList = new MovablePlayerList(
playerInformationCache,
playerInformationProvider,
saveGame,
true,
WorldPlayerType.from(saveGame));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import amidst.documentation.CalledOnlyBy;
import amidst.documentation.ThreadSafe;
import amidst.logging.AmidstLogger;
import amidst.mojangapi.file.facade.PlayerInformationProvider;
import amidst.mojangapi.file.facade.SaveGame;
import amidst.mojangapi.file.facade.SaveGamePlayer;
import amidst.threading.WorkerExecutor;
Expand All @@ -19,19 +20,19 @@ public static MovablePlayerList dummy() {
return DUMMY;
}

private final PlayerInformationCache playerInformationCache;
private final PlayerInformationProvider playerInformationProvider;
private final SaveGame saveGame;
private final boolean isSaveEnabled;

private volatile WorldPlayerType worldPlayerType;
private volatile ConcurrentLinkedQueue<Player> players = new ConcurrentLinkedQueue<>();

public MovablePlayerList(
PlayerInformationCache playerInformationCache,
PlayerInformationProvider playerInformationProvider,
SaveGame saveGame,
boolean isSaveEnabled,
WorldPlayerType worldPlayerType) {
this.playerInformationCache = playerInformationCache;
this.playerInformationProvider = playerInformationProvider;
this.saveGame = saveGame;
this.isSaveEnabled = isSaveEnabled;
this.worldPlayerType = worldPlayerType;
Expand Down Expand Up @@ -77,10 +78,7 @@ private void loadPlayers(

@CalledOnlyBy(AmidstThread.WORKER)
private void loadPlayer(ConcurrentLinkedQueue<Player> players, SaveGamePlayer saveGamePlayer) {
players.offer(saveGamePlayer.map(
() -> new Player(PlayerInformation.theSingleplayerPlayer(), saveGamePlayer),
playerUUID -> new Player(playerInformationCache.getByUUID(playerUUID), saveGamePlayer),
playerName -> new Player(playerInformationCache.getByName(playerName), saveGamePlayer)));
players.offer(new Player(saveGamePlayer.getPlayerInformation(playerInformationProvider), saveGamePlayer));
}

public boolean canSave() {
Expand Down

This file was deleted.

0 comments on commit bc895dc

Please sign in to comment.