Skip to content
This repository has been archived by the owner on Mar 30, 2018. It is now read-only.

Commit

Permalink
Implemented different types of arena resetters.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalus committed Jan 21, 2012
1 parent e018be2 commit 4f9d3f1
Show file tree
Hide file tree
Showing 7 changed files with 358 additions and 57 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -9,7 +9,7 @@
<repositories>
<repository>
<id>bukkit-repo</id>
<url>http://repo.bukkit.org/artifactory/repo</url>
<url>http://repo.bukkit.org/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
Expand Down
99 changes: 44 additions & 55 deletions src/main/java/de/beimax/simplespleef/game/GameStandard.java
Expand Up @@ -18,11 +18,6 @@
**/
package de.beimax.simplespleef.game;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.*;

import net.milkbowl.vault.economy.EconomyResponse;
Expand All @@ -41,6 +36,10 @@
import org.bukkit.inventory.*;

import de.beimax.simplespleef.SimpleSpleef;
import de.beimax.simplespleef.game.arenarestoring.ArenaRestorer;
import de.beimax.simplespleef.game.arenarestoring.HardArenaRestorer;
import de.beimax.simplespleef.game.arenarestoring.SoftRestorer;
import de.beimax.simplespleef.game.floortracking.FloorThread;
import de.beimax.simplespleef.game.floortracking.FloorTracker;
import de.beimax.simplespleef.util.*;

Expand Down Expand Up @@ -119,6 +118,11 @@ public class GameStandard extends Game {
*/
protected FloorTracker floorTracker;

/**
* arena restorer for this game
*/
protected ArenaRestorer arenaRestorer;

/**
* shortcuts for digging settings
*/
Expand Down Expand Up @@ -499,8 +503,8 @@ public boolean delete(CommandSender sender) {
// send message
sendMessage(SimpleSpleef.getPlugin().ll("feedback.delete", "[ARENA]", getName(), "[PLAYER]", sender.getName()),
SimpleSpleef.getPlugin().getConfig().getBoolean("settings.announceStop", true));
// call the game handler to tell it that the game is over
SimpleSpleef.getGameHandler().gameOver(this);
// call the game handler to tell it that the game is over - only if game status has been reset
if (status == STATUS_NEW) SimpleSpleef.getGameHandler().gameOver(this);
return true;
}

Expand Down Expand Up @@ -544,11 +548,10 @@ protected boolean endGame() {
teleportPlayer(spleefer.getPlayer(), "lounge");
}
// restore arena
restoreArena();
}

// change game status
status = STATUS_NEW;
restoreArena(); // this will also eventually reset the game status and call the game handler
} else
// change game status
status = STATUS_NEW;
return true;
}

Expand Down Expand Up @@ -1021,8 +1024,8 @@ protected void gameOver() {

// clean up game and end it
endGame();
// call the game handler to tell it that the game is over
SimpleSpleef.getGameHandler().gameOver(this);
// call the game handler to tell it that the game is over - only if game status has been reset
if (status == STATUS_NEW) SimpleSpleef.getGameHandler().gameOver(this);
}

/**
Expand Down Expand Up @@ -1390,55 +1393,41 @@ private boolean checkMayBreakBlockLocation(Block block) {
* save arena information, if setting restoreArenaAfterGame has been set
*/
protected void saveArena() {
if (arena == null || !configuration.getBoolean("restoreArenaAfterGame", true)) return; // ignore, if arena not defined or setting false
SerializableBlockData[][][] blockData = arena.getSerializedBlocks();

// output file
File file = new File(SimpleSpleef.getPlugin().getDataFolder(), "arena_" + getId() + ".save");
// delete old file
if (file.exists() && !file.delete()) {
SimpleSpleef.log.warning("[SimpleSpleef] Could not delete file " + file.getName());
return;
// explicitly set to false
if (configuration.isBoolean("restoreArenaAfterGame") && !configuration.getBoolean("restoreArenaAfterGame")) {
arenaRestorer = null;
return; // ignore, if arena not defined or setting false
}
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());

// determine type of arena
String type;
if (configuration.isBoolean("restoreArenaAfterGame")) type = "soft";
else if (configuration.isString("restoreArenaAfterGame")) type = configuration.getString("restoreArenaAfterGame");
else type = "soft"; // fall back

if (type.equals("arenahard")) { // hard arena restorer
arenaRestorer = new HardArenaRestorer();
arenaRestorer.saveArena(this, arena);
} else if (type.equals("floorhard")) { // hard floor restorer
arenaRestorer = new HardArenaRestorer();
arenaRestorer.saveArena(this, floor);
} else { // soft restorer
// create soft restorer
arenaRestorer = new SoftRestorer();
arenaRestorer.saveArena(this, floor);

if (floorTracker == null) // create floor tracker, if needed
floorTracker = new FloorTracker();
// create a new tracker/restorer
floorTracker.addFloorThread((FloorThread) arenaRestorer);
}
}

/**
* restore arena information, if setting restoreArenaAfterGame has been set
*/
protected void restoreArena() {
if (arena == null || !configuration.getBoolean("restoreArenaAfterGame", true)) return; // ignore, if arena not defined or setting false

// input file
File file = new File(SimpleSpleef.getPlugin().getDataFolder(), "arena_" + 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
arena.setSerializedBlocks(blockData);
// delete file at the end - cleanup work...
if (!file.delete()) SimpleSpleef.log.warning("[SimpleSpleef] Could not delete file " + file.getName());
if (arenaRestorer != null) arenaRestorer.restoreArena();
}

@Override
Expand Down
@@ -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();
}
@@ -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);
}
}
}

0 comments on commit 4f9d3f1

Please sign in to comment.