Search in sources :

Example 21 with Skin

use of com.github.games647.craftapi.model.skin.Skin in project ChangeSkin by games647.

the class MojangSkinApi method downloadSkin.

public Optional<SkinModel> downloadSkin(UUID ownerUUID) {
    if (crackedUUID.containsKey(ownerUUID)) {
        return Optional.empty();
    }
    // unsigned is needed in order to receive the signature
    String uuidString = UUIDTypeAdapter.toMojangId(ownerUUID);
    try {
        HttpURLConnection httpConnection = getConnection(String.format(SKIN_URL, uuidString));
        if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) {
            crackedUUID.put(ownerUUID, new Object());
            return Optional.empty();
        }
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), StandardCharsets.UTF_8))) {
            TexturesModel texturesModel = gson.fromJson(reader, TexturesModel.class);
            SkinProperty[] properties = texturesModel.getProperties();
            if (properties != null && properties.length > 0) {
                SkinProperty propertiesModel = properties[0];
                // base64 encoded skin data
                String encodedSkin = propertiesModel.getValue();
                String signature = propertiesModel.getSignature();
                return Optional.of(SkinModel.createSkinFromEncoded(encodedSkin, signature));
            }
        }
    } catch (IOException ex) {
        logger.error("Tried downloading skin data of: {} from Mojang", ownerUUID, ex);
    }
    return Optional.empty();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) SkinProperty(com.github.games647.changeskin.core.model.skin.SkinProperty) TexturesModel(com.github.games647.changeskin.core.model.skin.TexturesModel)

Example 22 with Skin

use of com.github.games647.craftapi.model.skin.Skin in project ChangeSkin by games647.

the class ChangeSkinSponge method onInit.

@Listener
public void onInit(GameInitializationEvent initEvent) {
    if (!initialized)
        return;
    CommandManager cmdManager = Sponge.getCommandManager();
    // command and event register
    cmdManager.register(this, injector.getInstance(SelectCommand.class).buildSpec(), "skin-select", "skinselect");
    cmdManager.register(this, injector.getInstance(InfoCommand.class).buildSpec(), "skin-info");
    cmdManager.register(this, injector.getInstance(UploadCommand.class).buildSpec(), "skin-upload");
    cmdManager.register(this, injector.getInstance(SetCommand.class).buildSpec(), "changeskin", "setskin", "skin");
    cmdManager.register(this, injector.getInstance(InvalidateCommand.class).buildSpec(), "skininvalidate", "skin-invalidate");
    Sponge.getEventManager().registerListeners(this, injector.getInstance(LoginListener.class));
    // incoming channel
    ChannelRegistrar channelReg = Sponge.getChannelRegistrar();
    String updateChannelName = new NamespaceKey(ARTIFACT_ID, UPDATE_SKIN_CHANNEL).getCombinedName();
    String permissionChannelName = new NamespaceKey(ARTIFACT_ID, CHECK_PERM_CHANNEL).getCombinedName();
    RawDataChannel updateChannel = channelReg.getOrCreateRaw(this, updateChannelName);
    RawDataChannel permChannel = channelReg.getOrCreateRaw(this, permissionChannelName);
    updateChannel.addListener(Type.SERVER, injector.getInstance(UpdateSkinListener.class));
    permChannel.addListener(Type.SERVER, injector.getInstance(CheckPermissionListener.class));
}
Also used : NamespaceKey(com.github.games647.changeskin.core.message.NamespaceKey) CommandManager(org.spongepowered.api.command.CommandManager) ChannelRegistrar(org.spongepowered.api.network.ChannelRegistrar) UpdateSkinListener(com.github.games647.changeskin.sponge.bungee.UpdateSkinListener) RawDataChannel(org.spongepowered.api.network.ChannelBinding.RawDataChannel) CheckPermissionListener(com.github.games647.changeskin.sponge.bungee.CheckPermissionListener) CheckPermissionListener(com.github.games647.changeskin.sponge.bungee.CheckPermissionListener) UpdateSkinListener(com.github.games647.changeskin.sponge.bungee.UpdateSkinListener) Listener(org.spongepowered.api.event.Listener)

Example 23 with Skin

use of com.github.games647.craftapi.model.skin.Skin in project ChangeSkin by games647.

the class PluginMessageListener method onPermissionSuccess.

