Skip to content

Commit

Permalink
feat: all plot is now forced to paste in (0, 0)
Browse files Browse the repository at this point in the history
* Reverted change to MIN_WORLD_HEIGHT variable
* Added heightThreshold to be accounted for the additional height when a plot schematic is made

* Shifted every creation of plots to (0, 0) by its center point
* Also edited any related properties to shifting plot like TPLL
  • Loading branch information
tintinkung committed Apr 11, 2024
1 parent c5eb6c5 commit 81b86c1
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 53 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/alpsbte/plotsystem/commands/CMD_Tpll.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
// Convert terra coordinates to plot relative coordinates
CompletableFuture<double[]> plotCoords = plot != null ? PlotUtils.convertTerraToPlotXZ(plot, terraCoords) : null;

Bukkit.getLogger().log(Level.INFO, "doing TPLL to coords: " + plotCoords);

if(plotCoords == null) {
player.sendMessage(Utils.ChatUtils.getAlertFormat(langUtil.get(sender, LangPaths.Message.Error.CANNOT_TELEPORT_OUTSIDE_PLOT)));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
import java.util.List;
import java.util.logging.Level;

import static com.alpsbte.plotsystem.core.system.plot.world.PlotWorld.MAX_WORLD_HEIGHT;
import static com.alpsbte.plotsystem.core.system.plot.world.PlotWorld.MIN_WORLD_HEIGHT;

public abstract class AbstractPlot {
public static final double PLOT_VERSION = 3;

Expand Down Expand Up @@ -166,6 +169,9 @@ public BlockVector3 getCenter() {
Clipboard clipboard = FaweAPI.load(getOutlinesSchematic());
if (clipboard != null) {
Vector3 clipboardCenter = clipboard.getRegion().getCenter();

Bukkit.getLogger().log(Level.INFO, "Loading Clipboard Center at: " + BlockVector3.at(clipboardCenter.getX(), this.getWorld().getPlotHeightCentered(), clipboardCenter.getZ()));

return BlockVector3.at(clipboardCenter.getX(), this.getWorld().getPlotHeightCentered(), clipboardCenter.getZ());
}
} catch (IOException | SQLException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.sk89q.worldedit.function.mask.RegionMask;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector2;
import com.sk89q.worldedit.regions.CylinderRegion;
Expand All @@ -68,6 +69,7 @@
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -174,13 +176,21 @@ protected void createPlotProtection() throws StorageException, SQLException, IOE
RegionManager regionManager = regionContainer.get(BukkitAdapter.adapt(world.getBukkitWorld()));

if (regionManager != null) {
BlockVector2 center = BlockVector2.at(plot.getCenter().getX(), plot.getCenter().getZ()) ;
// Create build region for plot from the outline of the plot
ProtectedRegion protectedBuildRegion = new ProtectedPolygonalRegion(world.getRegionName(), plot.getOutline(), PlotWorld.MIN_WORLD_HEIGHT, PlotWorld.MAX_WORLD_HEIGHT);
List<BlockVector2> plotOutlines = plot.getOutline();

Bukkit.getLogger().log(Level.INFO, "Configuring Plot outlines protection from:\n" + plotOutlines);
for(int i = 0; i < plotOutlines.size(); i++)
plotOutlines.set(i, BlockVector2.at(plotOutlines.get(i).getX() - center.getX(), plotOutlines.get(i).getZ() - center.getZ()));
Bukkit.getLogger().log(Level.INFO, "Configured Plot outlines protection to:\n" + plotOutlines);

ProtectedRegion protectedBuildRegion = new ProtectedPolygonalRegion(world.getRegionName(), plotOutlines, PlotWorld.MIN_WORLD_HEIGHT, PlotWorld.MAX_WORLD_HEIGHT);
protectedBuildRegion.setPriority(100);

// Create protected plot region for plot
World weWorld = new BukkitWorld(world.getBukkitWorld());
CylinderRegion cylinderRegion = new CylinderRegion(weWorld, plot.getCenter(), Vector2.at(PlotWorld.PLOT_SIZE, PlotWorld.PLOT_SIZE), PlotWorld.MIN_WORLD_HEIGHT, PlotWorld.MAX_WORLD_HEIGHT);
CylinderRegion cylinderRegion = new CylinderRegion(weWorld, BlockVector3.at(0, plot.getCenter().getY(), 0), Vector2.at(PlotWorld.PLOT_SIZE, PlotWorld.PLOT_SIZE), PlotWorld.MIN_WORLD_HEIGHT, PlotWorld.MAX_WORLD_HEIGHT);
ProtectedRegion protectedRegion = new ProtectedPolygonalRegion(world.getRegionName() + "-1", cylinderRegion.polygonize(-1), PlotWorld.MIN_WORLD_HEIGHT, PlotWorld.MAX_WORLD_HEIGHT);
protectedRegion.setPriority(50);

Expand Down Expand Up @@ -294,18 +304,25 @@ public static void pasteSchematic(@Nullable Mask pasteMask, File schematicFile,
World weWorld = new BukkitWorld(world.getBukkitWorld());
try (EditSession editSession = WorldEdit.getInstance().newEditSession(BukkitAdapter.adapt(world.getBukkitWorld()))) {
if (clearArea) {
Polygonal2DRegion polyRegion = new Polygonal2DRegion(weWorld, world.getPlot().getOutline(), 0, PlotWorld.MAX_WORLD_HEIGHT);
BlockVector3 center = BlockVector3.at(world.getPlot().getCenter().getBlockX(), world.getPlotHeight(), world.getPlot().getCenter().getBlockZ());
Polygonal2DRegion polyRegion = new Polygonal2DRegion(weWorld, world.getPlot().getOutline(), 0, PlotWorld.MAX_WORLD_HEIGHT);
polyRegion.shift(BlockVector3.at(-center.getX(), 0, -center.getZ()));

Bukkit.getLogger().log(Level.INFO, "Clearing plot region at:\n" + polyRegion);

editSession.setMask(new RegionMask(polyRegion));
editSession.setBlocks(polyRegion, Objects.requireNonNull(BlockTypes.AIR).getDefaultState());
}
}
try (EditSession editSession = WorldEdit.getInstance().newEditSession(BukkitAdapter.adapt(world.getBukkitWorld()))) {
if(pasteMask != null) editSession.setMask(pasteMask);
Clipboard clipboard = FaweAPI.load(schematicFile);

Operation clipboardHolder = new ClipboardHolder(clipboard)
.createPaste(editSession)
.to(BlockVector3.at(world.getPlot().getCenter().getBlockX(), world.getPlotHeight(), world.getPlot().getCenter().getBlockZ()))
.to(BlockVector3.at(0, world.getPlotHeight(), 0))
.build();

Operations.complete(clipboardHolder);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.alpsbte.plotsystem.utils.io.LangPaths;
import com.alpsbte.plotsystem.utils.io.LangUtil;
import com.fastasyncworldedit.core.FaweAPI;
import com.fastasyncworldedit.core.util.collection.BlockVector3Set;
import com.github.fierioziy.particlenativeapi.api.ParticleNativeAPI;
import com.github.fierioziy.particlenativeapi.api.Particles_1_8;
import com.github.fierioziy.particlenativeapi.plugin.ParticleNativePlugin;
Expand Down Expand Up @@ -74,8 +75,10 @@
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.util.BlockVector;
import org.jetbrains.annotations.Nullable;

import java.io.File;
Expand All @@ -93,6 +96,8 @@
import java.util.concurrent.CompletionException;
import java.util.logging.Level;

import static com.alpsbte.plotsystem.core.system.plot.world.PlotWorld.MAX_WORLD_HEIGHT;
import static com.alpsbte.plotsystem.core.system.plot.world.PlotWorld.MIN_WORLD_HEIGHT;
import static net.md_5.bungee.api.ChatColor.*;

public final class PlotUtils {
Expand Down Expand Up @@ -200,12 +205,24 @@ public static boolean savePlotAsSchematic(Plot plot) throws IOException, SQLExce

// Get plot outline
List<BlockVector2> plotOutlines = plot.getOutline();
Bukkit.getLogger().log(Level.INFO, "Getting Plot region for saving from: " + cuboidRegion);



// Shift schematic region to the force (0, 0) paste
for(int i = 0; i < plotOutlines.size(); i++)
plotOutlines.set(i, BlockVector2.at(plotOutlines.get(i).getX() - plotCenter.getX(), plotOutlines.get(i).getZ() - plotCenter.getZ()));
cuboidRegion.shift(BlockVector3.at(-plotCenter.getX(), 0, -plotCenter.getZ()));

Bukkit.getLogger().log(Level.INFO, "Shifted Plot region for saving to: " + cuboidRegion);

// Load finished plot region as cuboid region
if (plot.getWorld().loadWorld()) {
com.sk89q.worldedit.world.World world = new BukkitWorld(plot.getWorld().getBukkitWorld());
Polygonal2DRegion region = new Polygonal2DRegion(world, plotOutlines, cuboidRegion.getMinimumPoint().getBlockY(), cuboidRegion.getMaximumPoint().getBlockY());

Bukkit.getLogger().log(Level.INFO, "Saving schems at region: " + region);

// Copy and write finished plot clipboard to schematic file
File finishedSchematicFile = Paths.get(PlotUtils.getDefaultSchematicPath(),
String.valueOf(plot.getCity().getCountry().getServer().getID()),
Expand All @@ -221,8 +238,10 @@ public static boolean savePlotAsSchematic(Plot plot) throws IOException, SQLExce

Clipboard cb = new BlockArrayClipboard(region);
if (plot.getVersion() >= 3) {
cb.setOrigin(BlockVector3.at(plotCenter.getX(), cuboidRegion.getMinimumY(), (double) plotCenter.getZ()));
Bukkit.getLogger().log(Level.INFO, "Setting origin schems at: " + BlockVector3.at(0, cuboidRegion.getMinimumY(), (double) 0));
cb.setOrigin(BlockVector3.at(0, cuboidRegion.getMinimumY(), (double) 0));
} else {
Bukkit.getLogger().log(Level.SEVERE, "This should not happen, whyyy");
BlockVector3 terraCenter = plot.getMinecraftCoordinates();
plotCenter = BlockVector3.at(
(double) terraCenter.getX() - (double) clipboard.getMinimumPoint().getX() + cuboidRegion.getMinimumPoint().getX(),
Expand Down Expand Up @@ -280,8 +299,8 @@ public static CompletableFuture<double[]> convertTerraToPlotXZ(AbstractPlot plot

// Add additional plot sizes to relative plot schematic coordinates
double[] plotCoords = {
schematicCoords[0] + plotRegion.getMinimumPoint().getX(),
schematicCoords[1] + plotRegion.getMinimumPoint().getZ()
schematicCoords[0] + plotRegion.getMinimumPoint().getX() - plot.getCenter().getX(),
schematicCoords[1] + plotRegion.getMinimumPoint().getZ() - plot.getCenter().getZ()
};

// Return coordinates if they are in the schematic plot region
Expand Down Expand Up @@ -567,26 +586,63 @@ public static void showOutlines(){

if(plots.isEmpty()) continue;

for(Plot plot : plots){
if(!plot.getWorld().getWorldName().equals(player.getWorld().getName()))
for(Plot plot : plots) {
if (!plot.getWorld().getWorldName().equals(player.getWorld().getName()))
continue;

if(!plot.getPlotOwner().getPlotTypeSetting().hasEnvironment() || plot.getVersion() <= 2)
if (!plot.getPlotOwner().getPlotTypeSetting().hasEnvironment() || plot.getVersion() <= 2)
continue;

List<BlockVector2> points = plot.getBlockOutline();
// BlockVector2 center = BlockVector2.at(plot.getCenter().getX(), plot.getCenter().getZ());

for(BlockVector2 point : points) if(point.distanceSq(playerPos2D) < 50*50)
if(!ParticleAPIEnabled)
player.spawnParticle(Particle.FLAME, point.getX(), player.getLocation().getY() + 1, point.getZ(), 1, 0.0 ,0.0,0.0, 0);
else{
Location loc = new Location(player.getWorld(), point.getX(), player.getLocation().getY() + 1, point.getZ());
// create a particle packet
Object packet = particles.FLAME().packet(true, loc);
// for(int i = 0; i < points.size(); i++)
// points.set(i, BlockVector2.at(points.get(i).getX() - center.getX(), points.get(i).getZ() - center.getZ()));

// send this packet to player
particles.sendPacket(player, packet);
BlockVector2 min = BlockVector2.ZERO;
BlockVector2 max = BlockVector2.ZERO;

int minX = points.get(0).getX();
int minZ = points.get(0).getZ();
int maxX = points.get(0).getX();
int maxZ = points.get(0).getZ();

for (BlockVector2 v : points) {
int x = v.getX();
int z = v.getZ();
if (x < minX) {
minX = x;
}
if (z < minZ) {
minZ = z;
}
if (x > maxX) {
maxX = x;
}
if (z > maxZ) {
maxZ = z;
}
}

min = BlockVector2.at(minX, minZ);
max = BlockVector2.at(maxX, maxZ);

Vector3 center = min.add(max).toVector3().divide(2);

for (BlockVector2 point : points) {
BlockVector2 shiftedPoint = BlockVector2.at(point.getX() - center.getX(), point.getZ() - center.getZ());
if (shiftedPoint.distanceSq(playerPos2D) < 50 * 50)
if (!ParticleAPIEnabled)
player.spawnParticle(Particle.FLAME, shiftedPoint.getX(), player.getLocation().getY() + 1, shiftedPoint.getZ(), 1, 0.0, 0.0, 0.0, 0);
else {
Location loc = new Location(player.getWorld(), shiftedPoint.getX(), player.getLocation().getY() + 1, shiftedPoint.getZ());
// create a particle packet
Object packet = particles.FLAME().packet(true, loc);

// send this packet to player
particles.sendPacket(player, packet);
}
}
}
}
} catch (SQLException | IOException ex) {
Expand Down Expand Up @@ -715,5 +771,12 @@ public static void sendUnreviewedPlotsReminderMessage(List<Plot> plots, Player p
tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(LangUtil.getInstance().get(player, LangPaths.MenuTitle.SHOW_PLOTS))));
player.spigot().sendMessage(tc);
}

public static BlockVector3 getCoordinateNormalised(int x, int y, int z) {

BlockVector3 normalised = BlockVector3.at(x, y, z).normalize();

return BlockVector3.at(normalised.getX(), y, normalised.getZ());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,14 @@ public int getPlotHeightCentered() throws IOException {
public int getWorldHeight() throws IOException {
Clipboard clipboard = FaweAPI.load(getPlot().getOutlinesSchematic());
int plotHeight = clipboard != null ? clipboard.getMinimumPoint().getBlockY() : MIN_WORLD_HEIGHT;
int heightThreshold = 50; // Additional height the plot use to save as schematic need to be included as a threshold

// Plots created below min world height are not supported
if (plotHeight < MIN_WORLD_HEIGHT) throw new IOException("Plot height is not supported");
if (plotHeight + heightThreshold < MIN_WORLD_HEIGHT) throw new IOException("Plot height is not supported");

// Move Y height to a usable value below 256 blocks
while (plotHeight >= 150) {
plotHeight -= 150;
while (plotHeight >= (heightThreshold + 100)) {
plotHeight -= (heightThreshold + 100);
}
return plotHeight;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.Polygonal2DRegion;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
Expand All @@ -50,13 +51,14 @@
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;

public class PlotWorld implements IWorld {
public static final int PLOT_SIZE = 150;
public static final int MAX_WORLD_HEIGHT = 256;
public static final int MIN_WORLD_HEIGHT = -69;
public static final int MIN_WORLD_HEIGHT = 5;

private final MultiverseCore mvCore = PlotSystem.DependencyManager.getMultiverseCore();
private final String worldName;
Expand Down Expand Up @@ -127,7 +129,7 @@ public boolean unloadWorld(boolean movePlayers) {
@Override
public boolean teleportPlayer(@NotNull Player player) {
if (loadWorld() && plot != null) {
player.teleport(getSpawnPoint(plot instanceof TutorialPlot ? null : plot.getCenter()));
player.teleport(getSpawnPoint(plot instanceof TutorialPlot ? null : BlockVector3.at(0, plot.getCenter().getY(), 0)));
return true;
} else Bukkit.getLogger().log(Level.WARNING, "Could not teleport player " + player.getName() + " to world " + worldName + "!");
return false;
Expand Down
Loading

0 comments on commit 81b86c1

Please sign in to comment.