From 318c49cf309005be5b2ed03a895896f194b6649d Mon Sep 17 00:00:00 2001 From: KennyTV Date: Tue, 27 Apr 2021 13:25:18 +0200 Subject: [PATCH] More interfaces, keep ProtocolRegistry.SERVER_PROTOCOL legacy api --- .../api/protocol/ProtocolRegistry.java | 18 +- .../api/connection/ProtocolInfo.java | 6 +- .../viaversion/api/data/IntArrayMappings.java | 122 ++++++++++++ .../viaversion/api/data/MappingData.java | 185 ++++++----------- .../viaversion/api/data/MappingDataBase.java | 186 ++++++++++++++++++ .../api/data/MappingDataLoader.java | 18 +- .../viaversion/api/data/Mappings.java | 93 +-------- .../ExternalJoinGameListener.java | 2 +- .../viaversion/api/platform/PlatformTask.java | 46 +++++ .../viaversion/api/platform/ViaPlatform.java | 18 +- .../api/protocol/base/Protocol.java | 1 + .../viaversion/util/Int2IntBiHashMap.java | 124 ++++++++++++ .../viaversion/util/Int2IntBiMap.java | 100 +--------- .../viaversion/ViaVersionPlugin.java | 39 ++-- .../bukkit/platform/BukkitTaskId.java | 23 ++- .../viaversion/viaversion/BungeePlugin.java | 35 ++-- .../bungee/handlers/BungeeServerHandler.java | 2 +- .../bungee/platform/BungeeTaskId.java | 20 +- .../viaversion/viaversion/ViaManagerImpl.java | 4 +- .../connection/ProtocolInfoImpl.java | 3 +- .../viaversion/data/EntityTracker.java | 2 +- .../protocol/ProtocolManagerImpl.java | 3 + .../Protocol1_13_1To1_13.java | 13 +- .../data/MappingData.java | 25 +-- .../data/MappingData.java | 3 +- .../data/MappingData.java | 6 +- .../data/MappingData.java | 3 +- .../data/MappingData.java | 3 +- .../Protocol1_17To1_16_4.java | 17 +- .../viaversion/rewriter/TagRewriter.java | 2 +- .../viaversion/viaversion/SpongePlugin.java | 43 ++-- .../sponge/platform/SpongeTaskId.java | 17 +- .../sponge/platform/SpongeViaLoader.java | 4 +- .../viaversion/viaversion/VelocityPlugin.java | 21 +- .../velocity/platform/VelocityTaskId.java | 17 +- 35 files changed, 740 insertions(+), 484 deletions(-) rename api/src/main/java/com/viaversion/viaversion/api/platform/TaskId.java => api-legacy/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java (81%) create mode 100644 api/src/main/java/com/viaversion/viaversion/api/data/IntArrayMappings.java create mode 100644 api/src/main/java/com/viaversion/viaversion/api/data/MappingDataBase.java rename api/src/main/java/com/viaversion/viaversion/api/{data => platform}/ExternalJoinGameListener.java (96%) create mode 100644 api/src/main/java/com/viaversion/viaversion/api/platform/PlatformTask.java create mode 100644 api/src/main/java/com/viaversion/viaversion/util/Int2IntBiHashMap.java diff --git a/api/src/main/java/com/viaversion/viaversion/api/platform/TaskId.java b/api-legacy/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java similarity index 81% rename from api/src/main/java/com/viaversion/viaversion/api/platform/TaskId.java rename to api-legacy/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java index c42aea10e..e338970e0 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/platform/TaskId.java +++ b/api-legacy/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java @@ -20,14 +20,14 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package com.viaversion.viaversion.api.platform; +package us.myles.ViaVersion.api.protocol; -public interface TaskId { - /** - * Returns the actual object represented by this TaskId - * Null if task cannot be cancelled. - * - * @return Platform based Object (don't assume) - */ - Object getObject(); +/** + * @deprecated may be removed at some point, use {@link com.viaversion.viaversion.api.protocol.ProtocolManager} + */ +@Deprecated +public class ProtocolRegistry { + + @Deprecated + public static int SERVER_PROTOCOL = -1; } diff --git a/api/src/main/java/com/viaversion/viaversion/api/connection/ProtocolInfo.java b/api/src/main/java/com/viaversion/viaversion/api/connection/ProtocolInfo.java index 837e70088..034c7f100 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/connection/ProtocolInfo.java +++ b/api/src/main/java/com/viaversion/viaversion/api/connection/ProtocolInfo.java @@ -24,7 +24,7 @@ package com.viaversion.viaversion.api.connection; import com.viaversion.viaversion.api.protocol.ProtocolPipeline; import com.viaversion.viaversion.api.protocol.packet.State; -import org.checkerframework.checker.nullness.qual.MonotonicNonNull; +import org.checkerframework.checker.nullness.qual.Nullable; import java.util.UUID; @@ -65,7 +65,7 @@ public interface ProtocolInfo { * * @return username, set when entering the {@link State#PLAY} state */ - @MonotonicNonNull String getUsername(); + @Nullable String getUsername(); void setUsername(String username); @@ -75,7 +75,7 @@ public interface ProtocolInfo { * * @return uuid, set when entering the {@link State#PLAY} state */ - UUID getUuid(); + @Nullable UUID getUuid(); void setUuid(UUID uuid); diff --git a/api/src/main/java/com/viaversion/viaversion/api/data/IntArrayMappings.java b/api/src/main/java/com/viaversion/viaversion/api/data/IntArrayMappings.java new file mode 100644 index 000000000..fad8386de --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/data/IntArrayMappings.java @@ -0,0 +1,122 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2021 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.data; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.Arrays; + +public class IntArrayMappings implements Mappings { + protected final int[] oldToNew; + + public IntArrayMappings(int[] oldToNew) { + this.oldToNew = oldToNew; + } + + /** + * Maps old identifiers to the new ones. + * If an old value cannot be found in the new mappings, the diffmapping will be checked for the given entry. + * + * @param size set size of the underlying short array + * @param oldMapping mappings to map from + * @param newMapping mappings to map to + * @param diffMapping extra mappings that will be used/scanned when an entry cannot be found + */ + public IntArrayMappings(int size, JsonObject oldMapping, JsonObject newMapping, @Nullable JsonObject diffMapping) { + oldToNew = new int[size]; + Arrays.fill(oldToNew, -1); + MappingDataLoader.mapIdentifiers(oldToNew, oldMapping, newMapping, diffMapping); + } + + public IntArrayMappings(JsonObject oldMapping, JsonObject newMapping, @Nullable JsonObject diffMapping) { + this(oldMapping.entrySet().size(), oldMapping, newMapping, diffMapping); + } + + /** + * Maps old identifiers to the new ones. + * + * @param size set size of the underlying short array + * @param oldMapping mappings to map from + * @param newMapping mappings to map to + */ + public IntArrayMappings(int size, JsonObject oldMapping, JsonObject newMapping) { + oldToNew = new int[size]; + Arrays.fill(oldToNew, -1); + MappingDataLoader.mapIdentifiers(oldToNew, oldMapping, newMapping); + } + + public IntArrayMappings(JsonObject oldMapping, JsonObject newMapping) { + this(oldMapping.entrySet().size(), oldMapping, newMapping); + } + + /** + * Maps old identifiers to the new ones. + * + * @param size set size of the underlying short array + * @param oldMapping mappings to map from + * @param newMapping mappings to map to + * @param diffMapping extra mappings that will be used/scanned when an entry cannot be found + * @param warnOnMissing should "No key for x" be printed if there is no matching identifier + */ + public IntArrayMappings(int size, JsonArray oldMapping, JsonArray newMapping, JsonObject diffMapping, boolean warnOnMissing) { + oldToNew = new int[size]; + Arrays.fill(oldToNew, -1); + MappingDataLoader.mapIdentifiers(oldToNew, oldMapping, newMapping, diffMapping, warnOnMissing); + } + + public IntArrayMappings(int size, JsonArray oldMapping, JsonArray newMapping, boolean warnOnMissing) { + this(size, oldMapping, newMapping, null, warnOnMissing); + } + + public IntArrayMappings(JsonArray oldMapping, JsonArray newMapping, boolean warnOnMissing) { + this(oldMapping.size(), oldMapping, newMapping, warnOnMissing); + } + + public IntArrayMappings(int size, JsonArray oldMapping, JsonArray newMapping) { + this(size, oldMapping, newMapping, true); + } + + public IntArrayMappings(JsonArray oldMapping, JsonArray newMapping, JsonObject diffMapping) { + this(oldMapping.size(), oldMapping, newMapping, diffMapping, true); + } + + public IntArrayMappings(JsonArray oldMapping, JsonArray newMapping) { + this(oldMapping.size(), oldMapping, newMapping, true); + } + + @Override + public int getNewId(int id) { + return id >= 0 && id < oldToNew.length ? oldToNew[id] : -1; + } + + @Override + public void setNewId(int id, int newId) { + oldToNew[id] = newId; + } + + public int[] getOldToNew() { + return oldToNew; + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/data/MappingData.java b/api/src/main/java/com/viaversion/viaversion/api/data/MappingData.java index 811ff2fe6..0010d43bc 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/data/MappingData.java +++ b/api/src/main/java/com/viaversion/viaversion/api/data/MappingData.java @@ -22,138 +22,65 @@ */ package com.viaversion.viaversion.api.data; -import com.google.gson.JsonObject; -import org.checkerframework.checker.nullness.qual.Nullable; -import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.util.Int2IntBiMap; +import org.checkerframework.checker.nullness.qual.Nullable; -public class MappingData { - protected final String oldVersion; - protected final String newVersion; - protected final boolean hasDiffFile; - protected Int2IntBiMap itemMappings; - protected ParticleMappings particleMappings; - protected Mappings blockMappings; - protected Mappings blockStateMappings; - protected Mappings soundMappings; - protected Mappings statisticsMappings; - protected boolean loadItems = true; - - public MappingData(String oldVersion, String newVersion) { - this(oldVersion, newVersion, false); - } - - public MappingData(String oldVersion, String newVersion, boolean hasDiffFile) { - this.oldVersion = oldVersion; - this.newVersion = newVersion; - this.hasDiffFile = hasDiffFile; - } - - public void load() { - Via.getPlatform().getLogger().info("Loading " + oldVersion + " -> " + newVersion + " mappings..."); - JsonObject diffmapping = hasDiffFile ? loadDiffFile() : null; - JsonObject oldMappings = MappingDataLoader.loadData("mapping-" + oldVersion + ".json", true); - JsonObject newMappings = MappingDataLoader.loadData("mapping-" + newVersion + ".json", true); - - blockMappings = loadFromObject(oldMappings, newMappings, diffmapping, "blocks"); - blockStateMappings = loadFromObject(oldMappings, newMappings, diffmapping, "blockstates"); - soundMappings = loadFromArray(oldMappings, newMappings, diffmapping, "sounds"); - statisticsMappings = loadFromArray(oldMappings, newMappings, diffmapping, "statistics"); - - Mappings particles = loadFromArray(oldMappings, newMappings, diffmapping, "particles"); - if (particles != null) { - particleMappings = new ParticleMappings(oldMappings.getAsJsonArray("particles"), particles); - } - - if (loadItems && newMappings.has("items")) { - itemMappings = new Int2IntBiMap(); - itemMappings.defaultReturnValue(-1); - MappingDataLoader.mapIdentifiers(itemMappings, oldMappings.getAsJsonObject("items"), newMappings.getAsJsonObject("items"), - diffmapping != null ? diffmapping.getAsJsonObject("items") : null); - } - - loadExtras(oldMappings, newMappings, diffmapping); - } - - public int getNewBlockStateId(int id) { - return checkValidity(id, blockStateMappings.getNewId(id), "blockstate"); - } - - public int getNewBlockId(int id) { - return checkValidity(id, blockMappings.getNewId(id), "block"); - } - - public int getNewItemId(int id) { - return checkValidity(id, itemMappings.get(id), "item"); - } - - public int getOldItemId(int id) { - int oldId = itemMappings.inverse().get(id); - // Remap new items to stone - return oldId != -1 ? oldId : 1; - } - - public int getNewParticleId(int id) { - return checkValidity(id, particleMappings.getMappings().getNewId(id), "particles"); - } - - public @Nullable Int2IntBiMap getItemMappings() { - return itemMappings; - } - - public @Nullable ParticleMappings getParticleMappings() { - return particleMappings; - } - - public @Nullable Mappings getBlockMappings() { - return blockMappings; - } - - public @Nullable Mappings getBlockStateMappings() { - return blockStateMappings; - } - - public @Nullable Mappings getSoundMappings() { - return soundMappings; - } - - public @Nullable Mappings getStatisticsMappings() { - return statisticsMappings; - } - - protected @Nullable Mappings loadFromArray(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings, String key) { - if (!oldMappings.has(key) || !newMappings.has(key)) return null; - - JsonObject diff = diffMappings != null ? diffMappings.getAsJsonObject(key) : null; - return new Mappings(oldMappings.getAsJsonArray(key), newMappings.getAsJsonArray(key), diff); - } - - protected @Nullable Mappings loadFromObject(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings, String key) { - if (!oldMappings.has(key) || !newMappings.has(key)) return null; - - JsonObject diff = diffMappings != null ? diffMappings.getAsJsonObject(key) : null; - return new Mappings(oldMappings.getAsJsonObject(key), newMappings.getAsJsonObject(key), diff); - } - - protected JsonObject loadDiffFile() { - return MappingDataLoader.loadData("mappingdiff-" + oldVersion + "to" + newVersion + ".json"); - } - - protected int checkValidity(int id, int mappedId, String type) { - if (mappedId == -1) { - Via.getPlatform().getLogger().warning(String.format("Missing %s %s for %s %s %d", newVersion, type, oldVersion, type, id)); - return 0; - } - return mappedId; - } +public interface MappingData { /** - * To be overridden. - * - * @param oldMappings old mappings - * @param newMappings new mappings - * @param diffMappings diff mappings if present + * Loads the mapping data. */ - protected void loadExtras(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings) { - } + void load(); + + /** + * Returns the mapped block state id, or -1 if unmapped. + * + * @param id unmapped block state id + * @return mapped block state id, or -1 if unmapped + */ + int getNewBlockStateId(int id); + + /** + * Returns the mapped block id, or -1 if unmapped. + * + * @param id unmapped block id + * @return mapped block id, or -1 if unmapped + */ + int getNewBlockId(int id); + + /** + * Returns the mapped item id, or -1 if unmapped. + * + * @param id unmapped item id + * @return mapped item id, or -1 if unmapped + */ + int getNewItemId(int id); + + /** + * Returns the backwards mapped item id, or -1 if unmapped. + * + * @param id mapped item id + * @return backwards mapped item id, or -1 if unmapped + */ + int getOldItemId(int id); + + /** + * Returns the mapped particle id, or -1 if unmapped. + * + * @param id unmapped particle id + * @return mapped particle id, or -1 if unmapped + */ + int getNewParticleId(int id); + + @Nullable Int2IntBiMap getItemMappings(); + + @Nullable ParticleMappings getParticleMappings(); + + @Nullable Mappings getBlockMappings(); + + @Nullable Mappings getBlockStateMappings(); + + @Nullable Mappings getSoundMappings(); + + @Nullable Mappings getStatisticsMappings(); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataBase.java b/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataBase.java new file mode 100644 index 000000000..b61e43859 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataBase.java @@ -0,0 +1,186 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2021 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.data; + +import com.google.gson.JsonObject; +import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.util.Int2IntBiHashMap; +import com.viaversion.viaversion.util.Int2IntBiMap; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.logging.Logger; + +public class MappingDataBase implements MappingData { + protected final String oldVersion; + protected final String newVersion; + protected final boolean hasDiffFile; + protected Int2IntBiMap itemMappings; + protected ParticleMappings particleMappings; + protected Mappings blockMappings; + protected Mappings blockStateMappings; + protected Mappings soundMappings; + protected Mappings statisticsMappings; + protected boolean loadItems = true; + + public MappingDataBase(String oldVersion, String newVersion) { + this(oldVersion, newVersion, false); + } + + public MappingDataBase(String oldVersion, String newVersion, boolean hasDiffFile) { + this.oldVersion = oldVersion; + this.newVersion = newVersion; + this.hasDiffFile = hasDiffFile; + } + + @Override + public void load() { + getLogger().info("Loading " + oldVersion + " -> " + newVersion + " mappings..."); + JsonObject diffmapping = hasDiffFile ? loadDiffFile() : null; + JsonObject oldMappings = MappingDataLoader.loadData("mapping-" + oldVersion + ".json", true); + JsonObject newMappings = MappingDataLoader.loadData("mapping-" + newVersion + ".json", true); + + blockMappings = loadFromObject(oldMappings, newMappings, diffmapping, "blocks"); + blockStateMappings = loadFromObject(oldMappings, newMappings, diffmapping, "blockstates"); + soundMappings = loadFromArray(oldMappings, newMappings, diffmapping, "sounds"); + statisticsMappings = loadFromArray(oldMappings, newMappings, diffmapping, "statistics"); + + Mappings particles = loadFromArray(oldMappings, newMappings, diffmapping, "particles"); + if (particles != null) { + particleMappings = new ParticleMappings(oldMappings.getAsJsonArray("particles"), particles); + } + + if (loadItems && newMappings.has("items")) { + itemMappings = new Int2IntBiHashMap(); + itemMappings.defaultReturnValue(-1); + MappingDataLoader.mapIdentifiers(itemMappings, oldMappings.getAsJsonObject("items"), newMappings.getAsJsonObject("items"), + diffmapping != null ? diffmapping.getAsJsonObject("items") : null); + } + + loadExtras(oldMappings, newMappings, diffmapping); + } + + @Override + public int getNewBlockStateId(int id) { + return checkValidity(id, blockStateMappings.getNewId(id), "blockstate"); + } + + @Override + public int getNewBlockId(int id) { + return checkValidity(id, blockMappings.getNewId(id), "block"); + } + + @Override + public int getNewItemId(int id) { + return checkValidity(id, itemMappings.get(id), "item"); + } + + @Override + public int getOldItemId(int id) { + int oldId = itemMappings.inverse().get(id); + // Remap new items to stone + return oldId != -1 ? oldId : 1; + } + + @Override + public int getNewParticleId(int id) { + return checkValidity(id, particleMappings.getMappings().getNewId(id), "particles"); + } + + @Override + public @Nullable Int2IntBiMap getItemMappings() { + return itemMappings; + } + + @Override + public @Nullable ParticleMappings getParticleMappings() { + return particleMappings; + } + + @Override + public @Nullable Mappings getBlockMappings() { + return blockMappings; + } + + @Override + public @Nullable Mappings getBlockStateMappings() { + return blockStateMappings; + } + + @Override + public @Nullable Mappings getSoundMappings() { + return soundMappings; + } + + @Override + public @Nullable Mappings getStatisticsMappings() { + return statisticsMappings; + } + + protected @Nullable Mappings loadFromArray(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings, String key) { + if (!oldMappings.has(key) || !newMappings.has(key)) return null; + + JsonObject diff = diffMappings != null ? diffMappings.getAsJsonObject(key) : null; + return new IntArrayMappings(oldMappings.getAsJsonArray(key), newMappings.getAsJsonArray(key), diff); + } + + protected @Nullable Mappings loadFromObject(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings, String key) { + if (!oldMappings.has(key) || !newMappings.has(key)) return null; + + JsonObject diff = diffMappings != null ? diffMappings.getAsJsonObject(key) : null; + return new IntArrayMappings(oldMappings.getAsJsonObject(key), newMappings.getAsJsonObject(key), diff); + } + + protected JsonObject loadDiffFile() { + return MappingDataLoader.loadData("mappingdiff-" + oldVersion + "to" + newVersion + ".json"); + } + + protected Logger getLogger() { + return Via.getPlatform().getLogger(); + } + + /** + * Returns the given mapped id if valid, else 0 with a warning logged to the console. + * + * @param id unmapped id + * @param mappedId mapped id + * @param type mapping type (e.g. "item") + * @return the given mapped id if valid, else 0 + */ + protected int checkValidity(int id, int mappedId, String type) { + if (mappedId == -1) { + getLogger().warning(String.format("Missing %s %s for %s %s %d", newVersion, type, oldVersion, type, id)); + return 0; + } + return mappedId; + } + + /** + * To be overridden. + * + * @param oldMappings old mappings + * @param newMappings new mappings + * @param diffMappings diff mappings if present + */ + protected void loadExtras(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings) { + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataLoader.java b/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataLoader.java index 60319fbf9..cf83e8ea4 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataLoader.java +++ b/api/src/main/java/com/viaversion/viaversion/api/data/MappingDataLoader.java @@ -27,12 +27,12 @@ import com.google.gson.JsonElement; import com.google.gson.JsonIOException; import com.google.gson.JsonObject; import com.google.gson.JsonSyntaxException; -import it.unimi.dsi.fastutil.objects.Object2IntMap; -import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; -import org.checkerframework.checker.nullness.qual.Nullable; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.util.GsonUtil; import com.viaversion.viaversion.util.Int2IntBiMap; +import it.unimi.dsi.fastutil.objects.Object2IntMap; +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; +import org.checkerframework.checker.nullness.qual.Nullable; import java.io.File; import java.io.FileReader; @@ -62,8 +62,9 @@ public class MappingDataLoader { } /** - * Returns the cached mappings. Cleared after Via has been fully loaded. + * Returns the cached mappings. Cleared after ViaVersion has been fully loaded. * + * @return cached mapping file json objects * @see #isCacheJsonMappings() */ public static Map getMappingsCache() { @@ -72,6 +73,8 @@ public class MappingDataLoader { /** * Loads the file from the plugin folder if present, else from the bundled resources. + * + * @return loaded json object, or null if not found or invalid */ public static @Nullable JsonObject loadFromDataDir(String name) { File file = new File(Via.getPlatform().getDataFolder(), name); @@ -92,6 +95,8 @@ public class MappingDataLoader { /** * Loads the file from the bundled resources. Uses the cache if enabled. + * + * @return loaded json object from bundled resources if present */ public static @Nullable JsonObject loadData(String name) { return loadData(name, false); @@ -101,6 +106,7 @@ public class MappingDataLoader { * Loads the file from the bundled resources. Uses the cache if enabled. * * @param cacheIfEnabled whether loaded files should be cached + * @return loaded json object from bundled resources if present */ public static @Nullable JsonObject loadData(String name, boolean cacheIfEnabled) { if (cacheJsonMappings) { @@ -148,7 +154,7 @@ public class MappingDataLoader { for (Map.Entry entry : oldIdentifiers.entrySet()) { int value = mapIdentifierEntry(entry, newIdentifierMap, diffIdentifiers); if (value != -1) { - output[Integer.parseInt(entry.getKey())] = (short) value; + output[Integer.parseInt(entry.getKey())] = value; } } } @@ -200,7 +206,7 @@ public class MappingDataLoader { continue; } } - output[i] = (short) mappedId; + output[i] = mappedId; } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/data/Mappings.java b/api/src/main/java/com/viaversion/viaversion/api/data/Mappings.java index e5699f6e4..cbda2ab04 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/data/Mappings.java +++ b/api/src/main/java/com/viaversion/viaversion/api/data/Mappings.java @@ -22,95 +22,22 @@ */ package com.viaversion.viaversion.api.data; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import org.checkerframework.checker.nullness.qual.Nullable; - -import java.util.Arrays; - -public class Mappings { - protected final int[] oldToNew; - - public Mappings(int[] oldToNew) { - this.oldToNew = oldToNew; - } +public interface Mappings { /** - * Maps old identifiers to the new ones. - * If an old value cannot be found in the new mappings, the diffmapping will be checked for the given entry. + * Returns the mapped id from the given id, or -1 if invalid/out of bounds. * - * @param size set size of the underlying short array - * @param oldMapping mappings to map from - * @param newMapping mappings to map to - * @param diffMapping extra mappings that will be used/scanned when an entry cannot be found + * @param id unmapped id + * @return mapped id, or -1 if invalid/out of bounds */ - public Mappings(int size, JsonObject oldMapping, JsonObject newMapping, @Nullable JsonObject diffMapping) { - oldToNew = new int[size]; - Arrays.fill(oldToNew, -1); - MappingDataLoader.mapIdentifiers(oldToNew, oldMapping, newMapping, diffMapping); - } - - public Mappings(JsonObject oldMapping, JsonObject newMapping, @Nullable JsonObject diffMapping) { - this(oldMapping.entrySet().size(), oldMapping, newMapping, diffMapping); - } + int getNewId(int id); /** - * Maps old identifiers to the new ones. + * Manually maps a specific id. * - * @param size set size of the underlying short array - * @param oldMapping mappings to map from - * @param newMapping mappings to map to + * @param id unmapped id + * @param newId mapped id + * @throws IndexOutOfBoundsException if the unmapped id is invalid */ - public Mappings(int size, JsonObject oldMapping, JsonObject newMapping) { - oldToNew = new int[size]; - Arrays.fill(oldToNew, -1); - MappingDataLoader.mapIdentifiers(oldToNew, oldMapping, newMapping); - } - - public Mappings(JsonObject oldMapping, JsonObject newMapping) { - this(oldMapping.entrySet().size(), oldMapping, newMapping); - } - - /** - * Maps old identifiers to the new ones. - * - * @param size set size of the underlying short array - * @param oldMapping mappings to map from - * @param newMapping mappings to map to - * @param diffMapping extra mappings that will be used/scanned when an entry cannot be found - * @param warnOnMissing should "No key for x" be printed if there is no matching identifier - */ - public Mappings(int size, JsonArray oldMapping, JsonArray newMapping, JsonObject diffMapping, boolean warnOnMissing) { - oldToNew = new int[size]; - Arrays.fill(oldToNew, -1); - MappingDataLoader.mapIdentifiers(oldToNew, oldMapping, newMapping, diffMapping, warnOnMissing); - } - - public Mappings(int size, JsonArray oldMapping, JsonArray newMapping, boolean warnOnMissing) { - this(size, oldMapping, newMapping, null, warnOnMissing); - } - - public Mappings(JsonArray oldMapping, JsonArray newMapping, boolean warnOnMissing) { - this(oldMapping.size(), oldMapping, newMapping, warnOnMissing); - } - - public Mappings(int size, JsonArray oldMapping, JsonArray newMapping) { - this(size, oldMapping, newMapping, true); - } - - public Mappings(JsonArray oldMapping, JsonArray newMapping, JsonObject diffMapping) { - this(oldMapping.size(), oldMapping, newMapping, diffMapping, true); - } - - public Mappings(JsonArray oldMapping, JsonArray newMapping) { - this(oldMapping.size(), oldMapping, newMapping, true); - } - - public int getNewId(int old) { - return old >= 0 && old < oldToNew.length ? oldToNew[old] : -1; - } - - public int[] getOldToNew() { - return oldToNew; - } + void setNewId(int id, int newId); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/data/ExternalJoinGameListener.java b/api/src/main/java/com/viaversion/viaversion/api/platform/ExternalJoinGameListener.java similarity index 96% rename from api/src/main/java/com/viaversion/viaversion/api/data/ExternalJoinGameListener.java rename to api/src/main/java/com/viaversion/viaversion/api/platform/ExternalJoinGameListener.java index 5b08706ab..155845a38 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/data/ExternalJoinGameListener.java +++ b/api/src/main/java/com/viaversion/viaversion/api/platform/ExternalJoinGameListener.java @@ -20,7 +20,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package com.viaversion.viaversion.api.data; +package com.viaversion.viaversion.api.platform; public interface ExternalJoinGameListener { diff --git a/api/src/main/java/com/viaversion/viaversion/api/platform/PlatformTask.java b/api/src/main/java/com/viaversion/viaversion/api/platform/PlatformTask.java new file mode 100644 index 000000000..6a7c8e55f --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/platform/PlatformTask.java @@ -0,0 +1,46 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2021 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.platform; + +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * @param task type + */ +public interface PlatformTask { + + /** + * Returns the actual object represented by this task/task id. + * Null if task cannot be cancelled. + * + * @return platform based object, or null if not cancellable + */ + @Nullable T getObject(); + + /** + * Cancels the task. + * + * @throws IllegalArgumentException if the task is not cancellable + */ + void cancel(); +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/platform/ViaPlatform.java b/api/src/main/java/com/viaversion/viaversion/api/platform/ViaPlatform.java index d27f1747f..bb32557bc 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/platform/ViaPlatform.java +++ b/api/src/main/java/com/viaversion/viaversion/api/platform/ViaPlatform.java @@ -85,7 +85,7 @@ public interface ViaPlatform { * @param runnable The task to run * @return The Task ID */ - TaskId runAsync(Runnable runnable); + PlatformTask runAsync(Runnable runnable); /** * Run a task Sync @@ -93,7 +93,7 @@ public interface ViaPlatform { * @param runnable The task to run * @return The Task ID */ - TaskId runSync(Runnable runnable); + PlatformTask runSync(Runnable runnable); /** * Run a task Sync after a interval @@ -103,7 +103,7 @@ public interface ViaPlatform { * @param ticks The interval to run it after * @return The Task ID */ - TaskId runSync(Runnable runnable, Long ticks); + PlatformTask runSync(Runnable runnable, Long ticks); /** * Run a task at a repeating interval. @@ -113,14 +113,18 @@ public interface ViaPlatform { * @param ticks The interval to run it at * @return The Task ID */ - TaskId runRepeatingSync(Runnable runnable, Long ticks); + PlatformTask runRepeatingSync(Runnable runnable, Long ticks); /** - * Cancel a task + * Cancels a task. * - * @param taskId The task ID to cancel + * @param task task to cancel */ - void cancelTask(TaskId taskId); + default void cancelTask(PlatformTask task) { + if (task.getObject() != null) { + task.cancel(); + } + } /** * Get the online players diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/base/Protocol.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/base/Protocol.java index 773944f50..20513f627 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/base/Protocol.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/base/Protocol.java @@ -24,6 +24,7 @@ package com.viaversion.viaversion.api.protocol.base; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.data.MappingData; +import com.viaversion.viaversion.api.data.MappingDataBase; import com.viaversion.viaversion.api.platform.providers.ViaProviders; import com.viaversion.viaversion.api.protocol.AbstractSimpleProtocol; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; diff --git a/api/src/main/java/com/viaversion/viaversion/util/Int2IntBiHashMap.java b/api/src/main/java/com/viaversion/viaversion/util/Int2IntBiHashMap.java new file mode 100644 index 000000000..80cb1e3ff --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/util/Int2IntBiHashMap.java @@ -0,0 +1,124 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2021 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.util; + +import com.google.common.base.Preconditions; +import it.unimi.dsi.fastutil.ints.Int2IntMap; +import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; +import it.unimi.dsi.fastutil.ints.IntSet; +import it.unimi.dsi.fastutil.objects.ObjectSet; +import org.checkerframework.checker.nullness.qual.NonNull; + +public class Int2IntBiHashMap implements Int2IntBiMap { + + private final Int2IntMap map; + private final Int2IntBiHashMap inverse; + + public Int2IntBiHashMap() { + this.map = new Int2IntOpenHashMap(); + this.inverse = new Int2IntBiHashMap(this); + } + + private Int2IntBiHashMap(Int2IntBiHashMap inverse) { + this.map = new Int2IntOpenHashMap(); + this.inverse = inverse; + } + + @Override + public Int2IntBiMap inverse() { + return inverse; + } + + @Override + public int put(int key, int value) { + if (containsKey(key) && value == get(key)) return value; + + Preconditions.checkArgument(!containsValue(value), "value already present: %s", value); + map.put(key, value); + inverse.map.put(value, key); + return defaultReturnValue(); + } + + @Override + public boolean remove(int key, int value) { + map.remove(key, value); + return inverse.map.remove(key, value); + } + + @Override + public int get(int key) { + return map.get(key); + } + + @Override + public void clear() { + map.clear(); + inverse.map.clear(); + } + + @Override + public int size() { + return map.size(); + } + + @Override + public boolean isEmpty() { + return map.isEmpty(); + } + + @Override + public void defaultReturnValue(int rv) { + map.defaultReturnValue(rv); + inverse.map.defaultReturnValue(rv); + } + + @Override + public int defaultReturnValue() { + return map.defaultReturnValue(); + } + + @Override + public ObjectSet int2IntEntrySet() { + return map.int2IntEntrySet(); + } + + @Override + public @NonNull IntSet keySet() { + return map.keySet(); + } + + @Override + public @NonNull IntSet values() { + return inverse.map.keySet(); + } + + @Override + public boolean containsKey(int key) { + return map.containsKey(key); + } + + @Override + public boolean containsValue(int value) { + return inverse.map.containsKey(value); + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/util/Int2IntBiMap.java b/api/src/main/java/com/viaversion/viaversion/util/Int2IntBiMap.java index 7534cb10e..680054d19 100644 --- a/api/src/main/java/com/viaversion/viaversion/util/Int2IntBiMap.java +++ b/api/src/main/java/com/viaversion/viaversion/util/Int2IntBiMap.java @@ -22,42 +22,24 @@ */ package com.viaversion.viaversion.util; -import com.google.common.base.Preconditions; import it.unimi.dsi.fastutil.ints.Int2IntMap; -import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; -import it.unimi.dsi.fastutil.ints.IntSet; -import it.unimi.dsi.fastutil.objects.ObjectSet; import org.checkerframework.checker.nullness.qual.NonNull; import java.util.Map; - /** * Simple wrapper class for two {@link Int2IntMap}s. * * @see #inverse() to get the inversed map */ -public class Int2IntBiMap implements Int2IntMap { - - private final Int2IntMap map; - private final Int2IntBiMap inverse; - - public Int2IntBiMap() { - this.map = new Int2IntOpenHashMap(); - this.inverse = new Int2IntBiMap(this); - } - - private Int2IntBiMap(Int2IntBiMap inverse) { - this.map = new Int2IntOpenHashMap(); - this.inverse = inverse; - } +public interface Int2IntBiMap extends Int2IntMap { /** + * Returns the inverse of this bimap. + * * @return the inverse of this bimap */ - public Int2IntBiMap inverse() { - return inverse; - } + Int2IntBiMap inverse(); /** * Puts the key and value into the maps. @@ -68,81 +50,11 @@ public class Int2IntBiMap implements Int2IntMap { * @throws IllegalArgumentException if the value already exists in the map */ @Override - public int put(int key, int value) { - if (containsKey(key) && value == get(key)) return value; - - Preconditions.checkArgument(!containsValue(value), "value already present: %s", value); - map.put(key, value); - inverse.map.put(value, key); - return defaultReturnValue(); - } - - @Override - public boolean remove(int key, int value) { - map.remove(key, value); - return inverse.map.remove(key, value); - } - - @Override - public int get(int key) { - return map.get(key); - } - - @Override - public void clear() { - map.clear(); - inverse.map.clear(); - } - - @Override - public int size() { - return map.size(); - } - - @Override - public boolean isEmpty() { - return map.isEmpty(); - } + int put(int key, int value); @Override @Deprecated - public void putAll(@NonNull Map m) { + default void putAll(@NonNull Map m) { throw new UnsupportedOperationException(); } - - @Override - public void defaultReturnValue(int rv) { - map.defaultReturnValue(rv); - inverse.map.defaultReturnValue(rv); - } - - @Override - public int defaultReturnValue() { - return map.defaultReturnValue(); - } - - @Override - public ObjectSet int2IntEntrySet() { - return map.int2IntEntrySet(); - } - - @Override - public @NonNull IntSet keySet() { - return map.keySet(); - } - - @Override - public @NonNull IntSet values() { - return inverse.map.keySet(); - } - - @Override - public boolean containsKey(int key) { - return map.containsKey(key); - } - - @Override - public boolean containsValue(int value) { - return inverse.map.containsKey(value); - } } diff --git a/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java b/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java index 903c966f9..990d26a4d 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java @@ -18,17 +18,13 @@ package com.viaversion.viaversion; import com.google.gson.JsonObject; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.java.JavaPlugin; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.ViaAPI; import com.viaversion.viaversion.api.command.ViaCommandSender; import com.viaversion.viaversion.api.configuration.ConfigurationProvider; import com.viaversion.viaversion.api.data.MappingDataLoader; -import com.viaversion.viaversion.api.platform.TaskId; +import com.viaversion.viaversion.api.platform.PlatformTask; +import com.viaversion.viaversion.api.platform.UnsupportedSoftware; import com.viaversion.viaversion.api.platform.ViaPlatform; import com.viaversion.viaversion.bukkit.classgenerator.ClassGenerator; import com.viaversion.viaversion.bukkit.commands.BukkitCommandHandler; @@ -42,7 +38,11 @@ import com.viaversion.viaversion.bukkit.platform.BukkitViaLoader; import com.viaversion.viaversion.bukkit.util.NMSUtil; import com.viaversion.viaversion.dump.PluginInfo; import com.viaversion.viaversion.util.GsonUtil; -import com.viaversion.viaversion.api.platform.UnsupportedSoftware; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.java.JavaPlugin; import java.util.ArrayList; import java.util.Collection; @@ -178,9 +178,9 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform } @Override - public TaskId runAsync(Runnable runnable) { + public PlatformTask runAsync(Runnable runnable) { if (isPluginEnabled()) { - return new BukkitTaskId(getServer().getScheduler().runTaskAsynchronously(this, runnable).getTaskId()); + return new BukkitTaskId(getServer().getScheduler().runTaskAsynchronously(this, runnable)); } else { asyncQueuedTasks.add(runnable); return new BukkitTaskId(null); @@ -188,9 +188,9 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform } @Override - public TaskId runSync(Runnable runnable) { + public PlatformTask runSync(Runnable runnable) { if (isPluginEnabled()) { - return new BukkitTaskId(getServer().getScheduler().runTask(this, runnable).getTaskId()); + return new BukkitTaskId(getServer().getScheduler().runTask(this, runnable)); } else { queuedTasks.add(runnable); return new BukkitTaskId(null); @@ -198,22 +198,13 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform } @Override - public TaskId runSync(Runnable runnable, Long ticks) { - return new BukkitTaskId(getServer().getScheduler().runTaskLater(this, runnable, ticks).getTaskId()); + public PlatformTask runSync(Runnable runnable, Long ticks) { + return new BukkitTaskId(getServer().getScheduler().runTaskLater(this, runnable, ticks)); } @Override - public TaskId runRepeatingSync(Runnable runnable, Long ticks) { - return new BukkitTaskId(getServer().getScheduler().runTaskTimer(this, runnable, 0, ticks).getTaskId()); - } - - @Override - public void cancelTask(TaskId taskId) { - if (taskId == null) return; - if (taskId.getObject() == null) return; - if (taskId instanceof BukkitTaskId) { - getServer().getScheduler().cancelTask((Integer) taskId.getObject()); - } + public PlatformTask runRepeatingSync(Runnable runnable, Long ticks) { + return new BukkitTaskId(getServer().getScheduler().runTaskTimer(this, runnable, 0, ticks)); } @Override diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitTaskId.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitTaskId.java index efad99a40..abb2f7176 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitTaskId.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitTaskId.java @@ -17,17 +17,26 @@ */ package com.viaversion.viaversion.bukkit.platform; -import com.viaversion.viaversion.api.platform.TaskId; +import com.google.common.base.Preconditions; +import com.viaversion.viaversion.api.platform.PlatformTask; +import org.bukkit.scheduler.BukkitTask; +import org.checkerframework.checker.nullness.qual.Nullable; -public class BukkitTaskId implements TaskId { - private final Integer object; +public class BukkitTaskId implements PlatformTask { + private final BukkitTask task; - public BukkitTaskId(Integer object) { - this.object = object; + public BukkitTaskId(@Nullable BukkitTask task) { + this.task = task; } @Override - public Integer getObject() { - return object; + public @Nullable BukkitTask getObject() { + return task; + } + + @Override + public void cancel() { + Preconditions.checkArgument(task != null, "Task cannot be cancelled"); + task.cancel(); } } diff --git a/bungee/src/main/java/com/viaversion/viaversion/BungeePlugin.java b/bungee/src/main/java/com/viaversion/viaversion/BungeePlugin.java index 962303124..ff963de7b 100644 --- a/bungee/src/main/java/com/viaversion/viaversion/BungeePlugin.java +++ b/bungee/src/main/java/com/viaversion/viaversion/BungeePlugin.java @@ -18,17 +18,12 @@ package com.viaversion.viaversion; import com.google.gson.JsonObject; -import net.md_5.bungee.api.ProxyServer; -import net.md_5.bungee.api.connection.ProxiedPlayer; -import net.md_5.bungee.api.plugin.Listener; -import net.md_5.bungee.api.plugin.Plugin; -import net.md_5.bungee.protocol.ProtocolConstants; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.ViaAPI; import com.viaversion.viaversion.api.command.ViaCommandSender; import com.viaversion.viaversion.api.configuration.ConfigurationProvider; import com.viaversion.viaversion.api.data.MappingDataLoader; -import com.viaversion.viaversion.api.platform.TaskId; +import com.viaversion.viaversion.api.platform.PlatformTask; import com.viaversion.viaversion.api.platform.ViaPlatform; import com.viaversion.viaversion.bungee.commands.BungeeCommand; import com.viaversion.viaversion.bungee.commands.BungeeCommandHandler; @@ -41,6 +36,11 @@ import com.viaversion.viaversion.bungee.platform.BungeeViaLoader; import com.viaversion.viaversion.bungee.service.ProtocolDetectorService; import com.viaversion.viaversion.dump.PluginInfo; import com.viaversion.viaversion.util.GsonUtil; +import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.plugin.Listener; +import net.md_5.bungee.api.plugin.Plugin; +import net.md_5.bungee.protocol.ProtocolConstants; import java.util.ArrayList; import java.util.Collection; @@ -112,32 +112,23 @@ public class BungeePlugin extends Plugin implements ViaPlatform, } @Override - public TaskId runAsync(Runnable runnable) { - return new BungeeTaskId(getProxy().getScheduler().runAsync(this, runnable).getId()); + public PlatformTask runAsync(Runnable runnable) { + return new BungeeTaskId(getProxy().getScheduler().runAsync(this, runnable)); } @Override - public TaskId runSync(Runnable runnable) { + public PlatformTask runSync(Runnable runnable) { return runAsync(runnable); } @Override - public TaskId runSync(Runnable runnable, Long ticks) { - return new BungeeTaskId(getProxy().getScheduler().schedule(this, runnable, ticks * 50, TimeUnit.MILLISECONDS).getId()); + public PlatformTask runSync(Runnable runnable, Long ticks) { + return new BungeeTaskId(getProxy().getScheduler().schedule(this, runnable, ticks * 50, TimeUnit.MILLISECONDS)); } @Override - public TaskId runRepeatingSync(Runnable runnable, Long ticks) { - return new BungeeTaskId(getProxy().getScheduler().schedule(this, runnable, 0, ticks * 50, TimeUnit.MILLISECONDS).getId()); - } - - @Override - public void cancelTask(TaskId taskId) { - if (taskId == null) return; - if (taskId.getObject() == null) return; - if (taskId instanceof BungeeTaskId) { - getProxy().getScheduler().cancel((Integer) taskId.getObject()); - } + public PlatformTask runRepeatingSync(Runnable runnable, Long ticks) { + return new BungeeTaskId(getProxy().getScheduler().schedule(this, runnable, 0, ticks * 50, TimeUnit.MILLISECONDS)); } @Override diff --git a/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java b/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java index 078580108..5711ed89a 100644 --- a/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java +++ b/bungee/src/main/java/com/viaversion/viaversion/bungee/handlers/BungeeServerHandler.java @@ -21,7 +21,7 @@ import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.ProtocolInfo; import com.viaversion.viaversion.api.connection.StoredObject; import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.data.ExternalJoinGameListener; +import com.viaversion.viaversion.api.platform.ExternalJoinGameListener; import com.viaversion.viaversion.api.protocol.ProtocolPathEntry; import com.viaversion.viaversion.api.protocol.ProtocolPipeline; import com.viaversion.viaversion.api.protocol.base.Protocol; diff --git a/bungee/src/main/java/com/viaversion/viaversion/bungee/platform/BungeeTaskId.java b/bungee/src/main/java/com/viaversion/viaversion/bungee/platform/BungeeTaskId.java index 14fd9c2e3..733f48632 100644 --- a/bungee/src/main/java/com/viaversion/viaversion/bungee/platform/BungeeTaskId.java +++ b/bungee/src/main/java/com/viaversion/viaversion/bungee/platform/BungeeTaskId.java @@ -17,17 +17,23 @@ */ package com.viaversion.viaversion.bungee.platform; -import com.viaversion.viaversion.api.platform.TaskId; +import com.viaversion.viaversion.api.platform.PlatformTask; +import net.md_5.bungee.api.scheduler.ScheduledTask; -public class BungeeTaskId implements TaskId { - private final Integer object; +public class BungeeTaskId implements PlatformTask { + private final ScheduledTask task; - public BungeeTaskId(Integer object) { - this.object = object; + public BungeeTaskId(ScheduledTask task) { + this.task = task; } @Override - public Integer getObject() { - return object; + public ScheduledTask getObject() { + return task; + } + + @Override + public void cancel() { + task.cancel(); } } diff --git a/common/src/main/java/com/viaversion/viaversion/ViaManagerImpl.java b/common/src/main/java/com/viaversion/viaversion/ViaManagerImpl.java index fa7fa4b53..7faa1e848 100644 --- a/common/src/main/java/com/viaversion/viaversion/ViaManagerImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/ViaManagerImpl.java @@ -20,7 +20,7 @@ package com.viaversion.viaversion; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.ViaManager; import com.viaversion.viaversion.api.connection.ConnectionManager; -import com.viaversion.viaversion.api.platform.TaskId; +import com.viaversion.viaversion.api.platform.PlatformTask; import com.viaversion.viaversion.api.platform.UnsupportedSoftware; import com.viaversion.viaversion.api.platform.ViaInjector; import com.viaversion.viaversion.api.platform.ViaPlatform; @@ -55,7 +55,7 @@ public class ViaManagerImpl implements ViaManager { private final ViaPlatformLoader loader; private final Set subPlatforms = new HashSet<>(); private List enableListeners = new ArrayList<>(); - private TaskId mappingLoadingTask; + private PlatformTask mappingLoadingTask; private boolean debug; public ViaManagerImpl(ViaPlatform platform, ViaInjector injector, ViaCommandHandler commandHandler, ViaPlatformLoader loader) { diff --git a/common/src/main/java/com/viaversion/viaversion/connection/ProtocolInfoImpl.java b/common/src/main/java/com/viaversion/viaversion/connection/ProtocolInfoImpl.java index f2781ef00..e5ca9af26 100644 --- a/common/src/main/java/com/viaversion/viaversion/connection/ProtocolInfoImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/connection/ProtocolInfoImpl.java @@ -28,7 +28,6 @@ import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.protocol.ProtocolPipeline; import com.viaversion.viaversion.api.protocol.packet.State; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; -import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import java.util.UUID; @@ -78,7 +77,7 @@ public class ProtocolInfoImpl extends StoredObject implements ProtocolInfo { } @Override - public @MonotonicNonNull String getUsername() { + public String getUsername() { return username; } diff --git a/common/src/main/java/com/viaversion/viaversion/data/EntityTracker.java b/common/src/main/java/com/viaversion/viaversion/data/EntityTracker.java index 839934556..3438321da 100644 --- a/common/src/main/java/com/viaversion/viaversion/data/EntityTracker.java +++ b/common/src/main/java/com/viaversion/viaversion/data/EntityTracker.java @@ -18,7 +18,7 @@ package com.viaversion.viaversion.data; import org.checkerframework.checker.nullness.qual.Nullable; -import com.viaversion.viaversion.api.data.ExternalJoinGameListener; +import com.viaversion.viaversion.api.platform.ExternalJoinGameListener; import com.viaversion.viaversion.api.connection.StoredObject; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.entities.EntityType; diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java index 3667df8af..81d610e52 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java @@ -68,6 +68,7 @@ import io.netty.buffer.ByteBuf; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import org.checkerframework.checker.nullness.qual.Nullable; +import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import java.util.ArrayList; import java.util.Arrays; @@ -318,6 +319,8 @@ public class ProtocolManagerImpl implements ProtocolManager { public void setServerProtocol(ServerProtocolVersion serverProtocolVersion) { this.serverProtocolVersion = serverProtocolVersion; + //noinspection deprecation + ProtocolRegistry.SERVER_PROTOCOL = serverProtocolVersion.lowestSupportedVersion(); } @Override diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java index 2f95ff6c9..517516667 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java @@ -17,17 +17,15 @@ */ package com.viaversion.viaversion.protocols.protocol1_13_1to1_13; -import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.data.MappingData; import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.data.MappingData; +import com.viaversion.viaversion.api.data.MappingDataBase; import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.Protocol; +import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; import com.viaversion.viaversion.api.protocol.remapper.ValueTransformer; -import com.viaversion.viaversion.rewriter.RegistryType; -import com.viaversion.viaversion.rewriter.StatisticsRewriter; -import com.viaversion.viaversion.rewriter.TagRewriter; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.metadata.MetadataRewriter1_13_1To1_13; import com.viaversion.viaversion.protocols.protocol1_13_1to1_13.packets.EntityPackets; @@ -37,10 +35,13 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPacke import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13; import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; +import com.viaversion.viaversion.rewriter.RegistryType; +import com.viaversion.viaversion.rewriter.StatisticsRewriter; +import com.viaversion.viaversion.rewriter.TagRewriter; public class Protocol1_13_1To1_13 extends Protocol { - public static final MappingData MAPPINGS = new MappingData("1.13", "1.13.2", true); + public static final MappingData MAPPINGS = new MappingDataBase("1.13", "1.13.2", true); public Protocol1_13_1To1_13() { super(ClientboundPackets1_13.class, ClientboundPackets1_13.class, ServerboundPackets1_13.class, ServerboundPackets1_13.class); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/MappingData.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/MappingData.java index 8075e87ad..1ce2ea489 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/MappingData.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/data/MappingData.java @@ -24,11 +24,13 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; -import org.checkerframework.checker.nullness.qual.Nullable; import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.data.IntArrayMappings; +import com.viaversion.viaversion.api.data.MappingDataBase; import com.viaversion.viaversion.api.data.MappingDataLoader; import com.viaversion.viaversion.api.data.Mappings; import com.viaversion.viaversion.util.GsonUtil; +import org.checkerframework.checker.nullness.qual.Nullable; import java.io.IOException; import java.io.InputStreamReader; @@ -37,7 +39,7 @@ import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; -public class MappingData extends com.viaversion.viaversion.api.data.MappingData { +public class MappingData extends MappingDataBase { private final Map blockTags = new HashMap<>(); private final Map itemTags = new HashMap<>(); private final Map fluidTags = new HashMap<>(); @@ -58,22 +60,21 @@ public class MappingData extends com.viaversion.viaversion.api.data.MappingData loadTags(fluidTags, newMappings.getAsJsonObject("fluid_tags")); loadEnchantments(oldEnchantmentsIds, oldMappings.getAsJsonObject("enchantments")); - enchantmentMappings = new Mappings(72, oldMappings.getAsJsonObject("enchantments"), newMappings.getAsJsonObject("enchantments")); + enchantmentMappings = new IntArrayMappings(72, oldMappings.getAsJsonObject("enchantments"), newMappings.getAsJsonObject("enchantments")); // Map minecraft:snow[layers=1] of 1.12 to minecraft:snow[layers=2] in 1.13 if (Via.getConfig().isSnowCollisionFix()) { - blockMappings.getOldToNew()[1248] = 3416; + blockMappings.setNewId(1248, 3416); } // Remap infested blocks, as they are instantly breakabale in 1.13+ and can't be broken by those clients on older servers if (Via.getConfig().isInfestedBlocksFix()) { - int[] oldToNew = blockMappings.getOldToNew(); - oldToNew[1552] = 1; // stone - oldToNew[1553] = 14; // cobblestone - oldToNew[1554] = 3983; // stone bricks - oldToNew[1555] = 3984; // mossy stone bricks - oldToNew[1556] = 3985; // cracked stone bricks - oldToNew[1557] = 3986; // chiseled stone bricks + blockMappings.setNewId(1552, 1); // stone + blockMappings.setNewId(1553, 14); // cobblestone + blockMappings.setNewId(1554, 3983); // stone bricks + blockMappings.setNewId(1555, 3984); // mossy stone bricks + blockMappings.setNewId(1556, 3985); // cracked stone bricks + blockMappings.setNewId(1557, 3986); // chiseled stone bricks } JsonObject object = MappingDataLoader.loadFromDataDir("channelmappings-1.13.json"); @@ -125,7 +126,7 @@ public class MappingData extends com.viaversion.viaversion.api.data.MappingData protected Mappings loadFromObject(JsonObject oldMappings, JsonObject newMappings, @Nullable JsonObject diffMappings, String key) { if (key.equals("blocks")) { // Need to use a custom size since there are larger gaps in ids - return new Mappings(4084, oldMappings.getAsJsonObject("blocks"), newMappings.getAsJsonObject("blockstates")); + return new IntArrayMappings(4084, oldMappings.getAsJsonObject("blocks"), newMappings.getAsJsonObject("blockstates")); } else { return super.loadFromObject(oldMappings, newMappings, diffMappings, key); } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/data/MappingData.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/data/MappingData.java index 8970b23c4..a094b0d9c 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/data/MappingData.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/data/MappingData.java @@ -20,6 +20,7 @@ package com.viaversion.viaversion.protocols.protocol1_14to1_13_2.data; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.viaversion.viaversion.api.data.MappingDataBase; import it.unimi.dsi.fastutil.ints.IntOpenHashSet; import it.unimi.dsi.fastutil.ints.IntSet; import com.viaversion.viaversion.api.Via; @@ -28,7 +29,7 @@ import com.viaversion.viaversion.api.data.MappingDataLoader; import java.util.HashMap; import java.util.Map; -public class MappingData extends com.viaversion.viaversion.api.data.MappingData { +public class MappingData extends MappingDataBase { private IntSet motionBlocking; private IntSet nonFullBlocks; diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/data/MappingData.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/data/MappingData.java index 41d44d9e8..033395b43 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/data/MappingData.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_15to1_14_4/data/MappingData.java @@ -18,10 +18,12 @@ package com.viaversion.viaversion.protocols.protocol1_15to1_14_4.data; import com.google.gson.JsonObject; +import com.viaversion.viaversion.api.data.IntArrayMappings; +import com.viaversion.viaversion.api.data.MappingDataBase; import org.checkerframework.checker.nullness.qual.Nullable; import com.viaversion.viaversion.api.data.Mappings; -public class MappingData extends com.viaversion.viaversion.api.data.MappingData { +public class MappingData extends MappingDataBase { public MappingData() { super("1.14", "1.15", true); @@ -34,6 +36,6 @@ public class MappingData extends com.viaversion.viaversion.api.data.MappingData } // Ignore removed sounds - return new Mappings(oldMappings.getAsJsonArray(key), newMappings.getAsJsonArray(key), false); + return new IntArrayMappings(oldMappings.getAsJsonArray(key), newMappings.getAsJsonArray(key), false); } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/data/MappingData.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/data/MappingData.java index a1305368f..f3dd7072e 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/data/MappingData.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16_2to1_16_1/data/MappingData.java @@ -23,6 +23,7 @@ import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.Tag; import com.google.gson.JsonObject; import com.viaversion.viaversion.api.Via; +import com.viaversion.viaversion.api.data.MappingDataBase; import com.viaversion.viaversion.api.data.MappingDataLoader; import com.viaversion.viaversion.api.minecraft.nbt.BinaryTagIO; @@ -30,7 +31,7 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -public class MappingData extends com.viaversion.viaversion.api.data.MappingData { +public class MappingData extends MappingDataBase { private final Map dimensionDataMap = new HashMap<>(); private CompoundTag dimensionRegistry; diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/MappingData.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/MappingData.java index 33be63a23..cfdc2c506 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/MappingData.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/data/MappingData.java @@ -20,8 +20,9 @@ package com.viaversion.viaversion.protocols.protocol1_16to1_15_2.data; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.google.gson.JsonObject; +import com.viaversion.viaversion.api.data.MappingDataBase; -public class MappingData extends com.viaversion.viaversion.api.data.MappingData { +public class MappingData extends MappingDataBase { private final BiMap attributeMappings = HashBiMap.create(); public MappingData() { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java index dc84fe8e0..9f4b361f0 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_17to1_16_4/Protocol1_17To1_16_4.java @@ -18,16 +18,12 @@ package com.viaversion.viaversion.protocols.protocol1_17to1_16_4; import com.viaversion.viaversion.api.Via; -import com.viaversion.viaversion.api.data.MappingData; import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; +import com.viaversion.viaversion.api.data.MappingData; +import com.viaversion.viaversion.api.data.MappingDataBase; import com.viaversion.viaversion.api.protocol.Protocol; +import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.remapper.PacketRemapper; -import com.viaversion.viaversion.rewriter.MetadataRewriter; -import com.viaversion.viaversion.rewriter.RegistryType; -import com.viaversion.viaversion.rewriter.SoundRewriter; -import com.viaversion.viaversion.rewriter.StatisticsRewriter; -import com.viaversion.viaversion.rewriter.TagRewriter; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2; import com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ServerboundPackets1_16_2; @@ -37,10 +33,15 @@ import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.Inventor import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.packets.WorldPackets; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.storage.BiomeStorage; import com.viaversion.viaversion.protocols.protocol1_17to1_16_4.storage.EntityTracker1_17; +import com.viaversion.viaversion.rewriter.MetadataRewriter; +import com.viaversion.viaversion.rewriter.RegistryType; +import com.viaversion.viaversion.rewriter.SoundRewriter; +import com.viaversion.viaversion.rewriter.StatisticsRewriter; +import com.viaversion.viaversion.rewriter.TagRewriter; public class Protocol1_17To1_16_4 extends Protocol { - public static final MappingData MAPPINGS = new MappingData("1.16.2", "1.17", true); + public static final MappingData MAPPINGS = new MappingDataBase("1.16.2", "1.17", true); private static final String[] NEW_GAME_EVENT_TAGS = {"minecraft:ignore_vibrations_sneaking", "minecraft:vibrations"}; private TagRewriter tagRewriter; diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java index 01370173c..c891db544 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/TagRewriter.java @@ -17,11 +17,11 @@ */ package com.viaversion.viaversion.rewriter; +import com.viaversion.viaversion.api.data.MappingData; import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.ints.IntList; import org.checkerframework.checker.nullness.qual.Nullable; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; -import com.viaversion.viaversion.api.data.MappingData; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; diff --git a/sponge/src/main/java/com/viaversion/viaversion/SpongePlugin.java b/sponge/src/main/java/com/viaversion/viaversion/SpongePlugin.java index ed5636223..8f5572f84 100644 --- a/sponge/src/main/java/com/viaversion/viaversion/SpongePlugin.java +++ b/sponge/src/main/java/com/viaversion/viaversion/SpongePlugin.java @@ -19,24 +19,14 @@ package com.viaversion.viaversion; import com.google.gson.JsonObject; import com.google.inject.Inject; -import org.spongepowered.api.Game; -import org.spongepowered.api.config.DefaultConfig; -import org.spongepowered.api.entity.living.player.Player; -import org.spongepowered.api.event.Listener; -import org.spongepowered.api.event.game.state.GameAboutToStartServerEvent; -import org.spongepowered.api.event.game.state.GameInitializationEvent; -import org.spongepowered.api.event.game.state.GameStoppingServerEvent; -import org.spongepowered.api.plugin.Plugin; -import org.spongepowered.api.plugin.PluginContainer; -import org.spongepowered.api.scheduler.Task; -import org.spongepowered.api.text.serializer.TextSerializers; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.command.ViaCommandSender; import com.viaversion.viaversion.api.configuration.ConfigurationProvider; import com.viaversion.viaversion.api.data.MappingDataLoader; -import com.viaversion.viaversion.api.platform.TaskId; +import com.viaversion.viaversion.api.platform.PlatformTask; import com.viaversion.viaversion.api.platform.ViaPlatform; import com.viaversion.viaversion.dump.PluginInfo; +import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import com.viaversion.viaversion.sponge.commands.SpongeCommandHandler; import com.viaversion.viaversion.sponge.commands.SpongeCommandSender; import com.viaversion.viaversion.sponge.platform.SpongeTaskId; @@ -48,7 +38,17 @@ import com.viaversion.viaversion.sponge.util.LoggerWrapper; import com.viaversion.viaversion.util.ChatColorUtil; import com.viaversion.viaversion.util.GsonUtil; import com.viaversion.viaversion.util.VersionInfo; -import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import org.spongepowered.api.Game; +import org.spongepowered.api.config.DefaultConfig; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.game.state.GameAboutToStartServerEvent; +import org.spongepowered.api.event.game.state.GameInitializationEvent; +import org.spongepowered.api.event.game.state.GameStoppingServerEvent; +import org.spongepowered.api.plugin.Plugin; +import org.spongepowered.api.plugin.PluginContainer; +import org.spongepowered.api.scheduler.Task; +import org.spongepowered.api.text.serializer.TextSerializers; import java.io.File; import java.util.ArrayList; @@ -127,7 +127,7 @@ public class SpongePlugin implements ViaPlatform { } @Override - public TaskId runAsync(Runnable runnable) { + public PlatformTask runAsync(Runnable runnable) { return new SpongeTaskId( Task.builder() .execute(runnable) @@ -137,7 +137,7 @@ public class SpongePlugin implements ViaPlatform { } @Override - public TaskId runSync(Runnable runnable) { + public PlatformTask runSync(Runnable runnable) { return new SpongeTaskId( Task.builder() .execute(runnable) @@ -146,7 +146,7 @@ public class SpongePlugin implements ViaPlatform { } @Override - public TaskId runSync(Runnable runnable, Long ticks) { + public PlatformTask runSync(Runnable runnable, Long ticks) { return new SpongeTaskId( Task.builder() .execute(runnable) @@ -156,7 +156,7 @@ public class SpongePlugin implements ViaPlatform { } @Override - public TaskId runRepeatingSync(Runnable runnable, Long ticks) { + public PlatformTask runRepeatingSync(Runnable runnable, Long ticks) { return new SpongeTaskId( Task.builder() .execute(runnable) @@ -165,15 +165,6 @@ public class SpongePlugin implements ViaPlatform { ); } - @Override - public void cancelTask(TaskId taskId) { - if (taskId == null) return; - if (taskId.getObject() == null) return; - if (taskId instanceof SpongeTaskId) { - ((SpongeTaskId) taskId).getObject().cancel(); - } - } - @Override public ViaCommandSender[] getOnlinePlayers() { ViaCommandSender[] array = new ViaCommandSender[game.getServer().getOnlinePlayers().size()]; diff --git a/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeTaskId.java b/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeTaskId.java index 4bd53cad7..da5b6d2b0 100644 --- a/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeTaskId.java +++ b/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeTaskId.java @@ -18,17 +18,22 @@ package com.viaversion.viaversion.sponge.platform; import org.spongepowered.api.scheduler.Task; -import com.viaversion.viaversion.api.platform.TaskId; +import com.viaversion.viaversion.api.platform.PlatformTask; -public class SpongeTaskId implements TaskId { - private final Task object; +public class SpongeTaskId implements PlatformTask { + private final Task task; - public SpongeTaskId(Task object) { - this.object = object; + public SpongeTaskId(Task task) { + this.task = task; } @Override public Task getObject() { - return object; + return task; + } + + @Override + public void cancel() { + task.cancel(); } } diff --git a/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaLoader.java b/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaLoader.java index 1e2bf77e5..f5d92c8fa 100644 --- a/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaLoader.java +++ b/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaLoader.java @@ -22,7 +22,7 @@ import com.viaversion.viaversion.SpongePlugin; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.item.Item; -import com.viaversion.viaversion.api.platform.TaskId; +import com.viaversion.viaversion.api.platform.PlatformTask; import com.viaversion.viaversion.api.platform.ViaPlatformLoader; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.BulkChunkTranslatorProvider; @@ -45,7 +45,7 @@ public class SpongeViaLoader implements ViaPlatformLoader { private final SpongePlugin plugin; private final Set listeners = new HashSet<>(); - private final Set tasks = new HashSet<>(); + private final Set tasks = new HashSet<>(); public SpongeViaLoader(SpongePlugin plugin) { this.plugin = plugin; diff --git a/velocity/src/main/java/com/viaversion/viaversion/VelocityPlugin.java b/velocity/src/main/java/com/viaversion/viaversion/VelocityPlugin.java index 35b4f46cb..3edadc210 100644 --- a/velocity/src/main/java/com/viaversion/viaversion/VelocityPlugin.java +++ b/velocity/src/main/java/com/viaversion/viaversion/VelocityPlugin.java @@ -27,13 +27,11 @@ import com.velocitypowered.api.plugin.PluginContainer; import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; -import org.slf4j.Logger; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.command.ViaCommandSender; import com.viaversion.viaversion.api.configuration.ConfigurationProvider; import com.viaversion.viaversion.api.data.MappingDataLoader; -import com.viaversion.viaversion.api.platform.TaskId; +import com.viaversion.viaversion.api.platform.PlatformTask; import com.viaversion.viaversion.api.platform.ViaPlatform; import com.viaversion.viaversion.dump.PluginInfo; import com.viaversion.viaversion.util.ChatColorUtil; @@ -48,6 +46,8 @@ import com.viaversion.viaversion.velocity.platform.VelocityViaInjector; import com.viaversion.viaversion.velocity.platform.VelocityViaLoader; import com.viaversion.viaversion.velocity.service.ProtocolDetectorService; import com.viaversion.viaversion.velocity.util.LoggerWrapper; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import org.slf4j.Logger; import java.io.File; import java.nio.file.Path; @@ -127,17 +127,17 @@ public class VelocityPlugin implements ViaPlatform { } @Override - public TaskId runAsync(Runnable runnable) { + public PlatformTask runAsync(Runnable runnable) { return runSync(runnable); } @Override - public TaskId runSync(Runnable runnable) { + public PlatformTask runSync(Runnable runnable) { return runSync(runnable, 0L); } @Override - public TaskId runSync(Runnable runnable, Long ticks) { + public PlatformTask runSync(Runnable runnable, Long ticks) { return new VelocityTaskId( PROXY.getScheduler() .buildTask(this, runnable) @@ -146,7 +146,7 @@ public class VelocityPlugin implements ViaPlatform { } @Override - public TaskId runRepeatingSync(Runnable runnable, Long ticks) { + public PlatformTask runRepeatingSync(Runnable runnable, Long ticks) { return new VelocityTaskId( PROXY.getScheduler() .buildTask(this, runnable) @@ -154,13 +154,6 @@ public class VelocityPlugin implements ViaPlatform { ); } - @Override - public void cancelTask(TaskId taskId) { - if (taskId instanceof VelocityTaskId) { - ((VelocityTaskId) taskId).getObject().cancel(); - } - } - @Override public ViaCommandSender[] getOnlinePlayers() { return PROXY.getAllPlayers().stream() diff --git a/velocity/src/main/java/com/viaversion/viaversion/velocity/platform/VelocityTaskId.java b/velocity/src/main/java/com/viaversion/viaversion/velocity/platform/VelocityTaskId.java index 776e01c51..c7560ecf9 100644 --- a/velocity/src/main/java/com/viaversion/viaversion/velocity/platform/VelocityTaskId.java +++ b/velocity/src/main/java/com/viaversion/viaversion/velocity/platform/VelocityTaskId.java @@ -18,17 +18,22 @@ package com.viaversion.viaversion.velocity.platform; import com.velocitypowered.api.scheduler.ScheduledTask; -import com.viaversion.viaversion.api.platform.TaskId; +import com.viaversion.viaversion.api.platform.PlatformTask; -public class VelocityTaskId implements TaskId { - private final ScheduledTask object; +public class VelocityTaskId implements PlatformTask { + private final ScheduledTask task; - public VelocityTaskId(ScheduledTask object) { - this.object = object; + public VelocityTaskId(ScheduledTask task) { + this.task = task; } @Override public ScheduledTask getObject() { - return object; + return task; + } + + @Override + public void cancel() { + task.cancel(); } }