Skip to content

Commit

Permalink
Split out repair sign logic
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerS1066 committed Sep 15, 2024
1 parent 0d3cb11 commit 49c3eda
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.countercraft.movecraft.repair;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
Expand Down Expand Up @@ -208,7 +209,12 @@ public void onLeftClick(Sign sign, Player player, PlayerCraft craft) {
}
}

if (!WEUtils.saveCraftSchematic(craft, sign)) {
File repairDirectory = new File(MovecraftRepair.getInstance().getDataFolder(), "RepairStates");
File playerDirectory = new File(repairDirectory, craft.getPilot().getUniqueId().toString());
if (!playerDirectory.exists())
playerDirectory.mkdirs();
String repairName = ChatColor.stripColor(sign.getLine(1));
if (!WEUtils.saveCraftSchematic(playerDirectory, repairName, craft.getWorld(), craft.getHitBox(), sign.getLocation())) {
player.sendMessage(I18nSupport.getInternationalisedComponent("Repair - Could not save file"));
return;
}
Expand Down
31 changes: 14 additions & 17 deletions src/main/java/net/countercraft/movecraft/repair/util/WEUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Sign;
Expand Down Expand Up @@ -131,20 +132,17 @@ public static boolean saveSchematic(File directory, String name, Clipboard clipb
/**
* Save a schematic from a craft
*
* @param craft The craft to save
* @param directory Directory to save in
* @param name Name to save as
* @param world World to save from
* @param hitbox Hitbox to save from
* @param origin Origin point to save from
* @return true on success
*/
public static boolean saveCraftSchematic(@NotNull PilotedCraft craft, @NotNull Sign sign) {
File repairDirectory = new File(MovecraftRepair.getInstance().getDataFolder(), "RepairStates");
File playerDirectory = new File(repairDirectory, craft.getPilot().getUniqueId().toString());
if (!playerDirectory.exists())
playerDirectory.mkdirs();
String repairName = ChatColor.stripColor(sign.getLine(1));

HitBox hitbox = craft.getHitBox();
public static boolean saveCraftSchematic(File directory, String name, World world, @NotNull HitBox hitbox, @NotNull Location origin) {
BlockVector3 minPos = BlockVector3.at(hitbox.getMinX(), hitbox.getMinY(), hitbox.getMinZ());
BlockVector3 maxPos = BlockVector3.at(hitbox.getMaxX(), hitbox.getMaxY(), hitbox.getMaxZ());
BlockVector3 origin = BlockVector3.at(sign.getX(), sign.getY(), sign.getZ());
BlockVector3 weOrigin = BlockVector3.at(origin.getBlockX(), origin.getBlockY(), origin.getBlockZ());
CuboidRegion region = new CuboidRegion(minPos, maxPos);
// Calculate a hitbox of all blocks within the cuboid region but not within the
// hitbox (so we don't save them)
Expand All @@ -153,17 +151,16 @@ public static boolean saveCraftSchematic(@NotNull PilotedCraft craft, @NotNull S
new MovecraftLocation(hitbox.getMaxX(), hitbox.getMaxY(), hitbox.getMaxZ()));
surrounding = new BitmapHitBox(surrounding).difference(hitbox);

World bukkitWorld = craft.getWorld();
com.sk89q.worldedit.world.World world = new BukkitWorld(bukkitWorld);
com.sk89q.worldedit.world.World weWorld = new BukkitWorld(world);

Set<BaseBlock> blocks = getWorldEditBlocks(craft.getHitBox(), bukkitWorld);
Set<BaseBlock> blocks = getWorldEditBlocks(hitbox, world);

Clipboard clipboard;
try {
clipboard = new BlockArrayClipboard(region);
clipboard.setOrigin(origin);
EditSession source = WorldEdit.getInstance().newEditSession(world);
ForwardExtentCopy copy = new ForwardExtentCopy(source, region, origin, clipboard, origin);
clipboard.setOrigin(weOrigin);
EditSession source = WorldEdit.getInstance().newEditSession(weWorld);
ForwardExtentCopy copy = new ForwardExtentCopy(source, region, weOrigin, clipboard, weOrigin);
BlockMask mask = new BlockMask(source, blocks);
copy.setSourceMask(mask);
Operations.complete(copy);
Expand All @@ -178,7 +175,7 @@ public static boolean saveCraftSchematic(@NotNull PilotedCraft craft, @NotNull S
return false;
}

return saveSchematic(playerDirectory, repairName, clipboard);
return saveSchematic(directory, name, clipboard);
}

@NotNull
Expand Down

0 comments on commit 49c3eda

Please sign in to comment.