Search in sources :

Example 36 with GlowPlayer

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

the class CreativeItemHandler method handle.

@Override
public void handle(GlowSession session, CreativeItemMessage message) {
    GlowPlayer player = session.getPlayer();
    GlowInventory inv = player.getInventory();
    // CraftBukkit does use a inventory view with both inventories set to the player's inventory
    // for the creative inventory as there is no second inventory (no crafting) visible for the client
    InventoryView view = player.getOpenInventory();
    int viewSlot = message.getSlot();
    int slot = view.convertSlot(viewSlot);
    ItemStack stack = ItemIds.sanitize(message.getItem());
    SlotType type = inv.getSlotType(slot);
    // only if creative mode
    if (player.getGameMode() != GameMode.CREATIVE) {
        player.kickPlayer("Illegal creative mode item selection");
        return;
    }
    // only if default (player) inventory
    if (!GlowInventoryView.isDefault(player.getOpenInventory())) {
        player.kickPlayer("Illegal creative mode item selection");
        return;
    }
    // clicking outside drops the item
    if (message.getSlot() < 0) {
        InventoryCreativeEvent event = EventFactory.callEvent(new InventoryCreativeEvent(view, SlotType.OUTSIDE, -999, stack));
        if (event.isCancelled()) {
            session.send(new SetWindowSlotMessage(-1, -1, stack));
        } else {
            player.drop(event.getCursor());
        }
        return;
    }
    // this happens quiet often as the client tends to update the whole inventory at once
    if (Objects.equals(stack, view.getItem(viewSlot))) {
        return;
    }
    InventoryCreativeEvent event = EventFactory.callEvent(new InventoryCreativeEvent(view, type, viewSlot, stack));
    if (event.isCancelled()) {
        // send original slot to player to prevent async inventories
        player.sendItemChange(viewSlot, view.getItem(viewSlot));
        // don't keep track of player's current item, just give them back what they tried to place
        session.send(new SetWindowSlotMessage(-1, -1, stack));
        return;
    }
    view.setItem(viewSlot, stack);
}
Also used : GlowInventoryView(net.glowstone.inventory.GlowInventoryView) InventoryView(org.bukkit.inventory.InventoryView) InventoryCreativeEvent(org.bukkit.event.inventory.InventoryCreativeEvent) GlowPlayer(net.glowstone.entity.GlowPlayer) SetWindowSlotMessage(net.glowstone.net.message.play.inv.SetWindowSlotMessage) GlowInventory(net.glowstone.inventory.GlowInventory) ItemStack(org.bukkit.inventory.ItemStack) SlotType(org.bukkit.event.inventory.InventoryType.SlotType)

Example 37 with GlowPlayer

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

the class InteractEntityHandler method handle.

