Removed enhanced For from World and WorldRegion.

This commit is contained in:
MobiusDevelopment
2020-08-30 19:26:33 +00:00
parent b88d13a836
commit d86b65da9b
67 changed files with 2640 additions and 1929 deletions

View File

@@ -16,7 +16,6 @@
*/
package org.l2jmobius.gameserver.handler;
import java.io.IOException;
import java.util.List;
import org.l2jmobius.gameserver.model.Skill;
@@ -30,9 +29,8 @@ public interface ISkillHandler
* @param creature
* @param skill
* @param targets
* @throws IOException
*/
void useSkill(Creature creature, Skill skill, List<Creature> targets) throws IOException;
void useSkill(Creature creature, Skill skill, List<Creature> targets);
/**
* this method is called at initialization to register all the item ids automatically

View File

@@ -16,7 +16,6 @@
*/
package org.l2jmobius.gameserver.handler.skillhandlers;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
@@ -675,14 +674,14 @@ public class Disablers implements ISkillHandler
final List<Creature> tgts = new ArrayList<>();
tgts.add(target);
try
{
healhandler.useSkill(creature, skill, tgts);
}
catch (IOException e)
{
LOGGER.warning(e.getMessage());
}
// try
// {
healhandler.useSkill(creature, skill, tgts);
// }
// catch (IOException e)
// {
// LOGGER.warning(e.getMessage());
// }
}
}
for (String stat : negateEffectTypes)

View File

