Split PlayerWorldAction to PlayerWorldTeleporter and EnforcementHandler

This commit is contained in:
Ben Woo 2023-09-12 13:13:54 +08:00
parent a45849799d
commit f9bd7f136d
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
7 changed files with 118 additions and 109 deletions

View File

@ -47,7 +47,8 @@ import org.mvplugins.multiverse.core.worldnew.LoadedMultiverseWorld;
import org.mvplugins.multiverse.core.worldnew.WorldManager;
import org.mvplugins.multiverse.core.worldnew.entrycheck.EntryFeeResult;
import org.mvplugins.multiverse.core.worldnew.entrycheck.WorldEntryCheckerProvider;
import org.mvplugins.multiverse.core.worldnew.helpers.PlayerWorldActions;
import org.mvplugins.multiverse.core.worldnew.helpers.EnforcementHandler;
import org.mvplugins.multiverse.core.worldnew.helpers.PlayerWorldTeleporter;
/**
* Multiverse's Listener for players.
@ -63,9 +64,8 @@ public class MVPlayerListener implements InjectableListener {
private final MVEconomist economist;
private final WorldEntryCheckerProvider worldEntryCheckerProvider;
private final Provider<MVCommandManager> commandManagerProvider;
private final CorePermissionsChecker permissionsChecker;
private final DestinationsProvider destinationsProvider;
private final PlayerWorldActions playerWorldActions;
private final EnforcementHandler enforcementHandler;
private final Map<String, String> playerWorld = new ConcurrentHashMap<String, String>();
@ -80,9 +80,8 @@ public class MVPlayerListener implements InjectableListener {
MVEconomist economist,
WorldEntryCheckerProvider worldEntryCheckerProvider,
Provider<MVCommandManager> commandManagerProvider,
CorePermissionsChecker permissionsChecker,
DestinationsProvider destinationsProvider,
PlayerWorldActions playerWorldActions) {
EnforcementHandler enforcementHandler) {
this.plugin = plugin;
this.config = config;
this.worldManagerProvider = worldManagerProvider;
@ -92,9 +91,8 @@ public class MVPlayerListener implements InjectableListener {
this.economist = economist;
this.worldEntryCheckerProvider = worldEntryCheckerProvider;
this.commandManagerProvider = commandManagerProvider;
this.permissionsChecker = permissionsChecker;
this.destinationsProvider = destinationsProvider;
this.playerWorldActions = playerWorldActions;
this.enforcementHandler = enforcementHandler;
}
private WorldManager getWorldManager() {
@ -374,8 +372,8 @@ public class MVPlayerListener implements InjectableListener {
if (!player.isOnline() || !player.getWorld().equals(world)) {
return;
}
playerWorldActions.handleFlightEnforcement(player);
playerWorldActions.handleGameModeEnforcement(player);
enforcementHandler.handleFlightEnforcement(player);
enforcementHandler.handleGameModeEnforcement(player);
}, 1L);
}
}

View File

@ -37,7 +37,7 @@ import org.mvplugins.multiverse.core.worldnew.generators.GeneratorProvider;
import org.mvplugins.multiverse.core.worldnew.helpers.DataStore.GameRulesStore;
import org.mvplugins.multiverse.core.worldnew.helpers.DataTransfer;
import org.mvplugins.multiverse.core.worldnew.helpers.FilesManipulator;
import org.mvplugins.multiverse.core.worldnew.helpers.PlayerWorldActions;
import org.mvplugins.multiverse.core.worldnew.helpers.PlayerWorldTeleporter;
import org.mvplugins.multiverse.core.worldnew.options.CloneWorldOptions;
import org.mvplugins.multiverse.core.worldnew.options.CreateWorldOptions;
import org.mvplugins.multiverse.core.worldnew.options.ImportWorldOptions;
@ -72,7 +72,7 @@ public class WorldManager {
private final WorldsConfigManager worldsConfigManager;
private final WorldNameChecker worldNameChecker;
private final GeneratorProvider generatorProvider;
private final PlayerWorldActions playerWorldActions;
private final PlayerWorldTeleporter playerWorldActions;
private final FilesManipulator filesManipulator;
private final BlockSafety blockSafety;
private final SafeTTeleporter safetyTeleporter;
@ -83,7 +83,7 @@ public class WorldManager {
@NotNull WorldsConfigManager worldsConfigManager,
@NotNull WorldNameChecker worldNameChecker,
@NotNull GeneratorProvider generatorProvider,
@NotNull PlayerWorldActions playerWorldActions,
@NotNull PlayerWorldTeleporter playerWorldActions,
@NotNull FilesManipulator filesManipulator,
@NotNull BlockSafety blockSafety,
@NotNull SafeTTeleporter safetyTeleporter,

View File

@ -24,26 +24,23 @@ import org.mvplugins.multiverse.core.configuration.migration.NullStringMigratorA
import org.mvplugins.multiverse.core.configuration.migration.VersionMigrator;
import org.mvplugins.multiverse.core.world.configuration.AllowedPortalType;
import org.mvplugins.multiverse.core.worldnew.LoadedMultiverseWorld;
import org.mvplugins.multiverse.core.worldnew.helpers.PlayerWorldActions;
import org.mvplugins.multiverse.core.worldnew.helpers.EnforcementHandler;
/**
* Represents a world configuration.
*/
public final class WorldConfig {
private final PlayerWorldActions playerWorldActions;
private final String worldName;
private final WorldConfigNodes configNodes;
private final ConfigurationSectionHandle configHandle;
WorldConfig(
@NotNull PlayerWorldActions playerWorldActions,
@NotNull String worldName,
@NotNull ConfigurationSection configSection) {
this.playerWorldActions = playerWorldActions;
@NotNull ConfigurationSection configSection,
@NotNull EnforcementHandler enforcementHandler) {
this.worldName = worldName;
this.configNodes = new WorldConfigNodes(playerWorldActions);
this.configNodes = new WorldConfigNodes(enforcementHandler);
this.configHandle = ConfigurationSectionHandle.builder(configSection)
.logger(Logging.getLogger())
.nodes(configNodes.getNodes())

View File

@ -15,7 +15,7 @@ import org.mvplugins.multiverse.core.configuration.node.Node;
import org.mvplugins.multiverse.core.configuration.node.NodeGroup;
import org.mvplugins.multiverse.core.world.configuration.AllowedPortalType;
import org.mvplugins.multiverse.core.worldnew.LoadedMultiverseWorld;
import org.mvplugins.multiverse.core.worldnew.helpers.PlayerWorldActions;
import org.mvplugins.multiverse.core.worldnew.helpers.EnforcementHandler;
/**
* Represents nodes in a world configuration.
@ -24,11 +24,11 @@ public class WorldConfigNodes {
private static final double CONFIG_VERSION = 1.0;
private final NodeGroup nodes = new NodeGroup();
private PlayerWorldActions playerWorldActions;
private EnforcementHandler enforcementHandler;
private LoadedMultiverseWorld world = null;
WorldConfigNodes(@NotNull PlayerWorldActions playerWorldActions) {
this.playerWorldActions = playerWorldActions;
WorldConfigNodes(@NotNull EnforcementHandler enforcementHandler) {
this.enforcementHandler = enforcementHandler;
}
LoadedMultiverseWorld getWorld() {
@ -65,7 +65,7 @@ public class WorldConfigNodes {
.defaultValue(false)
.onSetValue((oldValue, newValue) -> {
if (world == null) return;
playerWorldActions.handleAllFlightEnforcement(world);
enforcementHandler.handleAllFlightEnforcement(world);
})
.build());
@ -123,7 +123,7 @@ public class WorldConfigNodes {
.defaultValue(GameMode.SURVIVAL)
.onSetValue((oldValue, newValue) -> {
if (world == null) return;
playerWorldActions.handleAllGameModeEnforcement(world);
enforcementHandler.handleAllGameModeEnforcement(world);
})
.build());

View File

@ -21,7 +21,7 @@ import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.MultiverseCore;
import org.mvplugins.multiverse.core.worldnew.helpers.PlayerWorldActions;
import org.mvplugins.multiverse.core.worldnew.helpers.EnforcementHandler;
/**
* Manages the worlds.yml file.
@ -35,14 +35,14 @@ public final class WorldsConfigManager {
private final File worldConfigFile;
private YamlConfiguration worldsConfig;
private final PlayerWorldActions playerWorldActions;
private final EnforcementHandler enforcementHandler;
@Inject
WorldsConfigManager(@NotNull MultiverseCore core, @NotNull PlayerWorldActions playerWorldActions) {
WorldsConfigManager(@NotNull MultiverseCore core, @NotNull EnforcementHandler enforcementHandler) {
worldConfigMap = new HashMap<>();
worldConfigFile = core.getDataFolder().toPath().resolve(CONFIG_FILENAME).toFile();
this.playerWorldActions = playerWorldActions;
this.enforcementHandler = enforcementHandler;
}
/**
@ -126,9 +126,9 @@ public final class WorldsConfigManager {
.peek(config -> config.load(getWorldConfigSection(worldName)))
.onEmpty(() -> {
WorldConfig newWorldConfig = new WorldConfig(
playerWorldActions,
worldName,
getWorldConfigSection(worldName));
getWorldConfigSection(worldName),
enforcementHandler);
worldConfigMap.put(worldName, newWorldConfig);
newWorldsAdded.add(newWorldConfig);
});
@ -183,7 +183,7 @@ public final class WorldsConfigManager {
if (worldConfigMap.containsKey(worldName)) {
throw new IllegalArgumentException("WorldConfig for world " + worldName + " already exists.");
}
WorldConfig worldConfig = new WorldConfig(playerWorldActions, worldName, getWorldConfigSection(worldName));
WorldConfig worldConfig = new WorldConfig(worldName, getWorldConfigSection(worldName), enforcementHandler);
worldConfigMap.put(worldName, worldConfig);
return worldConfig;
}

View File

@ -1,104 +1,29 @@
package org.mvplugins.multiverse.core.worldnew.helpers;
import java.util.List;
import com.dumptruckman.minecraft.util.Logging;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.api.SafeTTeleporter;
import org.mvplugins.multiverse.core.permissions.CorePermissionsChecker;
import org.mvplugins.multiverse.core.worldnew.LoadedMultiverseWorld;
import org.mvplugins.multiverse.core.worldnew.WorldManager;
/**
* Handles all player actions that need to be done when a change in world related activity occurs.
*/
@Service
public class PlayerWorldActions {
public class EnforcementHandler {
private final CorePermissionsChecker permissionsChecker;
private final SafeTTeleporter safetyTeleporter;
private final Provider<WorldManager> worldManagerProvider;
@Inject
PlayerWorldActions(
@NotNull CorePermissionsChecker permissionsChecker,
@NotNull SafeTTeleporter safetyTeleporter,
@NotNull Provider<WorldManager> worldManagerProvider) {
EnforcementHandler(CorePermissionsChecker permissionsChecker, Provider<WorldManager> worldManagerProvider) {
this.permissionsChecker = permissionsChecker;
this.safetyTeleporter = safetyTeleporter;
this.worldManagerProvider = worldManagerProvider;
}
/**
* Removes all players from the given world.
*
* @param world The world to remove all players from.
*/
public void removeFromWorld(@NotNull LoadedMultiverseWorld world) {
// TODO: Better handling of fallback world
World toWorld = Bukkit.getWorlds().get(0);
transferFromWorldTo(world, toWorld);
}
/**
* Transfers all players from the given world to another world's spawn location.
*
* @param from The world to transfer players from.
* @param to The location to transfer players to.
*/
public void transferFromWorldTo(@NotNull LoadedMultiverseWorld from, @NotNull LoadedMultiverseWorld to) {
transferAllFromWorldToLocation(from, to.getSpawnLocation());
}
/**
* Transfers all players from the given world to another world's spawn location.
*
* @param from The world to transfer players from.
* @param to The world to transfer players to.
*/
public void transferFromWorldTo(@NotNull LoadedMultiverseWorld from, @NotNull World to) {
transferAllFromWorldToLocation(from, to.getSpawnLocation());
}
/**
* Transfers all players from the given world to the given location.
*
* @param world The world to transfer players from.
* @param location The location to transfer players to.
*/
public void transferAllFromWorldToLocation(@NotNull LoadedMultiverseWorld world, @NotNull Location location) {
world.getPlayers().peek(players -> players.forEach(player -> {
if (player.isOnline()) {
Logging.fine("Teleporting player '%s' to world spawn: %s", player.getName(), location);
safetyTeleporter.safelyTeleport(null, player, location, true);
}
}));
}
/**
* Teleports all players to the given world's spawn location.
*
* @param players The players to teleport.
* @param world The world to teleport players to.
*/
public void teleportPlayersToWorld(@NotNull List<Player> players, @NotNull LoadedMultiverseWorld world) {
players.forEach(player -> {
Location spawnLocation = world.getSpawnLocation();
if (player.isOnline()) {
safetyTeleporter.safelyTeleport(null, player, spawnLocation, true);
}
});
}
/**
* Teleports all players to the given location.
*

View File

@ -0,0 +1,89 @@
package org.mvplugins.multiverse.core.worldnew.helpers;
import java.util.List;
import com.dumptruckman.minecraft.util.Logging;
import jakarta.inject.Inject;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.api.SafeTTeleporter;
import org.mvplugins.multiverse.core.worldnew.LoadedMultiverseWorld;
/**
* Handles all player actions that need to be done when a change in world related activity occurs.
*/
@Service
public class PlayerWorldTeleporter {
private final SafeTTeleporter safetyTeleporter;
@Inject
PlayerWorldTeleporter(@NotNull SafeTTeleporter safetyTeleporter) {
this.safetyTeleporter = safetyTeleporter;
}
/**
* Removes all players from the given world.
*
* @param world The world to remove all players from.
*/
public void removeFromWorld(@NotNull LoadedMultiverseWorld world) {
// TODO: Better handling of fallback world
World toWorld = Bukkit.getWorlds().get(0);
transferFromWorldTo(world, toWorld);
}
/**
* Transfers all players from the given world to another world's spawn location.
*
* @param from The world to transfer players from.
* @param to The location to transfer players to.
*/
public void transferFromWorldTo(@NotNull LoadedMultiverseWorld from, @NotNull LoadedMultiverseWorld to) {
transferAllFromWorldToLocation(from, to.getSpawnLocation());
}
/**
* Transfers all players from the given world to another world's spawn location.
*
* @param from The world to transfer players from.
* @param to The world to transfer players to.
*/
public void transferFromWorldTo(@NotNull LoadedMultiverseWorld from, @NotNull World to) {
transferAllFromWorldToLocation(from, to.getSpawnLocation());
}
/**
* Transfers all players from the given world to the given location.
*
* @param world The world to transfer players from.
* @param location The location to transfer players to.
*/
public void transferAllFromWorldToLocation(@NotNull LoadedMultiverseWorld world, @NotNull Location location) {
world.getPlayers().peek(players -> players.forEach(player -> {
if (player.isOnline()) {
Logging.fine("Teleporting player '%s' to world spawn: %s", player.getName(), location);
safetyTeleporter.safelyTeleport(null, player, location, true);
}
}));
}
/**
* Teleports all players to the given world's spawn location.
*
* @param players The players to teleport.
* @param world The world to teleport players to.
*/
public void teleportPlayersToWorld(@NotNull List<Player> players, @NotNull LoadedMultiverseWorld world) {
players.forEach(player -> {
Location spawnLocation = world.getSpawnLocation();
if (player.isOnline()) {
safetyTeleporter.safelyTeleport(null, player, spawnLocation, true);
}
});
}
}