Replace setValue method with BiConsumer.

This will provide ability to use setters directly in caller GUIs.
This commit is contained in:
BONNe1704 2019-01-18 16:50:34 +02:00
parent 09f69bd46a
commit 25371fc6aa
3 changed files with 29 additions and 34 deletions

View File

@ -8,12 +8,12 @@ import org.bukkit.inventory.ItemStack;
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;
/**
@ -21,9 +21,9 @@ import world.bentobox.challenges.panel.CommonGUI;
*/
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();
@ -35,7 +35,7 @@ public class ItemSwitchGUI
*/
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);
@ -94,9 +94,7 @@ public class ItemSwitchGUI
}
}
this.parentGUI.setValue(returnItems);
this.user.closeInventory();
this.parentGUI.build();
this.consumer.accept(true, returnItems);
return true;
};
@ -108,7 +106,7 @@ public class ItemSwitchGUI
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;
@ -219,10 +217,6 @@ public class ItemSwitchGUI
// Section: Variables
// ---------------------------------------------------------------------
/**
* ParentGUI from which current gui is called.
*/
private CommonGUI parentGUI;
/**
* User who opens current gui.
@ -233,4 +227,9 @@ public class ItemSwitchGUI
* List with original items.
*/
private List<ItemStack> itemStacks;
/**
* Consumer that returns item stacks on save action.
*/
private BiConsumer<Boolean, List<ItemStack>> consumer;
}

View File

@ -5,12 +5,12 @@ import org.bukkit.Material;
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;
/**
@ -18,11 +18,11 @@ import world.bentobox.challenges.panel.CommonGUI;
*/
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;
@ -35,7 +35,7 @@ public class NumberGUI
*/
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));
@ -93,10 +93,7 @@ public class NumberGUI
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;
@ -108,7 +105,7 @@ public class NumberGUI
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;
@ -290,9 +287,9 @@ public class NumberGUI
// ---------------------------------------------------------------------
/**
* This variable stores return GUI.
* This variable stores current GUI consumer.
*/
private CommonGUI parentGUI;
private BiConsumer<Boolean, Integer> consumer;
/**
* User who runs GUI.

View File

@ -5,12 +5,12 @@ import org.bukkit.Material;
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;
/**
@ -19,16 +19,16 @@ import world.bentobox.challenges.panel.CommonGUI;
*/
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
{
@ -42,7 +42,7 @@ public class StringListGUI
*/
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));
@ -82,9 +82,7 @@ public class StringListGUI
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;
};
@ -96,7 +94,8 @@ public class StringListGUI
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;
@ -198,9 +197,9 @@ public class StringListGUI
/**
* This variable stores return GUI.
* This variable stores consumer.
*/
private CommonGUI parentGUI;
private BiConsumer<Boolean, List<String>> consumer;
/**
* User who runs GUI.