Fixed the Thread Safe Cache

Dieser Commit ist enthalten in:
Wyatt Childers 2016-02-02 18:15:33 -05:00
Ursprung 81960c6b54
Commit c62125cd70

Datei anzeigen

@ -24,6 +24,9 @@ import net.minecraft.server.MinecraftServer;
import net.minecraftforge.fml.common.FMLCommonHandler; import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent; import net.minecraftforge.fml.common.gameevent.TickEvent;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
@ -34,12 +37,10 @@ import java.util.concurrent.CopyOnWriteArraySet;
/** /**
* Caches data that cannot be accessed from another thread safely. * Caches data that cannot be accessed from another thread safely.
*/ */
public class ThreadSafeCache { public class ThreadSafeCache implements Runnable {
private static final long REFRESH_DELAY = 1000 * 30;
private static final ThreadSafeCache INSTANCE = new ThreadSafeCache(); private static final ThreadSafeCache INSTANCE = new ThreadSafeCache();
private Set<UUID> onlineIds = Collections.emptySet(); private Set<UUID> onlineIds = Collections.emptySet();
private long lastRefresh = 0;
/** /**
* Get an concurrent-safe set of UUIDs of online players. * Get an concurrent-safe set of UUIDs of online players.
@ -50,28 +51,15 @@ public class ThreadSafeCache {
return onlineIds; return onlineIds;
} }
@SubscribeEvent @Override
public void tickStart(TickEvent event) { public void run() {
long now = System.currentTimeMillis(); Set<UUID> onlineIds = new HashSet<>();
if (now - lastRefresh > REFRESH_DELAY) { for (Player player : Sponge.getServer().getOnlinePlayers()) {
Set<UUID> onlineIds = new HashSet<UUID>(); onlineIds.add(player.getUniqueId());
MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
if (server == null || server.getConfigurationManager() == null) {
return;
}
for (Object object : FMLCommonHandler.instance().getMinecraftServerInstance().getConfigurationManager().playerEntityList) {
if (object != null) {
EntityPlayerMP player = (EntityPlayerMP) object;
onlineIds.add(player.getUniqueID());
}
} }
this.onlineIds = new CopyOnWriteArraySet<UUID>(onlineIds); this.onlineIds = new CopyOnWriteArraySet<>(onlineIds);
lastRefresh = now;
}
} }
public static ThreadSafeCache getInstance() { public static ThreadSafeCache getInstance() {