Permalink
Browse files

Move some more code to core.

  • Loading branch information...
me4502 committed Aug 10, 2018
1 parent e31fa30 commit b2e64498a8c113d5e4d2333112be376afebc663f
Showing with 184 additions and 164 deletions.
  1. +110 −0 worldguard-core/src/main/java/com/sk89q/worldguard/WorldGuard.java
  2. +4 −8 ...guard/bukkit → worldguard-core/src/main/java/com/sk89q/worldguard}/commands/task/RegionAdder.java
  3. +16 −20 ...uard/bukkit → worldguard-core/src/main/java/com/sk89q/worldguard}/commands/task/RegionLister.java
  4. +1 −1 ...it → worldguard-core/src/main/java/com/sk89q/worldguard}/commands/task/RegionManagerReloader.java
  5. +1 −1 ...ukkit → worldguard-core/src/main/java/com/sk89q/worldguard}/commands/task/RegionManagerSaver.java
  6. +1 −1 ...ard/bukkit → worldguard-core/src/main/java/com/sk89q/worldguard}/commands/task/RegionRemover.java
  7. +9 −0 worldguard-core/src/main/java/com/sk89q/worldguard/internal/platform/WorldGuardPlatform.java
  8. +1 −1 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/BukkitRegionContainer.java
  9. +6 −0 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/BukkitWorldGuardPlatform.java
  10. +2 −100 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlugin.java
  11. +2 −1 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/commands/AsyncCommandHelper.java
  12. +2 −2 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/commands/WorldGuardCommands.java
  13. +8 −8 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/commands/region/MemberCommands.java
  14. +21 −21 worldguard-legacy/src/main/java/com/sk89q/worldguard/bukkit/commands/region/RegionCommands.java
@@ -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 {
@@ -35,6 +56,10 @@
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;
@@ -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);
}
/**
@@ -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();
}
}
@@ -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;
@@ -37,7 +37,6 @@
*/
public class RegionAdder implements Callable<ProtectedRegion> {
private final WorldGuardPlugin plugin;
private final RegionManager manager;
private final ProtectedRegion region;
@Nullable
@@ -47,16 +46,13 @@
/**
* 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;
}
@@ -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);
@@ -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;
@@ -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;
}
@@ -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 + "'");
@@ -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()) {
@@ -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) {
@@ -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> {
@@ -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;
@@ -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;
@@ -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;
@@ -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.
*/
@@ -119,4 +121,11 @@
* @return The default game mode
*/
GameMode getDefaultGameMode();
/**
* Gets the configuration directory.
*
* @return The config directory
*/
Path getConfigDir();
}
@@ -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);
@@ -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;
@@ -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();
}
}
Oops, something went wrong.

0 comments on commit b2e6449

Please sign in to comment.