Java源码示例:cn.nukkit.potion.Effect
示例1
protected void initEntity() {
if (this.namedTag.contains("ActiveEffects")) {
ListTag<CompoundTag> effects = this.namedTag.getList("ActiveEffects", CompoundTag.class);
for (CompoundTag e : effects.getAll()) {
Effect effect = Effect.getEffect(e.getByte("Id"));
if (effect == null) {
continue;
}
effect.setAmplifier(e.getByte("Amplifier")).setDuration(e.getInt("Duration")).setVisible(e.getBoolean("showParticles"));
this.addEffect(effect);
}
}
if (this.namedTag.contains("CustomName")) {
this.setNameTag(this.namedTag.getString("CustomName"));
if (this.namedTag.contains("CustomNameVisible")) {
this.setNameTagVisible(this.namedTag.getBoolean("CustomNameVisible"));
}
}
this.scheduleUpdate();
}
示例2
public void addEffect(Effect effect) {
if (effect == null) {
return; //here add null means add nothing
}
Effect oldEffect = this.effects.getOrDefault(effect.getId(), null);
if (oldEffect != null) {
if (Math.abs(effect.getAmplifier()) < Math.abs(oldEffect.getAmplifier())) return;
if (Math.abs(effect.getAmplifier()) == Math.abs(oldEffect.getAmplifier())
&& effect.getDuration() < oldEffect.getDuration()) return;
effect.add(this, true);
} else {
effect.add(this, false);
}
this.effects.put(effect.getId(), effect);
this.recalculateEffectColor();
if (effect.getId() == Effect.HEALTH_BOOST) {
this.setHealth(this.getHealth() + 4 * (effect.getAmplifier() + 1));
}
}
示例3
public boolean attack(EntityDamageEvent source) {
if (hasEffect(Effect.FIRE_RESISTANCE)
&& (source.getCause() == DamageCause.FIRE
|| source.getCause() == DamageCause.FIRE_TICK
|| source.getCause() == DamageCause.LAVA)) {
return false;
}
getServer().getPluginManager().callEvent(source);
if (source.isCancelled()) {
return false;
}
if (this.absorption > 0) { //Damage Absorption
float absorptionHealth = this.absorption - source.getFinalDamage() > 0 ? source.getFinalDamage() : this.absorption;
this.setAbsorption(this.absorption - absorptionHealth);
source.setDamage(-absorptionHealth, EntityDamageEvent.DamageModifier.ABSORPTION);
}
setLastDamageCause(source);
setHealth(getHealth() - source.getFinalDamage());
return true;
}
示例4
public void fall(float fallDistance) {
float damage = (float) Math.floor(fallDistance - 3 - (this.hasEffect(Effect.JUMP) ? this.getEffect(Effect.JUMP).getAmplifier() + 1 : 0));
if (damage > 0) {
this.attack(new EntityDamageEvent(this, DamageCause.FALL, damage));
}
if (fallDistance > 0.75) {
BlockVector3 v = new BlockVector3(getFloorX(), getFloorY() - 1, getFloorZ());
int down = this.level.getBlockIdAt(v.x, v.y, v.z);
if (down == Item.FARMLAND) {
if (this instanceof Player) {
Player p = (Player) this;
PlayerInteractEvent ev = new PlayerInteractEvent(p, p.getInventory().getItemInHand(), this.temporalVector.setComponents(v.x, v.y, v.z), null, Action.PHYSICAL);
this.server.getPluginManager().callEvent(ev);
if (ev.isCancelled()) {
return;
}
}
this.level.setBlock(this.temporalVector.setComponents(v.x, v.y, v.z), new BlockDirt(), true, true);
}
}
}
示例5
public double getBreakTime(Item item, Player player) {
Objects.requireNonNull(item, "getBreakTime: Item can not be null");
Objects.requireNonNull(player, "getBreakTime: Player can not be null");
double blockHardness = getHardness();
boolean correctTool = correctTool0(getToolType(), item);
boolean canHarvestWithHand = canHarvestWithHand();
int blockId = getId();
int itemToolType = toolType0(item);
int itemTier = item.getTier();
int efficiencyLoreLevel = Optional.ofNullable(item.getEnchantment(Enchantment.ID_EFFICIENCY))
.map(Enchantment::getLevel).orElse(0);
int hasteEffectLevel = Optional.ofNullable(player.getEffect(Effect.HASTE))
.map(Effect::getAmplifier).orElse(0);
boolean insideOfWaterWithoutAquaAffinity = player.isInsideOfWater() &&
Optional.ofNullable(player.getInventory().getHelmet().getEnchantment(Enchantment.ID_WATER_WORKER))
.map(Enchantment::getLevel).map(l -> l >= 1).orElse(false);
boolean outOfWaterButNotOnGround = (!player.isInsideOfWater()) && (!player.isOnGround());
return breakTime0(blockHardness, correctTool, canHarvestWithHand, blockId, itemToolType, itemTier,
efficiencyLoreLevel, hasteEffectLevel, insideOfWaterWithoutAquaAffinity, outOfWaterButNotOnGround);
}
示例6
public boolean attack(EntityDamageEvent source) {
if (hasEffect(Effect.FIRE_RESISTANCE)
&& (source.getCause() == DamageCause.FIRE
|| source.getCause() == DamageCause.FIRE_TICK
|| source.getCause() == DamageCause.LAVA)) {
return false;
}
getServer().getPluginManager().callEvent(source);
if (source.isCancelled()) {
return false;
}
if (this.absorption > 0) { // Damage Absorption
this.setAbsorption(Math.max(0, this.getAbsorption() + source.getDamage(EntityDamageEvent.DamageModifier.ABSORPTION)));
}
setLastDamageCause(source);
setHealth(getHealth() - source.getFinalDamage());
return true;
}
示例7
public double getBreakTime(Item item, Player player) {
Objects.requireNonNull(item, "getBreakTime: Item can not be null");
Objects.requireNonNull(player, "getBreakTime: Player can not be null");
double blockHardness = getHardness();
if (blockHardness == 0) {
return 0;
}
boolean correctTool = correctTool0(getToolType(), item);
boolean canHarvestWithHand = canHarvestWithHand();
int blockId = getId();
int itemToolType = toolType0(item);
int itemTier = item.getTier();
int efficiencyLoreLevel = Optional.ofNullable(item.getEnchantment(Enchantment.ID_EFFICIENCY))
.map(Enchantment::getLevel).orElse(0);
int hasteEffectLevel = Optional.ofNullable(player.getEffect(Effect.HASTE))
.map(Effect::getAmplifier).orElse(0);
boolean insideOfWaterWithoutAquaAffinity = player.isInsideOfWater() &&
Optional.ofNullable(player.getInventory().getHelmet().getEnchantment(Enchantment.ID_WATER_WORKER))
.map(Enchantment::getLevel).map(l -> l >= 1).orElse(false);
boolean outOfWaterButNotOnGround = (!player.isInsideOfWater()) && (!player.isOnGround());
return breakTime0(blockHardness, correctTool, canHarvestWithHand, blockId, itemToolType, itemTier,
efficiencyLoreLevel, hasteEffectLevel, insideOfWaterWithoutAquaAffinity, outOfWaterButNotOnGround);
}
示例8
@Override
public void onEntityCollide(Entity entity) {
entity.highestPosition -= (entity.highestPosition - entity.y) * 0.5;
// Always setting the duration to 15 seconds? TODO
EntityCombustByBlockEvent ev = new EntityCombustByBlockEvent(this, entity, 15);
Server.getInstance().getPluginManager().callEvent(ev);
if (!ev.isCancelled()
// Making sure the entity is actually alive and not invulnerable.
&& entity.isAlive()
&& entity.noDamageTicks == 0) {
entity.setOnFire(ev.getDuration());
}
if (!entity.hasEffect(Effect.FIRE_RESISTANCE)) {
entity.attack(new EntityDamageByBlockEvent(this, entity, DamageCause.LAVA, 4));
}
super.onEntityCollide(entity);
}
示例9
protected void initEntity() {
if (this.namedTag.contains("ActiveEffects")) {
ListTag<CompoundTag> effects = this.namedTag.getList("ActiveEffects", CompoundTag.class);
for (CompoundTag e : effects.getAll()) {
Effect effect = Effect.getEffect(e.getByte("Id"));
if (effect == null) {
continue;
}
effect.setAmplifier(e.getByte("Amplifier")).setDuration(e.getInt("Duration")).setVisible(e.getBoolean("showParticles"));
this.addEffect(effect);
}
}
if (this.namedTag.contains("CustomName")) {
this.setNameTag(this.namedTag.getString("CustomName"));
if (this.namedTag.contains("CustomNameVisible")) {
this.setNameTagVisible(this.namedTag.getBoolean("CustomNameVisible"));
}
}
this.scheduleUpdate();
}
示例10
public boolean attack(EntityDamageEvent source) {
if (hasEffect(Effect.FIRE_RESISTANCE)
&& (source.getCause() == DamageCause.FIRE
|| source.getCause() == DamageCause.FIRE_TICK
|| source.getCause() == DamageCause.LAVA)) {
return false;
}
getServer().getPluginManager().callEvent(source);
if (source.isCancelled()) {
return false;
}
if (this.absorption > 0) { //Damage Absorption
float absorptionHealth = this.absorption - source.getFinalDamage() > 0 ? source.getFinalDamage() : this.absorption;
this.setAbsorption(this.absorption - absorptionHealth);
source.setDamage(-absorptionHealth, EntityDamageEvent.DamageModifier.ABSORPTION);
}
setLastDamageCause(source);
setHealth(getHealth() - source.getFinalDamage());
return true;
}
示例11
public double getBreakTime(Item item, Player player) {
Objects.requireNonNull(item, "getBreakTime: Item can not be null");
Objects.requireNonNull(player, "getBreakTime: Player can not be null");
double blockHardness = getHardness();
boolean correctTool = correctTool0(getToolType(), item);
boolean canHarvestWithHand = canHarvestWithHand();
int blockId = getId();
int itemToolType = toolType0(item);
int itemTier = item.getTier();
int efficiencyLoreLevel = Optional.ofNullable(item.getEnchantment(Enchantment.ID_EFFICIENCY))
.map(Enchantment::getLevel).orElse(0);
int hasteEffectLevel = Optional.ofNullable(player.getEffect(Effect.HASTE))
.map(Effect::getAmplifier).orElse(0);
boolean insideOfWaterWithoutAquaAffinity = player.isInsideOfWater() &&
Optional.ofNullable(player.getInventory().getHelmet().getEnchantment(Enchantment.ID_WATER_WORKER))
.map(Enchantment::getLevel).map(l -> l >= 1).orElse(false);
boolean outOfWaterButNotOnGround = (!player.isInsideOfWater()) && (!player.isOnGround());
return breakTime0(blockHardness, correctTool, canHarvestWithHand, blockId, itemToolType, itemTier,
efficiencyLoreLevel, hasteEffectLevel, insideOfWaterWithoutAquaAffinity, outOfWaterButNotOnGround);
}
示例12
@Override
public void onEntityCollide(Entity entity) {
entity.highestPosition -= (entity.highestPosition - entity.y) * 0.5;
if (!entity.hasEffect(Effect.FIRE_RESISTANCE)) {
entity.attack(new EntityDamageByBlockEvent(this, entity, DamageCause.LAVA, 4));
}
// Always setting the duration to 15 seconds? TODO
EntityCombustByBlockEvent ev = new EntityCombustByBlockEvent(this, entity, 15);
Server.getInstance().getPluginManager().callEvent(ev);
if (!ev.isCancelled()
// Making sure the entity is acutally alive and not invulnerable.
&& entity.isAlive()
&& entity.noDamageTicks == 0) {
entity.setOnFire(ev.getDuration());
}
super.onEntityCollide(entity);
}
示例13
public void removeEffect(int effectId) {
if (this.effects.containsKey(effectId)) {
Effect effect = this.effects.get(effectId);
this.effects.remove(effectId);
effect.remove(this);
this.recalculateEffectColor();
}
}
示例14
protected void recalculateEffectColor() {
int[] color = new int[3];
int count = 0;
boolean ambient = true;
for (Effect effect : this.effects.values()) {
if (effect.isVisible()) {
int[] c = effect.getColor();
color[0] += c[0] * (effect.getAmplifier() + 1);
color[1] += c[1] * (effect.getAmplifier() + 1);
color[2] += c[2] * (effect.getAmplifier() + 1);
count += effect.getAmplifier() + 1;
if (!effect.isAmbient()) {
ambient = false;
}
}
}
if (count > 0) {
int r = (color[0] / count) & 0xff;
int g = (color[1] / count) & 0xff;
int b = (color[2] / count) & 0xff;
this.setDataProperty(new IntEntityData(Entity.DATA_POTION_COLOR, (r << 16) + (g << 8) + b));
this.setDataProperty(new ByteEntityData(Entity.DATA_POTION_AMBIENT, ambient ? 1 : 0));
} else {
this.setDataProperty(new IntEntityData(Entity.DATA_POTION_COLOR, 0));
this.setDataProperty(new ByteEntityData(Entity.DATA_POTION_AMBIENT, 0));
}
}
示例15
public void sendPotionEffects(Player player) {
for (Effect effect : this.effects.values()) {
MobEffectPacket pk = new MobEffectPacket();
pk.entityRuntimeId = this.getId();
pk.effectId = effect.getId();
pk.amplifier = effect.getAmplifier();
pk.particles = effect.isVisible();
pk.duration = effect.getDuration();
pk.eventId = MobEffectPacket.EVENT_ADD;
player.dataPacket(pk);
}
}
示例16
@Override
protected boolean onEatenBy(Player player) {
super.onEatenBy(player);
List<Effect> toApply = new LinkedList<>();
effects.forEach((effect, chance) -> {
if (chance >= Math.random()) toApply.add(effect.clone());
});
toApply.forEach(player::addEffect);
return true;
}
示例17
@Override
public void doPostAttack(Entity attacker, Entity entity) {
if (entity instanceof EntityArthropod) {
int duration = 20 + ThreadLocalRandom.current().nextInt(10 * this.level);
entity.addEffect(Effect.getEffect(Effect.SLOWNESS).setDuration(duration).setAmplifier(3));
}
}
示例18
@Override
public void onEntityCollide(Entity entity) {
entity.highestPosition -= (entity.highestPosition - entity.y) * 0.5;
if (!entity.hasEffect(Effect.FIRE_RESISTANCE)) {
entity.attack(new EntityDamageByBlockEvent(this, entity, DamageCause.LAVA, 4));
}
EntityCombustByBlockEvent ev = new EntityCombustByBlockEvent(this, entity, 15);
Server.getInstance().getPluginManager().callEvent(ev);
if (!ev.isCancelled()) {
entity.setOnFire(ev.getDuration());
}
super.onEntityCollide(entity);
}
示例19
@Override
public void onEntityCollide(Entity entity) {
if (!entity.hasEffect(Effect.FIRE_RESISTANCE)) {
entity.attack(new EntityDamageByBlockEvent(this, entity, DamageCause.FIRE, 1));
}
EntityCombustByBlockEvent ev = new EntityCombustByBlockEvent(this, entity, 8);
if (entity instanceof EntityArrow) {
ev.setCancelled();
}
Server.getInstance().getPluginManager().callEvent(ev);
if (!ev.isCancelled() && entity instanceof Player && !((Player) entity).isCreative()) {
entity.setOnFire(ev.getDuration());
}
}
示例20
public void updateFoodExpLevel(double use) {
if (!this.getPlayer().isFoodEnabled()) return;
if (Server.getInstance().getDifficulty() == 0) return;
if (this.getPlayer().hasEffect(Effect.SATURATION)) return;
this.foodExpLevel += use;
if (this.foodExpLevel > 4) {
this.useHunger(1);
this.foodExpLevel = 0;
}
}
示例21
public EntityDamageEvent(Entity entity, DamageCause cause, Map<DamageModifier, Float> modifiers) {
this.entity = entity;
this.cause = cause;
this.modifiers = modifiers;
this.originals = this.modifiers;
if (!this.modifiers.containsKey(DamageModifier.BASE)) {
throw new EventException("BASE Damage modifier missing");
}
if (entity.hasEffect(Effect.DAMAGE_RESISTANCE)) {
this.setDamage((float) -(this.getDamage(DamageModifier.BASE) * 0.20 * (entity.getEffect(Effect.DAMAGE_RESISTANCE).getAmplifier() + 1)), DamageModifier.RESISTANCE);
}
}
示例22
protected void addAttackerModifiers(Entity damager) {
if (damager.hasEffect(Effect.STRENGTH)) {
this.setDamage((float) (this.getDamage(DamageModifier.BASE) * 0.3 * (damager.getEffect(Effect.STRENGTH).getAmplifier() + 1)), DamageModifier.STRENGTH);
}
if (damager.hasEffect(Effect.WEAKNESS)) {
this.setDamage(-(float) (this.getDamage(DamageModifier.BASE) * 0.2 * (damager.getEffect(Effect.WEAKNESS).getAmplifier() + 1)), DamageModifier.WEAKNESS);
}
}
示例23
protected void initEntity() {
if (this.namedTag.contains("ActiveEffects")) {
ListTag<CompoundTag> effects = this.namedTag.getList("ActiveEffects", CompoundTag.class);
for (CompoundTag e : effects.getAll()) {
Effect effect = Effect.getEffect(e.getByte("Id"));
if (effect == null) {
continue;
}
effect.setAmplifier(e.getByte("Amplifier")).setDuration(e.getInt("Duration")).setVisible(e.getBoolean("showParticles"));
this.addEffect(effect);
}
}
if (this.namedTag.contains("CustomName")) {
this.setNameTag(this.namedTag.getString("CustomName"));
if (this.namedTag.contains("CustomNameVisible")) {
this.setNameTagVisible(this.namedTag.getBoolean("CustomNameVisible"));
}
if(this.namedTag.contains("CustomNameAlwaysVisible")){
this.setNameTagAlwaysVisible(this.namedTag.getBoolean("CustomNameAlwaysVisible"));
}
}
this.setDataFlag(DATA_FLAGS, DATA_FLAG_HAS_COLLISION, true);
this.dataProperties.putFloat(DATA_BOUNDING_BOX_HEIGHT, this.getHeight());
this.dataProperties.putFloat(DATA_BOUNDING_BOX_WIDTH, this.getWidth());
this.dataProperties.putInt(DATA_HEALTH, (int) this.getHealth());
this.scheduleUpdate();
}
示例24
public void removeEffect(int effectId) {
if (this.effects.containsKey(effectId)) {
Effect effect = this.effects.get(effectId);
this.effects.remove(effectId);
effect.remove(this);
this.recalculateEffectColor();
}
}
示例25
protected void recalculateEffectColor() {
int[] color = new int[3];
int count = 0;
boolean ambient = true;
for (Effect effect : this.effects.values()) {
if (effect.isVisible()) {
int[] c = effect.getColor();
color[0] += c[0] * (effect.getAmplifier() + 1);
color[1] += c[1] * (effect.getAmplifier() + 1);
color[2] += c[2] * (effect.getAmplifier() + 1);
count += effect.getAmplifier() + 1;
if (!effect.isAmbient()) {
ambient = false;
}
}
}
if (count > 0) {
int r = (color[0] / count) & 0xff;
int g = (color[1] / count) & 0xff;
int b = (color[2] / count) & 0xff;
this.setDataProperty(new IntEntityData(Entity.DATA_POTION_COLOR, (r << 16) + (g << 8) + b));
this.setDataProperty(new ByteEntityData(Entity.DATA_POTION_AMBIENT, ambient ? 1 : 0));
} else {
this.setDataProperty(new IntEntityData(Entity.DATA_POTION_COLOR, 0));
this.setDataProperty(new ByteEntityData(Entity.DATA_POTION_AMBIENT, 0));
}
}
示例26
public void sendPotionEffects(Player player) {
for (Effect effect : this.effects.values()) {
MobEffectPacket pk = new MobEffectPacket();
pk.eid = this.getId();
pk.effectId = effect.getId();
pk.amplifier = effect.getAmplifier();
pk.particles = effect.isVisible();
pk.duration = effect.getDuration();
pk.eventId = MobEffectPacket.EVENT_ADD;
player.dataPacket(pk);
}
}
示例27
@Override
protected boolean onEatenBy(Player player) {
super.onEatenBy(player);
List<Effect> toApply = new LinkedList<>();
effects.forEach((effect, chance) -> {
if (chance >= Math.random()) toApply.add(effect.clone());
});
toApply.forEach(player::addEffect);
return true;
}
示例28
@Override
public void doPostAttack(Entity attacker, Entity entity) {
if (entity instanceof EntityArthropod) {
int duration = 20 + ThreadLocalRandom.current().nextInt(10 * this.level);
entity.addEffect(Effect.getEffect(Effect.SLOWNESS).setDuration(duration).setAmplifier(3));
}
}
示例29
@Override
public void onEntityCollide(Entity entity) {
if (!entity.hasEffect(Effect.FIRE_RESISTANCE)) {
if (entity instanceof Player) {
Player p = (Player) entity;
if (!p.isCreative() && !p.isSpectator() && !p.isSneaking()) {
entity.attack(new EntityDamageByBlockEvent(this, entity, EntityDamageEvent.DamageCause.LAVA, 1));
}
} else {
entity.attack(new EntityDamageByBlockEvent(this, entity, EntityDamageEvent.DamageCause.LAVA, 1));
}
}
}
示例30
@Override
public void onEntityCollide(Entity entity) {
if (!entity.hasEffect(Effect.FIRE_RESISTANCE)) {
entity.attack(new EntityDamageByBlockEvent(this, entity, DamageCause.FIRE, 1));
}
EntityCombustByBlockEvent ev = new EntityCombustByBlockEvent(this, entity, 8);
if (entity instanceof EntityArrow) {
ev.setCancelled();
}
Server.getInstance().getPluginManager().callEvent(ev);
if (!ev.isCancelled() && entity.isAlive() && entity.noDamageTicks == 0) {
entity.setOnFire(ev.getDuration());
}
}