Re-write cape system again

This commit is contained in:
noil 2023-05-08 22:45:58 -04:00
parent 8c1cf70d2b
commit c3941045d8
3 changed files with 52 additions and 129 deletions

View File

@ -15,7 +15,10 @@ import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* @author Seth * @author Seth
@ -23,115 +26,73 @@ import java.util.*;
*/ */
public final class CapeManager { public final class CapeManager {
private List<CapeUser> capeUserList = new ArrayList<>(); private final List<CapeUser> capeUserList = new ArrayList<>();
private HashMap<String, ResourceLocation> capesMap = new HashMap<>(); private final HashMap<String, ResourceLocation> capesMap = new HashMap<>();
public CapeManager() { public CapeManager() {
//this.downloadCapeUsers(); this.downloadCapeUsers();
//this.downloadCapes(); this.downloadCapes();
Seppuku.INSTANCE.getEventManager().addEventListener(this); Seppuku.INSTANCE.getEventManager().addEventListener(this);
} }
@Listener @Listener
public void displayCape(EventCapeLocation event) { public void displayCape(EventCapeLocation event) {
if (Minecraft.getMinecraft().player != null && event.getPlayer() != Minecraft.getMinecraft().player) { if (event.getPlayer() != null) {
final String uuid = event.getPlayer().getUniqueID().toString().replace("-", ""); if (Minecraft.getMinecraft().player != null && event.getPlayer() != Minecraft.getMinecraft().player) {
if (this.hasCape(uuid)) { if (this.getCape(event.getPlayer()) != null) {
final ResourceLocation cape = this.getCape(event.getPlayer()); event.setLocation(this.getCape(event.getPlayer()));
if (cape != null) {
event.setLocation(cape);
event.setCanceled(true); event.setCanceled(true);
} }
} }
} }
} }
public boolean hasCapeForUuid(String uuid) { public void downloadCapeUsers() {
for (CapeUser capeUser : this.capeUserList) { // Thread t = new Thread(new Runnable() {
if (capeUser.getUuid().equals(uuid)) { // public void run() {
return true; // }
} // });
} // t.start();
return false;
}
public boolean findCape(String uuid) {
if (this.hasCapeForUuid(uuid))
return true;
try { try {
URL url = new URL("https://seppuku.pw/cape/" + uuid); URL url = new URL("https://seppuku.pw/capes");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.addRequestProperty("User-Agent", "Mozilla/4.76"); httpURLConnection.addRequestProperty("User-Agent", "Mozilla/4.76");
final BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); final BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
reader.lines().forEachOrdered(line -> {
String line; final String[] split = line.split(";");
while ((line = reader.readLine()) != null) { if (line.toLowerCase().endsWith("png")) {
if (!line.toLowerCase().startsWith("no") && line.toLowerCase().endsWith("png")) { this.getCapeUserList().add(new CapeUser(split[0], split[1]));
final BufferedImage imageIO = ImageIO.read(httpURLConnection.getInputStream());
if (imageIO != null) {
if (imageIO.getWidth() == 512 && imageIO.getHeight() == 256) {
this.capeUserList.add(new CapeUser(uuid, line));
}
}
} else {
return false;
} }
} });
reader.close(); reader.close();
return true;
} catch (Exception e) { } catch (Exception e) {
//e.printStackTrace(); //e.printStackTrace();
} }
return false;
} }
/** /**
* Download and cache each cape for each user * Download and cache each cape for each user
* TODO thread this * TODO thread this
*/ */
public void downloadCape(String uuid) { public void downloadCapes() {
CapeUser existingUser = null; this.capeUserList.parallelStream().filter(capeUser -> this.findResource(capeUser.getCape()) == null).forEach(capeUser -> {
for (CapeUser capeUser : this.capeUserList) { try {
if (capeUser.getUuid().equals(uuid)) { URL url = new URL(capeUser.getCape());
existingUser = capeUser; HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
break; httpURLConnection.addRequestProperty("User-Agent", "Mozilla/4.76");
} final BufferedImage imageIO = ImageIO.read(httpURLConnection.getInputStream());
} if (imageIO != null) {
if (existingUser != null) { if (imageIO.getWidth() <= 2048 && imageIO.getHeight() <= 1024) {
if (this.capesMap.containsKey(existingUser.getCape())) { final DynamicTexture texture = new DynamicTexture(imageIO);
return; final ResourceLocation location = Minecraft.getMinecraft().getTextureManager().getDynamicTextureLocation("seppuku/capes", texture);
} this.capesMap.put(capeUser.getCape(), location);
}
try {
Minecraft.getMinecraft().getTextureManager();
for (CapeUser user : this.capeUserList) {
if (user != null) {
if (Objects.equals(user.getUuid(), uuid)) {
final ResourceLocation cape = this.findResource(user.getCape());
if (cape == null) {
URL url = new URL(user.getCape());
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.addRequestProperty("User-Agent", "Mozilla/4.76");
final BufferedImage imageIO = ImageIO.read(httpURLConnection.getInputStream());
if (imageIO != null) {
if (imageIO.getWidth() == 512 && imageIO.getHeight() == 256) {
final DynamicTexture texture = new DynamicTexture(imageIO);
final ResourceLocation location = Minecraft.getMinecraft().getTextureManager().getDynamicTextureLocation("seppuku/capes", texture);
this.capesMap.put(user.getCape(), location);
}
}
}
} }
} }
} catch (Exception e) {
//e.printStackTrace();
} }
} catch (Exception e) { });
//e.printStackTrace();
}
} }
/** /**
@ -141,42 +102,7 @@ public final class CapeManager {
* @return * @return
*/ */
public ResourceLocation findResource(String key) { public ResourceLocation findResource(String key) {
for (Map.Entry<String, ResourceLocation> entry : this.capesMap.entrySet()) { return this.capesMap.entrySet().stream().filter(entry -> entry.getKey().equals(key)).findFirst().map(Map.Entry::getValue).orElse(null);
if (entry.getKey().equals(key)) {
return entry.getValue();
}
}
return null;
}
//this.capeUserList.add(new CapeUser(split[0], split[1]));
// /**
// * Read a list of UUIDS and their cape names
// */
// protected void downloadCapeUsers() {
// try {
// URL url = new URL("https://seppuku.pw/files/capes_new.txt");
// HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
// httpURLConnection.addRequestProperty("User-Agent", "Mozilla/4.76");
// final BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
//
// String line;
// while ((line = reader.readLine()) != null) {
// final String[] split = line.split(";");
// this.capeUserList.add(new CapeUser(split[0], split[1]));
// }
//
// reader.close();
// } catch (Exception e) {
// e.printStackTrace();
// }
public boolean hasCape(String uuid) {
if (this.findCape(uuid)) {
this.downloadCape(uuid);
return true;
}
return false;
} }
/** /**
@ -187,9 +113,11 @@ public final class CapeManager {
*/ */
public ResourceLocation getCape(AbstractClientPlayer player) { public ResourceLocation getCape(AbstractClientPlayer player) {
final CapeUser user = this.find(player); final CapeUser user = this.find(player);
if (user != null) { if (user != null) {
return this.findResource(user.getCape()); return this.findResource(user.getCape());
} }
return null; return null;
} }
@ -208,9 +136,14 @@ public final class CapeManager {
return user; return user;
} }
} }
return null; return null;
} }
public boolean hasCape() {
return this.capeUserList.stream().anyMatch(capeUser -> capeUser.getUuid().equals(Minecraft.getMinecraft().session.getProfile().getId().toString().replace("-", "")));
}
public void unload() { public void unload() {
this.capeUserList.clear(); this.capeUserList.clear();
Seppuku.INSTANCE.getEventManager().removeEventListener(this); Seppuku.INSTANCE.getEventManager().removeEventListener(this);
@ -220,15 +153,7 @@ public final class CapeManager {
return capeUserList; return capeUserList;
} }
public void setCapeUserList(List<CapeUser> capeUserList) {
this.capeUserList = capeUserList;
}
public HashMap<String, ResourceLocation> getCapesMap() { public HashMap<String, ResourceLocation> getCapesMap() {
return capesMap; return capesMap;
} }
public void setCapesMap(HashMap<String, ResourceLocation> capesMap) {
this.capesMap = capesMap;
}
} }

