Search in sources :

Example 6 with ResourcePack

use of org.spongepowered.api.resourcepack.ResourcePack in project SpongeCommon by SpongePowered.

the class ResourcePackState method unwind.

@Override
public void unwind(BasicPacketContext phaseContext) {
    final net.minecraft.server.level.ServerPlayer player = phaseContext.getPacketPlayer();
    final ServerGamePacketListenerImpl connection = player.connection;
    final ServerGamePacketListenerImplBridge mixinHandler = (ServerGamePacketListenerImplBridge) connection;
    final ServerboundResourcePackPacketAccessor resource = phaseContext.getPacket();
    final ResourcePackStatusEvent.ResourcePackStatus status;
    ResourcePack pack;
    switch(resource.accessor$action()) {
        case ACCEPTED:
            pack = mixinHandler.bridge$popReceivedResourcePack(true);
            status = ResourcePackStatusEvent.ResourcePackStatus.ACCEPTED;
            break;
        case DECLINED:
            pack = mixinHandler.bridge$popReceivedResourcePack(false);
            status = ResourcePackStatusEvent.ResourcePackStatus.DECLINED;
            break;
        case SUCCESSFULLY_LOADED:
            pack = mixinHandler.bridge$popAcceptedResourcePack();
            status = ResourcePackStatusEvent.ResourcePackStatus.SUCCESSFULLY_LOADED;
            break;
        case FAILED_DOWNLOAD:
            pack = mixinHandler.bridge$popAcceptedResourcePack();
            status = ResourcePackStatusEvent.ResourcePackStatus.FAILED;
            break;
        default:
            throw new AssertionError();
    }
    if (pack == null) {
        return;
    }
    SpongeCommon.post(SpongeEventFactory.createResourcePackStatusEvent(PhaseTracker.getCauseStackManager().currentCause(), pack, (ServerPlayer) player, status));
}
Also used : ResourcePack(org.spongepowered.api.resourcepack.ResourcePack) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) ServerGamePacketListenerImplBridge(org.spongepowered.common.bridge.server.network.ServerGamePacketListenerImplBridge) ServerboundResourcePackPacketAccessor(org.spongepowered.common.accessor.network.protocol.game.ServerboundResourcePackPacketAccessor) ServerGamePacketListenerImpl(net.minecraft.server.network.ServerGamePacketListenerImpl) ResourcePackStatusEvent(org.spongepowered.api.event.entity.living.player.ResourcePackStatusEvent)

Example 7 with ResourcePack

use of org.spongepowered.api.resourcepack.ResourcePack in project LanternServer by LanternPowered.

the class LanternResourcePackFactory method fromUri.

private ResourcePack fromUri(URI uri, boolean unchecked) throws IOException {
    final CacheKey key = new CacheKey(uri, unchecked);
    if (this.resourcePacksByKey.containsKey(key)) {
        return this.resourcePacksByKey.get(key);
    }
    final String path = uri.toString();
    final String plainPath = path.replaceAll("[^\\p{L}\\p{Nd}]+", "");
    String hash = null;
    String id = "{URI:" + path;
    if (!unchecked) {
        final URL url;
        if (path.startsWith("level://")) {
            final String path0 = path.replaceFirst("level://", "");
            final Path file = this.levelPacksFolder.resolve(path0);
            if (!Files.exists(file)) {
                throw new FileNotFoundException("Cannot find the file: \"" + file.toAbsolutePath() + "\" which" + " is required to generate the hash for \"" + path + "\"");
            }
            url = PathUtils.toURL(file);
            uri = file.toUri();
        } else {
            url = PathUtils.toURL(uri);
        }
        try (InputStream is = url.openStream()) {
            hash = Hashing.sha1().hashBytes(ByteStreams.toByteArray(is)).toString();
        }
        id += ";Hash:" + hash;
    }
    id += "}";
    final ResourcePack resourcePack = new LanternResourcePack(uri, plainPath, id, hash);
    this.resourcePacks.put(id, resourcePack);
    this.resourcePacksByKey.put(key, resourcePack);
    return resourcePack;
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) ResourcePack(org.spongepowered.api.resourcepack.ResourcePack) FileNotFoundException(java.io.FileNotFoundException) URL(java.net.URL)

Example 8 with ResourcePack

use of org.spongepowered.api.resourcepack.ResourcePack in project LanternServer by LanternPowered.

the class ResourcePackSendQueue method poll.

public Optional<ResourcePack> poll(ResourcePackStatusEvent.ResourcePackStatus status) {
    synchronized (this.queue) {
        final ResourcePack resourcePack = this.waitingForResponse;
        // status message for this resource pack
        if (!status.wasSuccessful().isPresent()) {
            this.counter = -1;
            return Optional.ofNullable(resourcePack);
        }
        if (!this.queue.isEmpty()) {
            send(this.queue.remove(0));
            this.counter = 0;
        } else {
            this.waitingForResponse = null;
        }
        return Optional.ofNullable(resourcePack);
    }
}
Also used : MessagePlayOutSendResourcePack(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSendResourcePack) ResourcePack(org.spongepowered.api.resourcepack.ResourcePack)

Example 9 with ResourcePack

use of org.spongepowered.api.resourcepack.ResourcePack in project LanternServer by LanternPowered.

the class HandlerPlayInResourcePackStatus method handle.

@Override
public void handle(NetworkContext context, MessagePlayInResourcePackStatus message) {
    final Optional<ResourcePack> resourcePack = context.getSession().getPlayer().getResourcePackSendQueue().poll(message.getStatus());
    final LanternPlayer player = context.getSession().getPlayer();
    if (!resourcePack.isPresent()) {
        Lantern.getLogger().warn("{} received a unexpected resource pack status message ({}), no resource pack was pending", player.getName(), message.getStatus());
        return;
    }
    final CauseStack causeStack = CauseStack.current();
    try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
        frame.addContext(EventContextKeys.PLAYER, player);
        Sponge.getEventManager().post(SpongeEventFactory.createResourcePackStatusEvent(frame.getCurrentCause(), resourcePack.get(), player, message.getStatus()));
    }
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) ResourcePack(org.spongepowered.api.resourcepack.ResourcePack) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Aggregations

ResourcePack (org.spongepowered.api.resourcepack.ResourcePack)9 Player (org.spongepowered.api.entity.living.player.Player)3 NetHandlerPlayServer (net.minecraft.network.NetHandlerPlayServer)2 GameProfile (com.mojang.authlib.GameProfile)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 Instant (java.time.Instant)1 UUID (java.util.UUID)1 Nullable (javax.annotation.Nullable)1 Entity (net.minecraft.entity.Entity)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 PacketBuffer (net.minecraft.network.PacketBuffer)1 CPacketPlayer (net.minecraft.network.play.client.CPacketPlayer)1 CPacketResourcePackStatus (net.minecraft.network.play.client.CPacketResourcePackStatus)1 SPacketCustomPayload (net.minecraft.network.play.server.SPacketCustomPayload)1 SPacketEntityEffect (net.minecraft.network.play.server.SPacketEntityEffect)1