Search in sources :

Example 1 with SpongeUser

use of org.spongepowered.common.entity.player.SpongeUser in project SpongeCommon by SpongePowered.

the class MixinTraitInventory method getPlugin.

@Override
public PluginContainer getPlugin() {
    if (this.plugin == null) {
        Object base = this;
        PluginContainer container;
        if (base instanceof CarriedInventory) {
            final Optional<?> carrier = ((CarriedInventory<?>) base).getCarrier();
            if (carrier.isPresent()) {
                base = carrier.get();
            }
        }
        if (base instanceof TileEntity) {
            final String id = ((TileEntity) base).getBlock().getType().getId();
            final String pluginId = id.substring(0, id.indexOf(":"));
            container = Sponge.getPluginManager().getPlugin(pluginId).orElseThrow(() -> new AssertionError("Missing plugin " + pluginId + " for block " + id));
        } else if (base instanceof Entity) {
            final String id = ((Entity) base).getType().getId();
            final String pluginId = id.substring(0, id.indexOf(":"));
            container = Sponge.getPluginManager().getPlugin(pluginId).orElseThrow(() -> new AssertionError("Missing plugin " + pluginId + " for entity " + id + " (" + this.getClass().getName() + ")"));
        } else if (base instanceof SpongeUser) {
            container = SpongeImpl.getMinecraftPlugin();
        } else {
            container = Sponge.getPluginManager().getPlugin(SpongeImplHooks.getModIdFromClass(this.getClass())).orElseGet(() -> {
                SpongeImpl.getLogger().warn("Unknown plugin for " + this);
                return SpongeImpl.getMinecraftPlugin();
            });
        }
        this.plugin = container;
    }
    return this.plugin;
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) Entity(org.spongepowered.api.entity.Entity) PluginContainer(org.spongepowered.api.plugin.PluginContainer) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) SpongeUser(org.spongepowered.common.entity.player.SpongeUser)

Example 2 with SpongeUser

use of org.spongepowered.common.entity.player.SpongeUser in project SpongeCommon by SpongePowered.

the class MixinPlayerList method initializeConnectionToPlayer.

