Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import net.buildtheearth.buildteamtools.modules.generator.model.History;
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;
Expand All @@ -16,54 +14,44 @@

public class GeneratorCommand implements CommandExecutor {


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.");
return true;
}

if(!p.hasPermission(Permissions.GENERATOR_USE)) {
if (!p.hasPermission(Permissions.GENERATOR_USE)) {
p.sendMessage(ChatHelper.getErrorString("You don't have permission to use this command!"));
return true;
}

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


// Command Usage: /gen house ...
switch (args[0]) {
switch (args[0].toLowerCase()) {
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);
case "railway":
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);
p.sendMessage("§cThis generator has serious issues and is currently disabled.");
return true;

// Command Usage: /gen history
case "history":
if (GeneratorModule.getInstance().getPlayerHistory(p).getHistoryEntries().isEmpty()) {
p.sendMessage("§cYou didn't generate any structures yet. Use /gen to create one.");
Expand All @@ -85,6 +73,7 @@ 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;
Expand All @@ -93,7 +82,6 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N

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");
Expand All @@ -103,7 +91,6 @@ public static void sendHelp(CommandSender sender) {
sender.sendMessage("§eGenerator History:§7 /gen history");
sender.sendMessage("§eUndo last command:§7 /gen undo");
sender.sendMessage("§eRedo last command:§7 /gen redo");

});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package net.buildtheearth.buildteamtools.modules.generator.components.rail;

import com.alpsbte.alpslib.utils.GeneratorUtils;
import com.sk89q.worldedit.regions.Region;
import net.buildtheearth.buildteamtools.modules.generator.GeneratorModule;
import net.buildtheearth.buildteamtools.modules.generator.components.rail.selection.RailSelectionPointReader;
import net.buildtheearth.buildteamtools.modules.generator.model.GeneratorComponent;
import net.buildtheearth.buildteamtools.modules.generator.model.GeneratorType;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;

import java.util.List;

public class Rail extends GeneratorComponent {

Expand All @@ -13,15 +18,39 @@ public Rail() {
}

@Override
public boolean checkForPlayer(Player p) {
return !GeneratorUtils.checkForNoWorldEditSelection(p);
public boolean checkForPlayer(Player player) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Method name doesnt really correspond to what is checked inside

return hasValidRailSelection(player);
}

private boolean hasValidRailSelection(Player player) {
if (GeneratorUtils.checkForNoWorldEditSelection(player)) {
player.sendMessage("§cRail Generator requires an active WorldEdit selection.");
return false;
}

Region region = GeneratorUtils.getWorldEditSelection(player);
RailSelectionPointReader reader = new RailSelectionPointReader(player, region);

if (!reader.isSupportedSelection()) {
player.sendMessage("§cRail Generator only supports cuboid, polygonal and convex WorldEdit selections.");
return false;
}

List<Vector> controlPoints = reader.readControlPoints();

if (controlPoints.size() < 2) {
player.sendMessage("§cRail Generator could not read enough points from this selection.");
return false;
}

return true;
}

@Override
public void generate(Player p) {
if (!GeneratorModule.getInstance().getRail().checkForPlayer(p))
public void generate(Player player) {
if (!GeneratorModule.getInstance().getRail().checkForPlayer(player))
return;

new RailScripts(p, this);
new RailScripts(player, this);
}
}
Original file line number Diff line number Diff line change
@@ -1,127 +1,102 @@
package net.buildtheearth.buildteamtools.modules.generator.components.rail;

import com.alpsbte.alpslib.utils.GeneratorUtils;
import net.buildtheearth.buildteamtools.BuildTeamTools;
import net.buildtheearth.buildteamtools.modules.generator.GeneratorModule;
import net.buildtheearth.buildteamtools.modules.generator.components.rail.path.RailPath;
import net.buildtheearth.buildteamtools.modules.generator.components.rail.path.RailPathBuilder;
import net.buildtheearth.buildteamtools.modules.generator.components.rail.placement.RailBlockPlacement;
import net.buildtheearth.buildteamtools.modules.generator.components.rail.placement.RailPlacementBuilder;
import net.buildtheearth.buildteamtools.modules.generator.components.rail.placement.RailWorldEditPlacer;
import net.buildtheearth.buildteamtools.modules.generator.components.rail.selection.RailSelectionPointReader;
import net.buildtheearth.buildteamtools.modules.generator.components.rail.types.RailType;
import net.buildtheearth.buildteamtools.modules.generator.components.rail.types.SampleRailType;
import net.buildtheearth.buildteamtools.modules.generator.model.GeneratorComponent;
import net.buildtheearth.buildteamtools.modules.generator.model.GeneratorType;
import net.buildtheearth.buildteamtools.modules.generator.model.History;
import net.buildtheearth.buildteamtools.modules.generator.model.Script;
import org.bukkit.block.Block;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

public class RailScripts extends Script {

private final RailPathBuilder pathBuilder = new RailPathBuilder();
private final RailPlacementBuilder placementBuilder = new RailPlacementBuilder();
private final RailWorldEditPlacer placer = new RailWorldEditPlacer();

private final RailType railType = new SampleRailType();

public RailScripts(Player player, GeneratorComponent generatorComponent) {
super(player, generatorComponent);

throw new UnsupportedOperationException("RailScripts is currently broken.");
//getPlayer().chat("/clearhistory");
//Bukkit.getScheduler().runTaskAsynchronously(BuildTeamTools.getInstance(), this::railScript_v_1_3);
Thread thread = new Thread(this::generateRail);
thread.start();
}

public void railScript_v_1_3() {
List<String> commands = new ArrayList<>();
//HashMap<Flag, String > flags = rail.getPlayerSettings().get(p.getUniqueId()).getValues();

int xPos = getPlayer().getLocation().getBlockX();
int zPos = getPlayer().getLocation().getBlockZ();

int operations = 0;

int railWidth = 5;


// TODO START TEMP

Block[][][] regionBlocks = GeneratorUtils.analyzeRegion(getPlayer(), getPlayer().getWorld());
List<Vector> points = GeneratorUtils.getSelectionPointsFromRegion(getRegion());
createCuboidSelection(points.get(0), points.get(1));
createCommand("//set redstone_block");
createBreakPoint();
createCommand("//set gold_block");

finish(regionBlocks, points);


// TODO END TEMP
private void generateRail() {
try {
List<Vector> controlPoints = getControlPoints();

/*
// Get the points of the region
List<Vector> points = GeneratorUtils.getSelectionPointsFromRegion(getRegion());
points = GeneratorUtils.populatePoints(points, 5);
if (controlPoints.size() < 2) {
getPlayer().sendMessage("§cRail Generator needs at least two usable points in the selection.");
return;
}

// ----------- PREPARATION 01 ----------
// Replace all unnecessary blocks with air
RailPath railPath = pathBuilder.build(controlPoints);

List<Vector> polyRegionLine = new ArrayList<>(points);
polyRegionLine = GeneratorUtils.extendPolyLine(polyRegionLine);
List<Vector> polyRegionPoints = GeneratorUtils.shiftPoints(polyRegionLine, railWidth + 2, true);
if (!railPath.isValid()) {
getPlayer().sendMessage("§cRail Generator could not derive a valid path from this selection.");
return;
}

// Create a region from the points
GeneratorUtils.createPolySelection(getPlayer(), polyRegionPoints, null);
List<RailBlockPlacement> placements = placementBuilder.buildPlacements(railPath);

getPlayer().chat("//expand 30 up");
getPlayer().chat("//expand 10 down");
if (placements.isEmpty()) {
getPlayer().sendMessage("§cRail Generator did not create any block placements.");
return;
}

// Remove non-solid blocks
getPlayer().chat("//gmask !#solid");
getPlayer().chat("//replace 0");
operations++;
Bukkit.getScheduler().runTask(BuildTeamTools.getInstance(), () -> placeRail(placements));
} catch (Exception exception) {
getPlayer().sendMessage("§cRail Generator failed while generating from the WorldEdit selection.");

// Remove all trees and pumpkins
getPlayer().chat("//gmask");
getPlayer().chat("//replace leaves,log,pumpkin 0");
operations++;

getPlayer().chat("//gmask");


Block[][][] regionBlocks = GeneratorUtils.analyzeRegion(getPlayer(), getPlayer().getWorld());
GeneratorUtils.adjustHeight(points, regionBlocks);


// ----------- RAILWAY ----------

// Draw the railway curve

GeneratorUtils.createConvexSelection(commands, points);
commands.add("//gmask !solid");
commands.add("//curve 42");
operations++;
commands.add("//gmask");
BuildTeamTools.getInstance().getLogger().log(
Level.SEVERE,
"Rail Generator failed while generating from the WorldEdit selection.",
exception
);
}
}

private List<Vector> getControlPoints() {
RailSelectionPointReader reader = new RailSelectionPointReader(getPlayer(), getRegion());
return reader.readControlPoints();
}

// Create the railway
GeneratorUtils.createPolySelection(commands, polyRegionPoints);
private void placeRail(List<RailBlockPlacement> placements) {
boolean placedWithWorldEdit = placer.placeWithWorldEdit(this, placements, railType);

commands.add("//replace \"0 !>42 =queryRel(0,-1,-1,42,-1)||queryRel(0,-1,1,42,-1)\" 145:1");
operations++;
commands.add("//replace \"0 !>42 =queryRel(-1,-1,0,42,-1)||queryRel(1,-1,0,42,-1)\" 145:0");
operations++;
if (!placedWithWorldEdit) {
getPlayer().sendMessage("§eWorldEdit history is unavailable. Falling back to Bukkit placement.");
getPlayer().sendMessage("§eUse §6/gen undo§e instead of §6//undo§e for this generation.");

commands.add("//gmask =(sqrt((x-(" + xPos + "))^2+(z-(" + zPos + "))^2)%3)-2");
commands.add("//replace \"0 =queryRel(0,0,1,145,-1)||queryRel(0,0,-1,145,-1)||queryRel(1,0,0,145,-1)||queryRel(-1,0,0,145,-1)\" 44:0");
operations++;
commands.add("//replace \"!145 !0 <145\" 43:0");
operations++;
commands.add("//gmask");
commands.add("//replace 42 2");
operations++;
List<History.BlockChange> changes = placer.placeWithBukkitFallback(this, placements, railType);

commands.add("//gmask");
GeneratorModule.getInstance()
.getPlayerHistory(getPlayer())
.addHistoryEntry(new History.HistoryEntry(GeneratorType.RAILWAY, this, changes));

// Depending on the selection type, the selection needs to be restored correctly
if(getRegion() instanceof Polygonal2DRegion || getRegion() instanceof ConvexPolyhedralRegion)
GeneratorUtils.createConvexSelection(commands, points);
else if(getRegion() instanceof CuboidRegion){
CuboidRegion cuboidRegion = (CuboidRegion) getRegion();
Vector pos1 = new Vector(cuboidRegion.getPos1().getX(), cuboidRegion.getPos1().getY(), cuboidRegion.getPos1().getZ());
Vector pos2 = new Vector(cuboidRegion.getPos2().getX(), cuboidRegion.getPos2().getY(), cuboidRegion.getPos2().getZ());
GeneratorUtils.createCuboidSelection(commands, pos1, pos2);
getGeneratorComponent().sendSuccessMessage(getPlayer());
return;
}

GeneratorModule.getInstance().getGeneratorCommands().add(new Command(getPlayer(), getGeneratorComponent(), commands, operations, regionBlocks));
GeneratorModule.getInstance().getPlayerHistory(getPlayer()).addHistoryEntry(new History.HistoryEntry(GeneratorType.RAILWAY, operations));*/
GeneratorModule.getInstance()
.getPlayerHistory(getPlayer())
.addHistoryEntry(new History.HistoryEntry(GeneratorType.RAILWAY, this, 1));

getGeneratorComponent().sendSuccessMessage(getPlayer());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.buildtheearth.buildteamtools.modules.generator.components.rail.path;

import org.bukkit.util.Vector;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RailPath {

private final List<Vector> centerPath;

public RailPath(List<Vector> centerPath) {
this.centerPath = new ArrayList<>(centerPath);
}

public List<Vector> getCenterPath() {
return Collections.unmodifiableList(centerPath);
}

public int size() {
return centerPath.size();
}

public boolean isValid() {
return centerPath.size() >= 2;
}
}
Loading
Loading