Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2531 command button panel #2535

Merged
merged 5 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/main/java/world/bentobox/bentobox/BentoBox.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package world.bentobox.bentobox;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -466,16 +464,6 @@ public boolean loadSettings() {
return false;
}

log("Saving default panels...");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer required because they are loaded from Jar if not found

if (!Files.exists(Path.of(this.getDataFolder().getPath(), "panels", "island_creation_panel.yml"))) {
log("Saving default island_creation_panel...");
this.saveResource("panels/island_creation_panel.yml", false);
}

if (!Files.exists(Path.of(this.getDataFolder().getPath(), "panels", "language_panel.yml"))) {
log("Saving default language_panel...");
this.saveResource("panels/language_panel.yml", false);
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private CompositeCommand getCommandFromArgs(String[] args) {
*
* @return IslandsManager
*/
protected IslandsManager getIslands() {
public IslandsManager getIslands() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More useful

return plugin.getIslands();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public boolean canExecute(User user, String label, List<String> args) {

@Override
public boolean execute(User user, String label, List<String> args) {
Map<String, IslandInfo> names = getNameIslandMap(user);
// Check if the home is known
if (!args.isEmpty()) {
Map<String, IslandInfo> names = getNameIslandMap(user);
final String name = String.join(" ", args);
if (!names.containsKey(name)) {
// Failed home name check
Expand Down Expand Up @@ -113,7 +113,11 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>

}

private record IslandInfo(Island island, boolean islandName) {}
/**
* Record of islands and the name to type
*/
private record IslandInfo(Island island, boolean islandName) {
}

private Map<String, IslandInfo> getNameIslandMap(User user) {
Map<String, IslandInfo> islandMap = new HashMap<>();
Expand All @@ -129,7 +133,8 @@ private Map<String, IslandInfo> getNameIslandMap(User user) {
islandMap.put(text, new IslandInfo(island, true));
}
// Add homes. Homes do not need an island specified
island.getHomes().keySet().forEach(n -> islandMap.put(n, new IslandInfo(island, false)));
island.getHomes().keySet().stream().filter(n -> !n.isBlank())
.forEach(n -> islandMap.put(n, new IslandInfo(island, false)));
}

return islandMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package world.bentobox.bentobox.api.commands.island;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
import world.bentobox.bentobox.panels.customizable.IslandHomesPanel;

public class IslandHomesCommand extends ConfirmableCommand {

private List<Island> islands;
public class IslandHomesCommand extends CompositeCommand {

public IslandHomesCommand(CompositeCommand islandCommand) {
super(islandCommand, "homes");
Expand All @@ -28,9 +21,8 @@ public void setup() {

@Override
public boolean canExecute(User user, String label, List<String> args) {
islands = getIslands().getIslands(getWorld(), user);
// Check island
if (islands.isEmpty()) {
if (getIslands().getIslands(getWorld(), user).isEmpty()) {
user.sendMessage("general.errors.no-island");
return false;
}
Expand All @@ -39,22 +31,8 @@ public boolean canExecute(User user, String label, List<String> args) {

@Override
public boolean execute(User user, String label, List<String> args) {
user.sendMessage("commands.island.sethome.homes-are");
islands.forEach(island ->
island.getHomes().keySet().stream().filter(s -> !s.isEmpty())
.forEach(s -> user.sendMessage("commands.island.sethome.home-list-syntax", TextVariables.NAME, s)));
IslandHomesPanel.openPanel(this, user);
return true;
}

@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
List<String> result = new ArrayList<>();
for (Island island : getIslands().getIslands(getWorld(), user.getUniqueId())) {
result.addAll(island.getHomes().keySet());
}
return Optional.of(Util.tabLimit(result, lastArg));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,8 @@ ItemSlot nextItemSlot() {
* this button is present.
*
* @return Map that links button type to amount in the gui.
* @deprecated Use {@link #amount(String)} instead.
* Use {@link #amount(String)} instead.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be deprecated, and is used a lot.

*/
@Deprecated
public Map<String, Integer> amountMap() {
return this.parentPanel.typeSlotMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,29 @@ public static PanelTemplateRecord readTemplatePanel(@NonNull String panelName, @
}

File file = new File(panelLocation, templateName.endsWith(YML) ? templateName : templateName + YML);

String absolutePath = file.getAbsolutePath();
if (!file.exists())
{
BentoBox.getInstance().logError(file.getAbsolutePath() + " does not exist for panel template");
// Return as file does not exist.
return null;
// Try to get it from the JAR
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's not there, then try and get it from the Jar.


String keyword = "panels/";

// Find the index of the keyword "panels/"
int index = absolutePath.indexOf(keyword);

// If the keyword is found, extract the substring starting from that index
if (index != -1) {
BentoBox.getInstance().saveResource(absolutePath.substring(index), false);
file = new File(panelLocation, templateName.endsWith(YML) ? templateName : templateName + YML);
} else {
BentoBox.getInstance().logError(file.getAbsolutePath() + " does not exist for panel template");
// Return as file does not exist.
return null;
}

}

final String panelKey = file.getAbsolutePath() + ":" + panelName;
final String panelKey = absolutePath + ":" + panelName;

// Check if panel is already crafted.
if (TemplateReader.loadedPanels.containsKey(panelKey))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package world.bentobox.bentobox.panels.customizable;

import java.io.File;

import org.bukkit.event.inventory.ClickType;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.TemplatedPanel;
import world.bentobox.bentobox.api.panels.reader.ItemTemplateRecord;
import world.bentobox.bentobox.api.user.User;

/**
* @author tastybento
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for use by all customizable panels.

*/
public abstract class AbstractPanel {

// ---------------------------------------------------------------------
// Section: Constants
// ---------------------------------------------------------------------

/**
* This constant is used for button to indicate that it is Language type.
*/
public static final String LOCALE = "LOCALE";

/**
* This constant is used for button to indicate that it is previous page type.
*/
public static final String PREVIOUS = "PREVIOUS";

/**
* This constant is used for button to indicate that it is next page type.
*/
public static final String NEXT = "NEXT";

/**
* This constant is used for indicating that pages should contain numbering.
*/
public static final String INDEXING = "indexing";

/**
* This constant stores value for SELECT action that is used in panels.
*/
public static final String SELECT_ACTION = "SELECT";

/**
* This constant stores value for COMMANDS action that is used in panels.
*/
public static final String COMMANDS_ACTION = "COMMANDS";

/**
* This constant stores value for AUTHORS label that is used in panels.
*/
public static final String AUTHORS = "[authors]";

/**
* This constant stores value for SELECTED label that is used in panels.
*/
public static final String SELECTED = "[selected]";

/**
* This variable allows to access plugin object.
*/
final BentoBox plugin;

/**
* This variable stores main command that was triggered.
*/
final CompositeCommand command;

/**
* This variable holds user who opens panel. Without it panel cannot be opened.
*/
final User user;

/**
* This variable holds world where panel is opened. Without it panel cannot be opened.
*/
String mainLabel;

/**
* This variable holds current pageIndex for multi-page island choosing.
*/
int pageIndex;

public AbstractPanel(CompositeCommand command, User user) {
plugin = command.getPlugin();
this.command = command;
this.user = user;
this.pageIndex = 0; // Start with the first page by default
}

// Abstract methods for creating next and previous buttons
protected abstract PanelItem createNextButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot);

protected abstract PanelItem createPreviousButton(ItemTemplateRecord template, TemplatedPanel.ItemSlot slot);

// Abstract build method to allow each panel to define its own layout
protected abstract void build();

// Default method for pagination, can be overridden by subclasses if needed
protected boolean hasNextPage(int elementListSize, int itemsPerPage) {
return (pageIndex + 1) * itemsPerPage < elementListSize;
}

protected boolean hasPreviousPage() {
return pageIndex > 0;
}

// Method to handle the click event on next/previous buttons
protected boolean handlePageChange(ItemTemplateRecord.ActionRecords action, ClickType clickType,
String actionType) {
if ((clickType == action.clickType() || action.clickType() == ClickType.UNKNOWN)
&& actionType.equalsIgnoreCase(action.actionType())) {
if (actionType.equalsIgnoreCase("NEXT")) {
this.pageIndex++;
} else if (actionType.equalsIgnoreCase("PREVIOUS")) {
this.pageIndex--;
}
build();
return true;
}
return false;
}

/**
* This method returns if panel with the requested name is located in GameModeAddon folder.
* @param addon GameModeAddon that need to be checked.
* @param name Name of the panel.
* @return {@code true} if panel exists, {@code false} otherwise.
*/
protected boolean doesCustomPanelExists(GameModeAddon addon, String name) {
return addon.getDataFolder().exists() && new File(addon.getDataFolder(), "panels").exists()
&& new File(addon.getDataFolder(), "panels" + File.separator + name + ".yml").exists();
}

}
Loading
Loading