Skip to content

Commit

Permalink
Adds an island home panel. #2531
Browse files Browse the repository at this point in the history
Also enhances the customizable panel API by making templates load
dynamically when requested.
  • Loading branch information
tastybento committed Oct 20, 2024
1 parent 58690f2 commit e0a3f48
Show file tree
Hide file tree
Showing 8 changed files with 526 additions and 47 deletions.
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...");
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() {
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 @@ -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

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
Loading

0 comments on commit e0a3f48

Please sign in to comment.