Skip to content
Merged
37 changes: 37 additions & 0 deletions src/main/java/world/bentobox/bentobox/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ public class Settings implements ConfigObject {
@ConfigEntry(path = "general.fakeplayers", experimental = true)
private Set<String> fakePlayers = new HashSet<>();

@ConfigComment("Flingback power. How far hostile mobs will be flung back when a player teleports into them.")
@ConfigComment("2.5 will push back a number of blocks, 5 will throw them far, 1 will not do much.")
@ConfigEntry(path = "general.flingback")
private double flingback = 2.5D;

@ConfigComment("Kill mobs on teleport. If the world flag in Admin Settings is set, then they will be killed/removed instead of flung.")
@ConfigEntry(path = "general.teleport-remove-mobs")
private boolean teleportRemoveMobs = false;

/* PANELS */
@ConfigComment("Panel click cooldown. Value is in milliseconds. Prevents players spamming button presses in GUIs.")
@ConfigEntry(path = "panel.click-cooldown-ms")
Expand Down Expand Up @@ -1040,6 +1049,34 @@ public void setSafeSpotSearchRange(int safeSpotSearchRange) {
this.safeSpotSearchRange = safeSpotSearchRange;
}

/**
* @return the flingback
*/
public double getFlingback() {
return flingback;
}

/**
* @param flingback the flingback to set
*/
public void setFlingback(double flingback) {
this.flingback = flingback;
}

/**
* @return the teleportRemoveMobs
*/
public boolean isTeleportRemoveMobs() {
return teleportRemoveMobs;
}

/**
* @param teleportRemoveMobs the teleportRemoveMobs to set
*/
public void setTeleportRemoveMobs(boolean teleportRemoveMobs) {
this.teleportRemoveMobs = teleportRemoveMobs;
}

/**
* @return an immutable list of readyCommands
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand All @@ -20,6 +21,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.Tag;
import org.bukkit.World;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -71,6 +73,7 @@ public class IslandsManager {
private static final String SETSPAWN = "bentobox-setspawn";

private final BentoBox plugin;
private final Random rand = new Random();

private final Map<World, Island> spawns = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -1702,7 +1705,30 @@ public void clearArea(Location loc) {
.filter(en -> Util.isHostileEntity(en)
&& !plugin.getIWM().getRemoveMobsWhitelist(loc.getWorld()).contains(en.getType())
&& !(en instanceof PufferFish) && ((LivingEntity) en).getRemoveWhenFarAway())
.filter(en -> en.customName() == null).forEach(Entity::remove);
.filter(en -> en.customName() == null)
.forEach(e -> flingOrKill(e, loc));
}

private void flingOrKill(Entity e, Location loc) {
if (plugin.getSettings().isTeleportRemoveMobs()) {
e.remove();
return;
}
Vector entVec = e.getLocation().toVector();
double dist = plugin.getSettings().getFlingback() - entVec.distance(loc.toVector());
if (dist < 1) {
dist = 1;
}
Vector direction = entVec.subtract(loc.toVector());
if (direction.lengthSquared() < 3) {
// On top of us
direction.add(new Vector(rand.nextDouble(), 0, rand.nextDouble()));
}
// Add a bit of lift
direction.add(new Vector(0, rand.nextDouble(), 0));
direction.multiply(dist);
loc.getWorld().playSound(e, Sound.ENTITY_ILLUSIONER_HURT, 1F, 5F);
e.setVelocity(direction);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ general:
# /!\ This feature is experimental and might not work as expected or might not work at all.
fakeplayers:
- '[CoFH]'
# Flingback power. How far hostile mobs will be flung back when a player teleports into them.
flingback: 5.0
# Remove mobs on teleport.
teleport-remove-mobs: false
panel:
# Panel click cooldown. Value is in milliseconds. Prevents players spamming button presses in GUIs.
click-cooldown-ms: 250
Expand Down
7 changes: 3 additions & 4 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,6 @@ commands:
home-set: '&6 Your [prefix_island] home has been set to your current location.'
homes-are: '&6 [prefix_Island] homes are:'
home-list-syntax: '&6 [name]'
click-to-teleport: '&oClick to teleport'
nether:
not-allowed: '&c You are not allowed to set your home in the Nether.'
confirmation: '&c Are you sure you want to set your home in the Nether?'
Expand Down Expand Up @@ -1600,9 +1599,9 @@ protection:
name: Remove end exit [prefix_island]
REMOVE_MOBS:
description: |-
&a Remove monsters when
&a teleporting to [prefix_island]
name: Remove monsters
&a Push monsters away when
&a teleporting to island
name: Fling back monsters
RIDING:
description: Toggle riding
name: Animal riding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ public String getTitle() {
}

@Override
public String getOriginalTitle() {

public String getOriginalTitle() {
return "";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyDouble;

Check warning on line 9 in src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused import 'org.mockito.ArgumentMatchers.anyDouble'.

See more on https://sonarcloud.io/project/issues?id=BentoBoxWorld_BentoBox&issues=AZ0Tb99oTeeD3ppJ9eWG&open=AZ0Tb99oTeeD3ppJ9eWG&pullRequest=2122
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -33,6 +34,7 @@
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand All @@ -50,6 +52,7 @@
import org.bukkit.entity.Zombie;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -190,6 +193,7 @@
User.setPlugin(plugin);
// Set up user already
when(player.getUniqueId()).thenReturn(uuid);
when(player.getLocation()).thenReturn(location);
User.getInstance(player);

// Locales
Expand Down Expand Up @@ -219,6 +223,8 @@
when(location.clone()).thenReturn(location);
Chunk chunk = mock(Chunk.class);
when(location.getChunk()).thenReturn(chunk);
// Vector
when(location.toVector()).thenReturn(new Vector(100D, 120D, 100D));
when(space1.getRelative(BlockFace.DOWN)).thenReturn(ground);
when(space1.getRelative(BlockFace.UP)).thenReturn(space2);
// A safe spot
Expand Down Expand Up @@ -297,13 +303,17 @@
when(cow.getType()).thenReturn(EntityType.COW);
when(wither.getType()).thenReturn(EntityType.WITHER);
when(wither.getRemoveWhenFarAway()).thenReturn(true);
when(wither.getLocation()).thenReturn(location);
when(creeper.getType()).thenReturn(EntityType.CREEPER);
when(creeper.getRemoveWhenFarAway()).thenReturn(true);
when(creeper.getLocation()).thenReturn(location);
when(pufferfish.getType()).thenReturn(EntityType.PUFFERFISH);
when(pufferfish.getLocation()).thenReturn(location);
// Named monster
when(skelly.getType()).thenReturn(EntityType.SKELETON);
when(skelly.customName()).thenReturn(Component.text("Skelly"));
when(skelly.getRemoveWhenFarAway()).thenReturn(true);
when(skelly.getLocation()).thenReturn(location);

Collection<Entity> collection = new ArrayList<>();
collection.add(player);
Expand Down Expand Up @@ -970,7 +980,8 @@
* {@link world.bentobox.bentobox.managers.IslandsManager#clearArea(Location)}.
*/
@Test
void testClearArea() {
void testClearAreaRemove() {
settings.setTeleportRemoveMobs(true);
islandsManager.clearArea(location);
// Only the correct entities should be cleared
verify(zombie).remove();
Expand All @@ -982,6 +993,34 @@
verify(pufferfish, never()).remove();
verify(skelly, never()).remove();
}

/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#clearArea(Location)}.
*/
@Test
void testClearArea() {
islandsManager.clearArea(location);
// Only the correct entities should be flung
verify(zombie, never()).remove();
verify(player, never()).remove();
verify(cow, never()).remove();
verify(slime, never()).remove();
verify(wither, never()).remove();
verify(creeper, never()).remove();
verify(pufferfish, never()).remove();
verify(skelly, never()).remove();

verify(zombie).setVelocity(any(Vector.class));
verify(slime).setVelocity(any(Vector.class));
verify(creeper).setVelocity(any(Vector.class));
verify(player, never()).setVelocity(any(Vector.class));
verify(cow, never()).setVelocity(any(Vector.class));
verify(wither, never()).setVelocity(any(Vector.class));
verify(pufferfish, never()).setVelocity(any(Vector.class));
verify(skelly, never()).setVelocity(any(Vector.class));

verify(world).playSound(zombie, Sound.ENTITY_ILLUSIONER_HURT, 1F, 5F);
}

/**
* Test method for
Expand Down
Loading