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);
});
}
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);
}
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();
}
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();
}
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();
}
Aggregations