#1752 #1830 Do not hide inventory if player is not registered

This commit is contained in:
ljacqu 2019-06-22 23:30:56 +02:00
parent 4be130b71b
commit 81cf14fbc1
2 changed files with 21 additions and 12 deletions

View File

@ -27,6 +27,7 @@ import com.comphenix.protocol.reflect.StructureModifier;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.Material;
@ -48,10 +49,12 @@ class InventoryPacketAdapter extends PacketAdapter {
private static final int HOTBAR_SIZE = 9;
private final PlayerCache playerCache;
private final DataSource dataSource;
InventoryPacketAdapter(AuthMe plugin, PlayerCache playerCache) {
InventoryPacketAdapter(AuthMe plugin, PlayerCache playerCache, DataSource dataSource) {
super(plugin, PacketType.Play.Server.SET_SLOT, PacketType.Play.Server.WINDOW_ITEMS);
this.playerCache = playerCache;
this.dataSource = dataSource;
}
@Override
@ -59,20 +62,22 @@ class InventoryPacketAdapter extends PacketAdapter {
Player player = packetEvent.getPlayer();
PacketContainer packet = packetEvent.getPacket();
byte windowId = packet.getIntegers().read(0).byteValue();
if (windowId == PLAYER_INVENTORY && !playerCache.isAuthenticated(player.getName())) {
int windowId = packet.getIntegers().read(0);
if (windowId == PLAYER_INVENTORY && shouldHideInventory(player.getName())) {
packetEvent.setCancelled(true);
}
}
public void register(BukkitService bukkitService, boolean hideNow) {
public void register(BukkitService bukkitService) {
ProtocolLibrary.getProtocolManager().addPacketListener(this);
if (hideNow) {
bukkitService.getOnlinePlayers().stream()
.filter(player -> playerCache.isAuthenticated(player.getName()))
.forEach(this::sendBlankInventoryPacket);
}
bukkitService.getOnlinePlayers().stream()
.filter(player -> shouldHideInventory(player.getName()))
.forEach(this::sendBlankInventoryPacket);
}
private boolean shouldHideInventory(String playerName) {
return !playerCache.isAuthenticated(playerName) && dataSource.isAuthAvailable(playerName);
}
public void unregister() {

View File

@ -4,6 +4,7 @@ import ch.jalu.injector.annotations.NoFieldScan;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.Settings;
@ -28,12 +29,15 @@ public class ProtocolLibService implements SettingsDependent {
private AuthMe plugin;
private BukkitService bukkitService;
private PlayerCache playerCache;
private DataSource dataSource;
@Inject
ProtocolLibService(AuthMe plugin, Settings settings, BukkitService bukkitService, PlayerCache playerCache) {
ProtocolLibService(AuthMe plugin, Settings settings, BukkitService bukkitService, PlayerCache playerCache,
DataSource dataSource) {
this.plugin = plugin;
this.bukkitService = bukkitService;
this.playerCache = playerCache;
this.dataSource = dataSource;
reload(settings);
}
@ -59,8 +63,8 @@ public class ProtocolLibService implements SettingsDependent {
if (protectInvBeforeLogin) {
if (inventoryPacketAdapter == null) {
// register the packet listener and start hiding it for all already online players (reload)
inventoryPacketAdapter = new InventoryPacketAdapter(plugin, playerCache);
inventoryPacketAdapter.register(bukkitService, true);
inventoryPacketAdapter = new InventoryPacketAdapter(plugin, playerCache, dataSource);
inventoryPacketAdapter.register(bukkitService);
}
} else if (inventoryPacketAdapter != null) {
inventoryPacketAdapter.unregister();