2023-06-24 17:15:05 +10:00

133 lines
3.9 KiB
Java

package org.bukkit.craftbukkit.entity;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.util.List;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.MinecraftKey;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectList;
import net.minecraft.world.entity.projectile.EntityTippedArrow;
import org.bukkit.Color;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.potion.CraftPotionUtil;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.EntityType;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
public class CraftTippedArrow extends CraftArrow implements Arrow {
public CraftTippedArrow(CraftServer server, EntityTippedArrow entity) {
super(server, entity);
}
@Override
public EntityTippedArrow getHandle() {
return (EntityTippedArrow) entity;
}
@Override
public String toString() {
return "CraftTippedArrow";
}
@Override
public EntityType getType() {
return EntityType.ARROW;
}
@Override
public boolean addCustomEffect(PotionEffect effect, boolean override) {
int effectId = effect.getType().getId();
MobEffect existing = null;
for (MobEffect mobEffect : getHandle().effects) {
if (MobEffectList.getId(mobEffect.getEffect()) == effectId) {
existing = mobEffect;
}
}
if (existing != null) {
if (!override) {
return false;
}
getHandle().effects.remove(existing);
}
getHandle().addEffect(CraftPotionUtil.fromBukkit(effect));
getHandle().updateColor();
return true;
}
@Override
public void clearCustomEffects() {
getHandle().effects.clear();
getHandle().updateColor();
}
@Override
public List<PotionEffect> getCustomEffects() {
ImmutableList.Builder<PotionEffect> builder = ImmutableList.builder();
for (MobEffect effect : getHandle().effects) {
builder.add(CraftPotionUtil.toBukkit(effect));
}
return builder.build();
}
@Override
public boolean hasCustomEffect(PotionEffectType type) {
for (MobEffect effect : getHandle().effects) {
if (CraftPotionUtil.equals(effect.getEffect(), type)) {
return true;
}
}
return false;
}
@Override
public boolean hasCustomEffects() {
return !getHandle().effects.isEmpty();
}
@Override
public boolean removeCustomEffect(PotionEffectType effect) {
int effectId = effect.getId();
MobEffect existing = null;
for (MobEffect mobEffect : getHandle().effects) {
if (MobEffectList.getId(mobEffect.getEffect()) == effectId) {
existing = mobEffect;
}
}
if (existing == null) {
return false;
}
getHandle().effects.remove(existing);
getHandle().updateColor();
return true;
}
@Override
public void setBasePotionData(PotionData data) {
Preconditions.checkArgument(data != null, "PotionData cannot be null");
this.getHandle().potion = BuiltInRegistries.POTION.get(new MinecraftKey(CraftPotionUtil.fromBukkit(data)));
}
@Override
public PotionData getBasePotionData() {
return CraftPotionUtil.toBukkit(BuiltInRegistries.POTION.getKey(this.getHandle().potion).toString());
}
@Override
public void setColor(Color color) {
int colorRGB = (color == null) ? -1 : color.asRGB();
getHandle().setFixedColor(colorRGB);
}
@Override
public Color getColor() {
if (getHandle().getColor() <= -1) {
return null;
}
return Color.fromRGB(getHandle().getColor());
}
}