Remove commands from UtilGuis, instead use CommonGUI.valueObject to store new value.

Proper ItemSwitchGUI implementation.
This commit is contained in:
BONNe 2019-01-17 22:44:48 +02:00
parent bd48e85fcc
commit dab5f2b331
5 changed files with 249 additions and 217 deletions

View File

@ -1,10 +1,8 @@
package world.bentobox.challenges.panel.util;
import org.bukkit.Material;
import java.util.*;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User;
@ -22,18 +20,11 @@ public class ConfirmationGUI
*
* @param user Gui Caller.
* @param parentGUI Parent GUI.
* @param command Command .
* @param parameters Variables at the end of command.
*/
public ConfirmationGUI(CommonGUI parentGUI,
User user,
CompositeCommand command,
String... parameters)
public ConfirmationGUI(CommonGUI parentGUI, User user)
{
this.user = user;
this.parentGUI = parentGUI;
this.command = command;
this.parameters = parameters;
this.build();
}
@ -50,11 +41,8 @@ public class ConfirmationGUI
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.command
.execute(this.user, "CONFIRMATION", Arrays.asList(this.parameters));
clickHandler((panel, user1, clickType, index) -> {
this.parentGUI.setValue(true);
this.user.closeInventory();
this.parentGUI.build();
return true;
@ -66,6 +54,7 @@ public class ConfirmationGUI
icon(Material.RED_STAINED_GLASS_PANE).
clickHandler((panel, user1, clickType, i) ->
{
this.parentGUI.setValue(null);
this.parentGUI.build();
return true;
}).
@ -88,14 +77,4 @@ public class ConfirmationGUI
* Parent GUI where should return on cancel or proceed.
*/
private CommonGUI parentGUI;
/**
* Command that must be run on confirmation.
*/
private CompositeCommand command;
/**
* List of variables.
*/
private String[] parameters;
}

View File

@ -1,129 +0,0 @@
package world.bentobox.challenges.panel.util;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.ItemStack;
import java.util.Collections;
import java.util.List;
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;
/**
* This class allows to change Input ItemStacks to different ItemStacks.
*/
public class ItemGUI
{
public ItemGUI(User user, List<ItemStack> itemStacks)
{
this.user = user;
this.itemStacks = itemStacks;
this.build();
}
/**
* This method builds panel that allows to change given number value.
*/
private void build()
{
PanelBuilder panelBuilder = new PanelBuilder().name(this.user.getTranslation("challenges.gui.change-items"));
panelBuilder.listener(new CustomPanelListener());
for (ItemStack itemStack : this.itemStacks)
{
panelBuilder.item(new CustomPanelItem(itemStack));
}
panelBuilder.build().open(this.user);
}
// ---------------------------------------------------------------------
// Section: Private classes
// ---------------------------------------------------------------------
/**
* This CustomPanelItem does no lose Item original MetaData. After PanelItem has been
* created it restores original meta data. It also does not allow to change anything that
* could destroy meta data.
*/
private class CustomPanelItem extends PanelItem
{
CustomPanelItem(ItemStack item)
{
super(item.clone(), "", Collections.emptyList(), false, null, false);
this.getItem().setItemMeta(item.getItemMeta());
}
@Override
public void setGlow(boolean glow)
{
}
@Override
public void setDescription(List<String> description)
{
}
@Override
public void setName(String name)
{
}
@Override
public void setHead(ItemStack itemStack)
{
}
}
/**
* This CustomPanelListener allows to move items in current panel.
*/
private class CustomPanelListener implements PanelListener
{
@Override
public void setup()
{
}
@Override
public void onInventoryClose(InventoryCloseEvent inventoryCloseEvent)
{
}
@Override
public void onInventoryClick(User user, InventoryClickEvent event)
{
event.setCancelled(false);
}
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
/**
* User who opens current gui.
*/
private User user;
/**
* List with original items.
*/
private List<ItemStack> itemStacks;
}

View File

@ -0,0 +1,236 @@
package world.bentobox.challenges.panel.util;
import org.bukkit.Material;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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)
{
this.parentGUI = parentGUI;
this.user = user;
this.itemStacks = itemStacks;
this.build();
}
/**
* This method builds panel that allows to change given number value.
*/
private void build()
{
PanelBuilder panelBuilder = new PanelBuilder().name(this.user.getTranslation("challenges.gui.change-items"));
// Size of inventory that user can set via GUI.
panelBuilder.size(45);
panelBuilder.listener(new CustomPanelListener());
panelBuilder.item(0, this.getButton(Button.SAVE));
for (int i = 1; i < 8; i++)
{
panelBuilder.item(i, this.getButton(Button.EMPTY));
}
panelBuilder.item(8, this.getButton(Button.CANCEL));
for (ItemStack itemStack : this.itemStacks)
{
panelBuilder.item(new CustomPanelItem(itemStack));
}
panelBuilder.build().open(this.user);
}
/**
* This method create button that does some functionality in current gui.
* @param button Button functionality.
* @return PanelItem.
*/
private PanelItem getButton(Button button)
{
ItemStack icon;
String name;
List<String> description;
PanelItem.ClickHandler clickHandler;
switch (button)
{
case SAVE:
{
name = this.user.getTranslation("challenges.gui.buttons.save");
description = Collections.emptyList();
icon = new ItemStack(Material.COMMAND_BLOCK);
clickHandler = (panel, user, clickType, slot) -> {
// Magic number 9 - second row. First row is for custom buttons.
// Magic number 45 - This GUI is initialed with 45 elements.
List<ItemStack> returnItems = new ArrayList<>(36);
for (int i = 9; i < 45; i++)
{
ItemStack itemStack = panel.getInventory().getItem(i);
if (itemStack != null)
{
returnItems.add(itemStack);
}
}
this.parentGUI.setValue(returnItems);
this.user.closeInventory();
this.parentGUI.build();
return true;
};
break;
}
case CANCEL:
{
name = this.user.getTranslation("challenges.gui.buttons.cancel");
description = Collections.emptyList();
icon = new ItemStack(Material.IRON_DOOR);
clickHandler = (panel, user, clickType, slot) -> {
this.parentGUI.build();
return true;
};
break;
}
case EMPTY:
{
name = "";
description = Collections.emptyList();
icon = new ItemStack(Material.BARRIER);
clickHandler = (panel, user, clickType, slot) -> true;
break;
}
default:
return null;
}
return new PanelItem(icon, name, description, false, clickHandler, false);
}
// ---------------------------------------------------------------------
// Section: Private classes
// ---------------------------------------------------------------------
/**
* This CustomPanelItem does no lose Item original MetaData. After PanelItem has been
* created it restores original meta data. It also does not allow to change anything that
* could destroy meta data.
*/
private class CustomPanelItem extends PanelItem
{
CustomPanelItem(ItemStack item)
{
super(item.clone(), "", Collections.emptyList(), false, null, false);
this.getItem().setItemMeta(item.getItemMeta());
}
@Override
public void setGlow(boolean glow)
{
}
@Override
public void setDescription(List<String> description)
{
}
@Override
public void setName(String name)
{
}
@Override
public void setHead(ItemStack itemStack)
{
}
}
/**
* This CustomPanelListener allows to move items in current panel.
*/
private class CustomPanelListener implements PanelListener
{
@Override
public void setup()
{
}
@Override
public void onInventoryClose(InventoryCloseEvent inventoryCloseEvent)
{
}
@Override
public void onInventoryClick(User user, InventoryClickEvent event)
{
// First row of elements should be ignored, as it contains buttons and blocked slots.
event.setCancelled(event.getRawSlot() < 9);
}
}
// ---------------------------------------------------------------------
// Section: Enums
// ---------------------------------------------------------------------
/**
* This enum holds all button values in current gui.
*/
private enum Button
{
CANCEL,
SAVE,
EMPTY
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
/**
* ParentGUI from which current gui is called.
*/
private CommonGUI parentGUI;
/**
* User who opens current gui.
*/
private User user;
/**
* List with original items.
*/
private List<ItemStack> itemStacks;
}

View File

@ -3,11 +3,9 @@ package world.bentobox.challenges.panel.util;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
@ -20,17 +18,11 @@ import world.bentobox.challenges.panel.CommonGUI;
*/
public class NumberGUI
{
public NumberGUI(CommonGUI parentGUI,
User user,
int value,
CompositeCommand command,
String... parameters)
public NumberGUI(CommonGUI parentGUI, User user, int value)
{
this.parentGUI = parentGUI;
this.user = user;
this.value = value;
this.command = command;
this.parameters = parameters;
this.currentOperation = Button.SET;
@ -101,18 +93,9 @@ public class NumberGUI
description = Collections.emptyList();
icon = new ItemStack(Material.COMMAND_BLOCK);
clickHandler = (panel, user, clickType, slot) -> {
List<String> values = Arrays.asList(this.parameters);
values.add(Integer.toString(this.value));
if (this.command.execute(this.user, "NUMBER_GUI", values))
{
this.user.closeInventory();
this.parentGUI.build();
}
else
{
this.build();
}
this.parentGUI.setValue(this.value);
this.user.closeInventory();
this.parentGUI.build();
return true;
};
@ -321,16 +304,6 @@ public class NumberGUI
*/
private int value;
/**
* Command that must be processed on save.
*/
private CompositeCommand command;
/**
* Command input parameters before number.
*/
private String[] parameters;
/**
* This variable holds which operation now is processed.
*/

View File

@ -3,11 +3,9 @@ package world.bentobox.challenges.panel.util;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
@ -21,17 +19,11 @@ import world.bentobox.challenges.panel.CommonGUI;
*/
public class StringListGUI
{
public StringListGUI(CommonGUI parentGUI,
User user,
List<String> value,
CompositeCommand command,
String... parameters)
public StringListGUI(CommonGUI parentGUI, User user, List<String> value)
{
this.parentGUI = parentGUI;
this.user = user;
this.value = value;
this.command = command;
this.parameters = parameters;
if (this.value.size() > 18)
{
@ -90,18 +82,9 @@ public class StringListGUI
description = Collections.emptyList();
icon = new ItemStack(Material.COMMAND_BLOCK);
clickHandler = (panel, user, clickType, slot) -> {
List<String> values = Arrays.asList(this.parameters);
values.addAll(this.value);
if (this.command.execute(this.user, "STRING_LIST_GUI", values))
{
this.user.closeInventory();
this.parentGUI.build();
}
else
{
this.build();
}
this.parentGUI.setValue(this.value);
this.user.closeInventory();
this.parentGUI.build();
return true;
};
@ -228,14 +211,4 @@ public class StringListGUI
* Current value.
*/
private List<String> value;
/**
* Command that must be processed on save.
*/
private CompositeCommand command;
/**
* Command input parameters before number.
*/
private String[] parameters;
}