Skip to content

Commit

Permalink
Added a way to freeze the entire game or a player.
Browse files Browse the repository at this point in the history
Frozen players:
 - cannot move out of their current block;
 - cannot place or break blocks;
 - cannot be damaged;
 - cannot shoot arrows.

Closes #5, closes #6, related to #4.
  • Loading branch information
AmauryCarrade committed Aug 11, 2014
1 parent 73bf2ae commit 0aa0988
Show file tree
Hide file tree
Showing 6 changed files with 320 additions and 7 deletions.
127 changes: 127 additions & 0 deletions src/main/java/me/azenet/UHPlugin/UHFreezer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package me.azenet.UHPlugin;

import java.util.ArrayList;

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Creature;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

public class UHFreezer {

private UHPlugin p = null;

private Boolean globalFreeze = false;
private ArrayList<String> frozenPlayers = new ArrayList<String>();

public UHFreezer(UHPlugin plugin) {
this.p = plugin;
}


/**
* Freezes a player, if needed.
* The player is blocked inside the block he is currently.
*
* @param player The player to freeze
* @param from The old position from the PlayerMoveEvent
* @param to The new position from the PlayerMoveEvent
*/
public void freezePlayerIfNeeded(Player player, Location from, Location to) {
if(frozenPlayers.contains(player.getName())) {
// If the X or Z coordinate of the player change, he needs to be teleported inside the old block.
// The yaw and pitch are conserved, to teleport more smoothly.
if(from.getBlockX() != to.getBlockX() || from.getBlockZ() != to.getBlockZ()) {
player.teleport(new Location(from.getWorld(), from.getBlockX() + 0.5, from.getBlockY(), from.getBlockZ() + 0.5, to.getYaw(), to.getPitch()), TeleportCause.PLUGIN);
}
}
}



/**
* Enables or disables the global freeze of players, mobs, timer.
*
* @param freezed If true the global freeze will be enabled.
*/
public void setGlobalFreezeState(Boolean frozen) {
this.globalFreeze = frozen;

if(frozen) {
for(String playerName : p.getGameManager().getAlivePlayers()) {
if(!this.frozenPlayers.contains(playerName)) {
this.frozenPlayers.add(playerName);
}
}

// Freeze mobs by applying a Slowness effect. There isn't any EntityMoveEvent, so...
for(World world : p.getServer().getWorlds()) {
for(Entity entity : world.getEntities()) {
if(entity instanceof Creature) {
((Creature) entity).addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 10000000, 100, true));
}
}
}
}

else {
this.frozenPlayers = new ArrayList<String>();

// Removes the slowness effect
for(World world : p.getServer().getWorlds()) {
for(Entity entity : world.getEntities()) {
if(entity instanceof Creature) {
((Creature) entity).removePotionEffect(PotionEffectType.SLOW);
}
}
}
}
}

/**
* Gets the current state of the global freeze.
*
* @return True if the global freeze is enabled.
*/
public boolean getGlobalFreezeState() {
return this.globalFreeze;
}

/**
* Freezes a player.
*
* @param player The player to freeze.
* @param freezed If true the player will be frozen. If false, unfrozen.
*/
public void setPlayerFreezeState(Player player, Boolean frozen) {
if(frozen && !this.frozenPlayers.contains(player.getName())) {
this.frozenPlayers.add(player.getName());
}
else {
this.frozenPlayers.remove(player.getName());
}
}

/**
* Returns true if the given player is frozen.
*
* @param player The player to be checked.
* @return
*/
public boolean isPlayerFrozen(Player player) {
return frozenPlayers.contains(player.getName());
}

