From 11010ffda1ee091a6684a7e90304461ba53946b5 Mon Sep 17 00:00:00 2001 From: Stevertus Date: Sat, 6 Jan 2024 00:28:24 +0100 Subject: [PATCH] Return subcommands + Project supported formats --- lib/src/build/build.dart | 7 ++++++- lib/src/build/build_project.dart | 2 ++ lib/src/build/project.dart | 2 ++ lib/src/wrappers/effect.dart | 13 ++++++++----- lib/src/wrappers/return.dart | 20 +++++++++++++++++++- test/simple_wrappers_test.dart | 10 +++++++++- 6 files changed, 46 insertions(+), 8 deletions(-) diff --git a/lib/src/build/build.dart b/lib/src/build/build.dart index 18f7f78..f8c52c9 100644 --- a/lib/src/build/build.dart +++ b/lib/src/build/build.dart @@ -91,7 +91,12 @@ Map _getFiles(BuildProject prj, GenOptions options) { // create mcmeta if (!options.minified && prj.isGenMeta) { Map mcmeta = { - 'pack': {'pack_format': prj.pack_format, 'description': prj.description} + 'pack': { + 'pack_format': prj.pack_format, + 'description': prj.description, + if (prj.supportedFormats != null) + 'supported_formats': prj.supportedFormats, + } }; files['pack.mcmeta'] = json.encode(mcmeta); } diff --git a/lib/src/build/build_project.dart b/lib/src/build/build_project.dart index 41798bb..c595ace 100644 --- a/lib/src/build/build_project.dart +++ b/lib/src/build/build_project.dart @@ -9,6 +9,7 @@ class BuildProject { String name; String description; int pack_format; + List? supportedFormats; bool prod; bool isGen = true; bool isGenMeta = true; @@ -22,6 +23,7 @@ class BuildProject { name = prj.name, description = prj.description, pack_format = prj.getPackFormat(), + supportedFormats = prj.supportedFormats, context = Context(prod: false, version: prj.version) { packs = findPacks(prj.generate, context: context) .map( diff --git a/lib/src/build/project.dart b/lib/src/build/project.dart index 35131af..93bce45 100644 --- a/lib/src/build/project.dart +++ b/lib/src/build/project.dart @@ -7,6 +7,7 @@ class Project { final String description; final double version; final int? packFormat; + final List? supportedFormats; Widget generate; /// The project is a special Widget which is just defined once. @@ -29,6 +30,7 @@ class Project { this.version = 20.4, this.target = './', this.packFormat, + this.supportedFormats, this.description = 'This is a datapack generated with objd by Stevertus', }); diff --git a/lib/src/wrappers/effect.dart b/lib/src/wrappers/effect.dart index 3a8eaf5..bf5cec5 100644 --- a/lib/src/wrappers/effect.dart +++ b/lib/src/wrappers/effect.dart @@ -1,6 +1,6 @@ import 'package:objd/core.dart'; -class Effect extends RestActionAble { +class Effect extends RestActionAble implements GsonValue { EffectType? effect; Entity? entity; @@ -40,15 +40,18 @@ class Effect extends RestActionAble { Map getMap() { var ret = {}; - if (effect != null) ret['Id'] = EffectType.values.indexOf(effect!) + 1; - ret['Amplifier'] = Byte(amplifier != null ? amplifier! - 1 : 0); - ret['Duration'] = duration != null ? duration!.ticks : 0; - ret['ShowParticles'] = Byte( + if (effect != null) ret['id'] = EffectType.values.indexOf(effect!) + 1; + ret['amplifier'] = Byte(amplifier != null ? amplifier! - 1 : 0); + ret['duration'] = duration != null ? duration!.ticks : 0; + ret['show_particles'] = Byte( showParticles != null && showParticles! ? 1 : 0, ); return ret; } + @override + String toSimple() => gson.encode(getMap()); + @override Widget generate(Context context) { if (_type == 'clear') { diff --git a/lib/src/wrappers/return.dart b/lib/src/wrappers/return.dart index e0f6e15..1e4c7a2 100644 --- a/lib/src/wrappers/return.dart +++ b/lib/src/wrappers/return.dart @@ -3,6 +3,7 @@ import 'package:objd/core.dart'; /// Simple return command with integer return value class Return extends Widget { final int val; + final Widget? run; /// Simple return command with integer return value /// @@ -10,13 +11,30 @@ class Return extends Widget { /// ```dart /// Score(Entity.Self(),'test') << File('filename', child: Return(5)); /// ``` - Return(this.val); + Return(this.val) : run = null; + + /// Returns the result of a command + /// + /// The provided Widget should just return a single command! + Return.run(Widget this.run) : val = 0; + + /// Returns a failing state(success & value = 0) + Return.fail() + : val = 0, + run = Comment.LineBreak(); @override Widget generate(Context context) { if (context.version < 20) { throw "Return requires at least version 20"; } + if (run != null) { + if (run case Comment(text: '')) return Command('return fail'); + return Command( + 'return run ${getCommands(run!.generate(context), context: context).first}', + ); + } + return Command('return $val'); } } diff --git a/test/simple_wrappers_test.dart b/test/simple_wrappers_test.dart index 407f0cd..b63f750 100644 --- a/test/simple_wrappers_test.dart +++ b/test/simple_wrappers_test.dart @@ -62,7 +62,15 @@ void main() { ); }); - command('Return', Return(30), "return 30"); + group('Return', () { + command('integer', Return(30), "return 30"); + command('fail', Return.fail(), "return fail"); + command( + 'run', + Return.run(Data.get(Entity.All(), path: 'path')), + "return run data get entity @a path", + ); + }); group('Damage', () { final e = Entity.All();