Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

Commit

Permalink
🎉🥳 Commit number 100! ~ Begin implementation of capes and skins from …
Browse files Browse the repository at this point in the history
…UUID #4 #5 #8 #9 #17
  • Loading branch information
boomboompower committed Mar 29, 2020
1 parent 40f7f10 commit 8698804
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
import me.do_you_like.mods.skinchanger.gui.additional.ModOptionsMenu;
import me.do_you_like.mods.skinchanger.gui.additional.PlayerSelectMenu;
import me.do_you_like.mods.skinchanger.gui.additional.PlayerSelectMenu.StringSelectionType;
import me.do_you_like.mods.skinchanger.utils.game.ChatColor;
import me.do_you_like.mods.skinchanger.utils.gui.impl.ModernScroller;
import me.do_you_like.mods.skinchanger.options.SelectionOptions;
import me.do_you_like.mods.skinchanger.utils.gui.impl.ModernButton;
import me.do_you_like.mods.skinchanger.utils.game.ChatColor;
import me.do_you_like.mods.skinchanger.utils.gui.ModernGui;
import me.do_you_like.mods.skinchanger.utils.gui.impl.ModernButton;
import me.do_you_like.mods.skinchanger.utils.gui.impl.ModernHeader;
import me.do_you_like.mods.skinchanger.utils.gui.impl.ModernScroller;
import me.do_you_like.mods.skinchanger.utils.gui.impl.ModernSlider;

