-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
NPCShopAction.java
93 lines (70 loc) · 2.62 KB
/
NPCShopAction.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package net.citizensnpcs.trait.shop;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import com.google.common.collect.Lists;
import net.citizensnpcs.api.gui.InventoryMenuPage;
import net.citizensnpcs.api.persistence.PersistenceLoader;
import net.citizensnpcs.api.persistence.PersisterRegistry;
public abstract class NPCShopAction implements Cloneable {
@Override
public NPCShopAction clone() {
try {
return (NPCShopAction) super.clone();
} catch (CloneNotSupportedException e) {
throw new Error(e);
}
}
public abstract String describe();
public abstract int getMaxRepeats(Entity entity);
public abstract Transaction grant(Entity entity, int repeats);
public abstract Transaction take(Entity entity, int repeats);
public static interface GUI {
public InventoryMenuPage createEditor(NPCShopAction previous, Consumer<NPCShopAction> callback);
public ItemStack createMenuItem(NPCShopAction previous);
public boolean manages(NPCShopAction action);
}
public static class Transaction {
private final Runnable execute;
private final Supplier<Boolean> possible;
private final Runnable rollback;
public Transaction(Supplier<Boolean> isPossible, Runnable execute, Runnable rollback) {
possible = isPossible;
this.execute = execute;
this.rollback = rollback;
}
public boolean isPossible() {
return possible.get();
}
public void rollback() {
rollback.run();
}
public void run() {
execute.run();
}
public static Transaction create(Supplier<Boolean> isPossible, Runnable execute, Runnable rollback) {
return new Transaction(isPossible, execute, rollback);
}
public static Transaction fail() {
return create(() -> false, () -> {
}, () -> {
});
}
public static Transaction success() {
return create(() -> true, () -> {
}, () -> {
});
}
}
public static Iterable<GUI> getGUIs() {
return GUI;
}
public static void register(Class<? extends NPCShopAction> clazz, String type, GUI gui) {
REGISTRY.register(type, clazz);
GUI.add(gui);
}
private static List<GUI> GUI = Lists.newArrayList();
private static PersisterRegistry<NPCShopAction> REGISTRY = PersistenceLoader.createRegistry(NPCShopAction.class);
}