|
| 1 | +/* |
| 2 | + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion |
| 3 | + * Copyright (C) 2016-2026 ViaVersion and contributors |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in all |
| 13 | + * copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | + * SOFTWARE. |
| 22 | + */ |
| 23 | +package com.viaversion.viaversion.api.data; |
| 24 | + |
| 25 | +import com.google.common.base.Preconditions; |
| 26 | +import com.viaversion.viaversion.api.data.MappingDataLoader.IdentifiersPair; |
| 27 | +import com.viaversion.viaversion.util.Key; |
| 28 | +import it.unimi.dsi.fastutil.objects.Object2IntMap; |
| 29 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 30 | + |
| 31 | +public class FullIdentityMappings implements FullMappings { |
| 32 | + private static final String[] EMPTY_ARRAY = new String[0]; |
| 33 | + private final Object2IntMap<String> stringToId; |
| 34 | + private final String[] idToString; |
| 35 | + private final Mappings mappings; |
| 36 | + |
| 37 | + public FullIdentityMappings(final IdentifiersPair identifiersPair, final Mappings mappings) { |
| 38 | + Preconditions.checkNotNull(mappings, "Mappings cannot be null"); |
| 39 | + this.mappings = mappings; |
| 40 | + this.stringToId = FullMappingsBase.toInverseMap(identifiersPair.unmapped()); |
| 41 | + this.idToString = identifiersPair.unmapped().toArray(EMPTY_ARRAY); |
| 42 | + } |
| 43 | + |
| 44 | + private FullIdentityMappings(final Object2IntMap<String> stringToId, final String[] idToString, final Mappings mappings) { |
| 45 | + this.stringToId = stringToId; |
| 46 | + this.idToString = idToString; |
| 47 | + this.mappings = mappings; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public int id(final String identifier) { |
| 52 | + return stringToId.getInt(Key.stripMinecraftNamespace(identifier)); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public int mappedId(final String mappedIdentifier) { |
| 57 | + return id(mappedIdentifier); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String identifier(final int id) { |
| 62 | + if (id < 0 || id >= idToString.length) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + final String identifier = idToString[id]; |
| 66 | + return Key.namespaced(identifier); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public @Nullable String identifier(final String mappedIdentifier) { |
| 71 | + final int mappedId = mappedId(mappedIdentifier); |
| 72 | + if (mappedId == -1) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + final int id = mappings.inverse().getNewId(mappedId); |
| 76 | + return id != -1 ? identifier(id) : null; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public @Nullable String mappedIdentifier(final int mappedId) { |
| 81 | + return identifier(mappedId); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public @Nullable String mappedIdentifier(final String identifier) { |
| 86 | + return identifier(identifier); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public int getNewId(int id) { |
| 91 | + return mappings.getNewId(id); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void setNewId(int id, int mappedId) { |
| 96 | + mappings.setNewId(id, mappedId); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public int size() { |
| 101 | + return mappings.size(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public int mappedSize() { |
| 106 | + return mappings.mappedSize(); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public boolean isIdentity() { |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public boolean isIntIdIdentity() { |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public FullMappings inverse() { |
| 121 | + return new FullIdentityMappings(stringToId, idToString, mappings.inverse()); |
| 122 | + } |
| 123 | +} |
0 commit comments