Add Search and Destroy code
This commit is contained in:
parent
aa0659828a
commit
60fd6b01cc
@ -0,0 +1,213 @@
|
||||
package nautilus.game.arcade.game.games.searchanddestroy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Effect;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.TNTPrimed;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
|
||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import mineplex.core.common.util.C;
|
||||
import mineplex.core.updater.UpdateType;
|
||||
import mineplex.core.updater.event.UpdateEvent;
|
||||
import nautilus.game.arcade.ArcadeManager;
|
||||
import nautilus.game.arcade.GameType;
|
||||
import nautilus.game.arcade.events.GameStateChangeEvent;
|
||||
import nautilus.game.arcade.game.GameTeam;
|
||||
import nautilus.game.arcade.game.TeamGame;
|
||||
import nautilus.game.arcade.game.games.searchanddestroy.kits.*;
|
||||
import nautilus.game.arcade.kit.Kit;
|
||||
|
||||
public class SearchAndDestroy extends TeamGame
|
||||
{
|
||||
|
||||
private ArrayList<TeamBomb> _teamBombs = new ArrayList<TeamBomb>();
|
||||
|
||||
public ArrayList<TeamBomb> getBombs()
|
||||
{
|
||||
return _teamBombs;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onBombTick(UpdateEvent event)
|
||||
{
|
||||
if (event.getType() == UpdateType.SEC && this.GetState() == GameState.Live)
|
||||
{
|
||||
Iterator<TeamBomb> itel = _teamBombs.iterator();
|
||||
while (itel.hasNext())
|
||||
{
|
||||
TeamBomb bomb = itel.next();
|
||||
bomb.checkArmers();
|
||||
bomb.tickBomb();
|
||||
GameTeam bombOwner = bomb.getTeam();
|
||||
if (bomb.isArmed())
|
||||
{
|
||||
int timeLeft = bomb.getTimeUntilExplode();
|
||||
if (((timeLeft <= 30 && timeLeft % 10 == 0) || (timeLeft <= 5)) && timeLeft >= 1)
|
||||
{
|
||||
Bukkit.broadcastMessage(C.cGold + "" + timeLeft + " seconds left until " + bombOwner.GetColor()
|
||||
+ bombOwner.GetName() + "'s" + C.cGold + " bomb goes off!");
|
||||
for (Player player : bombOwner.GetPlayers(true))
|
||||
{
|
||||
player.playSound(player.getLocation(), Sound.BLAZE_DEATH, 30, 3);
|
||||
}
|
||||
}
|
||||
else if (timeLeft == 0)
|
||||
{
|
||||
// It exploded
|
||||
itel.remove();
|
||||
bomb.getBlockLocation().getWorld().playSound(bomb.getBlockLocation(), Sound.EXPLODE, 1000, 0);
|
||||
bomb.getBlockLocation().getWorld().playEffect(bomb.getBomb().getLocation(), Effect.EXPLOSION_HUGE, 0);
|
||||
bomb.removeBomb();
|
||||
for (Player player : this.GetPlayers(true))
|
||||
{
|
||||
GameTeam pTeam = GetTeam(player);
|
||||
if (bombOwner == pTeam)
|
||||
{
|
||||
Manager.GetDamage().NewDamageEvent(player, null, null, DamageCause.CUSTOM, 999, false, true,
|
||||
true, "Bomb", "Bomb Exploded");
|
||||
}
|
||||
}
|
||||
// TODO The code below may need changing
|
||||
if (_teamBombs.size() == 1)
|
||||
{
|
||||
GameTeam winning = _teamBombs.get(0).getTeam();
|
||||
Bukkit.broadcastMessage(bombOwner.GetColor() + bombOwner.GetName() + "'s" + ChatColor.RESET
|
||||
+ ChatColor.GOLD + " bomb exploded! " + winning.GetColor() + winning.GetName()
|
||||
+ ChatColor.RESET + ChatColor.GOLD + " wins!");
|
||||
}
|
||||
else
|
||||
{
|
||||
Bukkit.broadcastMessage(bombOwner.GetColor() + bombOwner.GetName() + " was defeated!");
|
||||
}
|
||||
}
|
||||
if (timeLeft > 0)
|
||||
{
|
||||
// TODO Refresh bomb
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onInteractBombBlock(PlayerInteractEvent event)
|
||||
{
|
||||
Player p = event.getPlayer();
|
||||
if (event.getAction() == Action.RIGHT_CLICK_BLOCK)
|
||||
{
|
||||
if (event.getItem() != null && event.getItem().getType() == Material.BLAZE_POWDER)
|
||||
{
|
||||
for (TeamBomb bomb : this._teamBombs)
|
||||
{
|
||||
if (!bomb.isArmed() && bomb.getBlockLocation().getBlock().equals(event.getClickedBlock()))
|
||||
{
|
||||
GameTeam team = GetTeam(p);
|
||||
if (team == bomb.getTeam())
|
||||
{
|
||||
p.sendMessage(ChatColor.RED + "That's your bomb!");
|
||||
}
|
||||
else
|
||||
{
|
||||
bomb.onInteractWithFuse(p);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onInteractBombEntity(PlayerInteractEntityEvent event)
|
||||
{
|
||||
Player p = event.getPlayer();
|
||||
if (event.getRightClicked() instanceof TNTPrimed)
|
||||
{
|
||||
ItemStack item = p.getItemInHand();
|
||||
if (item != null && item.getType() == Material.BLAZE_POWDER)
|
||||
{
|
||||
for (TeamBomb bomb : this._teamBombs)
|
||||
{
|
||||
if (bomb.isArmed() && bomb.getBomb().equals(event.getRightClicked()))
|
||||
{
|
||||
GameTeam team = GetTeam(p);
|
||||
if (team != bomb.getTeam())
|
||||
{
|
||||
p.sendMessage(ChatColor.RED + "That's not your bomb!");
|
||||
}
|
||||
else if (team != null)
|
||||
{
|
||||
bomb.onInteractWithFuse(p);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onGameEnd(GameStateChangeEvent event)
|
||||
{
|
||||
if (event.GetState() == GameState.End && !_teamBombs.isEmpty())
|
||||
{
|
||||
for (TeamBomb bomb : _teamBombs)
|
||||
{
|
||||
if (bomb.getTeam().IsTeamAlive())
|
||||
{
|
||||
bomb.restoreBomb();
|
||||
}
|
||||
else
|
||||
{
|
||||
bomb.removeBomb();
|
||||
}
|
||||
}
|
||||
_teamBombs.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public SearchAndDestroy(ArcadeManager manager)
|
||||
{
|
||||
super(manager, GameType.SearchAndDestroy, new Kit[]
|
||||
{
|
||||
new KitBow(manager)
|
||||
}, new String[]
|
||||
{
|
||||
"A test game of", "Search and Destroy"
|
||||
});
|
||||
|
||||
InventoryClick = true;
|
||||
WorldTimeSet = -1;
|
||||
WorldBoundaryKill = false;
|
||||
HungerSet = 20;
|
||||
RepairWeapons = false;
|
||||
AnnounceJoinQuit = false;
|
||||
DisableKillCommand = false;
|
||||
AllowParticles = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ParseData()
|
||||
{
|
||||
for (GameTeam team : GetTeamList())
|
||||
{
|
||||
for (Location loc : WorldData.GetDataLocs(team.GetColor() == ChatColor.AQUA ? "BLUE" : team.GetColor().name()))
|
||||
{
|
||||
_teamBombs.add(new TeamBomb(this.Manager, this, team, loc));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,298 @@
|
||||
package nautilus.game.arcade.game.games.searchanddestroy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Random;
|
||||
|
||||
import mineplex.core.common.util.C;
|
||||
import nautilus.game.arcade.ArcadeManager;
|
||||
import nautilus.game.arcade.game.GameTeam;
|
||||
import nautilus.game.arcade.game.TeamGame;
|
||||
import net.minecraft.server.v1_7_R4.EntityTNTPrimed;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Effect;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.TNTPrimed;
|
||||
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
|
||||
|
||||
class TeamBomb implements Comparable<TeamBomb>
|
||||
{
|
||||
private class ArmInfo
|
||||
{
|
||||
private ArrayList<Player> _armers = new ArrayList<Player>();
|
||||
private long _firstArmed = System.currentTimeMillis();
|
||||
private long _lastInteract;
|
||||
private int _timeToArmBomb = 11;
|
||||
private GameTeam _owningTeam;
|
||||
|
||||
public ArmInfo(GameTeam team)
|
||||
{
|
||||
this._owningTeam = team;
|
||||
}
|
||||
|
||||
public void addArmer(Player gamer)
|
||||
{
|
||||
if (!_armers.contains(gamer))
|
||||
{
|
||||
_timeToArmBomb -= 1;
|
||||
_armers.add(gamer);
|
||||
/* ItemStack item = gamer.getPlayer().getItemInHand();
|
||||
if (item != null && item.getType() != Material.AIR)
|
||||
{
|
||||
int timeToTake = 0;
|
||||
if (item.containsEnchantment(Enchants.BOMB_SPEED)
|
||||
&& item.getEnchantmentLevel(Enchants.BOMB_SPEED) > timeToTake)
|
||||
timeToTake = item.getEnchantmentLevel(Enchants.BOMB_SPEED);
|
||||
if (item.containsEnchantment(Enchants.BOMB_ARMING)
|
||||
&& item.getEnchantmentLevel(Enchants.BOMB_ARMING) > timeToTake && _bombEntity == null)
|
||||
timeToTake = item.getEnchantmentLevel(Enchants.BOMB_ARMING);
|
||||
if (item.containsEnchantment(Enchants.BOMB_DEFUSING)
|
||||
&& item.getEnchantmentLevel(Enchants.BOMB_DEFUSING) > timeToTake && _bombEntity != null)
|
||||
timeToTake = item.getEnchantmentLevel(Enchants.BOMB_DEFUSING);
|
||||
_timeToArmBomb -= timeToTake;
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
public float getPercentageOfBombDone()
|
||||
{
|
||||
float done = (float) (System.currentTimeMillis() - _firstArmed) / (1000 * _timeToArmBomb);
|
||||
return Math.min(done, 1);
|
||||
}
|
||||
|
||||
public boolean isFused()
|
||||
{
|
||||
return isValid() && _firstArmed + (1000 * _timeToArmBomb) < System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public boolean isValid()
|
||||
{
|
||||
return _owningTeam.IsTeamAlive() && _lastInteract + 1000 > System.currentTimeMillis();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Location _bombLocation;
|
||||
private TNTPrimed _bombEntity;
|
||||
private int _timeUntilExplode;
|
||||
private long _timeBombArmed;
|
||||
private long _lastHiss;
|
||||
private GameTeam _owningTeam;
|
||||
private ArrayList<ArmInfo> _armers = new ArrayList<ArmInfo>();
|
||||
private SearchAndDestroy _game;
|
||||
|
||||
public int getTimeUntilExplode()
|
||||
{
|
||||
return _timeUntilExplode;
|
||||
}
|
||||
|
||||
public boolean isArmed()
|
||||
{
|
||||
return _bombEntity != null;
|
||||
}
|
||||
|
||||
public TNTPrimed getBomb()
|
||||
{
|
||||
return _bombEntity;
|
||||
}
|
||||
|
||||
public GameTeam getTeam()
|
||||
{
|
||||
return _owningTeam;
|
||||
}
|
||||
|
||||
public Location getBlockLocation()
|
||||
{
|
||||
return _bombLocation;
|
||||
}
|
||||
|
||||
public TeamBomb(ArcadeManager manager, SearchAndDestroy game, GameTeam owningTeam, Location bombLocation)
|
||||
{
|
||||
this._game = game;
|
||||
this._owningTeam = owningTeam;
|
||||
this._bombLocation = bombLocation;
|
||||
}
|
||||
|
||||
public void checkArmers()
|
||||
{
|
||||
Iterator<ArmInfo> armers = _armers.iterator();
|
||||
while (armers.hasNext())
|
||||
{
|
||||
ArmInfo info = armers.next();
|
||||
if (!info.isValid())
|
||||
{
|
||||
armers.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onInteractWithFuse(Player player)
|
||||
{
|
||||
GameTeam hisTeam = _game.GetTeam(player);
|
||||
if (hisTeam != null)
|
||||
{
|
||||
if (_owningTeam == hisTeam)
|
||||
{
|
||||
if (_bombEntity == null)
|
||||
{
|
||||
player.sendMessage(C.cRed + "You cannot arm your bomb");
|
||||
}
|
||||
else
|
||||
{
|
||||
onInteract(hisTeam, player);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_bombEntity != null)
|
||||
{
|
||||
player.sendMessage(C.cRed + "You cannot disarm their bomb");
|
||||
}
|
||||
else
|
||||
{
|
||||
onInteract(hisTeam, player);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void onInteract(GameTeam gTeam, Player p)
|
||||
{
|
||||
if (_lastHiss + 500 <= System.currentTimeMillis())
|
||||
{
|
||||
p.getWorld().playSound(p.getLocation(), Sound.FIZZ, 1, 0);
|
||||
_lastHiss = System.currentTimeMillis();
|
||||
}
|
||||
p.getWorld()
|
||||
.spigot()
|
||||
.playEffect(
|
||||
_bombEntity == null ? _bombLocation.clone().add(0.5, 1.1, 0.5) : _bombEntity.getLocation().add(0, 1, 0),
|
||||
Effect.LARGE_SMOKE, 0, 0, 0.1F, 0.05F, 0.1F, 0, 3, 30);
|
||||
ArmInfo info = null;
|
||||
for (ArmInfo arm : _armers)
|
||||
{
|
||||
if (arm._owningTeam == gTeam)
|
||||
{
|
||||
info = arm;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (info != null && !info.isValid())
|
||||
{
|
||||
_armers.remove(info);
|
||||
info = null;
|
||||
}
|
||||
if (info == null)
|
||||
{
|
||||
info = new ArmInfo(gTeam);
|
||||
_armers.add(info);
|
||||
}
|
||||
info.addArmer(p);
|
||||
info._lastInteract = System.currentTimeMillis();
|
||||
if (info.isFused())
|
||||
{
|
||||
activateBomb(gTeam);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void activateBomb(GameTeam gameTeam)
|
||||
{
|
||||
if (!isArmed())
|
||||
{
|
||||
_timeBombArmed = System.currentTimeMillis();
|
||||
getBlockLocation().getBlock().setType(Material.AIR);
|
||||
_game.CreatureAllowOverride = true;
|
||||
EntityTNTPrimed entity = new EntityTNTPrimed(((CraftWorld) getBlockLocation().getWorld()).getHandle());
|
||||
_game.CreatureAllowOverride = false;
|
||||
double x = getBlockLocation().getX() + 0.5;
|
||||
double y = getBlockLocation().getY() + 1;
|
||||
double z = getBlockLocation().getZ() + 0.5;
|
||||
entity.setPosition(x, y, z);
|
||||
entity.motY = 0.20000000298023224D;
|
||||
entity.fuseTicks = 120000;
|
||||
entity.lastX = x;
|
||||
entity.lastY = y;
|
||||
entity.lastZ = z;
|
||||
((CraftWorld) getBlockLocation().getWorld()).getHandle().addEntity(entity, SpawnReason.CUSTOM);
|
||||
_bombEntity = (TNTPrimed) entity.getBukkitEntity();
|
||||
_timeUntilExplode = 60;
|
||||
Bukkit.broadcastMessage(ChatColor.GOLD + "" + ChatColor.MAGIC + "ab " + gameTeam.GetColor() + gameTeam.GetName()
|
||||
+ ChatColor.RESET + ChatColor.GOLD + " just armed " + getTeam().GetColor() + getTeam().GetName() + "'s"
|
||||
+ ChatColor.RESET + ChatColor.GOLD + " bomb! " + ChatColor.MAGIC + "ab");
|
||||
for (Player player : Bukkit.getOnlinePlayers())
|
||||
{
|
||||
GameTeam hisTeam = _game.GetTeam(player);
|
||||
if (hisTeam == getTeam())
|
||||
player.playSound(player.getLocation(), Sound.BLAZE_DEATH, 10000, 0);
|
||||
else
|
||||
player.playSound(player.getLocation(), Sound.AMBIENCE_THUNDER, 1, 0.8F + new Random().nextFloat() * 0.2F);
|
||||
}
|
||||
Collections.sort(_game.getBombs());
|
||||
}
|
||||
else
|
||||
{
|
||||
Bukkit.broadcastMessage(_owningTeam.GetColor() + _owningTeam.GetName() + ChatColor.RESET + ChatColor.GOLD
|
||||
+ " have just defused their bomb!");
|
||||
restoreBomb();
|
||||
}
|
||||
_armers.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(TeamBomb o)
|
||||
{
|
||||
if (o._timeBombArmed == _timeBombArmed)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (o._timeBombArmed < _timeBombArmed)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the bomb block to tnt, updates the hologram and removes the bomb entity
|
||||
*/
|
||||
public void restoreBomb()
|
||||
{
|
||||
_armers.clear();
|
||||
getBlockLocation().getBlock().setType(Material.TNT);
|
||||
if (getBomb() != null)
|
||||
{
|
||||
this.getBomb().remove();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the bomb block to air and removes the hologram and bomb entity
|
||||
*/
|
||||
public void removeBomb()
|
||||
{
|
||||
_armers.clear();
|
||||
getBlockLocation().getBlock().setType(Material.AIR);
|
||||
if (getBomb() != null)
|
||||
{
|
||||
this.getBomb().remove();
|
||||
}
|
||||
}
|
||||
|
||||
public void tickBomb()
|
||||
{
|
||||
if (_bombEntity != null)
|
||||
{
|
||||
_timeUntilExplode--;
|
||||
_bombEntity.getWorld().playSound(_bombEntity.getLocation(), Sound.CREEPER_DEATH, 1.85F, 1.2F);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user