@Override
public void handle(GlowSession session, InteractEntityMessage message) {
    GlowPlayer player = session.getPlayer();
    // You can't do anything when you're dead
    if (player.isDead()) {
        GlowServer.logger.info("Player " + player.getName() + " tried to interact with an entity while dead");
        return;
    }
    GlowEntity possibleTarget = player.getWorld().getEntityManager().getEntity(message.getId());
    GlowLivingEntity target = possibleTarget instanceof GlowLivingEntity ? (GlowLivingEntity) possibleTarget : null;
    if (message.getAction() == Action.ATTACK.ordinal()) {
        if (target == null) {
            if (possibleTarget != null) {
                possibleTarget.entityInteract(player, message);
            } else {
                GlowServer.logger.info("Player " + player.getName() + " tried to attack an entity that does not exist");
            }
        } else if (!target.isDead() && target.canTakeDamage(DamageCause.ENTITY_ATTACK)) {
            // Calculate damage amount
            ItemStack hand = player.getItemInHand();
            Material type = hand == null ? Material.AIR : hand.getType();
            // todo: Actual critical hit check
            float damage = AttackDamage.getMeleeDamage(type, false);
            // Set entity on fire if the item has Fire Aspect
            if (hand.getEnchantments().containsKey(Enchantment.FIRE_ASPECT)) {
                target.setFireTicks(target.getFireTicks() + hand.getEnchantments().get(Enchantment.FIRE_ASPECT) * 80);
            }
            // Apply damage. Calls the EntityDamageByEntityEvent
            target.damage(damage, player, DamageCause.ENTITY_ATTACK);
            player.incrementStatistic(Statistic.DAMAGE_DEALT, Math.round(damage));
            player.addExhaustion(0.1f);
            if (target.isDead()) {
                player.incrementStatistic(target.getType() == EntityType.PLAYER ? Statistic.PLAYER_KILLS : Statistic.MOB_KILLS);
            }
            // Apply durability loss (if applicable)
            short durabilityLoss = AttackDamage.getMeleeDurabilityLoss(type);
            if (durabilityLoss > 0 && !InventoryUtil.isEmpty(hand) && player.getGameMode() != GameMode.CREATIVE) {
                // Yes, this actually subtracts
                hand.setDurability((short) (hand.getDurability() + durabilityLoss));
            }
        }
    } else if (message.getAction() == Action.INTERACT_AT.ordinal()) {
    //todo: Handle hand variable
    // todo: Interaction with entity at a specified location (X, Y, and Z are present in the message)
    // used for adjusting specific portions of armor stands
    } else if (message.getAction() == Action.INTERACT.ordinal()) {
        //Todo: Handle hand variable
        PlayerInteractEntityEvent event = new PlayerInteractEntityEvent(player, possibleTarget);
        EventFactory.callEvent(event);
        if (!event.isCancelled()) {
            possibleTarget.entityInteract(player, message);
        }
    } else {
        GlowServer.logger.info("Player " + player.getName() + " sent unknown interact action: " + message.getAction());
    }
}
Also used : GlowPlayer(net.glowstone.entity.GlowPlayer) GlowEntity(net.glowstone.entity.GlowEntity) GlowLivingEntity(net.glowstone.entity.GlowLivingEntity) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) PlayerInteractEntityEvent(org.bukkit.event.player.PlayerInteractEntityEvent)

Example 38 with GlowPlayer

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

the class QueryTest method testFullStats.

@Test
public void testFullStats() throws Exception {
    World world = mock(World.class);
    when(world.getName()).thenReturn("world");
    GlowPlayer p1 = mock(GlowPlayer.class);
    GlowPlayer p2 = mock(GlowPlayer.class);
    when(p1.getName()).thenReturn("barneygale");
    when(p2.getName()).thenReturn("Vivalahelvig");
    PluginManager pluginManager = mock(PluginManager.class);
    when(glowServer.getMotd()).thenReturn("A Minecraft Server");
    Mockito.doReturn(Arrays.asList(p1, p2)).when(glowServer).getOnlinePlayers();
    when(glowServer.getMaxPlayers()).thenReturn(20);
    when(glowServer.getPort()).thenReturn(25565);
    when(glowServer.getWorlds()).thenReturn(Arrays.asList(world));
    when(glowServer.getIp()).thenReturn("");
    when(glowServer.getVersion()).thenReturn("123");
    when(glowServer.getBukkitVersion()).thenReturn("xyz");
    when(glowServer.getPluginManager()).thenReturn(pluginManager);
    when(pluginManager.getPlugins()).thenReturn(new Plugin[0]);
    QueryHandler handler = new QueryHandler(server, queryPlugins);
    when(random.nextInt()).thenReturn(9513307);
    server.generateChallengeToken(address);
    testChannelRead(handler, FULL_STATS_RECV, FULL_STATS_SEND);
}
Also used : PluginManager(org.bukkit.plugin.PluginManager) QueryHandler(net.glowstone.net.query.QueryHandler) GlowPlayer(net.glowstone.entity.GlowPlayer) World(org.bukkit.World) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 39 with GlowPlayer

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

the class GlowSession method setPlayer.

/**
     * Sets the player associated with this session.
     *
     * @param profile The player's profile with name and UUID information.
     * @throws IllegalStateException if there is already a player associated
     *                               with this session.
     */
