Skip to content

Commit 5043288

Browse files
authored
Merge pull request #2122 from BentoBoxWorld/forcefieldteleport
Force field fling on teleport
2 parents b529fe4 + e947069 commit 5043288

File tree

6 files changed

+112
-8
lines changed

6 files changed

+112
-8
lines changed

src/main/java/world/bentobox/bentobox/Settings.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ public class Settings implements ConfigObject {
143143
@ConfigEntry(path = "general.fakeplayers", experimental = true)
144144
private Set<String> fakePlayers = new HashSet<>();
145145

146+
@ConfigComment("Flingback power. How far hostile mobs will be flung back when a player teleports into them.")
147+
@ConfigComment("2.5 will push back a number of blocks, 5 will throw them far, 1 will not do much.")
148+
@ConfigEntry(path = "general.flingback")
149+
private double flingback = 2.5D;
150+
151+
@ConfigComment("Kill mobs on teleport. If the world flag in Admin Settings is set, then they will be killed/removed instead of flung.")
152+
@ConfigEntry(path = "general.teleport-remove-mobs")
153+
private boolean teleportRemoveMobs = false;
154+
146155
/* PANELS */
147156
@ConfigComment("Panel click cooldown. Value is in milliseconds. Prevents players spamming button presses in GUIs.")
148157
@ConfigEntry(path = "panel.click-cooldown-ms")
@@ -1040,6 +1049,34 @@ public void setSafeSpotSearchRange(int safeSpotSearchRange) {
10401049
this.safeSpotSearchRange = safeSpotSearchRange;
10411050
}
10421051

1052+
/**
1053+
* @return the flingback
1054+
*/
1055+
public double getFlingback() {
1056+
return flingback;
1057+
}
1058+
1059+
/**
1060+
* @param flingback the flingback to set
1061+
*/
1062+
public void setFlingback(double flingback) {
1063+
this.flingback = flingback;
1064+
}
1065+
1066+
/**
1067+
* @return the teleportRemoveMobs
1068+
*/
1069+
public boolean isTeleportRemoveMobs() {
1070+
return teleportRemoveMobs;
1071+
}
1072+
1073+
/**
1074+
* @param teleportRemoveMobs the teleportRemoveMobs to set
1075+
*/
1076+
public void setTeleportRemoveMobs(boolean teleportRemoveMobs) {
1077+
this.teleportRemoveMobs = teleportRemoveMobs;
1078+
}
1079+
10431080
/**
10441081
* @return an immutable list of readyCommands
10451082
*/

src/main/java/world/bentobox/bentobox/managers/IslandsManager.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Objects;
1212
import java.util.Optional;
1313
import java.util.Queue;
14+
import java.util.Random;
1415
import java.util.Set;
1516
import java.util.UUID;
1617
import java.util.concurrent.CompletableFuture;
@@ -20,6 +21,7 @@
2021
import org.bukkit.Bukkit;
2122
import org.bukkit.Location;
2223
import org.bukkit.Material;
24+
import org.bukkit.Sound;
2325
import org.bukkit.Tag;
2426
import org.bukkit.World;
2527
import org.bukkit.block.Block;
@@ -71,6 +73,7 @@ public class IslandsManager {
7173
private static final String SETSPAWN = "bentobox-setspawn";
7274

7375
private final BentoBox plugin;
76+
private final Random rand = new Random();
7477

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

@@ -1702,7 +1705,30 @@ public void clearArea(Location loc) {
17021705
.filter(en -> Util.isHostileEntity(en)
17031706
&& !plugin.getIWM().getRemoveMobsWhitelist(loc.getWorld()).contains(en.getType())
17041707
&& !(en instanceof PufferFish) && ((LivingEntity) en).getRemoveWhenFarAway())
1705-
.filter(en -> en.customName() == null).forEach(Entity::remove);
1708+
.filter(en -> en.customName() == null)
1709+
.forEach(e -> flingOrKill(e, loc));
1710+
}
1711+
1712+
private void flingOrKill(Entity e, Location loc) {
1713+
if (plugin.getSettings().isTeleportRemoveMobs()) {
1714+
e.remove();
1715+
return;
1716+
}
1717+
Vector entVec = e.getLocation().toVector();
1718+
double dist = plugin.getSettings().getFlingback() - entVec.distance(loc.toVector());
1719+
if (dist < 1) {
1720+
dist = 1;
1721+
}
1722+
Vector direction = entVec.subtract(loc.toVector());
1723+
if (direction.lengthSquared() < 3) {
1724+
// On top of us
1725+
direction.add(new Vector(rand.nextDouble(), 0, rand.nextDouble()));
1726+
}
1727+
// Add a bit of lift
1728+
direction.add(new Vector(0, rand.nextDouble(), 0));
1729+
direction.multiply(dist);
1730+
loc.getWorld().playSound(e, Sound.ENTITY_ILLUSIONER_HURT, 1F, 5F);
1731+
e.setVelocity(direction);
17061732
}
17071733

17081734
/**

src/main/resources/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ general:
8686
# /!\ This feature is experimental and might not work as expected or might not work at all.
8787
fakeplayers:
8888
- '[CoFH]'
89+
# Flingback power. How far hostile mobs will be flung back when a player teleports into them.
90+
flingback: 5.0
91+
# Remove mobs on teleport.
92+
teleport-remove-mobs: false
8993
panel:
9094
# Panel click cooldown. Value is in milliseconds. Prevents players spamming button presses in GUIs.
9195
click-cooldown-ms: 250

src/main/resources/locales/en-US.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,6 @@ commands:
704704
home-set: '&6 Your [prefix_island] home has been set to your current location.'
705705
homes-are: '&6 [prefix_Island] homes are:'
706706
home-list-syntax: '&6 [name]'
707-
click-to-teleport: '&oClick to teleport'
708707
nether:
709708
not-allowed: '&c You are not allowed to set your home in the Nether.'
710709
confirmation: '&c Are you sure you want to set your home in the Nether?'
@@ -1600,9 +1599,9 @@ protection:
16001599
name: Remove end exit [prefix_island]
16011600
REMOVE_MOBS:
16021601
description: |-
1603-
&a Remove monsters when
1604-
&a teleporting to [prefix_island]
1605-
name: Remove monsters
1602+
&a Push monsters away when
1603+
&a teleporting to island
1604+
name: Fling back monsters
16061605
RIDING:
16071606
description: Toggle riding
16081607
name: Animal riding

src/test/java/world/bentobox/bentobox/listeners/PanelListenerManagerTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ public String getTitle() {
165165
}
166166

167167
@Override
168-
public String getOriginalTitle() {
169-
168+
public String getOriginalTitle() {
170169
return "";
171170
}
172171

src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.junit.jupiter.api.Assertions.assertNull;
77
import static org.junit.jupiter.api.Assertions.assertTrue;
88
import static org.mockito.ArgumentMatchers.any;
9+
import static org.mockito.ArgumentMatchers.anyDouble;
910
import static org.mockito.ArgumentMatchers.anyString;
1011
import static org.mockito.ArgumentMatchers.eq;
1112
import static org.mockito.Mockito.mock;
@@ -33,6 +34,7 @@
3334
import org.bukkit.Chunk;
3435
import org.bukkit.Location;
3536
import org.bukkit.Material;
37+
import org.bukkit.Sound;
3638
import org.bukkit.World;
3739
import org.bukkit.block.Block;
3840
import org.bukkit.block.BlockFace;
@@ -50,6 +52,7 @@
5052
import org.bukkit.entity.Zombie;
5153
import org.bukkit.permissions.PermissionAttachmentInfo;
5254
import org.bukkit.scheduler.BukkitScheduler;
55+
import org.bukkit.util.Vector;
5356
import org.eclipse.jdt.annotation.NonNull;
5457
import org.eclipse.jdt.annotation.Nullable;
5558
import org.junit.jupiter.api.AfterEach;
@@ -190,6 +193,7 @@ public void setUp() throws Exception {
190193
User.setPlugin(plugin);
191194
// Set up user already
192195
when(player.getUniqueId()).thenReturn(uuid);
196+
when(player.getLocation()).thenReturn(location);
193197
User.getInstance(player);
194198

195199
// Locales
@@ -219,6 +223,8 @@ public void setUp() throws Exception {
219223
when(location.clone()).thenReturn(location);
220224
Chunk chunk = mock(Chunk.class);
221225
when(location.getChunk()).thenReturn(chunk);
226+
// Vector
227+
when(location.toVector()).thenReturn(new Vector(100D, 120D, 100D));
222228
when(space1.getRelative(BlockFace.DOWN)).thenReturn(ground);
223229
when(space1.getRelative(BlockFace.UP)).thenReturn(space2);
224230
// A safe spot
@@ -297,13 +303,17 @@ public void setUp() throws Exception {
297303
when(cow.getType()).thenReturn(EntityType.COW);
298304
when(wither.getType()).thenReturn(EntityType.WITHER);
299305
when(wither.getRemoveWhenFarAway()).thenReturn(true);
306+
when(wither.getLocation()).thenReturn(location);
300307
when(creeper.getType()).thenReturn(EntityType.CREEPER);
301308
when(creeper.getRemoveWhenFarAway()).thenReturn(true);
309+
when(creeper.getLocation()).thenReturn(location);
302310
when(pufferfish.getType()).thenReturn(EntityType.PUFFERFISH);
311+
when(pufferfish.getLocation()).thenReturn(location);
303312
// Named monster
304313
when(skelly.getType()).thenReturn(EntityType.SKELETON);
305314
when(skelly.customName()).thenReturn(Component.text("Skelly"));
306315
when(skelly.getRemoveWhenFarAway()).thenReturn(true);
316+
when(skelly.getLocation()).thenReturn(location);
307317

308318
Collection<Entity> collection = new ArrayList<>();
309319
collection.add(player);
@@ -970,7 +980,8 @@ void testClearAreaWrongWorld() {
970980
* {@link world.bentobox.bentobox.managers.IslandsManager#clearArea(Location)}.
971981
*/
972982
@Test
973-
void testClearArea() {
983+
void testClearAreaRemove() {
984+
settings.setTeleportRemoveMobs(true);
974985
islandsManager.clearArea(location);
975986
// Only the correct entities should be cleared
976987
verify(zombie).remove();
@@ -982,6 +993,34 @@ void testClearArea() {
982993
verify(pufferfish, never()).remove();
983994
verify(skelly, never()).remove();
984995
}
996+
997+
/**
998+
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#clearArea(Location)}.
999+
*/
1000+
@Test
1001+
void testClearArea() {
1002+
islandsManager.clearArea(location);
1003+
// Only the correct entities should be flung
1004+
verify(zombie, never()).remove();
1005+
verify(player, never()).remove();
1006+
verify(cow, never()).remove();
1007+
verify(slime, never()).remove();
1008+
verify(wither, never()).remove();
1009+
verify(creeper, never()).remove();
1010+
verify(pufferfish, never()).remove();
1011+
verify(skelly, never()).remove();
1012+
1013+
verify(zombie).setVelocity(any(Vector.class));
1014+
verify(slime).setVelocity(any(Vector.class));
1015+
verify(creeper).setVelocity(any(Vector.class));
1016+
verify(player, never()).setVelocity(any(Vector.class));
1017+
verify(cow, never()).setVelocity(any(Vector.class));
1018+
verify(wither, never()).setVelocity(any(Vector.class));
1019+
verify(pufferfish, never()).setVelocity(any(Vector.class));
1020+
verify(skelly, never()).setVelocity(any(Vector.class));
1021+
1022+
verify(world).playSound(zombie, Sound.ENTITY_ILLUSIONER_HURT, 1F, 5F);
1023+
}
9851024

9861025
/**
9871026
* Test method for

0 commit comments

Comments
 (0)