Skip to content

Commit

Permalink
remove ObjectProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Mar 3, 2024
1 parent a2439da commit d943c33
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package io.github.projectunified.minelib.scheduler.async;

import io.github.projectunified.minelib.scheduler.common.provider.ObjectProvider;
import com.google.common.cache.LoadingCache;
import io.github.projectunified.minelib.scheduler.common.scheduler.Scheduler;
import io.github.projectunified.minelib.scheduler.common.task.Task;
import io.github.projectunified.minelib.scheduler.common.util.Platform;
import org.bukkit.plugin.Plugin;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.BooleanSupplier;

/**
* A {@link Scheduler} that can run tasks asynchronously
*/
public interface AsyncScheduler extends Scheduler {
ObjectProvider<Plugin, AsyncScheduler> PROVIDER = new ObjectProvider<>(
ObjectProvider.entry(Platform.FOLIA::isPlatform, FoliaAsyncScheduler::new),
ObjectProvider.entry(BukkitAsyncScheduler::new)
LoadingCache<Plugin, AsyncScheduler> PROVIDER = Scheduler.createProvider(
Platform.FOLIA.isPlatform() ? FoliaAsyncScheduler::new : BukkitAsyncScheduler::new
);

/**
Expand All @@ -25,7 +25,11 @@ public interface AsyncScheduler extends Scheduler {
* @return the scheduler
*/
static AsyncScheduler get(Plugin plugin) {
return PROVIDER.get(plugin);
try {
return PROVIDER.get(plugin);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package io.github.projectunified.minelib.scheduler.canceller;

import io.github.projectunified.minelib.scheduler.common.provider.ObjectProvider;
import com.google.common.cache.LoadingCache;
import io.github.projectunified.minelib.scheduler.common.scheduler.Scheduler;
import io.github.projectunified.minelib.scheduler.common.util.Platform;
import org.bukkit.plugin.Plugin;

import java.util.concurrent.ExecutionException;

/**
* An interface for cancelling tasks
*/
public interface TaskCanceller {
ObjectProvider<Plugin, TaskCanceller> PROVIDER = new ObjectProvider<>(
ObjectProvider.entry(Platform.FOLIA::isPlatform, FoliaTaskCanceller::new),
ObjectProvider.entry(BukkitTaskCanceller::new)
LoadingCache<Plugin, TaskCanceller> PROVIDER = Scheduler.createProvider(
Platform.FOLIA.isPlatform() ? FoliaTaskCanceller::new : BukkitTaskCanceller::new
);

/**
Expand All @@ -20,7 +22,11 @@ public interface TaskCanceller {
* @return the {@link TaskCanceller}
*/
static TaskCanceller get(Plugin plugin) {
return PROVIDER.get(plugin);
try {
return PROVIDER.get(plugin);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
package io.github.projectunified.minelib.scheduler.common.scheduler;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import io.github.projectunified.minelib.scheduler.common.task.Task;

import java.util.function.BooleanSupplier;
import java.util.function.Function;

/**
* A scheduler that provides a way to run tasks
*/
public interface Scheduler {
/**
* Create a provider
*
* @param function the function
* @param <K> the key type
* @param <T> the scheduler type
* @return the provider
*/
static <K, T> LoadingCache<K, T> createProvider(Function<K, T> function) {
return CacheBuilder.newBuilder().weakKeys().build(CacheLoader.from(function::apply));
}

/**
* Run a task
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.projectunified.minelib.scheduler.entity;

import io.github.projectunified.minelib.scheduler.common.provider.ObjectProvider;
import com.google.common.cache.LoadingCache;
import io.github.projectunified.minelib.scheduler.common.scheduler.Scheduler;
import io.github.projectunified.minelib.scheduler.common.task.Task;
import io.github.projectunified.minelib.scheduler.common.util.Platform;
Expand All @@ -9,15 +9,15 @@
import org.bukkit.plugin.Plugin;

import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.function.BooleanSupplier;

/**
* A {@link Scheduler} that schedules tasks for an {@link Entity}
*/
public interface EntityScheduler extends Scheduler {
ObjectProvider<Key, EntityScheduler> PROVIDER = new ObjectProvider<>(
ObjectProvider.entry(Platform.FOLIA::isPlatform, FoliaEntityScheduler::new),
ObjectProvider.entry(BukkitEntityScheduler::new)
LoadingCache<Key, EntityScheduler> PROVIDER = Scheduler.createProvider(
Platform.FOLIA.isPlatform() ? FoliaEntityScheduler::new : BukkitEntityScheduler::new
);

/**
Expand All @@ -28,7 +28,11 @@ public interface EntityScheduler extends Scheduler {
* @return the scheduler
*/
static EntityScheduler get(Plugin plugin, Entity entity) {
return PROVIDER.get(new Key(plugin, entity));
try {
return PROVIDER.get(new Key(plugin, entity));
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package io.github.projectunified.minelib.scheduler.global;

import io.github.projectunified.minelib.scheduler.common.provider.ObjectProvider;
import com.google.common.cache.LoadingCache;
import io.github.projectunified.minelib.scheduler.common.scheduler.Scheduler;
import io.github.projectunified.minelib.scheduler.common.util.Platform;
import org.bukkit.plugin.Plugin;

import java.util.concurrent.ExecutionException;

/**
* A {@link Scheduler} that can run tasks globally
*/
public interface GlobalScheduler extends Scheduler {
ObjectProvider<Plugin, GlobalScheduler> PROVIDER = new ObjectProvider<>(
ObjectProvider.entry(Platform.FOLIA::isPlatform, FoliaGlobalScheduler::new),
ObjectProvider.entry(BukkitGlobalScheduler::new)
LoadingCache<Plugin, GlobalScheduler> PROVIDER = Scheduler.createProvider(
Platform.FOLIA.isPlatform() ? FoliaGlobalScheduler::new : BukkitGlobalScheduler::new
);

/**
Expand All @@ -21,6 +22,10 @@ public interface GlobalScheduler extends Scheduler {
* @return the scheduler
*/
static GlobalScheduler get(Plugin plugin) {
return PROVIDER.get(plugin);
try {
return PROVIDER.get(plugin);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package io.github.projectunified.minelib.scheduler.location;

import io.github.projectunified.minelib.scheduler.common.provider.ObjectProvider;
import com.google.common.cache.LoadingCache;
import io.github.projectunified.minelib.scheduler.common.scheduler.Scheduler;
import io.github.projectunified.minelib.scheduler.common.util.Platform;
import org.bukkit.Location;
import org.bukkit.plugin.Plugin;

import java.util.Objects;
import java.util.concurrent.ExecutionException;

/**
* A {@link Scheduler} that schedules tasks for a {@link Location}
*/
public interface LocationScheduler extends Scheduler {
ObjectProvider<Key, LocationScheduler> PROVIDER = new ObjectProvider<>(
ObjectProvider.entry(Platform.FOLIA::isPlatform, FoliaLocationScheduler::new),
ObjectProvider.entry(key -> new BukkitLocationScheduler(key.plugin))
LoadingCache<Key, LocationScheduler> PROVIDER = Scheduler.createProvider(
Platform.FOLIA.isPlatform() ? FoliaLocationScheduler::new : k -> new BukkitLocationScheduler(k.plugin)
);

/**
Expand All @@ -25,7 +25,11 @@ public interface LocationScheduler extends Scheduler {
* @return the scheduler
*/
static LocationScheduler get(Plugin plugin, Location location) {
return PROVIDER.get(new Key(plugin, location));
try {
return PROVIDER.get(new Key(plugin, location));
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package io.github.projectunified.minelib.scheduler.region;

import io.github.projectunified.minelib.scheduler.common.provider.ObjectProvider;
import com.google.common.cache.LoadingCache;
import io.github.projectunified.minelib.scheduler.common.scheduler.Scheduler;
import io.github.projectunified.minelib.scheduler.common.util.Platform;
import org.bukkit.World;
import org.bukkit.plugin.Plugin;

import java.util.Objects;
import java.util.concurrent.ExecutionException;

/**
* A {@link Scheduler} that schedules tasks for a region
*/
public interface RegionScheduler extends Scheduler {
ObjectProvider<Key, RegionScheduler> PROVIDER = new ObjectProvider<>(
ObjectProvider.entry(Platform.FOLIA::isPlatform, FoliaRegionScheduler::new),
ObjectProvider.entry(key -> new BukkitRegionScheduler(key.plugin))
LoadingCache<Key, RegionScheduler> PROVIDER = Scheduler.createProvider(
Platform.FOLIA.isPlatform() ? FoliaRegionScheduler::new : k -> new BukkitRegionScheduler(k.plugin)
);

/**
Expand All @@ -27,7 +27,11 @@ public interface RegionScheduler extends Scheduler {
* @return the scheduler
*/
static RegionScheduler get(Plugin plugin, World world, int chunkX, int chunkZ) {
return PROVIDER.get(new Key(plugin, world, chunkX, chunkZ));
try {
return PROVIDER.get(new Key(plugin, world, chunkX, chunkZ));
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}

/**
Expand Down

0 comments on commit d943c33

Please sign in to comment.