From 890130b46025667e7d7d4ce2ba870dd2ddba1841 Mon Sep 17 00:00:00 2001 From: Martoph Date: Mon, 14 Sep 2020 19:39:43 +1000 Subject: [PATCH] #746: Add Fluid types / tags --- .../org/bukkit/craftbukkit/CraftServer.java | 11 ++++++++ .../bukkit/craftbukkit/tag/CraftFluidTag.java | 27 +++++++++++++++++++ .../craftbukkit/util/CraftMagicNumbers.java | 20 ++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 src/main/java/org/bukkit/craftbukkit/tag/CraftFluidTag.java diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 3607707c1b..1b5c92469b 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -68,6 +68,7 @@ import net.minecraft.server.Enchantments; import net.minecraft.server.EntityPlayer; import net.minecraft.server.EnumDifficulty; import net.minecraft.server.EnumGamemode; +import net.minecraft.server.FluidType; import net.minecraft.server.GameRules; import net.minecraft.server.GeneratorSettings; import net.minecraft.server.IRecipe; @@ -166,6 +167,7 @@ import org.bukkit.craftbukkit.potion.CraftPotionBrewer; import org.bukkit.craftbukkit.scheduler.CraftScheduler; import org.bukkit.craftbukkit.scoreboard.CraftScoreboardManager; import org.bukkit.craftbukkit.tag.CraftBlockTag; +import org.bukkit.craftbukkit.tag.CraftFluidTag; import org.bukkit.craftbukkit.tag.CraftItemTag; import org.bukkit.craftbukkit.util.CraftChatMessage; import org.bukkit.craftbukkit.util.CraftIconCache; @@ -1953,6 +1955,10 @@ public final class CraftServer implements Server { Preconditions.checkArgument(clazz == org.bukkit.Material.class, "Item namespace must have material type"); return (org.bukkit.Tag) new CraftItemTag(console.getTagRegistry().getItemTags(), key); + case org.bukkit.Tag.REGISTRY_FLUIDS: + Preconditions.checkArgument(clazz == org.bukkit.Fluid.class, "Fluid namespace must have fluid type"); + + return (org.bukkit.Tag) new CraftFluidTag(console.getTagRegistry().getFluidTags(), key); default: throw new IllegalArgumentException(); } @@ -1972,6 +1978,11 @@ public final class CraftServer implements Server { Tags itemTags = console.getTagRegistry().getItemTags(); return itemTags.a().keySet().stream().map(key -> (org.bukkit.Tag) new CraftItemTag(itemTags, key)).collect(ImmutableList.toImmutableList()); + case org.bukkit.Tag.REGISTRY_FLUIDS: + Preconditions.checkArgument(clazz == org.bukkit.Material.class, "Fluid namespace must have fluid type"); + + Tags fluidTags = console.getTagRegistry().getFluidTags(); + return fluidTags.a().keySet().stream().map(key -> (org.bukkit.Tag) new CraftFluidTag(fluidTags, key)).collect(ImmutableList.toImmutableList()); default: throw new IllegalArgumentException(); } diff --git a/src/main/java/org/bukkit/craftbukkit/tag/CraftFluidTag.java b/src/main/java/org/bukkit/craftbukkit/tag/CraftFluidTag.java new file mode 100644 index 0000000000..d7b2bce1a1 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/tag/CraftFluidTag.java @@ -0,0 +1,27 @@ +package org.bukkit.craftbukkit.tag; + +import java.util.Collections; +import java.util.Set; +import java.util.stream.Collectors; +import net.minecraft.server.FluidType; +import net.minecraft.server.MinecraftKey; +import net.minecraft.server.Tags; +import org.bukkit.Fluid; +import org.bukkit.craftbukkit.util.CraftMagicNumbers; + +public class CraftFluidTag extends CraftTag { + + public CraftFluidTag(Tags registry, MinecraftKey tag) { + super(registry, tag); + } + + @Override + public boolean isTagged(Fluid fluid) { + return getHandle().isTagged(CraftMagicNumbers.getFluid(fluid)); + } + + @Override + public Set getValues() { + return Collections.unmodifiableSet(getHandle().getTagged().stream().map(CraftMagicNumbers::getFluid).collect(Collectors.toSet())); + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java index 4d8cd8234a..983444d7b7 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java @@ -24,6 +24,7 @@ import net.minecraft.server.ChatDeserializer; import net.minecraft.server.DataConverterRegistry; import net.minecraft.server.DataConverterTypes; import net.minecraft.server.DynamicOpsNBT; +import net.minecraft.server.FluidType; import net.minecraft.server.IBlockData; import net.minecraft.server.IRegistry; import net.minecraft.server.Item; @@ -37,8 +38,10 @@ import net.minecraft.server.NBTTagString; import net.minecraft.server.SavedFile; import net.minecraft.server.SharedConstants; import org.bukkit.Bukkit; +import org.bukkit.Fluid; import org.bukkit.Material; import org.bukkit.NamespacedKey; +import org.bukkit.Registry; import org.bukkit.UnsafeValues; import org.bukkit.advancement.Advancement; import org.bukkit.block.data.BlockData; @@ -83,8 +86,10 @@ public final class CraftMagicNumbers implements UnsafeValues { // ======================================================================== private static final Map BLOCK_MATERIAL = new HashMap<>(); private static final Map ITEM_MATERIAL = new HashMap<>(); + private static final Map FLUID_MATERIAL = new HashMap<>(); private static final Map MATERIAL_ITEM = new HashMap<>(); private static final Map MATERIAL_BLOCK = new HashMap<>(); + private static final Map MATERIAL_FLUID = new HashMap<>(); static { for (Block block : IRegistry.BLOCK) { @@ -95,6 +100,10 @@ public final class CraftMagicNumbers implements UnsafeValues { ITEM_MATERIAL.put(item, Material.getMaterial(IRegistry.ITEM.getKey(item).getKey().toUpperCase(Locale.ROOT))); } + for (FluidType fluid : IRegistry.FLUID) { + FLUID_MATERIAL.put(fluid, Registry.FLUID.get(CraftNamespacedKey.fromMinecraft(IRegistry.FLUID.getKey(fluid)))); + } + for (Material material : Material.values()) { if (material.isLegacy()) { continue; @@ -107,6 +116,9 @@ public final class CraftMagicNumbers implements UnsafeValues { IRegistry.BLOCK.getOptional(key).ifPresent((block) -> { MATERIAL_BLOCK.put(material, block); }); + IRegistry.FLUID.getOptional(key).ifPresent((fluid) -> { + MATERIAL_FLUID.put(material, fluid); + }); } } @@ -118,6 +130,10 @@ public final class CraftMagicNumbers implements UnsafeValues { return ITEM_MATERIAL.getOrDefault(item, Material.AIR); } + public static Fluid getFluid(FluidType fluid) { + return FLUID_MATERIAL.get(fluid); + } + public static Item getItem(Material material) { if (material != null && material.isLegacy()) { material = CraftLegacy.fromLegacy(material); @@ -134,6 +150,10 @@ public final class CraftMagicNumbers implements UnsafeValues { return MATERIAL_BLOCK.get(material); } + public static FluidType getFluid(Fluid fluid) { + return MATERIAL_FLUID.get(fluid); + } + public static MinecraftKey key(Material mat) { return CraftNamespacedKey.toMinecraft(mat.getKey()); }