|
| 1 | +package de.kcodeyt.headsdb.database; |
| 2 | + |
| 3 | +import cn.nukkit.Player; |
| 4 | +import cn.nukkit.Server; |
| 5 | +import cn.nukkit.form.element.ElementButton; |
| 6 | +import cn.nukkit.form.element.ElementButtonImageData; |
| 7 | +import cn.nukkit.form.window.FormWindowSimple; |
| 8 | +import cn.nukkit.item.Item; |
| 9 | +import com.google.common.collect.Iterables; |
| 10 | +import com.google.gson.Gson; |
| 11 | +import de.kcodeyt.heads.Heads; |
| 12 | +import de.kcodeyt.heads.util.HeadInput; |
| 13 | +import de.kcodeyt.headsdb.util.FormAPI; |
| 14 | +import de.kcodeyt.headsdb.util.HeadRender; |
| 15 | +import lombok.Getter; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.InputStream; |
| 19 | +import java.net.HttpURLConnection; |
| 20 | +import java.net.URL; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.*; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +@Getter |
| 26 | +public class Database { |
| 27 | + |
| 28 | + private static final String API_URL = "https://minecraft-heads.com/scripts/api.php"; |
| 29 | + private static final Gson GSON = new Gson(); |
| 30 | + |
| 31 | + private final Map<String, Integer> pageCount; |
| 32 | + private final List<Category> categories; |
| 33 | + private final List<DBSkin> dbSkins; |
| 34 | + |
| 35 | + public Database() { |
| 36 | + this.pageCount = new HashMap<>(); |
| 37 | + this.categories = new ArrayList<>(); |
| 38 | + this.dbSkins = new ArrayList<>(); |
| 39 | + } |
| 40 | + |
| 41 | + public boolean reload() { |
| 42 | + this.categories.clear(); |
| 43 | + this.dbSkins.clear(); |
| 44 | + return this.load(); |
| 45 | + } |
| 46 | + |
| 47 | + public boolean load() { |
| 48 | + try { |
| 49 | + for(final Categories value : Categories.values()) { |
| 50 | + final List<Map<String, String>> values = GSON.<List<Map<String, String>>>fromJson(this.httpRequest(API_URL + "?cat=" + value.getIdentifier()), List.class); |
| 51 | + final List<DBSkin> dbSkins = values.stream().map(map -> new DBSkin(map.get("name"), map.get("uuid"), map.get("value"))).collect(Collectors.toList()); |
| 52 | + this.dbSkins.addAll(dbSkins); |
| 53 | + this.categories.add(new Category(value, value.getDisplayName(), Iterables.getLast(dbSkins).getTexture(), Collections.unmodifiableList(dbSkins))); |
| 54 | + } |
| 55 | + |
| 56 | + return true; |
| 57 | + } catch(IOException e) { |
| 58 | + Server.getInstance().getLogger().error("Error whilst loading heads from database api!", e); |
| 59 | + e.printStackTrace(); |
| 60 | + return false; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private String httpRequest(String urlSpec) throws IOException { |
| 65 | + final HttpURLConnection connection = (HttpURLConnection) new URL(urlSpec).openConnection(); |
| 66 | + connection.setRequestProperty("User-Agent", "Chrome"); |
| 67 | + |
| 68 | + String content = "{}"; |
| 69 | + if(connection.getResponseCode() == 200) { |
| 70 | + try(final InputStream inputStream = connection.getInputStream()) { |
| 71 | + final StringBuilder builder = new StringBuilder(); |
| 72 | + final byte[] bytes = new byte[1024 * 1024]; |
| 73 | + for(int read; (read = inputStream.read(bytes)) > 0; ) |
| 74 | + builder.append(new String(Arrays.copyOf(bytes, read), StandardCharsets.UTF_8)); |
| 75 | + content = builder.toString(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + connection.disconnect(); |
| 80 | + return content; |
| 81 | + } |
| 82 | + |
| 83 | + private List<List<DBSkin>> toPages(Category category, int count) { |
| 84 | + final List<DBSkin> dbSkins = category.getSkins(); |
| 85 | + final int dbSize = dbSkins.size(); |
| 86 | + if(dbSize <= 0) |
| 87 | + return Collections.emptyList(); |
| 88 | + final List<List<DBSkin>> pages = new ArrayList<>(); |
| 89 | + final int chunks = (dbSize - 1) / count; |
| 90 | + for(int i = 0; i <= chunks; i++) |
| 91 | + pages.add(dbSkins.subList(i * count, i == chunks ? dbSize : (i + 1) * count)); |
| 92 | + return Collections.unmodifiableList(pages); |
| 93 | + } |
| 94 | + |
| 95 | + public void showForm(Player player) { |
| 96 | + final FormWindowSimple categoriesForm = new FormWindowSimple("Select a category", ""); |
| 97 | + final List<Category> categories = Collections.unmodifiableList(new ArrayList<>(this.categories)); |
| 98 | + for(final Category category : categories) |
| 99 | + categoriesForm.addButton(new ElementButton(category.getDisplayName(), new ElementButtonImageData(ElementButtonImageData.IMAGE_DATA_TYPE_URL, HeadRender.createUrl(category.getDisplaySkin())))); |
| 100 | + FormAPI.create(player, categoriesForm, () -> { |
| 101 | + if(categoriesForm.wasClosed()) |
| 102 | + return; |
| 103 | + final Category category = categories.get(categoriesForm.getResponse().getClickedButtonId()); |
| 104 | + if(category == null) |
| 105 | + return; |
| 106 | + final FormWindowSimple pagesForm = new FormWindowSimple("Select a page", ""); |
| 107 | + final List<List<DBSkin>> pages = this.toPages(category, this.pageCount.getOrDefault(player.getName(), 40)); |
| 108 | + for(int i = 0; i < pages.size(); i++) |
| 109 | + pagesForm.addButton(new ElementButton("Page " + (i + 1), new ElementButtonImageData(ElementButtonImageData.IMAGE_DATA_TYPE_URL, HeadRender.createUrl(Iterables.getLast(pages.get(i)).getTexture())))); |
| 110 | + FormAPI.create(player, pagesForm, () -> { |
| 111 | + if(pagesForm.wasClosed()) { |
| 112 | + this.showForm(player); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + final List<DBSkin> dbSkins = pages.get(pagesForm.getResponse().getClickedButtonId()); |
| 117 | + if(dbSkins == null) |
| 118 | + return; |
| 119 | + final FormWindowSimple subForm = new FormWindowSimple(category.getDisplayName(), ""); |
| 120 | + for(final DBSkin dbSkin : dbSkins) |
| 121 | + subForm.addButton(new ElementButton(dbSkin.getName(), new ElementButtonImageData(ElementButtonImageData.IMAGE_DATA_TYPE_URL, HeadRender.createUrl(dbSkin.getTexture())))); |
| 122 | + FormAPI.create(player, subForm, () -> { |
| 123 | + if(subForm.wasClosed()) |
| 124 | + return; |
| 125 | + final DBSkin dbSkin = dbSkins.get(subForm.getResponse().getClickedButtonId()); |
| 126 | + if(dbSkin == null) |
| 127 | + return; |
| 128 | + this.giveItem(player, dbSkin); |
| 129 | + }); |
| 130 | + }); |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + public void giveItem(Player player, DBSkin dbSkin) { |
| 135 | + Heads.createItem(HeadInput.ofTexture(dbSkin.getTexture(), dbSkin.getId())).whenComplete((result, throwable) -> { |
| 136 | + if(throwable != null) { |
| 137 | + player.sendMessage("§cCould not create the requested skull item!"); |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + final Item item = result.getItem(); |
| 142 | + item.setCustomName("§r§7" + dbSkin.getName()); |
| 143 | + final Item[] drops = player.getInventory().addItem(item); |
| 144 | + if(drops.length > 0) { |
| 145 | + for(final Item drop : drops) |
| 146 | + player.getLevel().dropItem(player, drop); |
| 147 | + } |
| 148 | + |
| 149 | + player.sendMessage("§aGave you the head " + dbSkin.getName() + "§r§a!"); |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments