Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. // Copyright © 2011 J.M. Linden
  2. // This program is free software; you can redistribute it and/or
  3. // modify it under the terms of the GNU General Public License
  4. // as published by the Free Software Foundation; either version 2
  5. // of the License, or (at your option) any later version.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. // GNU General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU General Public License
  13. // along with this program; if not, write to the Free Software
  14. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  15.  
  16. package org.mike.gielinor.game.service.event;
  17.  
  18. import static org.mike.gielinor.game.GameConstants.CYCLE_TIME;
  19. import static org.mike.gielinor.game.model.World.getWorld;
  20.  
  21. import java.util.ArrayList;
  22. import java.util.Iterator;
  23. import java.util.List;
  24.  
  25. import org.mike.core.service.event.Event;
  26. import org.mike.core.service.task.ConsecutiveTask;
  27. import org.mike.core.service.task.ParallelTask;
  28. import org.mike.core.service.task.Task;
  29. import org.mike.gielinor.game.model.Entity;
  30. import org.mike.gielinor.game.model.NPC;
  31. import org.mike.gielinor.game.model.Player;
  32. import org.mike.gielinor.game.model.update.UpdateAttributes;
  33. import org.mike.gielinor.game.service.GameEngine;
  34.  
  35. /**
  36.  * A {@link Task} which globally updates all registered {@link Entity entities}
  37.  * each server cycle.
  38.  *
  39.  * @author J.M. Linden
  40.  *
  41.  */
  42. public final class EntityUpdateEvent extends Event {
  43.  
  44.     /**
  45.      * The {@link List} of all tick {@link Task}s.
  46.      */
  47.     private final List<Task> tickTasks = new ArrayList<Task>();
  48.  
  49.     /**
  50.      * The {@link List} of all update {@link Task}s.
  51.      */
  52.     private final List<Task> updateTasks = new ArrayList<Task>();
  53.  
  54.     /**
  55.      * The {@link List} of all reset {@link Task}s.
  56.      */
  57.     private final List<Task> resetTasks = new ArrayList<Task>();
  58.  
  59.     /**
  60.      * The {@link GameEngine} to {@link #execute() execute} the cycle
  61.      * {@link Task}s in.
  62.      */
  63.     private final GameEngine engine = getWorld().getEngine();
  64.  
  65.     /**
  66.      * Creates a new {@link EntityUpdateEvent} with the default delay of the
  67.      * server's cycle time..
  68.      */
  69.     public EntityUpdateEvent() {
  70.         super(CYCLE_TIME);
  71.     }
  72.  
  73.     @Override
  74.     public void execute() {
  75.         tickTasks.clear();
  76.         updateTasks.clear();
  77.         resetTasks.clear();
  78.  
  79.         for (NPC npc : getWorld().getNPCs()) {
  80.             // fetch the cached updating tasks
  81.             final UpdateAttributes ua = npc.getUpdateAttributes();
  82.             tickTasks.add(ua.getTickTask());
  83.             resetTasks.add(ua.getResetTask());
  84.         }
  85.  
  86.         final Iterator<Player> it$ = getWorld().getPlayers().iterator();
  87.         while (it$.hasNext()) {
  88.             final Player player = it$.next();
  89.             if (!player.getChannel().isConnected()) {
  90.                 it$.remove();
  91.             } else {
  92.                 // fetch the cached updating tasks
  93.                 final UpdateAttributes ua = player.getUpdateAttributes();
  94.                 tickTasks.add(ua.getTickTask());
  95.                 updateTasks.add(new ConsecutiveTask(ua.getUpdateTasks()));
  96.                 resetTasks.add(ua.getResetTask());
  97.             }
  98.         }
  99.  
  100.         // tick tasks can no longer be parallel due to region code
  101.         final Task tickTask = new ConsecutiveTask(
  102.                 tickTasks.toArray(new Task[0]));
  103.         final Task updateTask = new ParallelTask(engine,
  104.                 updateTasks.toArray(new Task[0]));
  105.         final Task resetTask = new ParallelTask(engine,
  106.                 resetTasks.toArray(new Task[0]));
  107.  
  108.         getWorld().submit(new ConsecutiveTask(tickTask, updateTask, resetTask));
  109.     }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement