From ef34f88c6aa05280d3ed9869c0685945a9cfdae6 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Sat, 1 May 2021 23:50:51 +0800 Subject: [PATCH] Dynamically switch between legacy and standard material. --- pom.xml | 14 ++++++++ .../utils/CompatibilityLayer.java | 9 ++++++ .../utils/MaterialConverter.java | 32 ++++++++++++++++--- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index a6fee72d..f2ec9860 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,10 @@ minebench-repo https://repo.minebench.de/ + + papermc + https://papermc.io/repo/repository/maven-public/ + @@ -253,6 +257,10 @@ de.themoep.idconverter com.onarandombox.idconverter + + io.papermc.lib + com.onarandombox.paperlib + @@ -339,6 +347,12 @@ mappings 1.2-SNAPSHOT + + io.papermc + paperlib + 1.0.6 + compile + diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/CompatibilityLayer.java b/src/main/java/com/onarandombox/MultiverseCore/utils/CompatibilityLayer.java index 0126555e..99e6044f 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/CompatibilityLayer.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/CompatibilityLayer.java @@ -1,6 +1,7 @@ package com.onarandombox.MultiverseCore.utils; import com.dumptruckman.minecraft.util.Logging; +import io.papermc.lib.PaperLib; import org.bukkit.entity.Entity; import org.bukkit.entity.Ghast; import org.bukkit.entity.Monster; @@ -124,4 +125,12 @@ public class CompatibilityLayer { || entity instanceof Slime || (phantomClass != null && phantomClass.isInstance(entity))); } + + /** + * + * @return True if using legacy material ids, else false. + */ + public static boolean isUsingLegacyMaterials() { + return TestingMode.isDisabled() && PaperLib.getMinecraftVersion() < 13; + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/MaterialConverter.java b/src/main/java/com/onarandombox/MultiverseCore/utils/MaterialConverter.java index 0fd19457..566a17e8 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/MaterialConverter.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/MaterialConverter.java @@ -32,11 +32,33 @@ public class MaterialConverter { */ @Nullable public static Material convertTypeString(@Nullable String value) { - IdMappings.Mapping mapping = IdMappings.getById(value != null ? value : ""); - if (mapping != null) { - return Material.matchMaterial(mapping.getFlatteningType()); - } else { - return Material.matchMaterial(value != null ? value : ""); + String targetValue = value != null ? value : ""; + + IdMappings.Mapping idMapping = IdMappings.getById(targetValue); + if (idMapping != null) { + return getMappedType(idMapping); } + IdMappings.Mapping flattenMapping = IdMappings.getByFlatteningType(targetValue); + if (flattenMapping != null) { + return getMappedType(flattenMapping); + } + IdMappings.Mapping legacyMapping = IdMappings.getByLegacyType(targetValue); + if (legacyMapping != null) { + return getMappedType(legacyMapping); + } + + return Material.matchMaterial(targetValue); + } + + /** + * Gets flattened or legacy material type based on what the server supports. + * + * @param mapping Dynamic material mapping provided by {@link IdMappings}. + * @return Material of the mapping. + */ + public static Material getMappedType(IdMappings.Mapping mapping) { + return CompatibilityLayer.isUsingLegacyMaterials() + ? Material.matchMaterial(mapping.getLegacyType()) + : Material.matchMaterial(mapping.getFlatteningType()); } }