Skip to content

Commit

Permalink
feat: some progress in GenerateModScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed May 4, 2023
1 parent dbc3cbf commit c98bf94
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .run/[Windows] Run Client (Debug).run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</envs>
<option name="MAIN_CLASS_NAME" value="net.blueberrymc.client.main.ClientMain" />
<module name="Blueberry-Parent.blueberry.main" />
<option name="PROGRAM_PARAMETERS" value="--debug --version=debug --assetIndex=1.19 --versionType=debug --username=&quot;Dev&quot; --userType=mojang --uuid=&quot;dev&quot; --accessToken=abc --assetsDir=&quot;${APPDATA}\.minecraft\assets&quot; --mixin=mixins.blueberry.json" />
<option name="PROGRAM_PARAMETERS" value="--debug --version=debug --assetIndex=3 --versionType=debug --username=&quot;Dev&quot; --userType=mojang --uuid=&quot;dev&quot; --accessToken=abc --assetsDir=&quot;${APPDATA}\.minecraft\assets&quot; --mixin=mixins.blueberry.json" />
<option name="VM_PARAMETERS" value="-Dlog4j.configuration=&quot;$ProjectFileDir$/Blueberry-Client/src/main/resources/log4j2-debug.xml&quot; -Dblueberry.recursion.detector.enabled=true -Xmx8G -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M -Dmixin.debug=true -Dmixin.debug.strict=true -Dmixin.checks=true" />
<option name="WORKING_DIRECTORY" value="$ProjectFileDir$/work/testClient" />
<extension name="coverage">
Expand All @@ -19,4 +19,4 @@
<option name="Make" enabled="true" />
</method>
</configuration>
</component>
</component>
4 changes: 2 additions & 2 deletions .run/[Windows] Run Client.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</envs>
<option name="MAIN_CLASS_NAME" value="net.blueberrymc.client.main.ClientMain" />
<module name="Blueberry-Parent.blueberry.main" />
<option name="PROGRAM_PARAMETERS" value="--version=debug --assetIndex=1.19 --versionType=debug --username=&quot;Dev&quot; --uuid=&quot;abc&quot; --accessToken=&quot;abc&quot; --assetsDir=&quot;${APPDATA}\.minecraft\assets&quot; --mixin=mixins.blueberry.json" />
<option name="PROGRAM_PARAMETERS" value="--version=debug --assetIndex=3 --versionType=debug --username=&quot;Dev&quot; --uuid=&quot;abc&quot; --accessToken=&quot;abc&quot; --assetsDir=&quot;${APPDATA}\.minecraft\assets&quot; --mixin=mixins.blueberry.json" />
<option name="VM_PARAMETERS" value="-Xmx8G -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M" />
<option name="WORKING_DIRECTORY" value="$ProjectFileDir$/work/testClient" />
<extension name="coverage">
Expand All @@ -19,4 +19,4 @@
<option name="Make" enabled="true" />
</method>
</configuration>
</component>
</component>
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public FileList(@NotNull Minecraft minecraft) {
addEntry(new Entry("[ " + root + " ]", root));
}
}
addEntry(new Entry("./", options.getInitialDirectory()));
File parent = options.getInitialDirectory().getParentFile();
if (parent != null && FileUtil.isFileInsideBoundary(options.boundary(), parent)) {
addEntry(new Entry("../", parent));
Expand Down Expand Up @@ -162,8 +163,8 @@ public FileList(@NotNull Minecraft minecraft) {
}).bounds(0, 0, 20, 20).build();
cdButton.visible = false;
selectButton = Button.builder(Component.literal("✔").withStyle(ChatFormatting.GREEN), (button) -> {
minecraft.setScreen(previousScreen);
options.runCallback(getSelected().file);
minecraft.setScreen(previousScreen);
}).bounds(0, 0, 20, 20).build();
selectButton.visible = false;
addRenderableWidget(cdButton);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
package net.blueberrymc.client.gui.screens;