public void initializeConnectionToPlayer(NetworkManager netManager, EntityPlayerMP playerIn, @Nullable NetHandlerPlayServer handler) {
    GameProfile gameprofile = playerIn.getGameProfile();
    PlayerProfileCache playerprofilecache = this.mcServer.getPlayerProfileCache();
    GameProfile gameprofile1 = playerprofilecache.getProfileByUUID(gameprofile.getId());
    String s = gameprofile1 == null ? gameprofile.getName() : gameprofile1.getName();
    playerprofilecache.addEntry(gameprofile);
    // Sponge start - save changes to offline User before reading player data
    SpongeUser user = (SpongeUser) ((IMixinEntityPlayerMP) playerIn).getUserObject();
    if (SpongeUser.dirtyUsers.contains(user)) {
        user.save();
    }
    // Sponge end
    NBTTagCompound nbttagcompound = this.readPlayerDataFromFile(playerIn);
    WorldServer worldServer = this.mcServer.getWorld(playerIn.dimension);
    int actualDimensionId = ((IMixinWorldServer) worldServer).getDimensionId();
    BlockPos spawnPos = null;
    // Join data
    Optional<Instant> firstJoined = SpongePlayerDataHandler.getFirstJoined(playerIn.getUniqueID());
    Instant lastJoined = Instant.now();
    SpongePlayerDataHandler.setPlayerInfo(playerIn.getUniqueID(), firstJoined.orElse(lastJoined), lastJoined);
    if (actualDimensionId != playerIn.dimension) {
        SpongeImpl.getLogger().warn("Player [{}] has attempted to login to unloaded world [{}]. This is not safe so we have moved them to " + "the default world's spawn point.", playerIn.getName(), playerIn.dimension);
        if (!firstJoined.isPresent()) {
            spawnPos = SpongeImplHooks.getRandomizedSpawnPoint(worldServer);
        } else {
            spawnPos = worldServer.getSpawnPoint();
        }
        playerIn.dimension = actualDimensionId;
        playerIn.setPosition(spawnPos.getX(), spawnPos.getY(), spawnPos.getZ());
    }
    // Sponge start - fire login event
    @Nullable String kickReason = allowUserToConnect(netManager.getRemoteAddress(), gameprofile);
    Text disconnectMessage;
    if (kickReason != null) {
        disconnectMessage = SpongeTexts.fromLegacy(kickReason);
    } else {
        disconnectMessage = Text.of("You are not allowed to log in to this server.");
    }
    Player player = (Player) playerIn;
    Transform<World> fromTransform = player.getTransform().setExtent((World) worldServer);
    Sponge.getCauseStackManager().pushCause(player);
    ClientConnectionEvent.Login loginEvent = SpongeEventFactory.createClientConnectionEventLogin(Sponge.getCauseStackManager().getCurrentCause(), fromTransform, fromTransform, (RemoteConnection) netManager, new MessageEvent.MessageFormatter(disconnectMessage), (org.spongepowered.api.profile.GameProfile) gameprofile, player, false);
    if (kickReason != null) {
        loginEvent.setCancelled(true);
    }
    if (SpongeImpl.postEvent(loginEvent)) {
        Sponge.getCauseStackManager().popCause();
        disconnectClient(netManager, loginEvent.isMessageCancelled() ? Optional.empty() : Optional.of(loginEvent.getMessage()), gameprofile);
        return;
    }
    Sponge.getCauseStackManager().popCause();
    // Sponge end
    worldServer = (WorldServer) loginEvent.getToTransform().getExtent();
    double x = loginEvent.getToTransform().getPosition().getX();
    double y = loginEvent.getToTransform().getPosition().getY();
    double z = loginEvent.getToTransform().getPosition().getZ();
    float pitch = (float) loginEvent.getToTransform().getPitch();
    float yaw = (float) loginEvent.getToTransform().getYaw();
    playerIn.dimension = ((IMixinWorldServer) worldServer).getDimensionId();
    playerIn.setWorld(worldServer);
    playerIn.interactionManager.setWorld((WorldServer) playerIn.world);
    playerIn.setPositionAndRotation(x, y, z, yaw, pitch);
    // make sure the chunk is loaded for login
    worldServer.getChunkProvider().loadChunk(loginEvent.getToTransform().getLocation().getChunkPosition().getX(), loginEvent.getToTransform().getLocation().getChunkPosition().getZ());
    // Sponge end
    String s1 = "local";
    if (netManager.getRemoteAddress() != null) {
        s1 = netManager.getRemoteAddress().toString();
    }
    final WorldInfo worldinfo = worldServer.getWorldInfo();
    final BlockPos spawnBlockPos = worldServer.getSpawnPoint();
    this.setPlayerGameTypeBasedOnOther(playerIn, null, worldServer);
    // Sponge start
    if (handler == null) {
        // Create the handler here (so the player's gets set)
        handler = new NetHandlerPlayServer(this.mcServer, netManager, playerIn);
    }
    playerIn.connection = handler;
    // Sponge end
    // Support vanilla clients logging into custom dimensions
    final int dimensionId = WorldManager.getClientDimensionId(playerIn, worldServer);
    // Send dimension registration
    WorldManager.sendDimensionRegistration(playerIn, worldServer.provider);
    handler.sendPacket(new SPacketJoinGame(playerIn.getEntityId(), playerIn.interactionManager.getGameType(), worldinfo.isHardcoreModeEnabled(), dimensionId, worldServer.getDifficulty(), this.getMaxPlayers(), worldinfo.getTerrainType(), worldServer.getGameRules().getBoolean("reducedDebugInfo")));
    handler.sendPacket(new SPacketCustomPayload("MC|Brand", (new PacketBuffer(Unpooled.buffer())).writeString(this.getServerInstance().getServerModName())));
    handler.sendPacket(new SPacketServerDifficulty(worldinfo.getDifficulty(), worldinfo.isDifficultyLocked()));
    handler.sendPacket(new SPacketSpawnPosition(spawnBlockPos));
    handler.sendPacket(new SPacketPlayerAbilities(playerIn.capabilities));
    handler.sendPacket(new SPacketHeldItemChange(playerIn.inventory.currentItem));
    this.updatePermissionLevel(playerIn);
    playerIn.getStatFile().markAllDirty();
    playerIn.getRecipeBook().init(playerIn);
    this.mcServer.refreshStatusNextTick();
    handler.setPlayerLocation(x, y, z, yaw, pitch);
    this.playerLoggedIn(playerIn);
    // Sponge start - add world name to message
    LOGGER.info(playerIn.getName() + "[" + s1 + "] logged in with entity id " + playerIn.getEntityId() + " in " + worldServer.getWorldInfo().getWorldName() + "(" + ((IMixinWorldServer) worldServer).getDimensionId() + ") at (" + playerIn.posX + ", " + playerIn.posY + ", " + playerIn.posZ + ")");
    // Sponge end
    this.updateTimeAndWeatherForPlayer(playerIn, worldServer);
    // Sponge Start - Use the server's ResourcePack object
    Optional<ResourcePack> pack = ((Server) this.mcServer).getDefaultResourcePack();
    if (pack.isPresent()) {
        ((Player) playerIn).sendResourcePack(pack.get());
    }
    // Sponge End
    // Sponge Start
    // 
    // This sends the objective/score creation packets
    // to the player, without attempting to remove them from their
    // previous scoreboard (which is set in a field initializer).
    // This allows #getWorldScoreboard to function
    // as normal, without causing issues when it is initialized on the client.
    ((IMixinEntityPlayerMP) playerIn).initScoreboard();
    for (PotionEffect potioneffect : playerIn.getActivePotionEffects()) {
        handler.sendPacket(new SPacketEntityEffect(playerIn.getEntityId(), potioneffect));
    }
    if (nbttagcompound != null) {
        if (nbttagcompound.hasKey("RootVehicle", 10)) {
            NBTTagCompound nbttagcompound1 = nbttagcompound.getCompoundTag("RootVehicle");
            Entity entity2 = AnvilChunkLoader.readWorldEntity(nbttagcompound1.getCompoundTag("Entity"), worldServer, true);
            if (entity2 != null) {
                UUID uuid = nbttagcompound1.getUniqueId("Attach");
                if (entity2.getUniqueID().equals(uuid)) {
                    playerIn.startRiding(entity2, true);
                } else {
                    for (Entity entity : entity2.getRecursivePassengers()) {
                        if (entity.getUniqueID().equals(uuid)) {
                            playerIn.startRiding(entity, true);
                            break;
                        }
                    }
                }
                if (!playerIn.isRiding()) {
                    LOGGER.warn("Couldn\'t reattach entity to player");
                    worldServer.removeEntityDangerously(entity2);
                    for (Entity entity3 : entity2.getRecursivePassengers()) {
                        worldServer.removeEntityDangerously(entity3);
                    }
                }
            }
        } else if (nbttagcompound.hasKey("Riding", 10)) {
            Entity entity1 = AnvilChunkLoader.readWorldEntity(nbttagcompound.getCompoundTag("Riding"), worldServer, true);
            if (entity1 != null) {
                playerIn.startRiding(entity1, true);
            }
        }
    }
    playerIn.addSelfToInternalCraftingInventory();
    TextComponentTranslation chatcomponenttranslation;
    if (!playerIn.getName().equalsIgnoreCase(s)) {
        chatcomponenttranslation = new TextComponentTranslation("multiplayer.player.joined.renamed", playerIn.getDisplayName(), s);
    } else {
        chatcomponenttranslation = new TextComponentTranslation("multiplayer.player.joined", playerIn.getDisplayName());
    }
    chatcomponenttranslation.getStyle().setColor(TextFormatting.YELLOW);
    // Fire PlayerJoinEvent
    Text originalMessage = SpongeTexts.toText(chatcomponenttranslation);
    MessageChannel originalChannel = player.getMessageChannel();
    Sponge.getCauseStackManager().pushCause(player);
    final ClientConnectionEvent.Join event = SpongeEventFactory.createClientConnectionEventJoin(Sponge.getCauseStackManager().getCurrentCause(), originalChannel, Optional.of(originalChannel), new MessageEvent.MessageFormatter(originalMessage), player, false);
    SpongeImpl.postEvent(event);
    Sponge.getCauseStackManager().popCause();
    // Send to the channel
    if (!event.isMessageCancelled()) {
        event.getChannel().ifPresent(channel -> channel.send(player, event.getMessage()));
    }
// Sponge end
}
Also used : SPacketPlayerAbilities(net.minecraft.network.play.server.SPacketPlayerAbilities) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Entity(net.minecraft.entity.Entity) SPacketSpawnPosition(net.minecraft.network.play.server.SPacketSpawnPosition) MinecraftServer(net.minecraft.server.MinecraftServer) NetHandlerPlayServer(net.minecraft.network.NetHandlerPlayServer) Server(org.spongepowered.api.Server) WorldServer(net.minecraft.world.WorldServer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) PlayerProfileCache(net.minecraft.server.management.PlayerProfileCache) PotionEffect(net.minecraft.potion.PotionEffect) MessageEvent(org.spongepowered.api.event.message.MessageEvent) ResourcePack(org.spongepowered.api.resourcepack.ResourcePack) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) WorldServer(net.minecraft.world.WorldServer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) IMixinEntityPlayerMP(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP) World(org.spongepowered.api.world.World) SPacketServerDifficulty(net.minecraft.network.play.server.SPacketServerDifficulty) SPacketHeldItemChange(net.minecraft.network.play.server.SPacketHeldItemChange) WorldInfo(net.minecraft.world.storage.WorldInfo) BlockPos(net.minecraft.util.math.BlockPos) UUID(java.util.UUID) PacketBuffer(net.minecraft.network.PacketBuffer) Player(org.spongepowered.api.entity.living.player.Player) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) Instant(java.time.Instant) ClientConnectionEvent(org.spongepowered.api.event.network.ClientConnectionEvent) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) Text(org.spongepowered.api.text.Text) NetHandlerPlayServer(net.minecraft.network.NetHandlerPlayServer) SpongeUser(org.spongepowered.common.entity.player.SpongeUser) SPacketJoinGame(net.minecraft.network.play.server.SPacketJoinGame) SPacketCustomPayload(net.minecraft.network.play.server.SPacketCustomPayload) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) GameProfile(com.mojang.authlib.GameProfile) SPacketEntityEffect(net.minecraft.network.play.server.SPacketEntityEffect) Nullable(javax.annotation.Nullable)

