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))
{
itemBuilder.clickHandler((panel, user1, clickType, i) -> {
new ConfirmationGUI(this, this.user);
this.valueObject = challenge;
new ConfirmationGUI(this.user, value -> {
if (value)
{
this.addon.getChallengesManager().deleteChallenge(challenge);
}
});
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
// ---------------------------------------------------------------------

View File

@ -141,8 +141,13 @@ public class ListLevelsGUI extends CommonGUI
else if (this.currentMode.equals(Mode.DELETE))
{
itemBuilder.clickHandler((panel, user1, clickType, i) -> {
new ConfirmationGUI(this, this.user);
this.valueObject = challengeLevel;
new ConfirmationGUI(this.user, value -> {
if (value)
{
this.addon.getChallengesManager().
deleteChallengeLevel(challengeLevel);
}
});
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
// ---------------------------------------------------------------------

View File

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