• Chào bạn, hãy đăng ký hoặc đăng nhập để tham gia cùng bọn mình và sử dụng được đầy đủ chức năng của diễn đàn :).
anhcraft

Hướng dẫn Giới thiệu API của SpigotMC

anhcraft

Thành viên BQT
DEVELOPER
THÀNH VIÊN
Tham gia
18/09/2016
Bài viết
3,165
SpigotMC vừa có API mới :D ae có thể dùng nó để check update các kiểu rất tiện lợi.
API này open-source luôn nhé, link: https://github.com/SpigotMC/XenforoResourceManagerAPI

Toàn bộ API dùng GET (có thể xem trực tiếp bằng trình duyệt)

1. Lấy thông tin tài nguyên (cả free lẫn premium)
Đường dẫn: api.spigotmc.org

Ví dụ: link tới plugin là www.spigotmc.org thì XXX ở đây là 50393
Dữ liệu trả về ở dạng JSON:
JSON:
{
  "id": "2",
  "title": "HubKick",
  "tag": "Send players to lobby on kick. /lobby / hub",
  "current_version": "1.7.1",
  "author": {
    "id": "106",
    "username": "LaxWasHere"
  },
  "premium": {
    "price": "0.00",
    "currency": ""
  },
  "stats": {
    "downloads": "6387",
    "updates": "22",
    "reviews": "9",
    "rating": "5"
  }
}
Ae có thể dùng Gson để parse nhé (có sẵn trong file .jar của server rồi, ko cần cài thêm)
Ví dụ code (code này mình còn thiếu phần get phiên bản mới nhất nhé, ae tự thêm vào :v)
Java:
package dev.anhcraft.craftkit.common.utils;

import com.google.gson.JsonObject;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

public class SpigotResourceInfo {
    /**
     * Gets the information of a resource using its id.
     * @param id the resource id (for e.g: 39007)
     * @return {@link SpigotResourceInfo}
     * @throws IOException if any errors occurred
     */
    @NotNull
    public static SpigotResourceInfo of(String id) throws IOException {
        // ae sửa cái code này để get link nhé :3 có thể xem trên mạng (sợt google: java fetch data from url)
        String str = HttpUtil.fetchString("https://api.spigotmc.org/simple/0.1/index.php?action=getResource&id=" + id);
        JsonObject root = new Gson().fromJson(str, JsonObject.class);
        JsonObject author = root.get("author").getAsJsonObject();
        JsonObject premium = root.get("premium").getAsJsonObject();
        JsonObject stats = root.get("stats").getAsJsonObject();
        return new SpigotResourceInfo(
                root.get("id").getAsInt(),
                root.get("title").getAsString(),
                root.get("tag").getAsString(),
                author.get("id").getAsInt(),
                author.get("username").getAsString(),
                premium.get("price").getAsDouble(),
                premium.get("currency").getAsString(),
                stats.get("downloads").getAsInt(),
                stats.get("updates").getAsInt(),
                stats.get("reviews").getAsInt(),
                stats.get("rating").getAsDouble()
        );
    }

    private final int id;
    private final String title;
    private final String tag;
    private final int authorId;
    private final String authorName;
    private final double price;
    private final String currency;
    private final int downloadCount;
    private final int updateCount;
    private final int reviewCount;
    private final double rating;

    private SpigotResourceInfo(int id, @NotNull String title, @NotNull String tag, int authorId, @NotNull String authorName, double price, @NotNull String currency, int downloadCount, int updateCount, int reviewCount, double rating) {
        this.id = id;
        this.title = title;
        this.tag = tag;
        this.authorId = authorId;
        this.authorName = authorName;
        this.price = price;
        this.currency = currency;
        this.downloadCount = downloadCount;
        this.updateCount = updateCount;
        this.reviewCount = reviewCount;
        this.rating = rating;
    }

    /**
     * Gets the id.
     * @return resource's id.
     */
    public int getId() {
        return id;
    }

    /**
     * Gets the title.
     * @return resource's title.
     */
    @NotNull
    public String getTitle() {
        return title;
    }

