refactor: simplify NBT reading for ability properties and potion effects

This commit is contained in:
Kam committed 2026-02-13 14:17:17 -05:00
1 parent f16c8ae115
commit 185d915928
38 files changed
+429 -417

No files matched your search

+13 -3
View File
@@ -20,6 +20,7 @@ public class DataStats {
public EnumPotionType potionType = EnumPotionType.None;
public int potionDuration = 5; //20 = 1 second
public int potionAmp = 0;
public int potionManualId = 0;
public double maxHealth = 20;
public int respawnTime = 20;
@@ -45,6 +46,7 @@ public class DataStats {
public boolean pPhysics = true, pXlr8 = false, pGlows = false, pExplode = false;
public boolean pRender3D = false, pSpin = false, pStick = false, pBurnItem = false;
public EnumPotionType pEffect = EnumPotionType.None;
public int pManualId = 0;
public EnumParticleType pTrail = EnumParticleType.None;
public ScriptParticle pCustom = new ScriptParticle("");
public int pEffAmp = 0;
@@ -88,6 +90,8 @@ public class DataStats {
compound.setInteger("PotionEffect", potionType.ordinal());
compound.setInteger("PotionDuration", potionDuration);
compound.setInteger("PotionAmp", potionAmp);
if (potionType == EnumPotionType.Manual)
compound.setInteger("PotionManualId", potionManualId);
compound.setInteger("MaxFiringRange", rangedRange);
compound.setInteger("FireRate", fireRate);
@@ -122,6 +126,8 @@ public class DataStats {
compound.setTag("pCustom", pCustom.writeToNBT());
if (pEffect == EnumPotionType.Fire)
compound.setBoolean("pBurnItem", pBurnItem);
if (pEffect == EnumPotionType.Manual)
compound.setInteger("pManualId", pManualId);
compound.setBoolean("ImmuneToFire", immuneToFire);
compound.setBoolean("PotionImmune", potionImmune);
@@ -145,7 +151,7 @@ public class DataStats {
aggroRange = compound.getInteger("AggroRange");
respawnTime = compound.getInteger("RespawnTime");
spawnCycle = compound.getInteger("SpawnCycle");
creatureType = EnumCreatureAttribute.values()[compound.getInteger("CreatureType") % EnumPotionType.values().length];
creatureType = EnumCreatureAttribute.values()[compound.getInteger("CreatureType") % EnumCreatureAttribute.values().length];
ignoreCobweb = compound.getBoolean("IgnoreCobweb");
healthRegen = compound.getFloat("HealthRegen");
combatRegen = compound.getFloat("CombatRegen");
@@ -155,9 +161,11 @@ public class DataStats {
swingWarmUp = ValueUtil.clamp(compound.getInteger("SwingWarmup"), 0, 1000);
attackRange = compound.getInteger("AttackRange");
knockback = compound.getInteger("KnockBack");
potionType = EnumPotionType.values()[compound.getInteger("PotionEffect") % EnumPotionType.values().length];
potionType = EnumPotionType.fromOrdinal(compound.getInteger("PotionEffect"));
potionDuration = compound.getInteger("PotionDuration");
potionAmp = compound.getInteger("PotionAmp");
if (potionType == EnumPotionType.Manual)
potionManualId = compound.getInteger("PotionManualId");
rangedRange = compound.getInteger("MaxFiringRange");
fireRate = compound.getInteger("FireRate");
@@ -180,7 +188,7 @@ public class DataStats {
pRender3D = compound.getBoolean("pRender3D");
pSpin = compound.getBoolean("pSpin");
pStick = compound.getBoolean("pStick");
pEffect = EnumPotionType.values()[compound.getInteger("pEffect") % EnumPotionType.values().length];
pEffect = EnumPotionType.fromOrdinal(compound.getInteger("pEffect"));
pTrail = EnumParticleType.values()[compound.getInteger("pTrail") % EnumParticleType.values().length];
pEffAmp = compound.getInteger("pEffAmp");
fireSound = compound.getString("FiringSound");
@@ -195,6 +203,8 @@ public class DataStats {
pCustom = ScriptParticle.fromNBT(compound.getCompoundTag("pCustom"));
if (pEffect == EnumPotionType.Fire)
pBurnItem = compound.getBoolean("pBurnItem");
if (pEffect == EnumPotionType.Manual)
pManualId = compound.getInteger("pManualId");
immuneToFire = compound.getBoolean("ImmuneToFire");
potionImmune = compound.getBoolean("PotionImmune");
@@ -12,7 +12,6 @@ import noppes.npcs.constants.EnumPotionType;
public class SubGuiNpcMeleeProperties extends SubGuiInterface implements ITextfieldListener {
private DataStats stats;
private String[] potionNames = new String[]{"gui.none", "tile.fire.name", "potion.poison", "potion.hunger", "potion.weakness", "potion.moveSlowdown", "potion.confusion", "potion.blindness", "potion.wither"};
public SubGuiNpcMeleeProperties(DataStats stats) {
this.stats = stats;
@@ -42,18 +41,28 @@ public class SubGuiNpcMeleeProperties extends SubGuiInterface implements ITextfi
getTextField(4).integersOnly = true;
getTextField(4).setMinMaxDefault(0, Integer.MAX_VALUE, 0);
addLabel(new GuiNpcLabel(5, "stats.meleeeffect", guiLeft + 5, guiTop + 135));
addButton(new GuiButtonBiDirectional(5, guiLeft + 85, guiTop + 130, 100, 20, potionNames, stats.potionType.ordinal()));
addButton(new GuiButtonBiDirectional(5, guiLeft + 85, guiTop + 130, 100, 20, EnumPotionType.getLangKeys(), stats.potionType.ordinal()));
if (stats.potionType == EnumPotionType.Manual) {
addLabel(new GuiNpcLabel(8, "effect.potionid", guiLeft + 198, guiTop + 119));
addTextField(new GuiNpcTextField(8, this, fontRendererObj, guiLeft + 200, guiTop + 132, 40, 18, stats.potionManualId + ""));
getTextField(8).integersOnly = true;
getTextField(8).setMinMaxDefault(0, Integer.MAX_VALUE, 0);
}
int y = guiTop + 160;
if (stats.potionType != EnumPotionType.None) {
addLabel(new GuiNpcLabel(6, "gui.time", guiLeft + 5, guiTop + 165));
addTextField(new GuiNpcTextField(6, this, fontRendererObj, guiLeft + 85, guiTop + 160, 40, 18, stats.potionDuration + ""));
addLabel(new GuiNpcLabel(6, "gui.time", guiLeft + 5, y + 5));
addTextField(new GuiNpcTextField(6, this, fontRendererObj, guiLeft + 85, y, 40, 18, stats.potionDuration + ""));
getTextField(6).integersOnly = true;
getTextField(6).setMinMaxDefault(1, Integer.MAX_VALUE, 5);
if (stats.potionType != EnumPotionType.Fire) {
addLabel(new GuiNpcLabel(7, "stats.amplify", guiLeft + 5, guiTop + 195));
addButton(new GuiButtonBiDirectional(7, guiLeft + 85, guiTop + 190, 52, 20, new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, stats.potionAmp));
y += 30;
addLabel(new GuiNpcLabel(7, "stats.amplify", guiLeft + 5, y + 5));
addButton(new GuiButtonBiDirectional(7, guiLeft + 85, y, 52, 20, new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, stats.potionAmp));
}
}
addButton(new GuiNpcButton(66, guiLeft + 190, guiTop + 190, 60, 20, "gui.done"));
addButton(new GuiNpcButton(66, guiLeft + 190, guiTop + ySize - 26, 60, 20, "gui.done"));
}
public void unFocused(GuiNpcTextField textfield) {
@@ -67,13 +76,15 @@ public class SubGuiNpcMeleeProperties extends SubGuiInterface implements ITextfi
stats.knockback = textfield.getInteger();
} else if (textfield.id == 6) {
stats.potionDuration = textfield.getInteger();
} else if (textfield.id == 8) {
stats.potionManualId = textfield.getInteger();
}
}
protected void actionPerformed(GuiButton guibutton) {
GuiNpcButton button = (GuiNpcButton) guibutton;
if (button.id == 5) {
stats.potionType = EnumPotionType.values()[button.getValue()];
stats.potionType = EnumPotionType.fromOrdinal(button.getValue());
initGui();
}
if (button.id == 7) {
@@ -15,7 +15,6 @@ import noppes.npcs.constants.EnumPotionType;
public class SubGuiNpcProjectiles extends SubGuiInterface implements ITextfieldListener, ISubGuiListener {
private DataStats stats;
private String[] potionNames = new String[]{"gui.none", "tile.fire.name", "potion.poison", "potion.hunger", "potion.weakness", "potion.moveSlowdown", "potion.confusion", "potion.blindness", "potion.wither"};
private String[] trailNames = new String[]{"gui.none", "trail.smoke", "trail.portal", "trail.redstone",
"trail.lightning", "trail.largesmoke", "trail.magic", "trail.enchant", "trail.crit",
"trail.explode", "trail.music", "trail.flame", "trail.lava", "trail.splash",
@@ -76,22 +75,29 @@ public class SubGuiNpcProjectiles extends SubGuiInterface implements ITextfieldL
}
addLabel(new GuiNpcLabel(7, "stats.rangedeffect", guiLeft + 210, y + 5));
addButton(new GuiButtonBiDirectional(4, guiLeft + 280, y, 100, 20, potionNames, stats.pEffect.ordinal()));
addButton(new GuiButtonBiDirectional(4, guiLeft + 280, y, 100, 20, EnumPotionType.getLangKeys(), stats.pEffect.ordinal()));
if (stats.pEffect != EnumPotionType.None) {
int internalY = y + 30;
if (stats.pEffect == EnumPotionType.Manual) {
addLabel(new GuiNpcLabel(110, "effect.potionid", guiLeft + 210, internalY + 5));
addTextField(new GuiNpcTextField(11, this, fontRendererObj, guiLeft + 330, internalY, 52, 20, stats.pManualId + ""));
getTextField(11).integersOnly = true;
getTextField(11).setMinMaxDefault(0, Integer.MAX_VALUE, 0);
internalY += 30;
}
addLabel(new GuiNpcLabel(50, "gui.time", guiLeft + 210, internalY + 5));
addTextField(new GuiNpcTextField(5, this, fontRendererObj, guiLeft + 330, internalY, 52, 20, stats.pDur + ""));
getTextField(5).integersOnly = true;
getTextField(5).setMinMaxDefault(1, Integer.MAX_VALUE, 5);
if (stats.pEffect != EnumPotionType.Fire) {
internalY += 30;
addLabel(new GuiNpcLabel(70, "stats.amplify", guiLeft + 210, internalY + 5));
addButton(new GuiButtonBiDirectional(10, guiLeft + 280, internalY, 52, 20, new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, stats.pEffAmp));
} else {
if (stats.pEffect == EnumPotionType.Fire) {
internalY += 30;
addLabel(new GuiNpcLabel(100, "stats.burnItem", guiLeft + 210, internalY + 5));
addButton(new GuiNpcButtonYesNo(100, guiLeft + 330, internalY, 52, 20, stats.pBurnItem));
} else {
internalY += 30;
addLabel(new GuiNpcLabel(70, "stats.amplify", guiLeft + 210, internalY + 5));
addButton(new GuiButtonBiDirectional(10, guiLeft + 280, internalY, 52, 20, new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, stats.pEffAmp));
}
}
@@ -126,6 +132,8 @@ public class SubGuiNpcProjectiles extends SubGuiInterface implements ITextfieldL
stats.pSpeed = textfield.getInteger();
} else if (textfield.id == 5) {
stats.pDur = textfield.getInteger();
} else if (textfield.id == 11) {
stats.pManualId = textfield.getInteger();
}
}
@@ -146,7 +154,7 @@ public class SubGuiNpcProjectiles extends SubGuiInterface implements ITextfieldL
stats.pArea = button.getValue();
}
if (button.id == 4) {
stats.pEffect = EnumPotionType.values()[button.getValue()];
stats.pEffect = EnumPotionType.fromOrdinal(button.getValue());
initGui();
}
if (button.id == 5) {
@@ -180,7 +180,7 @@ public abstract class GuiNPCInterface extends GuiScreen {
return;
}
}
for (GuiNpcTextField tf : new ArrayList<GuiNpcTextField>(textfields.values()))
if (tf.enabled)
tf.mouseClicked(i, j, k);
@@ -415,7 +415,7 @@ public abstract class GuiNPCInterface extends GuiScreen {
if (tf.hasHoverText())
tf.drawHover(i, j, subGui);
}
for (GuiScreen gui : extra.values())
gui.drawScreen(i, j, f);
@@ -439,8 +439,7 @@ public abstract class GuiNPCInterface extends GuiScreen {
int bottomHeight = 0;
int bottomTextureV = 0;
if (ySize < bgTextureHeight) {
// Keep bottom border from texture bottom (last 6 pixels of actual content)
bottomHeight = 6;
bottomHeight = -1;
topHeight = ySize - bottomHeight;
bottomTextureV = bgTextureHeight - bottomHeight;
}
@@ -1,5 +1,83 @@
package noppes.npcs.constants;
import net.minecraft.potion.Potion;
public enum EnumPotionType {
None, Fire, Poison, Hunger, Weakness, Slowness, Nausea, Blindness, Wither;
None("gui.none", -1),
Fire("tile.fire.name", -1),
Poison("potion.poison", Potion.poison.id),
Hunger("potion.hunger", Potion.hunger.id),
Weakness("potion.weakness", Potion.weakness.id),
Slowness("potion.moveSlowdown", Potion.moveSlowdown.id),
Nausea("potion.confusion", Potion.confusion.id),
Blindness("potion.blindness", Potion.blindness.id),
Wither("potion.wither", Potion.wither.id),
MiningFatigue("potion.digSlowDown", Potion.digSlowdown.id),
Manual("effect.manual", -1);
private final String langKey;
private final int potionId;
EnumPotionType(String langKey, int potionId) {
this.langKey = langKey;
this.potionId = potionId;
}
public String getLangKey() {
return langKey;
}
public int getPotionId() {
return potionId;
}
/**
* Returns the actual potion ID to apply.
* For Manual type, returns the provided manualId.
* For all others, returns the mapped potionId.
*/
public int getResolvedPotionId(int manualId) {
if (this == Manual) return manualId;
return potionId;
}
public static String[] getLangKeys() {
EnumPotionType[] types = values();
String[] keys = new String[types.length];
for (int i = 0; i < types.length; i++) keys[i] = types[i].langKey;
return keys;
}
/**
* Returns lang keys excluding None. For use in effect lists where None is not a valid choice.
*/
public static String[] getLangKeysNoNone() {
EnumPotionType[] types = values();
String[] keys = new String[types.length - 1];
for (int i = 1; i < types.length; i++) keys[i - 1] = types[i].langKey;
return keys;
}
/**
* Maps a 0-based index (excluding None) back to the corresponding EnumPotionType.
* Index 0 = Fire, 1 = Poison, etc.
*/
public static EnumPotionType fromIndexNoNone(int index) {
return fromOrdinal(index + 1);
}
public static EnumPotionType fromOrdinal(int ordinal) {
EnumPotionType[] values = values();
if (ordinal >= 0 && ordinal < values.length) {
return values[ordinal];
}
return None;
}
/**
* Checks if the given potion ID is valid and registered in the Minecraft potion registry.
*/
public static boolean isValidPotionId(int id) {
return id >= 0 && id < Potion.potionTypes.length && Potion.potionTypes[id] != null;
}
}
@@ -384,11 +384,13 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti
}
}
if (stats.potionType != EnumPotionType.None) {
if (stats.potionType != EnumPotionType.Fire)
((EntityLivingBase) receiver).addPotionEffect(new PotionEffect(this.getPotionEffect(stats.potionType), stats.potionDuration * 20, stats.potionAmp));
else
receiver.setFire(stats.potionDuration);
if (stats.potionType == EnumPotionType.Fire) {
receiver.setFire(stats.potionDuration);
} else if (stats.potionType != EnumPotionType.None) {
int potionId = stats.potionType.getResolvedPotionId(stats.potionManualId);
if (EnumPotionType.isValidPotionId(potionId)) {
((EntityLivingBase) receiver).addPotionEffect(new PotionEffect(potionId, stats.potionDuration * 20, stats.potionAmp));
}
}
return didAttack;
}
@@ -1043,29 +1045,6 @@ public abstract class EntityNPCInterface extends EntityCreature implements IEnti
return weight;
}
/*
* Used for getting the applied potion effect from dataStats.
*/
private int getPotionEffect(EnumPotionType p) {
switch (p) {
case Poison:
return Potion.poison.id;
case Hunger:
return Potion.hunger.id;
case Weakness:
return Potion.weakness.id;
case Slowness:
return Potion.moveSlowdown.id;
case Nausea:
return Potion.confusion.id;
case Blindness:
return Potion.blindness.id;
case Wither:
return Potion.wither.id;
default:
return 0;
}
}
@Override
public void setAir(int air) {
@@ -94,6 +94,7 @@ public class EntityProjectile extends EntityThrowable {
public boolean destroyTerrain = true;
public int explosiveRadius = 0;
public EnumPotionType effect = EnumPotionType.None;
public int manualPotionId = 0;
public boolean burnItem = true;
public int duration = 5;
public int amplify = 0;
@@ -508,12 +509,12 @@ public class EntityProjectile extends EntityThrowable {
}
}
if (this.effect != EnumPotionType.None && entityliving != null) {
if (this.effect != EnumPotionType.Fire) {
int p = this.getPotionEffect(effect);
entityliving.addPotionEffect(new PotionEffect(p, this.duration * 20, this.amplify));
} else {
movingobjectposition.entityHit.setFire(duration);
if (this.effect == EnumPotionType.Fire && entityliving != null) {
movingobjectposition.entityHit.setFire(duration);
} else if (this.effect != EnumPotionType.None && entityliving != null) {
int potionId = this.effect.getResolvedPotionId(this.manualPotionId);
if (EnumPotionType.isValidPotionId(potionId)) {
entityliving.addPotionEffect(new PotionEffect(potionId, this.duration * 20, this.amplify));
}
}
} else if (this.hasGravity() && (this.isArrow() || this.sticksToWalls())) {
@@ -637,21 +638,23 @@ public class EntityProjectile extends EntityThrowable {
d1 = 1.0D;
}
int i = this.getPotionEffect(effect);
int potionId = this.effect.getResolvedPotionId(this.manualPotionId);
if (Potion.potionTypes[i].isInstant()) {
Potion.potionTypes[i].affectEntity(this.getThrower(), entitylivingbase, this.amplify, d1);
} else {
int j = (int) (d1 * (double) this.duration + 0.5D);
if (EnumPotionType.isValidPotionId(potionId)) {
if (Potion.potionTypes[potionId].isInstant()) {
Potion.potionTypes[potionId].affectEntity(this.getThrower(), entitylivingbase, this.amplify, d1);
} else {
int j = (int) (d1 * (double) this.duration + 0.5D);
if (j > 20) {
entitylivingbase.addPotionEffect(new PotionEffect(i, j, this.amplify));
if (j > 20) {
entitylivingbase.addPotionEffect(new PotionEffect(potionId, j, this.amplify));
}
}
}
}
}
}
this.worldObj.playAuxSFX(2002, (int) Math.round(this.posX), (int) Math.round(this.posY), (int) Math.round(this.posZ), this.getPotionColor(this.effect));
this.worldObj.playAuxSFX(2002, (int) Math.round(this.posX), (int) Math.round(this.posY), (int) Math.round(this.posZ), this.getPotionColor());
}
}
@@ -693,6 +696,8 @@ public class EntityProjectile extends EntityThrowable {
par1NBTTagCompound.setBoolean("explosive", explosive);
par1NBTTagCompound.setInteger("accuracy", accuracy);
par1NBTTagCompound.setInteger("PotionEffect", effect.ordinal());
if (effect == EnumPotionType.Manual)
par1NBTTagCompound.setInteger("PotionManualId", manualPotionId);
par1NBTTagCompound.setString("trail", this.dataWatcher.getWatchableObjectString(22));
par1NBTTagCompound.setByte("Render3D", this.dataWatcher.getWatchableObjectByte(28));
par1NBTTagCompound.setByte("Spins", this.dataWatcher.getWatchableObjectByte(29));
@@ -723,7 +728,9 @@ public class EntityProjectile extends EntityThrowable {
this.accelerate = par1NBTTagCompound.getBoolean("accelerate");
this.explosive = par1NBTTagCompound.getBoolean("explosive");
this.accuracy = par1NBTTagCompound.getInteger("accuracy");
this.effect = EnumPotionType.values()[par1NBTTagCompound.getInteger("PotionEffect") % EnumPotionType.values().length];
this.effect = EnumPotionType.fromOrdinal(par1NBTTagCompound.getInteger("PotionEffect"));
if (effect == EnumPotionType.Manual)
this.manualPotionId = par1NBTTagCompound.getInteger("PotionManualId");
this.dataWatcher.updateObject(22, par1NBTTagCompound.getString("trail"));
this.dataWatcher.updateObject(23, Integer.valueOf(par1NBTTagCompound.getInteger("size")));
this.dataWatcher.updateObject(24, Byte.valueOf((byte) (par1NBTTagCompound.getBoolean("glows") ? 1 : 0)));
@@ -774,46 +781,12 @@ public class EntityProjectile extends EntityThrowable {
return this.thrower;
}
private int getPotionEffect(EnumPotionType p) {
switch (p) {
case Poison:
return Potion.poison.id;
case Hunger:
return Potion.hunger.id;
case Weakness:
return Potion.weakness.id;
case Slowness:
return Potion.moveSlowdown.id;
case Nausea:
return Potion.confusion.id;
case Blindness:
return Potion.blindness.id;
case Wither:
return Potion.wither.id;
default:
return 0;
}
}
private int getPotionColor(EnumPotionType p) {
switch (p) {
case Poison:
return 32660;
case Hunger:
return 32660;
case Weakness:
return 32696;
case Slowness:
return 32698;
case Nausea:
return 32732;
case Blindness:
return Potion.blindness.id;
case Wither:
return 32732;
default:
return 0;
private int getPotionColor() {
int potionId = this.effect.getResolvedPotionId(this.manualPotionId);
if (EnumPotionType.isValidPotionId(potionId)) {
return Potion.potionTypes[potionId].getLiquidColor();
}
return 0;
}
public void getStatProperties(DataStats stats) {
@@ -823,6 +796,7 @@ public class EntityProjectile extends EntityThrowable {
this.explosive = stats.pExplode;
this.explosiveRadius = stats.pArea;
this.effect = stats.pEffect;
this.manualPotionId = stats.pManualId;
this.burnItem = stats.pBurnItem;
this.duration = stats.pDur;
this.amplify = stats.pEffAmp;