potion cleanup

This commit is contained in:
Sen 2025-03-26 21:07:23 +01:00
parent 460a58e062
commit e108650cd4
30 changed files with 644 additions and 968 deletions

View file

@ -4,249 +4,151 @@ import game.entity.types.EntityLiving;
import game.log.Log;
import game.nbt.NBTTagCompound;
public class PotionEffect
{
/** ID value of the potion this effect matches. */
private int potionID;
public class PotionEffect {
private final Potion potion;
private final int duration;
private final int amplifier;
private final boolean ambient;
private final boolean particles;
/** The duration of the potion effect */
private int duration;
private int remaining;
private boolean thrown;
public PotionEffect(Potion id, int duration, int amplifier) {
this(id, duration, amplifier, false, true);
}
/** The amplifier of the potion effect */
private int amplifier;
public PotionEffect(Potion id, int duration, int amplifier, boolean ambient, boolean showParticles) {
this.potion = id;
this.duration = this.remaining = duration;
this.amplifier = amplifier;
this.ambient = ambient;
this.particles = showParticles;
}
/** Whether the potion is a splash potion */
private boolean isSplashPotion;
public PotionEffect(PotionEffect other) {
this.potion = other.potion;
this.duration = this.remaining = other.duration;
this.amplifier = other.amplifier;
this.ambient = other.ambient;
this.particles = other.particles;
}
/** Whether the potion effect came from a beacon */
private boolean isAmbient;
public PotionEffect combine(PotionEffect other) {
if(this.potion != other.potion)
Log.JNI.warn("PotionEffect.combine(): Diese Methode sollte nur für gleiche Effekte aufgerufen werden!");
int duration = this.duration;
int amplifier = this.amplifier;
int remaining = this.remaining;
boolean ambient = this.ambient;
if(other.amplifier > this.amplifier) {
amplifier = other.amplifier;
duration = other.duration;
remaining = other.remaining;
}
else if(other.amplifier == this.amplifier && this.remaining < other.remaining) {
duration = other.duration;
remaining = other.remaining;
}
else if(!other.ambient && this.ambient) {
ambient = other.ambient;
}
return new PotionEffect(this.potion, duration, amplifier, ambient, other.particles).setRemaining(remaining);
}
/** True if potion effect duration is at maximum, false otherwise. */
private boolean isPotionDurationMax;
private boolean showParticles;
public Potion getPotion() {
return this.potion;
}
public PotionEffect(int id, int effectDuration)
{
this(id, effectDuration, 0);
}
public int getDuration() {
return this.duration;
}
public PotionEffect(int id, int effectDuration, int effectAmplifier)
{
this(id, effectDuration, effectAmplifier, false, true);
}
public boolean isInfinite() {
return this.duration == Integer.MAX_VALUE;
}
public PotionEffect(int id, int effectDuration, int effectAmplifier, boolean ambient, boolean showParticles)
{
this.potionID = id;
this.duration = effectDuration;
this.amplifier = effectAmplifier;
this.isAmbient = ambient;
this.showParticles = showParticles;
}
public int getRemaining() {
return this.remaining;
}
public PotionEffect(PotionEffect other)
{
this.potionID = other.potionID;
this.duration = other.duration;
this.amplifier = other.amplifier;
this.isAmbient = other.isAmbient;
this.showParticles = other.showParticles;
}
public int getAmplifier() {
return this.amplifier;
}
/**
* merges the input PotionEffect into this one if this.amplifier <= tomerge.amplifier. The duration in the supplied
* potion effect is assumed to be greater.
*/
public void combine(PotionEffect other)
{
if (this.potionID != other.potionID)
{
Log.JNI.warn("PotionEffect.combine(): Diese Methode sollte nur für gleiche Effekte aufgerufen werden!");
}
public PotionEffect setThrown(boolean thrown) {
this.thrown = thrown;
return this;
}
if (other.amplifier > this.amplifier)
{
this.amplifier = other.amplifier;
this.duration = other.duration;
}
else if (other.amplifier == this.amplifier && this.duration < other.duration)
{
this.duration = other.duration;
}
else if (!other.isAmbient && this.isAmbient)
{
this.isAmbient = other.isAmbient;
}
public PotionEffect setRemaining(int remaining) {
this.remaining = remaining;
return this;
}
this.showParticles = other.showParticles;
}
public boolean isAmbient() {
return this.ambient;
}
/**
* Retrieve the ID of the potion this effect matches.
*/
public int getPotionID()
{
return this.potionID;
}
public boolean getIsShowParticles() {
return this.particles;
}
public int getDuration()
{
return this.duration;
}
public boolean onUpdate(EntityLiving entityIn) {
if(this.isInfinite() && this.remaining < 20 * 60)
this.remaining = Integer.MAX_VALUE;
if(this.remaining > 0) {
this.potion.onUpdate(entityIn, this.remaining, this.amplifier);
--this.remaining;
}
return this.remaining > 0;
}
public int getAmplifier()
{
return this.amplifier;
}
public String getEffectName() {
return this.potion.getDisplay();
}
/**
* Set whether this potion is a splash potion.
*/
public void setSplashPotion(boolean splashPotion)
{
this.isSplashPotion = splashPotion;
}
public String getPotionName() {
return this.potion.getPotionDisplay();
}
/**
* Gets whether this potion effect originated from a beacon
*/
public boolean getIsAmbient()
{
return this.isAmbient;
}
public String getDurationString() {
if(this.isInfinite())
return "**:**";
int secs = this.remaining / 20;
int hrs = secs / (60 * 60);
secs = secs % (60 * 60);
int mins = secs / 60;
secs = secs % 60;
return (hrs > 0 ? hrs + ":" + (mins < 10 ? "0" : "") : "") + mins + ":" + (secs < 10 ? "0" : "") + secs;
}
public boolean getIsShowParticles()
{
return this.showParticles;
}
public int hashCode() {
return this.potion.ordinal();
}
public boolean onUpdate(EntityLiving entityIn)
{
if (this.duration > 0)
{
if (Potion.POTION_TYPES[this.potionID].isReady(this.duration, this.amplifier, entityIn.ticksExisted))
{
this.performEffect(entityIn);
}
public boolean equals(Object obj) {
if(!(obj instanceof PotionEffect))
return false;
PotionEffect other = (PotionEffect)obj;
return this.potion == other.potion && this.amplifier == other.amplifier && this.duration == other.duration
&& this.thrown == other.thrown && this.remaining == other.remaining && this.ambient == other.ambient;
}
--this.duration;
}
public NBTTagCompound toNbt() {
NBTTagCompound nbt = new NBTTagCompound();
nbt.setString("Type", this.potion.getName());
nbt.setByte("Amplifier", (byte)this.amplifier);
nbt.setInteger("Duration", this.duration);
nbt.setInteger("Remaining", this.remaining);
nbt.setBoolean("Ambient", this.ambient);
nbt.setBoolean("Particles", this.particles);
return nbt;
}
return this.duration > 0;
}
public void performEffect(EntityLiving entityIn)
{
if (this.duration > 0)
{
Potion.POTION_TYPES[this.potionID].performEffect(entityIn, this.amplifier);
}
}
public String getEffectName()
{
return Potion.POTION_TYPES[this.potionID].getDisplay();
}
public String getPotionName()
{
return Potion.POTION_TYPES[this.potionID].getPotionDisplay();
}
public int hashCode()
{
return this.potionID;
}
public String toString()
{
String s = "";
if (this.getAmplifier() > 0)
{
s = this.getEffectName() + " x " + (this.getAmplifier() + 1) + ", Duration: " + this.getDuration();
}
else
{
s = this.getEffectName() + ", Duration: " + this.getDuration();
}
if (this.isSplashPotion)
{
s = s + ", Splash: true";
}
if (!this.showParticles)
{
s = s + ", Particles: false";
}
return Potion.POTION_TYPES[this.potionID].isUsable() ? "(" + s + ")" : s;
}
public boolean equals(Object p_equals_1_)
{
if (!(p_equals_1_ instanceof PotionEffect))
{
return false;
}
else
{
PotionEffect potioneffect = (PotionEffect)p_equals_1_;
return this.potionID == potioneffect.potionID && this.amplifier == potioneffect.amplifier && this.duration == potioneffect.duration && this.isSplashPotion == potioneffect.isSplashPotion && this.isAmbient == potioneffect.isAmbient;
}
}
/**
* Write a custom potion effect to a potion item's NBT data.
*/
public NBTTagCompound writeCustomPotionEffectToNBT(NBTTagCompound nbt)
{
nbt.setByte("Id", (byte)this.getPotionID());
nbt.setByte("Amplifier", (byte)this.getAmplifier());
nbt.setInteger("Duration", this.getDuration());
nbt.setBoolean("Ambient", this.getIsAmbient());
nbt.setBoolean("ShowParticles", this.getIsShowParticles());
return nbt;
}
/**
* Read a custom potion effect from a potion item's NBT data.
*/
public static PotionEffect readCustomPotionEffectFromNBT(NBTTagCompound nbt)
{
int i = nbt.getByte("Id");
if (i >= 0 && i < Potion.POTION_TYPES.length && Potion.POTION_TYPES[i] != null)
{
int j = (int)(nbt.getByte("Amplifier") & 255);
int k = nbt.getInteger("Duration");
boolean flag = nbt.getBoolean("Ambient");
boolean flag1 = true;
if (nbt.hasKey("ShowParticles", 1))
{
flag1 = nbt.getBoolean("ShowParticles");
}
return new PotionEffect(i, k, j, flag, flag1);
}
else
{
return null;
}
}
/**
* Toggle the isPotionDurationMax field.
*/
public void setPotionDurationMax(boolean maxDuration)
{
this.isPotionDurationMax = maxDuration;
}
public boolean getIsPotionDurationMax()
{
return this.isPotionDurationMax;
}
public static PotionEffect fromNbt(NBTTagCompound nbt) {
Potion potion = Potion.getByName(nbt.getString("Type"));
return potion == null ? null : new PotionEffect(potion, nbt.getInteger("Duration"), (int)(nbt.getByte("Amplifier") & 255), nbt.getBoolean("Ambient"), nbt.getBoolean("Particles"))
.setRemaining(nbt.getInteger("Remaining"));
}
}