Example 3 with SpongeUser

use of org.spongepowered.common.entity.player.SpongeUser in project SpongeCommon by SpongePowered.

the class MixinPlayerList method onPlayerLogOut.

@Inject(method = "playerLoggedOut(Lnet/minecraft/entity/player/EntityPlayerMP;)V", at = @At("HEAD"))
private void onPlayerLogOut(EntityPlayerMP player, CallbackInfo ci) {
    // Synchronise with user object
    NBTTagCompound nbt = new NBTTagCompound();
    player.writeToNBT(nbt);
    ((SpongeUser) ((IMixinEntityPlayerMP) player).getUserObject()).readFromNbt(nbt);
    // Remove player reference from scoreboard
    ((IMixinServerScoreboard) ((Player) player).getScoreboard()).removePlayer(player, false);
}
Also used : IMixinServerScoreboard(org.spongepowered.common.interfaces.IMixinServerScoreboard) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) SpongeUser(org.spongepowered.common.entity.player.SpongeUser) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 4 with SpongeUser

use of org.spongepowered.common.entity.player.SpongeUser in project SpongeCommon by SpongePowered.

the class UserDiscoverer method create.

static User create(GameProfile profile) {
    User user = (User) new SpongeUser(profile);
    userCache.put(profile.getId(), user);
    return user;
}
Also used : SpongeUser(org.spongepowered.common.entity.player.SpongeUser) User(org.spongepowered.api.entity.living.player.User) SpongeUser(org.spongepowered.common.entity.player.SpongeUser)

