Implement Consumer in ConfirmationGUI instead of depending on setValue method.

This commit is contained in:
BONNe1704 2019-01-18 16:49:39 +02:00
parent c7445df56f
commit 09f69bd46a
3 changed files with 23 additions and 57 deletions

View File

@ -141,8 +141,12 @@ public class ListChallengesGUI extends CommonGUI
else if (this.currentMode.equals(Mode.DELETE)) else if (this.currentMode.equals(Mode.DELETE))
{ {
itemBuilder.clickHandler((panel, user1, clickType, i) -> { itemBuilder.clickHandler((panel, user1, clickType, i) -> {
new ConfirmationGUI(this, this.user); new ConfirmationGUI(this.user, value -> {
this.valueObject = challenge; if (value)
{
this.addon.getChallengesManager().deleteChallenge(challenge);
}
});
return true; return true;
}); });
} }
@ -151,25 +155,6 @@ public class ListChallengesGUI extends CommonGUI
} }
/**
* Overwriting set value allows to catch if ConfirmationGui returns true.
* @param value new Value of valueObject.
*/
@Override
public void setValue(Object value)
{
if (value instanceof Boolean && ((Boolean) value) && this.valueObject != null)
{
this.addon.getChallengesManager().deleteChallenge((Challenges) this.valueObject);
this.valueObject = null;
}
else
{
this.valueObject = null;
}
}
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Section: Enums // Section: Enums
// --------------------------------------------------------------------- // ---------------------------------------------------------------------

View File

@ -141,8 +141,13 @@ public class ListLevelsGUI extends CommonGUI
else if (this.currentMode.equals(Mode.DELETE)) else if (this.currentMode.equals(Mode.DELETE))
{ {
itemBuilder.clickHandler((panel, user1, clickType, i) -> { itemBuilder.clickHandler((panel, user1, clickType, i) -> {
new ConfirmationGUI(this, this.user); new ConfirmationGUI(this.user, value -> {
this.valueObject = challengeLevel; if (value)
{
this.addon.getChallengesManager().
deleteChallengeLevel(challengeLevel);
}
});
return true; return true;
}); });
} }
@ -151,25 +156,6 @@ public class ListLevelsGUI extends CommonGUI
} }
/**
* Overwriting set value allows to catch if ConfirmationGui returns true.
* @param value new Value of valueObject.
*/
@Override
public void setValue(Object value)
{
if (value instanceof Boolean && ((Boolean) value) && this.valueObject != null)
{
this.addon.getChallengesManager().deleteChallengeLevel((ChallengeLevels) this.valueObject);
this.valueObject = null;
}
else
{
this.valueObject = null;
}
}
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Section: Enums // Section: Enums
// --------------------------------------------------------------------- // ---------------------------------------------------------------------

View File

@ -3,10 +3,11 @@ package world.bentobox.challenges.panel.util;
import org.bukkit.Material; import org.bukkit.Material;
import java.util.function.Consumer;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder; import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.challenges.panel.CommonGUI;
/** /**
@ -19,12 +20,11 @@ public class ConfirmationGUI
* This constructor inits and opens ConfirmationGUI. * This constructor inits and opens ConfirmationGUI.
* *
* @param user Gui Caller. * @param user Gui Caller.
* @param parentGUI Parent GUI.
*/ */
public ConfirmationGUI(CommonGUI parentGUI, User user) public ConfirmationGUI(User user, Consumer<Boolean> consumer)
{ {
this.user = user; this.user = user;
this.parentGUI = parentGUI; this.consumer = consumer;
this.build(); this.build();
} }
@ -35,16 +35,13 @@ public class ConfirmationGUI
*/ */
public void build() public void build()
{ {
PanelBuilder panelBuilder = new PanelBuilder() PanelBuilder panelBuilder = new PanelBuilder().user(this.user).name(this.user.getTranslation("challenges.gui.admin.confirm-title"));
.name(this.user.getTranslation("challenges.gui.admin.confirm-title"));
panelBuilder.item(3, new PanelItemBuilder(). panelBuilder.item(3, new PanelItemBuilder().
name(this.user.getTranslation("challenges.gui.admin.buttons.proceed")). name(this.user.getTranslation("challenges.gui.admin.buttons.proceed")).
icon(Material.GREEN_STAINED_GLASS_PANE). icon(Material.GREEN_STAINED_GLASS_PANE).
clickHandler((panel, user1, clickType, index) -> { clickHandler((panel, user1, clickType, index) -> {
this.parentGUI.setValue(true); this.consumer.accept(true);
this.user.closeInventory();
this.parentGUI.build();
return true; return true;
}). }).
build()); build());
@ -52,10 +49,8 @@ public class ConfirmationGUI
panelBuilder.item(5, new PanelItemBuilder(). panelBuilder.item(5, new PanelItemBuilder().
name(this.user.getTranslation("challenges.gui.admin.buttons.cancel")). name(this.user.getTranslation("challenges.gui.admin.buttons.cancel")).
icon(Material.RED_STAINED_GLASS_PANE). icon(Material.RED_STAINED_GLASS_PANE).
clickHandler((panel, user1, clickType, i) -> clickHandler((panel, user1, clickType, i) -> {
{ this.consumer.accept(false);
this.parentGUI.setValue(null);
this.parentGUI.build();
return true; return true;
}). }).
build()); build());
@ -74,7 +69,7 @@ public class ConfirmationGUI
private User user; private User user;
/** /**
* Parent GUI where should return on cancel or proceed. * Stores current Consumer
*/ */
private CommonGUI parentGUI; private Consumer<Boolean> consumer;
} }