Skip to content

Commit

Permalink
First version of the new Item type
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Sep 10, 2023
1 parent f73b3dd commit 17f18ea
Show file tree
Hide file tree
Showing 25 changed files with 587 additions and 163 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
package me.gabber235.typewriter.entries.action

import lirand.api.extensions.inventory.meta
import me.gabber235.typewriter.adapters.Colors
import me.gabber235.typewriter.adapters.Entry
import me.gabber235.typewriter.adapters.modifiers.Colored
import me.gabber235.typewriter.adapters.modifiers.Help
import me.gabber235.typewriter.adapters.modifiers.MultiLine
import me.gabber235.typewriter.adapters.modifiers.Placeholder
import me.gabber235.typewriter.entry.Criteria
import me.gabber235.typewriter.entry.Modifier
import me.gabber235.typewriter.entry.entries.ActionEntry
import me.gabber235.typewriter.utils.Icons
import me.gabber235.typewriter.utils.asMini
import org.bukkit.Material
import me.gabber235.typewriter.utils.Item
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta

@Entry("give_item", "Give an item to the player", Colors.RED, Icons.WAND_SPARKLES)
/**
Expand All @@ -32,34 +25,11 @@ class GiveItemActionEntry(
override val modifiers: List<Modifier>,
override val triggers: List<String> = emptyList(),
@Help("The item to give.")
// The Minecraft material of the item to give.
private val material: Material = Material.AIR,
@Help("The amount of items to give.")
private val amount: Int = 1,
@Colored
@Placeholder
@Help("The display name of the item. (Defaults to the item's display name)")
// The display name of the item to give. If not specified, the item will have it's default display name.
private val displayName: String = "",
@MultiLine
@Colored
@Placeholder
@Help("The lore of the item. (Defaults to the item's lore)")
// The lore of the item to give. If not specified, the item will have it's default lore.
private val lore: String,
val item: Item = Item.Empty,
) : ActionEntry {
override fun execute(player: Player) {
super.execute(player)

val item = ItemStack(material, amount).meta<ItemMeta> {
if (this@GiveItemActionEntry.displayName.isNotBlank()) displayName(this@GiveItemActionEntry.displayName.asMini())
if (this@GiveItemActionEntry.lore.isNotBlank()) {
lore(
this@GiveItemActionEntry.lore.split("\n").map { "<gray>$it".asMini() })

}
}

player.inventory.addItem(item)
player.inventory.addItem(item.build(player))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class CinematicConsoleCommandEntry(
override val name: String,
override val criteria: List<Criteria>,
@Segments(Colors.YELLOW, Icons.TERMINAL)
@Placeholder
@InnerMax(Max(1))
// Run commands on different segments
override val segments: List<CommandSegment>,
Expand Down Expand Up @@ -60,7 +59,6 @@ class CinematicPlayerCommandEntry(
override val name: String,
override val criteria: List<Criteria>,
@Segments(Colors.YELLOW, Icons.TERMINAL)
@Placeholder
@InnerMax(Max(1))
// Run commands on different segments
override val segments: List<CommandSegment>,
Expand All @@ -79,6 +77,7 @@ data class CommandSegment(
override val startFrame: Int,
override val endFrame: Int,
@Help("The command to run")
@Placeholder
val command: String,
) : Segment

Expand Down
12 changes: 7 additions & 5 deletions app/lib/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class AppRouter extends _$AppRouter {
final PassingRef ref;

@override
RouteType get defaultRouteType => const RouteType.custom(transitionsBuilder: TransitionsBuilders.noTransition);
RouteType get defaultRouteType => const RouteType.custom(
transitionsBuilder: TransitionsBuilders.noTransition);

@override
List<AutoRoute> get routes => [
Expand Down Expand Up @@ -79,7 +80,8 @@ extension AppRouterX on AppRouter {
Future<void> navigateToEntry(PassingRef ref, String entryId) async {
final currentPage = ref.read(currentPageProvider);

if (currentPage != null && currentPage.entries.none((e) => e.id == entryId)) {
if (currentPage != null &&
currentPage.entries.none((e) => e.id == entryId)) {
final entryPage = ref.read(globalEntryWithPageProvider(entryId))?.key;
if (entryPage != null) {
await navigateToPage(ref, entryPage);
Expand All @@ -93,9 +95,9 @@ extension AppRouterX on AppRouter {
final currentPage = ref.read(currentPageProvider);

if (currentPage?.name != pageId) {
await ref
.read(appRouter)
.push(PageEditorRoute(id: pageId), onFailure: (e) => debugPrint("Failed to navigate to page $pageId: $e"));
await ref.read(appRouter).push(PageEditorRoute(id: pageId),
onFailure: (e) =>
debugPrint("Failed to navigate to page $pageId: $e"));
}
}
}
28 changes: 22 additions & 6 deletions app/lib/models/adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ List<String> entryTags(EntryTagsRef ref, String name) =>
/// Gets all the modifiers with a given name.
@riverpod
Map<String, Modifier> fieldModifiers(
FieldModifiersRef ref, String blueprint, String name,) {
FieldModifiersRef ref,
String blueprint,
String name,
) {
return ref
.watch(entryBlueprintProvider(blueprint))
?.fieldsWithModifier(name) ??
Expand All @@ -44,7 +47,10 @@ Map<String, Modifier> fieldModifiers(
/// Gets all the paths from fields with a given modifier.
@riverpod
List<String> modifierPaths(
ModifierPathsRef ref, String blueprint, String name,) {
ModifierPathsRef ref,
String blueprint,
String name,
) {
return ref.watch(fieldModifiersProvider(blueprint, name)).keys.toList();
}

Expand Down Expand Up @@ -153,16 +159,24 @@ extension EntryBlueprintExt on EntryBlueprint {

/// Parse through the fields of this entry and return a list of all the fields that have the given modifier with [name].
Map<String, Modifier> _fieldsWithModifier(
String name, String path, FieldInfo info,) {
String name,
String path,
FieldInfo info,
) {
final fields = {
if (info.hasModifier(name)) path: info.getModifier(name)!,
};

final separator = path.isEmpty ? "" : ".";
if (info is ObjectField) {
for (final field in info.fields.entries) {
fields.addAll(_fieldsWithModifier(
name, "$path$separator${field.key}", field.value,),);
fields.addAll(
_fieldsWithModifier(
name,
"$path$separator${field.key}",
field.value,
),
);
}
} else if (info is ListField) {
fields.addAll(_fieldsWithModifier(name, "$path$separator*", info.type));
Expand Down Expand Up @@ -215,6 +229,8 @@ extension EntryBlueprintExt on EntryBlueprint {
}
}

final _customEditorCustomLayout = ["optional", "item"];

/// Since freezed does not support methods on data models, we have to create a separate extension class.
extension FieldTypeExtension on FieldInfo {
/// Get the default value for this field type.
Expand Down Expand Up @@ -243,7 +259,7 @@ extension FieldTypeExtension on FieldInfo {
bool get hasCustomLayout {
if (this is CustomField) {
final editor = (this as CustomField).editor;
if (editor == "optional") {
if (_customEditorCustomLayout.contains(editor)) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/lib/pages/page_editor.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions app/lib/utils/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,22 @@ extension ObjectExtension on Object? {
T? cast<T>() => this is T ? this as T : null;
}

extension ListExtensions on List<dynamic> {
extension ListExtensions<T> on List<T> {
List<int> get indices => List.generate(length, (index) => index);

List<T> difference(List<T> other) {
return where((element) => !other.contains(element)).toList()
..addAll(other.where((element) => !contains(element)).toSet());
}
}

TextInputFormatter snakeCaseFormatter() => TextInputFormatter.withFunction(
(oldValue, newValue) => newValue.copyWith(
text: newValue.text
.toLowerCase()
.replaceAll(" ", "_")
.replaceAll("-", "_"),),
text: newValue.text
.toLowerCase()
.replaceAll(" ", "_")
.replaceAll("-", "_"),
),
);

extension RandomColor on String {
Expand Down
6 changes: 3 additions & 3 deletions app/lib/widgets/components/app/cinematic_view.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions app/lib/widgets/components/general/error_box.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import "package:flutter/material.dart";
import "package:flutter_hooks/flutter_hooks.dart";

class ErrorBox extends HookWidget {
const ErrorBox({
required this.message,
super.key,
});

final String message;

@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.redAccent,
borderRadius: BorderRadius.circular(4),
),
child: Text(
message,
style: Theme.of(context).textTheme.bodySmall,
),
);
}
}
2 changes: 2 additions & 0 deletions app/lib/widgets/inspector/editors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "package:typewriter/widgets/inspector/editors/cron.dart";
import "package:typewriter/widgets/inspector/editors/duration.dart";
import "package:typewriter/widgets/inspector/editors/entry_selector.dart";
import "package:typewriter/widgets/inspector/editors/enum.dart";
import "package:typewriter/widgets/inspector/editors/item.dart";
import "package:typewriter/widgets/inspector/editors/list.dart";
import "package:typewriter/widgets/inspector/editors/location.dart";
import "package:typewriter/widgets/inspector/editors/map.dart";
Expand Down Expand Up @@ -42,6 +43,7 @@ List<EditorFilter> editorFilters(EditorFiltersRef ref) => [
DurationEditorFilter(),
CronEditorFilter(),
PotionEffectEditorFilter(),
ItemEditorFilter(),

// Default filters
StringEditorFilter(),
Expand Down
2 changes: 1 addition & 1 deletion app/lib/widgets/inspector/editors.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions app/lib/widgets/inspector/editors/field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "package:collection/collection.dart";
import "package:flutter/material.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:typewriter/models/adapter.dart";
import "package:typewriter/widgets/components/general/error_box.dart";
import "package:typewriter/widgets/inspector/editors.dart";

class FieldEditor extends HookConsumerWidget {
Expand Down Expand Up @@ -34,16 +35,6 @@ class _NoEditorFound extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.redAccent,
borderRadius: BorderRadius.circular(4),
),
child: Text(
"Could not find a editor for $path",
style: Theme.of(context).textTheme.bodySmall,
),
);
return ErrorBox(message: "Could not find a editor for $path");
}
}
Loading

0 comments on commit 17f18ea

Please sign in to comment.