Rename and clean up MapleMiniDungeon

This commit is contained in:
P0nk
2021-09-09 22:32:24 +02:00
parent efb1cb0632
commit 867624f980
4 changed files with 34 additions and 31 deletions

View File

@@ -74,7 +74,7 @@ public final class Channel {
private final Map<Integer, Integer> storedVars = new HashMap<>(); private final Map<Integer, Integer> storedVars = new HashMap<>();
private Set<Integer> playersAway = new HashSet<>(); private Set<Integer> playersAway = new HashSet<>();
private Map<ExpeditionType, Expedition> expeditions = new HashMap<>(); private Map<ExpeditionType, Expedition> expeditions = new HashMap<>();
private Map<Integer, MapleMiniDungeon> dungeons = new HashMap<>(); private Map<Integer, MiniDungeon> dungeons = new HashMap<>();
private List<ExpeditionType> expedType = new ArrayList<>(); private List<ExpeditionType> expedType = new ArrayList<>();
private Set<MapleMap> ownedMaps = Collections.synchronizedSet(Collections.newSetFromMap(new WeakHashMap<>())); private Set<MapleMap> ownedMaps = Collections.synchronizedSet(Collections.newSetFromMap(new WeakHashMap<>()));
private Event event; private Event event;
@@ -659,7 +659,7 @@ public final class Channel {
if(dungeons.containsKey(dungeonid)) return false; if(dungeons.containsKey(dungeonid)) return false;
MapleMiniDungeonInfo mmdi = MapleMiniDungeonInfo.getDungeon(dungeonid); MapleMiniDungeonInfo mmdi = MapleMiniDungeonInfo.getDungeon(dungeonid);
MapleMiniDungeon mmd = new MapleMiniDungeon(mmdi.getBase(), this.getMapFactory().getMap(mmdi.getDungeonId()).getTimeLimit()); // thanks Conrad for noticing hardcoded time limit for minidungeons MiniDungeon mmd = new MiniDungeon(mmdi.getBase(), this.getMapFactory().getMap(mmdi.getDungeonId()).getTimeLimit()); // thanks Conrad for noticing hardcoded time limit for minidungeons
dungeons.put(dungeonid, mmd); dungeons.put(dungeonid, mmd);
return true; return true;
@@ -668,7 +668,7 @@ public final class Channel {
} }
} }
public MapleMiniDungeon getMiniDungeon(int dungeonid) { public MiniDungeon getMiniDungeon(int dungeonid) {
lock.lock(); lock.lock();
try { try {
return dungeons.get(dungeonid); return dungeons.get(dungeonid);

View File

@@ -962,7 +962,7 @@ public class World {
if (MapleMiniDungeonInfo.isDungeonMap(oldLeaderMapid)) { if (MapleMiniDungeonInfo.isDungeonMap(oldLeaderMapid)) {
if (oldLeaderMapid != target.getMapId()) { if (oldLeaderMapid != target.getMapId()) {
MapleMiniDungeon mmd = mc.getClient().getChannelServer().getMiniDungeon(oldLeaderMapid); MiniDungeon mmd = mc.getClient().getChannelServer().getMiniDungeon(oldLeaderMapid);
if(mmd != null) { if(mmd != null) {
mmd.close(); mmd.close();
} }

View File

@@ -2444,7 +2444,7 @@ public class MapleMap {
} }
}, travelTime); }, travelTime);
} else if (MapleMiniDungeonInfo.isDungeonMap(mapid)) { } else if (MapleMiniDungeonInfo.isDungeonMap(mapid)) {
MapleMiniDungeon mmd = chr.getClient().getChannelServer().getMiniDungeon(mapid); MiniDungeon mmd = chr.getClient().getChannelServer().getMiniDungeon(mapid);
if (mmd != null) { if (mmd != null) {
mmd.registerPlayer(chr); mmd.registerPlayer(chr);
} }
@@ -2682,7 +2682,7 @@ public class MapleMap {
} }
if (MapleMiniDungeonInfo.isDungeonMap(mapid)) { if (MapleMiniDungeonInfo.isDungeonMap(mapid)) {
MapleMiniDungeon mmd = cserv.getMiniDungeon(mapid); MiniDungeon mmd = cserv.getMiniDungeon(mapid);
if (mmd != null) { if (mmd != null) {
if (!mmd.unregisterPlayer(chr)) { if (!mmd.unregisterPlayer(chr)) {
cserv.removeMiniDungeon(mapid); cserv.removeMiniDungeon(mapid);

View File

@@ -31,70 +31,73 @@ import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
/** /**
*
* @author Ronan * @author Ronan
*/ */
public class MapleMiniDungeon { public class MiniDungeon {
List<Character> players = new ArrayList<>(); List<Character> players = new ArrayList<>();
ScheduledFuture<?> timeoutTask = null; ScheduledFuture<?> timeoutTask = null;
Lock lock = MonitoredReentrantLockFactory.createLock(MonitoredLockType.MINIDUNGEON, true); Lock lock = MonitoredReentrantLockFactory.createLock(MonitoredLockType.MINIDUNGEON, true);
int baseMap; int baseMap;
long expireTime; long expireTime;
public MapleMiniDungeon(int base, long timeLimit) { public MiniDungeon(int base, long timeLimit) {
baseMap = base; baseMap = base;
expireTime = timeLimit * 1000; expireTime = timeLimit * 1000;
timeoutTask = TimerManager.getInstance().schedule(() -> close(), expireTime); timeoutTask = TimerManager.getInstance().schedule(() -> close(), expireTime);
expireTime += System.currentTimeMillis(); expireTime += System.currentTimeMillis();
} }
public boolean registerPlayer(Character chr) { public boolean registerPlayer(Character chr) {
int time = (int)((expireTime - System.currentTimeMillis()) / 1000); int time = (int) ((expireTime - System.currentTimeMillis()) / 1000);
if(time > 0) chr.sendPacket(PacketCreator.getClock(time)); if (time > 0) {
chr.sendPacket(PacketCreator.getClock(time));
}
lock.lock(); lock.lock();
try { try {
if(timeoutTask == null) return false; if (timeoutTask == null) {
return false;
}
players.add(chr); players.add(chr);
} finally { } finally {
lock.unlock(); lock.unlock();
} }
return true; return true;
} }
public boolean unregisterPlayer(Character chr) { public boolean unregisterPlayer(Character chr) {
chr.sendPacket(PacketCreator.removeClock()); chr.sendPacket(PacketCreator.removeClock());
lock.lock(); lock.lock();
try { try {
players.remove(chr); players.remove(chr);
if(players.isEmpty()) { if (players.isEmpty()) {
dispose(); dispose();
return false; return false;
} }
} finally { } finally {
lock.unlock(); lock.unlock();
} }
if (chr.isPartyLeader()) { // thanks Conrad for noticing party is not sent out of the MD as soon as leader leaves it if (chr.isPartyLeader()) { // thanks Conrad for noticing party is not sent out of the MD as soon as leader leaves it
close(); close();
} }
return true; return true;
} }
public void close() { public void close() {
lock.lock(); lock.lock();
try { try {
List<Character> lchr = new ArrayList<>(players); List<Character> lchr = new ArrayList<>(players);
for(Character chr : lchr) { for (Character chr : lchr) {
chr.changeMap(baseMap); chr.changeMap(baseMap);
} }
@@ -104,13 +107,13 @@ public class MapleMiniDungeon {
lock.unlock(); lock.unlock();
} }
} }
public void dispose() { public void dispose() {
lock.lock(); lock.lock();
try { try {
players.clear(); players.clear();
if(timeoutTask != null) { if (timeoutTask != null) {
timeoutTask.cancel(false); timeoutTask.cancel(false);
timeoutTask = null; timeoutTask = null;
} }