make removeEffect send a packet

Make Potion immutable, and use long instead of Long
This commit is contained in:
ThatCreeper 2020-12-30 19:07:07 -06:00
parent b1cab4ee05
commit c3d6af3afe
5 changed files with 38 additions and 23 deletions

View File

@ -405,7 +405,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
// remove expired effects
{
effects.removeIf(timedPotion -> time >=
(timedPotion.getStartingTime() + timedPotion.getPotion().duration * MinecraftServer.TICK_MS));
(timedPotion.getStartingTime() + timedPotion.getPotion().getDuration() * MinecraftServer.TICK_MS));
}
// scheduled tasks
@ -1464,7 +1464,13 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
* @param effect The effect to remove
*/
public void removeEffect(@NotNull PotionEffect effect) {
effects.removeIf(timedPotion -> timedPotion.getPotion().effect == effect);
effects.removeIf(timedPotion -> {
if (timedPotion.getPotion().getEffect() == effect) {
timedPotion.getPotion().sendRemovePacket(this);
return true;
}
return false;
});
}
/**
@ -1473,7 +1479,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
* @param potion The potion to add
*/
public void addEffect(@NotNull Potion potion) {
removeEffect(potion.effect);
removeEffect(potion.getEffect());
effects.add(new TimedPotion(potion, System.currentTimeMillis()));
potion.sendAddPacket(this);
}

View File

@ -14,9 +14,9 @@ public class EntityEffectPacket implements ServerPacket {
@Override
public void write(@NotNull BinaryWriter writer) {
writer.writeVarInt(entityId);
writer.writeByte((byte) potion.effect.getId());
writer.writeByte(potion.amplifier);
writer.writeVarInt(potion.duration);
writer.writeByte((byte) potion.getEffect().getId());
writer.writeByte(potion.getAmplifier());
writer.writeVarInt(potion.getDuration());
writer.writeByte(potion.getFlags());
}

View File

@ -7,12 +7,10 @@ import net.minestom.server.network.packet.server.play.RemoveEntityEffectPacket;
import org.jetbrains.annotations.NotNull;
public class Potion {
public PotionEffect effect;
public byte amplifier;
public int duration;
public boolean ambient;
public boolean particles;
public boolean icon;
private final PotionEffect effect;
private final byte amplifier;
private final int duration;
private final byte flags;
public Potion(PotionEffect effect, byte amplifier, int duration) {
this(effect, amplifier, duration, true, true, false);
@ -30,17 +28,27 @@ public class Potion {
this.effect = effect;
this.amplifier = amplifier;
this.duration = duration;
this.particles = particles;
this.icon = icon;
this.ambient = ambient;
byte flags = 0;
if (ambient) flags = (byte)(flags | 0x01);
if (particles) flags = (byte)(flags | 0x02);
if (icon) flags = (byte)(flags | 0x04);
this.flags = flags;
}
public PotionEffect getEffect() {
return effect;
}
public byte getAmplifier() {
return amplifier;
}
public int getDuration() {
return duration;
}
public byte getFlags() {
byte computed = 0x00;
if (ambient) computed = (byte)(computed | 0x01);
if (particles) computed = (byte)(computed | 0x02);
if (icon) computed = (byte)(computed | 0x04);
return computed;
return flags;
}
public void sendAddPacket(@NotNull Entity entity) {

View File

@ -2,9 +2,9 @@ package net.minestom.server.potion;
public class TimedPotion {
private final Potion potion;
private final Long startingTime;
private final long startingTime;
public TimedPotion(Potion potion, Long startingTime) {
public TimedPotion(Potion potion, long startingTime) {
this.potion = potion;
this.startingTime = startingTime;
}
@ -13,7 +13,7 @@ public class TimedPotion {
return potion;
}
public Long getStartingTime() {
public long getStartingTime() {
return startingTime;
}
}

View File

@ -40,6 +40,7 @@ public class PotionCommand extends Command {
final PotionEffect potion = args.getPotionEffect("potion");
final int duration = args.getInteger("duration");
player.sendMessage(player.getActiveEffects().toString());
player.addEffect(new Potion(
potion,
(byte) 0,