mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-18 11:29:15 +00:00
* Bump pack.mcmeta formats * 1.19 biome modifiers * Mark ClientPlayerNetworkEvent.LoggedOutEvent's getters as nullable * Add docs and package-info to client extension interfaces package * Move RenderBlockOverlayEvent hooks to ForgeHooksClient * Add package-infos to client events package * Rename SoundLoadEvent to SoundEngineLoadEvent This reduces confusion from consumers which may think the name SoundLoadEvent refers to an individual sound being loaded rather than the sound engine. * Document and change SoundLoadEvent to fire on mod bus Previously, it fired on both the mod bus and the Forge bus, which is confusing for consumers. * Delete SoundSetupEvent Looking at its original implementation shows that there isn't an appropriate place in the new sound code to reinsert the event, and the place of 'sound engine/manager initialization event' is taken already by SoundLoadEvent. * Perform some cleanup on client events - Removed nullable annotations from ClientPlayerNetworkEvent - Renamed #getPartialTicks methods to #getPartialTick, to be consistent with vanilla's naming of the partial tick - Cleanup documentation to remove line breaks, use the spelling 'cancelled' over 'canceled', and improve docs on existing and new methods. * Remove EntityEvent.CanUpdate Closes MinecraftForge/MinecraftForge#6394 * Switch to Jetbrains nullability annotations * New PlayLevelSoundEvent; replaces old PlaySoundAtEntityEvent * Remove ForgeWorldPresetScreens * Remove IForgeRegistryEntry * Remove use of List<Throwable> in FML's CompletableFutures * Add docs to mod loading stages, stages, and phases * Gradle 7.4.2 * Use SLF4J in FMLLoader and other subprojects * Switch dynamic versions in subprojects to pinned ones * Switch ForgeRoot and MDK to FG plugin markers * Configure Forge javadoc task The task now uses a custom stylesheet with MCForge elements, and configured to combine the generation from the four FML subprojects (fmlloader, fmlcore, javafmllanguage, mclanguage) and the Forge project into the javadoc output. * Update docs/md files, for 1.19 update and the move away from IRC to Discord. * Make "Potentially dangerous alternative prefix" a debug warning, not info. Co-authored-by: Curle <[email protected]> Co-authored-by: sciwhiz12 <[email protected]> Signed-off-by: SizableShrimp <[email protected]>
207 lines
5.6 KiB
Java
207 lines
5.6 KiB
Java
/*
|
|
* Copyright (c) Forge Development LLC and contributors
|
|
* SPDX-License-Identifier: LGPL-2.1-only
|
|
*/
|
|
|
|
package net.minecraftforge.common;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.lang.reflect.Type;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.UUID;
|
|
|
|
import com.google.common.base.Charsets;
|
|
import net.minecraftforge.fml.loading.FMLLoader;
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
import com.google.common.reflect.TypeToken;
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.GsonBuilder;
|
|
import com.google.gson.JsonSyntaxException;
|
|
import org.apache.logging.log4j.Marker;
|
|
import org.apache.logging.log4j.MarkerManager;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
/**
|
|
* Caches player's last known usernames
|
|
* <p>
|
|
* Modders should use {@link #getLastKnownUsername(UUID)} to determine a players
|
|
* last known username.<br>
|
|
* For convenience, {@link #getMap()} is provided to get an immutable copy of
|
|
* the caches underlying map.
|
|
*/
|
|
public final class UsernameCache {
|
|
|
|
private static Map<UUID, String> map = new HashMap<>();
|
|
|
|
private static final Path saveFile = FMLLoader.getGamePath().resolve("usernamecache.json");
|
|
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
|
|
|
private static final Logger LOGGER = LogManager.getLogger(UsernameCache.class);
|
|
private static final Marker USRCACHE = MarkerManager.getMarker("USERNAMECACHE");
|
|
|
|
private UsernameCache() {}
|
|
|
|
/**
|
|
* Set a player's current usernamee
|
|
*
|
|
* @param uuid
|
|
* the player's {@link java.util.UUID UUID}
|
|
* @param username
|
|
* the player's username
|
|
*/
|
|
protected static void setUsername(UUID uuid, String username)
|
|
{
|
|
Objects.requireNonNull(uuid);
|
|
Objects.requireNonNull(username);
|
|
|
|
if (username.equals(map.get(uuid))) return;
|
|
|
|
map.put(uuid, username);
|
|
save();
|
|
}
|
|
|
|
/**
|
|
* Remove a player's username from the cache
|
|
*
|
|
* @param uuid
|
|
* the player's {@link java.util.UUID UUID}
|
|
* @return if the cache contained the user
|
|
*/
|
|
protected static boolean removeUsername(UUID uuid)
|
|
{
|
|
Objects.requireNonNull(uuid);
|
|
|
|
if (map.remove(uuid) != null)
|
|
{
|
|
save();
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Get the player's last known username
|
|
* <p>
|
|
* <b>May be <code>null</code></b>
|
|
*
|
|
* @param uuid
|
|
* the player's {@link java.util.UUID UUID}
|
|
* @return the player's last known username, or <code>null</code> if the
|
|
* cache doesn't have a record of the last username
|
|
*/
|
|
@Nullable
|
|
public static String getLastKnownUsername(UUID uuid)
|
|
{
|
|
Objects.requireNonNull(uuid);
|
|
return map.get(uuid);
|
|
}
|
|
|
|
/**
|
|
* Check if the cache contains the given player's username
|
|
*
|
|
* @param uuid
|
|
* the player's {@link java.util.UUID UUID}
|
|
* @return if the cache contains a username for the given player
|
|
*/
|
|
public static boolean containsUUID(UUID uuid)
|
|
{
|
|
Objects.requireNonNull(uuid);
|
|
return map.containsKey(uuid);
|
|
}
|
|
|
|
/**
|
|
* Get an immutable copy of the cache's underlying map
|
|
*
|
|
* @return the map
|
|
*/
|
|
public static Map<UUID, String> getMap()
|
|
{
|
|
return ImmutableMap.copyOf(map);
|
|
}
|
|
|
|
/**
|
|
* Save the cache to file
|
|
*/
|
|
protected static void save()
|
|
{
|
|
new SaveThread(gson.toJson(map)).start();
|
|
}
|
|
|
|
/**
|
|
* Load the cache from file
|
|
*/
|
|
protected static void load()
|
|
{
|
|
if (!Files.exists(saveFile)) return;
|
|
|
|
try (final BufferedReader reader = Files.newBufferedReader(saveFile, Charsets.UTF_8))
|
|
{
|
|
@SuppressWarnings("serial")
|
|
Type type = new TypeToken<Map<UUID, String>>(){}.getType();
|
|
map = gson.fromJson(reader, type);
|
|
}
|
|
catch (JsonSyntaxException | IOException e)
|
|
{
|
|
LOGGER.error(USRCACHE,"Could not parse username cache file as valid json, deleting file {}", saveFile, e);
|
|
try
|
|
{
|
|
Files.delete(saveFile);
|
|
}
|
|
catch (IOException e1)
|
|
{
|
|
LOGGER.error(USRCACHE,"Could not delete file {}", saveFile.toString());
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
// Can sometimes occur when the json file is malformed
|
|
if (map == null)
|
|
{
|
|
map = new HashMap<>();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Used for saving the {@link com.google.gson.Gson#toJson(Object) Gson}
|
|
* representation of the cache to disk
|
|
*/
|
|
private static class SaveThread extends Thread {
|
|
|
|
/** The data that will be saved to disk */
|
|
private final String data;
|
|
|
|
public SaveThread(String data)
|
|
{
|
|
this.data = data;
|
|
}
|
|
|
|
@Override
|
|
public void run()
|
|
{
|
|
try
|
|
{
|
|
// Make sure we don't save when another thread is still saving
|
|
synchronized (saveFile)
|
|
{
|
|
Files.write(saveFile, data.getBytes(StandardCharsets.UTF_8));
|
|
}
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
LOGGER.error(USRCACHE, "Failed to save username cache to file!", e);
|
|
}
|
|
}
|
|
}
|
|
}
|