Search in sources :

Example 51 with GlowPlayer

use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.

the class ItemItemFrame method rightClickBlock.

@Override
public void rightClickBlock(GlowPlayer player, GlowBlock target, BlockFace face, ItemStack holding, Vector clickedLoc, EquipmentSlot hand) {
    GlowItemFrame entity = new GlowItemFrame(player, target.getRelative(face).getLocation(), face);
    if (EventFactory.getInstance().callEvent(new HangingPlaceEvent(entity, player, target, face)).isCancelled()) {
        return;
    }
    List<Message> spawnMessage = entity.createSpawnMessage();
    entity.getWorld().getRawPlayers().stream().filter(p -> p.canSeeEntity(entity)).forEach(p -> p.getSession().sendAll(spawnMessage.toArray(new Message[spawnMessage.size()])));
}
Also used : Vector(org.bukkit.util.Vector) GlowBlock(net.glowstone.block.GlowBlock) List(java.util.List) EventFactory(net.glowstone.EventFactory) Message(com.flowpowered.network.Message) GlowPlayer(net.glowstone.entity.GlowPlayer) BlockFace(org.bukkit.block.BlockFace) GlowItemFrame(net.glowstone.entity.objects.GlowItemFrame) HangingPlaceEvent(org.bukkit.event.hanging.HangingPlaceEvent) EquipmentSlot(org.bukkit.inventory.EquipmentSlot) ItemStack(org.bukkit.inventory.ItemStack) HangingPlaceEvent(org.bukkit.event.hanging.HangingPlaceEvent) Message(com.flowpowered.network.Message) GlowItemFrame(net.glowstone.entity.objects.GlowItemFrame)

Example 52 with GlowPlayer

use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.

the class PlaySoundCommand method execute.

@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
    if (!testPermission(sender, commandMessages.getPermissionMessage())) {
        return true;
    }
    if (args.length < 3 || args.length == 4 || args.length == 5) {
        sendUsageMessage(sender, commandMessages);
        return false;
    }
    final World world = CommandUtils.getWorld(sender);
    String stringSound = args[0];
    String stringCategory = args[1];
    String playerPattern = args[2];
    final Sound sound = GlowSound.getVanillaSound(CommandUtils.toNamespaced(stringSound));
    final SoundCategory soundCategory = SoundUtil.buildSoundCategory(stringCategory);
    List<GlowPlayer> targets;
    boolean relativeLocation = false;
    double volume = 1;
    double minimumVolume = 0;
    double pitch = 1;
    if (sound == null) {
        new LocalizedStringImpl("playsound.invalid", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, stringSound);
        return false;
    }
    if (soundCategory == null) {
        new LocalizedStringImpl("playsound.invalid.category", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, stringCategory);
        return false;
    }
    // Manage player(s)
    if (playerPattern.startsWith("@") && playerPattern.length() > 1 && CommandUtils.isPhysical(sender)) {
        // Manage selectors
        final Location senderLocation = CommandUtils.getLocation(sender);
        final Entity[] entities = new CommandTarget(sender, args[0]).getMatched(senderLocation);
        targets = Arrays.stream(entities).filter(GlowPlayer.class::isInstance).map(GlowPlayer.class::cast).collect(Collectors.toList());
    } else {
        final GlowPlayer player = (GlowPlayer) Bukkit.getPlayerExact(playerPattern);
        if (player == null) {
            commandMessages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, playerPattern);
            return false;
        } else {
            targets = Collections.singletonList(player);
        }
    }
    if (args.length >= 9) {
        try {
            minimumVolume = Double.valueOf(args[8]);
            if (minimumVolume < 0 || minimumVolume > 1) {
                new LocalizedStringImpl("playsound.invalid.volume", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, args[8]);
                return false;
            }
        } catch (final NumberFormatException n) {
            commandMessages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, args[8]);
            return false;
        }
    }
    if (args.length >= 8) {
        try {
            pitch = Double.valueOf(args[7]);
            if (pitch < 0 || pitch > 2) {
                new LocalizedStringImpl("playsound.invalid.pitch", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, args[7]);
                return false;
            } else if (pitch < 0.5) {
                pitch = 0.5;
            }
        } catch (final NumberFormatException n) {
            commandMessages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, args[7]);
            return false;
        }
    }
    if (args.length >= 7) {
        try {
            volume = Double.valueOf(args[6]);
        } catch (final NumberFormatException n) {
            commandMessages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, args[6]);
            return false;
        }
    }
    if (args.length >= 6) {
        relativeLocation = args[3].startsWith("~") || args[4].startsWith("~") || args[5].startsWith("~");
    }
    for (final GlowPlayer target : targets) {
        Location soundLocation;
        Location targetLocation = target.getLocation();
        double targetVolume = volume;
        try {
            if (relativeLocation) {
                soundLocation = CommandUtils.getLocation(targetLocation, args[3], args[4], args[5]);
            } else if (args.length >= 6) {
                soundLocation = CommandUtils.getLocation(new Location(world, 0, 0, 0), args[3], args[4], args[5]);
            } else {
                soundLocation = targetLocation;
            }
        } catch (final NumberFormatException n) {
            new LocalizedStringImpl("playsound.invalid.position", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, args[3], args[4], args[5]);
            return false;
        }
        // If the target is outside the normal audible sphere
        if (targetLocation.distanceSquared(soundLocation) > Math.pow(volume, 2)) {
            if (minimumVolume <= 0) {
                new LocalizedStringImpl("playsound.too-far", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, target.getName());
                return false;
            } else {
                final double deltaX = soundLocation.getX() - targetLocation.getX();
                final double deltaY = soundLocation.getX() - targetLocation.getY();
                final double deltaZ = soundLocation.getX() - targetLocation.getZ();
                final double delta = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2) + Math.pow(deltaZ, 2));
                soundLocation = targetLocation;
                soundLocation.add(deltaX / delta, deltaY / delta, deltaZ / delta);
                targetVolume = minimumVolume;
            }
        }
        target.playSound(soundLocation, sound, soundCategory, (float) targetVolume, (float) pitch);
    }
    return true;
}
Also used : Entity(org.bukkit.entity.Entity) SoundCategory(org.bukkit.SoundCategory) CommandTarget(net.glowstone.command.CommandTarget) GlowPlayer(net.glowstone.entity.GlowPlayer) Sound(org.bukkit.Sound) GlowSound(net.glowstone.constants.GlowSound) World(org.bukkit.World) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) Location(org.bukkit.Location)

