Skip to content

Commit

Permalink
Merge branch '1.21' into 1.21.3
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/com/minelittlepony/common/client/gui/OutsideWorldRenderer.java
#	src/main/java/com/minelittlepony/common/client/gui/ScrollContainer.java
#	src/main/java/com/minelittlepony/common/client/gui/sprite/ItemStackSprite.java
#	src/main/java/com/minelittlepony/common/event/SkinFilterCallback.java
#	src/main/java/com/minelittlepony/common/mixin/MixinPlayerSkinTexture.java
  • Loading branch information
Sollace committed Nov 18, 2024
2 parents d9feff1 + ea226c9 commit 93e1697
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static BlockEntityRenderDispatcher configure(@Nullable World world) {
public static void renderStack(DrawContext context, ItemStack stack, int x, int y) {
try {
configure(null);
} catch (Throwable t) {}
} catch (Throwable ignored) {}
context.drawItem(stack, x, y);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ public void init(Runnable contentInitializer) {

@Override
public final void render(DrawContext context, int mouseX, int mouseY, float tickDelta) {
MatrixStack matrices = context.getMatrices();

context.enableScissor(margin.left, margin.top, margin.left + getBounds().width, margin.top + getBounds().height);

MatrixStack matrices = context.getMatrices();
matrices.push();
getBounds().translate(matrices);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
/**
* Represents a toggle button that switches between different
* styles as you toggle through its different states.
* <p>
* "Iconic" here refers to how it uses an icon instead of text
*
* @author Sollace
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.minelittlepony.common.client.gui.element;

import java.util.function.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import com.minelittlepony.common.client.gui.IField;
import com.minelittlepony.common.client.gui.style.IMultiStyled;
import com.minelittlepony.common.client.gui.style.Style;

/**
* Represents a toggle button that switches between different
* values as you toggle through its different states.
*
* @author Sollace
*
*/
public class Selector<T> extends Button implements IMultiStyled<Selector<T>>, IField<T, Selector<T>> {
private final Style defaultStyle;
private final T defaultValue;
private T value;

@NotNull
private IChangeCallback<T> action = IChangeCallback::none;

private final Function<T, Style> styleFunction;

public Selector(int x, int y, int width, int height, T value, Function<T, Style> styleFunction) {
super(x, y, width, height);
this.defaultValue = value;
this.value = value;
this.styleFunction = styleFunction;
this.defaultStyle = styleFunction.apply(value);
setStyle(this.defaultStyle);
}

@Override
public Selector<T> onChange(IChangeCallback<T> action) {
this.action = action;

return this;
}

@Override
public T getValue() {
return value;
}

@Override
public Selector<T> setValue(@Nullable T value) {
this.value = value == null ? defaultValue : value;
this.setStyle(styleFunction.apply(this.value));

return this;
}

/**
* Sets the styles to use for each state this toggle is able to be in.
* The number of styles here determines the number of possible states
* and the value is the index to the array of styles.
*/
@Override
public Selector<T> setStyles(Style... styles) {
return this;
}

@Override
public Style[] getStyles() {
return new Style[] {getStyle()};
}

@Override
public void onPress() {
setValue(action.perform(getValue()));
super.onPress();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* @author Sollace
*/
public class Slider extends AbstractSlider<Float> {

public Slider(int x, int y, float min, float max, Supplier<? extends Number> value) {
this(x, y, min, max, Objects.requireNonNull(value.get(), "value was null").floatValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import net.minecraft.component.type.DyedColorComponent;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Colors;

public class ItemStackSprite implements ISprite {

private ItemStack stack = ItemStack.EMPTY;

private int tint = 0xFFFFFFFF;
private int tint = Colors.WHITE;

private boolean renderFailed;
private boolean needsWorld;
Expand All @@ -24,11 +25,14 @@ public ItemStackSprite setStack(ItemConvertible iitem) {

public ItemStackSprite setStack(ItemStack stack) {
this.stack = stack;
renderFailed = false;
needsWorld = false;

return setTint(tint);
}

public ItemStackSprite setTint(int tint) {
this.tint = tint;
stack.set(DataComponentTypes.DYED_COLOR, new DyedColorComponent(tint, true));
return this;
}
Expand All @@ -41,7 +45,7 @@ public void render(DrawContext context, int x, int y, int mouseX, int mouseY, fl

if (!needsWorld) {
try {
context.drawItem(stack, x, y);
context.drawItem(stack, x + 2, y + 2);
RenderSystem.disableDepthTest();
return;
} catch (Throwable ignored) {
Expand All @@ -51,7 +55,7 @@ public void render(DrawContext context, int x, int y, int mouseX, int mouseY, fl

try {
OutsideWorldRenderer.configure(null);
context.drawItem(stack, x, y);
context.drawItem(stack, x + 2, y + 2);
RenderSystem.disableDepthTest();
} catch (Throwable ignored) {
renderFailed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public NativeImage processImage(NativeImage image, int initialWidth, int initial
return image;
}

@Deprecated
@Override
public boolean shouldAllowTransparency(NativeImage image, int initialWidth, int initialHeight) {
for (SkinFilterCallback event : listeners) {
Expand All @@ -39,14 +40,34 @@ default boolean shouldAllowTransparency(NativeImage image, int initialWidth, int
return true; // default is true since in most cases this is the desired effect
}

/**
* Checks whether the incoming image size corresponds to the old 1:2 ratio player skins.
*/
static boolean isLegacyAspectRatio(int width, int height) {
return width == height * 2;
}

/**
* Gets the scale of the image as a multiple of its vanilla image width.
*/
static int getResolutionScale(int width, int height) {
return width / VANILLA_SKIN_WIDTH;
}

/**
* Fills a scaled section of an image with a solid color.
* @param image
* @param xFrom
* @param yFrom
* @param xTo
* @param yTo
* @param color
*/
static void fill(NativeImage image, int xFrom, int yFrom, int xTo, int yTo, int color) {
int scale = getResolutionScale(image.getWidth(), image.getHeight());
image.fillRect(xFrom * scale, yFrom * scale, xTo * scale, yTo * scale, color);
}

/**
* Copies a scaled section from one region to another.
*
Expand All @@ -64,7 +85,7 @@ static void copy(NativeImage image,
int xOffset, int yOffset,
int width, int height,
boolean mirrorX, boolean mirrorY) {
int scale = image.getWidth() / 64;
int scale = getResolutionScale(image.getWidth(), image.getHeight());
image.copyRect(
xFrom * scale, yFrom * scale,
xOffset * scale, yOffset * scale,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,52 @@
package com.minelittlepony.common.mixin;

import com.llamalad7.mixinextras.sugar.Share;
import com.llamalad7.mixinextras.sugar.ref.LocalIntRef;
import com.minelittlepony.common.event.SkinFilterCallback;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.client.texture.PlayerSkinTexture;
import net.minecraft.client.texture.ResourceTexture;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(PlayerSkinTexture.class)
public abstract class MixinPlayerSkinTexture extends ResourceTexture {
private MixinPlayerSkinTexture() { super(null); }

private static int initialWidth;
private static int initialHeight;
private static final String FILTER_IMAGE = "remapTexture(Lnet/minecraft/client/texture/NativeImage;)Lnet/minecraft/client/texture/NativeImage;";
private static final String STRIP_COLOR = "net/minecraft/client/texture/PlayerSkinTexture.stripColor(Lnet/minecraft/client/texture/NativeImage;IIII)V";
private static final String STRIP_ALPHA = "net/minecraft/client/texture/PlayerSkinTexture.stripAlpha(Lnet/minecraft/client/texture/NativeImage;IIII)V";

@Inject(method = "remapTexture", at = @At("HEAD"))
private void beforeUpdate(NativeImage image, CallbackInfoReturnable<NativeImage> info) {
initialWidth = image.getWidth();
initialHeight = image.getHeight();
@Inject(method = FILTER_IMAGE, at = @At("HEAD"))
private void beforeUpdate(NativeImage image,
CallbackInfoReturnable<NativeImage> ci,
@Share(value = "kirinmlp_initialWidth") LocalIntRef initialWidth,
@Share(value = "kirinmlp_initialHeight") LocalIntRef initialHeight) {
initialWidth.set(image.getWidth());
initialHeight.set(image.getHeight());
}

@Inject(method = "remapTexture", at = @At("RETURN"))
private void update(NativeImage image, CallbackInfoReturnable<NativeImage> ci) {
@Inject(method = FILTER_IMAGE, at = @At("RETURN"), cancellable = true)
private void update(NativeImage image,
CallbackInfoReturnable<NativeImage> ci,
@Share(value = "kirinmlp_initialWidth") LocalIntRef initialWidth,
@Share(value = "kirinmlp_initialHeight") LocalIntRef initialHeight) {
// convert skins from mojang server
ci.setReturnValue(SkinFilterCallback.EVENT.invoker().processImage(ci.getReturnValue(), initialWidth, initialHeight));
ci.setReturnValue(SkinFilterCallback.EVENT.invoker().processImage(ci.getReturnValue(), initialWidth.get(), initialHeight.get()));
}

// Sorry, Mahjon. Input validation is good 'n all, but this interferes with our other mods.
@Inject(method = "stripAlpha", at = @At("HEAD"), cancellable = true)
private static void cancelAlphaStrip(NativeImage image, int beginX, int beginY, int endX, int endY, CallbackInfo info) {
if (SkinFilterCallback.EVENT.invoker().shouldAllowTransparency(image, initialWidth, initialHeight)) {
info.cancel();
}
}

@Inject(method = "stripColor", at = @At("HEAD"), cancellable = true)
private static void cancelColorStrip(NativeImage image, int beginX, int beginY, int endX, int endY, CallbackInfo info) {
if (SkinFilterCallback.EVENT.invoker().shouldAllowTransparency(image, initialWidth, initialHeight)) {
info.cancel();
@Inject(method = FILTER_IMAGE, at = {
@At(value = "INVOKE", target = STRIP_ALPHA),
@At(value = "INVOKE", target = STRIP_COLOR)
}, cancellable = true)
private void cancelAlphaStrip(NativeImage image, CallbackInfoReturnable<NativeImage> ci,
@Share(value = "kirinmlp_initialWidth") LocalIntRef initialWidth,
@Share(value = "kirinmlp_initialHeight") LocalIntRef initialHeight) {
if (SkinFilterCallback.EVENT.invoker().shouldAllowTransparency(image, initialWidth.get(), initialHeight.get())) {
ci.cancel();
}
}
// -
Expand Down

0 comments on commit 93e1697

Please sign in to comment.