Skip to content

Commit

Permalink
Improve skin applying with BungeeCord
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed Feb 10, 2021
1 parent 3e72619 commit 7b38b5a
Showing 1 changed file with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,52 @@
import com.github.camotoy.geyserskinmanager.spigot.profile.GameProfileWrapper;
import com.github.camotoy.geyserskinmanager.spigot.profile.MinecraftProfileWrapper;
import com.github.camotoy.geyserskinmanager.spigot.profile.PaperProfileWrapper;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import io.papermc.lib.PaperLib;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.messaging.PluginMessageListener;

import javax.annotation.Nonnull;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

public class BungeecordPluginMessageListener implements PluginMessageListener {
public class BungeecordPluginMessageListener implements Listener, PluginMessageListener {
private final GeyserSkinManager plugin;
private final Function<Player, MinecraftProfileWrapper> getProfileFunction;
private final SpigotSkinApplier skinApplier;

/**
* Information is stored here in the event that a plugin message is received before the player has joined.
*/
private final Cache<UUID, SkinEntry> skinEntryCache = CacheBuilder.newBuilder()
.expireAfterWrite(10, TimeUnit.SECONDS)
.build();

public BungeecordPluginMessageListener(GeyserSkinManager plugin) {
this.plugin = plugin;
this.skinApplier = new SpigotSkinApplier(plugin);
this.getProfileFunction = (PaperLib.isPaper() && PaperLib.isVersion(12, 2)) ?
PaperProfileWrapper::from : GameProfileWrapper::from;

Bukkit.getPluginManager().registerEvents(this, this.plugin);
}

@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent event) {
SkinEntry skinEntry = skinEntryCache.getIfPresent(event.getPlayer().getUniqueId());
if (skinEntry != null) {
skinApplier.setSkin(getProfileFunction.apply(event.getPlayer()), event.getPlayer(), skinEntry);
}
}

@Override
Expand All @@ -44,14 +68,16 @@ public void onPluginMessageReceived(@Nonnull String channel, @Nonnull Player pla
return;
}
UUID uuid = new UUID(in.readLong(), in.readLong());
String value = in.readUTF();
String signature = in.readUTF();
SkinEntry skinEntry = new SkinEntry(value, signature);

Player bedrockPlayer = Bukkit.getPlayer(uuid);
if (bedrockPlayer == null) {
this.plugin.getLogger().warning("Player with UUID " + uuid + " could not be found!");
// Wait until they have officially joined
skinEntryCache.put(uuid, skinEntry);
return;
}
String value = in.readUTF();
String signature = in.readUTF();
SkinEntry skinEntry = new SkinEntry(value, signature);
skinApplier.setSkin(getProfileFunction.apply(bedrockPlayer), bedrockPlayer, skinEntry);
} catch (IOException e) {
e.printStackTrace();
Expand Down

0 comments on commit 7b38b5a

Please sign in to comment.