Skip to content

Commit

Permalink
Replace setValue method with BiConsumer.
Browse files Browse the repository at this point in the history
This will provide ability to use setters directly in caller GUIs.
  • Loading branch information
BONNe committed Jan 18, 2019
1 parent 09f69bd commit 25371fc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;

import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.PanelListener;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.challenges.panel.CommonGUI;


/**
* This class allows to change Input ItemStacks to different ItemStacks.
*/
public class ItemSwitchGUI
{
public ItemSwitchGUI(CommonGUI parentGUI, User user, List<ItemStack> itemStacks)
public ItemSwitchGUI(User user, List<ItemStack> itemStacks, BiConsumer<Boolean, List<ItemStack>> consumer)
{
this.parentGUI = parentGUI;
this.consumer = consumer;
this.user = user;
this.itemStacks = itemStacks;
this.build();
Expand All @@ -35,7 +35,7 @@ public ItemSwitchGUI(CommonGUI parentGUI, User user, List<ItemStack> itemStacks)
*/
private void build()
{
PanelBuilder panelBuilder = new PanelBuilder().name(this.user.getTranslation("challenges.gui.change-items"));
PanelBuilder panelBuilder = new PanelBuilder().user(this.user).name(this.user.getTranslation("challenges.gui.change-items"));

// Size of inventory that user can set via GUI.
panelBuilder.size(45);
Expand Down Expand Up @@ -94,9 +94,7 @@ private PanelItem getButton(Button button)
}
}

this.parentGUI.setValue(returnItems);
this.user.closeInventory();
this.parentGUI.build();
this.consumer.accept(true, returnItems);

return true;
};
Expand All @@ -108,7 +106,7 @@ private PanelItem getButton(Button button)
description = Collections.emptyList();
icon = new ItemStack(Material.IRON_DOOR);
clickHandler = (panel, user, clickType, slot) -> {
this.parentGUI.build();
this.consumer.accept(false, Collections.emptyList());
return true;
};
break;
Expand Down Expand Up @@ -219,10 +217,6 @@ private enum Button
// Section: Variables
// ---------------------------------------------------------------------

/**
* ParentGUI from which current gui is called.
*/
private CommonGUI parentGUI;

/**
* User who opens current gui.
Expand All @@ -233,4 +227,9 @@ private enum Button
* List with original items.
*/
private List<ItemStack> itemStacks;

/**
* Consumer that returns item stacks on save action.
*/
private BiConsumer<Boolean, List<ItemStack>> consumer;
}
19 changes: 8 additions & 11 deletions src/main/java/world/bentobox/challenges/panel/util/NumberGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
import org.bukkit.inventory.ItemStack;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;

import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.challenges.panel.CommonGUI;


/**
* This gui allows to change current number and returns it to previous GUI
*/
public class NumberGUI
{
public NumberGUI(CommonGUI parentGUI, User user, int value)
public NumberGUI(User user, int value, BiConsumer<Boolean, Integer> consumer)
{
this.parentGUI = parentGUI;
this.user = user;
this.value = value;
this.consumer = consumer;

this.currentOperation = Button.SET;

Expand All @@ -35,7 +35,7 @@ public NumberGUI(CommonGUI parentGUI, User user, int value)
*/
private void build()
{
PanelBuilder panelBuilder = new PanelBuilder().name(this.user.getTranslation("challenges.gui.edit-number-title"));
PanelBuilder panelBuilder = new PanelBuilder().user(this.user).name(this.user.getTranslation("challenges.gui.edit-number-title"));

// Others
panelBuilder.item(0, this.getButton(Button.SAVE));
Expand Down Expand Up @@ -93,10 +93,7 @@ private PanelItem getButton(Button button)
description = Collections.emptyList();
icon = new ItemStack(Material.COMMAND_BLOCK);
clickHandler = (panel, user, clickType, slot) -> {
this.parentGUI.setValue(this.value);
this.user.closeInventory();
this.parentGUI.build();

this.consumer.accept(true, this.value);
return true;
};
glow = false;
Expand All @@ -108,7 +105,7 @@ private PanelItem getButton(Button button)
description = Collections.emptyList();
icon = new ItemStack(Material.IRON_DOOR);
clickHandler = (panel, user, clickType, slot) -> {
this.parentGUI.build();
this.consumer.accept(false, this.value);
return true;
};
glow = false;
Expand Down Expand Up @@ -290,9 +287,9 @@ private enum Button
// ---------------------------------------------------------------------

/**
* This variable stores return GUI.
* This variable stores current GUI consumer.
*/
private CommonGUI parentGUI;
private BiConsumer<Boolean, Integer> consumer;

/**
* User who runs GUI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import org.bukkit.inventory.ItemStack;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;

import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.challenges.panel.CommonGUI;


/**
Expand All @@ -19,16 +19,16 @@
*/
public class StringListGUI
{
public StringListGUI(CommonGUI parentGUI, User user, List<String> value)
public StringListGUI(User user, List<String> value, BiConsumer<Boolean, List<String>> consumer)
{
this.parentGUI = parentGUI;
this.consumer = consumer;
this.user = user;
this.value = value;

if (this.value.size() > 18)
{
// TODO: throw error that so large list cannot be edited.
this.parentGUI.build();
this.consumer.accept(false, this.value);
}
else
{
Expand All @@ -42,7 +42,7 @@ public StringListGUI(CommonGUI parentGUI, User user, List<String> value)
*/
private void build()
{
PanelBuilder panelBuilder = new PanelBuilder().name(this.user.getTranslation("challenges.gui.text-edit-title"));
PanelBuilder panelBuilder = new PanelBuilder().user(this.user).name(this.user.getTranslation("challenges.gui.text-edit-title"));

panelBuilder.item(0, this.getButton(Button.SAVE));
panelBuilder.item(1, this.getButton(Button.VALUE));
Expand Down Expand Up @@ -82,9 +82,7 @@ private PanelItem getButton(Button button)
description = Collections.emptyList();
icon = new ItemStack(Material.COMMAND_BLOCK);
clickHandler = (panel, user, clickType, slot) -> {
this.parentGUI.setValue(this.value);
this.user.closeInventory();
this.parentGUI.build();
this.consumer.accept(true, this.value);

return true;
};
Expand All @@ -96,7 +94,8 @@ private PanelItem getButton(Button button)
description = Collections.emptyList();
icon = new ItemStack(Material.IRON_DOOR);
clickHandler = (panel, user, clickType, slot) -> {
this.parentGUI.build();
this.consumer.accept(false, this.value);

return true;
};
break;
Expand Down Expand Up @@ -198,9 +197,9 @@ private enum Button


/**
* This variable stores return GUI.
* This variable stores consumer.
*/
private CommonGUI parentGUI;
private BiConsumer<Boolean, List<String>> consumer;

/**
* User who runs GUI.
Expand Down

0 comments on commit 25371fc

Please sign in to comment.