use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.
the class GlowItemFrame method generateTeleportMessage.
void generateTeleportMessage(BlockFace face) {
int xoffset = 0;
int zoffset = 0;
int yaw = 0;
switch(getFacingNumber(face)) {
case 1:
xoffset = -32;
yaw = 64;
break;
case 2:
zoffset = -32;
yaw = -128;
break;
case 3:
xoffset = 32;
yaw = -64;
break;
case 0:
zoffset = 32;
yaw = 0;
break;
}
Location itemframelocation = location;
Key key = new Key(itemframelocation.getBlockX() >> 4, itemframelocation.getBlockZ() >> 4);
for (GlowPlayer player : getWorld().getRawPlayers()) {
if (player.canSeeChunk(key)) {
double x = location.getX();
double y = location.getY();
double z = location.getZ();
player.getSession().send(new EntityTeleportMessage(id, x + xoffset, y, z + zoffset, yaw, 0));
}
}
}
use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.
the class TitleCommand method execute.
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!testPermission(sender))
return true;
if (args.length < 2) {
sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
return false;
}
Player player = Bukkit.getPlayerExact(args[0]);
if (player == null || sender instanceof Player && !((Player) sender).canSee(player)) {
sender.sendMessage("There's no player by that name online.");
return false;
}
String action = args[1];
if (action.equalsIgnoreCase("clear")) {
((GlowPlayer) player).clearTitle();
sender.sendMessage("Cleared " + player.getName() + "'s title");
} else if (action.equalsIgnoreCase("reset")) {
player.resetTitle();
sender.sendMessage("Reset " + player.getName() + "'s title");
} else if (action.equalsIgnoreCase("title")) {
if (args.length < 3) {
sender.sendMessage(ChatColor.RED + "Usage: /title <player> " + action + " <raw json>");
return false;
}
StringBuilder message = new StringBuilder();
for (int i = 2; i < args.length; i++) {
message.append(args[i]);
}
String raw = message.toString().trim();
if (!validJson(raw)) {
sender.sendMessage(ChatColor.RED + "Invalid JSON: Could not parse, invalid format?");
return false;
}
String component = raw;
Map<String, Object> parsed = getJson(raw);
if (parsed != null) {
component = convertJson(parsed);
}
((GlowPlayer) player).updateTitle(TitleMessage.Action.TITLE, component);
((GlowPlayer) player).sendTitle();
sender.sendMessage("Updated " + player.getName() + "'s title");
} else if (action.equalsIgnoreCase("subtitle")) {
if (args.length < 3) {
sender.sendMessage(ChatColor.RED + "Usage: /title <player> " + action + " <raw json>");
return false;
}
StringBuilder message = new StringBuilder();
for (int i = 2; i < args.length; i++) {
message.append(args[i]);
}
String raw = message.toString().trim();
if (!validJson(raw)) {
sender.sendMessage(ChatColor.RED + "Invalid JSON: Could not parse, invalid format?");
return false;
}
String component = raw;
Object parsed = JSONValue.parse(raw);
if (parsed instanceof JSONObject) {
component = convertJson((JSONObject) parsed);
}
((GlowPlayer) player).updateTitle(TitleMessage.Action.SUBTITLE, component);
sender.sendMessage("Updated " + player.getName() + "'s subtitle");
} else if (action.equalsIgnoreCase("times")) {
if (args.length != 5) {
sender.sendMessage(ChatColor.RED + "Usage: /title <player> " + action + " <fade in> <stay time> <fade out>");
return false;
}
if (!tryParseInt(args[2])) {
sender.sendMessage(ChatColor.RED + "'" + args[2] + "' is not a number");
return false;
}
if (!tryParseInt(args[3])) {
sender.sendMessage(ChatColor.RED + "'" + args[3] + "' is not a number");
return false;
}
if (!tryParseInt(args[4])) {
sender.sendMessage(ChatColor.RED + "'" + args[4] + "' is not a number");
return false;
}
((GlowPlayer) player).updateTitle(TitleMessage.Action.TIMES, toInt(args[2]), toInt(args[3]), toInt(args[4]));
sender.sendMessage("Updated " + player.getName() + "'s times");
} else {
sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
return false;
}
return true;
}
use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.
the class PlayerAbilitiesHandler method handle.
@Override
public void handle(GlowSession session, PlayerAbilitiesMessage message) {
// player sends this when changing whether or not they are currently flying
// other values should match what we've sent in the past but are ignored here
GlowPlayer player = session.getPlayer();
boolean flying = (message.getFlags() & 0x02) != 0;
player.setFlying(player.getAllowFlight() && flying);
}
use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.
the class PlayerSwingArmHandler method handle.
@Override
public void handle(GlowSession session, PlayerSwingArmMessage message) {
GlowPlayer player = session.getPlayer();
Block block;
try {
block = player.getTargetBlock((Set<Material>) null, 6);
} catch (IllegalStateException ex) {
// getTargetBlock failed to find any block at all
block = null;
}
if (block == null || block.isEmpty()) {
if (EventFactory.onPlayerInteract(player, Action.LEFT_CLICK_AIR).useItemInHand() == Result.DENY)
return;
// todo: item interactions with air
}
if (!EventFactory.callEvent(new PlayerAnimationEvent(player)).isCancelled()) {
// play the animation to others
Message toSend = new AnimateEntityMessage(player.getEntityId(), message.getHand() == 1 ? AnimateEntityMessage.SWING_OFF_HAND : AnimateEntityMessage.SWING_MAIN_HAND);
player.getWorld().getRawPlayers().stream().filter(observer -> observer != player && observer.canSeeEntity(player)).forEach(observer -> observer.getSession().send(toSend));
}
}
use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.
the class PlayerUpdateHandler method handle.
@Override
public void handle(GlowSession session, PlayerUpdateMessage message) {
GlowPlayer player = session.getPlayer();
Location oldLocation = player.getLocation();
Location newLocation = oldLocation.clone();
message.update(newLocation);
// don't let players reach an illegal position
if (Math.abs(newLocation.getBlockX()) > 32000000 || Math.abs(newLocation.getBlockZ()) > 32000000) {
session.getPlayer().kickPlayer("Illegal position");
return;
}
/*
don't let players move more than 100 blocks in a single packet
if they move greater than 10 blocks, but less than 100, just warn
this is NOT robust hack prevention - only to prevent client
confusion about where its actual location is (e.g. during login)
*/
if (message.moved() && !player.isDead()) {
if (player.teleportedTo != null) {
if (newLocation.equals(player.teleportedTo)) {
player.endTeleport();
return;
} else {
// outdated location, so skip packet
return;
}
} else {
double distance = newLocation.distanceSquared(oldLocation);
if (distance > 100 * 100) {
session.getPlayer().kickPlayer("You moved too quickly :( (Hacking?)");
return;
} else if (distance > 100) {
GlowServer.logger.warning(session.getPlayer().getName() + " moved too quickly!");
}
}
}
// call move event if movement actually occurred and there are handlers registered
if (!oldLocation.equals(newLocation) && PlayerMoveEvent.getHandlerList().getRegisteredListeners().length > 0) {
PlayerMoveEvent event = EventFactory.callEvent(new PlayerMoveEvent(player, oldLocation, newLocation));
if (event.isCancelled()) {
// tell client they're back where they started
session.send(new PositionRotationMessage(oldLocation));
return;
}
if (!event.getTo().equals(newLocation)) {
// teleport to the set destination: fires PlayerTeleportEvent and
// handles if the destination is in another world
player.teleport(event.getTo(), TeleportCause.PLUGIN);
return;
}
if (!Objects.equals(session.getPlayer().getLocation(), oldLocation)) {
// plugin changed location on move event
return;
}
}
// move event was not fired or did nothing, simply update location
player.setRawLocation(newLocation);
// do stuff with onGround if we need to
if (player.isOnGround() != message.isOnGround()) {
if (message.isOnGround() && player.getVelocity().getY() > 0) {
// jump
if (player.isSprinting()) {
player.addExhaustion(0.2f);
} else {
player.addExhaustion(0.05f);
}
}
player.setOnGround(message.isOnGround());
}
// Checks if the player is still wearing the Elytra
ItemStack chestplate = player.getInventory().getChestplate();
boolean hasElytra = chestplate != null && chestplate.getType() == Material.ELYTRA && chestplate.getDurability() < chestplate.getType().getMaxDurability();
if (player.isGliding() && (player.isOnGround() || !hasElytra)) {
player.setGliding(false);
}
player.addMoveExhaustion(newLocation);
// track movement stats
Vector delta = newLocation.clone().subtract(oldLocation).toVector();
delta.setX(Math.abs(delta.getX()));
delta.setY(Math.abs(delta.getY()));
delta.setZ(Math.abs(delta.getZ()));
int flatDistance = (int) Math.round(Math.sqrt(NumberConversions.square(delta.getX()) + NumberConversions.square(delta.getZ())) * 100.0);
if (message.isOnGround()) {
if (flatDistance > 0) {
if (player.isSprinting()) {
player.incrementStatistic(Statistic.SPRINT_ONE_CM, flatDistance);
} else if (player.isSneaking()) {
player.incrementStatistic(Statistic.CROUCH_ONE_CM, flatDistance);
} else {
player.incrementStatistic(Statistic.WALK_ONE_CM, flatDistance);
}
}
} else if (player.isFlying()) {
if (flatDistance > 0) {
player.incrementStatistic(Statistic.FLY_ONE_CM, flatDistance);
}
} else if (player.isInWater()) {
if (flatDistance > 0) {
player.incrementStatistic(Statistic.SWIM_ONE_CM, flatDistance);
}
}
}
Aggregations