Example 53 with GlowPlayer

use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.

the class GlowItemFrame method createTeleportMessage.

private void createTeleportMessage(BlockFace face) {
    int xoffset = 0;
    int zoffset = 0;
    int yaw = getYaw();
    switch(face) {
        case WEST:
            xoffset = -32;
            break;
        case NORTH:
            zoffset = -32;
            break;
        case EAST:
            xoffset = 32;
            break;
        case SOUTH:
            zoffset = 32;
            break;
        default:
    }
    Key key = GlowChunk.Key.of(location.getBlockX() >> 4, location.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(entityId, x + xoffset, y, z + zoffset, yaw, 0));
        }
    }
}
Also used : GlowPlayer(net.glowstone.entity.GlowPlayer) EntityTeleportMessage(net.glowstone.net.message.play.entity.EntityTeleportMessage) Key(net.glowstone.chunk.GlowChunk.Key)

Example 54 with GlowPlayer

use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.

the class GlowEgg method randomHatchSpawning.

/**
 * Handle spawning entities when the egg breaks.
 *
 * @param location The location the egg breaks
 */
private void randomHatchSpawning(Location location) {
    ThreadLocalRandom random = ThreadLocalRandom.current();
    // There is a 1/8 chance for egg to hatch and spawn at least 1 entity
    boolean hatching = random.nextInt(8) == 0;
    // ...and if the egg is hatching, there is now
    // 1/32 chance to spawn 3 more entities.
    byte amount;
    if (hatching) {
        amount = (byte) ((random.nextInt(32) == 0) ? 4 : 1);
    } else {
        amount = 0;
    }
    EntityType hatchingType = EntityType.CHICKEN;
    final ProjectileSource shooter = getShooter();
    if (shooter instanceof GlowPlayer) {
        PlayerEggThrowEvent event = EventFactory.getInstance().callEvent(new PlayerEggThrowEvent((GlowPlayer) shooter, this, hatching, amount, hatchingType));
        amount = event.getNumHatches();
        hatching = event.isHatching();
        hatchingType = event.getHatchingType();
    }
    if (hatching) {
        for (int i = 0; i < amount; i++) {
            GlowEntity entity = (GlowEntity) location.getWorld().spawnEntity(location.clone().add(0, 0.3, 0), hatchingType);
            if (entity instanceof GlowAgeable) {
                ((GlowAgeable) entity).setBaby();
            }
        }
    }
}
Also used : EntityType(org.bukkit.entity.EntityType) GlowPlayer(net.glowstone.entity.GlowPlayer) PlayerEggThrowEvent(org.bukkit.event.player.PlayerEggThrowEvent) GlowEntity(net.glowstone.entity.GlowEntity) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) GlowAgeable(net.glowstone.entity.GlowAgeable) ProjectileSource(org.bukkit.projectiles.ProjectileSource)

Example 55 with GlowPlayer

use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.

the class EntityStorage method load.

/**
 * Load an entity's data from the given compound tag.
 *
 * @param entity   The target entity.
 * @param compound The tag to load from.
 */
public static void load(GlowEntity entity, CompoundTag compound) {
    // look up the store for the entity
    EntityStore<?> store = find(entity.getClass(), "load");
    // work out the entity's location, using its current location if unavailable
    World world = NbtSerialization.readWorld(entity.getServer(), compound);
    if (world == null) {
        world = entity.getWorld();
    }
    Location location = NbtSerialization.listTagsToLocation(world, compound);
    if (location != null) {
        if (entity instanceof GlowPlayer && !((GlowPlayer) entity).hasJoined()) {
            entity.setRawLocation(location);
        } else {
            entity.teleport(location);
        }
    }
    // read the rest of the entity's information
    getBaseStore(store).load(entity, compound);
}
Also used : GlowPlayer(net.glowstone.entity.GlowPlayer) World(org.bukkit.World) GlowWorld(net.glowstone.GlowWorld) Location(org.bukkit.Location)

Aggregations

GlowPlayer (net.glowstone.entity.GlowPlayer)69 ItemStack (org.bukkit.inventory.ItemStack)15 Vector (org.bukkit.util.Vector)10 EventFactory (net.glowstone.EventFactory)9 Location (org.bukkit.Location)9 Player (org.bukkit.entity.Player)9 GlowEntity (net.glowstone.entity.GlowEntity)8 Message (com.flowpowered.network.Message)7 ArrayList (java.util.ArrayList)7 GlowBlock (net.glowstone.block.GlowBlock)7 Material (org.bukkit.Material)6 World (org.bukkit.World)6 Entity (org.bukkit.entity.Entity)6 List (java.util.List)5 GlowWorld (net.glowstone.GlowWorld)5 LocalizedStringImpl (net.glowstone.i18n.LocalizedStringImpl)5 BlockFace (org.bukkit.block.BlockFace)5 CommandTarget (net.glowstone.command.CommandTarget)4 AsyncableMessage (com.flowpowered.network.AsyncableMessage)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)3