-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a way to freeze the entire game or a player.
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
1 parent
73bf2ae
commit 0aa0988
Showing
6 changed files
with
320 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.