mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-01 14:07:43 +01:00
Replace PlayerConnection#sendPacket when possible
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
abeda20646
commit
7d930ee28d
@ -1,16 +1,13 @@
|
|||||||
package net.minestom.server.advancements;
|
package net.minestom.server.advancements;
|
||||||
|
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.minestom.server.entity.Player;
|
|
||||||
import net.minestom.server.item.ItemStack;
|
import net.minestom.server.item.ItemStack;
|
||||||
import net.minestom.server.item.Material;
|
import net.minestom.server.item.Material;
|
||||||
import net.minestom.server.network.packet.server.play.AdvancementsPacket;
|
import net.minestom.server.network.packet.server.play.AdvancementsPacket;
|
||||||
import net.minestom.server.utils.PacketUtils;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an advancement located in an {@link AdvancementTab}.
|
* Represents an advancement located in an {@link AdvancementTab}.
|
||||||
@ -323,13 +320,8 @@ public class Advancement {
|
|||||||
*/
|
*/
|
||||||
protected void update() {
|
protected void update() {
|
||||||
updateCriteria();
|
updateCriteria();
|
||||||
|
|
||||||
if (tab != null) {
|
if (tab != null) {
|
||||||
final Set<Player> viewers = tab.getViewers();
|
tab.sendPacketsToViewers(tab.removePacket, tab.createPacket());
|
||||||
AdvancementsPacket createPacket = tab.createPacket();
|
|
||||||
|
|
||||||
PacketUtils.sendGroupedPacket(viewers, tab.removePacket);
|
|
||||||
PacketUtils.sendGroupedPacket(viewers, createPacket);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@ package net.minestom.server.advancements;
|
|||||||
import net.minestom.server.Viewable;
|
import net.minestom.server.Viewable;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.network.packet.server.play.AdvancementsPacket;
|
import net.minestom.server.network.packet.server.play.AdvancementsPacket;
|
||||||
import net.minestom.server.network.player.PlayerConnection;
|
|
||||||
import net.minestom.server.utils.validate.Check;
|
import net.minestom.server.utils.validate.Check;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@ -115,17 +114,10 @@ public class AdvancementTab implements Viewable {
|
|||||||
@Override
|
@Override
|
||||||
public synchronized boolean addViewer(@NotNull Player player) {
|
public synchronized boolean addViewer(@NotNull Player player) {
|
||||||
final boolean result = viewers.add(player);
|
final boolean result = viewers.add(player);
|
||||||
if (!result) {
|
if (!result) return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
final PlayerConnection playerConnection = player.getPlayerConnection();
|
|
||||||
|
|
||||||
// Send the tab to the player
|
// Send the tab to the player
|
||||||
playerConnection.sendPacket(createPacket());
|
player.sendPacket(createPacket());
|
||||||
|
|
||||||
addPlayer(player);
|
addPlayer(player);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,16 +126,9 @@ public class AdvancementTab implements Viewable {
|
|||||||
if (!isViewer(player)) {
|
if (!isViewer(player)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
final PlayerConnection playerConnection = player.getPlayerConnection();
|
|
||||||
|
|
||||||
// Remove the tab
|
// Remove the tab
|
||||||
if (!player.isRemoved()) {
|
if (!player.isRemoved()) player.sendPacket(removePacket);
|
||||||
playerConnection.sendPacket(removePacket);
|
|
||||||
}
|
|
||||||
|
|
||||||
removePlayer(player);
|
removePlayer(player);
|
||||||
|
|
||||||
return viewers.remove(player);
|
return viewers.remove(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ public interface PacketGroupingAudience extends ForwardingAudience {
|
|||||||
* @param players the players
|
* @param players the players
|
||||||
* @return the audience
|
* @return the audience
|
||||||
*/
|
*/
|
||||||
static PacketGroupingAudience of(Collection<Player> players) {
|
static @NotNull PacketGroupingAudience of(@NotNull Collection<Player> players) {
|
||||||
return () -> players;
|
return () -> players;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,15 +45,15 @@ public interface PacketGroupingAudience extends ForwardingAudience {
|
|||||||
*
|
*
|
||||||
* @return the connections
|
* @return the connections
|
||||||
*/
|
*/
|
||||||
@NotNull Collection<Player> getPlayers();
|
@NotNull Collection<@NotNull Player> getPlayers();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Broadcast a ServerPacket to all players of this audience
|
* Broadcast a ServerPacket to all players of this audience
|
||||||
*
|
*
|
||||||
* @param packet the packet to broadcast
|
* @param packet the packet to broadcast
|
||||||
*/
|
*/
|
||||||
default void sendGroupedPacket(ServerPacket packet) {
|
default void sendGroupedPacket(@NotNull ServerPacket packet) {
|
||||||
PacketUtils.sendGroupedPacket(this.getPlayers(), packet);
|
PacketUtils.sendGroupedPacket(getPlayers(), packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -45,7 +45,7 @@ public class BossBarManager {
|
|||||||
public void addBossBar(@NotNull Player player, @NotNull BossBar bar) {
|
public void addBossBar(@NotNull Player player, @NotNull BossBar bar) {
|
||||||
BossBarHolder holder = this.getOrCreateHandler(bar);
|
BossBarHolder holder = this.getOrCreateHandler(bar);
|
||||||
if (holder.addViewer(player)) {
|
if (holder.addViewer(player)) {
|
||||||
player.getPlayerConnection().sendPacket(holder.createAddPacket());
|
player.sendPacket(holder.createAddPacket());
|
||||||
this.playerBars.computeIfAbsent(player.getUuid(), uuid -> new HashSet<>()).add(holder);
|
this.playerBars.computeIfAbsent(player.getUuid(), uuid -> new HashSet<>()).add(holder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,7 +59,7 @@ public class BossBarManager {
|
|||||||
public void removeBossBar(@NotNull Player player, @NotNull BossBar bar) {
|
public void removeBossBar(@NotNull Player player, @NotNull BossBar bar) {
|
||||||
BossBarHolder holder = this.bars.get(bar);
|
BossBarHolder holder = this.bars.get(bar);
|
||||||
if (holder != null && holder.removeViewer(player)) {
|
if (holder != null && holder.removeViewer(player)) {
|
||||||
player.getPlayerConnection().sendPacket(holder.createRemovePacket());
|
player.sendPacket(holder.createRemovePacket());
|
||||||
this.removePlayer(player, holder);
|
this.removePlayer(player, holder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,6 @@ import net.minestom.server.adventure.AdventurePacketConvertor;
|
|||||||
import net.minestom.server.adventure.Localizable;
|
import net.minestom.server.adventure.Localizable;
|
||||||
import net.minestom.server.adventure.audience.Audiences;
|
import net.minestom.server.adventure.audience.Audiences;
|
||||||
import net.minestom.server.attribute.Attribute;
|
import net.minestom.server.attribute.Attribute;
|
||||||
import net.minestom.server.command.CommandManager;
|
|
||||||
import net.minestom.server.command.CommandSender;
|
import net.minestom.server.command.CommandSender;
|
||||||
import net.minestom.server.coordinate.Point;
|
import net.minestom.server.coordinate.Point;
|
||||||
import net.minestom.server.coordinate.Pos;
|
import net.minestom.server.coordinate.Pos;
|
||||||
@ -247,14 +246,14 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
List.of("minestom:world"), nbt, dimensionType.toNBT(), dimensionType.getName().asString(),
|
List.of("minestom:world"), nbt, dimensionType.toNBT(), dimensionType.getName().asString(),
|
||||||
0, 0, MinecraftServer.getChunkViewDistance(), MinecraftServer.getChunkViewDistance(),
|
0, 0, MinecraftServer.getChunkViewDistance(), MinecraftServer.getChunkViewDistance(),
|
||||||
false, true, false, levelFlat);
|
false, true, false, levelFlat);
|
||||||
playerConnection.sendPacket(joinGamePacket);
|
sendPacket(joinGamePacket);
|
||||||
|
|
||||||
// Server brand name
|
// Server brand name
|
||||||
playerConnection.sendPacket(PluginMessagePacket.getBrandPacket());
|
sendPacket(PluginMessagePacket.getBrandPacket());
|
||||||
// Difficulty
|
// Difficulty
|
||||||
playerConnection.sendPacket(new ServerDifficultyPacket(MinecraftServer.getDifficulty(), true));
|
sendPacket(new ServerDifficultyPacket(MinecraftServer.getDifficulty(), true));
|
||||||
|
|
||||||
playerConnection.sendPacket(new SpawnPositionPacket(respawnPoint, 0));
|
sendPacket(new SpawnPositionPacket(respawnPoint, 0));
|
||||||
|
|
||||||
// Add player to list with spawning skin
|
// Add player to list with spawning skin
|
||||||
PlayerSkinInitEvent skinInitEvent = new PlayerSkinInitEvent(this, skin);
|
PlayerSkinInitEvent skinInitEvent = new PlayerSkinInitEvent(this, skin);
|
||||||
@ -277,13 +276,12 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
// Recipes start
|
// Recipes start
|
||||||
{
|
{
|
||||||
RecipeManager recipeManager = MinecraftServer.getRecipeManager();
|
RecipeManager recipeManager = MinecraftServer.getRecipeManager();
|
||||||
playerConnection.sendPacket(recipeManager.getDeclareRecipesPacket());
|
sendPacket(recipeManager.getDeclareRecipesPacket());
|
||||||
|
|
||||||
List<String> recipesIdentifier = new ArrayList<>();
|
List<String> recipesIdentifier = new ArrayList<>();
|
||||||
for (Recipe recipe : recipeManager.getRecipes()) {
|
for (Recipe recipe : recipeManager.getRecipes()) {
|
||||||
if (!recipe.shouldShow(this))
|
if (!recipe.shouldShow(this))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
recipesIdentifier.add(recipe.getRecipeId());
|
recipesIdentifier.add(recipe.getRecipeId());
|
||||||
}
|
}
|
||||||
if (!recipesIdentifier.isEmpty()) {
|
if (!recipesIdentifier.isEmpty()) {
|
||||||
@ -293,16 +291,16 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
false, false,
|
false, false,
|
||||||
false, false,
|
false, false,
|
||||||
recipesIdentifier, recipesIdentifier);
|
recipesIdentifier, recipesIdentifier);
|
||||||
playerConnection.sendPacket(unlockRecipesPacket);
|
sendPacket(unlockRecipesPacket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Recipes end
|
// Recipes end
|
||||||
|
|
||||||
// Tags
|
// Tags
|
||||||
this.playerConnection.sendPacket(TagsPacket.DEFAULT_TAGS);
|
sendPacket(TagsPacket.DEFAULT_TAGS);
|
||||||
|
|
||||||
// Some client updates
|
// Some client updates
|
||||||
this.playerConnection.sendPacket(getPropertiesPacket()); // Send default properties
|
sendPacket(getPropertiesPacket()); // Send default properties
|
||||||
triggerStatus((byte) (24 + permissionLevel)); // Set permission level
|
triggerStatus((byte) (24 + permissionLevel)); // Set permission level
|
||||||
refreshHealth(); // Heal and send health packet
|
refreshHealth(); // Heal and send health packet
|
||||||
refreshAbilities(); // Send abilities packet
|
refreshAbilities(); // Send abilities packet
|
||||||
@ -402,14 +400,13 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
|
|
||||||
// #buildDeathScreenText can return null, check here
|
// #buildDeathScreenText can return null, check here
|
||||||
if (deathText != null) {
|
if (deathText != null) {
|
||||||
playerConnection.sendPacket(new DeathCombatEventPacket(getEntityId(), -1, deathText));
|
sendPacket(new DeathCombatEventPacket(getEntityId(), -1, deathText));
|
||||||
}
|
}
|
||||||
|
|
||||||
// #buildDeathMessage can return null, check here
|
// #buildDeathMessage can return null, check here
|
||||||
if (chatMessage != null) {
|
if (chatMessage != null) {
|
||||||
Audiences.players().sendMessage(chatMessage);
|
Audiences.players().sendMessage(chatMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
super.kill();
|
super.kill();
|
||||||
}
|
}
|
||||||
@ -425,9 +422,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
setFireForDuration(0);
|
setFireForDuration(0);
|
||||||
setOnFire(false);
|
setOnFire(false);
|
||||||
refreshHealth();
|
refreshHealth();
|
||||||
RespawnPacket respawnPacket = new RespawnPacket(getDimensionType(), getDimensionType().getName().asString(),
|
sendPacket(new RespawnPacket(getDimensionType(), getDimensionType().getName().asString(),
|
||||||
0, gameMode, gameMode, false, levelFlat, true);
|
0, gameMode, gameMode, false, levelFlat, true));
|
||||||
getPlayerConnection().sendPacket(respawnPacket);
|
|
||||||
|
|
||||||
PlayerRespawnEvent respawnEvent = new PlayerRespawnEvent(this);
|
PlayerRespawnEvent respawnEvent = new PlayerRespawnEvent(this);
|
||||||
EventDispatcher.call(respawnEvent);
|
EventDispatcher.call(respawnEvent);
|
||||||
@ -443,8 +439,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
* Sends necessary packets to synchronize player data after a {@link RespawnPacket}
|
* Sends necessary packets to synchronize player data after a {@link RespawnPacket}
|
||||||
*/
|
*/
|
||||||
private void refreshClientStateAfterRespawn() {
|
private void refreshClientStateAfterRespawn() {
|
||||||
this.playerConnection.sendPacket(new UpdateHealthPacket(this.getHealth(), food, foodSaturation));
|
sendPacket(new UpdateHealthPacket(this.getHealth(), food, foodSaturation));
|
||||||
this.playerConnection.sendPacket(new SetExperiencePacket(exp, level, 0));
|
sendPacket(new SetExperiencePacket(exp, level, 0));
|
||||||
triggerStatus((byte) (24 + permissionLevel)); // Set permission level
|
triggerStatus((byte) (24 + permissionLevel)); // Set permission level
|
||||||
refreshAbilities();
|
refreshAbilities();
|
||||||
}
|
}
|
||||||
@ -455,9 +451,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
* again, and any changes will be visible to the player.
|
* again, and any changes will be visible to the player.
|
||||||
*/
|
*/
|
||||||
public void refreshCommands() {
|
public void refreshCommands() {
|
||||||
CommandManager commandManager = MinecraftServer.getCommandManager();
|
sendPacket(MinecraftServer.getCommandManager().createDeclareCommandsPacket(this));
|
||||||
DeclareCommandsPacket declareCommandsPacket = commandManager.createDeclareCommandsPacket(this);
|
|
||||||
playerConnection.sendPacket(declareCommandsPacket);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -507,7 +501,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendPacketToViewersAndSelf(@NotNull SendablePacket packet) {
|
public void sendPacketToViewersAndSelf(@NotNull SendablePacket packet) {
|
||||||
this.playerConnection.sendPacket(packet);
|
sendPacket(packet);
|
||||||
super.sendPacketToViewersAndSelf(packet);
|
super.sendPacketToViewersAndSelf(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -639,7 +633,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
* @param data the message data
|
* @param data the message data
|
||||||
*/
|
*/
|
||||||
public void sendPluginMessage(@NotNull String channel, byte @NotNull [] data) {
|
public void sendPluginMessage(@NotNull String channel, byte @NotNull [] data) {
|
||||||
playerConnection.sendPacket(new PluginMessagePacket(channel, data));
|
sendPacket(new PluginMessagePacket(channel, data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -675,7 +669,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void playSound(@NotNull Sound sound, double x, double y, double z) {
|
public void playSound(@NotNull Sound sound, double x, double y, double z) {
|
||||||
playerConnection.sendPacket(AdventurePacketConvertor.createSoundPacket(sound, x, y, z));
|
sendPacket(AdventurePacketConvertor.createSoundPacket(sound, x, y, z));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -686,12 +680,12 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
} else {
|
} else {
|
||||||
packet = AdventurePacketConvertor.createSoundPacket(sound, emitter);
|
packet = AdventurePacketConvertor.createSoundPacket(sound, emitter);
|
||||||
}
|
}
|
||||||
playerConnection.sendPacket(packet);
|
sendPacket(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stopSound(@NotNull SoundStop stop) {
|
public void stopSound(@NotNull SoundStop stop) {
|
||||||
playerConnection.sendPacket(AdventurePacketConvertor.createSoundStopPacket(stop));
|
sendPacket(AdventurePacketConvertor.createSoundStopPacket(stop));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -705,32 +699,32 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
* @param disableRelativeVolume disable volume scaling based on distance
|
* @param disableRelativeVolume disable volume scaling based on distance
|
||||||
*/
|
*/
|
||||||
public void playEffect(@NotNull Effects effect, int x, int y, int z, int data, boolean disableRelativeVolume) {
|
public void playEffect(@NotNull Effects effect, int x, int y, int z, int data, boolean disableRelativeVolume) {
|
||||||
playerConnection.sendPacket(new EffectPacket(effect.getId(), new Vec(x, y, z), data, disableRelativeVolume));
|
sendPacket(new EffectPacket(effect.getId(), new Vec(x, y, z), data, disableRelativeVolume));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendPlayerListHeaderAndFooter(@NotNull Component header, @NotNull Component footer) {
|
public void sendPlayerListHeaderAndFooter(@NotNull Component header, @NotNull Component footer) {
|
||||||
playerConnection.sendPacket(new PlayerListHeaderAndFooterPacket(header, footer));
|
sendPacket(new PlayerListHeaderAndFooterPacket(header, footer));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> void sendTitlePart(@NotNull TitlePart<T> part, @NotNull T value) {
|
public <T> void sendTitlePart(@NotNull TitlePart<T> part, @NotNull T value) {
|
||||||
playerConnection.sendPacket(AdventurePacketConvertor.createTitlePartPacket(part, value));
|
sendPacket(AdventurePacketConvertor.createTitlePartPacket(part, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendActionBar(@NotNull Component message) {
|
public void sendActionBar(@NotNull Component message) {
|
||||||
playerConnection.sendPacket(new ActionBarPacket(message));
|
sendPacket(new ActionBarPacket(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void resetTitle() {
|
public void resetTitle() {
|
||||||
playerConnection.sendPacket(new ClearTitlesPacket(true));
|
sendPacket(new ClearTitlesPacket(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clearTitle() {
|
public void clearTitle() {
|
||||||
playerConnection.sendPacket(new ClearTitlesPacket(false));
|
sendPacket(new ClearTitlesPacket(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -753,11 +747,11 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
.pages(book.pages()))
|
.pages(book.pages()))
|
||||||
.build();
|
.build();
|
||||||
// Set book in offhand
|
// Set book in offhand
|
||||||
playerConnection.sendPacket(new SetSlotPacket((byte) 0, 0, (short) PlayerInventoryUtils.OFFHAND_SLOT, writtenBook));
|
sendPacket(new SetSlotPacket((byte) 0, 0, (short) PlayerInventoryUtils.OFFHAND_SLOT, writtenBook));
|
||||||
// Open the book
|
// Open the book
|
||||||
playerConnection.sendPacket(new OpenBookPacket(Hand.OFF));
|
sendPacket(new OpenBookPacket(Hand.OFF));
|
||||||
// Restore the item in offhand
|
// Restore the item in offhand
|
||||||
playerConnection.sendPacket(new SetSlotPacket((byte) 0, 0, (short) PlayerInventoryUtils.OFFHAND_SLOT, getItemInOffHand()));
|
sendPacket(new SetSlotPacket((byte) 0, 0, (short) PlayerInventoryUtils.OFFHAND_SLOT, getItemInOffHand()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -771,7 +765,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
@Override
|
@Override
|
||||||
public void setHealth(float health) {
|
public void setHealth(float health) {
|
||||||
super.setHealth(health);
|
super.setHealth(health);
|
||||||
this.playerConnection.sendPacket(new UpdateHealthPacket(health, food, foodSaturation));
|
sendPacket(new UpdateHealthPacket(health, food, foodSaturation));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -816,7 +810,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
Check.argCondition(!MathUtils.isBetween(food, 0, 20),
|
Check.argCondition(!MathUtils.isBetween(food, 0, 20),
|
||||||
"Food has to be between 0 and 20");
|
"Food has to be between 0 and 20");
|
||||||
this.food = food;
|
this.food = food;
|
||||||
this.playerConnection.sendPacket(new UpdateHealthPacket(getHealth(), food, foodSaturation));
|
sendPacket(new UpdateHealthPacket(getHealth(), food, foodSaturation));
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getFoodSaturation() {
|
public float getFoodSaturation() {
|
||||||
@ -833,7 +827,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
Check.argCondition(!MathUtils.isBetween(foodSaturation, 0, 20),
|
Check.argCondition(!MathUtils.isBetween(foodSaturation, 0, 20),
|
||||||
"Food saturation has to be between 0 and 20");
|
"Food saturation has to be between 0 and 20");
|
||||||
this.foodSaturation = foodSaturation;
|
this.foodSaturation = foodSaturation;
|
||||||
this.playerConnection.sendPacket(new UpdateHealthPacket(getHealth(), food, foodSaturation));
|
sendPacket(new UpdateHealthPacket(getHealth(), food, foodSaturation));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -935,10 +929,10 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
RespawnPacket respawnPacket = new RespawnPacket(getDimensionType(), getDimensionType().getName().asString(),
|
RespawnPacket respawnPacket = new RespawnPacket(getDimensionType(), getDimensionType().getName().asString(),
|
||||||
0, gameMode, gameMode, false, levelFlat, true);
|
0, gameMode, gameMode, false, levelFlat, true);
|
||||||
|
|
||||||
playerConnection.sendPacket(removePlayerPacket);
|
sendPacket(removePlayerPacket);
|
||||||
playerConnection.sendPacket(destroyEntitiesPacket);
|
sendPacket(destroyEntitiesPacket);
|
||||||
playerConnection.sendPacket(addPlayerPacket);
|
sendPacket(addPlayerPacket);
|
||||||
playerConnection.sendPacket(respawnPacket);
|
sendPacket(respawnPacket);
|
||||||
refreshClientStateAfterRespawn();
|
refreshClientStateAfterRespawn();
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -1026,7 +1020,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
* @param resourcePack the resource pack
|
* @param resourcePack the resource pack
|
||||||
*/
|
*/
|
||||||
public void setResourcePack(@NotNull ResourcePack resourcePack) {
|
public void setResourcePack(@NotNull ResourcePack resourcePack) {
|
||||||
playerConnection.sendPacket(new ResourcePackSendPacket(resourcePack));
|
sendPacket(new ResourcePackSendPacket(resourcePack));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1053,7 +1047,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
private void facePosition(@NotNull FacePoint facePoint, @NotNull Point targetPosition,
|
private void facePosition(@NotNull FacePoint facePoint, @NotNull Point targetPosition,
|
||||||
@Nullable Entity entity, @Nullable FacePoint targetPoint) {
|
@Nullable Entity entity, @Nullable FacePoint targetPoint) {
|
||||||
final int entityId = entity != null ? entity.getEntityId() : 0;
|
final int entityId = entity != null ? entity.getEntityId() : 0;
|
||||||
playerConnection.sendPacket(new FacePlayerPacket(
|
sendPacket(new FacePlayerPacket(
|
||||||
facePoint == FacePoint.EYE ?
|
facePoint == FacePoint.EYE ?
|
||||||
FacePlayerPacket.FacePosition.EYES : FacePlayerPacket.FacePosition.FEET, targetPosition,
|
FacePlayerPacket.FacePosition.EYES : FacePlayerPacket.FacePosition.FEET, targetPosition,
|
||||||
entityId,
|
entityId,
|
||||||
@ -1067,7 +1061,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
* @param entity the entity to spectate
|
* @param entity the entity to spectate
|
||||||
*/
|
*/
|
||||||
public void spectate(@NotNull Entity entity) {
|
public void spectate(@NotNull Entity entity) {
|
||||||
playerConnection.sendPacket(new CameraPacket(entity));
|
sendPacket(new CameraPacket(entity));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1142,7 +1136,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
public void setExp(float exp) {
|
public void setExp(float exp) {
|
||||||
Check.argCondition(!MathUtils.isBetween(exp, 0, 1), "Exp should be between 0 and 1");
|
Check.argCondition(!MathUtils.isBetween(exp, 0, 1), "Exp should be between 0 and 1");
|
||||||
this.exp = exp;
|
this.exp = exp;
|
||||||
this.playerConnection.sendPacket(new SetExperiencePacket(exp, level, 0));
|
sendPacket(new SetExperiencePacket(exp, level, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1162,7 +1156,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
*/
|
*/
|
||||||
public void setLevel(int level) {
|
public void setLevel(int level) {
|
||||||
this.level = level;
|
this.level = level;
|
||||||
this.playerConnection.sendPacket(new SetExperiencePacket(exp, level, 0));
|
sendPacket(new SetExperiencePacket(exp, level, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1324,7 +1318,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
} else {
|
} else {
|
||||||
disconnectPacket = new DisconnectPacket(component);
|
disconnectPacket = new DisconnectPacket(component);
|
||||||
}
|
}
|
||||||
playerConnection.sendPacket(disconnectPacket);
|
sendPacket(disconnectPacket);
|
||||||
playerConnection.disconnect();
|
playerConnection.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1346,7 +1340,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
public void setHeldItemSlot(byte slot) {
|
public void setHeldItemSlot(byte slot) {
|
||||||
Check.argCondition(!MathUtils.isBetween(slot, 0, 8), "Slot has to be between 0 and 8");
|
Check.argCondition(!MathUtils.isBetween(slot, 0, 8), "Slot has to be between 0 and 8");
|
||||||
refreshHeldSlot(slot);
|
refreshHeldSlot(slot);
|
||||||
this.playerConnection.sendPacket(new HeldItemChangePacket(slot));
|
sendPacket(new HeldItemChangePacket(slot));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1361,8 +1355,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
public void setTeam(Team team) {
|
public void setTeam(Team team) {
|
||||||
super.setTeam(team);
|
super.setTeam(team);
|
||||||
if (team != null) {
|
if (team != null) {
|
||||||
var players = MinecraftServer.getConnectionManager().getOnlinePlayers();
|
PacketUtils.broadcastPacket(team.createTeamsCreationPacket());
|
||||||
PacketUtils.sendGroupedPacket(players, team.createTeamsCreationPacket());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1411,7 +1404,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
playerConnection.sendPacket(new OpenWindowPacket(newInventory.getWindowId(),
|
sendPacket(new OpenWindowPacket(newInventory.getWindowId(),
|
||||||
newInventory.getInventoryType().getWindowType(), newInventory.getTitle()));
|
newInventory.getInventoryType().getWindowType(), newInventory.getTitle()));
|
||||||
newInventory.addViewer(this);
|
newInventory.addViewer(this);
|
||||||
this.openInventory = newInventory;
|
this.openInventory = newInventory;
|
||||||
@ -1450,7 +1443,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
openInventory.removeViewer(this); // Clear cache
|
openInventory.removeViewer(this); // Clear cache
|
||||||
this.openInventory = null;
|
this.openInventory = null;
|
||||||
}
|
}
|
||||||
playerConnection.sendPacket(closeWindowPacket);
|
sendPacket(closeWindowPacket);
|
||||||
inventory.update();
|
inventory.update();
|
||||||
this.didCloseInventory = true;
|
this.didCloseInventory = true;
|
||||||
}
|
}
|
||||||
@ -1501,7 +1494,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
protected void synchronizePosition(boolean includeSelf) {
|
protected void synchronizePosition(boolean includeSelf) {
|
||||||
if (includeSelf) {
|
if (includeSelf) {
|
||||||
playerConnection.sendPacket(new PlayerPositionAndLookPacket(position, (byte) 0x00, getNextTeleportId(), false));
|
sendPacket(new PlayerPositionAndLookPacket(position, (byte) 0x00, getNextTeleportId(), false));
|
||||||
}
|
}
|
||||||
super.synchronizePosition(includeSelf);
|
super.synchronizePosition(includeSelf);
|
||||||
}
|
}
|
||||||
@ -1723,7 +1716,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||||||
flags |= PlayerAbilitiesPacket.FLAG_ALLOW_FLYING;
|
flags |= PlayerAbilitiesPacket.FLAG_ALLOW_FLYING;
|
||||||
if (instantBreak)
|
if (instantBreak)
|
||||||
flags |= PlayerAbilitiesPacket.FLAG_INSTANT_BREAK;
|
flags |= PlayerAbilitiesPacket.FLAG_INSTANT_BREAK;
|
||||||
playerConnection.sendPacket(new PlayerAbilitiesPacket(flags, flyingSpeed, fieldViewModifier));
|
sendPacket(new PlayerAbilitiesPacket(flags, flyingSpeed, fieldViewModifier));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -130,7 +130,7 @@ public class FakePlayer extends Player implements NavigableEntity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateNewViewer(@NotNull Player player) {
|
public void updateNewViewer(@NotNull Player player) {
|
||||||
player.getPlayerConnection().sendPacket(getAddPlayerToList());
|
player.sendPacket(getAddPlayerToList());
|
||||||
handleTabList(player.getPlayerConnection());
|
handleTabList(player.getPlayerConnection());
|
||||||
super.updateNewViewer(player);
|
super.updateNewViewer(player);
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ import java.util.List;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.CopyOnWriteArraySet;
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an inventory which can be viewed by a collection of {@link Player}.
|
* Represents an inventory which can be viewed by a collection of {@link Player}.
|
||||||
@ -26,9 +27,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
|
|||||||
* It can then be opened using {@link Player#openInventory(Inventory)}.
|
* It can then be opened using {@link Player#openInventory(Inventory)}.
|
||||||
*/
|
*/
|
||||||
public non-sealed class Inventory extends AbstractInventory implements Viewable {
|
public non-sealed class Inventory extends AbstractInventory implements Viewable {
|
||||||
|
private static final AtomicInteger ID_COUNTER = new AtomicInteger();
|
||||||
// incremented each time an inventory is created (used in the window packets)
|
|
||||||
private static byte LAST_INVENTORY_ID;
|
|
||||||
|
|
||||||
// the id of this inventory
|
// the id of this inventory
|
||||||
private final byte id;
|
private final byte id;
|
||||||
@ -58,11 +57,8 @@ public non-sealed class Inventory extends AbstractInventory implements Viewable
|
|||||||
this(inventoryType, Component.text(title));
|
this(inventoryType, Component.text(title));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static synchronized byte generateId() {
|
private static byte generateId() {
|
||||||
if (LAST_INVENTORY_ID == Byte.MAX_VALUE) {
|
return (byte) Math.abs((byte) ID_COUNTER.incrementAndGet());
|
||||||
LAST_INVENTORY_ID = 0;
|
|
||||||
}
|
|
||||||
return ++LAST_INVENTORY_ID;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -109,7 +105,7 @@ public non-sealed class Inventory extends AbstractInventory implements Viewable
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void clear() {
|
public synchronized void clear() {
|
||||||
cursorPlayersItem.clear();
|
this.cursorPlayersItem.clear();
|
||||||
super.clear();
|
super.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,14 +140,8 @@ public non-sealed class PlayerInventory extends AbstractInventory implements Equ
|
|||||||
case CHESTPLATE_SLOT -> EquipmentSlot.CHESTPLATE;
|
case CHESTPLATE_SLOT -> EquipmentSlot.CHESTPLATE;
|
||||||
case LEGGINGS_SLOT -> EquipmentSlot.LEGGINGS;
|
case LEGGINGS_SLOT -> EquipmentSlot.LEGGINGS;
|
||||||
case BOOTS_SLOT -> EquipmentSlot.BOOTS;
|
case BOOTS_SLOT -> EquipmentSlot.BOOTS;
|
||||||
default -> {
|
case OFFHAND_SLOT -> EquipmentSlot.OFF_HAND;
|
||||||
if (slot == player.getHeldSlot()) {
|
default -> slot == player.getHeldSlot() ? EquipmentSlot.MAIN_HAND : null;
|
||||||
yield EquipmentSlot.MAIN_HAND;
|
|
||||||
} else if (slot == OFFHAND_SLOT) {
|
|
||||||
yield EquipmentSlot.OFF_HAND;
|
|
||||||
}
|
|
||||||
yield null;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
if (equipmentSlot != null) {
|
if (equipmentSlot != null) {
|
||||||
EntityEquipEvent entityEquipEvent = new EntityEquipEvent(player, itemStack, equipmentSlot);
|
EntityEquipEvent entityEquipEvent = new EntityEquipEvent(player, itemStack, equipmentSlot);
|
||||||
|
@ -93,7 +93,7 @@ public class BlockPlacementListener {
|
|||||||
// using refreshChunk results in the client not being in sync
|
// using refreshChunk results in the client not being in sync
|
||||||
// after rapid invalid block placements
|
// after rapid invalid block placements
|
||||||
final Block block = instance.getBlock(placementPosition);
|
final Block block = instance.getBlock(placementPosition);
|
||||||
player.getPlayerConnection().sendPacket(new BlockChangePacket(placementPosition, block));
|
player.sendPacket(new BlockChangePacket(placementPosition, block));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,10 +45,9 @@ public final class PlayerDiggingListener {
|
|||||||
} else if (status == ClientPlayerDiggingPacket.Status.SWAP_ITEM_HAND) {
|
} else if (status == ClientPlayerDiggingPacket.Status.SWAP_ITEM_HAND) {
|
||||||
swapItemHand(player);
|
swapItemHand(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Acknowledge start/cancel/finish digging status
|
// Acknowledge start/cancel/finish digging status
|
||||||
if (diggingResult != null) {
|
if (diggingResult != null) {
|
||||||
player.getPlayerConnection().sendPacket(new AcknowledgePlayerDiggingPacket(blockPosition, diggingResult.block,
|
player.sendPacket(new AcknowledgePlayerDiggingPacket(blockPosition, diggingResult.block,
|
||||||
status, diggingResult.success));
|
status, diggingResult.success));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ import net.minestom.server.event.player.PlayerMoveEvent;
|
|||||||
import net.minestom.server.instance.Instance;
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.network.packet.client.play.*;
|
import net.minestom.server.network.packet.client.play.*;
|
||||||
import net.minestom.server.network.packet.server.play.PlayerPositionAndLookPacket;
|
import net.minestom.server.network.packet.server.play.PlayerPositionAndLookPacket;
|
||||||
import net.minestom.server.network.player.PlayerConnection;
|
|
||||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@ -62,8 +61,7 @@ public class PlayerPositionListener {
|
|||||||
}
|
}
|
||||||
if (playerMoveEvent.isCancelled()) {
|
if (playerMoveEvent.isCancelled()) {
|
||||||
// Teleport to previous position
|
// Teleport to previous position
|
||||||
PlayerConnection connection = player.getPlayerConnection();
|
player.sendPacket(new PlayerPositionAndLookPacket(currentPosition, (byte) 0x00, player.getNextTeleportId(), false));
|
||||||
connection.sendPacket(new PlayerPositionAndLookPacket(currentPosition, (byte) 0x00, player.getNextTeleportId(), false));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final Pos eventPosition = playerMoveEvent.getNewPosition();
|
final Pos eventPosition = playerMoveEvent.getNewPosition();
|
||||||
|
@ -7,6 +7,6 @@ import net.minestom.server.network.packet.server.play.CraftRecipeResponse;
|
|||||||
public class RecipeListener {
|
public class RecipeListener {
|
||||||
|
|
||||||
public static void listener(ClientCraftRecipeRequest packet, Player player) {
|
public static void listener(ClientCraftRecipeRequest packet, Player player) {
|
||||||
player.getPlayerConnection().sendPacket(new CraftRecipeResponse(packet.windowId(), packet.recipe()));
|
player.sendPacket(new CraftRecipeResponse(packet.windowId(), packet.recipe()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,9 +24,8 @@ public class StatusListener {
|
|||||||
playerStatistic.getStatisticId(), value));
|
playerStatistic.getStatisticId(), value));
|
||||||
}
|
}
|
||||||
StatisticsPacket statisticsPacket = new StatisticsPacket(statisticList);
|
StatisticsPacket statisticsPacket = new StatisticsPacket(statisticList);
|
||||||
player.getPlayerConnection().sendPacket(statisticsPacket);
|
player.sendPacket(statisticsPacket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -57,10 +57,9 @@ public class TabCompleteListener {
|
|||||||
Suggestion suggestion = new Suggestion(input, start, inputLength);
|
Suggestion suggestion = new Suggestion(input, start, inputLength);
|
||||||
suggestionCallback.apply(player, queryResult.context(), suggestion);
|
suggestionCallback.apply(player, queryResult.context(), suggestion);
|
||||||
|
|
||||||
player.getPlayerConnection().sendPacket(new TabCompletePacket(packet.transactionId(), suggestion.getStart(), suggestion.getLength(),
|
player.sendPacket(new TabCompletePacket(packet.transactionId(), suggestion.getStart(), suggestion.getLength(),
|
||||||
suggestion.getEntries().stream()
|
suggestion.getEntries().stream()
|
||||||
.map(suggestionEntry -> new TabCompletePacket.Match(suggestionEntry.getEntry(), suggestionEntry.getTooltip())).toList()));
|
.map(suggestionEntry -> new TabCompletePacket.Match(suggestionEntry.getEntry(), suggestionEntry.getTooltip())).toList()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -78,7 +78,7 @@ public class WindowListener {
|
|||||||
refreshCursorItem(player, inventory);
|
refreshCursorItem(player, inventory);
|
||||||
|
|
||||||
// (Why is the ping packet necessary?)
|
// (Why is the ping packet necessary?)
|
||||||
player.getPlayerConnection().sendPacket(new PingPacket((1 << 30) | (windowId << 16)));
|
player.sendPacket(new PingPacket((1 << 30) | (windowId << 16)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void pong(ClientPongPacket packet, Player player) {
|
public static void pong(ClientPongPacket packet, Player player) {
|
||||||
@ -111,7 +111,7 @@ public class WindowListener {
|
|||||||
throw new RuntimeException("Invalid inventory: " + inventory.getClass());
|
throw new RuntimeException("Invalid inventory: " + inventory.getClass());
|
||||||
}
|
}
|
||||||
final SetSlotPacket setSlotPacket = SetSlotPacket.createCursorPacket(cursorItem);
|
final SetSlotPacket setSlotPacket = SetSlotPacket.createCursorPacket(cursorItem);
|
||||||
player.getPlayerConnection().sendPacket(setSlotPacket);
|
player.sendPacket(setSlotPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void setCursor(Player player, AbstractInventory inventory, ItemStack itemStack) {
|
private static void setCursor(Player player, AbstractInventory inventory, ItemStack itemStack) {
|
||||||
|
@ -34,10 +34,9 @@ public class Messenger {
|
|||||||
*/
|
*/
|
||||||
public static boolean sendMessage(@NotNull Player player, @NotNull Component message, @NotNull ChatPosition position, @Nullable UUID uuid) {
|
public static boolean sendMessage(@NotNull Player player, @NotNull Component message, @NotNull ChatPosition position, @Nullable UUID uuid) {
|
||||||
if (getChatMessageType(player).accepts(position)) {
|
if (getChatMessageType(player).accepts(position)) {
|
||||||
player.getPlayerConnection().sendPacket(new ChatMessagePacket(message, position, uuid));
|
player.sendPacket(new ChatMessagePacket(message, position, uuid));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +80,7 @@ public class Messenger {
|
|||||||
* @param player the player
|
* @param player the player
|
||||||
*/
|
*/
|
||||||
public static void sendRejectionMessage(@NotNull Player player) {
|
public static void sendRejectionMessage(@NotNull Player player) {
|
||||||
player.getPlayerConnection().sendPacket(CANNOT_SEND_PACKET);
|
player.sendPacket(CANNOT_SEND_PACKET);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -278,9 +278,8 @@ public final class ConnectionManager {
|
|||||||
for (Player player : getOnlinePlayers()) {
|
for (Player player : getOnlinePlayers()) {
|
||||||
final long lastKeepAlive = tickStart - player.getLastKeepAlive();
|
final long lastKeepAlive = tickStart - player.getLastKeepAlive();
|
||||||
if (lastKeepAlive > KEEP_ALIVE_DELAY && player.didAnswerKeepAlive()) {
|
if (lastKeepAlive > KEEP_ALIVE_DELAY && player.didAnswerKeepAlive()) {
|
||||||
final PlayerConnection playerConnection = player.getPlayerConnection();
|
|
||||||
player.refreshKeepAlive(tickStart);
|
player.refreshKeepAlive(tickStart);
|
||||||
playerConnection.sendPacket(keepAlivePacket);
|
player.sendPacket(keepAlivePacket);
|
||||||
} else if (lastKeepAlive >= KEEP_ALIVE_KICK) {
|
} else if (lastKeepAlive >= KEEP_ALIVE_KICK) {
|
||||||
player.kick(TIMEOUT_TEXT);
|
player.kick(TIMEOUT_TEXT);
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package net.minestom.server.scoreboard;
|
|||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.network.packet.server.play.ScoreboardObjectivePacket;
|
import net.minestom.server.network.packet.server.play.ScoreboardObjectivePacket;
|
||||||
import net.minestom.server.network.player.PlayerConnection;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -56,29 +55,22 @@ public class BelowNameTag implements Scoreboard {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean addViewer(@NotNull Player player) {
|
public boolean addViewer(@NotNull Player player) {
|
||||||
boolean result = this.viewers.add(player);
|
final boolean result = this.viewers.add(player);
|
||||||
PlayerConnection connection = player.getPlayerConnection();
|
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
connection.sendPacket(this.scoreboardObjectivePacket);
|
player.sendPacket(this.scoreboardObjectivePacket);
|
||||||
connection.sendPacket(this.getDisplayScoreboardPacket((byte) 2));
|
player.sendPacket(this.getDisplayScoreboardPacket((byte) 2));
|
||||||
|
|
||||||
player.setBelowNameTag(this);
|
player.setBelowNameTag(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean removeViewer(@NotNull Player player) {
|
public boolean removeViewer(@NotNull Player player) {
|
||||||
boolean result = this.viewers.remove(player);
|
final boolean result = this.viewers.remove(player);
|
||||||
PlayerConnection connection = player.getPlayerConnection();
|
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
connection.sendPacket(this.getDestructionObjectivePacket());
|
player.sendPacket(this.getDestructionObjectivePacket());
|
||||||
player.setBelowNameTag(null);
|
player.setBelowNameTag(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,7 +8,6 @@ import net.minestom.server.network.packet.server.play.DisplayScoreboardPacket;
|
|||||||
import net.minestom.server.network.packet.server.play.ScoreboardObjectivePacket;
|
import net.minestom.server.network.packet.server.play.ScoreboardObjectivePacket;
|
||||||
import net.minestom.server.network.packet.server.play.TeamsPacket;
|
import net.minestom.server.network.packet.server.play.TeamsPacket;
|
||||||
import net.minestom.server.network.packet.server.play.UpdateScorePacket;
|
import net.minestom.server.network.packet.server.play.UpdateScorePacket;
|
||||||
import net.minestom.server.network.player.PlayerConnection;
|
|
||||||
import net.minestom.server.utils.validate.Check;
|
import net.minestom.server.utils.validate.Check;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@ -208,17 +207,15 @@ public class Sidebar implements Scoreboard {
|
|||||||
@Override
|
@Override
|
||||||
public boolean addViewer(@NotNull Player player) {
|
public boolean addViewer(@NotNull Player player) {
|
||||||
final boolean result = this.viewers.add(player);
|
final boolean result = this.viewers.add(player);
|
||||||
PlayerConnection playerConnection = player.getPlayerConnection();
|
|
||||||
|
|
||||||
ScoreboardObjectivePacket scoreboardObjectivePacket = this.getCreationObjectivePacket(this.title, ScoreboardObjectivePacket.Type.INTEGER);
|
ScoreboardObjectivePacket scoreboardObjectivePacket = this.getCreationObjectivePacket(this.title, ScoreboardObjectivePacket.Type.INTEGER);
|
||||||
DisplayScoreboardPacket displayScoreboardPacket = this.getDisplayScoreboardPacket((byte) 1);
|
DisplayScoreboardPacket displayScoreboardPacket = this.getDisplayScoreboardPacket((byte) 1);
|
||||||
|
|
||||||
playerConnection.sendPacket(scoreboardObjectivePacket); // Creative objective
|
player.sendPacket(scoreboardObjectivePacket); // Creative objective
|
||||||
playerConnection.sendPacket(displayScoreboardPacket); // Show sidebar scoreboard (wait for scores packet)
|
player.sendPacket(displayScoreboardPacket); // Show sidebar scoreboard (wait for scores packet)
|
||||||
|
|
||||||
for (ScoreboardLine line : lines) {
|
for (ScoreboardLine line : lines) {
|
||||||
playerConnection.sendPacket(line.sidebarTeam.getCreationPacket());
|
player.sendPacket(line.sidebarTeam.getCreationPacket());
|
||||||
playerConnection.sendPacket(line.getScoreCreationPacket(objectiveName));
|
player.sendPacket(line.getScoreCreationPacket(objectiveName));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -226,13 +223,11 @@ public class Sidebar implements Scoreboard {
|
|||||||
@Override
|
@Override
|
||||||
public boolean removeViewer(@NotNull Player player) {
|
public boolean removeViewer(@NotNull Player player) {
|
||||||
final boolean result = this.viewers.remove(player);
|
final boolean result = this.viewers.remove(player);
|
||||||
PlayerConnection playerConnection = player.getPlayerConnection();
|
|
||||||
ScoreboardObjectivePacket scoreboardObjectivePacket = this.getDestructionObjectivePacket();
|
ScoreboardObjectivePacket scoreboardObjectivePacket = this.getDestructionObjectivePacket();
|
||||||
playerConnection.sendPacket(scoreboardObjectivePacket);
|
player.sendPacket(scoreboardObjectivePacket);
|
||||||
|
|
||||||
for (ScoreboardLine line : lines) {
|
for (ScoreboardLine line : lines) {
|
||||||
playerConnection.sendPacket(line.getScoreDestructionPacket(objectiveName)); // Is it necessary?
|
player.sendPacket(line.getScoreDestructionPacket(objectiveName)); // Is it necessary?
|
||||||
playerConnection.sendPacket(line.sidebarTeam.getDestructionPacket());
|
player.sendPacket(line.sidebarTeam.getDestructionPacket());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package net.minestom.server.scoreboard;
|
|||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.network.packet.server.play.ScoreboardObjectivePacket;
|
import net.minestom.server.network.packet.server.play.ScoreboardObjectivePacket;
|
||||||
import net.minestom.server.network.player.PlayerConnection;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -52,26 +51,20 @@ public class TabList implements Scoreboard {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean addViewer(@NotNull Player player) {
|
public boolean addViewer(@NotNull Player player) {
|
||||||
boolean result = this.viewers.add(player);
|
final boolean result = this.viewers.add(player);
|
||||||
PlayerConnection connection = player.getPlayerConnection();
|
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
connection.sendPacket(this.getCreationObjectivePacket(Component.empty(), this.type));
|
player.sendPacket(this.getCreationObjectivePacket(Component.empty(), this.type));
|
||||||
connection.sendPacket(this.getDisplayScoreboardPacket((byte) 0));
|
player.sendPacket(this.getDisplayScoreboardPacket((byte) 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean removeViewer(@NotNull Player player) {
|
public boolean removeViewer(@NotNull Player player) {
|
||||||
boolean result = this.viewers.remove(player);
|
final boolean result = this.viewers.remove(player);
|
||||||
PlayerConnection connection = player.getPlayerConnection();
|
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
connection.sendPacket(this.getDestructionObjectivePacket());
|
player.sendPacket(this.getDestructionObjectivePacket());
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,14 +79,14 @@ public final class PacketUtils {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("OverrideOnly") // we need to access the audiences inside ForwardingAudience
|
@SuppressWarnings("OverrideOnly") // we need to access the audiences inside ForwardingAudience
|
||||||
public static void sendPacket(@NotNull Audience audience, @NotNull ServerPacket packet) {
|
public static void sendPacket(@NotNull Audience audience, @NotNull ServerPacket packet) {
|
||||||
if (audience instanceof Player) {
|
if (audience instanceof Player player) {
|
||||||
((Player) audience).getPlayerConnection().sendPacket(packet);
|
player.sendPacket(packet);
|
||||||
} else if (audience instanceof PacketGroupingAudience) {
|
} else if (audience instanceof PacketGroupingAudience groupingAudience) {
|
||||||
PacketUtils.sendGroupedPacket(((PacketGroupingAudience) audience).getPlayers(), packet);
|
PacketUtils.sendGroupedPacket(groupingAudience.getPlayers(), packet);
|
||||||
} else if (audience instanceof ForwardingAudience.Single) {
|
} else if (audience instanceof ForwardingAudience.Single singleAudience) {
|
||||||
PacketUtils.sendPacket(((ForwardingAudience.Single) audience).audience(), packet);
|
PacketUtils.sendPacket(singleAudience.audience(), packet);
|
||||||
} else if (audience instanceof ForwardingAudience) {
|
} else if (audience instanceof ForwardingAudience forwardingAudience) {
|
||||||
for (Audience member : ((ForwardingAudience) audience).audiences()) {
|
for (Audience member : forwardingAudience.audiences()) {
|
||||||
PacketUtils.sendPacket(member, packet);
|
PacketUtils.sendPacket(member, packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user