import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -263,9 +263,10 @@ protected void onGuiInitExtra() {
skinSettings.setOffsetBetweenDrawables(24F);

skinSettings.getSubDrawables().add(new ModernButton(12, 5, 20, buttonWidth, 20, "Load from Player").setAsPartOfHeader(skinSettings));
skinSettings.getSubDrawables().add(new ModernButton(13, 5, 20, buttonWidth, 20, "Load from URL").setAsPartOfHeader(skinSettings));
skinSettings.getSubDrawables().add(new ModernButton(14, 5, 20, buttonWidth, 20, "Load from File").setAsPartOfHeader(skinSettings));
skinSettings.getSubDrawables().add(new ModernButton(15, 5, 20, buttonWidth, 20, "Reset Skin").setAsPartOfHeader(skinSettings));
skinSettings.getSubDrawables().add(new ModernButton(13, 5, 20, buttonWidth, 20, "Load from UUID").setAsPartOfHeader(skinSettings));
skinSettings.getSubDrawables().add(new ModernButton(14, 5, 20, buttonWidth, 20, "Load from URL").setAsPartOfHeader(skinSettings));
skinSettings.getSubDrawables().add(new ModernButton(15, 5, 20, buttonWidth, 20, "Load from File").setAsPartOfHeader(skinSettings));
skinSettings.getSubDrawables().add(new ModernButton(16, 5, 20, buttonWidth, 20, "Reset Skin").setAsPartOfHeader(skinSettings));

// ----------------------------------

Expand All @@ -279,10 +280,11 @@ protected void onGuiInitExtra() {

capeSettings.setOffsetBetweenDrawables(24F);

capeSettings.getSubDrawables().add(new ModernButton(16, 5, 20, buttonWidth, 20, "Load from Player").setAsPartOfHeader(capeSettings));
capeSettings.getSubDrawables().add(new ModernButton(17, 5, 20, buttonWidth, 20, "Load from URL").setAsPartOfHeader(capeSettings));
capeSettings.getSubDrawables().add(new ModernButton(18, 5, 20, buttonWidth, 20, "Load from File").setAsPartOfHeader(capeSettings));
capeSettings.getSubDrawables().add(new ModernButton(19, 5, 20, buttonWidth, 20, "Reset Cape").setAsPartOfHeader(capeSettings));
capeSettings.getSubDrawables().add(new ModernButton(17, 5, 20, buttonWidth, 20, "Load from Player").setAsPartOfHeader(capeSettings));
capeSettings.getSubDrawables().add(new ModernButton(18, 5, 20, buttonWidth, 20, "Load from UUID").setAsPartOfHeader(capeSettings));
capeSettings.getSubDrawables().add(new ModernButton(19, 5, 20, buttonWidth, 20, "Load from URL").setAsPartOfHeader(capeSettings));
capeSettings.getSubDrawables().add(new ModernButton(20, 5, 20, buttonWidth, 20, "Load from File").setAsPartOfHeader(capeSettings));
capeSettings.getSubDrawables().add(new ModernButton(21, 5, 20, buttonWidth, 20, "Reset Cape").setAsPartOfHeader(capeSettings));

// ----------------------------------

Expand Down Expand Up @@ -317,8 +319,13 @@ protected void onButtonPressedExtra(ModernButton button) {

break;

// Skin from a URL
// Skin from a UUID
case 13:

break;

// Skin from a URL
case 14:
if (this.p_playerSelectMenu == null) {
this.p_playerSelectMenu = new PlayerSelectMenu(this, StringSelectionType.P_URL);
}
Expand All @@ -328,17 +335,19 @@ protected void onButtonPressedExtra(ModernButton button) {
break;

// Skin from a file
case 14:
case 15:
this.selectionOptions.loadFromFile((location) -> this.fakePlayer.setSkinLocation(location), false);

break;
case 15:

// Reset Skin
case 16:
this.fakePlayer.setSkinLocation(this.originalSkin);

break;

// Cape from a player name
case 16:
case 17:
if (this.c_playerSelectMenu == null) {
this.c_playerSelectMenu = new PlayerSelectMenu(this, StringSelectionType.C_USERNAME);
}
Expand All @@ -347,8 +356,14 @@ protected void onButtonPressedExtra(ModernButton button) {

break;

// Cape from a UUID
case 18:


break;

// Cape from a URL
case 17:
case 19:
if (this.c_playerSelectMenu == null) {
this.c_playerSelectMenu = new PlayerSelectMenu(this, StringSelectionType.C_URL);
}
Expand All @@ -358,13 +373,13 @@ protected void onButtonPressedExtra(ModernButton button) {
break;

// Cape from a file
case 18:
case 20:
this.selectionOptions.loadFromFile((location) -> this.fakePlayer.setCapeLocation(location), true);

break;

// Resets the cape of the entity
case 19:
case 21:
this.fakePlayer.setCapeLocation(this.originalCape);

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.awt.Color;
import java.util.Objects;

import java.util.UUID;

import lombok.Getter;

import me.do_you_like.mods.skinchanger.SkinChangerMod;
Expand Down Expand Up @@ -75,6 +77,8 @@ protected void onGuiInitExtra() {

if (this.selectionType.isTypeOfUrl()) {
entryBox.setMaxStringLength(520);
} else if (this.selectionType.isTypeOfUUID()) {
entryBox.setMaxStringLength(36);
} else {
entryBox.setMaxStringLength(16);
}
Expand Down Expand Up @@ -264,16 +268,27 @@ private int cap(int in) {
return in;
}

/**
* Tells this class which varient of itself it should use
*/
public enum StringSelectionType {
P_USERNAME("Enter the username of the player."),
C_USERNAME("Enter the username of the player."),

P_URL("Enter the URL of the skin. (https://....)"),
C_URL("Enter the URL of the cape. (https://....)");
C_URL("Enter the URL of the cape. (https://....)"),

P_UUID("Enter the UUID of the player. (ABCD-EFGH-...)"),
C_UUID("Enter the UUID of the player. (ABCD-EFGH-...)");

@Getter
private String displaySentence;

// If a UUID has been generated we should store
// it so we don't have to parse it twice
@Getter
private UUID storedUUID;

StringSelectionType(String displaySentence) {
this.displaySentence = displaySentence;
}
Expand All @@ -298,15 +313,41 @@ public boolean isValid(String input) {
return input.startsWith("https://") || input.startsWith("http://") || input.startsWith("www.");
}

if (isTypeOfUUID()) {
// Tiny performance increase
this.storedUUID = SkinChangerMod.getInstance().getMojangHooker().getUUIDFromStrippedString(input);

return this.storedUUID != null;
}

return false;
}

/**
* Is this enum a type of URL?
*
* @return true if the enum is of type URL
*/
public boolean isTypeOfUrl() {
return this == P_URL || this == C_URL;
}

/**
* Is this enum a type of username?
*
* @return true if the enum is of type username
*/
public boolean isTypeOfUsername() {
return this == P_USERNAME || this == C_USERNAME;
}

/**
* Is this enum a type of UUID?
*
* @return true if the enum is of type UUID
*/
public boolean isTypeOfUUID() {
return this == P_UUID || this == C_UUID;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import java.util.Base64;
import java.util.HashMap;

import java.util.UUID;
import java.util.regex.Pattern;

import me.do_you_like.mods.skinchanger.SkinChangerMod;
import me.do_you_like.mods.skinchanger.utils.backend.CacheRetriever.CacheType;
import me.do_you_like.mods.skinchanger.utils.general.BetterJsonObject;

import net.minecraft.client.Minecraft;
Expand All @@ -56,6 +58,8 @@ public class MojangHooker {
".minecraft.net",
".mojang.com"
};

private static final Pattern uuidPattern = Pattern.compile("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)");

private static final HashMap<String, String[]> idCaches = new HashMap<>(); // Store the id

Expand Down Expand Up @@ -164,6 +168,31 @@ public BetterJsonObject getTexturesFromId(String id) {

return new BetterJsonObject(getUrl("https://sessionserver.mojang.com/session/minecraft/profile/" + id));
}

/**
* Returns a UUID representation of a UUID which has had its braces removed. Mojang's API
* removes braces from UUID so this code is designed to unstrip them if possible. If a value
* being parsed into this method already contains braces then no attempt at conversion will be
* made. If the input cannot be converted to a valid UUID then null will be returned instead.
*
* @param strippedInput the input to be converted to a UUID
* @return a UUID representation of a String if possible or null
*/
public UUID getUUIDFromStrippedString(String strippedInput) {
if (strippedInput == null || strippedInput.trim().isEmpty()) {
return null;
}

if (strippedInput.contains("-")) {
return tryParseUUID(strippedInput);
}

// https://stackoverflow.com/a/19399768/12697448
// Pattern#compile improves performance.
String bracedString = uuidPattern.matcher(strippedInput).replaceFirst("$1-$2-$3-$4-$5");

return tryParseUUID(bracedString);
}

/**
* Gets the users profile from a username
Expand Down Expand Up @@ -346,7 +375,7 @@ private ResourceLocation getSkinFromIdUnsafe(String id) {
throw new IllegalArgumentException("Invalid payload, the domain issued was not trusted.");
}

ResourceLocation playerSkin = SkinChangerMod.getInstance().getCacheRetriever().loadIntoGame(id, url, CacheType.CAPE);
ResourceLocation playerSkin = SkinChangerMod.getInstance().getCacheRetriever().loadIntoGame(id, url, null);

skins.put(id, playerSkin);

Expand Down Expand Up @@ -465,4 +494,19 @@ private boolean isTrustedDomain(String url) {
}
return false;
}

/**
* A safe way of parsing a UUID
*
* @param input the input to parse, should be in hyphen format
*
* @return a UUID if valid or null
*/
private UUID tryParseUUID(String input) {
try {
return UUID.fromString(input);
} catch (IllegalArgumentException ex) {
return null;
}
}
}

0 comments on commit 8698804

Please sign in to comment.