87 lines
2.8 KiB
Java
87 lines
2.8 KiB
Java
package net.minecraft.world.item.alchemy;
|
|
|
|
import com.mojang.serialization.Codec;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
import net.minecraft.core.Holder;
|
|
import net.minecraft.core.registries.BuiltInRegistries;
|
|
import net.minecraft.core.registries.Registries;
|
|
import net.minecraft.network.RegistryFriendlyByteBuf;
|
|
import net.minecraft.network.codec.ByteBufCodecs;
|
|
import net.minecraft.network.codec.StreamCodec;
|
|
import net.minecraft.world.effect.MobEffectInstance;
|
|
import net.minecraft.world.flag.FeatureElement;
|
|
import net.minecraft.world.flag.FeatureFlag;
|
|
import net.minecraft.world.flag.FeatureFlagSet;
|
|
import net.minecraft.world.flag.FeatureFlags;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
/**
|
|
* Defines a type of potion in the game. These are used to associate one or more effects with items such as the bottled potion or the tipped arrows.
|
|
*/
|
|
public class Potion implements FeatureElement {
|
|
public static final Codec<Holder<Potion>> CODEC = BuiltInRegistries.POTION.holderByNameCodec();
|
|
public static final StreamCodec<RegistryFriendlyByteBuf, Holder<Potion>> STREAM_CODEC = ByteBufCodecs.holderRegistry(Registries.POTION);
|
|
/**
|
|
* The base name for the potion type.
|
|
*/
|
|
@Nullable
|
|
private final String name;
|
|
private final List<MobEffectInstance> effects;
|
|
private FeatureFlagSet requiredFeatures = FeatureFlags.VANILLA_SET;
|
|
|
|
public Potion(MobEffectInstance... effects) {
|
|
this(null, effects);
|
|
}
|
|
|
|
public Potion(@Nullable String name, MobEffectInstance... effects) {
|
|
this.name = name;
|
|
this.effects = List.of(effects);
|
|
}
|
|
|
|
public Potion requiredFeatures(FeatureFlag... requiredFeatures) {
|
|
this.requiredFeatures = FeatureFlags.REGISTRY.subset(requiredFeatures);
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public FeatureFlagSet requiredFeatures() {
|
|
return this.requiredFeatures;
|
|
}
|
|
|
|
public static String getName(Optional<Holder<Potion>> potion, String descriptionId) {
|
|
if (potion.isPresent()) {
|
|
String string = ((Potion)((Holder)potion.get()).value()).name;
|
|
if (string != null) {
|
|
return descriptionId + string;
|
|
}
|
|
}
|
|
|
|
String string = (String)potion.flatMap(Holder::unwrapKey).map(resourceKey -> resourceKey.location().getPath()).orElse("empty");
|
|
return descriptionId + string;
|
|
}
|
|
|
|
/**
|
|
* Gets the base effects applied by the potion.
|
|
* @return The effects applied by the potion.
|
|
*/
|
|
public List<MobEffectInstance> getEffects() {
|
|
return this.effects;
|
|
}
|
|
|
|
/**
|
|
* Checks if the potion contains any instant effects such as instant health or instant damage.
|
|
* @return Whether the potion contained an instant effect.
|
|
*/
|
|
public boolean hasInstantEffects() {
|
|
if (!this.effects.isEmpty()) {
|
|
for (MobEffectInstance mobEffectInstance : this.effects) {
|
|
if (mobEffectInstance.getEffect().value().isInstantenous()) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|