Skip to content

Commit

Permalink
Merge pull request #75 from TheBusyBiscuit/feature/effects
Browse files Browse the repository at this point in the history
Added Saturation and Potion Effects
  • Loading branch information
seeseemelk committed Jun 13, 2020
2 parents f89a05b + c34da62 commit d952559
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package be.seeseemelk.mockbukkit.entity;

import java.util.concurrent.TimeUnit;

import org.bukkit.potion.PotionEffect;

class ActivePotionEffect
{

private final PotionEffect effect;
private final long timestamp;

ActivePotionEffect(PotionEffect effect)
{
this.effect = effect;
this.timestamp = System.currentTimeMillis();
}

/**
* This returns whether this {@link PotionEffect} has expired.
*
* @return Whether the effect wore off.
*/
public boolean hasExpired()
{
int ticks = effect.getDuration() * 20;
return ticks < 1 || timestamp + TimeUnit.SECONDS.toMillis(ticks) < System.currentTimeMillis();
}

/**
* This method returns the underlying {@link PotionEffect}
*
* @return The actual {@link PotionEffect}.
*/
public PotionEffect getPotionEffect()
{
return effect;
}

}
90 changes: 76 additions & 14 deletions src/main/java/be/seeseemelk/mockbukkit/entity/LivingEntityMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -42,12 +44,15 @@

public abstract class LivingEntityMock extends EntityMock implements LivingEntity
{

private static final double MAX_HEALTH = 20.0;
private double health;
private double maxHealth = MAX_HEALTH;
protected boolean alive = true;
protected Map<Attribute, AttributeInstanceMock> attributes;

private final Set<ActivePotionEffect> activeEffects = new HashSet<>();

public LivingEntityMock(ServerMock server, UUID uuid)
{
super(server, uuid);
Expand Down Expand Up @@ -377,50 +382,107 @@ public Player getKiller()
@Override
public boolean addPotionEffect(PotionEffect effect)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return addPotionEffect(effect, false);
}

@Override
@Deprecated
public boolean addPotionEffect(PotionEffect effect, boolean force)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
if (effect != null)
{
// Bukkit now allows multiple effects of the same type,
// the force/success attributes are now obsolete
activeEffects.add(new ActivePotionEffect(effect));
return true;
}
else
{
return false;
}
}

@Override
public boolean addPotionEffects(Collection<PotionEffect> effects)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
boolean successful = true;

for (PotionEffect effect : effects)
{
if (!addPotionEffect(effect))
{
successful = false;
}
}

return successful;
}

@Override
public boolean hasPotionEffect(PotionEffectType type)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
for (PotionEffect effect : getActivePotionEffects())
{
if (effect.getType().equals(type))
{
return true;
}
}

return false;
}

@Override
public PotionEffect getPotionEffect(PotionEffectType type)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
for (PotionEffect effect : getActivePotionEffects())
{
if (effect.getType().equals(type))
{
return effect;
}
}

return null;
}

@Override
public void removePotionEffect(PotionEffectType type)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
Iterator<ActivePotionEffect> iterator = activeEffects.iterator();

while (iterator.hasNext())
{
ActivePotionEffect effect = iterator.next();

if (effect.hasExpired() || effect.getPotionEffect().getType().equals(type))
{
iterator.remove();
}
}
}

@Override
public Collection<PotionEffect> getActivePotionEffects()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
Set<PotionEffect> effects = new HashSet<>();
Iterator<ActivePotionEffect> iterator = activeEffects.iterator();

while (iterator.hasNext())
{
ActivePotionEffect effect = iterator.next();

if (effect.hasExpired())
{
iterator.remove();
}
else
{
effects.add(effect.getPotionEffect());
}
}

return effects;
}

@Override
Expand Down
65 changes: 7 additions & 58 deletions src/main/java/be/seeseemelk/mockbukkit/entity/PlayerMock.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package be.seeseemelk.mockbukkit.entity;

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

