forked from SteamWar/SteamWar
Improve network code
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2020 SteamWar.de-Serverteam
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.core;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@NoArgsConstructor
|
||||
public class Promise<E> {
|
||||
|
||||
private AtomicBoolean hasValue = new AtomicBoolean(false);
|
||||
private E value;
|
||||
|
||||
public void setValue(E value) {
|
||||
this.value = value;
|
||||
hasValue.set(true);
|
||||
}
|
||||
|
||||
public E getValue() {
|
||||
if (hasValue.get()) {
|
||||
return value;
|
||||
}
|
||||
|
||||
while (hasValue.get()) {
|
||||
Thread.yield();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,10 @@ package de.steamwar.entity;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import com.mojang.authlib.properties.Property;
|
||||
import de.steamwar.Reflection;
|
||||
import de.steamwar.core.*;
|
||||
import de.steamwar.core.BountifulWrapper;
|
||||
import de.steamwar.core.Core;
|
||||
import de.steamwar.core.FlatteningWrapper;
|
||||
import de.steamwar.core.ProtocolWrapper;
|
||||
import de.steamwar.network.NetworkSender;
|
||||
import de.steamwar.network.packets.common.PlayerSkinRequestPacket;
|
||||
import lombok.Getter;
|
||||
@@ -72,22 +75,21 @@ public class RPlayer extends REntity {
|
||||
server.addEntity(this);
|
||||
}
|
||||
|
||||
public static final Map<UUID, Promise<Property>> SKIN_DATA_PROMISES = new LinkedHashMap<UUID, Promise<Property>>() {
|
||||
public static final Map<UUID, Property> SKIN_DATA_PROMISES = new LinkedHashMap<UUID, Property>() {
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<UUID, Promise<Property>> eldest) {
|
||||
protected boolean removeEldestEntry(Map.Entry<UUID, Property> eldest) {
|
||||
return size() > 100;
|
||||
}
|
||||
};
|
||||
|
||||
private GameProfile getGameProfile() {
|
||||
Property property = SKIN_DATA_PROMISES.computeIfAbsent(uuid, __ -> {
|
||||
Promise<Property> future = new Promise<>();
|
||||
Property skinData = SKIN_DATA_PROMISES.computeIfAbsent(uuid, __ -> {
|
||||
NetworkSender.sendOrQueue(new PlayerSkinRequestPacket(uuid));
|
||||
return future;
|
||||
}).getValue();
|
||||
if (property != null) {
|
||||
return new Property("textures", null, null);
|
||||
});
|
||||
if (skinData.getValue() != null) {
|
||||
GameProfile gameProfile = new GameProfile(uuid, name);
|
||||
gameProfile.getProperties().put("textures", property);
|
||||
gameProfile.getProperties().put("textures", skinData);
|
||||
return gameProfile;
|
||||
} else {
|
||||
return new GameProfile(actualUUID, name);
|
||||
|
||||
@@ -21,7 +21,6 @@ package de.steamwar.network;
|
||||
|
||||
import com.mojang.authlib.properties.Property;
|
||||
import de.steamwar.core.BountifulWrapper;
|
||||
import de.steamwar.core.Promise;
|
||||
import de.steamwar.entity.RPlayer;
|
||||
import de.steamwar.network.handlers.InventoryHandler;
|
||||
import de.steamwar.network.packets.PacketHandler;
|
||||
@@ -75,8 +74,8 @@ public class CoreNetworkHandler extends PacketHandler {
|
||||
|
||||
@Handler
|
||||
public void handlePlayerSkinResponse(PlayerSkinResponsePacket packet) {
|
||||
Promise<Property> propertyPromise = RPlayer.SKIN_DATA_PROMISES.get(packet.getUuid());
|
||||
if (propertyPromise == null) return;
|
||||
propertyPromise.setValue(new Property("textures", packet.getSkin(), packet.getSignature()));
|
||||
Property property = RPlayer.SKIN_DATA_PROMISES.get(packet.getUuid());
|
||||
if (property == null) return;
|
||||
RPlayer.SKIN_DATA_PROMISES.put(packet.getUuid(), new Property("textures", packet.getSkin(), packet.getSignature()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,15 +27,12 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class NetworkSender implements Listener {
|
||||
|
||||
private static AtomicInteger numberOfPlayers = new AtomicInteger(0);
|
||||
private static List<NetworkPacket> queued = new ArrayList<>();
|
||||
|
||||
static {
|
||||
@@ -47,21 +44,17 @@ public class NetworkSender implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
numberOfPlayers.incrementAndGet();
|
||||
if (numberOfPlayers.get() > 1) return;
|
||||
if (!Bukkit.getOnlinePlayers().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Bukkit.getScheduler().runTaskLater(Core.getInstance(), () -> {
|
||||
queued.forEach(NetworkSender::send);
|
||||
queued.clear();
|
||||
}, 1);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
numberOfPlayers.decrementAndGet();
|
||||
}
|
||||
|
||||
public static void sendOrQueue(NetworkPacket packet) {
|
||||
if (numberOfPlayers.get() > 0) {
|
||||
if (!Bukkit.getOnlinePlayers().isEmpty()) {
|
||||
send(packet);
|
||||
} else {
|
||||
queued.add(packet);
|
||||
|
||||
Reference in New Issue
Block a user