Fixed EnterExitListener to support non-set Island's name

When an Island's name was reset (set to `null`) it was breaking EnterExitListener.
This commit is contained in:
Florian CUNY 2018-08-13 16:08:05 +02:00
parent a29a2ccd8c
commit 353c87cf6d
2 changed files with 17 additions and 12 deletions

View File

@ -62,7 +62,7 @@ public class Island implements DataObject {
// Display name // Display name
@Expose @Expose
private String name = ""; private String name;
// Time parameters // Time parameters
@Expose @Expose
@ -232,7 +232,7 @@ public class Island implements DataObject {
} }
/** /**
* @return the island display name or the owner's name if none is set * @return the island display name. Might be {@code null} if none is set.
*/ */
public String getName() { public String getName() {
return name; return name;
@ -472,11 +472,13 @@ public class Island implements DataObject {
} }
/** /**
* @param name - the display name to set * Sets the display name of this Island.
* Set to null to remove the display name * <br/><br/>
* An empty String or {@code null} will remove the display name.
* @param name The display name to set.
*/ */
public void setName(String name){ public void setName(String name){
this.name = name; this.name = (name != null && !name.equals("")) ? name : null;
} }
/** /**

View File

@ -11,6 +11,7 @@ import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import world.bentobox.bentobox.api.flags.AbstractFlagListener; import world.bentobox.bentobox.api.flags.AbstractFlagListener;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
@ -23,7 +24,6 @@ import world.bentobox.bentobox.lists.Flags;
public class EnterExitListener extends AbstractFlagListener { public class EnterExitListener extends AbstractFlagListener {
private static final Vector XZ = new Vector(1,0,1); private static final Vector XZ = new Vector(1,0,1);
private static final String NAME = "[name]";
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onMove(PlayerMoveEvent e) { public void onMove(PlayerMoveEvent e) {
@ -33,6 +33,7 @@ public class EnterExitListener extends AbstractFlagListener {
|| !Flags.ENTER_EXIT_MESSAGES.isSetForWorld(e.getFrom().getWorld())) { || !Flags.ENTER_EXIT_MESSAGES.isSetForWorld(e.getFrom().getWorld())) {
return; return;
} }
Optional<Island> from = this.getIslands().getProtectedIslandAt(e.getFrom()); Optional<Island> from = this.getIslands().getProtectedIslandAt(e.getFrom());
Optional<Island> to = this.getIslands().getProtectedIslandAt(e.getTo()); Optional<Island> to = this.getIslands().getProtectedIslandAt(e.getTo());
@ -48,14 +49,16 @@ public class EnterExitListener extends AbstractFlagListener {
if (from.equals(to)) { if (from.equals(to)) {
return; return;
} }
User user = User.getInstance(e.getPlayer()); User user = User.getInstance(e.getPlayer());
// Send message if island is owned by someone // Send message if island is owned by someone
from.filter(i -> i.getOwner() != null).ifPresent(i -> user.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-leaving", NAME, !i.getName().isEmpty() ? i.getName() : from.filter(i -> i.getOwner() != null).ifPresent(i -> user.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-leaving", TextVariables.NAME, (i.getName() != null) ? i.getName() :
user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", NAME, getPlugin().getPlayers().getName(i.getOwner())))); user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME, getPlugin().getPlayers().getName(i.getOwner()))));
to.filter(i -> i.getOwner() != null).ifPresent(i -> user.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-entering", NAME, !i.getName().isEmpty() ? i.getName() : to.filter(i -> i.getOwner() != null).ifPresent(i -> user.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-entering", TextVariables.NAME, (i.getName() != null) ? i.getName() :
user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", NAME, getPlugin().getPlayers().getName(i.getOwner())))); user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME, getPlugin().getPlayers().getName(i.getOwner()))));
// Send message if island is unowned, but has a name // Send message if island is unowned, but has a name
from.filter(i -> i.getOwner() == null && !i.getName().isEmpty()).ifPresent(i -> user.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-leaving", NAME, i.getName())); from.filter(i -> i.getOwner() == null && (i.getName() != null)).ifPresent(i -> user.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-leaving", TextVariables.NAME, i.getName()));
to.filter(i -> i.getOwner() == null && !i.getName().isEmpty()).ifPresent(i -> user.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-entering", NAME, i.getName())); to.filter(i -> i.getOwner() == null && (i.getName() != null)).ifPresent(i -> user.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-entering", TextVariables.NAME, i.getName()));
} }
} }