Skip to content

Commit

Permalink
Merge pull request #162 from Lodestone-Team/fix-flavour-type-bind
Browse files Browse the repository at this point in the history
lgtm
  • Loading branch information
Ynng authored Feb 20, 2023
2 parents c3d050e + a9b83b5 commit 3d205bf
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/bindings/FabricInstallerVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type FabricInstallerVersion = string;
3 changes: 3 additions & 0 deletions src/bindings/FabricLoaderVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type FabricLoaderVersion = string;
3 changes: 3 additions & 0 deletions src/bindings/ForgeBuildVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type ForgeBuildVersion = string;
16 changes: 15 additions & 1 deletion src/bindings/MinecraftFlavour.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { FabricInstallerVersion } from './FabricInstallerVersion';
import type { FabricLoaderVersion } from './FabricLoaderVersion';
import type { ForgeBuildVersion } from './ForgeBuildVersion';
import type { PaperBuildVersion } from './PaperBuildVersion';

export type MinecraftFlavour = 'vanilla' | 'fabric' | 'paper' | 'spigot';
export type MinecraftFlavour =
| 'vanilla'
| {
fabric: {
loader_version: FabricLoaderVersion | null;
installer_version: FabricInstallerVersion | null;
};
}
| { paper: { build_version: PaperBuildVersion | null } }
| 'spigot'
| { forge: { build_version: ForgeBuildVersion | null } };
3 changes: 3 additions & 0 deletions src/bindings/PaperBuildVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type PaperBuildVersion = bigint;
19 changes: 19 additions & 0 deletions src/bindings/impl/FlavourStringToMinecraftFlavour.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MinecraftFlavour } from 'bindings/MinecraftFlavour';

export function flavourStringToMinecraftFlavour(
flavour: string
): MinecraftFlavour {
switch (flavour.toLowerCase()) {
case 'vanilla':
return 'vanilla';
case 'fabric':
return { fabric: { loader_version: null, installer_version: null } };
case 'paper':
return { paper: { build_version: null } };
case 'spigot':
return 'spigot';
case 'forge':
return { forge: { build_version: null } };
}
throw new Error(`Unknown flavour: ${flavour}`);
}
10 changes: 3 additions & 7 deletions src/components/Minecraft/Create/MinecraftBasicForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useQuery } from '@tanstack/react-query';
import axios from 'axios';
import { MinecraftFlavour } from 'bindings/MinecraftFlavour';
import { MinecraftVersions } from 'bindings/MinecraftVersions';
import ComboField from 'components/Atoms/Form/ComboField';
import InputField from 'components/Atoms/Form/InputField';
Expand All @@ -10,12 +9,9 @@ import { MinecraftSetupConfigPrimitiveForm } from './form';

export default function MinecraftBasicForm() {
const { data: minecraftFlavours, isLoading: minecraftFlavoursLoading } =
useQuery<MinecraftFlavour[]>(['minecraft', 'flavours'], () =>
axios.get('/games/minecraft/flavours').then((res) => {
// sort by name
return res.data.sort((a: MinecraftFlavour, b: MinecraftFlavour) => {
return a.localeCompare(b);
});
useQuery<string[]>(['minecraft', 'flavours'], () =>
axios.get<Array<string>>('/games/minecraft/flavours').then((res) => {
return res.data;
})
);

Expand Down
3 changes: 2 additions & 1 deletion src/components/Minecraft/MinecraftCreateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import MinecraftAdvancedForm from './Create/MinecraftAdvancedForm';
import MinecraftBasicForm from './Create/MinecraftBasicForm';
import MinecraftNameForm from './Create/MinecraftNameForm';
import { flavourStringToMinecraftFlavour } from 'bindings/impl/FlavourStringToMinecraftFlavour';

function _renderStepContent(step: number) {
switch (step) {
Expand Down Expand Up @@ -61,7 +62,7 @@ export default function CreateMinecraftInstance({
) {
const parsedValues: MinecraftSetupConfigPrimitive = {
...values,
flavour: values.flavour as MinecraftFlavour,
flavour: flavourStringToMinecraftFlavour(values.flavour),
cmd_args: values.cmd_args.split(' ').filter((item) => item !== ''),
auto_start: values.auto_start === 'true',
restart_on_crash: values.restart_on_crash === 'true',
Expand Down

0 comments on commit 3d205bf

Please sign in to comment.