import com.mojang.blaze3d.vertex.PoseStack;
import net.blueberrymc.common.Blueberry;
import net.blueberrymc.common.resources.BlueberryText;
import net.blueberrymc.config.ModDescriptionFile;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class GenerateModScreen extends BlueberryScreen {
private final Screen previousScreen;
private final List<Object> blockers = new ArrayList<>();
private final List<Consumer<PoseStack>> callbacks = new ArrayList<>();
private String modName = "";
private String modId = "";
private String targetDirectory = "";
private Button generateButton;

public GenerateModScreen(@Nullable Screen previousScreen) {
super(BlueberryText.text("blueberry", "gui.screens.generate_mod.title"));
Expand All @@ -18,16 +39,158 @@ public GenerateModScreen(@Nullable Screen previousScreen) {

@Override
protected void init() {
blockers.clear();
assert this.minecraft != null;
int offset = 38;
int _maxWidth = 50;
var allTexts = List.of(
BlueberryText.text("blueberry", "gui.screens.generate_mod.mod_name"),
BlueberryText.text("blueberry", "gui.screens.generate_mod.mod_id"),
BlueberryText.text("blueberry", "gui.screens.generate_mod.target_directory")
);
for (var component : allTexts) {
int textWidth = font.width(component);
if (textWidth > _maxWidth) _maxWidth = textWidth;
}
final int maxWidth = _maxWidth;
BiConsumer<Component, Integer> addLabel = (component, finalOffset) -> callbacks.add(poseStack -> {
drawString(poseStack, font, component, this.width / 2 - this.width / 6 - maxWidth - 6, finalOffset + 6, 0xFFFFFF);
});
this.addRenderableWidget(
Button.builder(CommonComponents.GUI_BACK, (button) -> this.minecraft.setScreen(previousScreen))
.bounds(this.width / 2 - 50, this.height - 38, 96, 20)
.bounds(this.width / 2 + 2, this.height - 38, 96, 20)
.build());
generateButton = this.addRenderableWidget(Button
.builder(
BlueberryText.text("blueberry", "gui.screens.generate_mod.generate"),
(button) -> {
// TODO
})
.bounds(this.width / 2 - 98, this.height - 38, 96, 20)
.build());
addLabel.accept(allTexts.get(0), offset);
var modNameEditBox = new EditBox(font, width / 2 - width / 6 + 6, offset, width / 3, 20, Component.literal("Mod name"));
modNameEditBox.setMaxLength(255);
modNameEditBox.setValue(modName);
this.addRenderableWidget(modNameEditBox);
offset += 24;
addLabel.accept(allTexts.get(1), offset);
var modIdEditBox = new EditBox(font, width / 2 - width / 6 + 6, offset, width / 3, 20, Component.literal("Mod name"));
modIdEditBox.setMaxLength(255);
modIdEditBox.setValue(modId);
this.addRenderableWidget(modIdEditBox);
offset += 24;
addLabel.accept(allTexts.get(2), offset);
var targetDirectoryEditBox = new EditBox(font, width / 2 - width / 6 + 30, offset, width / 3 - 24, 20, Component.literal("Target directory"));
targetDirectoryEditBox.setMaxLength(2048);
targetDirectoryEditBox.setValue(targetDirectory);
targetDirectoryEditBox.setSuggestion(File.separator + modName);
modNameEditBox.setResponder(it -> {
modName = it;
targetDirectoryEditBox.setSuggestion(File.separator + modName);
checkTargetDirectory(targetDirectoryEditBox);
String projectedModId = it.replace(' ', '-').toLowerCase(Locale.ROOT);
if (!ModDescriptionFile.MOD_ID_PATTERN.matcher(projectedModId).matches()) {
modIdEditBox.setValue(projectedModId);
modIdEditBox.setTextColor(0xFF0000);
block(modIdEditBox);
} else {
modIdEditBox.setTextColor(0xFFFFFF);
modIdEditBox.setValue(projectedModId);
modId = projectedModId;
unblock(modIdEditBox);
}
});
modIdEditBox.setResponder(it -> {
if (!ModDescriptionFile.MOD_ID_PATTERN.matcher(it).matches()) {
modIdEditBox.setTextColor(0xFF0000);
block(modIdEditBox);
} else {
modIdEditBox.setTextColor(0xFFFFFF);
modId = it;
unblock(modIdEditBox);
}
});
targetDirectoryEditBox.setResponder(it -> {
targetDirectoryEditBox.setSuggestion(File.separator + modName);
checkTargetDirectory(targetDirectoryEditBox);
});
this.addRenderableWidget(targetDirectoryEditBox);
this.addRenderableWidget(
Button.builder(Component.literal("..."), (button) ->
this.minecraft.setScreen(FileDialogScreen.create(
this,
FileDialogScreenOptions
.builder()
//.boundary(Blueberry.getGameDir()) // we probably don't need boundary
.fileType(FileDialogScreenOptions.FileType.DIRECTORY)
.initialDirectory(Blueberry.getModsDir())
.title(BlueberryText.text("blueberry", "gui.screens.generate_mod.select.title"))
.callback(file -> {
if (file != null) {
targetDirectory = file.getAbsolutePath();
}
})
.build()
)
)
)
.bounds(this.width / 2 - this.width / 6 + 6, offset, 20, 20)
.build());
checkTargetDirectory(targetDirectoryEditBox);
}

private void checkTargetDirectory(EditBox editBox) {
if (editBox.getValue().isBlank()) {
block(editBox);
return;
}
if (modName.isBlank()) {
editBox.setTextColor(0xFF0000);
editBox.setTooltip(Tooltip.create(Component.literal("Mod Name is empty").withStyle(ChatFormatting.RED)));
block(editBox);
return;
}
try {
File file = new File(editBox.getValue() + File.separator + modName);
if (file.exists()) {
editBox.setTextColor(0xFF0000);
editBox.setTooltip(Tooltip.create(Component.literal("Directory already exists").withStyle(ChatFormatting.RED)));
block(editBox);
return;
}
} catch (Exception e) {
editBox.setTextColor(0xFF0000);
editBox.setTooltip(Tooltip.create(Component.literal(e.getMessage()).withStyle(ChatFormatting.RED)));
block(editBox);
return;
}
targetDirectory = editBox.getValue();
editBox.setTextColor(0xFFFFFF);
editBox.setTooltip(null);
unblock(editBox);
}

private void block(Object blocker) {
if (!blockers.contains(blocker)) blockers.add(blocker);
generateButton.active = false;
generateButton.setTooltip(Tooltip.create(Component.literal("Blocked by:\n" + blockers.stream().map(o -> ((AbstractWidget) o).getMessage().getString()).collect(Collectors.joining("\n")))));
}

private void unblock(Object blocker) {
blockers.remove(blocker);
generateButton.active = blockers.isEmpty();
if (blockers.isEmpty()) {
generateButton.setTooltip(null);
} else {
generateButton.setTooltip(Tooltip.create(Component.literal("Blocked by:\n" + blockers.stream().map(o -> ((AbstractWidget) o).getMessage().getString()).collect(Collectors.joining("\n")))));
}
}

@Override
public void render(@NotNull PoseStack poseStack, int mouseX, int mouseY, float deltaFrameTime) {
renderBackground(poseStack);
for (var callback : callbacks) callback.accept(poseStack);
super.render(poseStack, mouseX, mouseY, deltaFrameTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void main(@NotNull String@NotNull[] args) throws IOException {
OptionSet set = optionParser.parse(args); // use args because we don't need --tweakClass here
if (!set.has("assetsDir")) {
LOGGER.info("Auto-detected .minecraft directory: " + FileUtil.getMinecraftDir().getAbsolutePath());
arguments.add("--assetsDir=" + new File(FileUtil.getMinecraftDir(), "assets").getAbsolutePath() + "");
arguments.add("--assetsDir=" + new File(FileUtil.getMinecraftDir(), "assets").getAbsolutePath());
}
ServerMain.launch(Side.CLIENT, arguments, set, gameDirOption, sourceDirOption, includeDirOption);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.regex.Pattern;

public class ModDescriptionFile implements VersionedModInfo {
private static final Pattern MOD_ID_PATTERN = Pattern.compile("^[a-zA-Z0-9][a-zA-Z0-9_-]*$");
public static final Pattern MOD_ID_PATTERN = Pattern.compile("^[a-zA-Z0-9][a-zA-Z0-9_-]*$");
private static final Logger LOGGER = LogManager.getLogger();
@NotNull protected final String modId;
@NotNull protected final String version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void launch(String[] args) {
BlueberryPreBootstrap.destroyUniversalClassLoader();
String launchTarget = Objects.requireNonNull(primaryTweaker).getLaunchTarget();
// Class<?> clazz = Class.forName(launchTarget, false, classLoader);
Class<?> clazz = classLoader.findClass(launchTarget);
Class<?> clazz = Class.forName(launchTarget);
LOGGER.info("Loaded class {} from {}", clazz.getTypeName(), ClasspathUtil.getClasspath(clazz));
Method mainMethod = clazz.getMethod("main", String[].class);
LOGGER.info("Launching wrapped minecraft {}", launchTarget);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
"gui.screens.mods.reload.unsupported": "Reload not supported on this mod",
"gui.screens.mods.reload.reload_tooltip": "Reload the mod (might not work on some mods!)",
"gui.screens.generate_mod.title": "Generate a mod",
"gui.screens.generate_mod.mod_name": "Mod Name",
"gui.screens.generate_mod.mod_id": "Mod ID",
"gui.screens.generate_mod.target_directory": "Target Directory",
"gui.screens.generate_mod.generate": "Generate",
"gui.screens.generate_mod.select.title": "Select target directory",
"gui.screens.mod_config.deprecated": "**Deprecated**",
"gui.screens.mod_config.experimental": "**Experimental**",
"gui.screens.mod_config.default": "[Default: %s]",
Expand Down

0 comments on commit c98bf94

Please sign in to comment.