Search in sources :

Example 1 with MapInfo

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

the class MapInfoItemStackData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.MAP_INFO).supports(item -> item.getItem() instanceof MapItem).get(itemStack -> {
        if (itemStack.getTag() == null) {
            return null;
        }
        return (MapInfo) ((Level) Sponge.server().worldManager().defaultWorld()).getMapData(Constants.Map.MAP_PREFIX + itemStack.getTag().getInt(Constants.Map.MAP_ID));
    }).set((itemStack, mapInfo) -> {
        @Nullable CompoundTag nbt = itemStack.getTag();
        if (nbt == null) {
            nbt = new CompoundTag();
        }
        nbt.putInt(Constants.Map.MAP_ID, ((MapItemSavedDataBridge) mapInfo).bridge$getMapId());
        itemStack.setTag(nbt);
    });
}
Also used : Keys(org.spongepowered.api.data.Keys) CompoundTag(net.minecraft.nbt.CompoundTag) MapInfo(org.spongepowered.api.map.MapInfo) Constants(org.spongepowered.common.util.Constants) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) MapItem(net.minecraft.world.item.MapItem) Sponge(org.spongepowered.api.Sponge) ItemStack(net.minecraft.world.item.ItemStack) MapItemSavedDataBridge(org.spongepowered.common.bridge.world.storage.MapItemSavedDataBridge) Level(net.minecraft.world.level.Level) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Level(net.minecraft.world.level.Level) MapItem(net.minecraft.world.item.MapItem) Nullable(org.checkerframework.checker.nullness.qual.Nullable) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 2 with MapInfo

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

the class SpongeCommonEventFactory method fireCreateMapEvent.

public static Optional<MapInfo> fireCreateMapEvent(final Cause cause, final Set<Value<?>> values) {
    final ServerLevel defaultWorld = (ServerLevel) Sponge.server().worldManager().defaultWorld();
    final MapIdTrackerBridge mapIdTrackerBridge = (MapIdTrackerBridge) defaultWorld.getDataStorage().computeIfAbsent(MapIndex::new, Constants.Map.MAP_INDEX_DATA_NAME);
    final int id = mapIdTrackerBridge.bridge$getHighestMapId().orElse(-1) + 1;
    final String s = Constants.Map.MAP_PREFIX + id;
    final MapItemSavedData mapData = new MapItemSavedData(s);
    // Set default to prevent NPEs
    mapData.dimension = Level.OVERWORLD;
    final MapInfo mapInfo = (MapInfo) mapData;
    for (final Value<?> value : values) {
        mapInfo.offer(value);
    }
    final CreateMapEvent event = SpongeEventFactory.createCreateMapEvent(cause, mapInfo);
    SpongeCommon.post(event);
    if (event.isCancelled()) {
        return Optional.empty();
    }
    // Advance map id.
    final int mcId = defaultWorld.getFreeMapId();
    if (id != mcId) {
        // TODO: REMOVE OR replace for Integer.MAX_VALUE
        SpongeCommon.logger().warn("Map size corruption, vanilla only allows " + Integer.MAX_VALUE + "! " + "Expected next number was not equal to the true next number.");
        SpongeCommon.logger().warn("Expected: " + id + ". Got: " + mcId);
        SpongeCommon.logger().warn("Automatically cancelling map creation");
        mapIdTrackerBridge.bridge$setHighestMapId(id - 1);
        return Optional.empty();
    }
    defaultWorld.setMapData(mapData);
    ((SpongeMapStorage) Sponge.server().mapStorage()).addMapInfo(mapInfo);
    return Optional.of(mapInfo);
}
Also used : MapIdTrackerBridge(org.spongepowered.common.bridge.map.MapIdTrackerBridge) ServerLevel(net.minecraft.server.level.ServerLevel) MapItemSavedData(net.minecraft.world.level.saveddata.maps.MapItemSavedData) CreateMapEvent(org.spongepowered.api.event.action.CreateMapEvent) MapInfo(org.spongepowered.api.map.MapInfo) SpongeMapStorage(org.spongepowered.common.map.SpongeMapStorage)

Example 3 with MapInfo

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

the class MapTest method testMapShades.

private CommandResult testMapShades(final CommandContext ctx) throws CommandException {
    final Player player = this.requirePlayer(ctx);
    final Collection<RegistryEntry<MapShade>> mapShades = Sponge.game().registry(RegistryTypes.MAP_SHADE).streamEntries().collect(Collectors.toList());
    for (final RegistryEntry<MapShade> entry : mapShades) {
        final MapColor mapColor = MapColor.of(MapColorTypes.COLOR_GREEN.get(), entry.value());
        final MapCanvas mapCanvas = MapCanvas.builder().paintAll(mapColor).build();
        final MapInfo mapInfo = Sponge.server().mapStorage().createNewMapInfo().orElseThrow(() -> new CommandException(Component.text("Unable to create new map!")));
        mapInfo.offer(Keys.MAP_LOCKED, true);
        mapInfo.offer(Keys.MAP_CANVAS, mapCanvas);
        final ItemStack itemStack = ItemStack.of(ItemTypes.FILLED_MAP);
        itemStack.offer(Keys.MAP_INFO, mapInfo);
        itemStack.offer(Keys.CUSTOM_NAME, Component.text(entry.key().formatted()));
        player.inventory().primary().offer(itemStack);
    }
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) MapCanvas(org.spongepowered.api.map.MapCanvas) MapShade(org.spongepowered.api.map.color.MapShade) MapInfo(org.spongepowered.api.map.MapInfo) CommandException(org.spongepowered.api.command.exception.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack) RegistryEntry(org.spongepowered.api.registry.RegistryEntry) MapColor(org.spongepowered.api.map.color.MapColor)

Example 4 with MapInfo

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

the class MapTest method printMapData.

private CommandResult printMapData(final CommandContext ctx) throws CommandException {
    final Player player = this.requirePlayer(ctx);
    final ItemStack itemStack = player.itemInHand(HandTypes.MAIN_HAND);
    if (itemStack.type() != ItemTypes.FILLED_MAP.get()) {
        throw new CommandException(Component.text("You must hold a map in your hand"));
    }
    final MapInfo mapInfo = itemStack.require(Keys.MAP_INFO);
    final Audience console = Sponge.systemSubject();
    console.sendMessage(Component.text("the mapdata contains: " + mapInfo.toContainer()));
    console.sendMessage(Component.text("the map contains nbt: " + itemStack.toContainer()));
    // player.sendMessage(Text.of("the map contains vanilla nbt: " + itemStack.get().toContainer().get(DataQuery.of("UnsafeData")).get()));
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Audience(net.kyori.adventure.audience.Audience) MapInfo(org.spongepowered.api.map.MapInfo) CommandException(org.spongepowered.api.command.exception.CommandException) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 5 with MapInfo

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

the class MapTest method setMapWorld.

private CommandResult setMapWorld(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 ServerWorld serverWorld = (ServerWorld) player.location().world();
    mapInfo.offer(Keys.MAP_WORLD, serverWorld.key());
    player.sendMessage(Component.text("New map world: " + mapInfo.require(Keys.MAP_WORLD)));
    return CommandResult.success();
}
Also used : ServerWorld(org.spongepowered.api.world.server.ServerWorld) 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)

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