    /**
     * Gets the tag line.
     * @return resource's tag line.
     */
    @NotNull
    public String getTag() {
        return tag;
    }

    /**
     * Gets the id of the author.
     * @return author's id
     */
    public int getAuthorId() {
        return authorId;
    }

    /**
     * Gets the username of the author.
     * @return author's username.
     */
    @NotNull
    public String getAuthorName() {
        return authorName;
    }

    /**
     * Gets the resource price.
     * @return price (will be 0.00 if the resource is free)
     */
    public double getPrice() {
        return price;
    }

    /**
     * Gets the resource currency.
     * @return currency (e.g: usd, eur; will be empty if the resource is free)
     */
    @NotNull
    public String getCurrency() {
        return currency;
    }

    /**
     * Gets the total of downloads.
     * @return total of downloads.
     */
    public int getDownloadCount() {
        return downloadCount;
    }

    /**
     * Gets the total of updates.
     * @return total of updates.
     */
    public int getUpdateCount() {
        return updateCount;
    }

    /**
     * Gets the total of reviews.
     * @return total of reviews.
     */
    public int getReviewCount() {
        return reviewCount;
    }

    /**
     * Gets the average rating score.
     * @return rating score.
     */
    public double getRating() {
        return rating;
    }

    /**
     * Gets the link to the resource.
     * @return resource link.
     */
    @NotNull
    public String getLink() {
        return "https://spigotmc.org/resources/" + id;
    }
}

2. Lấy all thông tin tài nguyên của ai đó
Đường dẫn: api.spigotmc.org
Ví dụ: link tới profile là www.spigotmc.org thì XXX ở đây là 1
Dữ liệu trả về ở dạng JSON:
JSON:
[
  {
    "id": "201",
    "title": "XenPermissions",
    "tag": "Hook your permissions into Xenforo!",
    "current_version": "2013-09-28",
    "author": {
      "id": "1",
      "username": "md_5"
    },
    "premium": {
      "price": "0.00",
      "currency": ""
    },
    "stats": {
      "downloads": "939",
      "updates": "0",
      "reviews": "4",
      "rating": "4.5"
    }
  },
  {
    "id": "342",
    "title": "iTag",
    "tag": "Performant and Reliable TagAPI Replacement",
    "current_version": "22",
    "author": {
      "id": "1",
      "username": "md_5"
    },
    "premium": {
      "price": "0.00",
      "currency": ""
    },
    "stats": {
      "downloads": "23573",
      "updates": "2",
      "reviews": "17",
      "rating": "4.41176"
    }
  }
]

3. Xem thông tin profile
Đường dẫn: api.spigotmc.orggetAuthor&id=XXX

Ví dụ: link tới profile là www.spigotmc.org thì XXX ở đây là 1
Dữ liệu trả về ở dạng JSON:
JSON:
{
  "id": "1",
  "username": "md_5",
  "resource_count": "12",
  "identities": {
    "twitter": "md__5"
  },
  "avatar": {
    "info": "1545025664",
    "hash": "b53fd878a84d268da2b6456e0b96cae5"
  }
}
 
mấy cái này đọc chẳng hiểu, ghét java vl -.-
 
mấy cái này đọc chẳng hiểu, ghét java vl -.-
js cho những ai muốn đổi khẩu vị:
JavaScript:
fetch('https://cors-anywhere.herokuapp.com/https://api.spigotmc.org/simple/0.1/index.php?action=getResource&id=50393', {mode: 'cors'}).then((response) => {
  return response.json();
}).then((data) => {
  document.write(data.current_version);
});
 
js cho những ai muốn đổi khẩu vị:
JavaScript:
fetch('https://cors-anywhere.herokuapp.com/https://api.spigotmc.org/simple/0.1/index.php?action=getResource&id=50393', {mode: 'cors'}).then((response) => {
  return response.json();
}).then((data) => {
  document.write(data.current_version);
});
thì t học js mà :v thanks
 
Back
Top Bottom