Skip to content

Commit d00837a

Browse files
author
Robin
committed
Swap out AugmentRegistry#callEvent
1 parent a05f20c commit d00837a

6 files changed

Lines changed: 163 additions & 133 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package dev.oribuin.fishing.api.event;
2+
3+
import org.bukkit.event.Event;
4+
import org.bukkit.event.EventPriority;
5+
6+
import java.util.function.BiConsumer;
7+
8+
/**
9+
* Wrapper for an event to be registered with a function
10+
*
11+
* @param event The event to be registered
12+
* @param function The function to be called when the event is fired
13+
* @param order The priority of the event
14+
* @param <T> The event type to be registered with the function
15+
*/
16+
public record EventWrapper<T extends Event>(Class<T> event, BiConsumer<T, Integer> function, EventPriority order) {
17+
18+
/**
19+
* Call the function that was registered with the event, This will cast the event to the correct type
20+
*
21+
* @param event The {@link Event} to call
22+
* @param level The level of the event
23+
*/
24+
@SuppressWarnings("unchecked")
25+
public void accept(Event event, int level) {
26+
this.function.accept((T) event, level);
27+
}
28+
29+
/**
30+
* Get the event type that was registered
31+
*
32+
* @return The event that was registered
33+
*/
34+
@Override
35+
public Class<T> event() {
36+
return event;
37+
}
38+
39+
/**
40+
* Get the function that was registered
41+
*
42+
* @return The function that was registered
43+
*/
44+
@Override
45+
public BiConsumer<T, Integer> function() {
46+
return function;
47+
}
48+
49+
/**
50+
* Get the priority of the event
51+
*
52+
* @return The priority of the event
53+
*/
54+
@Override
55+
public EventPriority order() {
56+
return order;
57+
}
58+
59+
}

src/main/java/dev/oribuin/fishing/api/event/FishEventHandler.java

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.bukkit.event.Event;
44
import org.bukkit.event.EventPriority;
55

