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

Commit

Permalink
Added option to restore arenas after the match - somewhat experimental
Browse files Browse the repository at this point in the history
still, but should work.
  • Loading branch information
mkalus committed Sep 27, 2011
1 parent fe68be8 commit 34a5bf0
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 11 deletions.
14 changes: 14 additions & 0 deletions src/main/java/de/beimax/simplespleef/SimpleSpleef.java
Expand Up @@ -33,6 +33,7 @@ public class SimpleSpleef extends JavaPlugin {
private SimpleSpleefGame game;
private SimpleSpleefPlayerListener playerListener;
private SimpleSpleefEntityListener entityListener;
private SimpleSpleefBlockListener blockListener;
public Configuration conf; // General configuration
public Configuration ll; // Language configuration

Expand Down Expand Up @@ -169,6 +170,10 @@ public void init() {
conf.setProperty("also_loose_in_lava", true);
changed = true;
}
if (conf.getProperty("restore_blocks_after_game") == null) { // define if game should keep track of changed blocks and restore them after the game
conf.setProperty("restore_blocks_after_game", false);
changed = true;
}

// config has been changed: save it
if (changed) {
Expand Down Expand Up @@ -324,7 +329,15 @@ public void onEnable() {
Priority.Normal, this);
pm.registerEvent(Event.Type.ENTITY_DEATH, entityListener, // deaths are handled by entity
Priority.Normal, this);

// Register block breaks, if accounting is set on
if (conf.getBoolean("restore_blocks_after_game", false)) {
pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener,
Priority.Normal, this);
System.out.println("[SimpleSpleef] restore_blocks_after_game is enabled - listening to block breaks when game is on.");
}


PluginDescriptionFile pdfFile = this.getDescription();
System.out.println("[SimpleSpleef] " + pdfFile.getName() + " version "
+ pdfFile.getVersion() + " is enabled!");
Expand Down Expand Up @@ -371,6 +384,7 @@ protected boolean createGame() {
game = new SimpleSpleefGame(this, randomWins);
playerListener = new SimpleSpleefPlayerListener(this, game);
entityListener = new SimpleSpleefEntityListener(this, game);
blockListener = new SimpleSpleefBlockListener(this, game);
return true;
}

Expand Down
@@ -0,0 +1,31 @@
package de.beimax.simplespleef;

import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockListener;

/**
* Listener for block breaks
*
* @author mkalus
*
*/
public class SimpleSpleefBlockListener extends BlockListener {
//private final SimpleSpleef plugin;
private final SimpleSpleefGame game;

/**
* Constructor
* @param instance
* @param game
*/
public SimpleSpleefBlockListener(SimpleSpleef instance,
SimpleSpleefGame game) {
//plugin = instance;
this.game = game;
}

@Override
public void onBlockBreak(BlockBreakEvent event) {
game.checkBlockBreak(event);
}
}
93 changes: 83 additions & 10 deletions src/main/java/de/beimax/simplespleef/SimpleSpleefGame.java
Expand Up @@ -5,11 +5,15 @@

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Random;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
Expand All @@ -21,37 +25,56 @@
*
*/
public class SimpleSpleefGame {
// random number generator
/**
* random number generator
*/
private static Random generator = new Random();

// random gifts
/**
* random gifts
*/
private static Material[] randomWins;

// TODO: Does it make sense to make randomWins static?

// return next random element from Material Array
/**
* return next random element from Material Array
*/
public static Material get(Material[] array) {
int rnd = generator.nextInt(array.length);
return array[rnd];
}

/**
* reference to plugin
*/
private final SimpleSpleef plugin;

// list of spleefers currently spleefing
/**
* list of spleefers currently spleefing
*/
private HashSet<Player> spleefers;

// team list, only taken if needed
/**
* team list, only taken if needed
*/
private HashMap<Integer, HashSet<Player>> teams;

// list of players lost
/**
* list of players lost
*/
private HashSet<Player> lost;

// game has started?
/**
* game has started?
*/
private boolean started;

// private countdown class
/**
* private countdown class
*/
private Countdown countdown;

/**
* accumulated players at start of game - to count fees at the end
*/
Expand All @@ -72,6 +95,16 @@ public static Material get(Material[] array) {
* <code>plugin.conf.getBoolean("also_loose_in_lava", true);</code>
*/
private boolean alsoLooseInLava;

/**
* remember blocks broken - defined in the constructor by calling
* <code>plugin.conf.getBoolean("restore_blocks_after_game", true);</code>
*/
private boolean restoreBlocksAfterGame;
/**
* remembers blocks broken
*/
private HashMap<Block, Material> brokenBlocks;

/**
* Constructor
Expand All @@ -90,7 +123,11 @@ public SimpleSpleefGame(SimpleSpleef instance, Material[] randomWins) {
SimpleSpleefGame.randomWins = randomWins;
// to make this work faster
alsoLooseInLava = plugin.conf.getBoolean("also_loose_in_lava", true);
}
restoreBlocksAfterGame = plugin.conf.getBoolean("restore_blocks_after_game", false);
// remember blocks broken, if config says so - create hashset to remember
if (restoreBlocksAfterGame) brokenBlocks = new HashMap<Block, Material>();
else brokenBlocks = null;
}

/**
* return team name
Expand Down Expand Up @@ -431,6 +468,9 @@ protected void deleteGame(Player player) {

// remove shovels if needed
playersLooseShovels();

// restore blocks if needed
restoreBlocks();

// message and stop game
started = false;
Expand Down Expand Up @@ -742,6 +782,9 @@ private void declareVictory(int team) {
// remove shovels if needed
playersLooseShovels();

// restore blocks if needed
restoreBlocks();

if (spleefers.size() == lost.size())
plugin.getServer()
.broadcastMessage(
Expand Down Expand Up @@ -918,6 +961,36 @@ public void setItemPrize(Player player, int itemId) {
}
prizeItemId = itemId;
}

/**
* Check block breaks to restore them after the game
* @param event
*/
public void checkBlockBreak(BlockBreakEvent event) {
// no checks, if no game has started or no block breaking should be remembered
if (!started || !restoreBlocksAfterGame)
return;
// check, if player is in spleef list - if not, return
Player player = event.getPlayer();
if (!spleefers.contains(player))
return;
// already on looser list?
if (lost != null && lost.contains(player))
return;

// remember block
brokenBlocks.put(event.getBlock(), event.getBlock().getType());
}

/**
* Restore blocks after the game
*/
protected void restoreBlocks() {
if (!restoreBlocksAfterGame) return;
for (Entry<Block, Material> entry : brokenBlocks.entrySet()) {
entry.getKey().setType(entry.getValue());
}
}

/**
* Countdown class
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
@@ -1,6 +1,6 @@
name: SimpleSpleef
main: de.beimax.simplespleef.SimpleSpleef
version: 1.2
version: 1.3
author: maxkalus
description: >
Simple Spleef Plugin
Expand Down

0 comments on commit 34a5bf0

Please sign in to comment.