public void setPlayer(PlayerProfile profile) {
    if (player != null) {
        throw new IllegalStateException("Cannot set player twice");
    }
    // isActive check here in case player disconnected during authentication
    if (!isActive()) {
        // no need to call onDisconnect() since it only does anything if there's a player set
        return;
    }
    // initialize the player
    PlayerReader reader = server.getPlayerDataService().beginReadingData(profile.getUniqueId());
    player = new GlowPlayer(this, profile, reader);
    finalizeLogin(profile);
    // but before the GlowPlayer initialization was completed
    if (!isActive()) {
        reader.close();
        onDisconnect();
        return;
    }
    // Kick other players with the same UUID
    for (GlowPlayer other : getServer().getRawOnlinePlayers()) {
        if (other != player && other.getUniqueId().equals(player.getUniqueId())) {
            other.getSession().disconnect("You logged in from another location.", true);
            break;
        }
    }
    // login event
    PlayerLoginEvent event = EventFactory.onPlayerLogin(player, hostname);
    if (event.getResult() != Result.ALLOWED) {
        disconnect(event.getKickMessage(), true);
        return;
    }
    //joins the player
    player.join(this, reader);
    player.getWorld().getRawPlayers().add(player);
    online = true;
    GlowServer.logger.info(player.getName() + " [" + address + "] connected, UUID: " + player.getUniqueId());
    // message and user list
    String message = EventFactory.onPlayerJoin(player).getJoinMessage();
    if (message != null && !message.isEmpty()) {
        server.broadcastMessage(message);
    }
    Message addMessage = new UserListItemMessage(Action.ADD_PLAYER, player.getUserListEntry());
    List<Entry> entries = new ArrayList<>();
    for (GlowPlayer other : server.getRawOnlinePlayers()) {
        if (other != player && other.canSee(player)) {
            other.getSession().send(addMessage);
        }
        if (player.canSee(other)) {
            entries.add(other.getUserListEntry());
        }
    }
    send(new UserListItemMessage(Action.ADD_PLAYER, entries));
}
Also used : Entry(net.glowstone.net.message.play.game.UserListItemMessage.Entry) KickMessage(net.glowstone.net.message.KickMessage) Message(com.flowpowered.network.Message) PingMessage(net.glowstone.net.message.play.game.PingMessage) LoginSuccessMessage(net.glowstone.net.message.login.LoginSuccessMessage) DestroyEntitiesMessage(net.glowstone.net.message.play.entity.DestroyEntitiesMessage) SetCompressionMessage(net.glowstone.net.message.SetCompressionMessage) BlockPlacementMessage(net.glowstone.net.message.play.player.BlockPlacementMessage) UserListItemMessage(net.glowstone.net.message.play.game.UserListItemMessage) AsyncableMessage(com.flowpowered.network.AsyncableMessage) PlayerReader(net.glowstone.io.PlayerDataService.PlayerReader) GlowPlayer(net.glowstone.entity.GlowPlayer) PlayerLoginEvent(org.bukkit.event.player.PlayerLoginEvent) UserListItemMessage(net.glowstone.net.message.play.game.UserListItemMessage)

Aggregations

GlowPlayer (net.glowstone.entity.GlowPlayer)39 ItemStack (org.bukkit.inventory.ItemStack)10 GlowBlock (net.glowstone.block.GlowBlock)5 Player (org.bukkit.entity.Player)5 Message (com.flowpowered.network.Message)4 Location (org.bukkit.Location)4 GlowWorld (net.glowstone.GlowWorld)3 ItemType (net.glowstone.block.itemtype.ItemType)3 UserListItemMessage (net.glowstone.net.message.play.game.UserListItemMessage)3 BlockPlacementMessage (net.glowstone.net.message.play.player.BlockPlacementMessage)3 Material (org.bukkit.Material)3 Block (org.bukkit.block.Block)3 BlockFace (org.bukkit.block.BlockFace)3 Vector (org.bukkit.util.Vector)3 ChunkLocation (org.dragonet.ChunkLocation)3 PEPacket (org.dragonet.net.packet.minecraft.PEPacket)3 AsyncableMessage (com.flowpowered.network.AsyncableMessage)2 Message (com.flowpowered.networking.Message)2 ArrayList (java.util.ArrayList)2 BlockType (net.glowstone.block.blocktype.BlockType)2