Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Comment thread
Jasupa marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,33 @@
import com.alpsbte.alpslib.utils.ChatHelper;
import net.buildtheearth.buildteamtools.modules.generator.GeneratorModule;
import net.buildtheearth.buildteamtools.modules.generator.menu.GeneratorMenu;
import net.buildtheearth.buildteamtools.modules.generator.model.History;
import net.buildtheearth.buildteamtools.modules.generator.model.GeneratorType;
import net.buildtheearth.buildteamtools.modules.generator.model.HistoryEntry;
import net.buildtheearth.buildteamtools.modules.network.model.Permissions;
import net.buildtheearth.buildteamtools.utils.Utils;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class GeneratorCommand implements CommandExecutor {
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class GeneratorCommand implements CommandExecutor, TabCompleter {

public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String cmdLabel,
String @NotNull [] args) {
private static final List<String> HISTORY_COMMANDS = List.of("history", "undo", "redo");
private static final List<String> SUB_COMMANDS = createSubCommands();
private static final List<String> HELP_ARGUMENTS = List.of("help", "info", "?");

public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String cmdLabel, String @NotNull [] args) {
if (!(sender instanceof Player p)) {
sender.sendMessage("§cOnly players can execute this command.");
sender.sendMessage(ChatHelper.PREFIX_COMPONENT.append(ChatHelper.getErrorComponent(
"Only players can execute this command."
)));
return true;
}

Expand All @@ -29,55 +38,34 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
return true;
}

// Command Usage: /gen
if (args.length == 0) {
new GeneratorMenu(p, true);
return true;
}

GeneratorType generatorType = GeneratorType.fromCommandName(args[0]);
if (generatorType != null)
return runGeneratorCommand(p, args, generatorType);

// Command Usage: /gen house ...
switch (args[0]) {
case "house":
GeneratorModule.getInstance().getHouse().analyzeCommand(p, args);
return true;

// Command Usage: /gen road ...
case "road":
GeneratorModule.getInstance().getRoad().analyzeCommand(p, args);
return true;

// Command Usage: /gen rail ...
case "rail":
p.sendMessage(Component.text("This generator have some serious issues and is currently disabled.",
NamedTextColor.DARK_RED));
//GeneratorModule.getInstance().getRail().analyzeCommand(p, args);
return true;

// Command Usage: /gen tree ...
case "tree":
GeneratorModule.getInstance().getTree().analyzeCommand(p, args);
return true;

// Command Usage: /gen field ...
case "field":
p.sendMessage(Component.text("This generator have some serious issues and is currently disabled.",
NamedTextColor.DARK_RED));
//GeneratorModule.getInstance().getField().analyzeCommand(p, args);
return true;

// Command Usage: /gen history
switch (args[0].toLowerCase()) {
case "history":
if (GeneratorModule.getInstance().getPlayerHistory(p).getHistoryEntries().isEmpty()) {
p.sendMessage("§cYou didn't generate any structures yet. Use /gen to create one.");
p.sendMessage(ChatHelper.PREFIX_COMPONENT.append(ChatHelper.getErrorComponent(
"You didn't generate any structures yet. Use /gen to create one."
)));
return true;
}

ChatHelper.sendMessageBox(sender, "Generator History for " + p.getName(), () -> {
for (History.HistoryEntry history : GeneratorModule.getInstance().getPlayerHistory(p).getHistoryEntries()) {
for (HistoryEntry history : GeneratorModule.getInstance().getPlayerHistory(p).getHistoryEntries()) {
long timeDifference = System.currentTimeMillis() - history.getTimeCreated();
p.sendMessage("§e- " + history.getGeneratorType().name() + " §7-§e " + Utils.toDate(timeDifference) +
" ago §7-§e " + history.getWorldEditCommandCount() + " Commands executed");
p.sendMessage(ChatHelper.getStandardComponent(
false,
"- %s - %s ago - %s Commands executed",
history.getGeneratorType().name(),
Utils.toDate(timeDifference),
history.getWorldEditCommandCount()
));
}
});
return true;
Expand All @@ -89,25 +77,95 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
case "redo":
GeneratorModule.getInstance().getPlayerHistory(p).redoCommand(p);
return true;

default:
sendHelp(p);
return true;
}
}

public static void sendHelp(CommandSender sender) {
ChatHelper.sendMessageBox(sender, "Generator Command", () -> {

sender.sendMessage("§eHouse Generator:§7 /gen house help");
sender.sendMessage("§eRoad Generator:§7 /gen road help");
sender.sendMessage("§eRail Generator:§7 /gen rail help");
sender.sendMessage("§eTree Generator:§7 /gen tree help");
sender.sendMessage("§eField Generator:§7 /gen field help");
sender.sendMessage("§7----------------------");
sender.sendMessage("§eGenerator History:§7 /gen history");
sender.sendMessage("§eUndo last command:§7 /gen undo");
sender.sendMessage("§eRedo last command:§7 /gen redo");

});
ChatHelper.sendMessageBox(
sender,
"Generator Command",
() -> sender.sendMessage(ChatHelper.getStandardComponent(
false,
"Generators: " + createPlaceholders(GeneratorType.values().length),
(Object[]) getGeneratorHelpCommands())
.appendNewline()
Comment thread
Jasupa marked this conversation as resolved.
.append(ChatHelper.getStandardComponent(
false,
"History: %s, %s, %s",
"/gen history",
"/gen undo",
"/gen redo")))
);
}

@Override
public List<String> onTabComplete(
@NotNull CommandSender sender,
@NotNull Command command,
@NotNull String label,
String @NotNull [] args
) {
if (!sender.hasPermission(Permissions.GENERATOR_USE))
return List.of();

if (args.length == 1)
return getMatchingCompletions(SUB_COMMANDS, args[0]);

if (args.length == 2 && isGeneratorSubCommand(args[0]))
return getMatchingCompletions(HELP_ARGUMENTS, args[1]);

return List.of();
}

private boolean runGeneratorCommand(Player player, String[] args, GeneratorType generatorType) {
if (generatorType == GeneratorType.FIELD) {
player.sendMessage(ChatHelper.PREFIX_COMPONENT.append(ChatHelper.getErrorComponent(
"This generator has serious issues and is currently disabled."
)));
return true;
}

generatorType.getComponent(GeneratorModule.getInstance()).analyzeCommand(player, args);
return true;
}

private boolean isGeneratorSubCommand(String value) {
return GeneratorType.fromCommandName(value) != null;
}
Comment thread
Jasupa marked this conversation as resolved.

private List<String> getMatchingCompletions(List<String> options, String input) {
String normalizedInput = input.toLowerCase();
List<String> completions = new ArrayList<>();

for (String option : options)
if (option.startsWith(normalizedInput))
completions.add(option);

return completions;
}

private static List<String> createSubCommands() {
List<String> commands = new ArrayList<>();

Arrays.stream(GeneratorType.values())
.map(GeneratorType::getCommandName)
.forEach(commands::add);

commands.addAll(HISTORY_COMMANDS);
return List.copyOf(commands);
}

private static String[] getGeneratorHelpCommands() {
return Arrays.stream(GeneratorType.values())
.map(type -> "/gen " + type.getCommandName() + " help")
.toArray(String[]::new);
}

private static String createPlaceholders(int count) {
return String.join(", ", Collections.nCopies(count, "%s"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public boolean checkForPlayer(Player p) {
if (GeneratorUtils.checkForNoWorldEditSelection(p))
return false;

if (getPlayerSettings().get(p.getUniqueId()).getBlocks() == null) // Needed because block checks are made afterwards
if (getPlayerSettings().get(p.getUniqueId()).getBlocks() == null)
getPlayerSettings().get(p.getUniqueId()).setBlocks(GeneratorUtils.analyzeRegion(p, p.getWorld()));

Block[][][] blocks = getPlayerSettings().get(p.getUniqueId()).getBlocks();
Expand All @@ -35,4 +35,4 @@ public void generate(Player p) {

new HouseScripts(p, this);
}
}
}
Comment thread
Jasupa marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.buildtheearth.buildteamtools.modules.generator.components.rail;

import org.bukkit.util.Vector;

record PositionKey(int x, int y, int z) {

static PositionKey from(Vector vector) {
return new PositionKey(
vector.getBlockX(),
vector.getBlockY(),
vector.getBlockZ()
);
}

static PositionKey of(int x, int y, int z) {
return new PositionKey(x, y, z);
}

Vector toVector() {
return new Vector(x, y, z);
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,72 @@
package net.buildtheearth.buildteamtools.modules.generator.components.rail;

import com.alpsbte.alpslib.utils.ChatHelper;
import com.alpsbte.alpslib.utils.GeneratorUtils;
import com.sk89q.worldedit.regions.ConvexPolyhedralRegion;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Polygonal2DRegion;
import com.sk89q.worldedit.regions.Region;
import net.buildtheearth.buildteamtools.modules.generator.GeneratorModule;
import net.buildtheearth.buildteamtools.modules.generator.model.GeneratorComponent;
import net.buildtheearth.buildteamtools.modules.generator.model.GeneratorType;
import org.bukkit.Sound;
import org.bukkit.entity.Player;

import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

public class Rail extends GeneratorComponent {

private final Set<UUID> preparingPlayers = ConcurrentHashMap.newKeySet();

public Rail() {
super(GeneratorType.RAILWAY);
super(GeneratorType.RAIL);
}

@Override
public boolean checkForPlayer(Player p) {
return !GeneratorUtils.checkForNoWorldEditSelection(p);
public boolean checkForPlayer(Player player) {
if (GeneratorUtils.checkForNoWorldEditSelection(player))
return false;

Region region = GeneratorUtils.getWorldEditSelection(player);

if (isSupportedRailSelection(region))
return true;

player.sendMessage(ChatHelper.PREFIX_COMPONENT.append(ChatHelper.getErrorComponent(
"Rail Generator supports cuboid, polygonal and convex WorldEdit selections."
Comment thread
Jasupa marked this conversation as resolved.
)));
player.closeInventory();
player.playSound(player.getLocation(), Sound.ENTITY_ITEM_BREAK, 1.0F, 1.0F);
return false;
}

private boolean isSupportedRailSelection(Region region) {
return region instanceof CuboidRegion
|| region instanceof Polygonal2DRegion
|| region instanceof ConvexPolyhedralRegion;
}

@Override
public void generate(Player p) {
if (!GeneratorModule.getInstance().getRail().checkForPlayer(p))
public void generate(Player player) {
if (GeneratorModule.getInstance().isGenerating(player) || !preparingPlayers.add(player.getUniqueId())) {
sendAlreadyGeneratingMessage(player);
return;
}

if (!checkForPlayer(player)) {
preparingPlayers.remove(player.getUniqueId());
return;
}

new RailScripts(player, this, () -> preparingPlayers.remove(player.getUniqueId()));
}

new RailScripts(p, this);
private void sendAlreadyGeneratingMessage(Player player) {
player.sendMessage(ChatHelper.PREFIX_COMPONENT.append(ChatHelper.getErrorComponent(
"Rail Generator is already running. Please wait until the current generation is finished."
)));
player.playSound(player.getLocation(), Sound.ENTITY_ITEM_BREAK, 1.0F, 1.0F);
}
}
}
Loading
Loading