Example 5 with SpongeUser

use of org.spongepowered.common.entity.player.SpongeUser in project SpongeCommon by SpongePowered.

the class MixinSpongeUserInventory method onConstructed.

@Inject(method = "<init>*", at = @At("RETURN"), remap = false)
private void onConstructed(SpongeUser playerIn, CallbackInfo ci) {
    // We only care about Server inventories
    this.inventory = new IInventoryFabric((IInventory) this);
    this.slots = new SlotCollection.Builder().add(this.mainInventory.size()).add(this.offHandInventory.size()).add(this.armorInventory.size(), EquipmentSlotAdapter.class).add(this.getSizeInventory() - this.mainInventory.size() - this.offHandInventory.size() - this.armorInventory.size()).build();
    this.carrier = ((User) playerIn);
    this.lens = new PlayerInventoryLens(this, this.slots);
}
Also used : IInventory(net.minecraft.inventory.IInventory) IInventoryFabric(org.spongepowered.common.item.inventory.lens.impl.fabric.IInventoryFabric) EquipmentSlotAdapter(org.spongepowered.common.item.inventory.adapter.impl.slots.EquipmentSlotAdapter) SpongeUser(org.spongepowered.common.entity.player.SpongeUser) User(org.spongepowered.api.entity.living.player.User) PlayerInventoryLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.PlayerInventoryLens) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

SpongeUser (org.spongepowered.common.entity.player.SpongeUser)6 User (org.spongepowered.api.entity.living.player.User)3 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)2 Inject (org.spongepowered.asm.mixin.injection.Inject)2 GameProfile (com.mojang.authlib.GameProfile)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Instant (java.time.Instant)1 UUID (java.util.UUID)1 Nullable (javax.annotation.Nullable)1 Entity (net.minecraft.entity.Entity)1 IInventory (net.minecraft.inventory.IInventory)1 NetHandlerPlayServer (net.minecraft.network.NetHandlerPlayServer)1 PacketBuffer (net.minecraft.network.PacketBuffer)1 SPacketCustomPayload (net.minecraft.network.play.server.SPacketCustomPayload)1 SPacketEntityEffect (net.minecraft.network.play.server.SPacketEntityEffect)1 SPacketHeldItemChange (net.minecraft.network.play.server.SPacketHeldItemChange)1 SPacketJoinGame (net.minecraft.network.play.server.SPacketJoinGame)1 SPacketPlayerAbilities (net.minecraft.network.play.server.SPacketPlayerAbilities)1