|
| 1 | +/* |
| 2 | + * Copyright or © or Copr. ZLib contributors (2015 - 2016) |
| 3 | + * |
| 4 | + * This software is governed by the CeCILL-B license under French law and |
| 5 | + * abiding by the rules of distribution of free software. You can use, |
| 6 | + * modify and/ or redistribute the software under the terms of the CeCILL-B |
| 7 | + * license as circulated by CEA, CNRS and INRIA at the following URL |
| 8 | + * "http://www.cecill.info". |
| 9 | + * |
| 10 | + * As a counterpart to the access to the source code and rights to copy, |
| 11 | + * modify and redistribute granted by the license, users are provided only |
| 12 | + * with a limited warranty and the software's author, the holder of the |
| 13 | + * economic rights, and the successive licensors have only limited |
| 14 | + * liability. |
| 15 | + * |
| 16 | + * In this respect, the user's attention is drawn to the risks associated |
| 17 | + * with loading, using, modifying and/or developing or reproducing the |
| 18 | + * software by the user in light of its specific status of free software, |
| 19 | + * that may mean that it is complicated to manipulate, and that also |
| 20 | + * therefore means that it is reserved for developers and experienced |
| 21 | + * professionals having in-depth computer knowledge. Users are therefore |
| 22 | + * encouraged to load and test the software's suitability as regards their |
| 23 | + * requirements in conditions enabling the security of their systems and/or |
| 24 | + * data to be ensured and, more generally, to use and operate it in the |
| 25 | + * same conditions as regards security. |
| 26 | + * |
| 27 | + * The fact that you are presently reading this means that you have had |
| 28 | + * knowledge of the CeCILL-B license and that you accept its terms. |
| 29 | + */ |
| 30 | +package fr.zcraft.zlib.components.gui; |
| 31 | + |
| 32 | +import org.apache.commons.lang.NotImplementedException; |
| 33 | +import org.bukkit.entity.Player; |
| 34 | +import org.bukkit.inventory.ItemStack; |
| 35 | + |
| 36 | +import fr.zcraft.zlib.components.gui.ExplorerGui; |
| 37 | +import fr.zcraft.zlib.components.gui.Gui; |
| 38 | +import fr.zcraft.zlib.tools.Callback; |
| 39 | + |
| 40 | +public abstract class ArrayPromptGui<T> extends ExplorerGui<T> |
| 41 | +{ |
| 42 | + private Callback<T> cb; |
| 43 | + private String title; |
| 44 | + private T[] data; |
| 45 | + private boolean closeOnChoice; |
| 46 | + |
| 47 | + /** |
| 48 | + * @param player |
| 49 | + * The player making the choice |
| 50 | + * @param title |
| 51 | + * The gui title |
| 52 | + * @param data |
| 53 | + * An array of datas to display |
| 54 | + * @param closeOnChoice |
| 55 | + * If true, close the interface when the player has choosen |
| 56 | + * @param callback |
| 57 | + * Callback called when the player made a choice |
| 58 | + */ |
| 59 | + public <A> ArrayPromptGui(Player player, String title, T[] data, boolean closeOnChoice, Callback<T> callback) { |
| 60 | + this.cb = callback; |
| 61 | + this.title = title; |
| 62 | + this.data = data; |
| 63 | + this.closeOnChoice = closeOnChoice; |
| 64 | + |
| 65 | + Gui.open(player, this); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @see #onClick(Object) |
| 70 | + * |
| 71 | + * Constructor with no callback argument. Note that you must override |
| 72 | + * the onClick method if you use this constructor |
| 73 | + * |
| 74 | + * @param player |
| 75 | + * The player making the choice |
| 76 | + * @param title |
| 77 | + * The gui title |
| 78 | + * @param data |
| 79 | + * An array of datas to display |
| 80 | + * @param closeOnChoice |
| 81 | + * Close the interface when the player has choosen if true |
| 82 | + */ |
| 83 | + public <A> ArrayPromptGui(Player player, String title, T[] data, boolean closeOnChoice) { |
| 84 | + this(player, title, data, closeOnChoice, null); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Convert an object to an ItemStack |
| 89 | + * |
| 90 | + * @return The ItemStack to display |
| 91 | + */ |
| 92 | + public abstract ItemStack getViewItem(T data); |
| 93 | + |
| 94 | + /** |
| 95 | + * Called when player made a choice if no callback was provided |
| 96 | + * |
| 97 | + * @param data |
| 98 | + */ |
| 99 | + public void onClick(T data) { |
| 100 | + throw new NotImplementedException("Override this method or use a callback."); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + protected void onRightClick(T data) { |
| 105 | + |
| 106 | + if (cb != null) |
| 107 | + cb.call(data); |
| 108 | + else |
| 109 | + onClick(data); |
| 110 | + |
| 111 | + if (closeOnChoice) |
| 112 | + close(); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + protected void onUpdate() { |
| 117 | + setTitle(title); |
| 118 | + setMode(Mode.READONLY); |
| 119 | + setData(data); |
| 120 | + } |
| 121 | +} |
0 commit comments