Skip to content

Commit

Permalink
Merge pull request #69 from TheBusyBiscuit/feature/assert-hear
Browse files Browse the repository at this point in the history
Added Sound Assertions for PlayerMock
  • Loading branch information
seeseemelk committed May 20, 2020
2 parents 431f3a3 + 272da05 commit c9f97a7
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 188 deletions.
81 changes: 81 additions & 0 deletions src/main/java/be/seeseemelk/mockbukkit/entity/AudioExperience.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package be.seeseemelk.mockbukkit.entity;

import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.SoundCategory;
import org.bukkit.entity.Player;

/**
* This class represents a {@link Sound} that was heard by a {@link Player}.
*
* @author TheBusyBiscuit
*
*/
public final class AudioExperience
{
private final Sound sound;
private final SoundCategory category;
private final Location location;
private final float volume;
private final float pitch;

AudioExperience(Sound sound, SoundCategory category, Location l, float volume, float pitch)
{
this.sound = sound;
this.category = category;
this.location = l;
this.volume = volume;
this.pitch = pitch;
}

/**
* This returns the {@link Sound} that was played.
*
* @return The {@link Sound}
*/
public Sound getSound()
{
return sound;
}

/**
* This method returns the {@link SoundCategory} with which the {@link Sound} was played.
*
* @return The {@link SoundCategory}
*/
public SoundCategory getCategory()
{
return category;
}

/**
* This returns the {@link Location} at which this {@link Sound} was played.
*
* @return The {@link Location}
*/
public Location getLocation()
{
return location;
}

/**
* The volume of this {@link Sound}
*
* @return The volume
*/
public float getVolume()
{
return volume;
}

/**
* The pitch of this {@link Sound}
*
* @return The pitch
*/
public float getPitch()
{
return pitch;
}

}
50 changes: 28 additions & 22 deletions src/main/java/be/seeseemelk/mockbukkit/entity/LivingEntityMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public LivingEntityMock(ServerMock server, UUID uuid)
super(server, uuid);

attributes = new EnumMap<>(Attribute.class);
attributes.put(Attribute.GENERIC_MAX_HEALTH,
new AttributeInstanceMock(Attribute.GENERIC_MAX_HEALTH, 20));
attributes.put(Attribute.GENERIC_MAX_HEALTH, new AttributeInstanceMock(Attribute.GENERIC_MAX_HEALTH, 20));
this.setMaxHealth(MAX_HEALTH);
this.setHealth(MAX_HEALTH);
}
Expand All @@ -64,11 +63,11 @@ public double getHealth()
{
return health;
}

@Override
public boolean isDead()
public boolean isDead()
{
return !alive;
return !alive;
}

