Rename and clean up AbstractMapleCharacterObject

This commit is contained in:
P0nk
2021-09-09 21:11:53 +02:00
parent 7dc3a2159a
commit da8837710a
2 changed files with 120 additions and 105 deletions

View File

@@ -37,46 +37,45 @@ import java.util.Map;
import java.util.concurrent.locks.Lock;
/**
*
* @author RonanLana
*/
public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMapleMapObject {
public abstract class AbstractCharacterObject extends AbstractAnimatedMapleMapObject {
protected MapleMap map;
protected int str, dex, luk, int_, hp, maxhp, mp, maxmp;
protected int hpMpApUsed, remainingAp;
protected int[] remainingSp = new int[10];
protected transient int clientmaxhp, clientmaxmp, localmaxhp = 50, localmaxmp = 5;
protected float transienthp = Float.NEGATIVE_INFINITY, transientmp = Float.NEGATIVE_INFINITY;
private AbstractCharacterListener listener = null;
protected Map<MapleStat, Integer> statUpdates = new HashMap<>();
protected Lock effLock = MonitoredReentrantLockFactory.createLock(MonitoredLockType.CHARACTER_EFF, true);
protected MonitoredReadLock statRlock;
protected MonitoredWriteLock statWlock;
protected AbstractMapleCharacterObject() {
protected AbstractCharacterObject() {
MonitoredReentrantReadWriteLock locks = new MonitoredReentrantReadWriteLock(MonitoredLockType.CHARACTER_STA, true);
statRlock = MonitoredReadLockFactory.createLock(locks);
statWlock = MonitoredWriteLockFactory.createLock(locks);
for (int i = 0; i < remainingSp.length; i++) {
remainingSp[i] = 0;
}
}
protected void setListener(AbstractCharacterListener listener) {
this.listener = listener;
}
public void setMap(MapleMap map) {
this.map = map;
}
public MapleMap getMap() {
return map;
}
public int getStr() {
statRlock.lock();
try {
@@ -85,7 +84,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
statRlock.unlock();
}
}
public int getDex() {
statRlock.lock();
try {
@@ -94,7 +93,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
statRlock.unlock();
}
}
public int getInt() {
statRlock.lock();
try {
@@ -112,7 +111,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
statRlock.unlock();
}
}
public int getRemainingAp() {
statRlock.lock();
try {
@@ -121,7 +120,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
statRlock.unlock();
}
}
protected int getRemainingSp(int jobid) {
statRlock.lock();
try {
@@ -139,7 +138,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
statRlock.unlock();
}
}
public int getHpMpApUsed() {
statRlock.lock();
try {
@@ -148,7 +147,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
statRlock.unlock();
}
}
public boolean isAlive() {
statRlock.lock();
try {
@@ -157,7 +156,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
statRlock.unlock();
}
}
public int getHp() {
statRlock.lock();
try {
@@ -166,7 +165,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
statRlock.unlock();
}
}
public int getMp() {
statRlock.lock();
try {
@@ -175,7 +174,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
statRlock.unlock();
}
}
public int getMaxHp() {
statRlock.lock();
try {
@@ -184,7 +183,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
statRlock.unlock();
}
}
public int getMaxMp() {
statRlock.lock();
try {
@@ -193,15 +192,15 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
statRlock.unlock();
}
}
public int getClientMaxHp() {
return clientmaxhp;
}
public int getClientMaxMp() {
return clientmaxmp;
}
public int getCurrentMaxHp() {
return localmaxhp;
}
@@ -209,40 +208,42 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
public int getCurrentMaxMp() {
return localmaxmp;
}
private void setHpMpApUsed(int mpApUsed) {
this.hpMpApUsed = mpApUsed;
}
private void dispatchHpChanged(final int oldHp) {
listener.onHpChanged(oldHp);
}
private void dispatchHpmpPoolUpdated() {
listener.onHpmpPoolUpdate();
}
private void dispatchStatUpdated() {
listener.onStatUpdate();
}
private void dispatchStatPoolUpdateAnnounced() {
listener.onAnnounceStatPoolUpdate();
}
protected void setHp(int newHp) {
int oldHp = hp;
int thp = newHp;
if (thp < 0) {
thp = 0;
} else if (thp > localmaxhp) {
thp = localmaxhp;
}
if (this.hp != thp) this.transienthp = Float.NEGATIVE_INFINITY;
if (this.hp != thp) {
this.transienthp = Float.NEGATIVE_INFINITY;
}
this.hp = thp;
dispatchHpChanged(oldHp);
}
@@ -253,35 +254,41 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
} else if (tmp > localmaxmp) {
tmp = localmaxmp;
}
if (this.mp != tmp) this.transientmp = Float.NEGATIVE_INFINITY;
if (this.mp != tmp) {
this.transientmp = Float.NEGATIVE_INFINITY;
}
this.mp = tmp;
}
private void setRemainingAp(int remainingAp) {
this.remainingAp = remainingAp;
}
private void setRemainingSp(int remainingSp, int skillbook) {
this.remainingSp[skillbook] = remainingSp;
}
protected void setMaxHp(int hp_) {
if (this.maxhp < hp_) this.transienthp = Float.NEGATIVE_INFINITY;
if (this.maxhp < hp_) {
this.transienthp = Float.NEGATIVE_INFINITY;
}
this.maxhp = hp_;
this.clientmaxhp = Math.min(30000, hp_);
}
protected void setMaxMp(int mp_) {
if (this.maxmp < mp_) this.transientmp = Float.NEGATIVE_INFINITY;
if (this.maxmp < mp_) {
this.transientmp = Float.NEGATIVE_INFINITY;
}
this.maxmp = mp_;
this.clientmaxmp = Math.min(30000, mp_);
}
private static long clampStat(int v, int min, int max) {
return (v < min) ? min : ((v > max) ? max : v);
}
private static long calcStatPoolNode(Integer v, int displacement) {
long r;
if (v == null) {
@@ -289,21 +296,21 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
} else {
r = clampStat(v, -32767, 32767);
}
return ((r & 0x0FFFF) << displacement);
}
private static long calcStatPoolLong(Integer v1, Integer v2, Integer v3, Integer v4) {
long ret = 0;
ret |= calcStatPoolNode(v1, 48);
ret |= calcStatPoolNode(v2, 32);
ret |= calcStatPoolNode(v3, 16);
ret |= calcStatPoolNode(v4, 0);
return ret;
}
private void changeStatPool(Long hpMpPool, Long strDexIntLuk, Long newSp, int newAp, boolean silent) {
effLock.lock();
statWlock.lock();
@@ -317,7 +324,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
short newMp = (short) (hpMpPool >> 32);
short newMaxHp = (short) (hpMpPool >> 16);
short newMaxMp = hpMpPool.shortValue();
if (newMaxHp != Short.MIN_VALUE) {
if (newMaxHp < 50) {
newMaxHp = 50;
@@ -384,11 +391,11 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
statUpdate = true;
}
if (newSp != null) {
short sp = (short) (newSp >> 16);
short skillbook = newSp.shortValue();
setRemainingSp(sp, skillbook);
statUpdates.put(MapleStat.AVAILABLESP, remainingSp[skillbook]);
}
@@ -397,7 +404,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
if (poolUpdate) {
dispatchHpmpPoolUpdated();
}
if (statUpdate) {
dispatchStatUpdated();
}
@@ -411,64 +418,64 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
effLock.unlock();
}
}
public void healHpMp() {
updateHpMp(30000);
}
public void updateHpMp(int x) {
updateHpMp(x, x);
}
public void updateHpMp(int newhp, int newmp) {
changeHpMp(newhp, newmp, false);
}
protected void changeHpMp(int newhp, int newmp, boolean silent) {
changeHpMpPool(newhp, newmp, null, null, silent);
}
private void changeHpMpPool(Integer hp, Integer mp, Integer maxhp, Integer maxmp, boolean silent) {
long hpMpPool = calcStatPoolLong(hp, mp, maxhp, maxmp);
changeStatPool(hpMpPool, null, null, -1, silent);
}
public void updateHp(int hp) {
updateHpMaxHp(hp, null);
}
public void updateMaxHp(int maxhp) {
updateHpMaxHp(null, maxhp);
}
public void updateHpMaxHp(int hp, int maxhp) {
updateHpMaxHp(Integer.valueOf(hp), Integer.valueOf(maxhp));
}
private void updateHpMaxHp(Integer hp, Integer maxhp) {
changeHpMpPool(hp, null, maxhp, null, false);
}
public void updateMp(int mp) {
updateMpMaxMp(mp, null);
}
public void updateMaxMp(int maxmp) {
updateMpMaxMp(null, maxmp);
}
public void updateMpMaxMp(int mp, int maxmp) {
updateMpMaxMp(Integer.valueOf(mp), Integer.valueOf(maxmp));
}
private void updateMpMaxMp(Integer mp, Integer maxmp) {
changeHpMpPool(null, mp, null, maxmp, false);
}
public void updateMaxHpMaxMp(int maxhp, int maxmp) {
changeHpMpPool(null, null, maxhp, maxmp, false);
}
protected void enforceMaxHpMp() {
effLock.lock();
statWlock.lock();
@@ -481,7 +488,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
effLock.unlock();
}
}
public int safeAddHP(int delta) {
effLock.lock();
statWlock.lock();
@@ -497,7 +504,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
effLock.unlock();
}
}
public void addHP(int delta) {
effLock.lock();
statWlock.lock();
@@ -508,7 +515,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
effLock.unlock();
}
}
public void addMP(int delta) {
effLock.lock();
statWlock.lock();
@@ -530,7 +537,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
effLock.unlock();
}
}
protected void addMaxMPMaxHP(int hpdelta, int mpdelta, boolean silent) {
effLock.lock();
statWlock.lock();
@@ -541,7 +548,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
effLock.unlock();
}
}
public void addMaxHP(int delta) {
effLock.lock();
statWlock.lock();
@@ -552,7 +559,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
effLock.unlock();
}
}
public void addMaxMP(int delta) {
effLock.lock();
statWlock.lock();
@@ -563,39 +570,39 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
effLock.unlock();
}
}
private void setStr(int str) {
this.str = str;
}
private void setDex(int dex) {
this.dex = dex;
}
private void setInt(int int_) {
this.int_ = int_;
}
private void setLuk(int luk) {
this.luk = luk;
}
public boolean assignStr(int x) {
return assignStrDexIntLuk(x, null, null, null);
}
public boolean assignDex(int x) {
return assignStrDexIntLuk(null, x, null, null);
}
public boolean assignInt(int x) {
return assignStrDexIntLuk(null, null, x, null);
}
public boolean assignLuk(int x) {
return assignStrDexIntLuk(null, null, null, x);
}
public boolean assignHP(int deltaHP, int deltaAp) {
effLock.lock();
statWlock.lock();
@@ -603,7 +610,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
if (remainingAp - deltaAp < 0 || hpMpApUsed + deltaAp < 0 || maxhp >= 30000) {
return false;
}
long hpMpPool = calcStatPoolLong(null, null, maxhp + deltaHP, maxmp);
long strDexIntLuk = calcStatPoolLong(str, dex, int_, luk);
@@ -615,7 +622,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
effLock.unlock();
}
}
public boolean assignMP(int deltaMP, int deltaAp) {
effLock.lock();
statWlock.lock();
@@ -635,15 +642,15 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
effLock.unlock();
}
}
private static int apAssigned(Integer x) {
return x != null ? x : 0;
}
public boolean assignStrDexIntLuk(int deltaStr, int deltaDex, int deltaInt, int deltaLuk) {
return assignStrDexIntLuk(Integer.valueOf(deltaStr), Integer.valueOf(deltaDex), Integer.valueOf(deltaInt), Integer.valueOf(deltaLuk));
}
private boolean assignStrDexIntLuk(Integer deltaStr, Integer deltaDex, Integer deltaInt, Integer deltaLuk) {
effLock.lock();
statWlock.lock();
@@ -654,11 +661,19 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
}
int newStr = str, newDex = dex, newInt = int_, newLuk = luk;
if (deltaStr != null) newStr += deltaStr; // thanks Rohenn for noticing an NPE case after "null" started being used
if (deltaDex != null) newDex += deltaDex;
if (deltaInt != null) newInt += deltaInt;
if (deltaLuk != null) newLuk += deltaLuk;
if (deltaStr != null) {
newStr += deltaStr; // thanks Rohenn for noticing an NPE case after "null" started being used
}
if (deltaDex != null) {
newDex += deltaDex;
}
if (deltaInt != null) {
newInt += deltaInt;
}
if (deltaLuk != null) {
newLuk += deltaLuk;
}
if (newStr < 4 || newStr > YamlConfig.config.server.MAX_AP) {
return false;
}
@@ -683,11 +698,11 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
effLock.unlock();
}
}
public void updateStrDexIntLuk(int x) {
updateStrDexIntLuk(x, x, x, x, -1);
}
public void changeRemainingAp(int x, boolean silent) {
effLock.lock();
statWlock.lock();
@@ -698,7 +713,7 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
effLock.unlock();
}
}
public void gainAp(int deltaAp, boolean silent) {
effLock.lock();
statWlock.lock();
@@ -709,26 +724,26 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
effLock.unlock();
}
}
protected void updateStrDexIntLuk(int str, int dex, int int_, int luk, int remainingAp) {
changeStrDexIntLuk(str, dex, int_, luk, remainingAp, false);
}
private void changeStrDexIntLuk(Integer str, Integer dex, Integer int_, Integer luk, int remainingAp, boolean silent) {
long strDexIntLuk = calcStatPoolLong(str, dex, int_, luk);
changeStatPool(null, strDexIntLuk, null, remainingAp, silent);
}
private void changeStrDexIntLukSp(Integer str, Integer dex, Integer int_, Integer luk, int remainingAp, int remainingSp, int skillbook, boolean silent) {
long strDexIntLuk = calcStatPoolLong(str, dex, int_, luk);
long sp = calcStatPoolLong(0, 0, remainingSp, skillbook);
changeStatPool(null, strDexIntLuk, sp, remainingAp, silent);
}
protected void updateStrDexIntLukSp(int str, int dex, int int_, int luk, int remainingAp, int remainingSp, int skillbook) {
changeStrDexIntLukSp(str, dex, int_, luk, remainingAp, remainingSp, skillbook, false);
}
protected void setRemainingSp(int[] sps) {
effLock.lock();
statWlock.lock();
@@ -739,16 +754,16 @@ public abstract class AbstractMapleCharacterObject extends AbstractAnimatedMaple
effLock.unlock();
}
}
protected void updateRemainingSp(int remainingSp, int skillbook) {
changeRemainingSp(remainingSp, skillbook, false);
}
protected void changeRemainingSp(int remainingSp, int skillbook, boolean silent) {
long sp = calcStatPoolLong(0, 0, remainingSp, skillbook);
changeStatPool(null, null, sp, Short.MIN_VALUE, silent);
}
public void gainSp(int deltaSp, int skillbook, boolean silent) {
effLock.lock();
statWlock.lock();

View File

@@ -93,7 +93,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.regex.Pattern;
public class Character extends AbstractMapleCharacterObject {
public class Character extends AbstractCharacterObject {
private static final MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
private static final String LEVEL_200 = "[Congrats] %s has reached Level %d! Congratulate %s on such an amazing achievement!";
private static final String[] BLOCKED_NAMES = {"admin", "owner", "moderator", "intern", "donor", "administrator", "FREDRICK", "help", "helper", "alert", "notice", "maplestory", "fuck", "wizet", "fucking", "negro", "fuk", "fuc", "penis", "pussy", "asshole", "gay",