|
| 1 | +/* |
| 2 | + * This file is part of Sponge, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) SpongePowered <https://www.spongepowered.org> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package org.spongepowered.common.world.schematic; |
| 26 | + |
| 27 | +import net.minecraft.core.IdMapper; |
| 28 | +import org.spongepowered.api.registry.Registry; |
| 29 | +import org.spongepowered.api.registry.RegistryHolder; |
| 30 | +import org.spongepowered.api.world.schematic.Palette; |
| 31 | +import org.spongepowered.api.world.schematic.PaletteReference; |
| 32 | +import org.spongepowered.api.world.schematic.PaletteType; |
| 33 | + |
| 34 | +import java.util.Map; |
| 35 | +import java.util.Optional; |
| 36 | +import java.util.OptionalInt; |
| 37 | +import java.util.Spliterator; |
| 38 | +import java.util.stream.Stream; |
| 39 | +import java.util.stream.StreamSupport; |
| 40 | + |
| 41 | +public class PaletteWrapper<NT, T, R> implements Palette.Immutable<T, R> { |
| 42 | + |
| 43 | + public static <NT, T, R> PaletteWrapper<NT, T, R> of(PaletteType<T, R> type, IdMapper<NT> proxy, Registry<R> registry) { |
| 44 | + return new PaletteWrapper<>(type, proxy, registry); |
| 45 | + } |
| 46 | + |
| 47 | + private final PaletteType<T, R> type; |
| 48 | + private final IdMapper<NT> proxy; |
| 49 | + private final Registry<R> registry; |
| 50 | + |
| 51 | + private PaletteWrapper(PaletteType<T, R> type, IdMapper<NT> proxy, Registry<R> registry) { |
| 52 | + this.type = type; |
| 53 | + this.proxy = proxy; |
| 54 | + this.registry = registry; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public PaletteType<T, R> type() { |
| 59 | + return this.type; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public int highestId() { |
| 64 | + return this.proxy.size(); |
| 65 | + } |
| 66 | + |
| 67 | + @SuppressWarnings("unchecked") |
| 68 | + @Override |
| 69 | + public Optional<PaletteReference<T, R>> get(int id) { |
| 70 | + final var n = this.proxy.byId(id); |
| 71 | + if (n == null) { |
| 72 | + return Optional.empty(); |
| 73 | + } |
| 74 | + final var apply = this.type.stringifier().apply(this.registry, (T) n); |
| 75 | + return Optional.of(PaletteReference.byString(this.registry.type(), apply)); |
| 76 | + } |
| 77 | + |
| 78 | + @SuppressWarnings("unchecked") |
| 79 | + @Override |
| 80 | + public OptionalInt get(T type) { |
| 81 | + final var id = this.proxy.getId((NT) type); |
| 82 | + if (id >= 0) { |
| 83 | + return OptionalInt.of(id); |
| 84 | + } |
| 85 | + return OptionalInt.empty(); |
| 86 | + } |
| 87 | + |
| 88 | + @SuppressWarnings("unchecked") |
| 89 | + @Override |
| 90 | + public Stream<T> stream() { |
| 91 | + return StreamSupport.stream(this.proxy::spliterator, |
| 92 | + Spliterator.SIZED | Spliterator.ORDERED | Spliterator.IMMUTABLE, |
| 93 | + false) |
| 94 | + .map(n -> (T) n); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public Stream<Map.Entry<T, Integer>> streamWithIds() { |
| 99 | + return Stream.empty(); |
| 100 | + } |
| 101 | + |
| 102 | + @SuppressWarnings("unchecked") |
| 103 | + @Override |
| 104 | + public Mutable<T, R> asMutable(RegistryHolder registry) { |
| 105 | + final var mutable = new MutableBimapPalette<T, R>(this.type, (Registry<R>) this.registry); |
| 106 | + for (NT nt : this.proxy) { |
| 107 | + mutable.orAssign((T) nt); |
| 108 | + } |
| 109 | + return mutable; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public Immutable<T, R> asImmutable() { |
| 114 | + return this; |
| 115 | + } |
| 116 | +} |
0 commit comments