Thread: Player save

Results 1 to 2 of 2
  1. #1 Player save 
    Registered Member
    Join Date
    Mar 2012
    Posts
    94
    Thanks given
    4
    Thanks received
    13
    Rep Power
    21
    this is my player saving
    Code:
    public class PlayerBackupTask extends Task {
    
    	
    	//every 2 min
    	public PlayerBackupTask() {
    		super(250, true, StackType.NEVER_STACK, BreakType.NEVER, TaskIdentifier.CHARACTER_BACKUP);
    	}
    
    	@Override
    	public void execute() {
    		Thread t = new Thread(new Runnable() {
    			@Override
    			public void run() {
    				backup();
    				System.out.println("Saved all players on seperate thread.");
    			}
    		});
    		t.start();
    		try {
    			t.join();
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    
    	@Override
    	public void onStop() {
    	}
    
    	public static void backup() {
    	
    		for (Player players : World.getPlayers()) {
    			if (players != null && players.isActive() && World.getActivePlayers() >= 1) {
    				PlayerSave.save(players);
    			}
    		}
    
    	}
    
    //	public static void copyFile(File sourceFile, File destFile) throws IOException {
    //		Files.copy(Paths.get(sourceFile.getPath()), Paths.get(destFile.getPath()), StandardCopyOption.COPY_ATTRIBUTES);
    //	}
    }
    it's like every 2 mins and is called on startup like so;
    Code:
    						TaskQueue.queue(new PlayerBackupTask());

    however, i could do this:

    Code:
    public class PlayerBackupTask extends Task {
    	
    	private final Player player;
    
    	
    	//every 2 min
    	public PlayerBackupTask(Player player) {
    		super(250, true, StackType.NEVER_STACK, BreakType.NEVER, TaskIdentifier.CHARACTER_BACKUP);
    		this.player = player;
    	}
    
    	@Override
    	public void execute() {
    		Thread t = new Thread(new Runnable() {
    			@Override
    			public void run() {
    				PlayerSave.save(player);
    				
    				System.out.println("Saved all players on seperate thread.");
    			}
    		});
    		t.start();
    		try {
    			t.join();
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    
    	@Override
    	public void onStop() {
    	}
    
    
    
    //	public static void copyFile(File sourceFile, File destFile) throws IOException {
    //		Files.copy(Paths.get(sourceFile.getPath()), Paths.get(destFile.getPath()), StandardCopyOption.COPY_ATTRIBUTES);
    //	}
    }
    and then when a player logs in call this
    Code:
    TaskQueue.queue(new PlayerBackupTask());

    instead of being called on start up and saving for every player all the time.

    which would better for performance purposes
    Reply With Quote  
     

  2. #2  
    Java Programmer
    _Jon's Avatar
    Join Date
    Jan 2015
    Age
    30
    Posts
    206
    Thanks given
    36
    Thanks received
    63
    Rep Power
    47
    Example 1 would work best otherwise you are creating a new task for every player that logs in.

    [SPOIL]
    Quote Originally Posted by 'Varbit View Post
    this is my player saving
    Code:
    public class PlayerBackupTask extends Task {
    
        
        //every 2 min
        public PlayerBackupTask() {
            super(250, true, StackType.NEVER_STACK, BreakType.NEVER, TaskIdentifier.CHARACTER_BACKUP);
        }
    
        @Override
        public void execute() {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    backup();
                    System.out.println("Saved all players on seperate thread.");
                }
            });
            t.start();
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void onStop() {
        }
    
        public static void backup() {
        
            for (Player players : World.getPlayers()) {
                if (players != null && players.isActive() && World.getActivePlayers() >= 1) {
                    PlayerSave.save(players);
                }
            }
    
        }
    
    //    public static void copyFile(File sourceFile, File destFile) throws IOException {
    //        Files.copy(Paths.get(sourceFile.getPath()), Paths.get(destFile.getPath()), StandardCopyOption.COPY_ATTRIBUTES);
    //    }
    }
    it's like every 2 mins and is called on startup like so;
    Code:
                            TaskQueue.queue(new PlayerBackupTask());

    however, i could do this:

    Code:
    public class PlayerBackupTask extends Task {
        
        private final Player player;
    
        
        //every 2 min
        public PlayerBackupTask(Player player) {
            super(250, true, StackType.NEVER_STACK, BreakType.NEVER, TaskIdentifier.CHARACTER_BACKUP);
            this.player = player;
        }
    
        @Override
        public void execute() {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    PlayerSave.save(player);
                    
                    System.out.println("Saved all players on seperate thread.");
                }
            });
            t.start();
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void onStop() {
        }
    
    
    
    //    public static void copyFile(File sourceFile, File destFile) throws IOException {
    //        Files.copy(Paths.get(sourceFile.getPath()), Paths.get(destFile.getPath()), StandardCopyOption.COPY_ATTRIBUTES);
    //    }
    }
    and then when a player logs in call this
    Code:
    TaskQueue.queue(new PlayerBackupTask());

    instead of being called on start up and saving for every player all the time.

    which would better for performance purposes
    [/SPOIL]

    Also no need for null checks
    Code:
    Arrays.stream(World.getPlayers()).forEach(player -> {
        Optional<Player> node = Optional.ofNullable(player);
        node.ifPresent(p -> PlayerSave.save(p));
    }
    Not to mention you would be opening a new Thread per player which isn't necessary, you will also want to be careful of using data across threads.
    Github - Here
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. [474] Project Vortex (Player Saving)
    By Zahhak in forum Downloads
    Replies: 20
    Last Post: 03-09-2009, 04:17 AM
  2. [525]Player Saving. & Hitpoints bar
    By Extea in forum Requests
    Replies: 0
    Last Post: 03-08-2009, 03:19 AM
  3. Project Vortex (with player saving)
    By Zahhak in forum Projects
    Replies: 18
    Last Post: 12-30-2008, 12:35 PM
  4. Replies: 6
    Last Post: 08-22-2008, 12:11 AM
  5. Replies: 37
    Last Post: 06-12-2008, 08:14 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •