SPIGOT-5753: Back PotionType by a minecraft registry
By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
@@ -1,13 +1,8 @@
|
||||
package org.bukkit.craftbukkit.potion;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.base.Preconditions;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import net.minecraft.world.effect.MobEffect;
|
||||
import net.minecraft.world.item.alchemy.PotionRegistry;
|
||||
import org.bukkit.potion.PotionBrewer;
|
||||
import org.bukkit.potion.PotionData;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
@@ -15,23 +10,13 @@ import org.bukkit.potion.PotionEffectType;
|
||||
import org.bukkit.potion.PotionType;
|
||||
|
||||
public class CraftPotionBrewer implements PotionBrewer {
|
||||
private static final Map<PotionType, Collection<PotionEffect>> cache = Maps.newHashMap();
|
||||
|
||||
@Override
|
||||
public Collection<PotionEffect> getEffects(PotionType damage, boolean upgraded, boolean extended) {
|
||||
if (cache.containsKey(damage))
|
||||
return cache.get(damage);
|
||||
public Collection<PotionEffect> getEffects(PotionType type, boolean upgraded, boolean extended) {
|
||||
Preconditions.checkArgument(!type.getKey().getKey().startsWith("strong_"), "Strong potion type cannot be used directly, got %s", type.getKey());
|
||||
Preconditions.checkArgument(!type.getKey().getKey().startsWith("long_"), "Extended potion type cannot be used directly, got %s", type.getKey());
|
||||
|
||||
List<MobEffect> mcEffects = PotionRegistry.byName(CraftPotionUtil.fromBukkit(new PotionData(damage, extended, upgraded))).getEffects();
|
||||
|
||||
ImmutableList.Builder<PotionEffect> builder = new ImmutableList.Builder<PotionEffect>();
|
||||
for (MobEffect effect : mcEffects) {
|
||||
builder.add(CraftPotionUtil.toBukkit(effect));
|
||||
}
|
||||
|
||||
cache.put(damage, builder.build());
|
||||
|
||||
return cache.get(damage);
|
||||
return CraftPotionUtil.fromBukkit(new PotionData(type, upgraded, extended)).getPotionEffects();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package org.bukkit.craftbukkit.potion;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.base.Suppliers;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
import net.minecraft.core.IRegistry;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.world.item.alchemy.PotionRegistry;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.craftbukkit.CraftRegistry;
|
||||
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
import org.bukkit.potion.PotionType;
|
||||
|
||||
public class CraftPotionType implements PotionType.InternalPotionData {
|
||||
|
||||
public static PotionType minecraftToBukkit(PotionRegistry minecraft) {
|
||||
Preconditions.checkArgument(minecraft != null);
|
||||
|
||||
IRegistry<PotionRegistry> registry = CraftRegistry.getMinecraftRegistry(Registries.POTION);
|
||||
PotionType bukkit = Registry.POTION.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
|
||||
|
||||
Preconditions.checkArgument(bukkit != null);
|
||||
|
||||
return bukkit;
|
||||
}
|
||||
|
||||
public static PotionRegistry bukkitToMinecraft(PotionType bukkit) {
|
||||
Preconditions.checkArgument(bukkit != null);
|
||||
|
||||
return CraftRegistry.getMinecraftRegistry(Registries.POTION)
|
||||
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
|
||||
}
|
||||
|
||||
public static String bukkitToString(PotionType potionType) {
|
||||
Preconditions.checkArgument(potionType != null);
|
||||
|
||||
return potionType.getKey().toString();
|
||||
}
|
||||
|
||||
public static PotionType stringToBukkit(String string) {
|
||||
Preconditions.checkArgument(string != null);
|
||||
|
||||
return Registry.POTION.get(NamespacedKey.fromString(string));
|
||||
}
|
||||
|
||||
private final NamespacedKey key;
|
||||
private final PotionRegistry potion;
|
||||
private final Supplier<List<PotionEffect>> potionEffects;
|
||||
private final Supplier<Boolean> upgradeable;
|
||||
private final Supplier<Boolean> extendable;
|
||||
private final Supplier<Integer> maxLevel;
|
||||
|
||||
public CraftPotionType(NamespacedKey key, PotionRegistry potion) {
|
||||
this.key = key;
|
||||
this.potion = potion;
|
||||
this.potionEffects = Suppliers.memoize(() -> potion.getEffects().stream().map(CraftPotionUtil::toBukkit).toList());
|
||||
this.upgradeable = Suppliers.memoize(() -> Registry.POTION.get(new NamespacedKey(key.getNamespace(), "strong_" + key.getKey())) != null);
|
||||
this.extendable = Suppliers.memoize(() -> Registry.POTION.get(new NamespacedKey(key.getNamespace(), "long_" + key.getKey())) != null);
|
||||
this.maxLevel = Suppliers.memoize(() -> isUpgradeable() ? 2 : 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PotionEffectType getEffectType() {
|
||||
return getPotionEffects().isEmpty() ? null : getPotionEffects().get(0).getType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PotionEffect> getPotionEffects() {
|
||||
return potionEffects.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInstant() {
|
||||
return potion.hasInstantEffects();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUpgradeable() {
|
||||
return upgradeable.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isExtendable() {
|
||||
return extendable.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxLevel() {
|
||||
return maxLevel.get();
|
||||
}
|
||||
}
|
||||
@@ -12,78 +12,53 @@ import org.bukkit.potion.PotionType;
|
||||
|
||||
public class CraftPotionUtil {
|
||||
|
||||
private static final BiMap<PotionType, String> regular = ImmutableBiMap.<PotionType, String>builder()
|
||||
.put(PotionType.UNCRAFTABLE, "empty")
|
||||
.put(PotionType.WATER, "water")
|
||||
.put(PotionType.MUNDANE, "mundane")
|
||||
.put(PotionType.THICK, "thick")
|
||||
.put(PotionType.AWKWARD, "awkward")
|
||||
.put(PotionType.NIGHT_VISION, "night_vision")
|
||||
.put(PotionType.INVISIBILITY, "invisibility")
|
||||
.put(PotionType.JUMP, "leaping")
|
||||
.put(PotionType.FIRE_RESISTANCE, "fire_resistance")
|
||||
.put(PotionType.SPEED, "swiftness")
|
||||
.put(PotionType.SLOWNESS, "slowness")
|
||||
.put(PotionType.WATER_BREATHING, "water_breathing")
|
||||
.put(PotionType.INSTANT_HEAL, "healing")
|
||||
.put(PotionType.INSTANT_DAMAGE, "harming")
|
||||
.put(PotionType.POISON, "poison")
|
||||
.put(PotionType.REGEN, "regeneration")
|
||||
.put(PotionType.STRENGTH, "strength")
|
||||
.put(PotionType.WEAKNESS, "weakness")
|
||||
.put(PotionType.LUCK, "luck")
|
||||
.put(PotionType.TURTLE_MASTER, "turtle_master")
|
||||
.put(PotionType.SLOW_FALLING, "slow_falling")
|
||||
private static final BiMap<PotionType, PotionType> upgradeable = ImmutableBiMap.<PotionType, PotionType>builder()
|
||||
.put(PotionType.JUMP, PotionType.STRONG_LEAPING)
|
||||
.put(PotionType.SPEED, PotionType.STRONG_SWIFTNESS)
|
||||
.put(PotionType.INSTANT_HEAL, PotionType.STRONG_HEALING)
|
||||
.put(PotionType.INSTANT_DAMAGE, PotionType.STRONG_HARMING)
|
||||
.put(PotionType.POISON, PotionType.STRONG_POISON)
|
||||
.put(PotionType.REGEN, PotionType.STRONG_REGENERATION)
|
||||
.put(PotionType.STRENGTH, PotionType.STRONG_STRENGTH)
|
||||
.put(PotionType.SLOWNESS, PotionType.STRONG_SLOWNESS)
|
||||
.put(PotionType.TURTLE_MASTER, PotionType.STRONG_TURTLE_MASTER)
|
||||
.build();
|
||||
private static final BiMap<PotionType, String> upgradeable = ImmutableBiMap.<PotionType, String>builder()
|
||||
.put(PotionType.JUMP, "strong_leaping")
|
||||
.put(PotionType.SPEED, "strong_swiftness")
|
||||
.put(PotionType.INSTANT_HEAL, "strong_healing")
|
||||
.put(PotionType.INSTANT_DAMAGE, "strong_harming")
|
||||
.put(PotionType.POISON, "strong_poison")
|
||||
.put(PotionType.REGEN, "strong_regeneration")
|
||||
.put(PotionType.STRENGTH, "strong_strength")
|
||||
.put(PotionType.SLOWNESS, "strong_slowness")
|
||||
.put(PotionType.TURTLE_MASTER, "strong_turtle_master")
|
||||
.build();
|
||||
private static final BiMap<PotionType, String> extendable = ImmutableBiMap.<PotionType, String>builder()
|
||||
.put(PotionType.NIGHT_VISION, "long_night_vision")
|
||||
.put(PotionType.INVISIBILITY, "long_invisibility")
|
||||
.put(PotionType.JUMP, "long_leaping")
|
||||
.put(PotionType.FIRE_RESISTANCE, "long_fire_resistance")
|
||||
.put(PotionType.SPEED, "long_swiftness")
|
||||
.put(PotionType.SLOWNESS, "long_slowness")
|
||||
.put(PotionType.WATER_BREATHING, "long_water_breathing")
|
||||
.put(PotionType.POISON, "long_poison")
|
||||
.put(PotionType.REGEN, "long_regeneration")
|
||||
.put(PotionType.STRENGTH, "long_strength")
|
||||
.put(PotionType.WEAKNESS, "long_weakness")
|
||||
.put(PotionType.TURTLE_MASTER, "long_turtle_master")
|
||||
.put(PotionType.SLOW_FALLING, "long_slow_falling")
|
||||
private static final BiMap<PotionType, PotionType> extendable = ImmutableBiMap.<PotionType, PotionType>builder()
|
||||
.put(PotionType.NIGHT_VISION, PotionType.LONG_NIGHT_VISION)
|
||||
.put(PotionType.INVISIBILITY, PotionType.LONG_INVISIBILITY)
|
||||
.put(PotionType.JUMP, PotionType.LONG_LEAPING)
|
||||
.put(PotionType.FIRE_RESISTANCE, PotionType.LONG_FIRE_RESISTANCE)
|
||||
.put(PotionType.SPEED, PotionType.LONG_SWIFTNESS)
|
||||
.put(PotionType.SLOWNESS, PotionType.LONG_SLOWNESS)
|
||||
.put(PotionType.WATER_BREATHING, PotionType.LONG_WATER_BREATHING)
|
||||
.put(PotionType.POISON, PotionType.LONG_POISON)
|
||||
.put(PotionType.REGEN, PotionType.LONG_REGENERATION)
|
||||
.put(PotionType.STRENGTH, PotionType.LONG_STRENGTH)
|
||||
.put(PotionType.WEAKNESS, PotionType.LONG_WEAKNESS)
|
||||
.put(PotionType.TURTLE_MASTER, PotionType.LONG_TURTLE_MASTER)
|
||||
.put(PotionType.SLOW_FALLING, PotionType.LONG_SLOW_FALLING)
|
||||
.build();
|
||||
|
||||
public static String fromBukkit(PotionData data) {
|
||||
String type;
|
||||
public static PotionType fromBukkit(PotionData data) {
|
||||
PotionType type;
|
||||
if (data.isUpgraded()) {
|
||||
type = upgradeable.get(data.getType());
|
||||
} else if (data.isExtended()) {
|
||||
type = extendable.get(data.getType());
|
||||
} else {
|
||||
type = regular.get(data.getType());
|
||||
type = data.getType();
|
||||
}
|
||||
Preconditions.checkNotNull(type, "Unknown potion type from data " + data);
|
||||
|
||||
return "minecraft:" + type;
|
||||
return type;
|
||||
}
|
||||
|
||||
public static PotionData toBukkit(String type) {
|
||||
public static PotionData toBukkit(PotionType type) {
|
||||
if (type == null) {
|
||||
return new PotionData(PotionType.UNCRAFTABLE, false, false);
|
||||
}
|
||||
if (type.startsWith("minecraft:")) {
|
||||
type = type.substring(10);
|
||||
}
|
||||
PotionType potionType = null;
|
||||
|
||||
PotionType potionType;
|
||||
potionType = extendable.inverse().get(type);
|
||||
if (potionType != null) {
|
||||
return new PotionData(potionType, true, false);
|
||||
@@ -92,11 +67,8 @@ public class CraftPotionUtil {
|
||||
if (potionType != null) {
|
||||
return new PotionData(potionType, false, true);
|
||||
}
|
||||
potionType = regular.inverse().get(type);
|
||||
if (potionType != null) {
|
||||
return new PotionData(potionType, false, false);
|
||||
}
|
||||
return new PotionData(PotionType.UNCRAFTABLE, false, false);
|
||||
|
||||
return new PotionData(type, false, false);
|
||||
}
|
||||
|
||||
public static MobEffect fromBukkit(PotionEffect effect) {
|
||||
|
||||
Reference in New Issue
Block a user