View File

@ -179,7 +179,7 @@ public final class ModuleManager {
add(new FastProjectile()); add(new FastProjectile());
// p2w experience // p2w experience
if (Seppuku.INSTANCE.getCapeManager().hasCape(Minecraft.getMinecraft().session.getProfile().getId().toString().replace("-", ""))) if (Seppuku.INSTANCE.getCapeManager().hasCape())
add(new CapeModule()); add(new CapeModule());
this.loadExternalModules(); this.loadExternalModules();

View File

@ -19,12 +19,10 @@ public final class CapeModule extends Module {
@Listener @Listener
public void displayCape(EventCapeLocation event) { public void displayCape(EventCapeLocation event) {
if (Minecraft.getMinecraft().player != null && event.getPlayer() == Minecraft.getMinecraft().player) { if (event.getPlayer() != null) {
String uuid = Minecraft.getMinecraft().player.getUniqueID().toString().replace("-", ""); if (Minecraft.getMinecraft().player != null && event.getPlayer() == Minecraft.getMinecraft().player) {
if (Seppuku.INSTANCE.getCapeManager().hasCape(uuid)) { if (Seppuku.INSTANCE.getCapeManager().getCape(event.getPlayer()) != null) {
final ResourceLocation cape = Seppuku.INSTANCE.getCapeManager().getCape(event.getPlayer()); event.setLocation(Seppuku.INSTANCE.getCapeManager().getCape(event.getPlayer()));
if (cape != null) {
event.setLocation(cape);
event.setCanceled(true); event.setCanceled(true);
} }
} }