/** * Called when a player interacts * * @param event Relevant event details */ @EventHandler(event = PlayerInteractEvent.class) public void onPlayerInteract(PlayerInteractEvent event) { final LocalPlayer player = plugin.wrapPlayer(event.getPlayer()); final LocalWorld world = player.getWorld(); final WorldEdit we = plugin.getWorldEdit(); PlayerInteractEvent.Action action = event.getAction(); if (action == Action.LEFT_CLICK) { if (event.isAir() && ignoreLeftClickAir) { return; } if (!event.isAir()) { final Point clickedBlock = event.getInteractedPoint(); final WorldVector pos = new WorldVector(world, clickedBlock.getX(), clickedBlock.getY(), clickedBlock.getZ()); if (we.handleBlockLeftClick(player, pos)) { event.setCancelled(true); } } if (we.handleArmSwing(player)) { event.setCancelled(true); } if (!event.isAir() && !ignoreLeftClickAir) { final int taskId = Spout.getGame() .getScheduler() .scheduleSyncDelayedTask( plugin, new Runnable() { public void run() { ignoreLeftClickAir = false; } }, 2); if (taskId != -1) { ignoreLeftClickAir = true; } } } else if (action == Action.RIGHT_CLICK) { if (!event.isAir()) { final Point clickedBlock = event.getInteractedPoint(); final WorldVector pos = new WorldVector(world, clickedBlock.getX(), clickedBlock.getY(), clickedBlock.getZ()); if (we.handleBlockRightClick(player, pos)) { event.setCancelled(true); } } if (we.handleRightClick(player)) { event.setCancelled(true); } } }
@Override protected void sendPosition(Point p, Quaternion rot) { PlayerPositionLookMessage PPLMsg = new PlayerPositionLookMessage( p.getX(), p.getY() + STANCE, p.getZ(), p.getY(), rot.getYaw(), rot.getPitch(), true, VanillaBlockDataChannelMessage.CHANNEL_ID, getRepositionManager()); session.send(false, PPLMsg); }
@Override protected void freeChunk(Point p) { int x = (int) p.getX() >> Chunk.BLOCKS.BITS; int y = (int) p.getY() >> Chunk.BLOCKS.BITS; // + SEALEVEL_CHUNK; int z = (int) p.getZ() >> Chunk.BLOCKS.BITS; if (y < 0 || y >= p.getWorld().getHeight() >> Chunk.BLOCKS.BITS) { return; } TIntSet column = initializedChunks.get(x, z); CompressedChunkMessage CCMsg; if (column != null) { column.remove(y); if (column.isEmpty()) { if (initializedChunks.remove(x, z) != null) { activeChunks.remove(x, z); } CCMsg = new CompressedChunkMessage(x, z, true, null, null, null, true); } else { CCMsg = new CompressedChunkMessage(x, z, false, null, null, null, true); } session.send(false, CCMsg); } }
@Override public Region getRegion(Point point, boolean load) { int x = MathHelper.floor(point.getX()); int y = MathHelper.floor(point.getY()); int z = MathHelper.floor(point.getZ()); return regions.getRegionFromBlock(x, y, z, load); }
@Override public Block getBlock(Point point) { int x = MathHelper.floor(point.getX()); int y = MathHelper.floor(point.getY()); int z = MathHelper.floor(point.getZ()); return getBlock(x, y, z); }
@Override protected void worldChanged(World world) { GameMode gamemode = world.getComponentHolder().getData().get(VanillaData.GAMEMODE); // The world the player is entering has a different gamemode... if (gamemode != null) { if (gamemode != getPlayer().getData().get(VanillaData.GAMEMODE)) { PlayerGameModeChangedEvent event = Spout.getEngine() .getEventManager() .callEvent(new PlayerGameModeChangedEvent(player, gamemode)); if (!event.isCancelled()) { gamemode = event.getMode(); } } } else { // The world has no gamemode setting in its map so default to the Player's GameMode. gamemode = getPlayer().getData().get(VanillaData.GAMEMODE); } Difficulty difficulty = world.getComponentHolder().getData().get(VanillaData.DIFFICULTY); Dimension dimension = world.getComponentHolder().getData().get(VanillaData.DIMENSION); WorldType worldType = world.getComponentHolder().getData().get(VanillaData.WORLD_TYPE); // TODO Handle infinite height if (first) { first = false; int entityId = player.getId(); Server server = (Server) session.getEngine(); PlayerLoginRequestMessage idMsg = new PlayerLoginRequestMessage( entityId, worldType.toString(), gamemode.getId(), (byte) dimension.getId(), difficulty.getId(), (byte) server.getMaxPlayers()); player.getSession().send(false, true, idMsg); player.getSession().setState(State.GAME); for (int slot = 0; slot < 4; slot++) { ItemStack slotItem = owner.get(Human.class).getInventory().getArmor().get(slot); player.getSession().send(false, new EntityEquipmentMessage(entityId, slot, slotItem)); } } else { player .getSession() .send( false, new PlayerRespawnMessage( dimension.getId(), difficulty.getId(), gamemode.getId(), 256, worldType.toString())); } Point pos = world.getSpawnPoint().getPosition(); PlayerSpawnPositionMessage SPMsg = new PlayerSpawnPositionMessage((int) pos.getX(), (int) pos.getY(), (int) pos.getZ()); player.getSession().send(false, SPMsg); }
/** * Gets the largest distance between two points, when projected onto one of the axes. * * <p>This will return Double.MAX_VALUE if the other Point is null, either world is null, or the * two points are in different worlds. * * <p>Otherwise, it returns the max distance. */ public double getMaxDistance(Point other) { if (other == null || world == null || other.world == null || !world.equals(other.world)) { return Double.MAX_VALUE; } return Math.max( Math.abs(getX() - other.getX()), Math.max(Math.abs(getY() - other.getY()), Math.abs(getZ() - other.getZ()))); }
@Override public Chunk getChunk(Point point, boolean load) { int x = MathHelper.floor(point.getX()); int y = MathHelper.floor(point.getY()); int z = MathHelper.floor(point.getZ()); return getChunk( x >> Chunk.CHUNK_SIZE_BITS, y >> Chunk.CHUNK_SIZE_BITS, z >> Chunk.CHUNK_SIZE_BITS, load); }
/** * Gets the square of the distance between two points. * * <p>This will return Double.MAX_VALUE if the other Point is null, either world is null, or the * two points are in different worlds. * * <p>Otherwise, it returns the Manhattan distance. */ public double getSquaredDistance(Point other) { if (other == null || world == null || other.world == null || !world.equals(other.world)) { return Double.MAX_VALUE; } double dx = getX() - other.getX(); double dy = getY() - other.getY(); double dz = getZ() - other.getZ(); return dx * dx + dy * dy + dz * dz; }
public void strikePlayers(List<Player> toStrike) { for (Player player : toStrike) { Point playerPos = player.getScene().getPosition(); final int posX = GenericMath.floor(playerPos.getX()); final int posY = GenericMath.floor(playerPos.getY()); final int posZ = GenericMath.floor(playerPos.getZ()); for (int tries = 0; tries < 10; tries++) { // pick a random chunk between -4, -4, to 4, 4 relative to the player's position to strike // at int cx = (ra.nextBoolean() ? -1 : 1) * ra.nextInt(5); int cz = (ra.nextBoolean() ? -1 : 1) * ra.nextInt(5); // pick random coords to try to strike at inside the chunk (0, 0) to (15, 15) int rx = ra.nextInt(16); int rz = ra.nextInt(16); // pick a offset from the player's y position to strike at (-15 - +15) of their position int offsetY = (ra.nextBoolean() ? -1 : 1) * ra.nextInt(15); int x = posX + cx * 16 + rx; int y = posY + offsetY; int z = posZ + cz * 16 + rz; if (weather.isRainingAt(x, y, z, false)) { int lightning = 1; // 30% chance of extra lightning at the spot if (ra.nextInt(10) < 3) { lightning += ra.nextInt(MAX_LIGHTNING_BRANCHES); } for (int strikes = 0; strikes < lightning; strikes++) { float adjustX = 0.5F; float adjustY = 0.0F; float adjustZ = 0.5F; // if there are extra strikes, tweak their placement slightly if (strikes > 0) { adjustX += (ra.nextBoolean() ? -1 : 1) * ra.nextInt(2); adjustY += (ra.nextBoolean() ? -1 : 1) * ra.nextInt(8); adjustZ += (ra.nextBoolean() ? -1 : 1) * ra.nextInt(2); } World world = getWorld(); Point point = new Point(world, x + adjustX, y + adjustY, z + adjustZ); world.createAndSpawnEntity(point, Lightning.class, LoadOption.NO_LOAD); for (Player p : GeneralEffects.LIGHTNING_THUNDER.getNearbyPlayers(point, null, 600)) { double dist = p.getScene().getPosition().distanceSquared(point); float volume = (float) (10000F - Math.pow(dist, 0.73)); if (volume > 0) { GeneralEffects.LIGHTNING_THUNDER.adjust(volume, 0.7F).play(p, point); } } } // success, go to the next player break; } } } }
@Override protected void sendPosition(Point p, Quaternion rot) { // TODO: Implement Spout Protocol Session session = owner.getSession(); if (p.distanceSquared(entity.getPosition()) >= 16) { EntityTeleportMessage ETMMsg = new EntityTeleportMessage( entity.getId(), (int) p.getX(), (int) p.getY(), (int) p.getZ(), (int) rot.getYaw(), (int) rot.getPitch()); PlayerLookMessage PLMsg = new PlayerLookMessage(rot.getYaw(), rot.getPitch(), true); session.sendAll(false, ETMMsg, PLMsg); } else { PlayerPositionLookMessage PPLMsg = new PlayerPositionLookMessage( p.getX(), p.getY() + STANCE, p.getZ(), STANCE, rot.getYaw(), rot.getPitch(), true); session.send(false, PPLMsg); } }
@Override public boolean equals(Object obj) { if (!(obj instanceof Point)) { return false; } else { Point point = (Point) obj; boolean worldEqual = point.world == world || (point.world != null && point.world.equals(world)); return worldEqual && point.getX() == getX() && point.getY() == getY() && point.getZ() == getZ(); } }
@Override protected void worldChanged(World world) { // Grab world characteristics. GameMode gamemode = world.getDataMap().get(VanillaData.GAMEMODE); Difficulty difficulty = world.getDataMap().get(VanillaData.DIFFICULTY); Dimension dimension = world.getDataMap().get(VanillaData.DIMENSION); WorldType worldType = world.getDataMap().get(VanillaData.WORLD_TYPE); // TODO Handle infinite height if (first) { first = false; int entityId = owner.getEntity().getId(); VanillaPlayer vc = (VanillaPlayer) owner.getEntity().getController(); LoginRequestMessage idMsg = new LoginRequestMessage( entityId, worldType.toString(), gamemode.getId(), (byte) dimension.getId(), difficulty.getId(), (byte) session.getEngine().getMaxPlayers()); owner.getSession().send(false, true, idMsg); owner.getSession().setState(State.GAME); for (int slot = 0; slot < 4; slot++) { ItemStack slotItem = vc.getInventory().getArmor().getItem(slot); owner.getSession().send(false, new EntityEquipmentMessage(entityId, slot, slotItem)); } } else { owner .getSession() .send( false, new RespawnMessage( dimension.getId(), difficulty.getId(), gamemode.getId(), 256, worldType.toString())); } Point pos = world.getSpawnPoint().getPosition(); SpawnPositionMessage SPMsg = new SpawnPositionMessage((int) pos.getX(), (int) pos.getY(), (int) pos.getZ()); owner.getSession().send(false, SPMsg); }
@Override protected void freeChunk(Point p) { int x = (int) p.getX() >> Chunk.BLOCKS.BITS; int y = (int) p.getY() >> Chunk.BLOCKS.BITS; // + SEALEVEL_CHUNK; int z = (int) p.getZ() >> Chunk.BLOCKS.BITS; RepositionManager rm = getRepositionManager(); int cY = rm.convertChunkY(y); if (cY < 0 || cY >= p.getWorld().getHeight() >> Chunk.BLOCKS.BITS) { return; } TIntSet column = initializedChunks.get(x, z); if (column != null) { column.remove(y); if (column.isEmpty()) { emptyColumns.add(IntPairHashed.key(x, z)); } } }
@Command( aliases = {"list", "ls"}, desc = "Lists the existing waypoints", min = 0, max = 0) @Permissible("plugintest.waypoint.list") public void list(CommandSource source, CommandArguments args) throws CommandException { for (Map.Entry<String, Point> entry : this.waypoints.entrySet()) { Point p = entry.getValue(); source.sendMessage( entry.getKey() + " -> (" + p.getWorld().getName() + ":" + p.getX() + "," + p.getY() + "," + p.getZ() + ")"); } }
@Override protected void freeChunk(Point p) { int x = (int) p.getX() >> Chunk.BLOCKS.BITS; int y = (int) p.getY() >> Chunk.BLOCKS.BITS; // + SEALEVEL_CHUNK; int z = (int) p.getZ() >> Chunk.BLOCKS.BITS; if (y < 0 || y >= p.getWorld().getHeight() >> Chunk.BLOCKS.BITS) { return; } TIntSet column = initializedChunks.get(x, z); if (column != null) { column.remove(y); if (column.isEmpty()) { emptyColumns.add(IntPairHashed.key(x, z)); } // TODO - is this required? /* else { CCMsg = new ChunkDataMessage(x, z, false, null, null, null, true); }*/ } }
@Override public List<Message> getSpawnMessages(Entity entity, RepositionManager rm) { FallingBlock block = entity.get(FallingBlock.class); if (block != null) { VanillaBlockMaterial material = block.getMaterial(); int messageData = material.getMinecraftId() | (material.getMinecraftData(material.getData()) >> 16); List<Message> messages = new ArrayList<Message>(); final double p = 32d; Point pos = entity.getScene().getPosition(); int x = (int) Math.floor(pos.getX() * p); int y = (int) Math.floor(pos.getY() * p); int z = (int) Math.floor(pos.getZ() * p); byte yaw = (byte) ChannelBufferUtils.protocolifyYaw(entity.getScene().getRotation().getYaw()); byte pitch = (byte) ChannelBufferUtils.protocolifyPitch(entity.getScene().getRotation().getPitch()); short fallSpeed = (short) (block.getFallingSpeed() * 8000d); messages.add( new EntityObjectMessage( entity.getId(), (byte) typeId, x, y, z, messageData, (short) 0, fallSpeed, (short) 0, yaw, pitch, rm)); messages.add(new EntityMetadataMessage(entity.getId(), getSpawnParameters(entity))); return messages; } else { return Collections.emptyList(); } }
@Override protected void worldChanged(World world) { WorldConfigurationNode node = VanillaConfiguration.WORLDS.get(world); maxY = node.MAX_Y.getInt() & (~Chunk.BLOCKS.MASK); minY = node.MIN_Y.getInt() & (~Chunk.BLOCKS.MASK); stepY = node.STEP_Y.getInt() & (~Chunk.BLOCKS.MASK); lowY = maxY - stepY; highY = minY + stepY; lastY = Integer.MAX_VALUE; final DatatableComponent data = world.getComponentHolder().getData(); final Human human = player.get(Human.class); GameMode gamemode = null; Difficulty difficulty = data.get(VanillaData.DIFFICULTY); Dimension dimension = data.get(VanillaData.DIMENSION); WorldType worldType = data.get(VanillaData.WORLD_TYPE); int entityId = player.getId(); if (first) { first = false; if (human != null && human.getAttachedCount() > 1) { gamemode = human.getGameMode(); } else { gamemode = data.get(VanillaData.GAMEMODE); if (human != null) { human.setGamemode(gamemode); } } Server server = (Server) session.getEngine(); PlayerLoginRequestMessage idMsg = new PlayerLoginRequestMessage( entityId, worldType.toString(), gamemode.getId(), (byte) dimension.getId(), difficulty.getId(), (byte) server.getMaxPlayers()); player.getSession().send(false, true, idMsg); player.getSession().setState(State.GAME); } else { if (human != null) { gamemode = human.getGameMode(); } player .getSession() .send( false, new PlayerRespawnMessage( 0, difficulty.getId(), gamemode.getId(), 256, worldType.toString())); player .getSession() .send( false, new PlayerRespawnMessage( 1, difficulty.getId(), gamemode.getId(), 256, worldType.toString())); player .getSession() .send( false, new PlayerRespawnMessage( dimension.getId(), difficulty.getId(), gamemode.getId(), 256, worldType.toString())); } if (human != null) { if (first) { human.setGamemode(gamemode, false); } human.updateAbilities(); } PlayerInventory inv = player.get(PlayerInventory.class); if (inv != null) { inv.updateAll(); } Point pos = world.getSpawnPoint().getPosition(); PlayerSpawnPositionMessage SPMsg = new PlayerSpawnPositionMessage( (int) pos.getX(), (int) pos.getY(), (int) pos.getZ(), getRepositionManager()); player.getSession().send(false, SPMsg); session.send( false, new PlayerHeldItemChangeMessage( session .getPlayer() .add(PlayerInventory.class) .getQuickbar() .getSelectedSlot() .getIndex())); VanillaSky.getSky(world).updatePlayer(player); }
public static Vector toVector(Point loc) { return new Vector(loc.getX(), loc.getY(), loc.getZ()); }
public static Point center(Point loc) { return new Point(loc.getWorld(), loc.getX() + 0.5F, loc.getY() + 0.5F, loc.getZ() + 0.5F); }
public void doInput() { // inputManager.pollInput(); // One Mouse.getDX() to rule them all // TODO move this a plugin if (activePlayer == null) { return; } if (Mouse.isGrabbed()) { Transform ts = activePlayer.getTransform().getTransform(); float pitch = ts.getRotation().getPitch(); float yaw = ts.getRotation().getYaw(); float mouseDX = -Mouse.getDX() * 0.16f; float mouseDY = Mouse.getDY() * 0.16f; if (yaw + mouseDX >= 360) yaw += mouseDX - 360; else if (yaw + mouseDX < 0) yaw += mouseDX + 360; else yaw += mouseDX; if (pitch + mouseDY >= -80 && pitch + mouseDY <= 80) pitch += mouseDY; else if (pitch + mouseDY < -80) pitch = -80; else if (pitch + mouseDY > 80) pitch = 80; // System.out.println("yaw: "+yaw+" pitch: "+pitch); ts.setRotation(MathHelper.rotation(pitch, yaw, ts.getRotation().getRoll())); activePlayer.getTransform().setTransform(ts); // System.out.println(activePlayer.getTransform().getTransform().toMatrix().toString()); } boolean keyUp = Keyboard.isKeyDown(Keyboard.KEY_UP) || Keyboard.isKeyDown(Keyboard.KEY_Z); boolean keyDown = Keyboard.isKeyDown(Keyboard.KEY_DOWN) || Keyboard.isKeyDown(Keyboard.KEY_S); boolean keyLeft = Keyboard.isKeyDown(Keyboard.KEY_LEFT) || Keyboard.isKeyDown(Keyboard.KEY_Q); boolean keyRight = Keyboard.isKeyDown(Keyboard.KEY_RIGHT) || Keyboard.isKeyDown(Keyboard.KEY_D); boolean flyUp = Keyboard.isKeyDown(Keyboard.KEY_SPACE); boolean flyDown = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT); Point point = new Point(Point.ONE, activePlayer.getWorld()); if (keyUp) { point = point.multiply(activePlayer.getTransform().getTransform().forwardVector()); // activePlayer.getTransform().setPosition(activePlayer.getTransform().getPosition().subtract(activePlayer.getTransform().getTransform().forwardVector())); } if (keyDown) { point = point.multiply(activePlayer.getTransform().getTransform().forwardVector().multiply(-1)); // activePlayer.getTransform().setPosition(activePlayer.getTransform().getPosition().add(activePlayer.getTransform().getTransform().forwardVector())); } if (keyLeft) { point = point.multiply(activePlayer.getTransform().getTransform().rightVector()); // activePlayer.getTransform().setPosition(activePlayer.getTransform().getPosition().subtract(activePlayer.getTransform().getTransform().rightVector())); } if (keyRight) { point = point.multiply(activePlayer.getTransform().getTransform().rightVector().multiply(-1)); // activePlayer.getTransform().setPosition(activePlayer.getTransform().getPosition().add(activePlayer.getTransform().getTransform().rightVector())); } if (flyUp) { point = point.multiply(activePlayer.getTransform().getTransform().upVector()); // activePlayer.getTransform().setPosition(activePlayer.getTransform().getPosition().add(activePlayer.getTransform().getTransform().upVector())); } if (flyDown) { point = point.multiply(activePlayer.getTransform().getTransform().upVector().multiply(-1)); // activePlayer.getTransform().setPosition(activePlayer.getTransform().getPosition().subtract(activePlayer.getTransform().getTransform().upVector())); } if (keyUp || keyDown || keyLeft || keyRight || flyUp || flyDown) { // point = new Point(point.normalize(),activePlayer.getWorld()); System.out.println("Translation : " + point.getX() + "/" + point.getY() + "/" + point.getZ()); System.out.println( "Position : " + activePlayer.getTransform().getPosition().getX() + "/" + activePlayer.getTransform().getPosition().getY() + "/" + activePlayer.getTransform().getPosition().getZ()); activePlayer.getTransform().setPosition(activePlayer.getTransform().getPosition().add(point)); } /*for (Flags f : activePlayer.input().getFlagSet()) { switch(f) { case FORWARD: activePlayer.getTransform().setPosition(activePlayer.getTransform().getPosition().add(activePlayer.getTransform().getTransform().forwardVector())); break; case BACKWARD: activePlayer.getTransform().setPosition(activePlayer.getTransform().getPosition().subtract(activePlayer.getTransform().getTransform().forwardVector())); break; case LEFT: activePlayer.getTransform().setPosition(activePlayer.getTransform().getPosition().add(activePlayer.getTransform().getTransform().rightVector())); break; case RIGHT: activePlayer.getTransform().setPosition(activePlayer.getTransform().getPosition().subtract(activePlayer.getTransform().getTransform().rightVector())); break; case CROUCH: case FIRE_1: case FIRE_2: case INTERACT: case JUMP: case SELECT_DOWN: case SELECT_UP: } }*/ }