Skip to content

Commit

Permalink
Merge branch '1.19.2' into 1.18.2
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/net/uku3lig/ukulib/config/screen/ColorSelectScreen.java
  • Loading branch information
uku3lig committed Dec 31, 2022
2 parents 25bec9d + 18e2d55 commit 21d718b
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 16 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ dependencies {

include(implementation("com.moandjiezana.toml:toml4j:$toml4jVersion")!!)
include(implementation("gs.mclo.java:mclogs-java:2.1.1")!!)

include(modImplementation(fabricApi.module("fabric-resource-loader-v0", fabricVersion))!!)
include(modImplementation(fabricApi.module("fabric-command-api-v2", fabricVersion))!!)
}

tasks.processResources {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ yarnMappings=1.18.2+build.4
loaderVersion=0.14.12

# Mod Properties
modVersion=0.6.0
modVersion=0.6.1
mavenGroup=net.uku3lig
archivesBaseName=ukulib

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.uku3lig.ukulib.config.impl;

import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import net.uku3lig.ukulib.config.ConfigManager;
import net.uku3lig.ukulib.config.screen.TextInputScreen;

import java.util.Optional;
import java.util.function.Consumer;

/**
* Basic implementation of {@link TextInputScreen} used to input a {@link String} of any type.
*/
public class StringInputScreen extends TextInputScreen<String> {
/**
* Creates an input screen.
*
* @param parent The parent screen
* @param title The title of the screen
* @param label The label to be shown above the text input field
* @param callback The action to be performed when the value is changed
* @param last The last known value
* @param manager The config manager, used to save the config
*/
public StringInputScreen(Screen parent, Text title, Text label, Consumer<String> callback, String last, ConfigManager<?> manager) {
super(parent, title, label, callback, last, manager);
}

@Override
protected Optional<String> convert(String value) {
return Optional.ofNullable(value);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.uku3lig.ukulib.config.screen;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ConfirmChatLinkScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.util.math.MatrixStack;
Expand All @@ -12,53 +10,92 @@
import java.util.Optional;
import java.util.function.Consumer;

/**
* A color selection screen.
*/
public class ColorSelectScreen extends TextInputScreen<Integer> {
/**
* Constant for full alpha (255) with no color. <br>
* Can be used like this: <code>color |= FULL_ALPHA</code>.
*/
public static final int FULL_ALPHA = 0xFF000000;

/**
* Creates a color selection screen.
* @param title The title of the screen
* @param parent The parent screen
* @param callback The action to be done after the screen is closed
* @param last The last known color
* @param manager The config manager
*/
public ColorSelectScreen(Text title, Screen parent, Consumer<Integer> callback, int last, ConfigManager<?> manager) {
super(parent, title, Text.of("Color"), callback, last, manager);
}

@Override
protected void init() {
super.init();
addDrawableChild(new ButtonWidget(this.width / 2 - 100, this.height - 51, 200, 20, Text.of("Open Web Color Picker"), button ->
MinecraftClient.getInstance().setScreen(new ConfirmChatLinkScreen(confirmed -> {
if (confirmed) Util.getOperatingSystem().open("https://colors-picker.com/hex-color-picker/");
MinecraftClient.getInstance().setScreen(this);
}, "https://colors-picker.com/hex-color-picker/", true))));
addDrawableChild(new ButtonWidget(this.width / 2 - 100, this.height - 51, 200, 20, Text.of("Open Web Color Picker"),
button -> Util.getOperatingSystem().open("https://colors-picker.com/hex-color-picker/")));
}

@Override
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
super.render(matrices, mouseX, mouseY, delta);
convert(textField.getText()).ifPresent(color -> renderColor(matrices, mouseX, mouseY, delta, color));
convert(getTextField().getText()).ifPresent(color -> renderColor(matrices, mouseX, mouseY, delta, color));
}

/**
* Renders the selected color on the screen
* @param matrices The matrix stack
* @param mouseX The x position of the mouse
* @param mouseY The y position of the mouse
* @param delta The time delta
* @param color The chosen color
*/
@SuppressWarnings("unused")
protected void renderColor(MatrixStack matrices, int mouseX, int mouseY, float delta, int color) {
int x = this.width / 2 + 105;
int y = 116;
fill(matrices, x, y, x + 20, y + 20, color);
}

/**
* Sets if alpha is allowed along with the RGB color.
* @return <code>true</code> if allowed, <code>false</code> otherwise
*/
protected boolean allowAlpha() {
return true;
}

/**
* The default alpha value for the color. Default disables alpha.
* @return The default alpha value
*/
protected byte defaultAlpha() {
return (byte) 0xFF;
}

@Override
public Optional<Integer> convert(String value) {
try {
value = value.replace("#", "");
if (value.length() != 8 && value.length() != 6) return Optional.empty();

int color = Integer.parseUnsignedInt(value, 16);
if (color <= 0xFFFFFF) color |= FULL_ALPHA;
if (value.length() == 6 || (value.length() == 8 && allowAlpha())) {
int color = Integer.parseUnsignedInt(value, 16);
if (color <= 0xFFFFFF) color |= (defaultAlpha() << 24);

return Optional.of(color);
return Optional.of(color);
} else {
return Optional.empty();
}
} catch (Exception e) {
return Optional.empty();
}
}

@Override
public String format(Integer value) {
if (!allowAlpha()) value &= 0x00FFFFFF;
return "#" + Integer.toHexString(value);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.uku3lig.ukulib.config.screen;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
Expand Down Expand Up @@ -27,7 +29,12 @@ public abstract class TextInputScreen<T> extends CloseableScreen {
private final T last;
private final ConfigManager<?> manager;

TextFieldWidget textField;
/**
* The text field widget.
* @return The widget instance
*/
@Getter(AccessLevel.PROTECTED)
private TextFieldWidget textField;

/**
* Creates an input screen.
Expand All @@ -52,7 +59,10 @@ protected void init() {
final ButtonWidget doneButton = this.addDrawableChild(Ukutils.doneButton(this.width, this.height, this.parent));
textField = this.addDrawableChild(new TextFieldWidget(this.textRenderer, this.width / 2 - 100, 116, 200, 20, label));
textField.setText(format(last));
textField.setChangedListener(s -> doneButton.active = convert(s).isPresent());
textField.setChangedListener(s -> {
doneButton.active = convert(s).isPresent();
onTextChange(s);
});
}

/**
Expand All @@ -73,6 +83,15 @@ protected String format(T value) {
return String.valueOf(value);
}

/**
* Called when the text field value is changed
* @param value The new value
*/
@SuppressWarnings("unused")
protected void onTextChange(String value) {
// empty
}

@Override
public final void removed() {
convert(textField.getText()).ifPresent(t -> {
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/net/uku3lig/ukulib/utils/PlayerArgumentType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package net.uku3lig.ukulib.utils;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.command.CommandSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.Text;

import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;

/**
* An argument type which represents players. Can only be used in client commands.
*/
public class PlayerArgumentType implements ArgumentType<PlayerArgumentType.PlayerSelector> {
/**
* The exception thrown when the selected player is not found.
*/
public static final SimpleCommandExceptionType PLAYER_NOT_FOUND_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("name.entity.notfound.player"));

/**
* Returns a new instance.
* @return The instance
*/
public static PlayerArgumentType player() {
return new PlayerArgumentType();
}

/**
* Gets the player passed to the command.
* @param name The name of the argument
* @param context The command context
* @return The player entity
* @throws CommandSyntaxException if the player is not found
*/
public static PlayerEntity getPlayer(String name, CommandContext<FabricClientCommandSource> context) throws CommandSyntaxException {
PlayerSelector selector = context.getArgument(name, PlayerSelector.class);

return context.getSource().getWorld().getPlayers().stream()
.filter(p -> p.getEntityName().equalsIgnoreCase(selector.name) || p.getUuidAsString().equalsIgnoreCase(selector.name))
.findFirst()
.orElseThrow(PLAYER_NOT_FOUND_EXCEPTION::create);
}

@Override
public PlayerSelector parse(StringReader reader) throws CommandSyntaxException {
return new PlayerSelector(reader.readString());
}

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
if (context.getSource() instanceof FabricClientCommandSource source) {
return CommandSource.suggestMatching(source.getWorld().getPlayers().stream().map(PlayerEntity::getEntityName), builder);
} else {
return CommandSource.suggestMatching(Collections.emptyList(), builder);
}
}

@Override
public Collection<String> getExamples() {
return Collections.singleton("uku3lig");
}

/**
* Simple record used to store the player name for later use
* @param name The player's name or {@link java.util.UUID}
*/
public record PlayerSelector(String name) {
}
}
43 changes: 43 additions & 0 deletions src/main/java/net/uku3lig/ukulib/utils/SilentButtonWidget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.uku3lig.ukulib.utils;

import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.sound.SoundManager;
import net.minecraft.text.Text;

/**
* A button that doesn't make noise.
*/
@SuppressWarnings("unused")
public class SilentButtonWidget extends ButtonWidget {
/**
* Creates a new silent button
* @param x The x position of the button
* @param y The y position of the button
* @param width The width of the button
* @param height The height of the button
* @param message The text of the button
* @param onPress The action to be done when the button is pressed
* @param tooltipSupplier The tooltip supplier
*/
protected SilentButtonWidget(int x, int y, int width, int height, Text message, PressAction onPress, TooltipSupplier tooltipSupplier) {
super(x, y, width, height, message, onPress, tooltipSupplier);
}

/**
* Creates a new silent button
* @param x The x position of the button
* @param y The y position of the button
* @param width The width of the button
* @param height The height of the button
* @param message The text of the button
* @param onPress The action to be done when the button is pressed
*/
public SilentButtonWidget(int x, int y, int width, int height, Text message, PressAction onPress) {
this(x, y, width, height, message, onPress, EMPTY);
}

@Override
public void playDownSound(SoundManager soundManager) {
// don't play anything
}
}
10 changes: 10 additions & 0 deletions src/main/resources/assets/ukulib/lang/zh_cn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"ukulib.position": "位置",
"ukulib.position.topLeft": "左上角",
"ukulib.position.topRight": "右上角",
"ukulib.position.bottomLeft": "左下角",
"ukulib.position.bottomRight": "右下角",
"ukulib.position.middle": "在经验栏上方",
"ukulib.position.desc": "点击屏幕上的某个地方来改变元素的位置。",
"ukulib.position.desc.move": "按§e方向键§r来精确调整元素的位置。"
}

0 comments on commit 21d718b

Please sign in to comment.