6+
import java.util.Comparator;
67
import java.util.HashMap;
78
import java.util.Map;
89
import java.util.function.BiConsumer;
@@ -15,6 +16,25 @@ public abstract class FishEventHandler implements FishingEvents {
1516

1617
private final Map<Class<? extends Event>, EventWrapper<?>> events = new HashMap<>();
1718

19+
/**
20+
* Call an event from the handler's registered events, This will take priority into account.
21+
*
22+
* @param event The {@link Event} to call
23+
* @param <T> The event type to call
24+
*/
25+
public static <T extends FishEventHandler> void callEvents(Map<T, Integer> values, Event event) {
26+
if (!event.callEvent()) return; // Call the event through bukkit to allow other plugins to listen to the event
27+
28+
Map<T, Integer> applicable = new HashMap<>(values);
29+
applicable.keySet().removeIf(x -> !x.events().containsKey(event.getClass()));
30+
31+
applicable.entrySet()
32+
.stream()
33+
.map(x -> new MutableEventWrapper<>(x, event))
34+
.sorted(Comparator.comparingInt(o -> o.wrapper().order().getSlot()))
35+
.forEach(x -> x.wrapper().accept(event, x.level()));
36+
}
37+
1838
/**
1939
* Call an event from the handler's registered events, This will not take priority into account.
2040
*
@@ -101,58 +121,4 @@ public <T extends Event> EventWrapper<T> getWrapper(Class<T> event) {
101121
return (EventWrapper<T>) this.events.get(event);
102122
}
103123

104-
105-
/**
106-
* Wrapper for an event to be registered with a function
107-
*
108-
* @param event The event to be registered
109-
* @param function The function to be called when the event is fired
110-
* @param order The priority of the event
111-
* @param <T> The event type to be registered with the function
112-
*/
113-
public record EventWrapper<T extends Event>(Class<T> event, BiConsumer<T, Integer> function, EventPriority order) {
114-
115-
/**
116-
* Call the function that was registered with the event, This will cast the event to the correct type
117-
*
118-
* @param event The {@link Event} to call
119-
* @param level The level of the event
120-
*/
121-
@SuppressWarnings("unchecked")
122-
public void accept(Event event, int level) {
123-
this.function.accept((T) event, level);
124-
}
125-
126-
/**
127-
* Get the event type that was registered
128-
*
129-
* @return The event that was registered
130-
*/
131-
@Override
132-
public Class<T> event() {
133-
return event;
134-
}
135-
136-
/**
137-
* Get the function that was registered
138-
*
139-
* @return The function that was registered
140-
*/
141-
@Override
142-
public BiConsumer<T, Integer> function() {
143-
return function;
144-
}
145-
146-
/**
147-
* Get the priority of the event
148-
*
149-
* @return The priority of the event
150-
*/
151-
@Override
152-
public EventPriority order() {
153-
return order;
154-
}
155-
156-
}
157-
158124
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package dev.oribuin.fishing.api.event;
2+
3+
import com.google.common.base.Supplier;
4+
import dev.oribuin.fishing.model.augment.Augment;
5+
import dev.oribuin.fishing.model.augment.AugmentRegistry;
6+
import org.bukkit.event.Event;
7+
8+
import java.util.Map;
9+
10+
/**
11+
* Wrapper for a fishing event handler to be registered with a function
12+
*/
13+
public class MutableEventWrapper<T extends FishEventHandler> {
14+
15+
private final T type;
16+
private final int level;
17+
private final EventWrapper<?> wrapper;
18+
19+
/**
20+
* Wrapper for a fish event handler to be registered with a function in an event, Used to make the stream less annoying
21+
*
22+
* @param type The type to be registered
23+
* @param level The level of the augment
24+
* @param event The event to be registered
25+
*
26+
* @see FishEventHandler#callEvents(Map, Event) Where this is used
27+
*/
28+
public MutableEventWrapper(T type, int level, Event event) {
29+
this.type = type;
30+
this.level = level;
31+
this.wrapper = type.getWrapper(event.getClass());
32+
}
33+
34+
/**
35+
* Wrapper for a fish event handler to be registered with a function in an event, Used to make the stream less annoying
36+
*
37+
* @param entry The entry to be registered
38+
* @param event The event to be registered
39+
*
40+
* @see FishEventHandler#callEvents(Map, Event) Where this is used
41+
*/
42+
public MutableEventWrapper(Map.Entry<T, Integer> entry, Event event) {
43+
this(entry.getKey(), entry.getValue(), event);
44+
}
45+
46+
/**
47+
* Get the augment that was registered
48+
*
49+
* @return The {@link Augment} that was registered
50+
*/
51+
public T type() {
52+
return type;
53+
}
54+
55+
/**
56+
* Get the level of the augment that was registered
57+
*
58+
* @return The level of the augment that was registered
59+
*/
60+
public int level() {
61+
return level;
62+
}
63+
64+
/**
65+
* Get the event wrapper that was registered
66+
*
67+
* @return The event wrapper that was registered
68+
*/
69+
public EventWrapper<?> wrapper() {
70+
return wrapper;
71+
}
72+
73+
}

src/main/java/dev/oribuin/fishing/listener/FishListener.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package dev.oribuin.fishing.listener;
22

33
import dev.oribuin.fishing.FishingPlugin;
4+
import dev.oribuin.fishing.api.event.FishEventHandler;
45
import dev.oribuin.fishing.api.event.impl.FishCatchEvent;
56
import dev.oribuin.fishing.model.augment.AugmentRegistry;
67
import dev.oribuin.fishing.manager.DataManager;
78
import dev.oribuin.fishing.manager.FishManager;
89
import dev.oribuin.fishing.manager.LocaleManager;
10+
import dev.oribuin.fishing.model.economy.CurrencyRegistry;
911
import dev.oribuin.fishing.model.fish.Fish;
1012
import dev.oribuin.fishing.storage.Fisher;
1113
import org.bukkit.event.EventHandler;
@@ -48,8 +50,9 @@ public void onFish(PlayerFishEvent event) {
4850

4951
FishCatchEvent fishCatchEvent = new FishCatchEvent(event.getPlayer(), hand, event.getHook(), fish);
5052
fishCatchEvent.naturalExp(naturalExp); // Set the base experience gained
51-
AugmentRegistry.callEvent(AugmentRegistry.from(hand), fishCatchEvent);
5253

54+
FishEventHandler.callEvents(AugmentRegistry.from(hand), fishCatchEvent);
55+
5356
if (fishCatchEvent.isCancelled()) continue; // If the event is cancelled, do nothing
5457

5558
// Use the event values because they could have been modified
@@ -72,7 +75,7 @@ public void onFish(PlayerFishEvent event) {
7275

7376
inv.addItem(resultItem);
7477
}
75-
78+
7679
Fisher fisher = this.plugin.getManager(DataManager.class).get(event.getPlayer().getUniqueId());
7780
if (fisher == null) return;
7881

src/main/java/dev/oribuin/fishing/manager/FishManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dev.oribuin.fishing.manager;
22

3+
import dev.oribuin.fishing.api.event.FishEventHandler;
4+
import dev.oribuin.fishing.api.event.FishingEvents;
35
import dev.oribuin.fishing.api.event.impl.FishGenerateEvent;
46
import dev.oribuin.fishing.api.event.impl.InitialFishCatchEvent;
57
import dev.oribuin.fishing.model.augment.Augment;
@@ -41,7 +43,7 @@ public List<Fish> tryCatch(Player player, ItemStack rod, FishHook hook) {
4143
InitialFishCatchEvent event = new InitialFishCatchEvent(player, rod, hook);
4244

4345
// Run the augments onInitialCatch method
44-
AugmentRegistry.callEvent(augments, event);
46+
FishEventHandler.callEvents(augments, event);
4547

4648
// Cancel the event if it is cancelled
4749
if (event.isCancelled()) return result;
@@ -68,7 +70,7 @@ public Fish generateFish(Map<Augment, Integer> augments, Player player, ItemStac
6870
FishGenerateEvent event = new FishGenerateEvent(player, rod, hook);
6971
event.generate(); // Generate the fish
7072

71-
AugmentRegistry.callEvent(augments, event); // Call the augments
73+
FishEventHandler.callEvents(augments, event); // Call the augments
7274
if (event.isCancelled()) return null;
7375

7476
return event.fish();

src/main/java/dev/oribuin/fishing/model/augment/AugmentRegistry.java

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.base.Supplier;
44
import dev.oribuin.fishing.api.event.FishEventHandler;
5+
import dev.oribuin.fishing.api.event.MutableEventWrapper;
56
import dev.oribuin.fishing.model.augment.impl.AugmentBiomeBlend;
67
import dev.oribuin.fishing.model.augment.impl.AugmentEnlightened;
78
import dev.oribuin.fishing.model.augment.impl.AugmentFineSlicing;
@@ -55,27 +56,7 @@ private AugmentRegistry() {}
5556
register(AugmentEnlightened::new);
5657
register(AugmentIndulge::new);
5758
}
58-
59-
/**
60-
* Call an event in the plugin to be used by the augments
61-
*
62-
* @param augments The augments to call the event with
63-
* @param event The event to call
64-
*/
65-
public static void callEvent(Map<Augment, Integer> augments, Event event) {
66-
if (!event.callEvent()) return; // Call the event through bukkit to allow other plugins to listen to the event
67-
68-
Map<Augment, Integer> applicable = new HashMap<>(augments);
69-
applicable.keySet().removeIf(x -> !x.events().containsKey(event.getClass()));
70-
71-
// Form a Map.Entry<Augment, Integer> with a EventWrapper to call the event
72-
applicable.entrySet()
73-
.stream()
74-
.map(entry -> new AugmentEventWrapper(entry.getKey(), entry.getValue(), event))
75-
.sorted(Comparator.comparingInt(o -> o.wrapper.order().getSlot()))
76-
.forEach(x -> x.wrapper.accept(event, x.level()));
77-
}
78-
59+
7960
/**
8061
* Loads an augment into the registry to be used in the plugin and caches it.
8162
* Calls {@link Augment#reload()} to load the augment.
@@ -196,58 +177,4 @@ public static Map<String, Augment> all() {
196177
return augments;
197178
}
198179

199-
200-
/**
201-
* Wrapper for an augment to be registered with a function
202-
*/
203-
public static final class AugmentEventWrapper {
204-
205-
private final Augment augment;
206-
private final int level;
207-
private final FishEventHandler.EventWrapper<?> wrapper;
208-
209-
/**
210-
* Wrapper for an augment to be registered with a function in an event, Used to make the stream less annoying
211-
*
212-
* @param augment The augment to be registered
213-
* @param level The level of the augment
214-
* @param event The event to be registered
215-
*
216-
* @see AugmentRegistry#callEvent(Map, Event) Where this is used
217-
*/
218-
public AugmentEventWrapper(Augment augment, int level, Event event) {
219-
this.augment = augment;
220-
this.level = level;
221-
this.wrapper = augment.getWrapper(event.getClass());
222-
}
223-
224-
/**
225-
* Get the augment that was registered
226-
*
227-
* @return The {@link Augment} that was registered
228-
*/
229-
public Augment augment() {
230-
return augment;
231-
}
232-
233-
/**
234-
* Get the level of the augment that was registered
235-
*
236-
* @return The level of the augment that was registered
237-
*/
238-
public int level() {
239-
return level;
240-
}
241-
242-
/**
243-
* Get the event wrapper that was registered
244-
*
245-
* @return The event wrapper that was registered
246-
*/
247-
public FishEventHandler.EventWrapper<?> wrapper() {
248-
return wrapper;
249-
}
250-
251-
}
252-
253180
}

0 commit comments

Comments
 (0)