use of org.bukkit.GameMode in project Glowstone by GlowstoneMC.
the class GameModeCommand method execute.
@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages messages) {
if (!testPermission(sender, messages.getPermissionMessage())) {
return true;
}
if (args.length == 0 || args.length == 1 && !(sender instanceof Player)) {
sendUsageMessage(sender, messages);
return false;
}
final ResourceBundle bundle = messages.getResourceBundle();
String gm = args[0];
GameMode gamemode = GameModeUtils.build(gm, messages.getLocale());
if (gamemode == null) {
new LocalizedStringImpl("gamemode.unknown", bundle).sendInColor(ChatColor.RED, sender, gm);
return false;
}
if (args.length == 1) {
// self
Player player = (Player) sender;
updateGameMode(sender, player, gamemode, bundle);
return true;
}
String name = args[1];
if (name.startsWith("@") && name.length() >= 2 && CommandUtils.isPhysical(sender)) {
Location location = CommandUtils.getLocation(sender);
CommandTarget target = new CommandTarget(sender, name);
Entity[] matched = target.getMatched(location);
for (Entity entity : matched) {
if (entity instanceof Player) {
Player player = (Player) entity;
updateGameMode(sender, player, gamemode, bundle);
}
}
} else {
Player player = Bukkit.getPlayerExact(name);
if (player == null) {
messages.getGeneric(GenericMessage.OFFLINE).sendInColor(ChatColor.RED, sender, name);
} else {
updateGameMode(sender, player, gamemode, bundle);
}
}
return true;
}
use of org.bukkit.GameMode in project Essentials by drtshock.
the class Commandgamemode method run.
@Override
protected void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception {
GameMode gameMode;
if (args.length == 0) {
throw new NotEnoughArgumentsException();
} else if (args.length == 1) {
gameMode = matchGameMode(commandLabel);
gamemodeOtherPlayers(server, sender, gameMode, args[0]);
} else if (args.length == 2) {
gameMode = matchGameMode(args[0].toLowerCase(Locale.ENGLISH));
gamemodeOtherPlayers(server, sender, gameMode, args[1]);
}
}
use of org.bukkit.GameMode in project MyMaid2 by jaoafa.
the class Cmd_G method onCommand.
@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if (args.length >= 1 && args[0].equalsIgnoreCase("help")) {
SendUsageMessage(sender, cmd);
return true;
}
if (args.length == 0) {
if (!(sender instanceof Player)) {
SendMessage(sender, cmd, "このコマンドはゲーム内から実行してください。");
return true;
}
Player player = (Player) sender;
GameMode beforeGameMode = player.getGameMode();
if (player.getGameMode() == GameMode.SPECTATOR) {
// スペクテイターならクリエイティブにする
player.setGameMode(GameMode.CREATIVE);
if (player.getGameMode() != GameMode.CREATIVE) {
SendMessage(sender, cmd, "ゲームモードの変更ができませんでした。");
return true;
}
SendMessage(sender, cmd, beforeGameMode.name() + " -> " + GameMode.CREATIVE.name());
return true;
} else if (player.getGameMode() == GameMode.CREATIVE) {
// クリエイティブならスペクテイターにする
player.setGameMode(GameMode.SPECTATOR);
if (player.getGameMode() != GameMode.SPECTATOR) {
SendMessage(sender, cmd, "ゲームモードの変更ができませんでした。");
return true;
}
SendMessage(sender, cmd, beforeGameMode.name() + " -> " + GameMode.SPECTATOR.name());
return true;
} else {
// それ以外(サバイバル・アドベンチャー)ならクリエイティブにする
player.setGameMode(GameMode.CREATIVE);
if (player.getGameMode() != GameMode.CREATIVE) {
SendMessage(sender, cmd, "ゲームモードの変更ができませんでした。");
return true;
}
SendMessage(sender, cmd, beforeGameMode.name() + " -> " + GameMode.CREATIVE.name());
return true;
}
} else if (args.length == 1) {
if (!(sender instanceof Player)) {
SendMessage(sender, cmd, "このコマンドはゲーム内から実行してください。");
return true;
}
Player player = (Player) sender;
GameMode beforeGameMode = player.getGameMode();
int i;
try {
i = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
SendMessage(sender, cmd, "引数には数値を指定してください。");
return true;
}
GameMode gm = GameMode.getByValue(i);
if (gm == null) {
SendMessage(sender, cmd, "指定された引数からゲームモードが取得できませんでした。");
return true;
}
player.setGameMode(gm);
if (player.getGameMode() != gm) {
SendMessage(sender, cmd, "ゲームモードの変更ができませんでした。");
return true;
}
SendMessage(sender, cmd, beforeGameMode.name() + " -> " + gm.name());
return true;
} else if (args.length == 2) {
int i;
try {
i = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
SendMessage(sender, cmd, "引数には数値を指定してください。");
return true;
}
GameMode gm = GameMode.getByValue(i);
if (gm == null) {
SendMessage(sender, cmd, "指定された引数からゲームモードが取得できませんでした。");
return true;
}
String playername = args[1];
Player player = Bukkit.getPlayerExact(playername);
if (player == null) {
SendMessage(sender, cmd, "指定されたプレイヤー「" + playername + "」は見つかりませんでした。");
Player any_chance_player = Bukkit.getPlayer(playername);
if (any_chance_player != null) {
SendMessage(sender, cmd, "もしかして: " + any_chance_player.getName());
}
return true;
}
GameMode beforeGameMode = player.getGameMode();
player.setGameMode(gm);
if (player.getGameMode() != gm) {
SendMessage(sender, cmd, "ゲームモードの変更ができませんでした。");
return true;
}
SendMessage(sender, cmd, player.getName() + ": " + beforeGameMode.name() + " -> " + gm.name());
return true;
}
SendUsageMessage(sender, cmd);
return true;
}
use of org.bukkit.GameMode in project NoCheatPlus by NoCheatPlus.
the class CreativeFly method check.
/**
* @param player
* @param from
* @param to
* @param data
* @param cc
* @param time Milliseconds.
* @return
*/
public Location check(final Player player, final PlayerLocation from, final PlayerLocation to, final MovingData data, final MovingConfig cc, final IPlayerData pData, final long time, final int tick, final boolean useBlockChangeTracker) {
// Reset tags, just in case.
tags.clear();
final boolean debug = pData.isDebugActive(type);
// Some edge data for this move.
final GameMode gameMode = player.getGameMode();
final PlayerMoveData thisMove = data.playerMoves.getCurrentMove();
// if (!data.thisMove.from.extraPropertiesValid) {
// // TODO: Confine by model config flag or just always do [if the latter: do it in the listener]?
// data.thisMove.setExtraProperties(from, to);
// }
final PlayerMoveData lastMove = data.playerMoves.getFirstPastMove();
final ModelFlying model = thisMove.modelFlying;
// Proactive reset of elytraBoost (MC 1.11.2).
if (data.fireworksBoostDuration > 0) {
if (!lastMove.valid || lastMove.flyCheck != CheckType.MOVING_CREATIVEFLY || lastMove.modelFlying != model || data.fireworksBoostTickExpire < tick) {
data.fireworksBoostDuration = 0;
} else {
data.fireworksBoostDuration--;
}
}
// Calculate some distances.
final double yDistance = thisMove.yDistance;
final double hDistance = thisMove.hDistance;
final boolean flying = gameMode == BridgeMisc.GAME_MODE_SPECTATOR || player.isFlying();
final boolean sprinting = time <= data.timeSprinting + cc.sprintingGrace;
// Lost ground, if set so.
if (model.getGround()) {
MovingUtil.prepareFullCheck(from, to, thisMove, Math.max(cc.yOnGround, cc.noFallyOnGround));
if (!thisMove.from.onGroundOrResetCond) {
if (from.isSamePos(to)) {
if (// Copy and paste from sf.
lastMove.toIsValid && lastMove.hDistance > 0.0 && lastMove.yDistance < -0.3 && LostGround.lostGroundStill(player, from, to, hDistance, yDistance, sprinting, lastMove, data, cc, tags)) {
// Nothing to do.
}
} else if (LostGround.lostGround(player, from, to, hDistance, yDistance, sprinting, lastMove, data, cc, useBlockChangeTracker ? blockChangeTracker : null, tags)) {
// Nothing to do.
}
}
}
// Horizontal distance check.
double[] resH = hDist(player, from, to, hDistance, yDistance, sprinting, flying, lastMove, time, model, data, cc);
double limitH = resH[0];
double resultH = resH[1];
// Check velocity.
if (resultH > 0) {
double hFreedom = data.getHorizontalFreedom();
if (hFreedom < resultH) {
// Use queued velocity if possible.
hFreedom += data.useHorizontalVelocity(resultH - hFreedom);
}
if (hFreedom > 0.0) {
resultH = Math.max(0.0, resultH - hFreedom);
if (resultH <= 0.0) {
limitH = hDistance;
}
tags.add("hvel");
}
} else {
// TODO: test/check !
data.clearActiveHorVel();
}
// Normalize to % of a block.
resultH *= 100.0;
if (resultH > 0.0) {
tags.add("hdist");
}
// Vertical move.
// Limit.
double limitV = 0.0;
// Violation (normalized to 100 * 1 block, applies if > 0.0).
double resultV = 0.0;
// Distinguish checking method by y-direction of the move.
if (yDistance > 0.0) {
// Ascend.
double[] res = vDistAscend(from, to, yDistance, flying, thisMove, lastMove, model, data, cc);
resultV = Math.max(resultV, res[1]);
limitV = res[0];
} else if (yDistance < 0.0) {
// Descend.
double[] res = vDistDescend(from, to, yDistance, flying, lastMove, model, data, cc);
resultV = Math.max(resultV, res[1]);
limitV = res[0];
} else {
// Keep altitude.
double[] res = vDistZero(from, to, yDistance, flying, lastMove, model, data, cc);
resultV = Math.max(resultV, res[1]);
limitV = res[0];
}
// Velocity.
if (resultV > 0.0 && data.getOrUseVerticalVelocity(yDistance) != null) {
resultV = 0.0;
tags.add("vvel");
}
// Add tag for maximum height check (silent set back).
final double maximumHeight = model.getMaxHeight() + player.getWorld().getMaxHeight();
if (to.getY() > maximumHeight) {
// TODO: Allow use velocity there (would need a flag to signal the actual check below)?
tags.add("maxheight");
}
// Normalize to % of a block.
resultV *= 100.0;
if (resultV > 0.0) {
tags.add("vdist");
}
final double result = Math.max(0.0, resultH) + Math.max(0.0, resultV);
if (debug) {
outpuDebugMove(player, hDistance, limitH, yDistance, limitV, model, tags, data);
}
// Violation handling.
// Might get altered below.
Location setBack = null;
if (result > 0.0) {
// Increment violation level.
data.creativeFlyVL += result;
// Execute whatever actions are associated with this check and the violation level and find out if we
// should cancel the event.
final ViolationData vd = new ViolationData(this, player, data.creativeFlyVL, result, cc.creativeFlyActions);
if (vd.needsParameters()) {
vd.setParameter(ParameterName.LOCATION_FROM, String.format(Locale.US, "%.2f, %.2f, %.2f", from.getX(), from.getY(), from.getZ()));
vd.setParameter(ParameterName.LOCATION_TO, String.format(Locale.US, "%.2f, %.2f, %.2f", to.getX(), to.getY(), to.getZ()));
vd.setParameter(ParameterName.DISTANCE, String.format(Locale.US, "%.2f", TrigUtil.distance(from, to)));
if (!tags.isEmpty()) {
vd.setParameter(ParameterName.TAGS, StringUtil.join(tags, "+"));
}
}
if (executeActions(vd).willCancel()) {
// Compose a new location based on coordinates of "newTo" and viewing direction of "event.getTo()"
// to allow the player to look somewhere else despite getting pulled back by NoCheatPlus.
// (OK)
setBack = data.getSetBack(to);
}
} else {
// Maximum height check (silent set back).
if (to.getY() > maximumHeight) {
// (OK)
setBack = data.getSetBack(to);
if (debug) {
debug(player, "Maximum height exceeded, silent set-back.");
}
}
if (setBack == null) {
// Slowly reduce the violation level with each event.
data.creativeFlyVL *= 0.97;
}
}
// Return setBack, if set.
if (setBack != null) {
// Check for max height of the set back.
if (setBack.getY() > maximumHeight) {
// Correct the y position.
setBack.setY(getCorrectedHeight(maximumHeight, setBack.getWorld()));
if (debug) {
debug(player, "Maximum height exceeded by set back, correct to: " + setBack.getY());
}
}
data.sfJumpPhase = 0;
return setBack;
} else {
// Adjust the set back and other last distances.
data.setSetBack(to);
// Adjust jump phase.
if (!thisMove.from.onGroundOrResetCond && !thisMove.to.onGroundOrResetCond) {
data.sfJumpPhase++;
} else if (thisMove.touchedGround && !thisMove.to.onGroundOrResetCond) {
data.sfJumpPhase = 1;
} else {
data.sfJumpPhase = 0;
}
return null;
}
}
use of org.bukkit.GameMode in project NoCheatPlus by NoCheatPlus.
the class BlockBreakListener method onBlockBreak.
/**
* We listen to BlockBreak events for obvious reasons.
*
* @param event
* the event
*/
@EventHandler(ignoreCancelled = false, priority = EventPriority.LOWEST)
public void onBlockBreak(final BlockBreakEvent event) {
final long now = System.currentTimeMillis();
final Player player = event.getPlayer();
final IPlayerData pData = DataManager.getPlayerData(player);
// TODO: Legacy / encapsulate fully there.
if (Items.checkIllegalEnchantmentsAllHands(player, pData)) {
event.setCancelled(true);
counters.addPrimaryThread(idCancelDIllegalItem, 1);
} else if (MovingUtil.hasScheduledPlayerSetBack(player)) {
event.setCancelled(true);
}
// Cancelled events only leads to resetting insta break.
if (event.isCancelled()) {
isInstaBreak = AlmostBoolean.NO;
return;
}
// TODO: maybe invalidate instaBreak on some occasions.
final Block block = event.getBlock();
boolean cancelled = false;
// Do the actual checks, if still needed. It's a good idea to make computationally cheap checks first, because
// it may save us from doing the computationally expensive checks.
final BlockBreakConfig cc = pData.getGenericInstance(BlockBreakConfig.class);
final BlockBreakData data = pData.getGenericInstance(BlockBreakData.class);
final BlockInteractData bdata = pData.getGenericInstance(BlockInteractData.class);
/*
* Re-check if this is a block interacted with before. With instantly
* broken blocks, this may be off by one orthogonally.
*/
final int tick = TickTask.getTick();
final boolean isInteractBlock = !bdata.getLastIsCancelled() && bdata.matchesLastBlock(tick, block);
int skippedRedundantChecks = 0;
final GameMode gameMode = player.getGameMode();
// Has the player broken a block that was not damaged before?
final boolean wrongBlockEnabled = wrongBlock.isEnabled(player, pData);
if (wrongBlockEnabled && wrongBlock.check(player, block, cc, data, pData, isInstaBreak)) {
cancelled = true;
}
// Has the player broken more blocks per second than allowed?
if (!cancelled && frequency.isEnabled(player, pData) && frequency.check(player, tick, cc, data, pData)) {
cancelled = true;
}
// Has the player broken blocks faster than possible?
if (!cancelled && gameMode != GameMode.CREATIVE && fastBreak.isEnabled(player, pData) && fastBreak.check(player, block, isInstaBreak, cc, data, pData)) {
cancelled = true;
}
// Did the arm of the player move before breaking this block?
if (!cancelled && noSwing.isEnabled(player, pData) && noSwing.check(player, data, pData)) {
cancelled = true;
}
final FlyingQueueHandle flyingHandle;
final boolean reachEnabled = reach.isEnabled(player, pData);
final boolean directionEnabled = direction.isEnabled(player, pData);
if (reachEnabled || directionEnabled) {
flyingHandle = new FlyingQueueHandle(pData);
final Location loc = player.getLocation(useLoc);
final double eyeHeight = MovingUtil.getEyeHeight(player);
// Is the block really in reach distance?
if (!cancelled) {
if (isInteractBlock && bdata.isPassedCheck(CheckType.BLOCKINTERACT_REACH)) {
skippedRedundantChecks++;
} else if (reachEnabled && reach.check(player, eyeHeight, block, data, cc)) {
cancelled = true;
}
}
// TODO: Skip if checks were run on this block (all sorts of hashes/conditions).
if (!cancelled) {
if (isInteractBlock && (bdata.isPassedCheck(CheckType.BLOCKINTERACT_DIRECTION) || bdata.isPassedCheck(CheckType.BLOCKINTERACT_VISIBLE))) {
skippedRedundantChecks++;
} else if (directionEnabled && direction.check(player, loc, eyeHeight, block, flyingHandle, data, cc, pData)) {
cancelled = true;
}
}
useLoc.setWorld(null);
} else {
flyingHandle = null;
}
// Destroying liquid blocks.
if (!cancelled && BlockProperties.isLiquid(block.getType()) && !pData.hasPermission(Permissions.BLOCKBREAK_BREAK_LIQUID, player) && !NCPExemptionManager.isExempted(player, CheckType.BLOCKBREAK_BREAK)) {
cancelled = true;
}
// On cancel...
if (cancelled) {
event.setCancelled(cancelled);
// Reset damage position:
// TODO: Review this (!), check if set at all !?
data.clickedX = block.getX();
data.clickedY = block.getY();
data.clickedZ = block.getZ();
} else {
// Debug log (only if not cancelled, to avoid spam).
if (pData.isDebugActive(CheckType.BLOCKBREAK)) {
debugBlockBreakResult(player, block, skippedRedundantChecks, flyingHandle, pData);
}
}
if (isInstaBreak.decideOptimistically()) {
data.wasInstaBreak = now;
} else {
data.wasInstaBreak = 0;
}
// Adjust data.
data.fastBreakBreakTime = now;
// data.fastBreakfirstDamage = now;
isInstaBreak = AlmostBoolean.NO;
}
Aggregations