From d2b2a05e7bd1e85efce58607d2bf11dc08ad45dc Mon Sep 17 00:00:00 2001 From: Yanbing Zhao Date: Tue, 18 Jun 2019 01:13:40 +0800 Subject: [PATCH] Add a new option named Action to make configurations more clean if both PrimaryAction and SecondaryAction are the same --- .../assets/virtualchest/examples/example.conf | 9 ++-- .../virtualchest/examples/example2.conf | 6 +-- .../inventory/item/VirtualChestItem.java | 41 ++++++++++--------- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/resources/assets/virtualchest/examples/example.conf b/resources/assets/virtualchest/examples/example.conf index 4de475f..6a4546f 100644 --- a/resources/assets/virtualchest/examples/example.conf +++ b/resources/assets/virtualchest/examples/example.conf @@ -57,10 +57,9 @@ aliases { ] } Requirements = "player.hasPermission('virtualchest.open.self.example2')" - PrimaryAction { + Action { Command = "tell: You found the magic power of the compass!" } - SecondaryAction = ${?PrimaryAction} } clock-item { Item { @@ -78,10 +77,9 @@ aliases { "&eopening that GUI (&cvirtualchest.open.self.example2&e)" ] } - PrimaryAction { + Action { Command = "tell: You didn't find anything." } - SecondaryAction = ${?PrimaryAction} } } virtualchest { @@ -139,7 +137,7 @@ virtualchest { "&e&lToo young!" ] } - PrimaryAction { + Action { Command = """tellraw: { "bold": true, "text": "Too simple!", @@ -163,7 +161,6 @@ virtualchest { title:""" KeepInventoryOpen = false } - SecondaryAction = ${?PrimaryAction} } Slot7 = ${aliases.dye-item} { Item { diff --git a/resources/assets/virtualchest/examples/example2.conf b/resources/assets/virtualchest/examples/example2.conf index 1f74cad..40ee9c3 100644 --- a/resources/assets/virtualchest/examples/example2.conf +++ b/resources/assets/virtualchest/examples/example2.conf @@ -11,10 +11,9 @@ aliases { "&eWhat happened when you right click it?" ] } - PrimaryAction { + Action { Command = "console: say %player_name% has found the magic power of the compass!" } - SecondaryAction = ${?PrimaryAction} } clock-item { Item { @@ -33,10 +32,9 @@ aliases { ] } Requirements = "!player.hasPermission('virtualchest.open.self.example')" - PrimaryAction { + Action { Command = "console: say %player_name% found nothing." } - SecondaryAction = ${?PrimaryAction} } white-pane-item { Item { diff --git a/src/com/github/ustc_zzzz/virtualchest/inventory/item/VirtualChestItem.java b/src/com/github/ustc_zzzz/virtualchest/inventory/item/VirtualChestItem.java index aa48109..816b239 100644 --- a/src/com/github/ustc_zzzz/virtualchest/inventory/item/VirtualChestItem.java +++ b/src/com/github/ustc_zzzz/virtualchest/inventory/item/VirtualChestItem.java @@ -27,11 +27,13 @@ public class VirtualChestItem { public static final DataQuery ITEM = DataQuery.of("Item"); public static final DataQuery REQUIREMENTS = DataQuery.of("Requirements"); + public static final DataQuery IGNORED_PERMISSIONS = DataQuery.of("IgnoredPermissions"); + + public static final DataQuery ACTION = DataQuery.of("Action"); public static final DataQuery PRIMARY_ACTION = DataQuery.of("PrimaryAction"); public static final DataQuery SECONDARY_ACTION = DataQuery.of("SecondaryAction"); public static final DataQuery PRIMARY_SHIFT_ACTION = DataQuery.of("PrimaryShiftAction"); public static final DataQuery SECONDARY_SHIFT_ACTION = DataQuery.of("SecondaryShiftAction"); - public static final DataQuery IGNORED_PERMISSIONS = DataQuery.of("IgnoredPermissions"); private final VirtualChestPlugin plugin; private final VirtualChestItemStackSerializer serializer; @@ -47,17 +49,16 @@ public class VirtualChestItem public static DataContainer serialize(VirtualChestPlugin plugin, VirtualChestItem item) throws InvalidDataException { DataContainer container = SpongeUnimplemented.newDataContainer(DataView.SafetyMode.ALL_DATA_CLONED); + container.set(ITEM, item.serializedStack); + container.set(REQUIREMENTS, item.requirements.getFirst()); + container.set(IGNORED_PERMISSIONS, item.ignoredPermissions); + item.primaryAction.getObjectForSerialization().ifPresent(o -> container.set(PRIMARY_ACTION, o)); item.secondaryAction.getObjectForSerialization().ifPresent(o -> container.set(SECONDARY_ACTION, o)); - if (!item.requirements.getFirst().isEmpty()) - { - container.set(REQUIREMENTS, item.requirements.getFirst()); - } - if (!item.ignoredPermissions.isEmpty()) - { - container.set(IGNORED_PERMISSIONS, item.ignoredPermissions); - } + item.primaryShiftAction.getObjectForSerialization().ifPresent(o -> container.set(PRIMARY_SHIFT_ACTION, o)); + item.secondaryShiftAction.getObjectForSerialization().ifPresent(o -> container.set(SECONDARY_SHIFT_ACTION, o)); + return container; } @@ -68,21 +69,23 @@ public static VirtualChestItem deserialize(VirtualChestPlugin plugin, DataView d String requirementString = data.getString(REQUIREMENTS).orElse(""); Tuple requirements = plugin.getScriptManager().prepare(requirementString); - List primaryList = getViewListOrSingletonList(PRIMARY_ACTION, data); - VirtualChestActionDispatcher primaryAction = new VirtualChestActionDispatcher(primaryList); + List ignoredPermissions = data.getStringList(IGNORED_PERMISSIONS).orElse(ImmutableList.of()); + List actionList = getViewListOrSingletonList(ACTION, data); + List primaryList = getViewListOrSingletonList(PRIMARY_ACTION, data); List secondaryList = getViewListOrSingletonList(SECONDARY_ACTION, data); - VirtualChestActionDispatcher secondaryAction = new VirtualChestActionDispatcher(secondaryList); - List primaryShiftList = getViewListOrSingletonList(PRIMARY_SHIFT_ACTION, data); - List primaryShiftListFinal = primaryShiftList.isEmpty() ? primaryList : primaryShiftList; - VirtualChestActionDispatcher primaryShiftAction = new VirtualChestActionDispatcher(primaryShiftListFinal); - List secondaryShiftList = getViewListOrSingletonList(SECONDARY_SHIFT_ACTION, data); - List secondaryShiftListFinal = secondaryShiftList.isEmpty() ? secondaryList : secondaryShiftList; - VirtualChestActionDispatcher secondaryShiftAction = new VirtualChestActionDispatcher(secondaryShiftListFinal); - List ignoredPermissions = data.getStringList(IGNORED_PERMISSIONS).orElse(ImmutableList.of()); + List primaryListFinal = primaryList.isEmpty() ? actionList : primaryList; + List secondaryListFinal = secondaryList.isEmpty() ? actionList : secondaryList; + List primaryShiftListFinal = primaryShiftList.isEmpty() ? primaryListFinal : primaryShiftList; + List secondaryShiftListFinal = secondaryShiftList.isEmpty() ? secondaryListFinal : secondaryShiftList; + + VirtualChestActionDispatcher primaryAction = new VirtualChestActionDispatcher(primaryListFinal); + VirtualChestActionDispatcher secondaryAction = new VirtualChestActionDispatcher(secondaryListFinal); + VirtualChestActionDispatcher primaryShiftAction = new VirtualChestActionDispatcher(primaryShiftListFinal); + VirtualChestActionDispatcher secondaryShiftAction = new VirtualChestActionDispatcher(secondaryShiftListFinal); return new VirtualChestItem(plugin, serializedStack, requirements, primaryAction, secondaryAction, primaryShiftAction, secondaryShiftAction, ignoredPermissions);