@@ -115,21 +115,9 @@ public class World
*/
public void removeObjects(List<WorldObject> list)
{
for (WorldObject o : list)
for (int i = 0; i < list.size(); i++)
{
_allObjects.remove(o.getObjectId());
}
}
/**
* Removes the objects.
* @param objects the objects
*/
public void removeObjects(WorldObject[] objects)
{
for (WorldObject o : objects)
{
_allObjects.remove(o.getObjectId());
_allObjects.remove(list.get(i).getObjectId());
}
}
@@ -138,12 +126,12 @@ public class World
* <br>
* <b><u>Example of use</u>:</b><br>
* <li>Client packets : Action, AttackRequest, RequestJoinParty, RequestJoinPledge...</li><br>
* @param oID Identifier of the WorldObject
* @param objectId Identifier of the WorldObject
* @return the object
*/
public WorldObject findObject(int oID)
public WorldObject findObject(int objectId)
{
return _allObjects.get(oID);
return _allObjects.get(objectId);
}
/**
@@ -308,12 +296,10 @@ public class World
}
// Go through the visible objects contained in the circular area
for (WorldObject wo : getVisibleObjects(object, 2000))
final List<WorldObject> visibleObjects = getVisibleObjects(object, 2000);
for (int i = 0; i < visibleObjects.size(); i++)
{
if (wo == null)
{
continue;
}
final WorldObject wo = visibleObjects.get(i);
// Add the object in WorldObjectHashSet(WorldObject) _knownObjects of the visible Creature according to conditions :
// - Creature is visible
@@ -342,8 +328,11 @@ public class World
}
// Go through the visible objects contained in the circular area
for (WorldObject wo : getVisibleObjects(object, 2000))
final List<WorldObject> visibleObjects = getVisibleObjects(object, 2000);
for (int i = 0; i < visibleObjects.size(); i++)
{
final WorldObject wo = visibleObjects.get(i);
// Add the object in WorldObjectHashSet(WorldObject) _knownObjects of the visible Creature according to conditions :
// - Creature is visible
// - object is not already known
@@ -417,10 +406,14 @@ public class World
oldRegion.removeVisibleObject(object);
// Go through all surrounding WorldRegion Creatures
for (WorldRegion worldRegion : oldRegion.getSurroundingRegions())
final WorldRegion[] surroundingRegions = oldRegion.getSurroundingRegions();
for (int i = 0; i < surroundingRegions.length; i++)
{
for (WorldObject wo : worldRegion.getVisibleObjects())
final List<WorldObject> visibleObjects = surroundingRegions[i].getVisibleObjects();
for (int j = 0; j < visibleObjects.size(); j++)
{
final WorldObject wo = visibleObjects.get(j);
// Remove the WorldObject from the WorldObjectHashSet(WorldObject) _knownObjects of the surrounding WorldRegion Creatures
// If object is a PlayerInstance, remove the WorldObject from the WorldObjectHashSet(PlayerInstance) _knownPlayer of the surrounding WorldRegion Creatures
// If object is targeted by one of the surrounding WorldRegion Creatures, cancel ATTACK and cast
@@ -489,10 +482,13 @@ public class World
final List<WorldObject> result = new ArrayList<>();
// Go through the list of region
for (WorldRegion worldRegion : region.getSurroundingRegions())
final WorldRegion[] surroundingRegions = region.getSurroundingRegions();
for (int i = 0; i < surroundingRegions.length; i++)
{
for (WorldObject wo : worldRegion.getVisibleObjects())
final List<WorldObject> visibleObjects = surroundingRegions[i].getVisibleObjects();
for (int j = 0; j < visibleObjects.size(); j++)
{
final WorldObject wo = visibleObjects.get(j);
if (wo == null)
{
continue;
@@ -552,11 +548,14 @@ public class World
final List<WorldObject> result = new ArrayList<>();
// Go through the list of region
for (WorldRegion worldRegion : region.getSurroundingRegions())
final WorldRegion[] surroundingRegions = region.getSurroundingRegions();
for (int i = 0; i < surroundingRegions.length; i++)
{
// Go through visible objects of the selected region
for (WorldObject wo : worldRegion.getVisibleObjects())
final List<WorldObject> visibleObjects = surroundingRegions[i].getVisibleObjects();
for (int j = 0; j < visibleObjects.size(); j++)
{
final WorldObject wo = visibleObjects.get(j);
if (wo == null)
{
continue;
@@ -614,10 +613,13 @@ public class World
final List<WorldObject> result = new ArrayList<>();
// Go through visible object of the selected region
for (WorldRegion worldRegion : object.getWorldRegion().getSurroundingRegions())
final WorldRegion[] surroundingRegions = object.getWorldRegion().getSurroundingRegions();
for (int i = 0; i < surroundingRegions.length; i++)
{
for (WorldObject wo : worldRegion.getVisibleObjects())
final List<WorldObject> visibleObjects = surroundingRegions[i].getVisibleObjects();
for (int j = 0; j < visibleObjects.size(); j++)
{
final WorldObject wo = visibleObjects.get(j);
if (wo == null)
{
continue;
@@ -644,58 +646,6 @@ public class World
return result;
}
/**
* Return all visible players of the WorldRegion object's and of its surrounding WorldRegion.<br>
* <br>
* <b><u>Concept</u>:</b><br>
* <br>
* All visible object are identified in <b>_visibleObjects</b> of their current WorldRegion<br>
* All surrounding WorldRegion are identified in <b>_surroundingRegions</b> of the selected WorldRegion in order to scan a large area around a WorldObject<br>
* <br>
* <b><u>Example of use</u>:</b><br>
* <li>Find Close Objects for Creature</li><br>
* @param object WorldObject that determine the current WorldRegion
* @return the visible playable
*/
public List<PlayerInstance> getVisiblePlayers(WorldObject object)
{
final WorldRegion region = object.getWorldRegion();
if (region == null)
{
return Collections.emptyList();
}
// Create a list in order to contain all visible WorldObject
final List<PlayerInstance> result = new ArrayList<>();
// Go through the list of region
for (WorldRegion worldRegion : region.getSurroundingRegions())
{
// Go through visible object of the selected region
for (PlayerInstance playable : worldRegion.getAllPlayers())
{
if (playable == null)
{
continue;
}
if (playable.equals(object))
{
continue; // skip our own character
}
if (!playable.isSpawned())
{
continue; // skip dying objects
}
result.add(playable);
}
}
return result;
}
/**
* Calculate the current WorldRegions of the object according to its position (x,y).<br>
* <br>
@@ -793,24 +743,6 @@ public class World
LOGGER.info("All visible NPCs deleted.");
}
/**
* Gets the account players.
* @param account the account_name
* @return the account players
*/
public List<PlayerInstance> getAccountPlayers(String account)
{
final List<PlayerInstance> result = new ArrayList<>();
for (PlayerInstance actual : _allPlayers.values())
{
if (actual.getAccountName().equals(account))
{
result.add(actual);
}
}
return result;
}
public static World getInstance()
{
return SingletonHolder.INSTANCE;

View File

@@ -16,10 +16,9 @@
*/
package org.l2jmobius.gameserver.model;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Logger;
@@ -31,9 +30,7 @@ import org.l2jmobius.gameserver.ai.SiegeGuardAI;
import org.l2jmobius.gameserver.datatables.sql.SpawnTable;
import org.l2jmobius.gameserver.model.actor.Attackable;
import org.l2jmobius.gameserver.model.actor.Creature;
import org.l2jmobius.gameserver.model.actor.Playable;
import org.l2jmobius.gameserver.model.actor.instance.NpcInstance;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.spawn.Spawn;
import org.l2jmobius.gameserver.model.zone.ZoneManager;
import org.l2jmobius.gameserver.model.zone.ZoneType;
@@ -44,8 +41,7 @@ public class WorldRegion
{
private static final Logger LOGGER = Logger.getLogger(WorldRegion.class.getName());
private final Collection<PlayerInstance> _playerObjects = ConcurrentHashMap.newKeySet();
private final Collection<WorldObject> _visibleObjects = ConcurrentHashMap.newKeySet();
private final List<WorldObject> _visibleObjects = new ArrayList<>();
private WorldRegion[] _surroundingRegions;
private final int _regionX;
private final int _regionY;
@@ -74,7 +70,6 @@ public class WorldRegion
{
return;
}
_zoneManager.unregisterZone(zone);
}
@@ -127,8 +122,9 @@ public class WorldRegion
{
if (!isOn)
{
for (WorldObject wo : _visibleObjects)
for (int i = 0; i < _visibleObjects.size(); i++)
{
final WorldObject wo = _visibleObjects.get(i);
if (wo instanceof Attackable)
{
final Attackable mob = (Attackable) wo;
@@ -174,8 +170,9 @@ public class WorldRegion
}
else
{
for (WorldObject wo : _visibleObjects)
for (int i = 0; i < _visibleObjects.size(); i++)
{
final WorldObject wo = _visibleObjects.get(i);
if (wo instanceof Attackable)
{
// Start HP/MP/CP Regeneration task
@@ -190,20 +187,28 @@ public class WorldRegion
}
}
public Boolean isActive()
public boolean isActive()
{
return _active;
}
// check if all 9 neighbors (including self) are inactive or active but with no players.
// returns true if the above condition is met.
public Boolean areNeighborsEmpty()
public boolean areNeighborsEmpty()
{
for (WorldRegion worldRegion : _surroundingRegions)
for (int i = 0; i < _surroundingRegions.length; i++)
{
if (worldRegion.isActive() && !worldRegion.getAllPlayers().isEmpty())
final WorldRegion worldRegion = _surroundingRegions[i];
if (worldRegion.isActive())
{
return false;
final List<WorldObject> regionObjects = worldRegion.getVisibleObjects();
for (int j = 0; j < regionObjects.size(); j++)
{
if (regionObjects.get(j).isPlayable())
{
return false;
}
}
}
}
return true;
@@ -246,9 +251,9 @@ public class WorldRegion
// Then, set a timer to activate the neighbors.
_neighborsTask = ThreadPool.schedule(() ->
{
for (WorldRegion worldRegion : _surroundingRegions)
for (int i = 0; i < _surroundingRegions.length; i++)
{
worldRegion.setActive(true);
_surroundingRegions[i].setActive(true);
}
}, 1000 * Config.GRID_NEIGHBOR_TURNON_TIME);
}
@@ -272,8 +277,9 @@ public class WorldRegion
// Suggest means: first check if a neighbor has PlayerInstances in it. If not, deactivate.
_neighborsTask = ThreadPool.schedule(() ->
{
for (WorldRegion worldRegion : _surroundingRegions)
for (int i = 0; i < _surroundingRegions.length; i++)
{
final WorldRegion worldRegion = _surroundingRegions[i];
if (worldRegion.areNeighborsEmpty())
{
worldRegion.setActive(false);
@@ -296,18 +302,19 @@ public class WorldRegion
return;
}
_visibleObjects.add(object);
if (object instanceof PlayerInstance)
synchronized (_visibleObjects)
{
_playerObjects.add((PlayerInstance) object);
// if this is the first player to enter the region, activate self & neighbors
if ((_playerObjects.size() == 1) && !Config.GRIDS_ALWAYS_ON)
if (!_visibleObjects.contains(object))
{
startActivation();
_visibleObjects.add(object);
}
}
// If this is the first player to enter the region, activate self and neighbors.
if (object.isPlayable() && !_active && !Config.GRIDS_ALWAYS_ON)
{
startActivation();
}
}
/**
@@ -323,22 +330,36 @@ public class WorldRegion
return;
}
_visibleObjects.remove(object);
if (object instanceof Playable)
if (_visibleObjects.isEmpty())
{
_playerObjects.remove(object);
if (_playerObjects.isEmpty() && !Config.GRIDS_ALWAYS_ON)
{
startDeactivation();
}
return;
}
synchronized (_visibleObjects)
{
_visibleObjects.remove(object);
}
if (object.isPlayable() && areNeighborsEmpty() && !Config.GRIDS_ALWAYS_ON)
{
startDeactivation();
}
}
public void setSurroundingRegions(WorldRegion[] regions)
{
_surroundingRegions = regions;
// Make sure that this region is always the first region to improve bulk operations when this region should be updated first.
for (int i = 0; i < _surroundingRegions.length; i++)
{
if (_surroundingRegions[i] == this)
{
final WorldRegion first = _surroundingRegions[0];
_surroundingRegions[0] = this;
_surroundingRegions[i] = first;
}
}
}
/**
@@ -349,12 +370,7 @@ public class WorldRegion
return _surroundingRegions;
}
public Collection<PlayerInstance> getAllPlayers()
{
return _playerObjects;
}
public Collection<WorldObject> getVisibleObjects()
public List<WorldObject> getVisibleObjects()
{
return _visibleObjects;
}
@@ -370,8 +386,9 @@ public class WorldRegion
public synchronized void deleteVisibleNpcSpawns()
{
LOGGER.info("Deleting all visible NPCs in Region: " + getName());
for (WorldObject obj : _visibleObjects)
for (int i = 0; i < _visibleObjects.size(); i++)
{
final WorldObject obj = _visibleObjects.get(i);
if (obj instanceof NpcInstance)
{
final NpcInstance target = (NpcInstance) obj;
@@ -404,8 +421,10 @@ public class WorldRegion
final int down = y - range;
final int left = x + range;
final int right = x - range;
for (ZoneType e : _zoneManager.getZones())
final List<ZoneType> zones = _zoneManager.getZones();
for (int i = 0; i < zones.size(); i++)
{
final ZoneType e = zones.get(i);
if (e instanceof PeaceZone)
{
if (e.isInsideZone(x, up, z))

View File

@@ -141,9 +141,9 @@ public class WorldObjectKnownList
else
{
// Go through all visible WorldObject near the Creature
for (WorldObject playable : World.getInstance().getVisiblePlayers(_activeObject))
for (WorldObject object : World.getInstance().getVisibleObjects(_activeObject))
{
if (playable == null)
if ((object == null) || !object.isPlayable())
{
return;
}
@@ -151,7 +151,7 @@ public class WorldObjectKnownList
// Try to add object to active object's known objects
// Creature only needs to see visible PlayerInstance and PlayableInstance, when moving. Other creatures are currently only known from initial spawn area.
// Possibly look into getDistanceToForgetObject values before modifying this approach...
addKnownObject(playable);
addKnownObject(object);
}
}
}

View File

@@ -16,7 +16,6 @@
*/
package org.l2jmobius.gameserver.model.items;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@@ -404,36 +403,36 @@ public class Weapon extends Item
continue; // Skill condition not met
}
try
// try
// {
// Get the skill handler corresponding to the skill type
final ISkillHandler handler = SkillHandler.getInstance().getSkillHandler(skill.getSkillType());
final List<Creature> targets = new ArrayList<>();
targets.add(target);
// Launch the magic skill and calculate its effects
if (handler != null)
{
// Get the skill handler corresponding to the skill type
final ISkillHandler handler = SkillHandler.getInstance().getSkillHandler(skill.getSkillType());
final List<Creature> targets = new ArrayList<>();
targets.add(target);
// Launch the magic skill and calculate its effects
if (handler != null)
{
handler.useSkill(caster, skill, targets);
}
else
{
skill.useSkill(caster, targets);
}
if ((caster instanceof PlayerInstance) && (target instanceof NpcInstance))
{
for (Quest quest : ((NpcInstance) target).getTemplate().getEventQuests(EventType.ON_SKILL_USE))
{
quest.notifySkillUse((NpcInstance) target, (PlayerInstance) caster, skill);
}
}
output = true;
handler.useSkill(caster, skill, targets);
}
catch (IOException e)
else
{
skill.useSkill(caster, targets);
}
if ((caster instanceof PlayerInstance) && (target instanceof NpcInstance))
{
for (Quest quest : ((NpcInstance) target).getTemplate().getEventQuests(EventType.ON_SKILL_USE))
{
quest.notifySkillUse((NpcInstance) target, (PlayerInstance) caster, skill);
}
}
output = true;
// }
// catch (IOException e)
// {
// }
}
return output;