|
| 1 | +package com.nxyi.addon.perk; |
| 2 | + |
| 3 | +import meteordevelopment.meteorclient.MeteorClient; |
| 4 | +import meteordevelopment.meteorclient.events.world.TickEvent; |
| 5 | +import meteordevelopment.meteorclient.utils.PreInit; |
| 6 | +import meteordevelopment.meteorclient.utils.misc.MeteorIdentifier; |
| 7 | +import meteordevelopment.meteorclient.utils.network.Http; |
| 8 | +import meteordevelopment.meteorclient.utils.network.MeteorExecutor; |
| 9 | +import meteordevelopment.orbit.EventHandler; |
| 10 | +import net.minecraft.client.texture.NativeImage; |
| 11 | +import net.minecraft.client.texture.NativeImageBackedTexture; |
| 12 | +import net.minecraft.entity.player.PlayerEntity; |
| 13 | +import net.minecraft.util.Identifier; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.InputStream; |
| 17 | +import java.util.*; |
| 18 | +import java.util.stream.Stream; |
| 19 | + |
| 20 | +import static meteordevelopment.meteorclient.MeteorClient.mc; |
| 21 | + |
| 22 | +public class Capes { |
| 23 | + private static final String CAPE_OWNERS_URL = "https://raw.githubusercontent.com/Dark-Developments/Recauses/main/Perks/Capes/capeowners.txt"; |
| 24 | + private static final String CAPES_URL = "https://raw.githubusercontent.com/Dark-Developments/Recauses/main/Perks/Capes/capes.txt"; |
| 25 | + |
| 26 | + private static final Map<UUID, String> OWNERS = new HashMap<>(); |
| 27 | + private static final Map<String, String> URLS = new HashMap<>(); |
| 28 | + private static final Map<String, Cape> TEXTURES = new HashMap<>(); |
| 29 | + |
| 30 | + private static final List<Cape> TO_REGISTER = new ArrayList<>(); |
| 31 | + private static final List<Cape> TO_RETRY = new ArrayList<>(); |
| 32 | + private static final List<Cape> TO_REMOVE = new ArrayList<>(); |
| 33 | + |
| 34 | + @PreInit(dependencies = MeteorExecutor.class) |
| 35 | + public static void init() { |
| 36 | + OWNERS.clear(); |
| 37 | + URLS.clear(); |
| 38 | + TEXTURES.clear(); |
| 39 | + TO_REGISTER.clear(); |
| 40 | + TO_RETRY.clear(); |
| 41 | + TO_REMOVE.clear(); |
| 42 | + |
| 43 | + MeteorExecutor.execute(() -> { |
| 44 | + // Cape owners |
| 45 | + Stream<String> lines = Http.get(CAPE_OWNERS_URL).sendLines(); |
| 46 | + if (lines != null) lines.forEach(s -> { |
| 47 | + String[] split = s.split(" "); |
| 48 | + |
| 49 | + if (split.length >= 2) { |
| 50 | + OWNERS.put(UUID.fromString(split[0]), split[1]); |
| 51 | + if (!TEXTURES.containsKey(split[1])) TEXTURES.put(split[1], new Cape(split[1])); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + // Capes |
| 56 | + lines = Http.get(CAPES_URL).sendLines(); |
| 57 | + if (lines != null) lines.forEach(s -> { |
| 58 | + String[] split = s.split(" "); |
| 59 | + |
| 60 | + if (split.length >= 2) { |
| 61 | + if (!URLS.containsKey(split[0])) URLS.put(split[0], split[1]); |
| 62 | + } |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + MeteorClient.EVENT_BUS.subscribe(Capes.class); |
| 67 | + } |
| 68 | + |
| 69 | + @EventHandler |
| 70 | + private static void onTick(TickEvent.Post event) { |
| 71 | + synchronized (TO_REGISTER) { |
| 72 | + for (Cape cape : TO_REGISTER) cape.register(); |
| 73 | + TO_REGISTER.clear(); |
| 74 | + } |
| 75 | + |
| 76 | + synchronized (TO_RETRY) { |
| 77 | + TO_RETRY.removeIf(Cape::tick); |
| 78 | + } |
| 79 | + |
| 80 | + synchronized (TO_REMOVE) { |
| 81 | + for (Cape cape : TO_REMOVE) { |
| 82 | + URLS.remove(cape.name); |
| 83 | + TEXTURES.remove(cape.name); |
| 84 | + TO_REGISTER.remove(cape); |
| 85 | + TO_RETRY.remove(cape); |
| 86 | + } |
| 87 | + |
| 88 | + TO_REMOVE.clear(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public static Identifier get(PlayerEntity player) { |
| 93 | + String capeName = OWNERS.get(player.getUuid()); |
| 94 | + if (capeName != null) { |
| 95 | + Cape cape = TEXTURES.get(capeName); |
| 96 | + if (cape == null) return null; |
| 97 | + |
| 98 | + if (cape.isDownloaded()) return cape; |
| 99 | + |
| 100 | + cape.download(); |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + private static class Cape extends MeteorIdentifier { |
| 108 | + private static int COUNT = 0; |
| 109 | + |
| 110 | + private final String name; |
| 111 | + |
| 112 | + private boolean downloaded; |
| 113 | + private boolean downloading; |
| 114 | + |
| 115 | + private NativeImage img; |
| 116 | + |
| 117 | + private int retryTimer; |
| 118 | + |
| 119 | + public Cape(String name) { |
| 120 | + super("capes/" + COUNT++); |
| 121 | + |
| 122 | + this.name = name; |
| 123 | + } |
| 124 | + |
| 125 | + public void download() { |
| 126 | + if (downloaded || downloading || retryTimer > 0) return; |
| 127 | + downloading = true; |
| 128 | + |
| 129 | + MeteorExecutor.execute(() -> { |
| 130 | + try { |
| 131 | + String url = URLS.get(name); |
| 132 | + if (url == null) { |
| 133 | + synchronized (TO_RETRY) { |
| 134 | + TO_REMOVE.add(this); |
| 135 | + downloading = false; |
| 136 | + return; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + InputStream in = Http.get(url).sendInputStream(); |
| 141 | + if (in == null) { |
| 142 | + synchronized (TO_RETRY) { |
| 143 | + TO_RETRY.add(this); |
| 144 | + retryTimer = 10 * 20; |
| 145 | + downloading = false; |
| 146 | + return; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + img = NativeImage.read(in); |
| 151 | + |
| 152 | + synchronized (TO_REGISTER) { |
| 153 | + TO_REGISTER.add(this); |
| 154 | + } |
| 155 | + } catch (IOException e) { |
| 156 | + e.printStackTrace(); |
| 157 | + } |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + public void register() { |
| 162 | + mc.getTextureManager().registerTexture(this, new NativeImageBackedTexture(img)); |
| 163 | + img = null; |
| 164 | + |
| 165 | + downloading = false; |
| 166 | + downloaded = true; |
| 167 | + } |
| 168 | + |
| 169 | + public boolean tick() { |
| 170 | + if (retryTimer > 0) { |
| 171 | + retryTimer--; |
| 172 | + } else { |
| 173 | + download(); |
| 174 | + return true; |
| 175 | + } |
| 176 | + |
| 177 | + return false; |
| 178 | + } |
| 179 | + |
| 180 | + public boolean isDownloaded() { |
| 181 | + return downloaded; |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments