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