Search in sources :

Example 1 with MapIdTrackerBridge

use of org.spongepowered.common.bridge.map.MapIdTrackerBridge 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)

Aggregations

ServerLevel (net.minecraft.server.level.ServerLevel)1 MapItemSavedData (net.minecraft.world.level.saveddata.maps.MapItemSavedData)1 CreateMapEvent (org.spongepowered.api.event.action.CreateMapEvent)1 MapInfo (org.spongepowered.api.map.MapInfo)1 MapIdTrackerBridge (org.spongepowered.common.bridge.map.MapIdTrackerBridge)1 SpongeMapStorage (org.spongepowered.common.map.SpongeMapStorage)1