Plan/Plan/bukkit/src/main/java/com/djrapitops/plan/system/listeners/bukkit/GameModeChangeListener.java

78 lines
2.6 KiB
Java

/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the LGNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* LGNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.system.listeners.bukkit;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.settings.WorldAliasSettings;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.logging.error.ErrorHandler;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerGameModeChangeEvent;
import javax.inject.Inject;
import java.util.Optional;
import java.util.UUID;
/**
* Event Listener for PlayerGameModeChangeEvents.
*
* @author Rsl1122
*/
public class GameModeChangeListener implements Listener {
private final WorldAliasSettings worldAliasSettings;
private final ErrorHandler errorHandler;
@Inject
public GameModeChangeListener(
WorldAliasSettings worldAliasSettings,
ErrorHandler errorHandler
) {
this.worldAliasSettings = worldAliasSettings;
this.errorHandler = errorHandler;
}
@EventHandler(priority = EventPriority.MONITOR)
public void onGameModeChange(PlayerGameModeChangeEvent event) {
if (event.isCancelled()) {
return;
}
try {
actOnEvent(event);
} catch (Exception e) {
errorHandler.log(L.ERROR, this.getClass(), e);
}
}
private void actOnEvent(PlayerGameModeChangeEvent event) {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
long time = System.currentTimeMillis();
String gameMode = event.getNewGameMode().name();
String worldName = player.getWorld().getName();
worldAliasSettings.addWorld(worldName);
Optional<Session> cachedSession = SessionCache.getCachedSession(uuid);
cachedSession.ifPresent(session -> session.changeState(worldName, gameMode, time));
}
}