Skip to content

Commit

Permalink
fix: #92
Browse files Browse the repository at this point in the history
  • Loading branch information
IMB11 committed Aug 10, 2024
1 parent 7718c23 commit 4291959
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 106 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ dependencies {
include modImplementation("dev.isxander:yet-another-config-lib:${property('deps.yacl')}")

include implementation("org.jsoup:jsoup:1.16.1")
include implementation("org.mineskin:java-client:+")
include implementation("org.mineskin:java-client:1.2.4-SNAPSHOT")
include implementation("commons-validator:commons-validator:1.7")

// modRuntimeOnly("me.djtheredstoner:DevAuth-fabric:1.2.0")
modRuntimeOnly("me.djtheredstoner:DevAuth-fabric:1.2.0")
}

processResources {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/mineblock11/skinshuffle/api/SkinAPIs.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@
import com.mineblock11.skinshuffle.mixin.accessor.MinecraftClientAccessor;
import com.mineblock11.skinshuffle.mixin.accessor.MinecraftClientAuthAccessor;
import com.mineblock11.skinshuffle.mixin.accessor.YggdrasilUserApiServiceAccessor;
import com.mineblock11.skinshuffle.networking.ClientSkinHandling;
import com.mineblock11.skinshuffle.util.NetworkingUtil;
import com.mineblock11.skinshuffle.util.SkinCacheRegistry;
import com.mojang.authlib.minecraft.UserApiService;
import com.mojang.authlib.yggdrasil.YggdrasilUserApiService;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.client.MinecraftClient;
import org.jetbrains.annotations.Nullable;
import org.mineskin.MineskinClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
/*
* ALL RIGHTS RESERVED
*
* Copyright (c) 2024 Calum H. (IMB11) and enjarai
*
* THE SOFTWARE IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.mineblock11.skinshuffle.client.config;

import com.google.gson.*;
Expand All @@ -36,73 +22,48 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SkinPresetManager {
public static final Path PERSISTENT_SKINS_DIR = SkinShuffle.DATA_DIR.resolve("skins");
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static final Path PRESETS = SkinShuffle.DATA_DIR.resolve("presets.json");

private static final ArrayList<SkinPreset> loadedPresets = new ArrayList<>();
private static final List<SkinPreset> loadedPresets = new ArrayList<>();
private static SkinPreset chosenPreset = null;
private static SkinPreset apiPreset = null;

/**
* Get all loaded presets.
*/
public static ArrayList<SkinPreset> getLoadedPresets() {
return loadedPresets;
public static List<SkinPreset> getLoadedPresets() {
return Collections.unmodifiableList(loadedPresets);
}

/**
* Get the currently chosen preset.
*/
public static SkinPreset getChosenPreset() {
return chosenPreset;
}

/**
* Get the preset currently uploaded to Mojang API.
*/
public static SkinPreset getApiPreset() {
return apiPreset;
}

/**
* Swap the positions of two presets.
*/
public static void swapPresets(int index1, int index2) {
if(loadedPresets.size() - 1 < index2 || loadedPresets.size() - 1 < index1)
if (index1 < 0 || index2 < 0 || index1 >= loadedPresets.size() || index2 >= loadedPresets.size()) {
return;

}
Collections.swap(loadedPresets, index1, index2);
savePresets();
}

/**
* Set a chosen preset, and apply it.
*
* @param preset The preset to apply.
*/
public static void setChosenPreset(SkinPreset preset, boolean ignoreMatch) {
if (chosenPreset == preset && !ignoreMatch) return;
chosenPreset = preset;
savePresets();

apply();
}

/**
* Set the preset currently uploaded to Mojang API.
*
* @param preset The preset to apply.
*/
public static void setApiPreset(SkinPreset preset) {
apiPreset = preset;
}

/**
* Save the currently loaded presets to the presets.json file.
*/
public static void savePresets() {
JsonObject presetFile = new JsonObject();
presetFile.addProperty("chosenPreset", loadedPresets.indexOf(chosenPreset));
Expand All @@ -129,21 +90,20 @@ public static void savePresets() {

public static boolean LOADING_LOCK = false;

/**
* Load presets from the presets.json file.
*/
public static boolean hasLoadedPresets() {
return chosenPreset != null;
}

public static void loadPresets() {
if(LOADING_LOCK) return;
if (LOADING_LOCK) return;
LOADING_LOCK = true;

if (!PRESETS.toFile().exists()) {
// Generate a preset from the currently equipped skin when generating the presets file
if (!Files.exists(PRESETS)) {
if (chosenPreset == null) {
chosenPreset = SkinPreset.generateDefaultPreset();
apiPreset = chosenPreset;
loadedPresets.add(chosenPreset);
}

savePresets();
}

Expand All @@ -154,11 +114,7 @@ public static void loadPresets() {
String jsonString = Files.readString(PRESETS);
JsonObject presetFile = GSON.fromJson(jsonString, JsonObject.class);
int chosenPresetIndex = presetFile.get("chosenPreset").getAsInt();

int apiPresetIndex = -1;
if (presetFile.has("apiPreset")) {
apiPresetIndex = presetFile.get("apiPreset").getAsInt();
}
int apiPresetIndex = presetFile.has("apiPreset") ? presetFile.get("apiPreset").getAsInt() : -1;

JsonArray array = presetFile.get("loadedPresets").getAsJsonArray();
for (JsonElement jsonElement : array) {
Expand All @@ -175,52 +131,36 @@ public static void loadPresets() {
apiPreset = apiPresetIndex < 0 ? null : loadedPresets.get(apiPresetIndex);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
LOADING_LOCK = false;
}

LOADING_LOCK = false;
}

/**
* Create the necessary directories and cache files.
*/
public static void setup() {
try {
if (!PERSISTENT_SKINS_DIR.toFile().exists()) Files.createDirectories(PERSISTENT_SKINS_DIR);
if (!Files.exists(PERSISTENT_SKINS_DIR)) Files.createDirectories(PERSISTENT_SKINS_DIR);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/**
* Add a preset.
*
* @param preset The preset to add.
*/
public static void addPreset(SkinPreset preset) {
loadedPresets.add(preset);
savePresets();
}

/**
* Delete a preset.
*
* @param skinPreset The skin preset to delete.
*/
public static void deletePreset(SkinPreset skinPreset) {
loadedPresets.remove(skinPreset);
if (chosenPreset == skinPreset)
if (chosenPreset == skinPreset && !loadedPresets.isEmpty()) {
chosenPreset = loadedPresets.get(0);
}
savePresets();
}

/**
* Apply the currently chosen preset - ran after configuration load.
*/
public static void apply() {
MinecraftClient client = MinecraftClient.getInstance();
SkinPreset preset = getChosenPreset();

// Skip applying if the config says to.
if (SkinShuffleConfig.get().disableAPIUpload) {
SkinShuffle.LOGGER.info("Skipping skin preset application due to user preference.");
return;
Expand All @@ -235,27 +175,24 @@ public static void apply() {
ConfigSkin configSkin = preset.getSkin().saveToConfig();

try {
boolean successful;
if (preset.getSkin() instanceof UrlSkin urlSkin) {
boolean successful = SkinAPIs.setSkinTexture(urlSkin.getUrl(), urlSkin.getModel());
if(successful) setApiPreset(preset);
successful = SkinAPIs.setSkinTexture(urlSkin.getUrl(), urlSkin.getModel());
} else {
boolean successful = SkinAPIs.setSkinTexture(configSkin.getFile().toFile(), configSkin.getModel());
if(successful) setApiPreset(preset);
successful = SkinAPIs.setSkinTexture(configSkin.getFile().toFile(), configSkin.getModel());
}
if (successful) setApiPreset(preset);

if (client.world != null && ClientSkinHandling.isInstalledOnServer()) {
new Thread(() -> {
client.executeTask(() -> {
try {
String cachedURL = SkinCacheRegistry.getCachedUploadedSkin(configSkin.getFile().toFile());
Skin result;
if(cachedURL != null) {
result = SkinAPIs.MINESKIN_CLIENT.generateUrl(cachedURL).join();
} else {
result = SkinAPIs.MINESKIN_CLIENT.generateUpload(configSkin.getFile().toFile()).join();
}
Skin result = cachedURL != null
? SkinAPIs.MINESKIN_CLIENT.generateUrl(cachedURL).join()
: SkinAPIs.MINESKIN_CLIENT.generateUpload(configSkin.getFile().toFile()).join();

SkinQueryResult queryResult = new SkinQueryResult(false, null, preset.getSkin().getModel(), result.data.texture.signature, result.data.texture.value);
SkinQueryResult queryResult = new SkinQueryResult(false, null, preset.getSkin().getModel(), result.data.texture.signature, result.data.texture.value);
ClientSkinHandling.sendRefresh(queryResult);
} catch (Exception e) {
SkinShuffle.LOGGER.error(e.getMessage());
Expand All @@ -270,6 +207,6 @@ public static void apply() {
SkinShuffle.LOGGER.info("Skipping skin preset application due to skin not being fully loaded. If this is first startup, please ignore this message.");
}

SkinPresetManager.savePresets();
savePresets();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.mineblock11.skinshuffle.client.gui.cursed;

import com.mineblock11.skinshuffle.client.config.SkinPresetManager;
import com.mineblock11.skinshuffle.compat.ETFCompat;
import com.mojang.blaze3d.systems.RenderSystem;
import net.fabricmc.loader.api.FabricLoader;
Expand All @@ -32,7 +33,7 @@ public class GuiEntityRenderer {
* Render a player in the GUI.
*/
public static void drawEntity(MatrixStack matrices, int x, int y, int size, float rotation, double mouseX, double mouseY, LivingEntity entity) {
if(entity == null) return;
if(entity == null || !SkinPresetManager.hasLoadedPresets()) return;
float yaw = (float) Math.atan(mouseX / 40.0F);
float pitch = (float) Math.atan((mouseY) / 40.0F);

Expand Down Expand Up @@ -81,14 +82,9 @@ public static void drawEntity(MatrixStack matrices, int x, int y, int size, floa
ETFCompat.preventRenderLayerIssue();
}

try {
VertexConsumerProvider.Immediate vertexConsumers = MinecraftClient.getInstance().getBufferBuilders().getEntityVertexConsumers();
dispatcher.render(entity, 0.0, 0.0, 0.0, 0.0f, 1.0f, matrices, vertexConsumers, 0xF000F0);
vertexConsumers.draw();
} catch (Exception ignored) {

}

VertexConsumerProvider.Immediate vertexConsumers = MinecraftClient.getInstance().getBufferBuilders().getEntityVertexConsumers();
dispatcher.render(entity, 0.0, 0.0, 0.0, 0.0f, 1.0f, matrices, vertexConsumers, 0xF000F0);
vertexConsumers.draw();

dispatcher.setRenderShadows(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.mineblock11.skinshuffle.client.gui.GeneratedScreens;
import com.mineblock11.skinshuffle.client.gui.cursed.GuiEntityRenderer;
import com.mineblock11.skinshuffle.client.preset.SkinPreset;
import com.mineblock11.skinshuffle.mixin.accessor.DummyClientPlayerEntityAccessor;
import com.mineblock11.skinshuffle.util.DummyBuilder;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
Expand Down Expand Up @@ -66,9 +67,22 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
public void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) {
super.renderWidget(context, mouseX, mouseY, delta);
/*?}*/

if (this.entity != null && selectedPreset != null) {
// Don't want to render the entity if the skin is still loading
if (!selectedPreset.getSkin().isLoading()) {

// Solve null crashes and desync issues.
//? if >=1.20.4 {
if(!this.entity.getSkinTextures().equals(selectedPreset.getSkin().getSkinTextures())) {
((DummyClientPlayerEntityAccessor) this.entity).setSkinTextures(selectedPreset.getSkin().getSkinTextures());
}
//?} else {
/*if(this.entity.getSkinTexture() != selectedPreset.getSkin().getTexture()) {
((DummyClientPlayerEntityAccessor) this.entity).setSkinIdentifier(selectedPreset.getSkin().getTexture());
}
*///?}

float followX = (float) (this.getX() + (this.getWidth() / 2)) - mouseX;
float followY = (float) (this.getY() - 90) - mouseY;
float rotation = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mineblock11.skinshuffle.mixin.accessor;

import net.minecraft.util.Identifier;
import nl.enjarai.cicada.api.cursed.DummyClientPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(value = DummyClientPlayerEntity.class, remap = false)
public interface DummyClientPlayerEntityAccessor {
//? if >=1.20.4 {
@Accessor(value = "skinTextures", remap = false)
void setSkinTextures(net.minecraft.client.util.SkinTexturesSkinTextures skinTextures);
//?} else {
/*@Accessor(value = "skinIdentifier", remap = false)
void setSkinIdentifier(Identifier skinIdentifier);
*///?}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.mineblock11.skinshuffle.mixin.screen;

import com.mineblock11.skinshuffle.client.config.SkinPresetManager;
import com.mineblock11.skinshuffle.util.NetworkingUtil;
import com.mineblock11.skinshuffle.util.ToastHelper;
import net.minecraft.client.gui.screen.AccessibilityOnboardingScreen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(AccessibilityOnboardingScreen.class)
public class AccessibilityOnboardingScreenMixin {
@Unique
private static boolean appliedConfiguration = false;

@Inject(method = "render", at = @At("HEAD"))
public void refreshConfig(CallbackInfo ci) {
if (!appliedConfiguration) {
appliedConfiguration = true;
SkinPresetManager.loadPresets();
SkinPresetManager.apply();

if(!NetworkingUtil.isLoggedIn()) {
ToastHelper.showOfflineModeToast();
}
}
}
}
Loading

0 comments on commit 4291959

Please sign in to comment.