Search in sources :

Example 1 with PlayerExpChangeEvent

use of org.bukkit.event.player.PlayerExpChangeEvent in project Glowstone by GlowstoneMC.

the class GlowPlayer method giveExp.

@Override
public void giveExp(int xp) {
    PlayerExpChangeEvent event = EventFactory.getInstance().callEvent(new PlayerExpChangeEvent(this, xp));
    xp = event.getAmount();
    totalExperience += xp;
    // gradually award levels based on xp points
    float value = 1.0f / getExpToLevel();
    for (int i = 0; i < xp; ++i) {
        exp += value;
        if (exp >= 1) {
            exp -= 1;
            setLevel(level + 1);
            value = 1.0f / getExpToLevel(level);
        }
    }
    sendExperience();
}
Also used : PlayerExpChangeEvent(org.bukkit.event.player.PlayerExpChangeEvent)

Example 2 with PlayerExpChangeEvent

use of org.bukkit.event.player.PlayerExpChangeEvent in project Glowstone by GlowstoneMC.

the class GlowPlayerTest method testGiveExp.

@Test
public void testGiveExp() {
    entity.giveExp(20);
    verify(eventFactory, times(2)).callEvent(argThat(input -> {
        if (input instanceof PlayerExpChangeEvent) {
            PlayerExpChangeEvent event = (PlayerExpChangeEvent) input;
            assertEquals(20, event.getAmount());
            return true;
        } else if (input instanceof PlayerLevelChangeEvent) {
            PlayerLevelChangeEvent event = (PlayerLevelChangeEvent) input;
            assertSame(entity, event.getPlayer());
            assertEquals(2, event.getNewLevel());
            assertEquals(1, event.getOldLevel());
            return true;
        }
        return false;
    }));
    assertEquals(2, entity.getLevel());
    assertEquals(0.17, entity.getExp(), 0.1);
    assertEquals(20, entity.getTotalExperience());
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Statistic(org.bukkit.Statistic) PlayerStatisticIncrementEvent(org.bukkit.event.player.PlayerStatisticIncrementEvent) Event(org.bukkit.event.Event) BlockFace(org.bukkit.block.BlockFace) MaterialValueManager(net.glowstone.block.MaterialValueManager) Location(org.bukkit.Location) PlayerReader(net.glowstone.io.PlayerDataService.PlayerReader) Material(org.bukkit.Material) Bukkit(org.bukkit.Bukkit) GlowScheduler(net.glowstone.scheduler.GlowScheduler) UUID(java.util.UUID) EntityType(org.bukkit.entity.EntityType) ItemStack(org.bukkit.inventory.ItemStack) AdditionalAnswers.returnsFirstArg(org.mockito.AdditionalAnswers.returnsFirstArg) List(java.util.List) PlayerExpChangeEvent(org.bukkit.event.player.PlayerExpChangeEvent) ChunkLock(net.glowstone.chunk.ChunkManager.ChunkLock) UuidListFile(net.glowstone.util.bans.UuidListFile) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Cancellable(org.bukkit.event.Cancellable) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) InventoryUtil(net.glowstone.util.InventoryUtil) Assert.assertSame(org.junit.Assert.assertSame) Answer(org.mockito.stubbing.Answer) ImmutableList(com.google.common.collect.ImmutableList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PlayerStatisticIoService(net.glowstone.io.PlayerStatisticIoService) GlowSession(net.glowstone.net.GlowSession) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockito(org.powermock.api.mockito.PowerMockito) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Nullable(javax.annotation.Nullable) Before(org.junit.Before) RETURNS_SMART_NULLS(org.mockito.Answers.RETURNS_SMART_NULLS) EventFactory(net.glowstone.EventFactory) GlowPlayerProfile(net.glowstone.entity.meta.profile.GlowPlayerProfile) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) File(java.io.File) WorldScheduler(net.glowstone.scheduler.WorldScheduler) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) Assert.assertNull(org.junit.Assert.assertNull) BuiltinMaterialValueManager(net.glowstone.block.BuiltinMaterialValueManager) ChunkManager(net.glowstone.chunk.ChunkManager) PlayerLevelChangeEvent(org.bukkit.event.player.PlayerLevelChangeEvent) SessionRegistry(net.glowstone.net.SessionRegistry) Collections(java.util.Collections) GlowFishingHook(net.glowstone.entity.passive.GlowFishingHook) Assert.assertEquals(org.junit.Assert.assertEquals) PluginManager(org.bukkit.plugin.PluginManager) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PlayerLevelChangeEvent(org.bukkit.event.player.PlayerLevelChangeEvent) PlayerExpChangeEvent(org.bukkit.event.player.PlayerExpChangeEvent) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

PlayerExpChangeEvent (org.bukkit.event.player.PlayerExpChangeEvent)2 ImmutableList (com.google.common.collect.ImmutableList)1 File (java.io.File)1 Collections (java.util.Collections)1 List (java.util.List)1 UUID (java.util.UUID)1 Nullable (javax.annotation.Nullable)1 EventFactory (net.glowstone.EventFactory)1 BuiltinMaterialValueManager (net.glowstone.block.BuiltinMaterialValueManager)1 GlowBlock (net.glowstone.block.GlowBlock)1 MaterialValueManager (net.glowstone.block.MaterialValueManager)1 ChunkManager (net.glowstone.chunk.ChunkManager)1 ChunkLock (net.glowstone.chunk.ChunkManager.ChunkLock)1 GlowPlayerProfile (net.glowstone.entity.meta.profile.GlowPlayerProfile)1 GlowFishingHook (net.glowstone.entity.passive.GlowFishingHook)1 PlayerReader (net.glowstone.io.PlayerDataService.PlayerReader)1 PlayerStatisticIoService (net.glowstone.io.PlayerStatisticIoService)1 GlowSession (net.glowstone.net.GlowSession)1 SessionRegistry (net.glowstone.net.SessionRegistry)1 GlowScheduler (net.glowstone.scheduler.GlowScheduler)1