private void onPermissionSuccess(PermResultMessage message, ProxiedPlayer invoker) {
    SkinModel targetSkin = message.getSkin();
    UUID receiverUUID = message.getReceiverUUID();
    ProxiedPlayer receiver = ProxyServer.getInstance().getPlayer(receiverUUID);
    if (receiver == null || !receiver.isConnected()) {
        // receiver is not online cancel
        return;
    }
    // add cooldown
    core.getCooldownService().trackPlayer(invoker.getUniqueId());
    // Save the target uuid from the requesting player source
    final UserPreference preferences = core.getStorage().getPreferences(receiver.getUniqueId());
    preferences.setTargetSkin(targetSkin);
    ProxyServer.getInstance().getScheduler().runAsync(plugin, () -> {
        if (core.getStorage().save(targetSkin)) {
            core.getStorage().save(preferences);
        }
    });
    if (core.getConfig().getBoolean("instantSkinChange")) {
        plugin.getApi().applySkin(receiver, targetSkin);
        plugin.sendMessage(invoker, "skin-changed");
    } else {
        plugin.sendMessage(invoker, "skin-changed-no-instant");
    }
}
Also used : ProxiedPlayer(net.md_5.bungee.api.connection.ProxiedPlayer) UserPreference(com.github.games647.changeskin.core.model.UserPreference) UUID(java.util.UUID) SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

Example 24 with Skin

use of com.github.games647.craftapi.model.skin.Skin in project ChangeSkin by games647.

the class InfoCommand method sendSkinDetails.

private void sendSkinDetails(UUID uuid, UserPreference preference) {
    Optional<Player> optPlayer = Sponge.getServer().getPlayer(uuid);
    if (optPlayer.isPresent()) {
        Player player = optPlayer.get();
        Optional<SkinModel> optSkin = preference.getTargetSkin();
        if (optSkin.isPresent()) {
            String template = plugin.getCore().getMessage("skin-info");
            String formatted = formatter.apply(template, optSkin.get());
            Text text = TextSerializers.LEGACY_FORMATTING_CODE.deserialize(formatted);
            player.sendMessage(text);
        } else {
            plugin.sendMessage(player, "skin-not-found");
        }
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Text(org.spongepowered.api.text.Text) SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel)

Example 25 with Skin

use of com.github.games647.craftapi.model.skin.Skin in project ChangeSkin by games647.

the class SkinFormatter method apply.

@Override
public String apply(String template, SkinModel skin) {
    if (template == null) {
        return null;
    }
    int rowId = skin.getRowId();
    UUID ownerId = skin.getProfileId();
    String ownerName = skin.getProfileName();
    long timeFetched = skin.getTimestamp();
    Map<TextureType, TextureModel> textures = skin.getTextures();
    Optional<TextureModel> skinTexture = Optional.ofNullable(textures.get(TextureType.SKIN));
    Optional<TextureModel> capeTexture = Optional.ofNullable(textures.get(TextureType.CAPE));
    String skinUrl = skinTexture.map(TextureModel::getShortUrl).orElse("");
    String slimModel = skinTexture.map(TextureModel::isSlim).map(slim -> "Alex").orElse("Steve");
    String capeUrl = capeTexture.map(TextureModel::getShortUrl).orElse(" - ");
    String timeFormat = timeFormatter.format(Instant.ofEpochMilli(timeFetched));
    return template.replace("{0}", Integer.toString(rowId)).replace("{1}", ownerId.toString()).replace("{2}", ownerName).replace("{3}", timeFormat).replace("{4}", skinUrl).replace("{5}", slimModel).replace("{6}", capeUrl);
}
Also used : TextureModel(com.github.games647.changeskin.core.model.skin.TextureModel) FormatStyle(java.time.format.FormatStyle) SkinModel(com.github.games647.changeskin.core.model.skin.SkinModel) DateTimeFormatter(java.time.format.DateTimeFormatter) Map(java.util.Map) BiFunction(java.util.function.BiFunction) Optional(java.util.Optional) UUID(java.util.UUID) TextureType(com.github.games647.changeskin.core.model.skin.TextureType) Instant(java.time.Instant) TextureModel(com.github.games647.changeskin.core.model.skin.TextureModel) ZoneId(java.time.ZoneId) UUID(java.util.UUID) TextureType(com.github.games647.changeskin.core.model.skin.TextureType)

Aggregations

SkinModel (com.github.games647.changeskin.core.model.skin.SkinModel)11 UserPreference (com.github.games647.changeskin.core.model.UserPreference)10 UUID (java.util.UUID)8 Skin (com.github.games647.craftapi.model.skin.Skin)7 SkinProperty (com.github.games647.craftapi.model.skin.SkinProperty)6 SkinPropertyTest (com.github.games647.craftapi.model.skin.SkinPropertyTest)6 Test (org.junit.Test)6 ProxiedPlayer (net.md_5.bungee.api.connection.ProxiedPlayer)5 Texture (com.github.games647.craftapi.model.skin.Texture)3 EventHandler (net.md_5.bungee.event.EventHandler)3 SkinProperty (com.github.games647.changeskin.core.model.skin.SkinProperty)2 TextureModel (com.github.games647.changeskin.core.model.skin.TextureModel)2 TextureType (com.github.games647.changeskin.core.model.skin.TextureType)2 TexturesModel (com.github.games647.changeskin.core.model.skin.TexturesModel)2 IOException (java.io.IOException)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Optional (java.util.Optional)2