refactor(bukkit): 重构部分代码
This commit is contained in:
@@ -14,7 +14,7 @@ public class ButtonGroup {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private final PageConfig config;
|
private final PageConfig config;
|
||||||
@NotNull
|
@NotNull
|
||||||
private final HashMap<Character, String> buttonNameMap;
|
private final Map<Character, String> buttonNameMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实例化这个按钮组
|
* 实例化这个按钮组
|
||||||
@@ -31,6 +31,12 @@ public class ButtonGroup {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ButtonGroup(@NotNull String name, @NotNull PageConfig config, @NotNull Map<Character, String> buttonNameMap) {
|
||||||
|
this.name = name;
|
||||||
|
this.config = config;
|
||||||
|
this.buttonNameMap = buttonNameMap;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 以图形字符来获取按钮名称
|
* 以图形字符来获取按钮名称
|
||||||
* <p>
|
* <p>
|
||||||
@@ -86,7 +92,7 @@ public class ButtonGroup {
|
|||||||
* @param buttonName 按钮名称
|
* @param buttonName 按钮名称
|
||||||
* @return 按钮在 GUI 中全部的索引位置
|
* @return 按钮在 GUI 中全部的索引位置
|
||||||
*/
|
*/
|
||||||
public ArrayList<Integer> getButtonAllIndex(String buttonName) {
|
public ArrayList<Integer> getButtonAllIndex(@NotNull String buttonName) {
|
||||||
ArrayList<Integer> list = new ArrayList<>();
|
ArrayList<Integer> list = new ArrayList<>();
|
||||||
|
|
||||||
Character graphicKey = getGraphicKey(buttonName);
|
Character graphicKey = getGraphicKey(buttonName);
|
||||||
@@ -113,7 +119,7 @@ public class ButtonGroup {
|
|||||||
* @return 图形中的字符
|
* @return 图形中的字符
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public Character getGraphicKey(String buttonName) {
|
public Character getGraphicKey(@NotNull String buttonName) {
|
||||||
for (Map.Entry<Character, String> entry : buttonNameMap.entrySet()) {
|
for (Map.Entry<Character, String> entry : buttonNameMap.entrySet()) {
|
||||||
if (entry.getValue().equalsIgnoreCase(buttonName)) {
|
if (entry.getValue().equalsIgnoreCase(buttonName)) {
|
||||||
return entry.getKey();
|
return entry.getKey();
|
||||||
@@ -128,8 +134,8 @@ public class ButtonGroup {
|
|||||||
* @param graphicKey 图形字符
|
* @param graphicKey 图形字符
|
||||||
* @return 按钮物品
|
* @return 按钮物品
|
||||||
*/
|
*/
|
||||||
public ItemStack getButton(char graphicKey) {
|
public ItemStack getButtonItem(char graphicKey) {
|
||||||
return getButton(getButtonName(graphicKey));
|
return getButtonItem(getButtonName(graphicKey));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -138,7 +144,7 @@ public class ButtonGroup {
|
|||||||
* @param buttonName 按钮名称
|
* @param buttonName 按钮名称
|
||||||
* @return 按钮物品
|
* @return 按钮物品
|
||||||
*/
|
*/
|
||||||
public ItemStack getButton(String buttonName) {
|
public ItemStack getButtonItem(@NotNull String buttonName) {
|
||||||
ItemStack stack = config.getButtons().get(buttonName);
|
ItemStack stack = config.getButtons().get(buttonName);
|
||||||
if (stack != null) {
|
if (stack != null) {
|
||||||
stack = stack.clone();
|
stack = stack.clone();
|
||||||
@@ -162,7 +168,7 @@ public class ButtonGroup {
|
|||||||
* @return 把图形字符映射到按钮名称的表
|
* @return 把图形字符映射到按钮名称的表
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public HashMap<Character, String> getButtonNameMap() {
|
public Map<Character, String> getButtonNameMap() {
|
||||||
return buttonNameMap;
|
return buttonNameMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -82,7 +82,7 @@ public class PageConfig implements InventoryHolder {
|
|||||||
for (int j = 0; j < chars.length; j++) {
|
for (int j = 0; j < chars.length; j++) {
|
||||||
char c = chars[j];
|
char c = chars[j];
|
||||||
int index = i * 9 + j;
|
int index = i * 9 + j;
|
||||||
inventory.setItem(index, group.getButton(c));
|
inventory.setItem(index, group.getButtonItem(c));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -37,7 +37,7 @@ public class FixedPageHandler extends PageHandler {
|
|||||||
for (int j = 0; j < chars.length; j++) {
|
for (int j = 0; j < chars.length; j++) {
|
||||||
char c = chars[j];
|
char c = chars[j];
|
||||||
int index = i * 9 + j;
|
int index = i * 9 + j;
|
||||||
inventory.setItem(index, group.getButton(c));
|
inventory.setItem(index, group.getButtonItem(c));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -107,17 +107,17 @@ public abstract class PageableHandler<E> extends FixedPageHandler {
|
|||||||
elementSlot.put(buttonIndex, element);
|
elementSlot.put(buttonIndex, element);
|
||||||
|
|
||||||
String buttonName = getElementButtonName(element);
|
String buttonName = getElementButtonName(element);
|
||||||
ItemStack buttonItem = group.getButton(buttonName);
|
ItemStack buttonItem = group.getButtonItem(buttonName);
|
||||||
initElementButton(buttonIndex, element, buttonItem, variables);
|
initElementButton(buttonIndex, element, buttonItem, variables);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (page == 0) {
|
if (page == 0) {
|
||||||
// 如果页面已在首页则撤掉上一页按钮
|
// 如果页面已在首页则撤掉上一页按钮
|
||||||
inventory.setItem(group.getButtonIndex(previewButtonName), group.getButton(barrierButtonName));
|
inventory.setItem(group.getButtonIndex(previewButtonName), group.getButtonItem(barrierButtonName));
|
||||||
}
|
}
|
||||||
if (elements.size() <= (page + 1) * pageSize) {
|
if (elements.size() <= (page + 1) * pageSize) {
|
||||||
// 如果页面显示超出已有元素数量则撤掉下一页按钮
|
// 如果页面显示超出已有元素数量则撤掉下一页按钮
|
||||||
inventory.setItem(group.getButtonIndex(nextButtonName), group.getButton(barrierButtonName));
|
inventory.setItem(group.getButtonIndex(nextButtonName), group.getButtonItem(barrierButtonName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user