Skip to content

Commit

Permalink
Add a new option named Action to make configurations more clean if bo…
Browse files Browse the repository at this point in the history
…th PrimaryAction and SecondaryAction are the same
  • Loading branch information
ustc-zzzz committed Jun 17, 2019
1 parent b269d80 commit d2b2a05
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
9 changes: 3 additions & 6 deletions resources/assets/virtualchest/examples/example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -139,7 +137,7 @@ virtualchest {
"&e&lToo young!"
]
}
PrimaryAction {
Action {
Command = """tellraw: {
"bold": true,
"text": "Too simple!",
Expand All @@ -163,7 +161,6 @@ virtualchest {
title:"""
KeepInventoryOpen = false
}
SecondaryAction = ${?PrimaryAction}
}
Slot7 = ${aliases.dye-item} {
Item {
Expand Down
6 changes: 2 additions & 4 deletions resources/assets/virtualchest/examples/example2.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -68,21 +69,23 @@ public static VirtualChestItem deserialize(VirtualChestPlugin plugin, DataView d
String requirementString = data.getString(REQUIREMENTS).orElse("");
Tuple<String, CompiledScript> requirements = plugin.getScriptManager().prepare(requirementString);

List<DataView> primaryList = getViewListOrSingletonList(PRIMARY_ACTION, data);
VirtualChestActionDispatcher primaryAction = new VirtualChestActionDispatcher(primaryList);
List<String> ignoredPermissions = data.getStringList(IGNORED_PERMISSIONS).orElse(ImmutableList.of());

List<DataView> actionList = getViewListOrSingletonList(ACTION, data);
List<DataView> primaryList = getViewListOrSingletonList(PRIMARY_ACTION, data);
List<DataView> secondaryList = getViewListOrSingletonList(SECONDARY_ACTION, data);
VirtualChestActionDispatcher secondaryAction = new VirtualChestActionDispatcher(secondaryList);

List<DataView> primaryShiftList = getViewListOrSingletonList(PRIMARY_SHIFT_ACTION, data);
List<DataView> primaryShiftListFinal = primaryShiftList.isEmpty() ? primaryList : primaryShiftList;
VirtualChestActionDispatcher primaryShiftAction = new VirtualChestActionDispatcher(primaryShiftListFinal);

List<DataView> secondaryShiftList = getViewListOrSingletonList(SECONDARY_SHIFT_ACTION, data);
List<DataView> secondaryShiftListFinal = secondaryShiftList.isEmpty() ? secondaryList : secondaryShiftList;
VirtualChestActionDispatcher secondaryShiftAction = new VirtualChestActionDispatcher(secondaryShiftListFinal);

List<String> ignoredPermissions = data.getStringList(IGNORED_PERMISSIONS).orElse(ImmutableList.of());
List<DataView> primaryListFinal = primaryList.isEmpty() ? actionList : primaryList;
List<DataView> secondaryListFinal = secondaryList.isEmpty() ? actionList : secondaryList;
List<DataView> primaryShiftListFinal = primaryShiftList.isEmpty() ? primaryListFinal : primaryShiftList;
List<DataView> 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);
Expand Down

0 comments on commit d2b2a05

Please sign in to comment.