11package llc .redstone .hysentials .cosmetics .capes ;
22
3+ import com .mojang .authlib .minecraft .MinecraftProfileTexture ;
4+ import com .mojang .authlib .properties .Property ;
5+ import llc .redstone .hysentials .config .hysentialmods .CosmeticConfig ;
36import llc .redstone .hysentials .cosmetic .CosmeticManager ;
47import llc .redstone .hysentials .schema .HysentialsSchema ;
58import llc .redstone .hysentials .websocket .Socket ;
69import net .minecraft .client .Minecraft ;
10+ import net .minecraft .client .network .NetworkPlayerInfo ;
711import net .minecraft .client .renderer .texture .DynamicTexture ;
12+ import net .minecraft .client .resources .SkinManager ;
813import net .minecraft .util .ResourceLocation ;
914import net .minecraftforge .fml .common .eventhandler .SubscribeEvent ;
1015import net .minecraftforge .fml .common .gameevent .TickEvent ;
16+ import net .minecraftforge .fml .relauncher .ReflectionHelper ;
1117
1218import java .awt .image .BufferedImage ;
1319import java .io .InputStream ;
20+ import java .lang .reflect .Field ;
21+ import java .util .ArrayList ;
1422import java .util .HashMap ;
23+ import java .util .List ;
1524import java .util .UUID ;
1625
1726public class CapeHandler {
1827 public static HashMap <UUID , Integer > tickMap = new HashMap <>();
19- public static HashMap <UUID , DynamicTexture > textureMap = new HashMap <>();
20- public static HashMap <UUID , String > resourceMap = new HashMap <>();
28+ public static HashMap <String , ResourceLocation > resourceList = new HashMap <>();
29+
30+ private Field capeLocationField ;
31+ public CapeHandler () {
32+ try {
33+ capeLocationField = ReflectionHelper .findField (NetworkPlayerInfo .class , "locationCape" , "field_178862_f" );
34+ capeLocationField .setAccessible (true );
35+ } catch (Exception e ) {
36+ e .printStackTrace ();
37+ }
38+
39+ }
2140 @ SubscribeEvent
2241 public void onTickEvent (TickEvent .ClientTickEvent event ) {
2342 if (event .phase != TickEvent .Phase .END ) return ;
43+ if (CosmeticConfig .disableCustomCapes ) return ;
2444 try {
2545 for (String id : Socket .cachedUsers .keySet ()) {
2646 UUID uuid = UUID .fromString (id );
@@ -31,7 +51,6 @@ public void onTickEvent(TickEvent.ClientTickEvent event) {
3151 String name = cosmetic .getName ();
3252 if (CosmeticManager .hasCosmetic (uuid , name ) || CosmeticManager .isPreviewing (uuid , name )) {
3353 ResourceLocation location = new ResourceLocation (cosmetic .getResource ());
34- resourceMap .put (uuid , cosmetic .getResource ());
3554 try {
3655 InputStream stream = Minecraft .getMinecraft ().mcDefaultResourcePack .getInputStream (location );
3756 if (stream == null ) {
@@ -51,7 +70,19 @@ public void onTickEvent(TickEvent.ClientTickEvent event) {
5170 tickMap .put (uuid , tick );
5271 BufferedImage frameImage = image .getSubimage (0 , frame * 32 , 64 , 32 );
5372 DynamicTexture texture = new DynamicTexture (frameImage );
54- textureMap .put (uuid , texture );
73+ String frameString = (frames > 1 ? "_" + frame : "" );
74+ if (!resourceList .containsKey (name + frameString )) {
75+ resourceList .put (name + frameString , Minecraft .getMinecraft ().getTextureManager ().getDynamicTextureLocation (name + frameString , texture ));
76+ }
77+ ResourceLocation capeResource = resourceList .get (name + frameString );
78+ if (Minecraft .getMinecraft ().getNetHandler () == null || capeResource == null ) return ;
79+
80+ NetworkPlayerInfo info = Minecraft .getMinecraft ().getNetHandler ().getPlayerInfo (uuid );
81+ if (info != null ) {
82+ ResourceLocation loc = (ResourceLocation ) capeLocationField .get (info );
83+ capeLocationField .set (info , capeResource );
84+ }
85+
5586 stream .close ();
5687 } catch (Exception e ) {
5788 e .printStackTrace ();
@@ -60,9 +91,22 @@ public void onTickEvent(TickEvent.ClientTickEvent event) {
6091 }
6192 }
6293 if (!wearingCape ) {
63- tickMap .remove (uuid );
64- textureMap .remove (uuid );
65- resourceMap .remove (uuid );
94+ NetworkPlayerInfo info = Minecraft .getMinecraft ().getNetHandler ().getPlayerInfo (uuid );
95+ if (tickMap .containsKey (uuid )) {
96+ tickMap .remove (uuid );
97+ if (info == null ) return ;
98+ Minecraft .getMinecraft ().getSkinManager ().loadProfileTextures (info .getGameProfile (), new SkinManager .SkinAvailableCallback () {
99+ public void skinAvailable (MinecraftProfileTexture .Type typeIn , ResourceLocation location , MinecraftProfileTexture profileTexture ) {
100+ if (typeIn == MinecraftProfileTexture .Type .CAPE ) {
101+ try {
102+ capeLocationField .set (info , location );
103+ } catch (Exception e ) {
104+ e .printStackTrace ();
105+ }
106+ }
107+ }
108+ }, true );
109+ }
66110 }
67111 }
68112 } catch (Exception e ) {
0 commit comments