use of org.spongepowered.math.vector.Vector3i in project SpongeCommon by SpongePowered.
the class ObjectArrayMutableEntityBuffer method blockStateStream.
@Override
public VolumeStream<EntityVolume.Mutable, BlockState> blockStateStream(final Vector3i min, final Vector3i max, final StreamOptions options) {
VolumeStreamUtils.validateStreamArgs(min, max, this.min(), this.max(), options);
final ArrayMutableBlockBuffer buffer;
if (options.carbonCopy()) {
buffer = this.blockBuffer.copy();
} else {
buffer = this.blockBuffer;
}
final Stream<VolumeElement<EntityVolume.Mutable, BlockState>> stateStream = IntStream.range(min.x(), max.x() + 1).mapToObj(x -> IntStream.range(min.z(), max.z() + 1).mapToObj(z -> IntStream.range(min.y(), max.y() + 1).mapToObj(y -> VolumeElement.of((EntityVolume.Mutable) this, () -> buffer.block(x, y, z), new Vector3d(x, y, z)))).flatMap(Function.identity())).flatMap(Function.identity());
return new SpongeVolumeStream<>(stateStream, () -> this);
}
use of org.spongepowered.math.vector.Vector3i in project SpongeCommon by SpongePowered.
the class VolumeStreamTest method onGamePreInitialization.
@Listener
public void onGamePreInitialization(final RegisterCommandEvent<Command.Parameterized> event) throws IOException, CommandException {
this.schematicsDir = this.config.resolve("schematics");
Files.createDirectories(this.config);
final Parameter.Value<Biome> biomeKey = Parameter.registryElement(TypeToken.get(Biome.class), ImmutableList.of(VariableValueParameters.RegistryEntryBuilder.WORLD_FROM_LOCATABLE_HOLDER_PROVIDER, VariableValueParameters.RegistryEntryBuilder.WORLD_FROM_CAUSE_HOLDER_PROVIDER), RegistryTypes.BIOME, "minecraft").key("format").build();
event.register(this.plugin, Command.builder().shortDescription(Component.text("Sets the biome in a selected region")).permission(this.plugin.metadata().id() + ".command.setbiome").addParameter(biomeKey).executor(src -> {
if (!(src.cause().root() instanceof ServerPlayer)) {
src.sendMessage(Identity.nil(), Component.text("Player only.", NamedTextColor.RED));
return CommandResult.success();
}
final ServerPlayer player = (ServerPlayer) src.cause().root();
final PlayerData data = VolumeStreamTest.get(player);
if (data.getPos1() == null || data.getPos2() == null) {
player.sendMessage(Identity.nil(), Component.text("You must set both positions before copying", NamedTextColor.RED));
return CommandResult.success();
}
final Vector3i min = data.getPos1().min(data.getPos2());
final Vector3i max = data.getPos1().max(data.getPos2());
final Biome target = src.requireOne(biomeKey);
player.world().biomeStream(min, max, StreamOptions.forceLoadedAndCopied()).map((world, biome, x, y, z) -> target).apply(VolumeCollectors.of(player.world(), VolumePositionTranslators.identity(), VolumeApplicators.applyBiomes()));
return CommandResult.success();
}).build(), "setBiome");
event.register(this.plugin, Command.builder().shortDescription(Component.text("Copies a region of the world to your clipboard")).permission(this.plugin.metadata().id() + ".command.copy").executor(src -> {
if (!(src.cause().root() instanceof Player)) {
src.sendMessage(Identity.nil(), Component.text("Player only.", NamedTextColor.RED));
return CommandResult.success();
}
final Player player = (Player) src.cause().root();
final PlayerData data = VolumeStreamTest.get(player);
if (data.getPos1() == null || data.getPos2() == null) {
player.sendMessage(Identity.nil(), Component.text("You must set both positions before copying", NamedTextColor.RED));
return CommandResult.success();
}
final Vector3i min = data.getPos1().min(data.getPos2());
final Vector3i max = data.getPos1().max(data.getPos2());
data.setOrigin(player.blockPosition());
final ArchetypeVolume archetypeVolume = player.world().createArchetypeVolume(min, max, player.blockPosition());
data.setClipboard(archetypeVolume);
player.sendMessage(Identity.nil(), Component.text("Saved to clipboard.", VolumeStreamTest.GREEN));
return CommandResult.success();
}).build(), "copy");
event.register(this.plugin, Command.builder().shortDescription(Component.text("Pastes your clipboard to where you are standing")).permission(this.plugin.metadata().id() + ".command.paste").executor(src -> {
if (!(src.cause().root() instanceof ServerPlayer)) {
src.sendMessage(Identity.nil(), Component.text("Player only.", NamedTextColor.RED));
return CommandResult.success();
}
final ServerPlayer player = (ServerPlayer) src.cause().root();
final PlayerData data = VolumeStreamTest.get(player);
final ArchetypeVolume volume = data.getClipboard();
if (volume == null) {
player.sendMessage(Identity.nil(), Component.text("You must copy something before pasting", NamedTextColor.RED));
return CommandResult.success();
}
try (final CauseStackManager.StackFrame frame = Sponge.server().causeStackManager().pushCauseFrame()) {
frame.pushCause(this.plugin);
volume.applyToWorld(player.world(), player.blockPosition(), SpawnTypes.PLACEMENT::get);
}
src.sendMessage(Identity.nil(), Component.text("Pasted clipboard into world.", VolumeStreamTest.GREEN));
return CommandResult.success();
}).build(), "paste");
final Parameter.Value<String> fileName = Parameter.string().key("fileName").build();
event.register(this.plugin, Command.builder().shortDescription(Component.text("Pastes your clipboard to where you are standing")).permission(this.plugin.metadata().id() + ".command.paste").addParameter(fileName).executor(src -> {
if (!(src.cause().root() instanceof ServerPlayer)) {
src.sendMessage(Identity.nil(), Component.text("Player only.", NamedTextColor.RED));
return CommandResult.success();
}
final String file = src.requireOne(fileName);
final Path desiredFilePath = this.schematicsDir.resolve(file + VolumeStreamTest.FILE_ENDING);
if (Files.exists(desiredFilePath)) {
throw new CommandException(Component.text(file + " already exists, please delete the file first", NamedTextColor.RED));
}
if (Files.isDirectory(desiredFilePath)) {
throw new CommandException(Component.text(file + "is a directory, please use a file name", NamedTextColor.RED));
}
final ServerPlayer player = (ServerPlayer) src.cause().root();
final PlayerData data = VolumeStreamTest.get(player);
final ArchetypeVolume volume = data.getClipboard();
if (volume == null) {
player.sendMessage(Identity.nil(), Component.text("You must copy something before pasting", NamedTextColor.RED));
return CommandResult.success();
}
final Schematic schematic = Schematic.builder().volume(data.getClipboard()).metaValue(Schematic.METADATA_AUTHOR, player.name()).metaValue(Schematic.METADATA_NAME, file).build();
final DataContainer schematicData = Sponge.dataManager().translator(Schematic.class).orElseThrow(() -> new IllegalStateException("Sponge doesn't have a DataTranslator for Schematics!")).translate(schematic);
try {
final Path output = Files.createFile(desiredFilePath);
DataFormats.NBT.get().writeTo(new GZIPOutputStream(Files.newOutputStream(output)), schematicData);
player.sendMessage(Identity.nil(), Component.text("Saved schematic to " + output.toAbsolutePath(), VolumeStreamTest.SAVE));
} catch (final Exception e) {
e.printStackTrace();
final StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
final Component errorText = Component.text(writer.toString().replace("\t", " ").replace("\r\n", "\n").replace("\r", "\n"));
final TextComponent text = Component.text("Error saving schematic: " + e.getMessage(), NamedTextColor.RED).hoverEvent(HoverEvent.showText(errorText));
return CommandResult.builder().error(text).build();
}
return CommandResult.success();
}).build(), "save");
event.register(this.plugin, Command.builder().shortDescription(Component.text("Load a schematic from file")).permission(this.plugin.metadata().id() + ".command.load").addParameter(fileName).executor(src -> {
if (!(src.cause().root() instanceof ServerPlayer)) {
src.sendMessage(Identity.nil(), Component.text("Player only.", NamedTextColor.RED));
return CommandResult.success();
}
final ServerPlayer player = (ServerPlayer) src.cause().root();
final String file = src.requireOne(fileName);
final Path desiredFilePath = this.schematicsDir.resolve(file + VolumeStreamTest.FILE_ENDING);
if (!Files.isRegularFile(desiredFilePath)) {
throw new CommandException(Component.text("File " + file + " was not a normal schemaic file"));
}
final Schematic schematic;
final DataContainer schematicContainer;
try (final GZIPInputStream stream = new GZIPInputStream(Files.newInputStream(desiredFilePath))) {
schematicContainer = DataFormats.NBT.get().readFrom(stream);
} catch (IOException e) {
e.printStackTrace();
final StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
final Component errorText = Component.text(writer.toString().replace("\t", " ").replace("\r\n", "\n").replace("\r", "\n"));
final TextComponent text = Component.text("Error loading schematic: " + e.getMessage(), NamedTextColor.RED).hoverEvent(HoverEvent.showText(errorText));
return CommandResult.builder().error(text).build();
}
schematic = Sponge.dataManager().translator(Schematic.class).orElseThrow(() -> new IllegalStateException("Expected a DataTranslator for a Schematic")).translate(schematicContainer);
src.sendMessage(Identity.nil(), Component.text("Loaded schematic from " + file, TextColor.color(0x003434)));
final PlayerData data = VolumeStreamTest.get(player);
data.setClipboard(schematic);
data.setOrigin(player.blockPosition());
return CommandResult.success();
}).build(), "load");
final Parameter.Value<Rotation> rotation = Parameter.registryElement(TypeToken.get(Rotation.class), RegistryTypes.ROTATION).key("rotation").build();
event.register(this.plugin, Command.builder().shortDescription(Component.text("Rotate clipboard")).permission(this.plugin.metadata().id() + ".command.rotate").addParameter(rotation).executor(src -> {
if (!(src.cause().root() instanceof ServerPlayer)) {
src.sendMessage(Identity.nil(), Component.text("Player only.", NamedTextColor.RED));
return CommandResult.success();
}
final ServerPlayer player = (ServerPlayer) src.cause().root();
final Rotation desiredRotation = src.requireOne(rotation);
final Schematic schematic;
final PlayerData data = VolumeStreamTest.get(player);
if (data.clipboard == null) {
throw new CommandException(Component.text("Load a clipboard first before trying to rotate it"));
}
final ArchetypeVolume newClipboard = data.clipboard.transform(Transformation.builder().origin(data.clipboard.min().toDouble().add(data.clipboard.size().toDouble().div(2))).rotate(desiredRotation).build());
src.sendMessage(Identity.nil(), Component.text("Rotated clipboard " + desiredRotation.angle().degrees() + " degrees"));
data.setClipboard(newClipboard);
return CommandResult.success();
}).build(), "rotate");
}
use of org.spongepowered.math.vector.Vector3i in project SpongeCommon by SpongePowered.
the class BlockEventBasedTransaction method markCancelledTransactions.
@Override
public final boolean markCancelledTransactions(final ChangeBlockEvent.All event, final ImmutableList<? extends GameTransaction<ChangeBlockEvent.All>> blockTransactions) {
boolean cancelledAny = false;
if (event.isCancelled()) {
event.transactions().forEach(BlockTransaction::invalidate);
}
for (final Transaction<BlockSnapshot> transaction : event.transactions()) {
if (!transaction.isValid()) {
cancelledAny = true;
for (final GameTransaction<ChangeBlockEvent.All> gameTransaction : blockTransactions) {
final BlockEventBasedTransaction blockTransaction = (BlockEventBasedTransaction) gameTransaction;
final Vector3i position = transaction.original().position();
final BlockPos affectedPosition = blockTransaction.affectedPosition;
if (position.x() == affectedPosition.getX() && position.y() == affectedPosition.getY() && position.z() == affectedPosition.getZ()) {
gameTransaction.markCancelled();
}
}
}
}
return cancelledAny;
}
use of org.spongepowered.math.vector.Vector3i in project SpongeCommon by SpongePowered.
the class NeighborNotification method markCancelledTransactions.
@Override
public boolean markCancelledTransactions(final NotifyNeighborBlockEvent event, final ImmutableList<? extends GameTransaction<NotifyNeighborBlockEvent>> blockTransactions) {
boolean cancelledAny = false;
for (final NotificationTicket transaction : event.tickets()) {
if (!transaction.valid()) {
cancelledAny = true;
for (final GameTransaction<NotifyNeighborBlockEvent> gameTransaction : blockTransactions) {
final NeighborNotification blockTransaction = (NeighborNotification) gameTransaction;
final Vector3i position = transaction.targetPosition();
final BlockPos affectedPosition = blockTransaction.affectedPosition;
if (position.x() == affectedPosition.getX() && position.y() == affectedPosition.getY() && position.z() == affectedPosition.getZ()) {
gameTransaction.markCancelled();
}
}
}
}
return cancelledAny;
}
use of org.spongepowered.math.vector.Vector3i in project SpongeCommon by SpongePowered.
the class AbstractSpongeRayTrace method execute.
@Override
@NonNull
public Optional<RayTraceResult<@NonNull T>> execute() {
this.setupEnd();
// get the direction
final Vector3d directionWithLength = this.end.sub(this.start);
final double length = directionWithLength.length();
final Vector3d direction = directionWithLength.normalize();
if (direction.lengthSquared() == 0) {
throw new IllegalStateException("The start and end must be two different vectors");
}
final ServerWorld serverWorld = Sponge.server().worldManager().world(this.world).orElseThrow(() -> new IllegalStateException("World with key " + this.world.formatted() + " is not loaded!"));
Vector3i currentBlock = this.initialBlock(direction);
final Vector3i steps = this.createSteps(direction);
// The ray equation is, vec(u) + t vec(d). From a point (x, y), there is a t
// that we need to traverse to get to a boundary. We work that out now...
TData tData = this.createInitialTData(direction);
Vector3d currentLocation = new Vector3d(this.start.x(), this.start.y(), this.start.z());
final boolean requiresEntityTracking = this.requiresEntityTracking();
boolean requireAdvancement = true;
while (requireAdvancement) {
final net.minecraft.world.phys.Vec3 vec3dstart = VecHelper.toVanillaVector3d(currentLocation);
// As this iteration is for the CURRENT block location, we need to check where we are with the filter.
if (this.continueWhileLocation != null && !this.continueWhileLocation.test(ServerLocation.of(serverWorld, currentBlock))) {
return Optional.empty();
}
final Vector3d nextLocation;
final net.minecraft.world.phys.Vec3 vec3dend;
if (tData.getTotalTWithNextStep() > length) {
// This is the last step, we break out of the loop after this set of checks.
requireAdvancement = false;
nextLocation = this.end;
vec3dend = VecHelper.toVanillaVector3d(this.end);
} else {
nextLocation = currentLocation.add(direction.x() * tData.getNextStep(), direction.y() * tData.getNextStep(), direction.z() * tData.getNextStep());
vec3dend = VecHelper.toVanillaVector3d(nextLocation);
}
// Get the selection result.
final Optional<RayTraceResult<@NonNull T>> result = this.testSelectLocation(serverWorld, vec3dstart, vec3dend);
if (result.isPresent() && !this.shouldCheckFailures()) {
// that's blocking the view.
return result;
}
// Ensure that the block can be travelled through.
if (!this.shouldAdvanceThroughBlock(serverWorld, vec3dstart, vec3dend)) {
return Optional.empty();
}
// Ensure that the entities in the block can be travelled through.
if (requiresEntityTracking && this.continueWhileEntity != null) {
final double resultDistance;
if (result.isPresent()) {
resultDistance = result.get().hitPosition().distanceSquared(currentLocation);
} else {
resultDistance = Double.MAX_VALUE;
}
final AABB targetAABB = this.getBlockAABB(currentBlock);
for (final net.minecraft.world.entity.Entity entity : this.getFailingEntities(serverWorld, targetAABB)) {
final Optional<net.minecraft.world.phys.Vec3> vec3d = entity.getBoundingBox().clip(vec3dstart, vec3dend);
if (vec3d.isPresent()) {
final net.minecraft.world.phys.Vec3 hitPosition = vec3d.get();
final double sqdist = hitPosition.distanceToSqr(vec3dstart);
if (sqdist < resultDistance) {
// We have a failure, so at this point we just bail out and end the trace.
return Optional.empty();
}
}
}
}
// If we still have a result at this point, return it.
if (result.isPresent()) {
return result;
}
if (requireAdvancement) {
currentLocation = nextLocation;
currentBlock = this.getNextBlock(currentBlock, tData, steps);
tData = this.advance(tData, steps, direction);
}
}
return Optional.empty();
}
Aggregations