Skip to content

Commit

Permalink
Move some more code to core.
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Aug 10, 2018
1 parent 8a71834 commit eafe607
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 164 deletions.
110 changes: 110 additions & 0 deletions worldguard-core/src/main/java/com/sk89q/worldguard/WorldGuard.java
Expand Up @@ -21,11 +21,32 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.sk89q.squirrelid.cache.HashMapCache;
import com.sk89q.squirrelid.cache.ProfileCache;
import com.sk89q.squirrelid.cache.SQLiteCache;
import com.sk89q.squirrelid.resolver.BukkitPlayerService;
import com.sk89q.squirrelid.resolver.CacheForwardingService;
import com.sk89q.squirrelid.resolver.CombinedProfileService;
import com.sk89q.squirrelid.resolver.HttpRepositoryService;
import com.sk89q.squirrelid.resolver.ProfileService;
import com.sk89q.worldguard.internal.platform.WorldGuardPlatform;
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import com.sk89q.worldguard.protection.flags.registry.SimpleFlagRegistry;
import com.sk89q.worldguard.util.concurrent.EvenMoreExecutors;
import com.sk89q.worldguard.util.task.SimpleSupervisor;
import com.sk89q.worldguard.util.task.Supervisor;
import com.sk89q.worldguard.util.task.Task;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class WorldGuard {
Expand All @@ -35,6 +56,10 @@ public class WorldGuard {
private static final WorldGuard instance = new WorldGuard();
private WorldGuardPlatform platform;
private final SimpleFlagRegistry flagRegistry = new SimpleFlagRegistry();
private final Supervisor supervisor = new SimpleSupervisor();
private ProfileCache profileCache;
private ProfileService profileService;
private ListeningExecutorService executorService;

public static WorldGuard getInstance() {
return instance;
Expand All @@ -44,8 +69,26 @@ private WorldGuard() {
}

public void setup() {
executorService = MoreExecutors.listeningDecorator(EvenMoreExecutors.newBoundedCachedThreadPool(0, 1, 20));

getPlatform().load();
Flags.registerAll();

File cacheDir = new File(getPlatform().getConfigDir().toFile(), "cache");
cacheDir.mkdirs();

try {
profileCache = new SQLiteCache(new File(getPlatform().getConfigDir().toFile(), "profiles.sqlite"));
} catch (IOException e) {
WorldGuard.logger.log(Level.WARNING, "Failed to initialize SQLite profile cache");
profileCache = new HashMapCache();
}

profileService = new CacheForwardingService(
new CombinedProfileService(
BukkitPlayerService.getInstance(),
HttpRepositoryService.forMinecraft()),
profileCache);
}

/**
Expand All @@ -71,4 +114,71 @@ public void setPlatform(WorldGuardPlatform platform) {
public FlagRegistry getFlagRegistry() {
return this.flagRegistry;
}

/**
* Get the supervisor.
*
* @return the supervisor
*/
public Supervisor getSupervisor() {
return supervisor;
}

/**
* Get the global executor service for internal usage (please use your
* own executor service).
*
* @return the global executor service
*/
public ListeningExecutorService getExecutorService() {
return executorService;
}

/**
* Get the profile lookup service.
*
* @return the profile lookup service
*/
public ProfileService getProfileService() {
return profileService;
}

/**
* Get the profile cache.
*
* @return the profile cache
*/
public ProfileCache getProfileCache() {
return profileCache;
}

/**
* Called when WorldGuard should be disabled.
*/
public void disable() {
executorService.shutdown();

try {
WorldGuard.logger.log(Level.INFO, "Shutting down executor and waiting for any pending tasks...");

List<Task<?>> tasks = supervisor.getTasks();
if (!tasks.isEmpty()) {
StringBuilder builder = new StringBuilder("Known tasks:");
for (Task<?> task : tasks) {
builder.append("\n");
builder.append(task.getName());
}
WorldGuard.logger.log(Level.INFO, builder.toString());
}

Futures.successfulAsList(tasks).get();
executorService.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
WorldGuard.logger.log(Level.WARNING, "Some tasks failed while waiting for remaining tasks to finish", e);
}

platform.unload();
}
}
Expand Up @@ -17,10 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.bukkit.commands.task;
package com.sk89q.worldguard.commands.task;

import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.domains.DefaultDomain;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
Expand All @@ -37,7 +37,6 @@
*/
public class RegionAdder implements Callable<ProtectedRegion> {

private final WorldGuardPlugin plugin;
private final RegionManager manager;
private final ProtectedRegion region;
@Nullable
Expand All @@ -47,16 +46,13 @@ public class RegionAdder implements Callable<ProtectedRegion> {
/**
* Create a new instance.
*
* @param plugin the plugin
* @param manager the manage
* @param region the region
*/
public RegionAdder(WorldGuardPlugin plugin, RegionManager manager, ProtectedRegion region) {
checkNotNull(plugin);
public RegionAdder(RegionManager manager, ProtectedRegion region) {
checkNotNull(manager);
checkNotNull(region);

this.plugin = plugin;
this.manager = manager;
this.region = region;
}
Expand All @@ -77,7 +73,7 @@ public void addOwnersFromCommand(CommandContext args, int namesIndex) {
@Override
public ProtectedRegion call() throws Exception {
if (ownersInput != null) {
DomainInputResolver resolver = new DomainInputResolver(plugin.getProfileService(), ownersInput);
DomainInputResolver resolver = new DomainInputResolver(WorldGuard.getInstance().getProfileService(), ownersInput);
resolver.setLocatorPolicy(locatorPolicy);
DefaultDomain domain = resolver.call();
region.getOwners().addAll(domain);
Expand Down
Expand Up @@ -17,17 +17,18 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.bukkit.commands.task;
package com.sk89q.worldguard.commands.task;

import static com.google.common.base.Preconditions.checkNotNull;

import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.squirrelid.Profile;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.domains.DefaultDomain;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -39,24 +40,19 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.google.common.base.Preconditions.checkNotNull;

public class RegionLister implements Callable<Integer> {

private static final Logger log = Logger.getLogger(RegionLister.class.getCanonicalName());

private final WorldGuardPlugin plugin;
private final CommandSender sender;
private final Actor sender;
private final RegionManager manager;
private OwnerMatcher ownerMatcher;
private int page;

public RegionLister(WorldGuardPlugin plugin, RegionManager manager, CommandSender sender) {
checkNotNull(plugin);
public RegionLister(RegionManager manager, Actor sender) {
checkNotNull(manager);
checkNotNull(sender);

this.plugin = plugin;
this.manager = manager;
this.sender = sender;
}
Expand Down Expand Up @@ -124,7 +120,7 @@ public boolean isContainedWithin(DefaultDomain domain) throws CommandException {
Profile profile;

try {
profile = plugin.getProfileService().findByName(name);
profile = WorldGuard.getInstance().getProfileService().findByName(name);
} catch (IOException e) {
log.log(Level.WARNING, "Failed UUID lookup of '" + name + "'", e);
throw new CommandException("Failed to lookup the UUID of '" + name + "'");
Expand All @@ -150,7 +146,7 @@ public Integer call() throws Exception {
Map<String, ProtectedRegion> regions = manager.getRegions();

// Build a list of regions to show
List<RegionListEntry> entries = new ArrayList<RegionListEntry>();
List<RegionListEntry> entries = new ArrayList<>();

int index = 0;
for (String id : regions.keySet()) {
Expand All @@ -176,8 +172,8 @@ public Integer call() throws Exception {
final int pageSize = 10;
final int pages = (int) Math.ceil(totalSize / (float) pageSize);

sender.sendMessage(ChatColor.RED
+ (ownerMatcher == null ? "Regions (page " : "Regions for " + ownerMatcher.getName() + " (page ")
sender.printError(
(ownerMatcher == null ? "Regions (page " : "Regions for " + ownerMatcher.getName() + " (page ")
+ (page + 1) + " of " + pages + "):");

if (page < pages) {
Expand All @@ -187,17 +183,17 @@ public Integer call() throws Exception {
break;
}

sender.sendMessage(ChatColor.YELLOW.toString() + entries.get(i));
sender.print(String.valueOf(entries.get(i)));
}
}

return page;
}

private static interface OwnerMatcher {
public String getName();
private interface OwnerMatcher {
String getName();

public boolean isContainedWithin(DefaultDomain domain) throws CommandException;
boolean isContainedWithin(DefaultDomain domain) throws CommandException;
}

private class RegionListEntry implements Comparable<RegionListEntry> {
Expand Down
Expand Up @@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.bukkit.commands.task;
package com.sk89q.worldguard.commands.task;

import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.managers.storage.StorageException;
Expand Down
Expand Up @@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.bukkit.commands.task;
package com.sk89q.worldguard.commands.task;

import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.managers.storage.StorageException;
Expand Down
Expand Up @@ -17,7 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.bukkit.commands.task;
package com.sk89q.worldguard.commands.task;

import com.sk89q.minecraft.util.commands.CommandException;
import com.sk89q.worldguard.protection.managers.RegionManager;
Expand Down
Expand Up @@ -27,6 +27,8 @@
import com.sk89q.worldguard.protection.regions.RegionContainer;
import com.sk89q.worldguard.session.SessionManager;

import java.nio.file.Path;

/**
* A platform for implementing.
*/
Expand Down Expand Up @@ -119,4 +121,11 @@ public interface WorldGuardPlatform {
* @return The default game mode
*/
GameMode getDefaultGameMode();

/**
* Gets the configuration directory.
*
* @return The config directory
*/
Path getConfigDir();
}
Expand Up @@ -229,7 +229,7 @@ private void autoMigrate() {

if (config.migrateRegionsToUuid) {
RegionDriver driver = getDriver();
UUIDMigration migrator = new UUIDMigration(driver, plugin.getProfileService(), WorldGuard.getInstance().getFlagRegistry());
UUIDMigration migrator = new UUIDMigration(driver, WorldGuard.getInstance().getProfileService(), WorldGuard.getInstance().getFlagRegistry());
migrator.setKeepUnresolvedNames(config.keepUnresolvedNames);
try {
migrate(migrator);
Expand Down
Expand Up @@ -36,6 +36,7 @@
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permissible;

import java.nio.file.Path;
import java.util.Collection;
import java.util.Set;

Expand Down Expand Up @@ -128,4 +129,9 @@ public RegionContainer getRegionContainer() {
public GameMode getDefaultGameMode() {
return GameModes.get(Bukkit.getServer().getDefaultGameMode().name().toLowerCase());
}

@Override
public Path getConfigDir() {
return WorldGuardPlugin.inst().getDataFolder().toPath();
}
}

0 comments on commit eafe607

Please sign in to comment.