-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
ObjectProvider.java
38 lines (32 loc) · 1.13 KB
/
ObjectProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package net.citizensnpcs.api.scripting;
import java.util.concurrent.Callable;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.Callables;
public class ObjectProvider implements ContextProvider {
private final String name;
private final Callable<Object> provider;
public ObjectProvider(String name, Callable<Object> provider) {
Preconditions.checkNotNull(provider, "provider cannot be null");
Preconditions.checkNotNull(name, "name cannot be null");
this.name = name;
this.provider = provider;
}
public ObjectProvider(String name, Object obj) {
Preconditions.checkNotNull(obj, "provided object cannot be null");
Preconditions.checkNotNull(name, "name cannot be null");
this.name = name;
this.provider = Callables.returning(obj);
}
@Override
public void provide(Script script) {
Object res = null;
try {
res = provider.call();
} catch (Exception e) {
e.printStackTrace();
}
if (res == null)
return;
script.setAttribute(name, res);
}
}