Search in sources :

Example 11 with MapInfo

use of org.spongepowered.api.map.MapInfo in project SpongeCommon by SpongePowered.

the class SpongeMapStorage method allMapInfos.

@Override
public Collection<MapInfo> allMapInfos() {
    final Set<MapInfo> mapInfos = new HashSet<>();
    final ServerLevel defaultWorld = (ServerLevel) Sponge.server().worldManager().defaultWorld();
    final int highestId = ((MapIdTrackerBridge) defaultWorld.getDataStorage().computeIfAbsent(MapIndex::new, Constants.Map.MAP_INDEX_DATA_NAME)).bridge$getHighestMapId().orElse(-1);
    for (int i = 0; i <= highestId; i++) {
        @Nullable final MapInfo mapInfo = (MapInfo) defaultWorld.getMapData(Constants.Map.MAP_PREFIX + i);
        if (mapInfo == null) {
            SpongeCommon.logger().warn("Missing map with id: " + i);
            continue;
        }
        this.addMapInfo(mapInfo);
        mapInfos.add(mapInfo);
    }
    return mapInfos;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) MapInfo(org.spongepowered.api.map.MapInfo) MapIndex(net.minecraft.world.level.saveddata.maps.MapIndex) Nullable(org.checkerframework.checker.nullness.qual.Nullable) HashSet(java.util.HashSet)

Example 12 with MapInfo

use of org.spongepowered.api.map.MapInfo in project SpongeCommon by SpongePowered.

the class MapTest method recenterMap.

private CommandResult recenterMap(final CommandContext ctx) throws CommandException {
    final Player player = this.requirePlayer(ctx);
    final ItemStack heldMap = player.itemInHand(HandTypes.MAIN_HAND);
    if (heldMap.type() != ItemTypes.FILLED_MAP.get()) {
        throw new CommandException(Component.text("You must hold a map in your hand"));
    }
    final MapInfo mapInfo = heldMap.require(Keys.MAP_INFO);
    mapInfo.offer(Keys.MAP_LOCATION, player.location().blockPosition().toVector2(true));
    player.sendMessage(Component.text("New center " + mapInfo.require(Keys.MAP_LOCATION)));
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MapInfo(org.spongepowered.api.map.MapInfo) CommandException(org.spongepowered.api.command.exception.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 13 with MapInfo

use of org.spongepowered.api.map.MapInfo in project SpongeCommon by SpongePowered.

the class MapTest method getMapFromUUID.

private CommandResult getMapFromUUID(final Parameter.Value<UUID> uuidParameter, final CommandContext ctx) throws CommandException {
    final Player player = this.requirePlayer(ctx);
    final UUID uuid = ctx.one(uuidParameter).get();
    final ItemStack itemStack = ItemStack.of(ItemTypes.FILLED_MAP, 1);
    final MapInfo mapInfo = Sponge.server().mapStorage().mapInfo(uuid).orElseThrow(() -> new CommandException(Component.text("UUID " + uuid + " was not a valid map uuid!")));
    itemStack.offer(Keys.MAP_INFO, mapInfo);
    player.inventory().offer(itemStack);
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MapInfo(org.spongepowered.api.map.MapInfo) CommandException(org.spongepowered.api.command.exception.CommandException) UUID(java.util.UUID) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 14 with MapInfo

use of org.spongepowered.api.map.MapInfo in project SpongeCommon by SpongePowered.

the class MapTest method setColorAndLocked.

private CommandResult setColorAndLocked(final CommandContext ctx) throws CommandException {
    final Player player = this.requirePlayer(ctx);
    final ItemStack map = player.itemInHand(HandTypes.MAIN_HAND);
    if (map.type() != ItemTypes.FILLED_MAP.get()) {
        throw new CommandException(Component.text("You must hold a map in your hand"));
    }
    // map.offer(Keys.MAP_LOCATION, new Vector2i(10000,10000));
    final MapColor color = MapColor.of(MapColorTypes.TERRACOTTA_BLACK);
    final MapInfo mapInfo = map.require(Keys.MAP_INFO);
    mapInfo.offer(Keys.MAP_LOCKED, true);
    mapInfo.offer(Keys.MAP_CANVAS, MapCanvas.builder().paintAll(color).build());
    player.sendMessage(Component.text(mapInfo.require(Keys.MAP_CANVAS).color(0, 0).toContainer().toString()));
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MapInfo(org.spongepowered.api.map.MapInfo) CommandException(org.spongepowered.api.command.exception.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack) MapColor(org.spongepowered.api.map.color.MapColor)

Example 15 with MapInfo

use of org.spongepowered.api.map.MapInfo in project SpongeCommon by SpongePowered.

the class MapTest method testMapSerialization.

private CommandResult testMapSerialization(final CommandContext ctx) {
    MapInfo mapInfo = null;
    final Audience audience = ctx.cause().audience();
    if (audience instanceof Player) {
        final Player player = (Player) audience;
        mapInfo = player.itemInHand(HandTypes.MAIN_HAND).get(Keys.MAP_INFO).orElse(null);
    }
    final MapStorage mapStorage = Sponge.server().mapStorage();
    if (mapInfo == null) {
        mapInfo = mapStorage.allMapInfos().stream().findAny().orElse(mapStorage.createNewMapInfo().get());
    }
    final DataView mapInfoView = blankMapDecorationIds(mapInfo.toContainer());
    mapInfoView.set(DataQuery.of("MapData", "MapLocked"), true).set(DataQuery.of("UnsafeMapId"), 10);
    return CommandResult.success();
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) Player(org.spongepowered.api.entity.living.player.Player) Audience(net.kyori.adventure.audience.Audience) MapInfo(org.spongepowered.api.map.MapInfo) MapStorage(org.spongepowered.api.map.MapStorage)

Aggregations

MapInfo (org.spongepowered.api.map.MapInfo)17 Player (org.spongepowered.api.entity.living.player.Player)13 CommandException (org.spongepowered.api.command.exception.CommandException)11 ItemStack (org.spongepowered.api.item.inventory.ItemStack)11 MapCanvas (org.spongepowered.api.map.MapCanvas)4 HashSet (java.util.HashSet)3 Audience (net.kyori.adventure.audience.Audience)3 ServerLevel (net.minecraft.server.level.ServerLevel)3 MapColor (org.spongepowered.api.map.color.MapColor)3 ServerWorld (org.spongepowered.api.world.server.ServerWorld)3 BufferedImage (java.awt.image.BufferedImage)2 File (java.io.File)2 IOException (java.io.IOException)2 UUID (java.util.UUID)2 CompoundTag (net.minecraft.nbt.CompoundTag)2 ItemStack (net.minecraft.world.item.ItemStack)2 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 Sponge (org.spongepowered.api.Sponge)2 DataView (org.spongepowered.api.data.persistence.DataView)2 MapStorage (org.spongepowered.api.map.MapStorage)2