Skip to content

Commit

Permalink
Implement Visit Leave message.
Browse files Browse the repository at this point in the history
If player visits an island, then next time he leaves it, will send a message to all online members that player left it.

Fixes #21
  • Loading branch information
BONNe committed Jan 5, 2023
1 parent 3de17ef commit 1623b2b
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@
<!-- More visible way how to change dependency versions -->
<spigot.version>1.17-R0.1-SNAPSHOT</spigot.version>
<!-- BentoBox API version -->
<bentobox.version>1.20.0-SNAPSHOT</bentobox.version>
<bentobox.version>1.21.0</bentobox.version>
<bank.version>1.4.0</bank.version>
<vault.version>1.7</vault.version>
<!-- Panel Utils version -->
<panelutils.version>1.0.0</panelutils.version>
<panelutils.version>1.1.0</panelutils.version>
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
<!-- This allows to change between versions and snapshots. -->
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/world/bentobox/visit/VisitAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import world.bentobox.visit.commands.admin.VisitAdminCommand;
import world.bentobox.visit.commands.player.VisitPlayerCommand;
import world.bentobox.visit.configs.Settings;
import world.bentobox.visit.listeners.IslandLeaveListener;
import world.bentobox.visit.managers.VisitAddonManager;


Expand Down Expand Up @@ -136,6 +137,8 @@ public void onEnable()
this.registerFlag(ALLOW_VISITS_FLAG);
this.registerFlag(RECEIVE_VISIT_MESSAGE_FLAG);

this.registerListener(new IslandLeaveListener(this));

INSTANCE = this;
}
}
Expand Down
156 changes: 156 additions & 0 deletions src/main/java/world/bentobox/visit/listeners/IslandLeaveListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
//
// Created by BONNe
// Copyright - 2023
//


package world.bentobox.visit.listeners;


import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.visit.VisitAddon;
import world.bentobox.visit.utils.Constants;
import world.bentobox.visit.utils.Utils;


/**
* This listener sends chat message to island owners when visitor leaves the island.
*/
public class IslandLeaveListener implements Listener
{
/**
* Instantiates a new Island leave listener.
*
* @param addon the addon
*/
public IslandLeaveListener(VisitAddon addon)
{
this.addon = addon;
}


/**
* On move event listener
*
* @param event the player move event
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onMove(PlayerMoveEvent event)
{
this.handleExitMessage(User.getInstance(event.getPlayer()), event.getFrom(), event.getTo());
}


/**
* On teleport event listener.
*
* @param event the player teleport event
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onTeleport(PlayerTeleportEvent event)
{
this.handleExitMessage(User.getInstance(event.getPlayer()), event.getFrom(), event.getTo());
}


/**
* This method handles player exit island messaging.
* @param user User who need to be checked.
* @param from From location.
* @param to To location.
* @param event Player Move event.
*/
private void handleExitMessage(@NotNull User user,
@NotNull Location from,
@NotNull Location to)
{
if (!IslandLeaveListener.trackedPlayerSet.contains(user.getUniqueId()))
{
// This player is not visiting any island. Ignore it.
return;
}

// Only process if there is a change in X or Z coords
if (from.getWorld() != null &&
from.getWorld().equals(to.getWorld()) &&
from.toVector().multiply(XZ).equals(to.toVector().multiply(XZ)))
{
return;
}

Optional<Island> islandFrom = this.addon.getIslands().getProtectedIslandAt(from);
Optional<Island> islandTo = this.addon.getIslands().getProtectedIslandAt(to);

/*
* Options:
*
* from = empty, to = island - entering
* from = island1, to = island2 - leaving 1, entering 2
* from = island, to = empty - leaving
* from = empty, to = empty
* from = island, to = island
*/
if (islandFrom.equals(islandTo))
{
return;
}

// Now handle the messaging.
// Remove player from tracking set.
IslandLeaveListener.trackedPlayerSet.remove(user.getUniqueId());

// Send message to island members.
islandFrom.ifPresent(island -> {
if (!island.getMemberSet().contains(user.getUniqueId()) &&
island.isAllowed(VisitAddon.RECEIVE_VISIT_MESSAGE_FLAG))
{
// Send message that player is visiting the island.
island.getMemberSet().forEach(uuid ->
{
User member = User.getInstance(uuid);

if (member.isOnline())
{
Utils.sendMessage(member,
member.getTranslation(ISLAND_MESSAGE, Constants.PARAMETER_PLAYER, user.getName()));
}
});
}
});
}


/**
* Instance of visit addon.
*/
private final VisitAddon addon;

/**
* Set of tracked players.
*/
public static Set<UUID> trackedPlayerSet = new HashSet<>();

/**
* Coordinate normalization vector.
*/
private static final Vector XZ = new Vector(1,0,1);

/**
* The island send message constant.
*/
private static final String ISLAND_MESSAGE = Constants.CONVERSATIONS + "player-leaves-island";
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import world.bentobox.bentobox.util.teleport.SafeSpotTeleport;
import world.bentobox.visit.VisitAddon;
import world.bentobox.visit.events.VisitEvent;
import world.bentobox.visit.listeners.IslandLeaveListener;
import world.bentobox.visit.utils.Constants;
import world.bentobox.visit.utils.Utils;

Expand Down Expand Up @@ -582,6 +583,11 @@ private void startTeleportation(User user, Island island)
build();
}

// Add visitor to the tracked player set after 1 second.
Bukkit.getScheduler().runTaskLater(this.addon.getPlugin(),
() -> IslandLeaveListener.trackedPlayerSet.add(user.getUniqueId()),
20L);

if (island.isAllowed(VisitAddon.RECEIVE_VISIT_MESSAGE_FLAG))
{
// Send message that player is visiting the island.
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ visit:
visit-earn-bank: "&l&6 [player] &r&e deposited in your bank &l&6 $[payment] &r&e for visiting your island."
# Message that appears when someone visits player island.
player-visiting-island: "&e Player &l&6 [player] &r&e visited your island."
# Message that appears when visitor leaves player island.
player-leaves-island: "&e Player &l&6 [player] &r&e leaved your island."
# Protection flags that are used in current addon.
protection:
flags:
Expand Down

0 comments on commit 1623b2b

Please sign in to comment.