@Override
Expand All @@ -78,30 +77,34 @@ public void setHealth(double health)
{
this.health = 0;

if (this instanceof Player) {
Player player = (Player) this;
List<ItemStack> drops = Arrays.asList(player.getInventory().getContents());
if (this instanceof Player)
{
Player player = (Player) this;
List<ItemStack> drops = new ArrayList<>();
drops.addAll(Arrays.asList(player.getInventory().getContents()));
PlayerDeathEvent event = new PlayerDeathEvent(player, drops, 0, getName() + " got killed");
Bukkit.getPluginManager().callEvent(event);

// Terminate any InventoryView and the cursor item
player.closeInventory();

// Clear the Inventory if keep-inventory is not enabled
if (!getWorld().getGameRuleValue(GameRule.KEEP_INVENTORY).booleanValue()) {
player.getInventory().clear();
// Should someone try to provoke a RespawnEvent, they will now find the Inventory to be empty
if (!getWorld().getGameRuleValue(GameRule.KEEP_INVENTORY).booleanValue())
{
player.getInventory().clear();
// Should someone try to provoke a RespawnEvent, they will now find the Inventory to be empty
}

player.setLevel(0);
player.setExp(0);
player.setFoodLevel(0);
} else {
EntityDeathEvent event = new EntityDeathEvent(this, new ArrayList<>(), 0);
}
else
{
EntityDeathEvent event = new EntityDeathEvent(this, new ArrayList<>(), 0);
Bukkit.getPluginManager().callEvent(event);
}



alive = false;
}
else
Expand Down Expand Up @@ -138,10 +141,12 @@ public void damage(double amount)
{
Map<EntityDamageEvent.DamageModifier, Double> modifiers = new EnumMap<>(EntityDamageEvent.DamageModifier.class);
modifiers.put(EntityDamageEvent.DamageModifier.BASE, 1.0);
Map<EntityDamageEvent.DamageModifier, Function<Double, Double>> modifierFunctions = new EnumMap<>(EntityDamageEvent.DamageModifier.class);
Map<EntityDamageEvent.DamageModifier, Function<Double, Double>> modifierFunctions = new EnumMap<>(
EntityDamageEvent.DamageModifier.class);
modifierFunctions.put(EntityDamageEvent.DamageModifier.BASE, damage -> damage);

EntityDamageEvent event = new EntityDamageEvent(this, EntityDamageEvent.DamageCause.CUSTOM, modifiers, modifierFunctions);
EntityDamageEvent event = new EntityDamageEvent(this, EntityDamageEvent.DamageCause.CUSTOM, modifiers,
modifierFunctions);
event.setDamage(amount);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled())
Expand All @@ -157,11 +162,12 @@ public void damage(double amount, Entity source)
{
Map<EntityDamageEvent.DamageModifier, Double> modifiers = new EnumMap<>(EntityDamageEvent.DamageModifier.class);
modifiers.put(EntityDamageEvent.DamageModifier.BASE, 1.0);
Map<EntityDamageEvent.DamageModifier, Function<Double, Double>> modifierFunctions = new EnumMap<>(EntityDamageEvent.DamageModifier.class);
Map<EntityDamageEvent.DamageModifier, Function<Double, Double>> modifierFunctions = new EnumMap<>(
EntityDamageEvent.DamageModifier.class);
modifierFunctions.put(EntityDamageEvent.DamageModifier.BASE, damage -> damage);

EntityDamageByEntityEvent event = new EntityDamageByEntityEvent(source, this, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
modifiers, modifierFunctions);
EntityDamageByEntityEvent event = new EntityDamageByEntityEvent(source, this,
EntityDamageEvent.DamageCause.ENTITY_ATTACK, modifiers, modifierFunctions);
event.setDamage(amount);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled())
Expand Down
56 changes: 45 additions & 11 deletions src/main/java/be/seeseemelk/mockbukkit/entity/PlayerMock.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package be.seeseemelk.mockbukkit.entity;

import static org.junit.Assert.fail;
import static org.junit.Assert.assertEquals;

import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;

import org.bukkit.BanList;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -47,6 +50,7 @@
import org.bukkit.entity.memory.MemoryKey;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerChatEvent;
Expand Down Expand Up @@ -99,6 +103,8 @@ public class PlayerMock extends LivingEntityMock implements Player
private Location bedSpawnLocation;
private ItemStack cursor = null;

private final List<AudioExperience> heardSounds = new LinkedList<>();

public PlayerMock(ServerMock server, String name)
{
this(server, name, UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(StandardCharsets.UTF_8)));
Expand Down Expand Up @@ -322,23 +328,27 @@ public InventoryView getOpenInventory()
@Override
public void openInventory(InventoryView inventory)
{
// reset the cursor as it is a new InventoryView
cursor = null;
closeInventory();
inventoryView = inventory;
}

@Override
public InventoryView openInventory(Inventory inventory)
{
// reset the cursor as it is a new InventoryView
cursor = null;
closeInventory();
inventoryView = new PlayerInventoryViewMock(this, inventory);
return inventoryView;
}

@Override
public void closeInventory()
{
if (inventoryView instanceof PlayerInventoryViewMock)
{
InventoryCloseEvent event = new InventoryCloseEvent(inventoryView);
Bukkit.getPluginManager().callEvent(event);
}

// reset the cursor as it is a new InventoryView
cursor = null;
inventoryView = new SimpleInventoryViewMock(this, null, inventory, InventoryType.CRAFTING);
Expand Down Expand Up @@ -1037,16 +1047,41 @@ public void playSound(Location location, String sound, SoundCategory category, f
@Override
public void playSound(Location location, Sound sound, SoundCategory category, float volume, float pitch)
{
// We could send a packet here in case some wants to test that?
// But really I don't think this method should do much at all.
// Perhaps we could add an assertSound(...) method in the future?
heardSounds.add(new AudioExperience(sound, category, location, volume, pitch));
}

public void assertSoundHeard(String message, Sound sound)
{
assertSoundHeard(message, sound, e -> true);
}

public void assertSoundHeard(String message, Sound sound, Predicate<AudioExperience> predicate)
{
for (AudioExperience audio : heardSounds)
{
if (audio.getSound() == sound && predicate.test(audio))
{
return;
}
}

fail(message);
}

public void assertSoundHeard(Sound sound)
{
assertSoundHeard("Sound Heard Assertion failed", sound);
}

public void assertSoundHeard(Sound sound, Predicate<AudioExperience> predicate)
{
assertSoundHeard("Sound Heard Assertion failed", sound, predicate);
}

@Override
public void stopSound(Sound sound)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
stopSound(sound, SoundCategory.MASTER);
}

@Override
Expand All @@ -1059,8 +1094,7 @@ public void stopSound(String sound)
@Override
public void stopSound(Sound sound, SoundCategory category)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
// We just pretend the Sound has stopped.
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public PotionMetaMock clone()
{
PotionMetaMock mock = (PotionMetaMock) super.clone();
mock.effects = new ArrayList<>(effects);
mock.color = Color.fromRGB(color.asRGB());
mock.color = color == null ? null : Color.fromRGB(color.asRGB());
return mock;
}

Expand Down

0 comments on commit c9f97a7

Please sign in to comment.