use of net.glowstone.net.message.play.game.UserListItemMessage.Entry in project Glowstone by GlowstoneMC.
the class GlowSession method setPlayer.
// //////////////////////////////////////////////////////////////////////////
// Player and state management
/**
* Sets the player associated with this session.
*
* @param profile The player's profile with name and UUID information.
* @throws IllegalStateException if there is already a player associated with this
* session.
*/
public void setPlayer(GlowPlayerProfile profile) {
if (player != null) {
throw new IllegalStateException("Cannot set player twice");
}
// isActive check here in case player disconnected during authentication
if (!isActive()) {
// no need to call onDisconnect() since it only does anything if there's a player set
return;
}
// initialize the player
PlayerReader reader = server.getPlayerDataService().beginReadingData(profile.getId());
player = new GlowPlayer(this, profile, reader);
finalizeLogin(profile);
// but before the GlowPlayer initialization was completed
if (!isActive()) {
reader.close();
onDisconnect();
return;
}
// Kick other players with the same UUID
for (GlowPlayer other : getServer().getRawOnlinePlayers()) {
if (other != player && other.getUniqueId().equals(player.getUniqueId())) {
other.getSession().disconnect("You logged in from another location.", true);
break;
}
}
// login event
PlayerLoginEvent event = EventFactory.getInstance().onPlayerLogin(player, virtualHost.toString());
if (event.getResult() != Result.ALLOWED) {
disconnect(event.getKickMessage(), true);
return;
}
// joins the player
player.join(this, reader);
player.getWorld().getRawPlayers().add(player);
online = true;
GlowServer.logger.info(player.getName() + " [" + address + "] connected, UUID: " + UuidUtils.toString(player.getUniqueId()));
// message and user list
String message = EventFactory.getInstance().onPlayerJoin(player).getJoinMessage();
if (message != null && !message.isEmpty()) {
server.broadcastMessage(message);
}
Message addMessage = new UserListItemMessage(Action.ADD_PLAYER, player.getUserListEntry());
List<Entry> entries = new ArrayList<>();
for (GlowPlayer other : server.getRawOnlinePlayers()) {
if (other != player && other.canSee(player)) {
other.getSession().send(addMessage);
}
if (player.canSee(other)) {
entries.add(other.getUserListEntry());
}
}
send(new UserListItemMessage(Action.ADD_PLAYER, entries));
send(server.createAdvancementsMessage(false, Collections.emptyList(), player));
}
use of net.glowstone.net.message.play.game.UserListItemMessage.Entry in project Glowstone by GlowstoneMC.
the class UserListItemCodec method encode.
@Override
public ByteBuf encode(ByteBuf buf, UserListItemMessage message) throws IOException {
Action action = message.getAction();
List<Entry> entries = message.getEntries();
ByteBufUtils.writeVarInt(buf, message.getAction().ordinal());
ByteBufUtils.writeVarInt(buf, entries.size());
for (Entry entry : entries) {
GlowBufUtils.writeUuid(buf, entry.uuid);
// todo: implement the rest of the actions
switch(action) {
case ADD_PLAYER:
// this code is somewhat saddening
ByteBufUtils.writeUTF8(buf, entry.profile.getName());
ByteBufUtils.writeVarInt(buf, entry.profile.getProperties().size());
for (ProfileProperty property : entry.profile.getProperties()) {
ByteBufUtils.writeUTF8(buf, property.getName());
ByteBufUtils.writeUTF8(buf, property.getValue());
buf.writeBoolean(property.isSigned());
if (property.isSigned()) {
ByteBufUtils.writeUTF8(buf, property.getSignature());
}
}
ByteBufUtils.writeVarInt(buf, entry.gameMode);
ByteBufUtils.writeVarInt(buf, entry.ping);
if (entry.displayName != null) {
buf.writeBoolean(true);
GlowBufUtils.writeChat(buf, entry.displayName);
} else {
buf.writeBoolean(false);
}
break;
case UPDATE_GAMEMODE:
ByteBufUtils.writeVarInt(buf, entry.gameMode);
break;
case UPDATE_LATENCY:
ByteBufUtils.writeVarInt(buf, entry.ping);
break;
case UPDATE_DISPLAY_NAME:
if (entry.displayName != null) {
buf.writeBoolean(true);
GlowBufUtils.writeChat(buf, entry.displayName);
} else {
buf.writeBoolean(false);
}
break;
case REMOVE_PLAYER:
// nothing
break;
default:
throw new UnsupportedOperationException("not yet implemented: " + action);
}
}
return buf;
}
use of net.glowstone.net.message.play.game.UserListItemMessage.Entry in project Glowstone by GlowstoneMC.
the class GlowPlayer method pulse.
@Override
public void pulse() {
super.pulse();
incrementStatistic(Statistic.TIME_SINCE_DEATH);
if (usageItem != null) {
if (usageItem.equals(getItemInHand())) {
// todo: implement offhand
if (--usageTime == 0) {
ItemType item = ItemTable.instance().getItem(usageItem.getType());
if (item instanceof ItemFood) {
((ItemFood) item).eat(this, usageItem);
}
}
} else {
usageItem = null;
usageTime = 0;
}
}
if (digging != null) {
pulseDigging();
}
if (exhaustion > 4.0f) {
exhaustion -= 4.0f;
if (saturation > 0f) {
saturation = Math.max(saturation - 1f, 0f);
sendHealth();
} else if (world.getDifficulty() != Difficulty.PEACEFUL) {
FoodLevelChangeEvent event = EventFactory.getInstance().callEvent(new FoodLevelChangeEvent(this, Math.max(foodLevel - 1, 0)));
if (!event.isCancelled()) {
foodLevel = event.getFoodLevel();
}
sendHealth();
}
}
if (getHealth() < getMaxHealth() && !isDead()) {
if (foodLevel >= 18 && ticksLived % 80 == 0 || world.getDifficulty() == Difficulty.PEACEFUL) {
EntityUtils.heal(this, 1, EntityRegainHealthEvent.RegainReason.SATIATED);
exhaustion = Math.min(exhaustion + 3.0f, 40.0f);
saturation -= 3;
}
}
// Process food level and starvation based on difficulty.
switch(world.getDifficulty()) {
case PEACEFUL:
{
if (foodLevel < 20 && ticksLived % 20 == 0) {
foodLevel++;
}
break;
}
case EASY:
{
if (foodLevel == 0 && getHealth() > 10 && ticksLived % 80 == 0) {
damage(1, DamageCause.STARVATION);
}
break;
}
case NORMAL:
{
if (foodLevel == 0 && getHealth() > 1 && ticksLived % 80 == 0) {
damage(1, DamageCause.STARVATION);
}
break;
}
case HARD:
{
if (foodLevel == 0 && ticksLived % 80 == 0) {
damage(1, DamageCause.STARVATION);
}
break;
}
default:
{
// Do nothing when there are other game difficulties.
}
}
// process ender pearl cooldown, decrease by 1 every game tick.
if (enderPearlCooldown > 0) {
enderPearlCooldown--;
}
// stream world
streamBlocks();
processBlockChanges();
// add to playtime (despite inaccurate name, this counts ticks rather than minutes)
incrementStatistic(Statistic.PLAY_ONE_MINUTE);
if (isSneaking()) {
incrementStatistic(Statistic.SNEAK_TIME);
}
// update inventory
for (InventoryMonitor.Entry entry : invMonitor.getChanges()) {
sendItemChange(entry.slot, entry.item);
}
// send changed metadata
List<MetadataMap.Entry> changes = metadata.getChanges();
if (!changes.isEmpty()) {
session.send(new EntityMetadataMessage(getEntityId(), changes));
}
// Entity IDs are only unique per world, so we can't spawn or teleport between worlds while
// updating them.
worldLock.writeLock().lock();
try {
// update or remove entities
List<GlowEntity> destroyEntities = new LinkedList<>();
for (Iterator<GlowEntity> it = knownEntities.iterator(); it.hasNext(); ) {
GlowEntity entity = it.next();
if (!isWithinDistance(entity) || entity.isRemoved()) {
destroyEntities.add(entity);
} else {
entity.createUpdateMessage(session).forEach(session::send);
}
}
if (!destroyEntities.isEmpty()) {
List<Integer> destroyIds = new ArrayList<>(destroyEntities.size());
for (GlowEntity entity : destroyEntities) {
knownEntities.remove(entity);
destroyIds.add(entity.getEntityId());
}
session.send(new DestroyEntitiesMessage(destroyIds));
}
// add entities
knownChunks.forEach(key -> world.getChunkAt(key.getX(), key.getZ()).getRawEntities().stream().filter(entity -> this != entity && isWithinDistance(entity) && !entity.isDead() && !knownEntities.contains(entity) && !hiddenEntities.contains(entity.getUniqueId())).forEach((entity) -> Bukkit.getScheduler().runTaskAsynchronously(null, () -> {
worldLock.readLock().lock();
try {
knownEntities.add(entity);
} finally {
worldLock.readLock().unlock();
}
entity.createSpawnMessage().forEach(session::send);
entity.createAfterSpawnMessage(session).forEach(session::send);
})));
} finally {
worldLock.writeLock().unlock();
}
if (passengerChanged) {
session.send(new SetPassengerMessage(getEntityId(), getPassengers().stream().mapToInt(Entity::getEntityId).toArray()));
}
getAttributeManager().sendMessages(session);
GlowFishingHook hook = currentFishingHook.get();
if (hook != null) {
// bobber, or if the player stops holding a fishing rod.
if (getInventory().getItemInMainHand().getType() != Material.FISHING_ROD && getInventory().getItemInOffHand().getType() != Material.FISHING_ROD) {
setCurrentFishingHook(null);
}
if (hook.location.distanceSquared(location) > HOOK_MAX_DISTANCE * HOOK_MAX_DISTANCE) {
setCurrentFishingHook(null);
}
}
}
use of net.glowstone.net.message.play.game.UserListItemMessage.Entry in project Glowstone by GlowstoneMC.
the class GlowSession method setPlayer.
/**
* Sets the player associated with this session.
*
* @param profile The player's profile with name and UUID information.
* @throws IllegalStateException if there is already a player associated
* with this session.
*/
public void setPlayer(PlayerProfile profile) {
if (player != null) {
throw new IllegalStateException("Cannot set player twice");
}
// isActive check here in case player disconnected during authentication
if (!isActive()) {
// no need to call onDisconnect() since it only does anything if there's a player set
return;
}
// initialize the player
PlayerReader reader = server.getPlayerDataService().beginReadingData(profile.getUniqueId());
player = new GlowPlayer(this, profile, reader);
finalizeLogin(profile);
// but before the GlowPlayer initialization was completed
if (!isActive()) {
reader.close();
onDisconnect();
return;
}
// Kick other players with the same UUID
for (GlowPlayer other : getServer().getRawOnlinePlayers()) {
if (other != player && other.getUniqueId().equals(player.getUniqueId())) {
other.getSession().disconnect("You logged in from another location.", true);
break;
}
}
// login event
PlayerLoginEvent event = EventFactory.onPlayerLogin(player, hostname);
if (event.getResult() != Result.ALLOWED) {
disconnect(event.getKickMessage(), true);
return;
}
//joins the player
player.join(this, reader);
player.getWorld().getRawPlayers().add(player);
online = true;
GlowServer.logger.info(player.getName() + " [" + address + "] connected, UUID: " + player.getUniqueId());
// message and user list
String message = EventFactory.onPlayerJoin(player).getJoinMessage();
if (message != null && !message.isEmpty()) {
server.broadcastMessage(message);
}
Message addMessage = new UserListItemMessage(Action.ADD_PLAYER, player.getUserListEntry());
List<Entry> entries = new ArrayList<>();
for (GlowPlayer other : server.getRawOnlinePlayers()) {
if (other != player && other.canSee(player)) {
other.getSession().send(addMessage);
}
if (player.canSee(other)) {
entries.add(other.getUserListEntry());
}
}
send(new UserListItemMessage(Action.ADD_PLAYER, entries));
}
Aggregations