Search in sources :

Example 6 with MapInfo

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

the class MapTest method addWorldBanner.

private CommandResult addWorldBanner(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);
    final RayTraceResult<LocatableBlock> hit = RayTrace.block().sourcePosition(player).direction(player).world(player.serverLocation().world()).continueWhileBlock(RayTrace.onlyAir()).limit(100).select(a -> a.location().blockEntity().filter(entity -> entity instanceof Banner).isPresent()).execute().orElseThrow(() -> new CommandException(Component.text("You must look at a banner")));
    mapInfo.addBannerDecoration(hit.selectedObject().serverLocation());
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Banner(org.spongepowered.api.block.entity.Banner) MapInfo(org.spongepowered.api.map.MapInfo) LocatableBlock(org.spongepowered.api.world.LocatableBlock) CommandException(org.spongepowered.api.command.exception.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 7 with MapInfo

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

the class MapTest method loadMapFromFile.

private CommandResult loadMapFromFile(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"));
    }
    final File file = new File("map.png");
    if (!file.isFile()) {
        try {
            if (!file.createNewFile()) {
                throw new IOException("failed to create new file :(");
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
    final BufferedImage image;
    try {
        image = ImageIO.read(file);
    } catch (final IOException e) {
        throw new CommandException(Component.text(e.getMessage()), e);
    }
    final MapCanvas canvas = MapCanvas.builder().fromImage(image).build();
    final MapInfo mapInfo = map.require(Keys.MAP_INFO);
    mapInfo.offer(Keys.MAP_TRACKS_PLAYERS, false);
    mapInfo.offer(Keys.MAP_DECORATIONS, Collections.emptySet());
    mapInfo.offer(Keys.MAP_LOCKED, true);
    mapInfo.offer(Keys.MAP_CANVAS, canvas);
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MapCanvas(org.spongepowered.api.map.MapCanvas) MapInfo(org.spongepowered.api.map.MapInfo) CommandException(org.spongepowered.api.command.exception.CommandException) IOException(java.io.IOException) ItemStack(org.spongepowered.api.item.inventory.ItemStack) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 8 with MapInfo

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

the class MapTest method randomDecorations.

private CommandResult randomDecorations(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"));
    }
    player.sendMessage(Component.text("Getting mapInfo"));
    final MapInfo mapInfo = heldMap.require(Keys.MAP_INFO);
    mapInfo.offer(Keys.MAP_TRACKS_PLAYERS, true);
    final Set<MapDecoration> decorations = new HashSet<>();
    int x = Byte.MIN_VALUE;
    int y = Byte.MIN_VALUE;
    final List<MapDecorationType> types = RegistryTypes.MAP_DECORATION_TYPE.get().stream().collect(Collectors.toList());
    final Collection<MapDecorationOrientation> orientations = Sponge.game().registry(RegistryTypes.MAP_DECORATION_ORIENTATION).stream().collect(Collectors.toList());
    player.sendMessage(Component.text("Number of orientations: " + orientations.size()));
    player.sendMessage(Component.text("EAST: " + MapDecorationOrientations.EAST.get().key(RegistryTypes.MAP_DECORATION_ORIENTATION).toString()));
    for (final MapDecorationOrientation dir : orientations) {
        decorations.add(MapDecoration.builder().type(types.get(player.random().nextInt(types.size()))).rotation(dir).position(Vector2i.from(x, y)).build());
        player.sendMessage(Component.text(dir.key(RegistryTypes.MAP_DECORATION_ORIENTATION).value()).append(Component.text("x: " + x)).append(Component.text("y: " + y)));
        x += 16;
        if (x > Byte.MAX_VALUE) {
            y += 16;
            x = Byte.MIN_VALUE;
            if (y > Byte.MAX_VALUE) {
                player.sendMessage(Component.text("out of room, stopping"));
                mapInfo.offer(Keys.MAP_DECORATIONS, decorations);
                return CommandResult.success();
            }
        }
    }
    mapInfo.offer(Keys.MAP_DECORATIONS, decorations);
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MapDecorationOrientation(org.spongepowered.api.map.decoration.orientation.MapDecorationOrientation) MapInfo(org.spongepowered.api.map.MapInfo) MapDecoration(org.spongepowered.api.map.decoration.MapDecoration) MapDecorationType(org.spongepowered.api.map.decoration.MapDecorationType) CommandException(org.spongepowered.api.command.exception.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack) HashSet(java.util.HashSet)

Example 9 with MapInfo

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

the class EmptyMapItemMixin method impl$createMapWithSpongeData.

@Redirect(method = "use", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/MapItem;create(Lnet/minecraft/world/level/Level;IIBZZ)Lnet/minecraft/world/item/ItemStack;"))
private ItemStack impl$createMapWithSpongeData(final Level level, final int x, final int y, final byte scale, final boolean trackingPosition, final boolean unlimitedTracking, final Level level2, final net.minecraft.world.entity.player.Player playerIn, final InteractionHand usedHand) {
    if (level.isClientSide()) {
        // ignore on client
        return MapItem.create(level, x, y, scale, trackingPosition, unlimitedTracking);
    }
    final Player player = (Player) playerIn;
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        frame.addContext(EventContextKeys.PLAYER, player);
        final HandType handType = (HandType) (Object) usedHand;
        frame.addContext(EventContextKeys.USED_HAND, handType);
        frame.addContext(EventContextKeys.USED_ITEM, player.itemInHand(handType).createSnapshot());
        final Set<Value<?>> mapValues = Sets.newHashSet(Value.immutableOf(Keys.MAP_LOCATION, Vector2i.from((int) playerIn.getX(), (int) playerIn.getZ())), Value.immutableOf(Keys.MAP_WORLD, ((ServerWorld) level).key()), Value.immutableOf(Keys.MAP_TRACKS_PLAYERS, trackingPosition), Value.immutableOf(Keys.MAP_UNLIMITED_TRACKING, unlimitedTracking), Value.immutableOf(Keys.MAP_SCALE, (int) scale));
        final Optional<MapInfo> optMapInfo = SpongeCommonEventFactory.fireCreateMapEvent(frame.currentCause(), mapValues);
        if (!optMapInfo.isPresent()) {
            return ItemStack.EMPTY;
        }
        final int id = ((MapItemSavedDataBridge) optMapInfo.get()).bridge$getMapId();
        final ItemStack newMap = new ItemStack(Items.FILLED_MAP, 1);
        final CompoundTag nbt = newMap.getOrCreateTag();
        nbt.putInt(Constants.Map.MAP_ID, id);
        return newMap;
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) HandType(org.spongepowered.api.data.type.HandType) ServerWorld(org.spongepowered.api.world.server.ServerWorld) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Value(org.spongepowered.api.data.value.Value) MapInfo(org.spongepowered.api.map.MapInfo) ItemStack(net.minecraft.world.item.ItemStack) MapItemSavedDataBridge(org.spongepowered.common.bridge.world.storage.MapItemSavedDataBridge) CompoundTag(net.minecraft.nbt.CompoundTag) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 10 with MapInfo

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

the class SpongeMapStorage method mapInfo.

@Override
public Optional<MapInfo> mapInfo(final UUID uuid) {
    this.ensureHasMapUUIDIndex();
    final MapInfo mapInfo = this.loadedMapUUIDs.get(uuid);
    if (mapInfo != null) {
        return Optional.of(mapInfo);
    }
    final Integer mapId = this.mapIdUUIDIndex.inverse().get(uuid);
    if (mapId == null) {
        return Optional.empty();
    }
    final ServerLevel defaultWorld = (ServerLevel) Sponge.server().worldManager().defaultWorld();
    final MapInfo loadedMapInfo = (MapInfo) defaultWorld.getMapData(Constants.Map.MAP_PREFIX + mapId);
    return Optional.ofNullable(loadedMapInfo);
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) MapInfo(org.spongepowered.api.map.MapInfo)

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