package openonyx.features.render; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import javax.imageio.ImageIO; import net.minecraft.client.renderer.ImageBufferDownload; import net.minecraft.client.renderer.texture.DynamicTexture; import net.minecraft.util.ResourceLocation; import openonyx.core.OnyxClient; import openonyx.events.FileDialogUtils; import openonyx.events.NotificationDispatcher; import openonyx.features.Module; import openonyx.features.ModuleCategory; import openonyx.features.hud.group_d.NotificationLevel; import openonyx.history.internal.ActionSetting; import openonyx.history.internal.EnumSetting; import openonyx.history.internal.TextSetting; import openonyx.skins.SkinFetcher; import openonyx.skins.SkinOverride; import org.apache.logging.log4j.Logger; public final class SkinChanger extends Module { private static final ExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadExecutor(runnable -> { Thread thread = new Thread(runnable, "onyx-skin-loader"); thread.setDaemon(true); return thread; }); public EnumSetting source = new EnumSetting<>("Source", SourceMode.LOCAL_FILE); public ActionSetting browse; public ActionSetting apply; public EnumSetting armWidth; public TextSetting skinFile = new TextSetting("Skin file", "") .describe("Full path to a 64x64 or 64x32 skin PNG") .visibleWhen(this.source, sourceMode -> sourceMode == SourceMode.LOCAL_FILE); private static final ResourceLocation RESOURCE_LOCATION = new ResourceLocation("onyx", "skins/local"); public TextSetting playerName; @Override protected void onEnable() { this.l(); } private static void L(String string, NotificationLevel notificationLevel) { NotificationDispatcher.d("SkinChanger", string, notificationLevel); } private void L(BufferedImage bufferedImage, String string, String string1) { if ((bufferedImage = new ImageBufferDownload().parseUserSkin(bufferedImage)) == null) { L("Not a valid skin image", NotificationLevel.ERROR); } else { mc.addScheduledTask(() -> { mc.getTextureManager().deleteTexture(RESOURCE_LOCATION); mc.getTextureManager().loadTexture(RESOURCE_LOCATION, new DynamicTexture(bufferedImage)); if (this.isEnabled()) { SkinOverride.L(RESOURCE_LOCATION, string); L(string1, NotificationLevel.SUCCESS); } }); } } private void d(String string) { EXECUTOR_SERVICE.submit( () -> { SkinChanger skinChanger1 = this; try { bufferedImage = ImageIO.read(new File(string)); } catch (IOException iOException) { Logger logger = OnyxClient.LOGGER; Object[] objects = new Object[2]; boolean bl = true; objects[0] = string; objects[1] = iOException; logger.warn("SkinChanger: failed to read '{}'", objects); L("Can't read that file", NotificationLevel.ERROR); return; } if (bufferedImage == null) { L("Not a readable PNG", NotificationLevel.ERROR); } else if (bufferedImage.getWidth() == 64 && (bufferedImage.getHeight() == 64 || bufferedImage.getHeight() == 32)) { skinChanger1.L(bufferedImage, skinChanger1.armWidth.is(ArmWidthMode.SLIM) ? "slim" : "default", "Skin applied"); } else { L( new StringBuilder() .insert(0, "Skin must be 64x64 or 64x32, not ") .append(bufferedImage.getWidth()) .append("x") .append(bufferedImage.getHeight()) .toString(), NotificationLevel.ERROR ); } } ); } private void L(String string) { SkinFetcher.L( string, skinFetcherData -> this.L( skinFetcherData.image(), skinFetcherData.modelType(), new StringBuilder().insert(0, "Wearing ").append(string).append("'s skin").toString() ), string -> { Logger logger = OnyxClient.LOGGER; Object[] objects = new Object[1]; boolean bl = true; objects[0] = stringx; logger.warn("SkinChanger: {}", objects); L(stringx, NotificationLevel.ERROR); } ); } @Override protected void onDisable() { SkinOverride.L(); } public SkinChanger() { super("SkinChanger", "Overrides your own skin texture locally", ModuleCategory.RENDER); this.browse = new ActionSetting("Browse", () -> FileDialogUtils.d("Select a skin PNG", this.skinFile.getValue(), this.skinFile::setValue)) .withButtonLabel("Browse…") .describe("Or paste the path into the field above") .visibleWhen(() -> this.source.is(SourceMode.LOCAL_FILE)); this.playerName = new TextSetting("Player name", "").visibleWhen(this.source, sourceMode -> sourceMode == SourceMode.PLAYER_NAME); this.armWidth = new EnumSetting<>("Arm width", ArmWidthMode.DEFAULT) .describe("Slim (Alex) vs default (Steve) - can't be detected from a bare local PNG") .visibleWhen(this.source, sourceMode -> sourceMode == SourceMode.LOCAL_FILE); this.apply = new ActionSetting("Apply", () -> { if (this.source.is(SourceMode.LOCAL_FILE)) { if (this.skinFile.getValue().isEmpty()) { L("Pick a skin PNG first", NotificationLevel.WARNING); } else { this.d(this.skinFile.getValue()); } } else if (this.playerName.getValue().isEmpty()) { L("Type a player name first", NotificationLevel.WARNING); } else { this.L(this.playerName.getValue()); } }).withButtonLabel("Apply skin"); this.armWidth.onChange(armWidthMode -> { if (this.isEnabled() && this.source.is(SourceMode.LOCAL_FILE) && SkinOverride.L()) { SkinOverride.L(SkinOverride.getResourceLocation(), armWidthMode == ArmWidthMode.SLIM ? "slim" : "default"); } }); } }