use of org.spongepowered.api.map.decoration.MapDecorationType 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.decoration.MapDecorationType in project SpongeCommon by SpongePowered.
the class SpongeMapDecorationBuilder method fromContainer.
@Override
public MapDecoration.Builder fromContainer(final DataView container) {
// with that id, which is quite likely
if (!container.contains(Constants.Map.DECORATION_TYPE, Constants.Map.DECORATION_ROTATION)) {
return this;
}
this.reset();
final byte type = this.getByteFromContainer(container, Constants.Map.DECORATION_TYPE);
final byte rot = this.getByteFromContainer(container, Constants.Map.DECORATION_ROTATION);
final net.minecraft.world.level.saveddata.maps.MapDecoration.Type[] decorations = net.minecraft.world.level.saveddata.maps.MapDecoration.Type.values();
final int possibleTypes = decorations.length;
if (type < 0 || type > possibleTypes) {
throw new InvalidDataException(Constants.Map.DECORATION_TYPE + ", is out of bounds. Should be 0-" + (possibleTypes - 1));
}
final MapDecorationType mapDecorationType = SpongeMapDecorationType.toSpongeType(decorations[type]).orElseThrow(() -> new IllegalStateException("Missing a MapDecorationType, could not find one for Minecraft's MapDecoration.Type: " + decorations[type].toString()));
this.type(mapDecorationType);
final int intRot = MapUtil.normalizeDecorationOrientation(rot);
this.rotation(MapUtil.getMapRotById(intRot));
// They are mutable once it is created.
if (container.contains(Constants.Map.DECORATION_X, Constants.Map.DECORATION_Y)) {
final byte x = this.getByteFromContainer(container, Constants.Map.DECORATION_X);
final byte y = this.getByteFromContainer(container, Constants.Map.DECORATION_Y);
this.position(Vector2i.from(x, y));
}
if (container.contains(Constants.Map.NAME)) {
final Component component = GsonComponentSerializer.gson().deserialize(container.getString(Constants.Map.NAME).orElseThrow(() -> new InvalidDataException("Invalid data type for " + Constants.Map.NAME + ". Should be String")));
this.customName(component);
}
return this;
}
Aggregations