Various changes to SafeSpotTeleport

* Replaced SafeTeleportBuilder by SafeSpotTeleport.Builder
* Added "overrideGamemode" parameter
* AdminTeleportCommand no longer overrides the player's gamemode when teleporting (fixes #262 )
This commit is contained in:
Florian CUNY 2018-09-15 12:38:51 +02:00
parent 5db4866cb6
commit 58de346dc9
8 changed files with 172 additions and 169 deletions

View File

@ -11,7 +11,7 @@ import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
import world.bentobox.bentobox.util.teleport.SafeTeleportBuilder;
import world.bentobox.bentobox.util.teleport.SafeSpotTeleport;
public class AdminTeleportCommand extends CompositeCommand {
@ -51,10 +51,13 @@ public class AdminTeleportCommand extends CompositeCommand {
// Other wise, go to a safe spot
String failureMessage = user.getTranslation("commands.admin.tp.manual", "[location]", warpSpot.getBlockX() + " " + warpSpot.getBlockY() + " "
+ warpSpot.getBlockZ());
new SafeTeleportBuilder(getPlugin()).entity(user.getPlayer())
.location(warpSpot)
.failureMessage(failureMessage)
.build();
new SafeSpotTeleport.Builder(getPlugin())
.entity(user.getPlayer())
.location(warpSpot)
.failureMessage(failureMessage)
.overrideGamemode(false)
.build();
return true;
}
user.sendMessage("general.errors.player-has-no-island");

View File

@ -24,7 +24,7 @@ import org.bukkit.util.Vector;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
import world.bentobox.bentobox.util.teleport.SafeTeleportBuilder;
import world.bentobox.bentobox.util.teleport.SafeSpotTeleport;
public class NetherPortals implements Listener {
private static final String ERROR_NO_PERMISSION = "general.errors.no-permission";
@ -120,7 +120,7 @@ public class NetherPortals implements Listener {
// End exists and end islands are being used
Location to = plugin.getIslands().getIslandAt(e.getFrom()).map(i -> i.getSpawnPoint(Environment.THE_END)).orElse(e.getFrom().toVector().toLocation(endWorld));
e.setCancelled(true);
new SafeTeleportBuilder(plugin)
new SafeSpotTeleport.Builder(plugin)
.entity(e.getPlayer())
.location(to)
.build();
@ -183,7 +183,7 @@ public class NetherPortals implements Listener {
e.setCancelled(true);
// Else other worlds teleport to the nether
new SafeTeleportBuilder(plugin)
new SafeSpotTeleport.Builder(plugin)
.entity(e.getPlayer())
.location(to)
.portal()
@ -197,7 +197,7 @@ public class NetherPortals implements Listener {
: nether.getSpawnLocation();
e.setCancelled(true);
// Else other worlds teleport to the nether
new SafeTeleportBuilder(plugin)
new SafeSpotTeleport.Builder(plugin)
.entity(e.getPlayer())
.location(to)
.portal()

View File

@ -24,7 +24,7 @@ import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
import world.bentobox.bentobox.util.teleport.SafeTeleportBuilder;
import world.bentobox.bentobox.util.teleport.SafeSpotTeleport;
/**
* Listener for invincible visitor settings. Handles click listening and damage events
@ -111,7 +111,7 @@ public class InvincibleVisitorsListener extends AbstractFlagListener implements
if(e.getCause().equals(DamageCause.VOID)) {
// Will be set back after the teleport
p.setGameMode(GameMode.SPECTATOR);
getIslands().getIslandAt(p.getLocation()).ifPresent(i -> new SafeTeleportBuilder(getPlugin()).entity(p).island(i).build());
getIslands().getIslandAt(p.getLocation()).ifPresent(i -> new SafeSpotTeleport.Builder(getPlugin()).entity(p).island(i).build());
}
}
}

View File

@ -32,7 +32,7 @@ import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.island.IslandCache;
import world.bentobox.bentobox.util.DeleteIslandChunks;
import world.bentobox.bentobox.util.Util;
import world.bentobox.bentobox.util.teleport.SafeTeleportBuilder;
import world.bentobox.bentobox.util.teleport.SafeSpotTeleport;
/**
* The job of this class is manage all island related data.
@ -541,10 +541,11 @@ public class IslandsManager {
}
if (home == null) {
// Try to fix this teleport location and teleport the player if possible
new SafeTeleportBuilder(plugin).entity(player)
.island(plugin.getIslands().getIsland(world, user))
.homeNumber(number)
.build();
new SafeSpotTeleport.Builder(plugin)
.entity(player)
.island(plugin.getIslands().getIsland(world, user))
.homeNumber(number)
.build();
return;
}
player.teleport(home);

View File

@ -39,6 +39,7 @@ public class SafeSpotTeleport {
private final Location location;
private boolean portal;
private final int homeNumber;
private final boolean overrideGamemode;
// Locations
private Location bestSpot;
@ -55,16 +56,16 @@ public class SafeSpotTeleport {
* @param portal - true if this is a portal teleport
* @param homeNumber - home number to go to
*/
protected SafeSpotTeleport(BentoBox plugin, final Entity entity, final Location location, final String failureMessage, boolean portal,
int homeNumber) {
public SafeSpotTeleport(BentoBox plugin, final Entity entity, final Location location, final String failureMessage, boolean portal, int homeNumber, boolean overrideGamemode) {
this.plugin = plugin;
this.entity = entity;
this.location = location;
this.portal = portal;
this.homeNumber = homeNumber;
this.overrideGamemode = overrideGamemode;
// Put player into spectator mode
if (entity instanceof Player && ((Player)entity).getGameMode().equals(GameMode.SURVIVAL)) {
if (overrideGamemode && entity instanceof Player && ((Player)entity).getGameMode().equals(GameMode.SURVIVAL)) {
((Player)entity).setGameMode(GameMode.SPECTATOR);
}
@ -116,13 +117,13 @@ public class SafeSpotTeleport {
if (portal && bestSpot != null) {
// No portals found, teleport to the best spot we found
teleportEntity(bestSpot);
if (entity instanceof Player && ((Player)entity).getGameMode().equals(GameMode.SPECTATOR)) {
if (overrideGamemode && entity instanceof Player && ((Player)entity).getGameMode().equals(GameMode.SPECTATOR)) {
((Player)entity).setGameMode(plugin.getIWM().getDefaultGameMode(bestSpot.getWorld()));
}
} else if (entity instanceof Player && !failureMessage.isEmpty()) {
// Failed, no safe spot
entity.sendMessage(failureMessage);
if (((Player)entity).getGameMode().equals(GameMode.SPECTATOR)) {
if (overrideGamemode && ((Player)entity).getGameMode().equals(GameMode.SPECTATOR)) {
if (plugin.getIWM().inWorld(entity.getLocation())) {
((Player)entity).setGameMode(plugin.getIWM().getDefaultGameMode(entity.getWorld()));
} else {
@ -233,7 +234,7 @@ public class SafeSpotTeleport {
// Exit spectator mode if in it
if (entity instanceof Player) {
Player player = (Player)entity;
if (player.getGameMode().equals(GameMode.SPECTATOR)) {
if (overrideGamemode && player.getGameMode().equals(GameMode.SPECTATOR)) {
player.setGameMode(plugin.getIWM().getDefaultGameMode(loc.getWorld()));
}
} else {
@ -309,4 +310,107 @@ public class SafeSpotTeleport {
return true;
}
}
public static class Builder {
private BentoBox plugin;
private Entity entity;
private int homeNumber = 0;
private boolean portal = false;
private String failureMessage = "";
private Location location;
private boolean overrideGamemode = true;
public Builder(BentoBox plugin) {
this.plugin = plugin;
}
/**
* Set who or what is going to teleport
* @param entity entity to teleport
* @return Builder
*/
public Builder entity(Entity entity) {
this.entity = entity;
return this;
}
/**
* Set the island to teleport to
* @param island island destination
* @return Builder
*/
public Builder island(Island island) {
this.location = island.getCenter();
return this;
}
/**
* Set the home number to this number
* @param homeNumber home number
* @return Builder
*/
public Builder homeNumber(int homeNumber) {
this.homeNumber = homeNumber;
return this;
}
/**
* This is a portal teleportation
* @return Builder
*/
public Builder portal() {
this.portal = true;
return this;
}
/**
* Set the failure message if this teleport cannot happen
* @param failureMessage failure message to report to user
* @return Builder
*/
public Builder failureMessage(String failureMessage) {
this.failureMessage = failureMessage;
return this;
}
/**
* Set the desired location
* @param location the location
* @return Builder
*/
public Builder location(Location location) {
this.location = location;
return this;
}
/**
* Sets whether the player's gamemode should be overridden.
* @param overrideGamemode whether the player's gamemode should be overridden.
* @return Builder
*/
public Builder overrideGamemode(boolean overrideGamemode) {
this.overrideGamemode = overrideGamemode;
return this;
}
/**
* Try to teleport the player
* @return SafeSpotTeleport
*/
public SafeSpotTeleport build() {
// Error checking
if (entity == null) {
plugin.logError("Attempt to safe teleport a null entity!");
return null;
}
if (location == null) {
plugin.logError("Attempt to safe teleport to a null location!");
return null;
}
if (failureMessage.isEmpty() && entity instanceof Player) {
failureMessage = User.getInstance(entity).getTranslation("general.errors.warp-not-safe");
}
return new SafeSpotTeleport(plugin, entity, location, failureMessage, portal, homeNumber, overrideGamemode);
}
}
}

View File

@ -1,104 +0,0 @@
package world.bentobox.bentobox.util.teleport;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
public class SafeTeleportBuilder {
private BentoBox plugin;
private Entity entity;
private int homeNumber = 0;
private boolean portal = false;
private String failureMessage = "";
private Location location;
public SafeTeleportBuilder(BentoBox plugin) {
this.plugin = plugin;
}
/**
* Set who or what is going to teleport
* @param entity - entity to teleport
* @return SafeTeleportBuilder
*/
public SafeTeleportBuilder entity(Entity entity) {
this.entity = entity;
return this;
}
/**
* Set the island to teleport to
* @param island - island destination
* @return SafeTeleportBuilder
*/
public SafeTeleportBuilder island(Island island) {
this.location = island.getCenter();
return this;
}
/**
* Set the home number to this number
* @param homeNumber - home number
* @return SafeTeleportBuilder
*/
public SafeTeleportBuilder homeNumber(int homeNumber) {
this.homeNumber = homeNumber;
return this;
}
/**
* This is a portal teleportation
* @return SafeTeleportBuilder
*/
public SafeTeleportBuilder portal() {
this.portal = true;
return this;
}
/**
* Set the failure message if this teleport cannot happen
* @param failureMessage - failure message to report to user
* @return SafeTeleportBuilder
*/
public SafeTeleportBuilder failureMessage(String failureMessage) {
this.failureMessage = failureMessage;
return this;
}
/**
* Set the desired location
* @param location - the location
* @return SafeTeleportBuilder
*/
public SafeTeleportBuilder location(Location location) {
this.location = location;
return this;
}
/**
* Try to teleport the player
* @return SafeSpotTeleport
*/
public SafeSpotTeleport build() {
// Error checking
if (entity == null) {
plugin.logError("Attempt to safe teleport a null entity!");
return null;
}
if (location == null) {
plugin.logError("Attempt to safe teleport to a null location!");
return null;
}
if (failureMessage.isEmpty() && entity instanceof Player) {
failureMessage = User.getInstance(entity).getTranslation("general.errors.warp-not-safe");
}
return new SafeSpotTeleport(plugin, entity, location, failureMessage, portal, homeNumber);
}
}

View File

@ -1,11 +1,5 @@
package world.bentobox.bentobox.util.teleport;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.junit.Before;
@ -16,15 +10,20 @@ import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.LocalesManager;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest(SafeTeleportBuilder.class)
public class SafeTeleportBuilderTest {
@PrepareForTest(SafeSpotTeleport.Builder.class)
public class SafeSpotTeleportBuilderTest {
@Mock
private SafeSpotTeleport sst;
@ -36,14 +35,14 @@ public class SafeTeleportBuilderTest {
private Location loc;
@InjectMocks
private SafeTeleportBuilder stb;
private SafeSpotTeleport.Builder sstb;
@Before
public void setUp() throws Exception {
PowerMockito.whenNew(SafeSpotTeleport.class).withAnyArguments().thenReturn(sst);
// Users
User.setPlugin(plugin);
// Locales - final
// Locales - final
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
when(lm.get(any(), any())).thenReturn("mock translation");
@ -51,61 +50,61 @@ public class SafeTeleportBuilderTest {
@Test
public void test() throws Exception {
stb = new SafeTeleportBuilder(plugin);
stb.build();
SafeSpotTeleport ttt = new SafeSpotTeleport(plugin, player, loc, null, false, 0);
sstb = new SafeSpotTeleport.Builder(plugin);
sstb.build();
SafeSpotTeleport ttt = new SafeSpotTeleport(plugin, player, loc, null, false, 0, false);
assertEquals(sst, ttt);
}
@Test
public void testSafeTeleportBuilder() {
stb = new SafeTeleportBuilder(plugin);
public void testBuilder() {
sstb = new SafeSpotTeleport.Builder(plugin);
// Should fail because no data
assertNull(stb.build());
assertNull(sstb.build());
}
@Test
public void testEntity() throws Exception {
// Start builder
stb = new SafeTeleportBuilder(plugin);
sstb = new SafeSpotTeleport.Builder(plugin);
// Add entity
stb.entity(player);
sstb.entity(player);
// Test for error
assertNull(stb.build());
assertNull(sstb.build());
// Add location
stb.location(loc);
sstb.location(loc);
// Build - expect success
SafeSpotTeleport result = stb.build();
SafeSpotTeleport result = sstb.build();
assertEquals(sst, result);
}
@Test
public void testIsland() {
// Start builder
SafeTeleportBuilder stb = new SafeTeleportBuilder(plugin);
SafeSpotTeleport.Builder sstb = new SafeSpotTeleport.Builder(plugin);
// Add entity
stb.entity(player);
sstb.entity(player);
// Add island
Island island = mock(Island.class);
when(island.getCenter()).thenReturn(loc);
stb.island(island);
sstb.island(island);
// Build - expect success
SafeSpotTeleport result = stb.build();
SafeSpotTeleport result = sstb.build();
assertEquals(sst, result);
}
@Test
public void testHomeNumber() {
// Start builder
SafeTeleportBuilder stb = new SafeTeleportBuilder(plugin);
SafeSpotTeleport.Builder sstb = new SafeSpotTeleport.Builder(plugin);
// Add entity
stb.entity(player);
sstb.entity(player);
// Add location
stb.location(loc);
sstb.location(loc);
// Add home
stb.homeNumber(10);
sstb.homeNumber(10);
// Build - expect success
SafeSpotTeleport result = stb.build();
SafeSpotTeleport result = sstb.build();
assertEquals(sst, result);
}
@ -113,31 +112,30 @@ public class SafeTeleportBuilderTest {
@Test
public void testPortal() {
// Start builder
SafeTeleportBuilder stb = new SafeTeleportBuilder(plugin);
SafeSpotTeleport.Builder sstb = new SafeSpotTeleport.Builder(plugin);
// Add entity
stb.entity(player);
sstb.entity(player);
// Add location
stb.location(loc);
sstb.location(loc);
// Portal
stb.portal();
sstb.portal();
// Build - expect success
SafeSpotTeleport result = stb.build();
SafeSpotTeleport result = sstb.build();
assertEquals(sst, result);
}
@Test
public void testFailureMessage() {
// Start builder
SafeTeleportBuilder stb = new SafeTeleportBuilder(plugin);
SafeSpotTeleport.Builder sstb = new SafeSpotTeleport.Builder(plugin);
// Add entity
stb.entity(player);
sstb.entity(player);
// Add location
stb.location(loc);
sstb.location(loc);
// Add failure
stb.failureMessage("testing 123");
sstb.failureMessage("testing 123");
// Build - expect success
SafeSpotTeleport result = stb.build();
SafeSpotTeleport result = sstb.build();
assertEquals(sst, result);
}
}

View File

@ -114,7 +114,8 @@ public class SafeSpotTeleportTest {
when(loc.getBlock()).thenReturn(block);
boolean portal = false;
int homeNumber = 1;
new SafeSpotTeleport(plugin, player, loc, "failure message", portal, homeNumber);
boolean overrideGamemode = true;
new SafeSpotTeleport(plugin, player, loc, "failure message", portal, homeNumber, true);
}