GenuineSounds

Basic Entity ticking concept

May 21st, 2014
372
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. package com.genuinevm.coolrom;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.stream.IntStream;
  6.  
  7. public class Test {
  8.  
  9.     public static void main(String[] args) {
  10.         // The thread safe list of entities that can and should be run in parallel.
  11.         List<Entity> entityList = new ArrayList<>();
  12.         // Populate lists with entities.
  13.         IntStream.range(0, 256).forEach(Entity::new);
  14.         // Run the ticks like movement, path finding, deciding what to do next.
  15.         entityList.stream().parallel().forEach(Entity::normalTicks);
  16.         // These would be placing blocks, executing things that affect other mobs.
  17.         entityList.stream().sequential().forEach(Entity::notThreadSafeTicks);
  18.     }
  19. }
  20.  
  21. class Entity {
  22.  
  23.     private final int id;
  24.  
  25.     public Entity(int id) {
  26.         this.id = id;
  27.     }
  28.  
  29.     public void normalTicks() {
  30.         try {
  31.             Thread.sleep(10);
  32.         }
  33.         catch (InterruptedException e) {}
  34.     }
  35.  
  36.     public void notThreadSafeTicks() {
  37.         try {
  38.             Thread.sleep(10);
  39.         }
  40.         catch (InterruptedException e) {}
  41.     }
  42. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment