Skip to content

Commit

Permalink
Implement Consumer in ConfirmationGUI instead of depending on setValu…
Browse files Browse the repository at this point in the history
…e method.
  • Loading branch information
BONNe committed Jan 18, 2019
1 parent c7445df commit 09f69bd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,12 @@ private PanelItem createChallengeIcon(Challenges challenge)
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;
});
}
Expand All @@ -151,25 +155,6 @@ else if (this.currentMode.equals(Mode.DELETE))
}


/**
* 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
// ---------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,13 @@ private PanelItem createLevelIcon(ChallengeLevels challengeLevel)
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;
});
}
Expand All @@ -151,25 +156,6 @@ else if (this.currentMode.equals(Mode.DELETE))
}


/**
* 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
// ---------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

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;


/**
Expand All @@ -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();
}
Expand All @@ -35,27 +35,22 @@ public ConfirmationGUI(CommonGUI parentGUI, User user)
*/
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());

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());
Expand All @@ -74,7 +69,7 @@ public void build()
private User user;

/**
* Parent GUI where should return on cancel or proceed.
* Stores current Consumer
*/
private CommonGUI parentGUI;
private Consumer<Boolean> consumer;
}

0 comments on commit 09f69bd

Please sign in to comment.