Skip to content

Instantly share code, notes, and snippets.

@daltonks
Created November 4, 2011 00:11
Show Gist options
  • Save daltonks/1338320 to your computer and use it in GitHub Desktop.
Save daltonks/1338320 to your computer and use it in GitHub Desktop.
package me.dalton.mineconquer.capturing;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import me.dalton.mineconquer.ChunkData;
import me.dalton.mineconquer.GamePlayer;
import me.dalton.mineconquer.Globals;
import me.dalton.mineconquer.Group;
import me.dalton.mineconquer.Main;
import me.dalton.mineconquer.Util;
import org.bukkit.Location;
import org.bukkit.entity.Player;
public class CaptureHandler {
static List<CaptureSpot> captureSpots = new ArrayList<CaptureSpot>();
public static void start(){
Main.instance.getServer().getScheduler().scheduleSyncRepeatingTask(Main.instance, new Runnable(){public void run(){
for(Iterator<CaptureSpot> itr = captureSpots.iterator(); itr.hasNext();){
CaptureSpot cSection = itr.next();
int playersCapturing = 0;
for(GamePlayer playa : cSection.attackingPlayers){
try{
Player player = playa.getPlayer();
if(cSection.chunk.isAdjacentTo(new ChunkData(player.getLocation().getBlock().getChunk()))){
playersCapturing++;
if(cSection.outOfBoundsPlayers.contains(playa)){
cSection.outOfBoundsPlayers.remove(playa);
player.sendMessage(Util.blue + "You are capturing again!");
}
} else {
if(!cSection.outOfBoundsPlayers.contains(playa)){
player.sendMessage(Util.blue + "You have moved out of the capture zone!");
cSection.outOfBoundsPlayers.add(playa);
}
}
} catch (Exception e){}
}
cSection.strength -= playersCapturing / (cSection.group.getOnlinePlayers().size() + 1) * Globals.CAPTURE_UPDATE;
if(cSection.strength <= 0){
GamePlayer firstAttacker = cSection.attackingPlayers.get(0);
Group gainedGroup = firstAttacker.getGroup();
cSection.playerOwner.removeChunk(cSection.chunk);
firstAttacker.newPurchased(cSection.chunk);
try{
cSection.group.sendMessage(Util.red + "Your chunk has been taken over at " + Util.gray + (cSection.chunk.X * 16 + 8) + ", "+ (cSection.chunk.Z * 16 + 8) + Util.red + "!");
} catch (NullPointerException e){
cSection.playerOwner.sendMessage(Util.red + "Your chunk has been taken over at " + Util.gray + (cSection.chunk.X * 16 + 8) + ", "+ (cSection.chunk.Z * 16 + 8) + Util.red + "!");
}
try{
gainedGroup.sendMessage(Util.blue + firstAttacker.name + " has captured a chunk at " + Util.gray + (cSection.chunk.X * 16 + 8) + ", "+ (cSection.chunk.Z * 16 + 8) + Util.blue + "!");
} catch (NullPointerException e){
cSection.playerOwner.sendMessage(Util.blue + firstAttacker.name + " has captured a chunk at " + Util.gray + (cSection.chunk.X * 16 + 8) + ", "+ (cSection.chunk.Z * 16 + 8) + Util.blue + "!");
}
itr.remove();
}
}
}}, 20 * (long) Globals.CAPTURE_UPDATE, 20 * (long) Globals.CAPTURE_UPDATE);
}
public static void newCapturer(GamePlayer gP){
Location location = gP.getPlayer().getLocation();
int px = location.getBlockX();
int pz = location.getBlockZ();
if(!gP.isInAGroup()){
gP.getPlayer().sendMessage(Util.red + "You are not in a group to capture for!");
return;
}
ChunkData playerChunk = new ChunkData(gP.getPlayer().getLocation().getBlock().getChunk());
//get adjacent chunks and find the closest
double distance = 9001;
ChunkData closestChunk = null;
for(ChunkData enemyChunk : gP.getEnemyChunks())
if(enemyChunk.isAdjacentTo(playerChunk) && enemyChunk.tag.equals("u")){
double tempDistance = Util.getDistance(enemyChunk.X * 16 + 8, enemyChunk.Z * 16 + 8, px, pz);
if(tempDistance < distance){
distance = tempDistance;
closestChunk = enemyChunk;
}
}
if(closestChunk == null){
gP.getPlayer().sendMessage(Util.red + "There is no chunk close enough to capture.");
return;
}
for(CaptureSpot spot : captureSpots){
if(spot.chunk.equals(closestChunk)){
spot.addAttacker(gP);
gP.getPlayer().sendMessage(Util.blue + "You are now capturing!");
return;
} else if(spot.attackingPlayers.contains(gP))
spot.removeAttacker(gP);
}
CaptureSpot place = new CaptureSpot(gP, closestChunk);
captureSpots.add(place);
try{
gP.getGroup().sendMessage(Util.blue + "Your team is now capturing at " + Util.gray + (place.chunk.X * 16 + 8) + ", " + (place.chunk.Z * 16 + 8) + Util.blue + "!");
} catch (NullPointerException e){gP.getPlayer().sendMessage(Util.blue + "You are capturing at " + Util.gray + (place.chunk.X * 16 + 8) + ", " + (place.chunk.Z * 16 + 8) + Util.blue + "!");}
try{
place.group.sendMessage(Util.red + "A point is being captured at "
+ Util.gray + (place.chunk.X * 16 + 8) + ", " + (place.chunk.Z * 16 + 8) + Util.red + "!");
} catch (NullPointerException e){place.playerOwner.sendMessage(Util.blue + "You are being captured at " + Util.gray + (place.chunk.X * 16 + 8) + ", " + (place.chunk.Z * 16 + 8) + Util.blue + "!");}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment