Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't read last_location when teleport type is exact #464

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.onarandombox.MultiverseCore.event.MVConfigReloadEvent;
import com.onarandombox.MultiverseCore.event.MVTeleportEvent;
import com.onarandombox.MultiverseCore.event.MVVersionEvent;
import com.onarandombox.multiverseinventories.profile.GlobalProfile;
import com.onarandombox.multiverseinventories.profile.PlayerProfile;
Expand Down Expand Up @@ -228,6 +229,22 @@ public void playerChangedWorld(PlayerChangedWorldEvent event) {
inventories.getData().updateLastWorld(player.getName(), toWorld.getName());
}

/**
* Called when a player teleports using Multiverse.
*
* @param event The Multiverse teleport event.
*/
@EventHandler(priority = EventPriority.MONITOR)
public void playerMVTeleport(MVTeleportEvent event) {
if (event.isCancelled()
|| event.getFrom().getWorld().equals(event.getDestination().getLocation(event.getTeleportee()).getWorld())
|| !this.inventories.getMVIConfig().getOptionalShares().contains(Sharables.LAST_LOCATION)) {
return;
}

TeleportDetails.addTeleportDestination(event.getTeleportee(), event.getDestination());
}

/**
* Called when a player teleports.
*
Expand All @@ -250,6 +267,10 @@ public void playerTeleport(PlayerTeleportEvent event) {
PlayerProfile playerProfile = fromWorldProfileContainer.getPlayerData(player);
playerProfile.set(Sharables.LAST_LOCATION, event.getFrom());

if (TeleportDetails.getTeleportDestination(player) == null) {
TeleportDetails.addTeleportDestination(player, event.getTo());
}

List<WorldGroup> fromGroups = this.inventories.getGroupManager().getGroupsForWorld(fromWorldName);
for (WorldGroup fromGroup : fromGroups) {
playerProfile = fromGroup.getGroupProfileContainer().getPlayerData(event.getPlayer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public abstract class ShareHandler {
* inventories/stats for a player and persisting the changes.
*/
final void handleSharing() {
TeleportDetails.removeTeleportDestination(this.player);
ShareHandlingEvent event = this.createEvent();

Bukkit.getPluginManager().callEvent(event);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.onarandombox.multiverseinventories;

import com.onarandombox.MultiverseCore.api.MVDestination;
import org.bukkit.Location;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;

/**
* A utility class to keep track of currently happening teleportations.
* Keeps track of teleport destinations as well the type of destination.
*/
public class TeleportDetails {
public enum TeleportType {
MVTP,
OTHER
}

static class TeleportDestination<T> {
private final TeleportType type;
private final T destination;

TeleportDestination(TeleportType type, T destination) {
this.type = type;
this.destination = destination;
}

public TeleportType getType() {
return this.type;
}

public T getDestination() {
return this.destination;
}
}

private final static Map<Player, TeleportDestination<?>> teleportDestinationMap = new HashMap<>();

public static void addTeleportDestination(Player player, MVDestination mvDestination) {
teleportDestinationMap.put(player, new TeleportDestination<>(TeleportType.MVTP, mvDestination));
}

public static void addTeleportDestination(Player player, Location location) {
teleportDestinationMap.put(player, new TeleportDestination<>(TeleportType.OTHER, location));
}

public static TeleportDestination<?> getTeleportDestination(Player player) {
return teleportDestinationMap.get(player);
}

public static void removeTeleportDestination(Player player) {
teleportDestinationMap.remove(player);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.onarandombox.multiverseinventories;

import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.api.MVDestination;
import com.onarandombox.multiverseinventories.event.ShareHandlingEvent;
import com.onarandombox.multiverseinventories.event.WorldChangeShareHandlingEvent;
import com.onarandombox.multiverseinventories.profile.PlayerProfile;
Expand All @@ -21,6 +22,7 @@ final class WorldChangeShareHandler extends ShareHandler {
private final String toWorld;
private final List<WorldGroup> fromWorldGroups;
private final List<WorldGroup> toWorldGroups;
private final boolean readLastLocation;

WorldChangeShareHandler(MultiverseInventories inventories, Player player, String fromWorld, String toWorld) {
super(inventories, player);
Expand All @@ -32,6 +34,18 @@ final class WorldChangeShareHandler extends ShareHandler {
// Get any groups we may need to load stuff from.
this.toWorldGroups = getAffectedWorldGroups(toWorld);

// don't read last_location if there is an exact teleportation happening
boolean readLastLocation = true;
TeleportDetails.TeleportDestination<?> teleportDestination = TeleportDetails.getTeleportDestination(player);
if (teleportDestination != null && teleportDestination.getType() == TeleportDetails.TeleportType.MVTP) {
MVDestination mvDestination = (MVDestination) teleportDestination.getDestination();
if (mvDestination.getIdentifier().equals("e")) {
readLastLocation = false;
}
}

this.readLastLocation = readLastLocation;

prepareProfiles();
}

Expand Down Expand Up @@ -82,7 +96,7 @@ private boolean isPlayerBypassingChange() {

private void addProfiles() {
addWriteProfiles();
new ReadProfilesAggregator().addReadProfiles();
new ReadProfilesAggregator(this.readLastLocation).addReadProfiles();
}

private void addWriteProfiles() {
Expand All @@ -100,6 +114,11 @@ private boolean hasFromWorldGroups() {
private class ReadProfilesAggregator {

private Shares sharesToRead;
private final boolean readLastLocation;

ReadProfilesAggregator(boolean readLastLocation) {
this.readLastLocation = readLastLocation;
}

private void addReadProfiles() {
sharesToRead = Sharables.noneOf();
Expand Down Expand Up @@ -149,6 +168,10 @@ private void addReadProfileForWorldGroup(WorldGroup worldGroup) {
PlayerProfile playerProfile = getWorldGroupPlayerData(worldGroup);
Shares sharesToAdd = getWorldGroupShares(worldGroup);

if (!this.readLastLocation) {
sharesToAdd.remove(Sharables.LAST_LOCATION);
}

addReadProfile(playerProfile, sharesToAdd);
sharesToRead.addAll(sharesToAdd);
}
Expand Down Expand Up @@ -179,6 +202,10 @@ private boolean hasUnhandledShares() {
private void addUnhandledSharesFromToWorld() {
Shares unhandledShares = Sharables.complimentOf(sharesToRead);

if (!this.readLastLocation) {
unhandledShares.remove(Sharables.LAST_LOCATION);
}

Logging.finer("%s are left unhandled, defaulting to toWorld", unhandledShares);

addReadProfile(getToWorldPlayerData(), unhandledShares);
Expand Down