/**
* Returns the list of currently frozen players.
*
* @return The list.
*/
public ArrayList<String> getFrozenPlayers() {
return this.frozenPlayers;
}
}
11 changes: 11 additions & 0 deletions src/main/java/me/azenet/UHPlugin/UHPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public final class UHPlugin extends JavaPlugin {
private UHBorderManager borderManager = null;
private UHRecipeManager recipeManager = null;

private UHFreezer freezer = null;

private UHWorldBorderIntegration wbintegration = null;
private UHSpectatorPlusIntegration spintegration = null;
private UHDynmapIntegration dynmapintegration = null;
Expand All @@ -45,6 +47,8 @@ public void onEnable() {
borderManager = new UHBorderManager(this);
recipeManager = new UHRecipeManager(this);

freezer = new UHFreezer(this);

wbintegration = new UHWorldBorderIntegration(this);
spintegration = new UHSpectatorPlusIntegration(this);
dynmapintegration = new UHDynmapIntegration(this);
Expand Down Expand Up @@ -111,6 +115,7 @@ else if(teamRawSeparated.length == 1) { // "color"
logger.info(i18n.t("load.loaded"));
}


public UHTeamManager getTeamManager() {
return teamManager;
}
Expand All @@ -131,6 +136,11 @@ public UHRecipeManager getRecipeManager() {
return recipeManager;
}

public UHFreezer getFreezer() {
return freezer;
}


public UHWorldBorderIntegration getWorldBorderIntegration() {
return wbintegration;
}
Expand All @@ -143,6 +153,7 @@ public UHDynmapIntegration getDynmapIntegration() {
return dynmapintegration;
}


public I18n getI18n() {
return i18n;
}
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/me/azenet/UHPlugin/UHPluginCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class UHPluginCommand implements CommandExecutor {
private ArrayList<String> teamCommands = new ArrayList<String>();
private ArrayList<String> specCommands = new ArrayList<String>();
private ArrayList<String> borderCommands = new ArrayList<String>();
private ArrayList<String> freezeCommands = new ArrayList<String>();

private I18n i = null;

Expand All @@ -40,6 +41,7 @@ public UHPluginCommand(UHPlugin p) {
commands.add("border");
commands.add("heal");
commands.add("healall");
commands.add("freeze");
commands.add("resurrect");
commands.add("tpback");
commands.add("spec");
Expand All @@ -59,6 +61,11 @@ public UHPluginCommand(UHPlugin p) {
borderCommands.add("set");
borderCommands.add("warning");
borderCommands.add("check");

freezeCommands.add("all");
freezeCommands.add("none");
freezeCommands.add("on");
freezeCommands.add("off");
}

@SuppressWarnings("rawtypes")
Expand Down Expand Up @@ -139,6 +146,7 @@ private void help(CommandSender sender, boolean error) {
sender.sendMessage(i.t("cmd.helpAddspawn"));
sender.sendMessage(i.t("cmd.helpAddspawnXZ"));
sender.sendMessage(i.t("cmd.helpSpec"));
sender.sendMessage(i.t("cmd.helpFreeze"));
sender.sendMessage(i.t("cmd.helpWall"));
sender.sendMessage(i.t("cmd.helpBorder"));

Expand Down Expand Up @@ -880,6 +888,85 @@ private void doTeamMessage(CommandSender sender, Command command, String label,
}


/**
* This command freezes the players.
* Usage: /uh freeze on [player] | off [player] | all | none
* - on [player]: freezes the given player, or the sender if no player was provided.
* - off [player]: unfreezes the given player (or the sender, same condition).
* - all: freezes all the alive players, the mobs and the timer.
* - none: unfreezes all the alive players (even if there where frozen before using
* /uh freeze all), the mobs and the timer.
*
* @param sender
* @param command
* @param label
* @param args
*/
@SuppressWarnings("unused")
private void doFreeze(CommandSender sender, Command command, String label, String[] args) {
if(args.length == 1) { // /uh freeze
sender.sendMessage(i.t("cmd.freezeHelpOn"));
sender.sendMessage(i.t("cmd.freezeHelpOff"));
sender.sendMessage(i.t("cmd.freezeHelpAll"));
sender.sendMessage(i.t("cmd.freezeHelpNone"));
}
else {
String subcommand = args[1];

if(subcommand.equalsIgnoreCase("on") || subcommand.equalsIgnoreCase("off")) {

boolean on = subcommand.equalsIgnoreCase("on") ? true : false;

if(args.length == 2) { // /uh freeze on: freezes the sender
if(sender instanceof Player) {
p.getFreezer().setPlayerFreezeState((Player) sender, on);
if(on) {
sender.sendMessage(i.t("freeze.frozen", ((Player) sender).getName()));
}
else {
sender.sendMessage(i.t("freeze.unfrozen", ((Player) sender).getName()));
}
}
else {
sender.sendMessage(i.t("freeze.playerOnly"));
}
}
else if(args.length == 3) { // /uh freeze on <player>: freezes <player>.
Player player = p.getServer().getPlayer(args[2]);
if(player == null) {
sender.sendMessage(i.t("freeze.offline", args[2]));
}
else {
p.getFreezer().setPlayerFreezeState(player, on);
if(on) {
player.sendMessage(i.t("freeze.frozen", ((Player) sender).getName()));
sender.sendMessage(i.t("freeze.playerFrozen", player.getName()));
}
else {
player.sendMessage(i.t("freeze.unfrozen", ((Player) sender).getName()));
sender.sendMessage(i.t("freeze.playerUnfrozen", player.getName()));
}
}
}
}

else if(subcommand.equalsIgnoreCase("all") || subcommand.equalsIgnoreCase("none")) {

boolean on = subcommand.equalsIgnoreCase("all") ? true : false;

p.getFreezer().setGlobalFreezeState(on);

if(on) {
p.getServer().broadcastMessage(i.t("freeze.broadcast.globalFreeze"));
}
else {
p.getServer().broadcastMessage(i.t("freeze.broadcast.globalUnfreeze"));
}

}
}
}


public ArrayList<String> getCommands() {
return commands;
Expand All @@ -896,4 +983,8 @@ public ArrayList<String> getSpecCommands() {
public ArrayList<String> getBorderCommands() {
return borderCommands;
}

public ArrayList<String> getFreezeCommands() {
return freezeCommands;
}
}
Loading

0 comments on commit 0aa0988

Please sign in to comment.