use of com.sk89q.worldedit.math.Vector3 in project RedProtect by FabioZumbi12.
the class WEHook method pasteWithWE.
public static Region pasteWithWE(Player p, File file) {
World world = p.getWorld();
Location loc = p.getLocation();
final Region[] r = { null };
if (!p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType().isSolid()) {
RedProtect.get().getLanguageManager().sendMessage(p, "playerlistener.region.needground");
return null;
}
Clipboard clipboard = null;
ClipboardFormat format = ClipboardFormats.findByFile(file);
if (format == null) {
RedProtect.get().getLanguageManager().sendMessage(p, "playerlistener.region.copyfail");
return null;
}
try (ClipboardReader reader = format.getReader(new FileInputStream(file))) {
clipboard = reader.read();
} catch (IOException e) {
CoreUtil.printJarVersion();
e.printStackTrace();
}
if (clipboard != null) {
try (EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(world), -1)) {
BlockVector3 to = BlockVector3.at(loc.getX(), loc.getY(), loc.getZ());
// Rotate to player looking direction
float yaw = loc.getYaw();
if (yaw < 0) {
yaw += 360;
}
int rotate = 0;
if (yaw >= 315 || yaw < 45) {
rotate = 180;
} else if (yaw < 135) {
rotate = 270;
} else if (yaw < 225) {
rotate = 0;
} else if (yaw < 315) {
rotate = 90;
}
ClipboardHolder holder = new ClipboardHolder(clipboard);
AffineTransform transform = new AffineTransform();
transform = transform.rotateY(-rotate);
holder.setTransform(holder.getTransform().combine(transform));
Operation operation = holder.createPaste(editSession).to(to).ignoreAirBlocks(false).build();
Operations.complete(operation);
// Select the region min and max
BlockVector3 clipboardOffset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin());
Vector3 realTo = to.toVector3().add(holder.getTransform().apply(clipboardOffset.toVector3()));
Vector3 locMax = realTo.add(holder.getTransform().apply(clipboard.getRegion().getMaximumPoint().subtract(clipboard.getRegion().getMinimumPoint()).toVector3()));
Location min = new Location(world, realTo.getX(), realTo.getY(), realTo.getZ());
Location max = new Location(world, locMax.getX(), locMax.getY(), locMax.getZ());
if (RedProtect.get().getConfigManager().configRoot().region_settings.autoexpandvert_ondefine) {
min.setY(p.getWorld().getMinHeight());
max.setY(p.getWorld().getMaxHeight());
if (RedProtect.get().getConfigManager().configRoot().region_settings.claim.miny != -1)
min.setY(RedProtect.get().getConfigManager().configRoot().region_settings.claim.miny);
if (RedProtect.get().getConfigManager().configRoot().region_settings.claim.maxy != -1)
max.setY(RedProtect.get().getConfigManager().configRoot().region_settings.claim.maxy);
}
RedProtect.get().getLanguageManager().sendMessage(p, "regionbuilder.creating");
// Run claim async
Bukkit.getScheduler().runTaskAsynchronously(RedProtect.get(), () -> {
RegionBuilder rb2 = new DefineRegionBuilder(p, min, max, "", new PlayerRegion(p.getUniqueId().toString(), p.getName()), new HashSet<>(), false);
if (rb2.ready()) {
r[0] = rb2.build();
}
});
} catch (WorldEditException e) {
CoreUtil.printJarVersion();
e.printStackTrace();
r[0] = null;
}
}
return r[0];
}
Aggregations