Skip to content

Commit aba7df3

Browse files
committed
Basic concept for the EULA feature to demo how this will all work
1 parent 505a9a6 commit aba7df3

File tree

8 files changed

+90
-2
lines changed

8 files changed

+90
-2
lines changed

app/Transformers/Api/Client/ServerTransformer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function transform(Server $server): array
6262
'cpu' => $server->cpu,
6363
],
6464
'invocation' => $service->handle($server, ! $this->getUser()->can(Permission::ACTION_STARTUP_READ, $server)),
65+
'egg_features' => $server->egg->inherit_features,
6566
'feature_limits' => [
6667
'databases' => $server->database_limit,
6768
'allocations' => $server->allocation_limit,

config/egg_features/eula.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Egg Feature: EULA Popup
7+
|--------------------------------------------------------------------------
8+
|
9+
| This popup is enabled for Minecraft eggs and allows a custom frontend
10+
| hook to run that monitors the console output of the server and pops up
11+
| a modal asking the user to accept it if necessary.
12+
|
13+
| There is no additional configuration necessary.
14+
|
15+
*/
16+
];

resources/scripts/api/server/getServer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface Server {
3030
cpu: number;
3131
threads: string;
3232
};
33+
eggFeatures: string[];
3334
featureLimits: {
3435
databases: number;
3536
allocations: number;
@@ -53,6 +54,7 @@ export const rawDataToServerObject = ({ attributes: data }: FractalResponseData)
5354
},
5455
description: data.description ? ((data.description.length > 0) ? data.description : null) : null,
5556
limits: { ...data.limits },
57+
eggFeatures: data.egg_features || [],
5658
featureLimits: { ...data.feature_limits },
5759
isSuspended: data.is_suspended,
5860
isInstalling: data.is_installing,

resources/scripts/components/server/ServerConsole.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ServerContentBlock from '@/components/elements/ServerContentBlock';
88
import ServerDetailsBlock from '@/components/server/ServerDetailsBlock';
99
import isEqual from 'react-fast-compare';
1010
import PowerControls from '@/components/server/PowerControls';
11+
import { EulaModalFeature } from '@feature/index';
1112

1213
export type PowerAction = 'start' | 'stop' | 'restart' | 'kill';
1314

@@ -16,6 +17,8 @@ const ChunkedStatGraphs = lazy(() => import(/* webpackChunkName: "graphs" */'@/c
1617

1718
const ServerConsole = () => {
1819
const isInstalling = ServerContext.useStoreState(state => state.server.data!.isInstalling);
20+
// @ts-ignore
21+
const eggFeatures: string[] = ServerContext.useStoreState(state => state.server.data!.eggFeatures, isEqual);
1922

2023
return (
2124
<ServerContentBlock title={'Console'} css={tw`flex flex-wrap`}>
@@ -41,6 +44,11 @@ const ServerConsole = () => {
4144
<ChunkedConsole/>
4245
<ChunkedStatGraphs/>
4346
</SuspenseSpinner>
47+
{eggFeatures.includes('eula') &&
48+
<React.Suspense fallback={null}>
49+
<EulaModalFeature/>
50+
</React.Suspense>
51+
}
4452
</div>
4553
</ServerContentBlock>
4654
);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React, { useEffect, useState } from 'react';
2+
import { ServerContext } from '@/state/server';
3+
import Modal from '@/components/elements/Modal';
4+
import tw from 'twin.macro';
5+
6+
const EulaModalFeature = () => {
7+
const [ visible, setVisible ] = useState(false);
8+
const status = ServerContext.useStoreState(state => state.status.value);
9+
const { connected, instance } = ServerContext.useStoreState(state => state.socket);
10+
11+
useEffect(() => {
12+
if (!connected || !instance || status === 'running') return;
13+
14+
const listener = (line: string) => {
15+
if (line.toLowerCase().indexOf('you need to agree to the eula in order to run the server') >= 0) {
16+
setVisible(true);
17+
}
18+
};
19+
20+
instance.addListener('console output', listener);
21+
22+
return () => {
23+
instance.removeListener('console output', listener);
24+
};
25+
}, [ connected, instance, status ]);
26+
27+
return (
28+
!visible ?
29+
null
30+
:
31+
<Modal visible onDismissed={() => setVisible(false)}>
32+
<h2 css={tw`text-3xl mb-4 text-neutral-100`}>EULA Not Accepted</h2>
33+
<p css={tw`text-neutral-200`}>
34+
It looks like you have not yet accepted the Minecraft EULA. In order to start this server you
35+
must set eula=true inside the eula.txt file in the File Manager.
36+
</p>
37+
</Modal>
38+
);
39+
};
40+
41+
export default EulaModalFeature;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { lazy } from 'react';
2+
3+
/**
4+
* Custom features should be registered here as lazy components so that they do
5+
* not impact the generated JS bundle size. They will be automatically loaded in
6+
* whenever they are actually loaded for the client (which may be never, depending
7+
* on the feature and the egg).
8+
*/
9+
const EulaModalFeature = lazy(() => import(/* webpackChunkName: "feature.eula" */'@feature/eula/EulaModalFeature'));
10+
11+
export { EulaModalFeature };

tsconfig.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,27 @@
1111
"esModuleInterop": true,
1212
"sourceMap": true,
1313
"baseUrl": ".",
14-
"lib": ["es2015", "dom"],
14+
"lib": [
15+
"es2015",
16+
"dom"
17+
],
1518
"importsNotUsedAsValues": "preserve",
1619
"paths": {
1720
"@/*": [
1821
"./resources/scripts/*"
22+
],
23+
"@feature/*": [
24+
"./resources/scripts/components/server/features/*"
1925
]
2026
},
2127
"plugins": [
2228
{
2329
"name": "typescript-plugin-tw-template"
2430
}
2531
],
26-
"typeRoots": ["node_modules/@types"]
32+
"typeRoots": [
33+
"node_modules/@types"
34+
]
2735
},
2836
"include": [
2937
"./resources/scripts/**/*"

webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ module.exports = {
6363
extensions: ['.ts', '.tsx', '.js', '.json'],
6464
alias: {
6565
'@': path.join(__dirname, '/resources/scripts'),
66+
'@feature': path.join(__dirname, '/resources/scripts/components/server/features'),
6667
},
6768
symlinks: false,
6869
},

0 commit comments

Comments
 (0)