use of org.spongepowered.api.event.action.CreateMapEvent 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);
}
Aggregations