import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -66,8 +66,6 @@
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.map.MapView;
import org.bukkit.plugin.Plugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.RayTraceResult;
Expand All @@ -94,6 +92,7 @@ public class PlayerMock extends LivingEntityMock implements Player
private int expTotal = 0;
private float exp = 0;
private int foodLevel = 20;
private float saturation = 5.0F;
private int expLevel = 0;
private boolean sneaking = false;
private boolean whitelisted = true;
Expand Down Expand Up @@ -144,8 +143,8 @@ public int hashCode()
final int prime = 31;
int result = super.hashCode();
result = prime * result + Objects.hash(attributes, exp, expLevel, expTotal, displayName, gamemode, getHealth(),
inventory, enderChest, inventoryView, getMaxHealth(), online, whitelisted, compassTarget,
bedSpawnLocation, cursor);
foodLevel, saturation, inventory, enderChest, inventoryView, getMaxHealth(), online, whitelisted,
compassTarget, bedSpawnLocation, cursor);
return result;
}

Expand Down Expand Up @@ -636,55 +635,6 @@ public Player getKiller()
throw new UnimplementedOperationException();
}

@Override
public boolean addPotionEffect(PotionEffect effect)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
}

@Override
public boolean addPotionEffect(PotionEffect effect, boolean force)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
}

@Override
public boolean addPotionEffects(Collection<PotionEffect> effects)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
}

@Override
public boolean hasPotionEffect(PotionEffectType type)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
}

@Override
public PotionEffect getPotionEffect(PotionEffectType type)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
}

@Override
public void removePotionEffect(PotionEffectType type)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
}

@Override
public Collection<PotionEffect> getActivePotionEffects()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
}

@Override
public boolean hasLineOfSight(Entity other)
{
Expand Down Expand Up @@ -1430,15 +1380,14 @@ public void setExhaustion(float value)
@Override
public float getSaturation()
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
return saturation;
}

@Override
public void setSaturation(float value)
{
// TODO Auto-generated method stub
throw new UnimplementedOperationException();
// Saturation is constrained by the current food level
this.saturation = Math.min(getFoodLevel(), value);
}

@Override
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/be/seeseemelk/mockbukkit/entity/PlayerMockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;

import java.util.Arrays;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -42,6 +44,8 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -870,4 +874,58 @@ public void testCloseInventoryEvenFired()
assertNull(player.getItemOnCursor());
}

@Test
public void testSaturation()
{
// Default level
assertEquals(5.0F, player.getSaturation(), 0.1F);

player.setFoodLevel(20);
player.setSaturation(8);
assertEquals(8.0F, player.getSaturation(), 0.1F);

// Testing the constraint
player.setFoodLevel(20);
player.setSaturation(10000);
assertEquals(20.0F, player.getSaturation(), 0.1F);
}

@Test
public void testPotionEffects()
{
PotionEffect effect = new PotionEffect(PotionEffectType.CONFUSION, 3, 1);
assertTrue(player.addPotionEffect(effect));

assertTrue(player.hasPotionEffect(effect.getType()));
assertTrue(player.getActivePotionEffects().contains(effect));

assertEquals(effect, player.getPotionEffect(effect.getType()));

player.removePotionEffect(effect.getType());
assertFalse(player.hasPotionEffect(effect.getType()));
assertFalse(player.getActivePotionEffects().contains(effect));

}

@Test
public void testInstantEffect()
{
PotionEffect instant = new PotionEffect(PotionEffectType.HEAL, 0, 1);
assertTrue(player.addPotionEffect(instant));
assertFalse(player.hasPotionEffect(instant.getType()));
}

@Test
public void testMultiplePotionEffects()
{
Collection<PotionEffect> effects = Arrays.asList(new PotionEffect(PotionEffectType.BAD_OMEN, 3, 1),
new PotionEffect(PotionEffectType.LUCK, 5, 2));

assertTrue(player.addPotionEffects(effects));

for (PotionEffect effect : effects)
{
assertTrue(player.hasPotionEffect(effect.getType()));
}
}
}

0 comments on commit d952559

Please sign in to comment.