This repository has been archived by the owner on Mar 30, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented different types of arena resetters.
- Loading branch information
Showing
7 changed files
with
358 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/de/beimax/simplespleef/game/arenarestoring/ArenaRestorer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* | ||
*/ | ||
package de.beimax.simplespleef.game.arenarestoring; | ||
|
||
import de.beimax.simplespleef.game.Game; | ||
import de.beimax.simplespleef.util.Cuboid; | ||
|
||
/** | ||
* @author mkalus | ||
* Interface for classes that restore the arena eventually - these classes also have to call the | ||
* handler's game over method to delete the game when finished. | ||
*/ | ||
public interface ArenaRestorer { | ||
/** | ||
* Store the arena for one game | ||
* @param game | ||
* @param cuboid | ||
*/ | ||
public void saveArena(Game game, Cuboid cuboid); | ||
|
||
/** | ||
* Restore the arena for one game - please call handler's game over method to delete the game when finished restoring. | ||
* @param game | ||
*/ | ||
public void restoreArena(); | ||
} |
106 changes: 106 additions & 0 deletions
106
src/main/java/de/beimax/simplespleef/game/arenarestoring/HardArenaRestorer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* | ||
*/ | ||
package de.beimax.simplespleef.game.arenarestoring; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
|
||
import de.beimax.simplespleef.SimpleSpleef; | ||
import de.beimax.simplespleef.game.Game; | ||
import de.beimax.simplespleef.util.Cuboid; | ||
import de.beimax.simplespleef.util.SerializableBlockData; | ||
|
||
/** | ||
* @author mkalus | ||
* | ||
*/ | ||
public class HardArenaRestorer implements ArenaRestorer { | ||
/** | ||
* game to restore | ||
*/ | ||
private Game game; | ||
|
||
/** | ||
* cuboid to store/restore | ||
*/ | ||
private Cuboid cuboid; | ||
|
||
/* (non-Javadoc) | ||
* @see de.beimax.simplespleef.game.arenarestoring.ArenaRestorer#saveArena(de.beimax.simplespleef.game.Game) | ||
*/ | ||
@Override | ||
public void saveArena(Game game, Cuboid cuboid) { | ||
if (game == null || cuboid == null) return; //ignore invalid stuff | ||
this.game = game; | ||
this.cuboid = cuboid; | ||
|
||
SerializableBlockData[][][] blockData = this.cuboid.getSerializedBlocks(); | ||
|
||
// output file | ||
File file = new File(SimpleSpleef.getPlugin().getDataFolder(), "arena_" + this.game.getId() + ".save"); | ||
// delete old file | ||
if (file.exists() && !file.delete()) { | ||
SimpleSpleef.log.warning("[SimpleSpleef] Could not delete file " + file.getName()); | ||
return; | ||
} | ||
|
||
try { | ||
// serialize objects | ||
FileOutputStream fileStream = new FileOutputStream(file); | ||
ObjectOutputStream os = new ObjectOutputStream(fileStream); | ||
// write array itself | ||
os.writeObject(blockData); | ||
os.close(); | ||
} catch (Exception e) { | ||
SimpleSpleef.log.warning("[SimpleSpleef] Could not save arena file " + file.getName() + ". Reason: " + e.getMessage()); | ||
} | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see de.beimax.simplespleef.game.arenarestoring.ArenaRestorer#restoreArena() | ||
*/ | ||
@Override | ||
public void restoreArena() { | ||
// start restoration in new thread | ||
(new RestoreThread()).start(); | ||
} | ||
|
||
/** | ||
* restorer thread | ||
* @author mkalus | ||
* | ||
*/ | ||
private class RestoreThread extends Thread { | ||
@Override | ||
public void run() { | ||
// input file | ||
File file = new File(SimpleSpleef.getPlugin().getDataFolder(), "arena_" + game.getId() + ".save"); | ||
if (!file.exists()) { | ||
SimpleSpleef.log.warning("[SimpleSpleef] Could find arena file " + file.getName()); | ||
return; | ||
} | ||
SerializableBlockData[][][] blockData; | ||
try { | ||
// deserialize objects | ||
FileInputStream fileInputStream = new FileInputStream(file); | ||
ObjectInputStream oInputStream = new ObjectInputStream(fileInputStream); | ||
blockData = (SerializableBlockData[][][]) oInputStream.readObject(); | ||
oInputStream.close(); | ||
} catch (Exception e) { | ||
SimpleSpleef.log.warning("[SimpleSpleef] Could not restore arena file " + file.getName() + ". Reason: " + e.getMessage()); | ||
return; | ||
} | ||
// restore arena | ||
cuboid.setSerializedBlocks(blockData); | ||
// delete file at the end - cleanup work... | ||
if (!file.delete()) SimpleSpleef.log.warning("[SimpleSpleef] Could not delete file " + file.getName()); | ||
|
||
// call game handler to finish the game off | ||
SimpleSpleef.getGameHandler().gameOver(game); | ||
} | ||
} | ||
} |
Oops, something went wrong.