use of org.spongepowered.api.data.persistence.DataContainer in project SpongeCommon by SpongePowered.
the class EntityMixin_API method toContainer.
@Override
public DataContainer toContainer() {
final CompoundTag compound = new CompoundTag();
this.shadow$saveAsPassenger(compound);
final DataContainer unsafeNbt = NBTTranslator.INSTANCE.translateFrom(compound);
final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, this.contentVersion()).set(Constants.Entity.CLASS, this.getClass().getName()).set(Queries.WORLD_KEY, ((org.spongepowered.api.world.server.ServerWorld) this.world()).key().formatted()).createView(Constants.Sponge.SNAPSHOT_WORLD_POSITION).set(Queries.POSITION_X, this.position().x()).set(Queries.POSITION_Y, this.position().y()).set(Queries.POSITION_Z, this.position().z()).container().createView(Constants.Entity.ROTATION).set(Queries.POSITION_X, this.rotation().x()).set(Queries.POSITION_Y, this.rotation().y()).set(Queries.POSITION_Z, this.rotation().z()).container().createView(Constants.Entity.SCALE).set(Queries.POSITION_X, this.scale().x()).set(Queries.POSITION_Y, this.scale().y()).set(Queries.POSITION_Z, this.scale().z()).container().set(Constants.Entity.TYPE, Registry.ENTITY_TYPE.getKey((net.minecraft.world.entity.EntityType<?>) this.type())).set(Constants.Sponge.UNSAFE_NBT, unsafeNbt);
return container;
}
use of org.spongepowered.api.data.persistence.DataContainer 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.api.data.persistence.DataContainer in project SpongeCommon by SpongePowered.
the class BlockEntityMixin_API method toContainer.
@Override
public DataContainer toContainer() {
final ResourceKey key = (ResourceKey) (Object) Registry.BLOCK_ENTITY_TYPE.getKey(this.type);
final DataContainer container = DataContainer.createNew().set(Queries.CONTENT_VERSION, this.contentVersion()).set(Queries.WORLD_KEY, ((ServerWorld) this.level).key()).set(Queries.POSITION_X, this.shadow$getBlockPos().getX()).set(Queries.POSITION_Y, this.shadow$getBlockPos().getY()).set(Queries.POSITION_Z, this.shadow$getBlockPos().getZ()).set(Constants.TileEntity.TILE_TYPE, key);
final CompoundTag compound = new CompoundTag();
this.shadow$save(compound);
// We must filter the custom data so it isn't stored twice
Constants.NBT.filterSpongeCustomData(compound);
container.set(Constants.Sponge.UNSAFE_NBT, NBTTranslator.INSTANCE.translateFrom(compound));
return container;
}
use of org.spongepowered.api.data.persistence.DataContainer in project SpongeCommon by SpongePowered.
the class EnchantmentTableBlockEntityMixin_API method toContainer.
// @formatter:on
@Override
public DataContainer toContainer() {
DataContainer container = super.toContainer();
container.set(Constants.TileEntity.CUSTOM_NAME, this.name.getString());
return container;
}
use of org.spongepowered.api.data.persistence.DataContainer in project SpongeCommon by SpongePowered.
the class BeaconBlockEntityMixin_API method toContainer.
@Override
public DataContainer toContainer() {
DataContainer container = super.toContainer();
container.set(Constants.TileEntity.Beacon.PRIMARY, MobEffect.getId(this.primaryPower));
container.set(Constants.TileEntity.Beacon.SECONDARY, MobEffect.getId(this.secondaryPower));
return container;
}
Aggregations