1
0
mirror of https://github.com/BentoBoxWorld/Warps.git synced 2024-11-22 10:35:52 +01:00

Merge remote-tracking branch 'origin/develop'

This commit is contained in:
tastybento 2019-11-03 08:58:02 -08:00
commit 455242396f
22 changed files with 1511 additions and 322 deletions

22
.travis.yml Normal file
View File

@ -0,0 +1,22 @@
language: java
sudo: false
addons:
sonarcloud:
organization: "bentobox-world"
token:
secure: $SONAR_TOKEN
jdk:
- openjdk8
- openjdk11
matrix:
allow_failures:
- jdk: openjdk11
script:
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install sonar:sonar -Dsonar.projectKey=BentoBoxWorld_Warps
cache:
directories:
- '$HOME/.m2/repository'
- '$HOME/.sonar/cache'

161
pom.xml
View File

@ -1,20 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>world.bentobox</groupId> <groupId>world.bentobox</groupId>
<artifactId>warps</artifactId> <artifactId>warps</artifactId>
<version>${revision}</version> <version>${revision}</version>
<name>WelcomeWarpSigns</name> <name>WelcomeWarpSigns</name>
<description>WelcomeWarpSigns is an add-on for BentoBox, an expandable Minecraft Bukkit plugin for island-type games like ASkyBlock or AcidIsland.</description> <description>WelcomeWarpSigns is an add-on for BentoBox, an expandable Minecraft Bukkit plugin for island-type games like ASkyBlock or AcidIsland.</description>
<url>https://github.com/BentoBoxWorld/addon-welcomewarpsigns</url> <url>https://github.com/BentoBoxWorld/addon-welcomewarpsigns</url>
<inceptionYear>2018</inceptionYear> <inceptionYear>2018</inceptionYear>
<developers> <developers>
<developer> <developer>
<id>tastybento</id> <id>tastybento</id>
<email>tastybento@bentobox.world</email> <email>tastybento@bentobox.world</email>
<timezone>-8</timezone> <timezone>-8</timezone>
@ -24,34 +24,34 @@
</developer> </developer>
</developers> </developers>
<scm> <scm>
<connection>scm:git:https://github.com/BentoBoxWorld/addon-welcomewarpsigns.git</connection> <connection>scm:git:https://github.com/BentoBoxWorld/addon-welcomewarpsigns.git</connection>
<developerConnection>scm:git:git@github.com:BentoBoxWorld/addon-welcomewarpsigns.git</developerConnection> <developerConnection>scm:git:git@github.com:BentoBoxWorld/addon-welcomewarpsigns.git</developerConnection>
<url>https://github.com/BentoBoxWorld/addon-welcomewarpsigns</url> <url>https://github.com/BentoBoxWorld/addon-welcomewarpsigns</url>
</scm> </scm>
<ciManagement> <ciManagement>
<system>jenkins</system> <system>jenkins</system>
<url>http://ci.codemc.org/job/BentoBoxWorld/job/addon-welcomewarpsigns</url> <url>http://ci.codemc.org/job/BentoBoxWorld/job/addon-welcomewarpsigns</url>
</ciManagement> </ciManagement>
<issueManagement> <issueManagement>
<system>GitHub</system> <system>GitHub</system>
<url>https://github.com/BentoBoxWorld/addon-welcomewarpsigns/issues</url> <url>https://github.com/BentoBoxWorld/addon-welcomewarpsigns/issues</url>
</issueManagement> </issueManagement>
<distributionManagement> <distributionManagement>
<snapshotRepository> <snapshotRepository>
<id>codemc-snapshots</id> <id>codemc-snapshots</id>
<url>https://repo.codemc.org/repository/maven-snapshots</url> <url>https://repo.codemc.org/repository/maven-snapshots</url>
</snapshotRepository> </snapshotRepository>
<repository> <repository>
<id>codemc-releases</id> <id>codemc-releases</id>
<url>https://repo.codemc.org/repository/maven-releases</url> <url>https://repo.codemc.org/repository/maven-releases</url>
</repository> </repository>
</distributionManagement> </distributionManagement>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version> <java.version>1.8</java.version>
@ -66,8 +66,8 @@
<!-- Do not change unless you want different name for local builds. --> <!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number> <build.number>-LOCAL</build.number>
<!-- This allows to change between versions. --> <!-- This allows to change between versions. -->
<build.version>1.7.0</build.version> <build.version>1.8.1</build.version>
</properties> </properties>
<!-- Profiles will allow to automatically change build version. --> <!-- Profiles will allow to automatically change build version. -->
<profiles> <profiles>
@ -113,14 +113,14 @@
<id>sonar</id> <id>sonar</id>
<properties> <properties>
<sonar.host.url>https://sonarcloud.io</sonar.host.url> <sonar.host.url>https://sonarcloud.io</sonar.host.url>
<sonar.organization>tastybento-github</sonar.organization> <sonar.organization>bentobox-world</sonar.organization>
</properties> </properties>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.sonarsource.scanner.maven</groupId> <groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId> <artifactId>sonar-maven-plugin</artifactId>
<version>3.4.1.1168</version> <version>3.6.0.1398</version>
<executions> <executions>
<execution> <execution>
<phase>verify</phase> <phase>verify</phase>
@ -135,22 +135,22 @@
</profile> </profile>
</profiles> </profiles>
<repositories> <repositories>
<repository> <repository>
<id>spigot-repo</id> <id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots</url> <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots</url>
</repository> </repository>
<repository> <repository>
<id>codemc</id> <id>codemc</id>
<url>https://repo.codemc.org/repository/maven-snapshots/</url> <url>https://repo.codemc.org/repository/maven-snapshots/</url>
</repository> </repository>
<repository> <repository>
<id>codemc-public</id> <id>codemc-public</id>
<url>https://repo.codemc.org/repository/maven-public/</url> <url>https://repo.codemc.org/repository/maven-public/</url>
</repository> </repository>
</repositories> </repositories>
<dependencies> <dependencies>
<!-- Spigot API --> <!-- Spigot API -->
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
@ -183,15 +183,24 @@
<version>${bentobox.version}</version> <version>${bentobox.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>world.bentobox</groupId> <groupId>world.bentobox</groupId>
<artifactId>level</artifactId> <artifactId>level</artifactId>
<version>${level.version}</version> <version>${level.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> <!-- Static analysis -->
<!-- We are using Eclipse's annotations. If you're using IDEA, update
your project settings to take these into account for in real time static
analysis -->
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.annotation</artifactId>
<version>2.2.200</version>
</dependency>
</dependencies>
<build> <build>
<!-- By default ${revision} is ${build.version}-SNAPSHOT --> <!-- By default ${revision} is ${build.version}-SNAPSHOT -->
<!-- If GIT_BRANCH variable is set to origin/master, then it will <!-- If GIT_BRANCH variable is set to origin/master, then it will
be only ${build.version}. --> be only ${build.version}. -->
@ -202,18 +211,18 @@
be the empty string. --> be the empty string. -->
<finalName>${project.name}-${revision}${build.number}</finalName> <finalName>${project.name}-${revision}${build.number}</finalName>
<defaultGoal>clean package</defaultGoal> <defaultGoal>clean package</defaultGoal>
<resources> <resources>
<resource> <resource>
<directory>src/main/resources</directory> <directory>src/main/resources</directory>
<filtering>true</filtering> <filtering>true</filtering>
</resource> </resource>
<resource> <resource>
<directory>src/main/resources/locales</directory> <directory>src/main/resources/locales</directory>
<targetPath>./locales</targetPath> <targetPath>./locales</targetPath>
<filtering>false</filtering> <filtering>false</filtering>
</resource> </resource>
</resources> </resources>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
@ -300,8 +309,8 @@
<configuration> <configuration>
<append>true</append> <append>true</append>
<excludes> <excludes>
<!-- This is required to prevent Jacoco from adding synthetic fields <!-- This is required to prevent Jacoco from adding
to a JavaBean class (causes errors in testing) --> synthetic fields to a JavaBean class (causes errors in testing) -->
<exclude>**/*Names*</exclude> <exclude>**/*Names*</exclude>
</excludes> </excludes>
</configuration> </configuration>
@ -321,5 +330,5 @@
</executions> </executions>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@ -18,7 +18,7 @@ import world.bentobox.warps.commands.WarpsCommand;
import world.bentobox.warps.config.Settings; import world.bentobox.warps.config.Settings;
/** /**
* Addin to BSkyBlock that enables welcome warp signs * Addin to BentoBox that enables welcome warp signs
* @author tastybento * @author tastybento
* *
*/ */
@ -32,6 +32,11 @@ public class Warp extends Addon {
*/ */
private static final String LEVEL_ADDON_NAME = "Level"; private static final String LEVEL_ADDON_NAME = "Level";
/**
* Permission prefix for non-game world operation
*/
public static final String WELCOME_WARP_SIGNS = "welcomewarpsigns";
/** /**
* Warp panel Manager * Warp panel Manager
*/ */
@ -78,7 +83,11 @@ public class Warp extends Addon {
// Save default config.yml // Save default config.yml
this.saveDefaultConfig(); this.saveDefaultConfig();
// Load the plugin's config // Load the plugin's config
this.loadSettings(); if (this.loadSettings()) {
// Load the master warp and warps command
new WarpCommand(this);
new WarpsCommand(this);
}
} }
@ -111,16 +120,14 @@ public class Warp extends Addon {
// Register commands // Register commands
this.getPlugin().getAddonsManager().getGameModeAddons().forEach(gameModeAddon -> { this.getPlugin().getAddonsManager().getGameModeAddons().forEach(gameModeAddon -> {
if (!this.settings.getDisabledGameModes().contains(gameModeAddon.getDescription().getName())) if (!this.settings.getDisabledGameModes().contains(gameModeAddon.getDescription().getName())
&& gameModeAddon.getPlayerCommand().isPresent())
{ {
if (gameModeAddon.getPlayerCommand().isPresent()) this.registeredWorlds.add(gameModeAddon.getOverWorld());
{
this.registeredWorlds.add(gameModeAddon.getOverWorld());
new WarpCommand(this, gameModeAddon.getPlayerCommand().get()); new WarpCommand(this, gameModeAddon.getPlayerCommand().get());
new WarpsCommand(this, gameModeAddon.getPlayerCommand().get()); new WarpsCommand(this, gameModeAddon.getPlayerCommand().get());
this.hooked = true; this.hooked = true;
}
} }
}); });
@ -130,7 +137,7 @@ public class Warp extends Addon {
warpSignsManager = new WarpSignsManager(this, this.getPlugin()); warpSignsManager = new WarpSignsManager(this, this.getPlugin());
warpPanelManager = new WarpPanelManager(this); warpPanelManager = new WarpPanelManager(this);
// Load the listener // Load the listener
getServer().getPluginManager().registerEvents(new WarpSignsListener(this), this.getPlugin()); this.registerListener(new WarpSignsListener(this));
} }
} }
@ -146,7 +153,7 @@ public class Warp extends Addon {
/** /**
* This method loads addon configuration settings in memory. * This method loads addon configuration settings in memory.
*/ */
private void loadSettings() { private boolean loadSettings() {
if (settingsConfig == null) { if (settingsConfig == null) {
settingsConfig = new Config<>(this, Settings.class); settingsConfig = new Config<>(this, Settings.class);
} }
@ -155,7 +162,10 @@ public class Warp extends Addon {
// Disable // Disable
this.logError("WelcomeWarp settings could not load! Addon disabled."); this.logError("WelcomeWarp settings could not load! Addon disabled.");
this.setState(State.DISABLED); this.setState(State.DISABLED);
return false;
} }
settingsConfig.saveConfigObject(settings);
return true;
} }
@ -215,8 +225,8 @@ public class Warp extends Addon {
// Parse keys // Parse keys
if (metaData.containsKey("world")) { if (metaData.containsKey("world")) {
world = Bukkit.getWorld((String)metaData.get("world")); world = Bukkit.getWorld((String)metaData.get("world"));
if (world == null) return null;
} }
if (world == null) return null;
if (metaData.containsKey("uuid")) { if (metaData.containsKey("uuid")) {
try { try {
uuid = UUID.fromString((String)metaData.get("uuid")); uuid = UUID.fromString((String)metaData.get("uuid"));
@ -229,11 +239,11 @@ public class Warp extends Addon {
case "getSortedWarps": case "getSortedWarps":
return getWarpSignsManager().getSortedWarps(world); return getWarpSignsManager().getSortedWarps(world);
case "getWarp": case "getWarp":
return getWarpSignsManager().getWarp(world, uuid); return uuid == null ? null : getWarpSignsManager().getWarp(world, uuid);
case "getWarpMap": case "getWarpMap":
return getWarpSignsManager().getWarpMap(world); return getWarpSignsManager().getWarpMap(world);
case "hasWarp": case "hasWarp":
return getWarpSignsManager().hasWarp(world, uuid); return uuid == null ? null : getWarpSignsManager().hasWarp(world, uuid);
case "listWarps": case "listWarps":
return getWarpSignsManager().listWarps(world); return getWarpSignsManager().listWarps(world);
default: default:

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Material; import org.bukkit.Material;
@ -30,14 +31,9 @@ public class WarpPanelManager {
private PanelItem getPanelItem(World world, UUID warpOwner) { private PanelItem getPanelItem(World world, UUID warpOwner) {
PanelItemBuilder pib = new PanelItemBuilder() PanelItemBuilder pib = new PanelItemBuilder()
.name(addon.getPlugin().getPlayers().getName(warpOwner)) .name(addon.getSettings().getNameFormat() + addon.getPlugin().getPlayers().getName(warpOwner))
.description(getSign(world, warpOwner)) .description(getSign(world, warpOwner))
.clickHandler((panel, clicker, click, slot) -> { { .clickHandler((panel, clicker, click, slot) -> hander(world, clicker, warpOwner));
clicker.closeInventory();
addon.getWarpSignsManager().warpPlayer(world, clicker, warpOwner);
return true;
}
});
Material icon = getSignIcon(world, warpOwner); Material icon = getSignIcon(world, warpOwner);
if (icon.equals(Material.PLAYER_HEAD)) { if (icon.equals(Material.PLAYER_HEAD)) {
return pib.icon(addon.getPlayers().getName(warpOwner)).build(); return pib.icon(addon.getPlayers().getName(warpOwner)).build();
@ -46,6 +42,20 @@ public class WarpPanelManager {
} }
} }
private boolean hander(World world, User clicker, UUID warpOwner) {
clicker.closeInventory();
addon.getWarpSignsManager().warpPlayer(world, clicker, warpOwner);
return true;
}
private PanelItem getRandomButton(World world, User user, UUID warpOwner) {
///give @p minecraft:player_head{display:{Name:"{\"text\":\"Question Mark\"}"},SkullOwner:"MHF_Question"} 1
return new PanelItemBuilder()
.name(addon.getSettings().getNameFormat() + user.getTranslation("warps.random"))
.clickHandler((panel, clicker, click, slot) -> hander(world, clicker, warpOwner))
.icon(Material.END_CRYSTAL).build();
}
private Material getSignIcon(World world, UUID warpOwner) { private Material getSignIcon(World world, UUID warpOwner) {
// Add the worlds if we haven't seen this before // Add the worlds if we haven't seen this before
cachedSigns.putIfAbsent(world, new HashMap<>()); cachedSigns.putIfAbsent(world, new HashMap<>());
@ -83,6 +93,12 @@ public class WarpPanelManager {
*/ */
public void showWarpPanel(World world, User user, int index) { public void showWarpPanel(World world, User user, int index) {
List<UUID> warps = new ArrayList<>(addon.getWarpSignsManager().getSortedWarps(world)); List<UUID> warps = new ArrayList<>(addon.getWarpSignsManager().getSortedWarps(world));
UUID randomWarp = null;
// Add random UUID
if (!warps.isEmpty() && addon.getSettings().isRandomAllowed()) {
randomWarp = warps.get(new Random().nextInt(warps.size()));
warps.add(0, randomWarp);
}
if (index < 0) { if (index < 0) {
index = 0; index = 0;
} else if (index > (warps.size() / PANEL_MAX_SIZE)) { } else if (index > (warps.size() / PANEL_MAX_SIZE)) {
@ -90,18 +106,22 @@ public class WarpPanelManager {
} }
PanelBuilder panelBuilder = new PanelBuilder() PanelBuilder panelBuilder = new PanelBuilder()
.user(user) .user(user)
.name(user.getTranslation("warps.title") + " " + String.valueOf(index + 1)); .name(user.getTranslation("warps.title") + " " + (index + 1));
int i = index * PANEL_MAX_SIZE; int i = index * PANEL_MAX_SIZE;
for (; i < (index * PANEL_MAX_SIZE + PANEL_MAX_SIZE) && i < warps.size(); i++) { for (; i < (index * PANEL_MAX_SIZE + PANEL_MAX_SIZE) && i < warps.size(); i++) {
panelBuilder.item(getPanelItem(world, warps.get(i))); if (i == 0 && randomWarp != null) {
panelBuilder.item(getRandomButton(world, user, randomWarp));
} else {
panelBuilder.item(getPanelItem(world, warps.get(i)));
}
} }
final int panelNum = index; final int panelNum = index;
// Add signs // Add signs
if (i < warps.size()) { if (i < warps.size()) {
// Next // Next
panelBuilder.item(new PanelItemBuilder() panelBuilder.item(new PanelItemBuilder()
.name("Next") .name(user.getTranslation("warps.next"))
.icon(new ItemStack(Material.STONE)) .icon(new ItemStack(Material.STONE))
.clickHandler((panel, clicker, click, slot) -> { .clickHandler((panel, clicker, click, slot) -> {
user.closeInventory(); user.closeInventory();
@ -112,7 +132,7 @@ public class WarpPanelManager {
if (i > PANEL_MAX_SIZE) { if (i > PANEL_MAX_SIZE) {
// Previous // Previous
panelBuilder.item(new PanelItemBuilder() panelBuilder.item(new PanelItemBuilder()
.name("Previous") .name(user.getTranslation("warps.previous"))
.icon(new ItemStack(Material.COBBLESTONE)) .icon(new ItemStack(Material.COBBLESTONE))
.clickHandler((panel, clicker, click, slot) -> { .clickHandler((panel, clicker, click, slot) -> {
user.closeInventory(); user.closeInventory();

View File

@ -6,8 +6,10 @@ import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -46,39 +48,41 @@ public class WarpSignsListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onSignBreak(BlockBreakEvent e) { public void onSignBreak(BlockBreakEvent e) {
Block b = e.getBlock(); Block b = e.getBlock();
boolean inWorld = addon.getPlugin().getIWM().inWorld(b.getWorld());
// Signs only // Signs only
// FIXME: When we drop support for 1.13, switch to Tag.SIGNS // FIXME: When we drop support for 1.13, switch to Tag.SIGNS
if (!e.getBlock().getType().name().contains("SIGN")) { if (!e.getBlock().getType().name().contains("SIGN")
return; || (inWorld && !addon.inRegisteredWorld(b.getWorld()))
} || (!inWorld && !addon.getSettings().isAllowInOtherWorlds()) ) {
if (!addon.inRegisteredWorld(b.getWorld())) {
return; return;
} }
User user = User.getInstance(e.getPlayer()); User user = User.getInstance(e.getPlayer());
Sign s = (Sign) b.getState(); if (isWarpSign(b)) {
if (s == null) { if (isPlayersSign(e.getPlayer(), b, inWorld)) {
return; addon.getWarpSignsManager().removeWarp(b.getLocation());
} Bukkit.getPluginManager().callEvent(new WarpRemoveEvent(addon, b.getLocation(), user.getUniqueId()));
if (s.getLine(0).equalsIgnoreCase(ChatColor.GREEN + addon.getSettings().getWelcomeLine())) { } else {
// Do a quick check to see if this sign location is in // Someone else's sign - not allowed
// the list of warp signs user.sendMessage("warps.error.no-remove");
Map<UUID, Location> list = addon.getWarpSignsManager().getWarpMap(b.getWorld()); e.setCancelled(true);
if (list.containsValue(s.getLocation())) {
// Welcome sign detected - check to see if it is
// this player's sign
if ((list.containsKey(user.getUniqueId()) && list.get(user.getUniqueId()).equals(s.getLocation()))
|| user.isOp() || user.hasPermission(addon.getPermPrefix(e.getBlock().getWorld()) + "mod.removesign")) {
addon.getWarpSignsManager().removeWarp(s.getLocation());
Bukkit.getPluginManager().callEvent(new WarpRemoveEvent(addon, s.getLocation(), user.getUniqueId()));
} else {
// Someone else's sign - not allowed
user.sendMessage("warps.error.no-remove");
e.setCancelled(true);
}
} }
} }
} }
private boolean isPlayersSign(Player player, Block b, boolean inWorld) {
// Welcome sign detected - check to see if it is this player's sign
Map<UUID, Location> list = addon.getWarpSignsManager().getWarpMap(b.getWorld());
String reqPerm = inWorld ? addon.getPermPrefix(b.getWorld()) + "mod.removesign" : Warp.WELCOME_WARP_SIGNS + ".mod.removesign";
return ((list.containsKey(player.getUniqueId()) && list.get(player.getUniqueId()).equals(b.getLocation()))
|| player.isOp() || player.hasPermission(reqPerm));
}
private boolean isWarpSign(Block b) {
Sign s = (Sign) b.getState();
return s.getLine(0).equalsIgnoreCase(ChatColor.GREEN + addon.getSettings().getWelcomeLine())
&& addon.getWarpSignsManager().getWarpMap(b.getWorld()).containsValue(s.getLocation());
}
/** /**
* Event handler for Sign Changes * Event handler for Sign Changes
* *
@ -87,7 +91,8 @@ public class WarpSignsListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onSignWarpCreate(SignChangeEvent e) { public void onSignWarpCreate(SignChangeEvent e) {
Block b = e.getBlock(); Block b = e.getBlock();
if (!addon.inRegisteredWorld(b.getWorld())) { boolean inWorld = addon.getPlugin().getIWM().inWorld(b.getWorld());
if ((inWorld && !addon.inRegisteredWorld(b.getWorld())) || (!inWorld && !addon.getSettings().isAllowInOtherWorlds()) ) {
return; return;
} }
String title = e.getLine(0); String title = e.getLine(0);
@ -95,24 +100,10 @@ public class WarpSignsListener implements Listener {
// Check if someone is changing their own sign // Check if someone is changing their own sign
if (title.equalsIgnoreCase(addon.getSettings().getWelcomeLine())) { if (title.equalsIgnoreCase(addon.getSettings().getWelcomeLine())) {
// Welcome sign detected - check permissions // Welcome sign detected - check permissions
if (!(user.hasPermission(addon.getPermPrefix(b.getWorld()) + "island.addwarp"))) { if (noPerms(user, b.getWorld(), inWorld)) {
user.sendMessage("warps.error.no-permission");
user.sendMessage("general.errors.no-permission", "[permission]", addon.getPermPrefix(b.getWorld()) + "island.addwarp");
return; return;
} }
// Get level if level addon is available if (inWorld && noLevelOrIsland(user, b.getWorld())) {
Long level = addon.getLevel(Util.getWorld(b.getWorld()), user.getUniqueId());
if (level != null && level < addon.getSettings().getWarpLevelRestriction()) {
user.sendMessage("warps.error.not-enough-level");
user.sendMessage("warps.error.your-level-is",
"[level]", String.valueOf(level),
"[required]", String.valueOf(addon.getSettings().getWarpLevelRestriction()));
return;
}
// Check that the player is on their island
if (!(plugin.getIslands().userIsOnIsland(b.getWorld(), user))) {
user.sendMessage("warps.error.not-on-island");
e.setLine(0, ChatColor.RED + addon.getSettings().getWelcomeLine()); e.setLine(0, ChatColor.RED + addon.getSettings().getWelcomeLine());
return; return;
} }
@ -131,14 +122,12 @@ public class WarpSignsListener implements Listener {
if (oldSignBlock.getType().name().contains("SIGN")) { if (oldSignBlock.getType().name().contains("SIGN")) {
// The block is still a sign // The block is still a sign
Sign oldSign = (Sign) oldSignBlock.getState(); Sign oldSign = (Sign) oldSignBlock.getState();
if (oldSign != null) { if (oldSign.getLine(0).equalsIgnoreCase(ChatColor.GREEN + addon.getSettings().getWelcomeLine())) {
if (oldSign.getLine(0).equalsIgnoreCase(ChatColor.GREEN + addon.getSettings().getWelcomeLine())) { oldSign.setLine(0, ChatColor.RED + addon.getSettings().getWelcomeLine());
oldSign.setLine(0, ChatColor.RED + addon.getSettings().getWelcomeLine()); oldSign.update(true, false);
oldSign.update(true, false); user.sendMessage("warps.deactivate");
user.sendMessage("warps.deactivate"); addon.getWarpSignsManager().removeWarp(oldSignBlock.getWorld(), user.getUniqueId());
addon.getWarpSignsManager().removeWarp(oldSignBlock.getWorld(), user.getUniqueId()); Bukkit.getPluginManager().callEvent(new WarpRemoveEvent(addon, oldSign.getLocation(), user.getUniqueId()));
Bukkit.getPluginManager().callEvent(new WarpRemoveEvent(addon, oldSign.getLocation(), user.getUniqueId()));
}
} }
} }
// Set up the new warp sign // Set up the new warp sign
@ -148,6 +137,42 @@ public class WarpSignsListener implements Listener {
} }
private boolean noLevelOrIsland(User user, World world) {
// Get level if level addon is available
Long level = addon.getLevel(Util.getWorld(world), user.getUniqueId());
if (level != null && level < addon.getSettings().getWarpLevelRestriction()) {
user.sendMessage("warps.error.not-enough-level");
user.sendMessage("warps.error.your-level-is",
"[level]", String.valueOf(level),
"[required]", String.valueOf(addon.getSettings().getWarpLevelRestriction()));
return true;
}
// Check that the player is on their island
if (!(plugin.getIslands().userIsOnIsland(world, user))) {
user.sendMessage("warps.error.not-on-island");
return true;
}
return false;
}
/**
* Check if player has permission to execute command
* @param user - user
* @param world - world that the warp is in
* @param inWorld - true if warp is in a game world
* @return true if player does not have the required perms, false otherwise
*/
private boolean noPerms(User user, World world, boolean inWorld) {
String permReq = inWorld ? addon.getPermPrefix(world) + "island.addwarp" : Warp.WELCOME_WARP_SIGNS + ".addwarp";
if (!(user.hasPermission(permReq))) {
user.sendMessage("warps.error.no-permission");
user.sendMessage("general.errors.no-permission", "[permission]", permReq);
return true;
}
return false;
}
private void addSign(SignChangeEvent e, User user, Block b) { private void addSign(SignChangeEvent e, User user, Block b) {
if (addon.getWarpSignsManager().addWarp(user.getUniqueId(), b.getLocation())) { if (addon.getWarpSignsManager().addWarp(user.getUniqueId(), b.getLocation())) {
user.sendMessage("warps.success"); user.sendMessage("warps.success");

View File

@ -25,6 +25,8 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.permissions.PermissionAttachmentInfo;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.addons.Addon;
@ -59,7 +61,8 @@ public class WarpSignsManager {
* @param world - world * @param world - world
* @return map of warps * @return map of warps
*/ */
public Map<UUID, Location> getWarpMap(World world) { @NonNull
public Map<UUID, Location> getWarpMap(@Nullable World world) {
return worldsWarpList.computeIfAbsent(Util.getWorld(world), k -> new HashMap<>()); return worldsWarpList.computeIfAbsent(Util.getWorld(world), k -> new HashMap<>());
} }
@ -70,7 +73,7 @@ public class WarpSignsManager {
public WarpSignsManager(Warp addon, BentoBox plugin) { public WarpSignsManager(Warp addon, BentoBox plugin) {
this.addon = addon; this.addon = addon;
this.plugin = plugin; this.plugin = plugin;
// Set up the database handler to store and retrieve Island classes // Set up the database handler
// Note that these are saved by the BSkyBlock database // Note that these are saved by the BSkyBlock database
handler = new Database<>(addon, WarpsData.class); handler = new Database<>(addon, WarpsData.class);
// Load the warps // Load the warps
@ -86,7 +89,7 @@ public class WarpSignsManager {
*/ */
public boolean addWarp(final UUID playerUUID, final Location loc) { public boolean addWarp(final UUID playerUUID, final Location loc) {
// Do not allow null players to set warps // Do not allow null players to set warps
if (playerUUID == null) { if (playerUUID == null || loc == null) {
return false; return false;
} }
// Check for warps placed in a location where there was a warp before // Check for warps placed in a location where there was a warp before
@ -108,6 +111,7 @@ public class WarpSignsManager {
* - the warp requested * - the warp requested
* @return Location of warp or null * @return Location of warp or null
*/ */
@Nullable
public Location getWarp(World world, UUID playerUUID) { public Location getWarp(World world, UUID playerUUID) {
return getWarpMap(world).get(playerUUID); return getWarpMap(world).get(playerUUID);
} }
@ -117,6 +121,7 @@ public class WarpSignsManager {
* @param location to search * @param location to search
* @return Name of warp owner or empty string if there is none * @return Name of warp owner or empty string if there is none
*/ */
@NonNull
public String getWarpOwner(Location location) { public String getWarpOwner(Location location) {
return getWarpMap(location.getWorld()).entrySet().stream().filter(en -> en.getValue().equals(location)) return getWarpMap(location.getWorld()).entrySet().stream().filter(en -> en.getValue().equals(location))
.findFirst().map(en -> plugin.getPlayers().getName(en.getKey())).orElse(""); .findFirst().map(en -> plugin.getPlayers().getName(en.getKey())).orElse("");
@ -126,11 +131,12 @@ public class WarpSignsManager {
* Get sorted list of warps with most recent players listed first * Get sorted list of warps with most recent players listed first
* @return UUID list * @return UUID list
*/ */
public List<UUID> getSortedWarps(World world) { @NonNull
public List<UUID> getSortedWarps(@NonNull World world) {
// Remove any null locations - this can happen if an admin changes the name of the world and signs point to old locations // Remove any null locations - this can happen if an admin changes the name of the world and signs point to old locations
getWarpMap(world).values().removeIf(Objects::isNull); getWarpMap(world).values().removeIf(Objects::isNull);
// Bigger value of time means a more recent login // Bigger value of time means a more recent login
TreeMap<Long, UUID> map = new TreeMap<Long, UUID>(); TreeMap<Long, UUID> map = new TreeMap<>();
getWarpMap(world).entrySet().forEach(en -> { getWarpMap(world).entrySet().forEach(en -> {
UUID uuid = en.getKey(); UUID uuid = en.getKey();
// If never played, will be zero // If never played, will be zero
@ -148,7 +154,7 @@ public class WarpSignsManager {
} }
// Fire event // Fire event
WarpListEvent event = new WarpListEvent(addon, list); WarpListEvent event = new WarpListEvent(addon, list);
addon.getServer().getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
// Get the result of any changes by listeners // Get the result of any changes by listeners
list = event.getWarps(); list = event.getWarps();
return list; return list;
@ -160,7 +166,8 @@ public class WarpSignsManager {
* *
* @return UUID set of warps * @return UUID set of warps
*/ */
public Set<UUID> listWarps(World world) { @NonNull
public Set<UUID> listWarps(@NonNull World world) {
// Remove any null locations // Remove any null locations
getWarpMap(world).values().removeIf(Objects::isNull); getWarpMap(world).values().removeIf(Objects::isNull);
return getWarpMap(world).entrySet().stream().filter(e -> Util.sameWorld(world, e.getValue().getWorld())).map(Map.Entry::getKey).collect(Collectors.toSet()); return getWarpMap(world).entrySet().stream().filter(e -> Util.sameWorld(world, e.getValue().getWorld())).map(Map.Entry::getKey).collect(Collectors.toSet());
@ -194,11 +201,9 @@ public class WarpSignsManager {
Block b = loc.getBlock(); Block b = loc.getBlock();
if (b.getType().name().contains("SIGN")) { if (b.getType().name().contains("SIGN")) {
Sign s = (Sign) b.getState(); Sign s = (Sign) b.getState();
if (s != null) { if (s.getLine(0).equalsIgnoreCase(ChatColor.GREEN + addon.getSettings().getWelcomeLine())) {
if (s.getLine(0).equalsIgnoreCase(ChatColor.GREEN + addon.getSettings().getWelcomeLine())) { s.setLine(0, ChatColor.RED + addon.getSettings().getWelcomeLine());
s.setLine(0, ChatColor.RED + addon.getSettings().getWelcomeLine()); s.update(true, false);
s.update(true, false);
}
} }
} }
} }
@ -257,7 +262,8 @@ public class WarpSignsManager {
* @param uuid - player's uuid * @param uuid - player's uuid
* @return Sign's content and type * @return Sign's content and type
*/ */
public SignCache getSignInfo(World world, UUID uuid) { @NonNull
public SignCache getSignInfo(@NonNull World world, @NonNull UUID uuid) {
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
//get the sign info //get the sign info
Location signLocation = getWarp(world, uuid); Location signLocation = getWarp(world, uuid);
@ -268,10 +274,13 @@ public class WarpSignsManager {
result.remove(0); result.remove(0);
// Remove any trailing blank lines // Remove any trailing blank lines
result.removeIf(String::isEmpty); result.removeIf(String::isEmpty);
// Set the initial color per lore setting
for (int i = 0; i< result.size(); i++) {
result.set(i, ChatColor.translateAlternateColorCodes('&', addon.getSettings().getLoreFormat()) + result.get(i));
}
// Get the sign type // Get the sign type
String prefix = this.plugin.getIWM().getAddon(world).map( String prefix = plugin.getIWM().getAddon(world).map(Addon::getPermissionPrefix).orElse("");
Addon::getPermissionPrefix).orElse("");
Material icon; Material icon;
@ -306,7 +315,7 @@ public class WarpSignsManager {
* @param directionFacing - direction that sign is facing * @param directionFacing - direction that sign is facing
* @param pvp - true if this location allowed PVP * @param pvp - true if this location allowed PVP
*/ */
private void warpPlayer(User user, Location inFront, UUID signOwner, BlockFace directionFacing, boolean pvp) { private void warpPlayer(@NonNull User user, @NonNull Location inFront, @NonNull UUID signOwner, @NonNull BlockFace directionFacing, boolean pvp) {
// convert blockface to angle // convert blockface to angle
float yaw = blockFaceToFloat(directionFacing); float yaw = blockFaceToFloat(directionFacing);
final Location actualWarp = new Location(inFront.getWorld(), inFront.getBlockX() + 0.5D, inFront.getBlockY(), final Location actualWarp = new Location(inFront.getWorld(), inFront.getBlockX() + 0.5D, inFront.getBlockY(),
@ -331,7 +340,7 @@ public class WarpSignsManager {
* @param face * @param face
* @return degrees * @return degrees
*/ */
private float blockFaceToFloat(BlockFace face) { private float blockFaceToFloat(@NonNull BlockFace face) {
switch (face) { switch (face) {
case EAST: case EAST:
return 90F; return 90F;
@ -377,7 +386,7 @@ public class WarpSignsManager {
* @param user - user who is warping * @param user - user who is warping
* @param owner - owner of the warp * @param owner - owner of the warp
*/ */
public void warpPlayer(World world, User user, UUID owner) { public void warpPlayer(@NonNull World world, @NonNull User user, @NonNull UUID owner) {
final Location warpSpot = getWarp(world, owner); final Location warpSpot = getWarp(world, owner);
// Check if the warp spot is safe // Check if the warp spot is safe
if (warpSpot == null) { if (warpSpot == null) {
@ -394,7 +403,7 @@ public class WarpSignsManager {
return; return;
} }
Island island = addon.getPlugin().getIslands().getIsland(world, owner); Island island = addon.getIslands().getIsland(world, owner);
boolean pvp = false; boolean pvp = false;
if (island != null) { if (island != null) {
// Check for PVP // Check for PVP
@ -421,30 +430,29 @@ public class WarpSignsManager {
Location inFront = b.getRelative(directionFacing).getLocation(); Location inFront = b.getRelative(directionFacing).getLocation();
Location oneDown = b.getRelative(directionFacing).getRelative(BlockFace.DOWN).getLocation(); Location oneDown = b.getRelative(directionFacing).getRelative(BlockFace.DOWN).getLocation();
if ((plugin.getIslands().isSafeLocation(inFront))) { if ((plugin.getIslands().isSafeLocation(inFront))) {
addon.getWarpSignsManager().warpPlayer(user, inFront, owner, directionFacing, pvp); warpPlayer(user, inFront, owner, directionFacing, pvp);
return; return;
} else if (plugin.getIslands().isSafeLocation(oneDown)) { } else if (plugin.getIslands().isSafeLocation(oneDown)) {
// Try one block down if this is a wall sign // Try one block down if this is a wall sign
addon.getWarpSignsManager().warpPlayer(user, oneDown, owner, directionFacing, pvp); warpPlayer(user, oneDown, owner, directionFacing, pvp);
return; return;
} }
} else if (b.getType().name().contains("SIGN")) { } else if (b.getType().name().contains("SIGN")) {
org.bukkit.block.data.type.Sign s = (org.bukkit.block.data.type.Sign) b.getBlockData(); org.bukkit.block.data.type.Sign s = (org.bukkit.block.data.type.Sign) b.getBlockData();
BlockFace directionFacing = s.getRotation(); BlockFace directionFacing = s.getRotation();
Location inFront = b.getRelative(directionFacing).getLocation(); Location inFront = b.getRelative(directionFacing).getLocation();
if ((plugin.getIslands().isSafeLocation(inFront))) { if ((addon.getIslands().isSafeLocation(inFront))) {
addon.getWarpSignsManager().warpPlayer(user, inFront, owner, directionFacing, pvp); warpPlayer(user, inFront, owner, directionFacing, pvp);
return; return;
} }
} else { } else {
// Warp has been removed // Warp has been removed
user.sendMessage("warps.error.does-not-exist"); user.sendMessage("warps.error.does-not-exist");
addon.getWarpSignsManager().removeWarp(warpSpot); removeWarp(warpSpot);
return; return;
} }
if (!(plugin.getIslands().isSafeLocation(warpSpot))) { if (!(plugin.getIslands().isSafeLocation(warpSpot))) {
user.sendMessage("warps.error.not-safe"); user.sendMessage("warps.error.not-safe");
return;
} else { } else {
final Location actualWarp = new Location(warpSpot.getWorld(), warpSpot.getBlockX() + 0.5D, warpSpot.getBlockY(), final Location actualWarp = new Location(warpSpot.getWorld(), warpSpot.getBlockX() + 0.5D, warpSpot.getBlockY(),
warpSpot.getBlockZ() + 0.5D); warpSpot.getBlockZ() + 0.5D);
@ -455,7 +463,6 @@ public class WarpSignsManager {
user.getWorld().playSound(user.getLocation(), Sound.ENTITY_BAT_TAKEOFF, 1F, 1F); user.getWorld().playSound(user.getLocation(), Sound.ENTITY_BAT_TAKEOFF, 1F, 1F);
} }
user.teleport(actualWarp); user.teleport(actualWarp);
return;
} }
} }
@ -464,7 +471,7 @@ public class WarpSignsManager {
* @param playerUUID - player's UUID * @param playerUUID - player's UUID
* @return true if they have warp * @return true if they have warp
*/ */
public boolean hasWarp(World world, UUID playerUUID) { public boolean hasWarp(@NonNull World world, @NonNull UUID playerUUID) {
return getWarpMap(world).containsKey(playerUUID); return getWarpMap(world).containsKey(playerUUID);
} }
@ -482,7 +489,7 @@ public class WarpSignsManager {
* @param defaultValue Default value that will be returned if permission not found. * @param defaultValue Default value that will be returned if permission not found.
* @return String value that follows permissionPrefix. * @return String value that follows permissionPrefix.
*/ */
private String getPermissionValue(User user, String permissionPrefix, String defaultValue) private String getPermissionValue(@NonNull User user, @NonNull String permissionPrefix, @NonNull String defaultValue)
{ {
if (user.isPlayer()) if (user.isPlayer())
{ {

View File

@ -1,10 +1,12 @@
package world.bentobox.warps.commands; package world.bentobox.warps.commands;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.World;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
@ -21,13 +23,18 @@ public class WarpCommand extends CompositeCommand {
private Warp addon; private Warp addon;
public WarpCommand(Warp addon, CompositeCommand bsbIslandCmd) { public WarpCommand(Warp addon, CompositeCommand bsbIslandCmd) {
super(bsbIslandCmd, "warp"); super(bsbIslandCmd, addon.getSettings().getWarpCommand());
this.addon = addon;
}
public WarpCommand(Warp addon) {
super(addon.getSettings().getWarpCommand());
this.addon = addon; this.addon = addon;
} }
@Override @Override
public void setup() { public void setup() {
this.setPermission("island.warp"); this.setPermission(this.getParent() == null ? Warp.WELCOME_WARP_SIGNS + ".warp" : "island.warp");
this.setOnlyPlayer(true); this.setOnlyPlayer(true);
this.setParametersHelp("warp.help.parameters"); this.setParametersHelp("warp.help.parameters");
this.setDescription("warp.help.description"); this.setDescription("warp.help.description");
@ -36,22 +43,23 @@ public class WarpCommand extends CompositeCommand {
@Override @Override
public boolean execute(User user, String label, List<String> args) { public boolean execute(User user, String label, List<String> args) {
if (args.size() == 1) { if (args.size() == 1) {
World world = getWorld() == null ? user.getWorld() : getWorld();
// Warp somewhere command // Warp somewhere command
Set<UUID> warpList = addon.getWarpSignsManager().listWarps(getWorld()); Set<UUID> warpList = addon.getWarpSignsManager().listWarps(world);
if (warpList.isEmpty()) { if (warpList.isEmpty()) {
user.sendMessage("warps.error.no-warps-yet"); user.sendMessage("warps.error.no-warps-yet");
user.sendMessage("warps.warpTip", "[text]", addon.getSettings().getWelcomeLine()); user.sendMessage("warps.warpTip", "[text]", addon.getSettings().getWelcomeLine());
return true; return false;
} else { } else {
// Check if this is part of a name // Check if this is part of a name
UUID foundWarp = warpList.stream().filter(u -> getPlayers().getName(u).toLowerCase().equals(args.get(0).toLowerCase()) UUID foundWarp = warpList.stream().filter(u -> getPlayers().getName(u).equalsIgnoreCase(args.get(0))
|| getPlayers().getName(u).toLowerCase().startsWith(args.get(0).toLowerCase())).findFirst().orElse(null); || getPlayers().getName(u).toLowerCase().startsWith(args.get(0).toLowerCase())).findFirst().orElse(null);
if (foundWarp == null) { if (foundWarp == null) {
user.sendMessage("warps.error.does-not-exist"); user.sendMessage("warps.error.does-not-exist");
return false; return false;
} else { } else {
// Warp exists! // Warp exists!
addon.getWarpSignsManager().warpPlayer(getWorld(), user, foundWarp); addon.getWarpSignsManager().warpPlayer(world, user, foundWarp);
return true; return true;
} }
} }
@ -62,14 +70,8 @@ public class WarpCommand extends CompositeCommand {
@Override @Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) { public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
List<String> options = new ArrayList<>(); World world = getWorld() == null ? user.getWorld() : getWorld();
final Set<UUID> warpList = addon.getWarpSignsManager().listWarps(getWorld()); return Optional.of(addon.getWarpSignsManager().listWarps(world).stream().map(getPlayers()::getName).collect(Collectors.toList()));
for (UUID warp : warpList) {
options.add(addon.getPlugin().getPlayers().getName(warp));
}
return Optional.of(options);
} }

View File

@ -1,16 +1,16 @@
/**
*
*/
package world.bentobox.warps.commands; package world.bentobox.warps.commands;
import java.util.List; import java.util.List;
import org.bukkit.World;
import world.bentobox.warps.Warp; import world.bentobox.warps.Warp;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
/** /**
* @author ben * Handles the warps command
* @author tastybento
* *
*/ */
public class WarpsCommand extends CompositeCommand { public class WarpsCommand extends CompositeCommand {
@ -18,7 +18,12 @@ public class WarpsCommand extends CompositeCommand {
private Warp addon; private Warp addon;
public WarpsCommand(Warp addon, CompositeCommand bsbIslandCmd) { public WarpsCommand(Warp addon, CompositeCommand bsbIslandCmd) {
super(bsbIslandCmd, "warps"); super(bsbIslandCmd, addon.getSettings().getWarpsCommand());
this.addon = addon;
}
public WarpsCommand(Warp addon) {
super(addon.getSettings().getWarpsCommand());
this.addon = addon; this.addon = addon;
} }
@ -27,7 +32,7 @@ public class WarpsCommand extends CompositeCommand {
*/ */
@Override @Override
public void setup() { public void setup() {
this.setPermission("island.warp"); this.setPermission(this.getParent() == null ? Warp.WELCOME_WARP_SIGNS + ".warp" : "island.warp");
this.setOnlyPlayer(true); this.setOnlyPlayer(true);
this.setDescription("warps.help.description"); this.setDescription("warps.help.description");
} }
@ -37,12 +42,13 @@ public class WarpsCommand extends CompositeCommand {
*/ */
@Override @Override
public boolean execute(User user, String label, List<String> args) { public boolean execute(User user, String label, List<String> args) {
if (addon.getWarpSignsManager().listWarps(getWorld()).isEmpty()) { World world = getWorld() == null ? user.getWorld() : getWorld();
if (addon.getWarpSignsManager().listWarps(world).isEmpty()) {
user.sendMessage("warps.error.no-warps-yet"); user.sendMessage("warps.error.no-warps-yet");
user.sendMessage("warps.warpTip", "[text]", addon.getSettings().getWelcomeLine()); user.sendMessage("warps.warpTip", "[text]", addon.getSettings().getWelcomeLine());
} else { return false;
addon.getWarpPanelManager().showWarpPanel(getWorld(), user,0);
} }
addon.getWarpPanelManager().showWarpPanel(world, user, 0);
return true; return true;
} }

View File

@ -45,6 +45,36 @@ public class Settings implements ConfigObject
@ConfigEntry(path = "disabled-gamemodes") @ConfigEntry(path = "disabled-gamemodes")
private Set<String> disabledGameModes = new HashSet<>(); private Set<String> disabledGameModes = new HashSet<>();
@ConfigComment("")
@ConfigComment("Warp panel name formatting.")
@ConfigComment("Example: &c will make names red. &f is white")
@ConfigEntry(path = "name-format")
private String nameFormat = "&f";
@ConfigComment("")
@ConfigComment("Warp panel default lore formatting.")
@ConfigComment("Example: &c will make lore red. &f is white")
@ConfigEntry(path = "lore-format")
private String loreFormat = "&f";
@ConfigComment("")
@ConfigComment("Allow random teleport - adds a button to the warp panel that goes to a random warp sign")
@ConfigEntry(path = "random-allowed")
private boolean randomAllowed = true;
@ConfigComment("")
@ConfigComment("Allow use in other worlds. Players must have the welcomewarpsigns.warp permission.")
@ConfigEntry(path = "allow-in-other-worlds")
private boolean allowInOtherWorlds = false;
@ConfigComment("")
@ConfigComment("Warp and warps commands. You can change them if they clash with other addons or plugins.")
@ConfigEntry(path = "warp-command")
String warpCommand = "warp";
@ConfigEntry(path = "warps-command")
String warpsCommand = "warps";
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Section: Constructor // Section: Constructor
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
@ -143,4 +173,102 @@ public class Settings implements ConfigObject
{ {
this.icon = icon; this.icon = icon;
} }
/**
* @return the nameFormat
*/
public String getNameFormat() {
return nameFormat;
}
/**
* @param nameFormat the nameFormat to set
*/
public void setNameFormat(String nameFormat) {
this.nameFormat = nameFormat;
}
/**
* @return the loreFormat
*/
public String getLoreFormat() {
return loreFormat;
}
/**
* @param loreFormat the loreFormat to set
*/
public void setLoreFormat(String loreFormat) {
this.loreFormat = loreFormat;
}
/**
* @return the randomAllowed
*/
public boolean isRandomAllowed() {
return randomAllowed;
}
/**
* @param randomAllowed the randomAllowed to set
*/
public void setRandomAllowed(boolean randomAllowed) {
this.randomAllowed = randomAllowed;
}
/**
* @return the allowInOtherWorlds
*/
public boolean isAllowInOtherWorlds() {
return allowInOtherWorlds;
}
/**
* @param allowInOtherWorlds the allowInOtherWorlds to set
*/
public void setAllowInOtherWorlds(boolean allowInOtherWorlds) {
this.allowInOtherWorlds = allowInOtherWorlds;
}
/**
* @return the warpCommand
*/
public String getWarpCommand() {
return warpCommand;
}
/**
* @param warpCommand the warpCommand to set
*/
public void setWarpCommand(String warpCommand) {
this.warpCommand = warpCommand;
}
/**
* @return the warpsCommand
*/
public String getWarpsCommand() {
return warpsCommand;
}
/**
* @param warpsCommand the warpsCommand to set
*/
public void setWarpsCommand(String warpsCommand) {
this.warpsCommand = warpsCommand;
}
} }

View File

@ -11,39 +11,39 @@ import world.bentobox.warps.Warp;
/** /**
* This event is fired when a Warp is created * This event is fired when a Warp is created
* A Listener to this event can use it only to get informations. e.g: broadcast something * A Listener to this event can use it only to get informations. e.g: broadcast something
* *
* @author Poslovitch * @author Poslovitch
* *
*/ */
public class WarpCreateEvent extends Event{ public class WarpCreateEvent extends Event{
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private Location warpLoc; private Location warpLoc;
private UUID creator; private UUID creator;
/** /**
* @param plugin - BSkyBlock plugin objects * @param plugin - BSkyBlock plugin objects
* @param warpLoc * @param warpLoc
* @param creator * @param creator
*/ */
public WarpCreateEvent(Warp plugin, Location warpLoc, UUID creator){ public WarpCreateEvent(Warp plugin, Location warpLoc, UUID creator){
this.warpLoc = warpLoc; this.warpLoc = warpLoc;
this.creator = creator; this.creator = creator;
} }
/** /**
* Get the location of the created Warp * Get the location of the created Warp
* @return created warp's location * @return created warp's location
*/ */
public Location getWarpLocation(){return this.warpLoc;} public Location getWarpLocation(){return this.warpLoc;}
/** /**
* Get who has created the warp * Get who has created the warp
* @return the warp's creator * @return the warp's creator
*/ */
public UUID getCreator(){return this.creator;} public UUID getCreator(){return this.creator;}
@Override @Override
public HandlerList getHandlers() { public HandlerList getHandlers() {
return handlers; return handlers;
} }

View File

@ -10,34 +10,34 @@ import org.bukkit.event.HandlerList;
import world.bentobox.warps.Warp; import world.bentobox.warps.Warp;
/** /**
* This event is fired when a player tries to do a warp * This event is fired when a player tries to do a warp
* A Listener to this event can use it to get informations. e.g: broadcast something * A Listener to this event can use it to get informations. e.g: broadcast something
* *
* @author tastybento * @author tastybento
* *
*/ */
public class WarpInitiateEvent extends Event implements Cancellable { public class WarpInitiateEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private boolean cancelled; private boolean cancelled;
private Location warpLoc; private Location warpLoc;
private final UUID player; private final UUID player;
/** /**
* @param plugin - BSkyBlock plugin objects * @param plugin - BSkyBlock plugin objects
* @param warpLoc - where the player is warping to * @param warpLoc - where the player is warping to
* @param player - the UUID of the player * @param player - the UUID of the player
*/ */
public WarpInitiateEvent(Warp plugin, Location warpLoc, UUID player){ public WarpInitiateEvent(Warp plugin, Location warpLoc, UUID player){
this.warpLoc = warpLoc; this.warpLoc = warpLoc;
this.player = player; this.player = player;
} }
/** /**
* Get the location of the Warp * Get the location of the Warp
* @return created warp's location * @return created warp's location
*/ */
public Location getWarpLoc(){return this.warpLoc;} public Location getWarpLoc(){return this.warpLoc;}
/** /**
* Set a different location to where the player will go * Set a different location to where the player will go
* @param warpLoc * @param warpLoc
@ -47,12 +47,12 @@ public class WarpInitiateEvent extends Event implements Cancellable {
} }
/** /**
* Get who is warping * Get who is warping
* @return the warping player's uuid * @return the warping player's uuid
*/ */
public UUID getPlayer(){return this.player;} public UUID getPlayer(){return this.player;}
@Override @Override
public HandlerList getHandlers() { public HandlerList getHandlers() {
return handlers; return handlers;
} }
@ -63,13 +63,12 @@ public class WarpInitiateEvent extends Event implements Cancellable {
@Override @Override
public boolean isCancelled() { public boolean isCancelled() {
// TODO Auto-generated method stub
return cancelled; return cancelled;
} }
@Override @Override
public void setCancelled(boolean cancelled) { public void setCancelled(boolean cancelled) {
this.cancelled = cancelled; this.cancelled = cancelled;
} }
} }

View File

@ -12,13 +12,15 @@ import com.google.gson.annotations.Expose;
import world.bentobox.bentobox.database.objects.DataObject; import world.bentobox.bentobox.database.objects.DataObject;
public class WarpsData implements DataObject { public class WarpsData implements DataObject {
@Expose @Expose
private String uniqueId = "warps"; private String uniqueId = "warps";
@Expose @Expose
private Map<Location, UUID> warpSigns = new HashMap<>(); private Map<Location, UUID> warpSigns = new HashMap<>();
public WarpsData() {} public WarpsData() {
// Required by YAML database
}
@Override @Override
public String getUniqueId() { public String getUniqueId() {
@ -31,8 +33,8 @@ public class WarpsData implements DataObject {
} }
public Map<Location, UUID> getWarpSigns() { public Map<Location, UUID> getWarpSigns() {
if (warpSigns == null) if (warpSigns == null)
return new HashMap<>(); return new HashMap<>();
return warpSigns; return warpSigns;
} }

View File

@ -23,3 +23,21 @@ icon: 'SIGN'
# disabled-gamemodes: # disabled-gamemodes:
# - BSkyBlock # - BSkyBlock
disabled-gamemodes: [] disabled-gamemodes: []
#
# Warp panel name formatting.
# Example: &c will make names red, &f is white
name-format: &f
#
# Warp panel default lore formatting.
# Example: &c will make lore red. &f is white
lore-format: &f
#
# Allow random teleport - adds a button to the warp panel that goes to a random warp sign
random-allowed: true
#
# Allow use in other worlds. Players must have the welcomewarpsigns.warp permission.
allow-in-other-worlds: false
#
# Warp and warps commands. You can change them if they clash with other addons or plugins.
warp-command: warp
warps-command: warps

View File

@ -23,6 +23,7 @@ warps:
next: "&6Next page" next: "&6Next page"
player-warped: "&2[name] warped to your warp sign!" player-warped: "&2[name] warped to your warp sign!"
previous: "&6Previous page" previous: "&6Previous page"
random: "&4Random Warp"
sign-removed: "&CWarp sign removed!" sign-removed: "&CWarp sign removed!"
success: "&ASuccess!" success: "&ASuccess!"
title: "Warp Signs" title: "Warp Signs"

View File

@ -1,30 +1,28 @@
########################################################################################### ---
# This is a YML file. Be careful when editing. Check your edits in a YAML checker like # warp:
# the one at http://yaml-online-parser.appspot.com # help:
########################################################################################### description: te téléporte au Warp d'un autre joueur
parameters: "<pseudo>"
warp: warps:
help:
description: "te téléporte au Warp d'un autre joueur"
parameters: <pseudo>
warps:
deactivate: "&cAncien panneau de Warp désactivé !" deactivate: "&cAncien panneau de Warp désactivé !"
error: error:
does-not-exist: "&cCe Warp n'existe plus !" does-not-exist: "&cCe Warp n'existe plus !"
no-permission: "&cVous n'avez pas la permission pour faire cela !" no-permission: "&cVous n'avez pas la permission pour faire cela !"
no-remove: "&cVous ne pouvez pas supprimer ce panneau !" no-remove: "&cVous ne pouvez pas supprimer ce panneau !"
no-warps-yet: "&cIl n'y a encore aucun Warp sur ce serveur." not-enough-level: "&cVotre niveau d'île n'est pas assez élevé pour faire cela
not-enough-level: "&cVotre niveau d'île n'est pas assez élevé pour faire cela !" !"
not-on-island: "&cVous devez être sur votre île pour faire cela !" not-on-island: "&cVous devez être sur votre île pour faire cela !"
not-safe: "&cCe Warp n'est pas sûr!" not-safe: "&cCe Warp n'est pas sûr!"
no-warps-yet: "&cIl n'y a encore aucun Warp sur ce serveur."
your-level-is: "&cVotre île est seulement niveau [level] et doit être niveau [required]." your-level-is: "&cVotre île est seulement niveau [level] et doit être niveau [required]."
help: help:
description: "Ouvre le menu des Warps" description: Ouvre le menu des Warps
next: "&6Page suivante" next: "&6Page suivante"
player-warped: "&2[name] s'est téléporté sur votre île !" player-warped: "&2[name] s'est téléporté sur votre île !"
previous: "&6Page précédente" previous: "&6Page précédente"
sign-removed: "&cPanneau de Warp supprimé !" sign-removed: "&cPanneau de Warp supprimé !"
success: "&aSuccès !" success: "&aSuccès !"
title: "Panneau Warp" title: Panneau Warp
warpTip: "&6Placez un panneau et écrivez [text] sur la première ligne." warpTip: "&6Placez un panneau et écrivez [text] sur la première ligne."
warpToPlayersSign: "&6Téléportation sur l'île de [player]..." warpToPlayersSign: "&6Téléportation sur l'île de [player]..."
random: "&4Warp aléatoire"

View File

@ -9,10 +9,10 @@ warps:
does-not-exist: "&cAk vai! Uzaicinājuma zīme vairāk neeksistē!" does-not-exist: "&cAk vai! Uzaicinājuma zīme vairāk neeksistē!"
no-permission: "&CTev nav nepieciešamās atļaujas, lai veiktu darbību!" no-permission: "&CTev nav nepieciešamās atļaujas, lai veiktu darbību!"
no-remove: "&CTu nevari noņemt šo zīmi!" no-remove: "&CTu nevari noņemt šo zīmi!"
no-warps-yet: "&CNav neviena aktīva uzaicinājuma zīme."
not-enough-level: "&CTavas salas līmenis ir pārāk zems!" not-enough-level: "&CTavas salas līmenis ir pārāk zems!"
not-on-island: "&CTev ir jāatrodas uz savas salas, lai veiktu šo darbību!" not-on-island: "&CTev ir jāatrodas uz savas salas, lai veiktu šo darbību!"
not-safe: "&cŠī uzaicinājuma zīme nav droša!" not-safe: "&cŠī uzaicinājuma zīme nav droša!"
no-warps-yet: "&CNav neviena aktīva uzaicinājuma zīme."
your-level-is: "&cTavas salas līmenis ir tikai [level], bet ir nepieciešams vismaz your-level-is: "&cTavas salas līmenis ir tikai [level], bet ir nepieciešams vismaz
[required]. Vari mēģināt pārrēķināt savas salas līmeni." [required]. Vari mēģināt pārrēķināt savas salas līmeni."
help: help:
@ -25,3 +25,4 @@ warps:
title: Uzaicinājuma Zīmes title: Uzaicinājuma Zīmes
warpTip: "&6Novieto zīmi ar tekstu [text] pirmajā rindā" warpTip: "&6Novieto zīmi ar tekstu [text] pirmajā rindā"
warpToPlayersSign: "&6Pārvietojās uz [player] uzaicinājuma zīmes" warpToPlayersSign: "&6Pārvietojās uz [player] uzaicinājuma zīmes"
random: "&4Nejauša Zīmes Izvēle"

View File

@ -1,32 +1,27 @@
########################################################################################### ---
# 本文件是 YML 格式的文件. 编辑时请务必小心. # warp:
# 请在 http://yaml-online-parser.appspot.com 等 YAML 检查器中检查您的编辑. # help:
###########################################################################################
warp:
help:
description: 传送到该玩家的传送木牌处 description: 传送到该玩家的传送木牌处
parameters: <玩家名称> parameters: "<player name>"
warps: warps:
deactivate: "&c禁用的旧转移标志!" error:
error:
does-not-exist: "&c转移不再存在!"
no-permission: "&C权限不足!" no-permission: "&C权限不足!"
no-remove: "&C无权移除传送木牌!" no-remove: "&C无权移除传送木牌!"
no-warps-yet: "&C暂无可用传送木牌"
not-enough-level: "&C岛屿等级不够高!" not-enough-level: "&C岛屿等级不够高!"
not-on-island: "&C操作必须在空岛上进行!" no-warps-yet: "&C暂无可用传送木牌"
not-safe: "&c转移不安全!"
your-level-is: "&c岛屿当前等级 [level], 需要等级 [required]" your-level-is: "&c岛屿当前等级 [level], 需要等级 [required]"
help: does-not-exist: "&c那个传送已经不存在了"
not-on-island: "&C操作必须在你的岛屿上进行!"
not-safe: "&c那个传送不安全"
help:
description: 打开传送面板 description: 打开传送面板
next: "&6下一页" next: "&6下一页"
player-warped: "&2[name]转移到你的标志!"
previous: "&6上一页" previous: "&6上一页"
sign-removed: "&C传送木牌已移除!" sign-removed: "&C传送木牌已移除!"
success: "&A成功!" success: "&A成功!"
title: 传送木牌 title: 传送木牌
warpTip: "&6放置一个第一行是 [text] 的木牌以创建传送木牌" warpTip: "&6放置一个第一行是 [text] 的木牌以创建传送木牌"
warpToPlayersSign: "&6正传送到 [player] 的传送木牌" warpToPlayersSign: "&6正传送到 [player] 的传送木牌"
deactivate: "&c旧传送牌已不再使用"
player-warped: "&2[name] 刚刚传送到了你的传送牌!"
random: "&4随机传送"

View File

@ -1,9 +1,12 @@
package world.bentobox.warps; package world.bentobox.warps;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -23,6 +26,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
@ -57,6 +61,8 @@ public class WarpPanelManagerTest {
@Mock @Mock
private Inventory top; private Inventory top;
private UUID uuid; private UUID uuid;
@Mock
private Settings settings;
/** /**
* @throws java.lang.Exception * @throws java.lang.Exception
@ -104,7 +110,6 @@ public class WarpPanelManagerTest {
when(Bukkit.createInventory(any(), Mockito.anyInt(), any())).thenReturn(top); when(Bukkit.createInventory(any(), Mockito.anyInt(), any())).thenReturn(top);
Settings settings = mock(Settings.class);
when(settings.getIcon()).thenReturn("SIGN"); when(settings.getIcon()).thenReturn("SIGN");
when(addon.getSettings()).thenReturn(settings); when(addon.getSettings()).thenReturn(settings);
@ -133,11 +138,43 @@ public class WarpPanelManagerTest {
*/ */
@Test @Test
public void testShowWarpPanelFirst() { public void testShowWarpPanelFirst() {
ArgumentCaptor<ItemStack> argument = ArgumentCaptor.forClass(ItemStack.class);
WarpPanelManager wpm = new WarpPanelManager(addon); WarpPanelManager wpm = new WarpPanelManager(addon);
wpm.showWarpPanel(world, user, 0); wpm.showWarpPanel(world, user, 0);
verify(player).openInventory(Mockito.eq(top)); verify(player).openInventory(Mockito.eq(top));
// Just next sign // Just next sign
verify(top, Mockito.times(53)).setItem(Mockito.anyInt(), Mockito.any(ItemStack.class)); verify(top, Mockito.times(53)).setItem(Mockito.anyInt(),argument.capture());
assertEquals(Material.STONE, argument.getAllValues().get(52).getType());
}
/**
* Test method for {@link WarpPanelManager#showWarpPanel(org.bukkit.World, world.bentobox.bbox.api.user.User, int)}.
*/
@Test
public void testShowWarpPanelFirstRandom() {
when(settings.isRandomAllowed()).thenReturn(true);
ArgumentCaptor<ItemStack> argument = ArgumentCaptor.forClass(ItemStack.class);
WarpPanelManager wpm = new WarpPanelManager(addon);
wpm.showWarpPanel(world, user, 0);
verify(player).openInventory(Mockito.eq(top));
// Check crystal
verify(top, Mockito.atLeastOnce()).setItem(anyInt(), argument.capture());
assertEquals(Material.END_CRYSTAL, argument.getAllValues().get(0).getType());
}
/**
* Test method for {@link WarpPanelManager#showWarpPanel(org.bukkit.World, world.bentobox.bbox.api.user.User, int)}.
*/
@Test
public void testShowWarpPanelNoRandom() {
when(settings.isRandomAllowed()).thenReturn(false);
ArgumentCaptor<ItemStack> argument = ArgumentCaptor.forClass(ItemStack.class);
WarpPanelManager wpm = new WarpPanelManager(addon);
wpm.showWarpPanel(world, user, 0);
verify(player).openInventory(Mockito.eq(top));
// Check crystal
verify(top, Mockito.atLeastOnce()).setItem(anyInt(), argument.capture());
assertFalse(argument.getAllValues().get(0).getType().equals(Material.END_CRYSTAL));
} }
/** /**
@ -145,11 +182,14 @@ public class WarpPanelManagerTest {
*/ */
@Test @Test
public void testShowWarpPanelMiddle() { public void testShowWarpPanelMiddle() {
ArgumentCaptor<ItemStack> argument = ArgumentCaptor.forClass(ItemStack.class);
WarpPanelManager wpm = new WarpPanelManager(addon); WarpPanelManager wpm = new WarpPanelManager(addon);
wpm.showWarpPanel(world, user, 1); wpm.showWarpPanel(world, user, 1);
verify(player).openInventory(Mockito.eq(top)); verify(player).openInventory(Mockito.eq(top));
// includes previous and next signs // includes previous and next signs
verify(top, Mockito.times(54)).setItem(Mockito.anyInt(), Mockito.any(ItemStack.class)); verify(top, Mockito.times(54)).setItem(Mockito.anyInt(), argument.capture());
assertEquals(Material.STONE, argument.getAllValues().get(52).getType());
assertEquals(Material.COBBLESTONE, argument.getAllValues().get(53).getType());
} }
/** /**
@ -157,11 +197,13 @@ public class WarpPanelManagerTest {
*/ */
@Test @Test
public void testShowWarpPanelLast() { public void testShowWarpPanelLast() {
ArgumentCaptor<ItemStack> argument = ArgumentCaptor.forClass(ItemStack.class);
WarpPanelManager wpm = new WarpPanelManager(addon); WarpPanelManager wpm = new WarpPanelManager(addon);
wpm.showWarpPanel(world, user, 3); wpm.showWarpPanel(world, user, 3);
verify(player).openInventory(Mockito.eq(top)); verify(player).openInventory(Mockito.eq(top));
// Final amount, just previous sign // Final amount, just previous sign
verify(top, Mockito.times(46)).setItem(Mockito.anyInt(), Mockito.any(ItemStack.class)); verify(top, Mockito.times(46)).setItem(Mockito.anyInt(), argument.capture());
assertEquals(Material.COBBLESTONE, argument.getAllValues().get(45).getType());
} }
/** /**

View File

@ -10,6 +10,7 @@ import static org.mockito.Mockito.when;
import static org.mockito.Mockito.any; import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.never;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -79,6 +80,8 @@ public class WarpSignsListenerTest {
private Settings settings; private Settings settings;
@Mock @Mock
private IslandsManager im; private IslandsManager im;
@Mock
private IslandWorldManager iwm;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -121,6 +124,7 @@ public class WarpSignsListenerTest {
Location location = mock(Location.class); Location location = mock(Location.class);
when(location.getBlock()).thenReturn(block); when(location.getBlock()).thenReturn(block);
when(s.getLocation()).thenReturn(location); when(s.getLocation()).thenReturn(location);
when(block.getLocation()).thenReturn(location);
list.put(uuid, location); list.put(uuid, location);
// Player is in world // Player is in world
when(wsm.getWarpMap(Mockito.eq(world))).thenReturn(list); when(wsm.getWarpMap(Mockito.eq(world))).thenReturn(list);
@ -155,9 +159,10 @@ public class WarpSignsListenerTest {
// Sufficient level // Sufficient level
when(addon.getLevel(any(), any())).thenReturn(100L); when(addon.getLevel(any(), any())).thenReturn(100L);
IslandWorldManager iwm = mock(IslandWorldManager.class); // IWM
when(plugin.getIWM()).thenReturn(iwm); when(plugin.getIWM()).thenReturn(iwm);
when(iwm.getAddon(any())).thenReturn(Optional.empty()); when(iwm.getAddon(any())).thenReturn(Optional.empty());
when(iwm.inWorld(any(World.class))).thenReturn(true);
// Util // Util
PowerMockito.mockStatic(Util.class); PowerMockito.mockStatic(Util.class);
@ -190,7 +195,7 @@ public class WarpSignsListenerTest {
} }
@Test @Test
public void testOnSignBreakWrongWorld() { public void testOnSignNotGameWorld() {
WarpSignsListener wsl = new WarpSignsListener(addon); WarpSignsListener wsl = new WarpSignsListener(addon);
BlockBreakEvent e = new BlockBreakEvent(block, player); BlockBreakEvent e = new BlockBreakEvent(block, player);
when(addon.inRegisteredWorld(any())).thenReturn(false); when(addon.inRegisteredWorld(any())).thenReturn(false);
@ -199,16 +204,6 @@ public class WarpSignsListenerTest {
verify(addon).inRegisteredWorld(Mockito.eq(world)); verify(addon).inRegisteredWorld(Mockito.eq(world));
} }
@Test
public void testOnSignBreakNullState() {
WarpSignsListener wsl = new WarpSignsListener(addon);
BlockBreakEvent e = new BlockBreakEvent(block, player);
when(block.getState()).thenReturn(null);
wsl.onSignBreak(e);
assertFalse(e.isCancelled());
verify(block).getState();
}
@Test @Test
public void testOnSignNotWelcomeSign() { public void testOnSignNotWelcomeSign() {
WarpSignsListener wsl = new WarpSignsListener(addon); WarpSignsListener wsl = new WarpSignsListener(addon);
@ -283,7 +278,7 @@ public class WarpSignsListenerTest {
* Sign create * Sign create
*/ */
@Test @Test
public void testOnCreateWrongWorld() { public void testOnCreateWrongWorldGameWorld() {
when(player.hasPermission(anyString())).thenReturn(true); when(player.hasPermission(anyString())).thenReturn(true);
WarpSignsListener wsl = new WarpSignsListener(addon); WarpSignsListener wsl = new WarpSignsListener(addon);
SignChangeEvent e = new SignChangeEvent(block, player, lines); SignChangeEvent e = new SignChangeEvent(block, player, lines);
@ -292,6 +287,43 @@ public class WarpSignsListenerTest {
verify(addon).inRegisteredWorld(Mockito.eq(world)); verify(addon).inRegisteredWorld(Mockito.eq(world));
} }
@Test
public void testOnCreateNotGameWorldAllowed() {
when(settings.isAllowInOtherWorlds()).thenReturn(true);
when(iwm.inWorld(any(World.class))).thenReturn(false);
when(player.hasPermission(anyString())).thenReturn(true);
WarpSignsListener wsl = new WarpSignsListener(addon);
SignChangeEvent e = new SignChangeEvent(block, player, lines);
when(addon.inRegisteredWorld(any())).thenReturn(false);
wsl.onSignWarpCreate(e);
verify(player).sendMessage("warps.success");
assertEquals(ChatColor.GREEN + "[WELCOME]", e.getLine(0));
}
@Test
public void testOnCreateNotGameWorldNotAllowed() {
when(settings.isAllowInOtherWorlds()).thenReturn(false);
when(iwm.inWorld(any(World.class))).thenReturn(false);
when(player.hasPermission(anyString())).thenReturn(true);
WarpSignsListener wsl = new WarpSignsListener(addon);
SignChangeEvent e = new SignChangeEvent(block, player, lines);
when(addon.inRegisteredWorld(any())).thenReturn(false);
wsl.onSignWarpCreate(e);
verify(player, never()).sendMessage("warps.success");
}
@Test
public void testOnCreateNotGameWorldNoPerm() {
when(settings.isAllowInOtherWorlds()).thenReturn(true);
when(iwm.inWorld(any(World.class))).thenReturn(false);
when(player.hasPermission(anyString())).thenReturn(false);
WarpSignsListener wsl = new WarpSignsListener(addon);
SignChangeEvent e = new SignChangeEvent(block, player, lines);
when(addon.inRegisteredWorld(any())).thenReturn(false);
wsl.onSignWarpCreate(e);
verify(player).sendMessage("warps.error.no-permission");
}
@Test @Test
public void testOnCreateWrongText() { public void testOnCreateWrongText() {
when(player.hasPermission(anyString())).thenReturn(true); when(player.hasPermission(anyString())).thenReturn(true);

View File

@ -0,0 +1,450 @@
package world.bentobox.warps;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.AbstractDatabaseHandler;
import world.bentobox.bentobox.database.DatabaseSetup;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.util.Util;
import world.bentobox.warps.config.Settings;
import world.bentobox.warps.event.WarpInitiateEvent;
import world.bentobox.warps.objects.WarpsData;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, Util.class, DatabaseSetup.class})
public class WarpSignsManagerTest {
@Mock
private Warp addon;
@Mock
private BentoBox plugin;
@Mock
private World world;
@Mock
private static AbstractDatabaseHandler<Object> handler;
private WarpSignsManager wsm;
@Mock
private Logger logger;
@Mock
private WarpsData load;
private UUID uuid = UUID.randomUUID();
@Mock
private Location location;
@Mock
private Block block;
@Mock
private PluginManager pim;
@Mock
private Server server;
@Mock
private Player player;
@Mock
private WarpPanelManager wpm;
@Mock
private PlayersManager pm;
@Mock
private OfflinePlayer offlinePlayer;
@Mock
private Settings settings;
@Mock
private IslandWorldManager iwm;
@Mock
private IslandsManager im;
@Mock
private Island island;
@SuppressWarnings("unchecked")
@BeforeClass
public static void beforeClass() {
// This has to be done beforeClass otherwise the tests will interfere with each other
handler = mock(AbstractDatabaseHandler.class);
// Database
PowerMockito.mockStatic(DatabaseSetup.class);
DatabaseSetup dbSetup = mock(DatabaseSetup.class);
when(DatabaseSetup.getDatabase()).thenReturn(dbSetup);
when(dbSetup.getHandler(any())).thenReturn(handler);
}
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
when(addon.getPlugin()).thenReturn(plugin);
when(addon.getLogger()).thenReturn(logger);
// Player
when(player.getUniqueId()).thenReturn(uuid);
User.getInstance(player);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(lm.get(Mockito.any(), Mockito.any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
when(plugin.getLocalesManager()).thenReturn(lm);
// Return the same string
PlaceholdersManager phm = mock(PlaceholdersManager.class);
when(phm.replacePlaceholders(any(), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
when(plugin.getPlaceholdersManager()).thenReturn(phm);
// Server
when(addon.getServer()).thenReturn(server);
when(server.getPlayer(any(UUID.class))).thenReturn(player);
// Util
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenAnswer((Answer<World>) invocation -> invocation.getArgument(0, World.class));
when(Util.sameWorld(any(), any())).thenReturn(true);
// Location
when(location.getWorld()).thenReturn(world);
when(location.getBlock()).thenReturn(block);
when(location.getBlockX()).thenReturn(23);
when(location.getBlockY()).thenReturn(24);
when(location.getBlockZ()).thenReturn(25);
when(world.getEnvironment()).thenReturn(Environment.NORMAL);
// Block
when(block.getType()).thenReturn(Material.ACACIA_SIGN);
when(block.getLocation()).thenReturn(location);
Sign sign = mock(Sign.class);
String[] lines = {"[Welcome]", "line2", "line3", "line4"};
when(sign.getLines()).thenReturn(lines);
when(sign.getLine(anyInt())).thenReturn("[Welcome]");
when(sign.getType()).thenReturn(Material.ACACIA_SIGN);
when(block.getState()).thenReturn(sign);
org.bukkit.block.data.type.Sign signBd = mock(org.bukkit.block.data.type.Sign.class);
when(signBd.getRotation()).thenReturn(BlockFace.EAST);
when(block.getBlockData()).thenReturn(signBd);
when(block.getRelative(any())).thenReturn(block);
// Handler
when(handler.objectExists(eq("warps"))).thenReturn(true);
Map<Location, UUID> warpMap = Collections.singletonMap(location, uuid);
when(load.getWarpSigns()).thenReturn(warpMap);
when(handler.loadObject(anyString())).thenReturn(load);
// Settings
when(addon.getSettings()).thenReturn(settings);
when(settings.getWelcomeLine()).thenReturn("[Welcome]");
when(settings.getLoreFormat()).thenReturn("&f");
when(settings.getIcon()).thenReturn("SIGN");
// Bukkit
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getPluginManager()).thenReturn(pim);
// Players Manager
when(plugin.getPlayers()).thenReturn(pm);
when(pm.getName(eq(uuid))).thenReturn("tastybento");
// Offline player
when(server.getOfflinePlayer(any(UUID.class))).thenReturn(offlinePlayer);
when(offlinePlayer.getLastPlayed()).thenReturn(System.currentTimeMillis());
// IWM
when(plugin.getIWM()).thenReturn(iwm);
when(iwm.getPermissionPrefix(any())).thenReturn("bskyblock.");
// Island Manager
when(addon.getIslands()).thenReturn(im);
when(im.getIsland(any(), any(UUID.class))).thenReturn(island);
when(im.isSafeLocation(any())).thenReturn(true);
// WarpPanelManager
when(addon.getWarpPanelManager()).thenReturn(wpm);
// User
wsm = new WarpSignsManager(addon, plugin);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
User.clearUsers();
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#getWarpMap(org.bukkit.World)}.
*/
@Test
public void testGetWarpMap() {
assertFalse("Map is empty", wsm.getWarpMap(world).isEmpty());
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#getWarpMap(org.bukkit.World)}.
*/
@Test
public void testGetWarpMapNullWorld() {
when(location.getWorld()).thenReturn(null);
wsm = new WarpSignsManager(addon, plugin);
assertTrue("Map is not empty", wsm.getWarpMap(world).isEmpty());
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#getWarpMap(org.bukkit.World)}.
*/
@Test
public void testGetWarpMapWrongBlockType() {
when(block.getType()).thenReturn(Material.COAL_ORE);
wsm = new WarpSignsManager(addon, plugin);
assertTrue("Map is not empty", wsm.getWarpMap(world).isEmpty());
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#getWarpMap(org.bukkit.World)}.
*/
@Test
public void testGetWarpMapNullLocation() {
Map<Location, UUID> warpMap = Collections.singletonMap(null, uuid);
when(load.getWarpSigns()).thenReturn(warpMap);
wsm = new WarpSignsManager(addon, plugin);
assertTrue("Map is not empty", wsm.getWarpMap(world).isEmpty());
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#getWarpMap(org.bukkit.World)}.
* @throws Exception
*/
@Test
public void testGetWarpMapNullDatabaseObject() throws Exception {
when(handler.loadObject(anyString())).thenReturn(null);
wsm = new WarpSignsManager(addon, plugin);
assertTrue("Map is not empty", wsm.getWarpMap(world).isEmpty());
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#getWarpMap(org.bukkit.World)}.
*/
@Test
public void testGetWarpMapNothingInDatabase() {
when(handler.objectExists(eq("warps"))).thenReturn(false);
wsm = new WarpSignsManager(addon, plugin);
assertTrue("Map is not empty", wsm.getWarpMap(world).isEmpty());
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#WarpSignsManager(world.bentobox.warps.Warp, world.bentobox.bentobox.BentoBox)}.
* @throws Exception
*/
@Test
public void testWarpSignsManager() throws Exception {
verify(logger).info("Loading warps...");
verify(load).getWarpSigns();
verify(block).getType();
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#addWarp(java.util.UUID, org.bukkit.Location)}.
*/
@Test
public void testAddWarpNullPlayer() {
assertFalse(wsm.addWarp(null, null));
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#addWarp(java.util.UUID, org.bukkit.Location)}.
*/
@Test
public void testAddWarpNullLocation() {
assertFalse(wsm.addWarp(uuid, null));
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#addWarp(java.util.UUID, org.bukkit.Location)}.
*/
@Test
public void testAddWarpReplaceOldSign() {
assertTrue(wsm.addWarp(uuid, location));
verify(player).sendMessage("warps.sign-removed");
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#addWarp(java.util.UUID, org.bukkit.Location)}.
*/
@Test
public void testAddWarpReplaceOldSignDifferentPlayer() {
assertTrue(wsm.addWarp(UUID.randomUUID(), location));
verify(player).sendMessage("warps.sign-removed");
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#addWarp(java.util.UUID, org.bukkit.Location)}.
*/
@Test
public void testAddWarp() {
Location loc = mock(Location.class);
assertTrue(wsm.addWarp(uuid, loc));
verify(pim).callEvent(any(WarpInitiateEvent.class));
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#getWarp(org.bukkit.World, java.util.UUID)}.
*/
@Test
public void testGetWarpWorldWorld() {
assertNull(wsm.getWarp(mock(World.class), uuid));
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#getWarp(org.bukkit.World, java.util.UUID)}.
*/
@Test
public void testGetWarp() {
assertEquals(location, wsm.getWarp(world, uuid));
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#getWarpOwner(org.bukkit.Location)}.
*/
@Test
public void testGetWarpOwner() {
assertEquals("tastybento", wsm.getWarpOwner(location));
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#getSortedWarps(org.bukkit.World)}.
*/
@Test
public void testGetSortedWarps() {
assertEquals(1, wsm.getSortedWarps(world).size());
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#listWarps(org.bukkit.World)}.
*/
@Test
public void testListWarps() {
assertEquals(1, wsm.listWarps(world).size());
assertEquals(uuid, wsm.listWarps(world).toArray()[0]);
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#removeWarp(org.bukkit.Location)}.
*/
@Test
public void testRemoveWarpLocation() {
wsm.removeWarp(location);
assertTrue(wsm.listWarps(world).isEmpty());
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#removeWarp(org.bukkit.World, java.util.UUID)}.
*/
@Test
public void testRemoveWarpWorldUUID() {
wsm.removeWarp(world, uuid);
assertTrue(wsm.listWarps(world).isEmpty());
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#saveWarpList()}.
* @throws IntrospectionException
* @throws InvocationTargetException
* @throws Exception
*/
@Test
public void testSaveWarpList() throws Exception {
wsm.saveWarpList();
verify(handler, Mockito.atLeastOnce()).saveObject(any());
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#getSignInfo(org.bukkit.World, java.util.UUID)}.
*/
@Test
public void testGetSignInfo() {
SignCache sc = wsm.getSignInfo(world, uuid);
assertEquals(Material.ACACIA_SIGN, sc.getType());
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#warpPlayer(org.bukkit.World, world.bentobox.bentobox.api.user.User, java.util.UUID)}.
*/
@Test
public void testWarpPlayer() {
Player p = mock(Player.class);
when(p.getUniqueId()).thenReturn(UUID.randomUUID());
when(p.getWorld()).thenReturn(world);
when(p.getName()).thenReturn("tastybento");
@Nullable
User u = User.getInstance(p);
wsm.warpPlayer(world, u, uuid);
verify(p).teleport(any(Location.class));
verify(player).sendMessage("warps.player-warped");
}
/**
* Test method for {@link world.bentobox.warps.WarpSignsManager#hasWarp(org.bukkit.World, java.util.UUID)}.
*/
@Test
public void testHasWarp() {
assertTrue(wsm.hasWarp(world, uuid));
assertFalse(wsm.hasWarp(mock(World.class), uuid));
assertFalse(wsm.hasWarp(world, UUID.randomUUID()));
}
}

View File

@ -0,0 +1,236 @@
package world.bentobox.warps.commands;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
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.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.warps.Warp;
import world.bentobox.warps.WarpSignsManager;
import world.bentobox.warps.config.Settings;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class})
public class WarpCommandTest {
private static final String WELCOME_LINE = "[Welcome]";
@Mock
private CompositeCommand ic;
private UUID uuid;
@Mock
private User user;
@Mock
private World world;
@Mock
private IslandWorldManager iwm;
@Mock
private Warp addon;
// Command under test
private WarpCommand wc;
@Mock
private Settings settings;
@Mock
private WarpSignsManager wsm;
@Mock
private PlayersManager pm;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Addon
when(ic.getAddon()).thenReturn(addon);
when(ic.getPermissionPrefix()).thenReturn("bskyblock.");
when(ic.getWorld()).thenReturn(world);
// IWM friendly name
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
// Player
uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
when(user.getWorld()).thenReturn(world);
// settings
when(addon.getSettings()).thenReturn(settings);
when(settings.getWarpCommand()).thenReturn("warp");
when(settings.getWelcomeLine()).thenReturn(WELCOME_LINE);
// Warp Signs Manager
when(addon.getWarpSignsManager()).thenReturn(wsm);
@NonNull
Set<UUID> set = new HashSet<>();
set.add(UUID.randomUUID());
set.add(UUID.randomUUID());
set.add(UUID.randomUUID());
when(wsm.listWarps(eq(world))).thenReturn(set);
// Players Manager
when(plugin.getPlayers()).thenReturn(pm);
when(addon.getPlayers()).thenReturn(pm);
// Repeat twice because it is asked twice
when(pm.getName(any())).thenReturn("tastybento", "tastybento", "poslovich", "poslovich", "BONNe", "BONNe", "Joe");
}
public void warpCommandWarpCompositeCommand() {
// Command under test
wc = new WarpCommand(addon, ic);
}
public void warpCommandWarp() {
// Command under test
wc = new WarpCommand(addon);
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpCommand#setup()}.
*/
@Test
public void testSetupWarpCompositeCommand() {
warpCommandWarpCompositeCommand();
assertEquals("bskyblock.island.warp", wc.getPermission());
assertTrue(wc.isOnlyPlayer());
assertEquals("warp.help.parameters", wc.getParameters());
assertEquals("warp.help.description", wc.getDescription());
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpCommand#setup()}.
*/
@Test
public void testSetupWarp() {
warpCommandWarp();
assertEquals(Warp.WELCOME_WARP_SIGNS + ".warp", wc.getPermission());
assertTrue(wc.isOnlyPlayer());
assertEquals("warp.help.parameters", wc.getParameters());
assertEquals("warp.help.description", wc.getDescription());
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoArgs() {
warpCommandWarpCompositeCommand();
wc.execute(user, "warp", Collections.emptyList());
verify(user).sendMessage(eq("commands.help.header"), eq(TextVariables.LABEL), eq("BSkyBlock"));
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringKnownPlayer() {
warpCommandWarpCompositeCommand();
assertTrue(wc.execute(user, "warp", Collections.singletonList("tastybento")));
verify(wsm).warpPlayer(eq(world), eq(user), any());
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringKnownPlayerWarp() {
warpCommandWarp();
assertTrue(wc.execute(user, "warp", Collections.singletonList("tastybento")));
verify(wsm).warpPlayer(eq(world), eq(user), any());
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringKnownPlayerMixedCase() {
warpCommandWarpCompositeCommand();
assertTrue(wc.execute(user, "warp", Collections.singletonList("tAsTyBEnTo")));
verify(wsm).warpPlayer(eq(world), eq(user), any());
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringKnownPlayerStartOnly() {
warpCommandWarpCompositeCommand();
assertTrue(wc.execute(user, "warp", Collections.singletonList("tAsTy")));
verify(wsm).warpPlayer(eq(world), eq(user), any());
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringUnknownPlayer() {
warpCommandWarpCompositeCommand();
assertFalse(wc.execute(user, "warp", Collections.singletonList("LSPVicky")));
verify(user).sendMessage(eq("warps.error.does-not-exist"));
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoWarpsYet() {
when(wsm.listWarps(eq(world))).thenReturn(Collections.emptySet());
warpCommandWarpCompositeCommand();
assertFalse(wc.execute(user, "warp", Collections.singletonList("LSPVicky")));
verify(user).sendMessage(eq("warps.error.no-warps-yet"));
verify(user).sendMessage(eq("warps.warpTip"), eq("[text]"), eq(WELCOME_LINE));
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabCompleteUserStringListOfString() {
warpCommandWarpCompositeCommand();
List<String> op = wc.tabComplete(user, "warp", Collections.singletonList("tas")).get();
assertEquals("tastybento", op.get(0));
assertEquals("tastybento", op.get(1));
assertEquals("poslovich", op.get(2));
}
}

View File

@ -0,0 +1,186 @@
package world.bentobox.warps.commands;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.warps.Warp;
import world.bentobox.warps.WarpPanelManager;
import world.bentobox.warps.WarpSignsManager;
import world.bentobox.warps.config.Settings;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class})
public class WarpsCommandTest {
private static final String WELCOME_LINE = "[Welcome]";
@Mock
private CompositeCommand ic;
@Mock
private User user;
@Mock
private World world;
@Mock
private IslandWorldManager iwm;
@Mock
private Warp addon;
// Command under test
private WarpsCommand wc;
@Mock
private Settings settings;
@Mock
private WarpSignsManager wsm;
@Mock
private PlayersManager pm;
@Mock
private WarpPanelManager wpm;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Addon
when(ic.getAddon()).thenReturn(addon);
when(ic.getPermissionPrefix()).thenReturn("bskyblock.");
// World
when(world.toString()).thenReturn("world");
// Player
when(user.getWorld()).thenReturn(world);
// settings
when(addon.getSettings()).thenReturn(settings);
when(settings.getWarpsCommand()).thenReturn("warps");
when(settings.getWelcomeLine()).thenReturn(WELCOME_LINE);
// Warp Signs Manager
when(addon.getWarpSignsManager()).thenReturn(wsm);
@NonNull
Set<UUID> set = new HashSet<>();
set.add(UUID.randomUUID());
set.add(UUID.randomUUID());
set.add(UUID.randomUUID());
when(wsm.listWarps(eq(world))).thenReturn(set);
// Warp Panel Manager
when(addon.getWarpPanelManager()).thenReturn(wpm);
}
public void warpCommandWarpsCompositeCommand() {
// Command under test
wc = new WarpsCommand(addon, ic);
}
public void warpCommandWarps() {
// Command under test
wc = new WarpsCommand(addon);
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpsCommand#setup()}.
*/
@Test
public void testSetupWarpCompositeCommand() {
warpCommandWarpsCompositeCommand();
assertEquals("bskyblock.island.warp", wc.getPermission());
assertTrue(wc.isOnlyPlayer());
assertEquals("warps.help.description", wc.getDescription());
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpsCommand#setup()}.
*/
@Test
public void testSetupWarp() {
warpCommandWarps();
assertEquals(Warp.WELCOME_WARP_SIGNS + ".warp", wc.getPermission());
assertTrue(wc.isOnlyPlayer());
assertEquals("warps.help.description", wc.getDescription());
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpsCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoWarpsYet() {
when(wsm.listWarps(eq(world))).thenReturn(Collections.emptySet());
warpCommandWarpsCompositeCommand();
assertFalse(wc.execute(user, "warps", Collections.emptyList()));
verify(user).sendMessage(eq("warps.error.no-warps-yet"));
verify(user).sendMessage(eq("warps.warpTip"), eq("[text]"), eq(WELCOME_LINE));
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpsCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoWarpsYetNoAddon() {
when(wsm.listWarps(eq(world))).thenReturn(Collections.emptySet());
warpCommandWarps();
assertFalse(wc.execute(user, "warps", Collections.emptyList()));
verify(user).sendMessage(eq("warps.error.no-warps-yet"));
verify(user).sendMessage(eq("warps.warpTip"), eq("[text]"), eq(WELCOME_LINE));
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpsCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfString() {
warpCommandWarpsCompositeCommand();
assertTrue(wc.execute(user, "warps", Collections.emptyList()));
verify(wpm).showWarpPanel(world, user, 0);
}
/**
* Test method for {@link world.bentobox.warps.commands.WarpsCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoAddon() {
warpCommandWarps();
assertTrue(wc.execute(user, "warps", Collections.emptyList()));
verify(wpm).showWarpPanel(world, user, 0);
}
}