Skip to content

Commit cc64ce7

Browse files
author
Robin
committed
more event stuff
1 parent d00837a commit cc64ce7

File tree

6 files changed

+53
-89
lines changed

6 files changed

+53
-89
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @param <T> The event type to be registered with the function
1515
*/
1616
public record EventWrapper<T extends Event>(Class<T> event, BiConsumer<T, Integer> function, EventPriority order) {
17-
17+
1818
/**
1919
* Call the function that was registered with the event, This will cast the event to the correct type
2020
*

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

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

6-
import java.util.Comparator;
76
import java.util.HashMap;
87
import java.util.Map;
98
import java.util.function.BiConsumer;
@@ -22,17 +21,14 @@ public abstract class FishEventHandler implements FishingEvents {
2221
* @param event The {@link Event} to call
2322
* @param <T> The event type to call
2423
*/
25-
public static <T extends FishEventHandler> void callEvents(Map<T, Integer> values, Event event) {
24+
public static <T extends FishEventHandler, Z extends Event> void callEvents(Map<T, Integer> values, Z event) {
2625
if (!event.callEvent()) return; // Call the event through bukkit to allow other plugins to listen to the event
2726

2827
Map<T, Integer> applicable = new HashMap<>(values);
29-
applicable.keySet().removeIf(x -> !x.events().containsKey(event.getClass()));
28+
applicable.keySet().removeIf(x -> !x.applicable(event));
3029

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()));
30+
// Sort the events by their priority and call them
31+
applicable.entrySet().stream().map(x -> new MutableEventWrapper<>(x, event)).sorted(MutableEventWrapper::compare).forEachOrdered(x -> x.type().callEvent(event, x.level()));
3632
}
3733

3834
/**
@@ -41,16 +37,13 @@ public static <T extends FishEventHandler> void callEvents(Map<T, Integer> value
4137
* @param event The {@link Event} to call
4238
* @param level The level of the event
4339
* @param <T> The event type to call
44-
*
45-
* @return The event that was called
4640
*/
4741
@SuppressWarnings("unchecked")
48-
public <T extends Event> T callEvent(T event, int level) {
42+
public <T extends Event> void callEvent(T event, int level) {
4943
EventWrapper<?> wrapper = this.events.get(event.getClass());
50-
if (wrapper == null) return event;
44+
if (wrapper == null) return;
5145

5246
((EventWrapper<T>) wrapper).function().accept(event, level);
53-
return event;
5447
}
5548

5649
/**

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package dev.oribuin.fishing.api.event;
22

3-
import com.google.common.base.Supplier;
43
import dev.oribuin.fishing.model.augment.Augment;
5-
import dev.oribuin.fishing.model.augment.AugmentRegistry;
64
import org.bukkit.event.Event;
75

86
import java.util.Map;
@@ -13,7 +11,7 @@
1311
public class MutableEventWrapper<T extends FishEventHandler> {
1412

1513
private final T type;
16-
private final int level;
14+
private final Integer level;
1715
private final EventWrapper<?> wrapper;
1816

1917
/**
@@ -25,7 +23,7 @@ public class MutableEventWrapper<T extends FishEventHandler> {
2523
*
2624
* @see FishEventHandler#callEvents(Map, Event) Where this is used
2725
*/
28-
public MutableEventWrapper(T type, int level, Event event) {
26+
public MutableEventWrapper(T type, Integer level, Event event) {
2927
this.type = type;
3028
this.level = level;
3129
this.wrapper = type.getWrapper(event.getClass());
@@ -43,6 +41,17 @@ public MutableEventWrapper(Map.Entry<T, Integer> entry, Event event) {
4341
this(entry.getKey(), entry.getValue(), event);
4442
}
4543

44+
/**
45+
* Compare the priority of two events to determine which one should be called first
46+
*
47+
* @param other The other event to compare
48+
*
49+
* @return The comparison of the two events
50+
*/
51+
public int compare(MutableEventWrapper<T> other) {
52+
return Integer.compare(this.wrapper.order().getSlot(), other.wrapper().order().getSlot());
53+
}
54+
4655
/**
4756
* Get the augment that was registered
4857
*
@@ -57,7 +66,7 @@ public T type() {
5766
*
5867
* @return The level of the augment that was registered
5968
*/
60-
public int level() {
69+
public Integer level() {
6170
return level;
6271
}
6372

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.apache.commons.lang3.StringUtils;
1515
import org.bukkit.Material;
1616
import org.bukkit.NamespacedKey;
17+
import org.bukkit.event.Event;
1718
import org.jetbrains.annotations.NotNull;
1819

1920
import java.nio.file.Path;
@@ -193,6 +194,19 @@ public final StringPlaceholders placeholders() {
193194
.build();
194195
}
195196

197+
/**
198+
* Call an event from the handler's registered events, This will not take priority into account.
199+
*
200+
* @param event The {@link Event} to call
201+
* @param level The level of the event
202+
*/
203+
@Override
204+
public <T extends Event> void callEvent(T event, int level) {
205+
if (!this.enabled) return;
206+
207+
super.callEvent(event, level);
208+
}
209+
196210
/**
197211
* Information about the augment which will be displayed in top of the augment configuration file
198212
*

src/main/java/dev/oribuin/fishing/model/totem/upgrade/TotemUpgrade.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,27 @@
33
import dev.oribuin.fishing.api.event.FishEventHandler;
44
import dev.oribuin.fishing.config.Configurable;
55
import dev.oribuin.fishing.model.economy.Cost;
6+
import dev.oribuin.fishing.model.item.ItemConstruct;
67
import dev.oribuin.fishing.model.totem.Totem;
8+
import org.bukkit.Material;
79
import org.jetbrains.annotations.NotNull;
810

911
import java.nio.file.Path;
1012

1113
public abstract class TotemUpgrade extends FishEventHandler implements Configurable {
12-
13-
/**
14-
* The name of the totem upgrade to be displayed to the player
15-
*
16-
* @return The name of the upgrade
17-
*/
18-
public abstract String name();
19-
20-
/**
21-
* Apply the upgrade to the totem object
22-
*
23-
* @param totem The totem object to apply the upgrade to
24-
* @param level The level of the upgrade
25-
*/
26-
public void applyTo(Totem totem, int level) {
14+
15+
protected final String name;
16+
private boolean enabled;
17+
private ItemConstruct icon;
18+
private int maxLevel;
19+
20+
public TotemUpgrade(String name) {
21+
this.name = name;
22+
this.enabled = true;
23+
this.icon = ItemConstruct.of(Material.STONE); // TODO: Change this to a custom item
24+
this.maxLevel = 1;
2725
}
28-
29-
/**
30-
* Get the cost of the upgrade for a specific level
31-
*
32-
* @param level The level of the upgrade
33-
*
34-
* @return The cost of the upgrade
35-
*/
36-
public abstract Cost costFor(int level);
37-
26+
3827
/**
3928
* The path to the configuration file to be loaded. All paths will be relative to the {@link #parentFolder()},
4029
* If you wish to overwrite this functionality, override the {@link #parentFolder()} method
@@ -43,7 +32,7 @@ public void applyTo(Totem totem, int level) {
4332
*/
4433
@Override
4534
public @NotNull Path configPath() {
46-
return Path.of("totem", "upgrade", this.name().toLowerCase() + ".yml");
35+
return Path.of("totem", "upgrade", this.name.toLowerCase() + ".yml");
4736
}
4837

4938
}

src/main/java/dev/oribuin/fishing/model/totem/upgrade/impl/UpgradeTotemRadius.java

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,49 +19,8 @@ public class UpgradeTotemRadius extends TotemUpgrade implements Configurable {
1919

2020
private final Map<Integer, Tier> tiers = new HashMap<>();
2121

22-
/**
23-
* The name of the totem upgrade to be displayed to the player
24-
*
25-
* @return The name of the upgrade
26-
*/
27-
@Override
28-
public String name() {
29-
return "Radius";
30-
}
31-
32-
/**
33-
* Apply the upgrade to the totem object
34-
*
35-
* @param totem The totem object to apply the upgrade to
36-
* @param level The level of the upgrade
37-
*/
38-
@Override
39-
public void applyTo(Totem totem, int level) {
40-
Tier tier = this.tiers.get(level);
41-
if (tier == null) {
42-
FishingPlugin.get().getLogger().warning("Failed to apply upgrade: " + this.name() + " to totem, The tier does not exist.");
43-
return;
44-
}
45-
46-
totem.radius(tier.newRadius());
47-
}
48-
49-
/**
50-
* Get the cost of the upgrade for a specific level
51-
*
52-
* @param level The level of the upgrade
53-
*
54-
* @return The cost of the upgrade
55-
*/
56-
@Override
57-
public Cost costFor(int level) {
58-
Tier tier = this.tiers.get(level);
59-
if (tier == null) {
60-
FishingPlugin.get().getLogger().warning("Failed to get cost for upgrade: " + this.name() + ", The tier does not exist.");
61-
return new Cost(CurrencyRegistry.ENTROPY, 0);
62-
}
63-
64-
return this.tiers.get(level).cost();
22+
public UpgradeTotemRadius() {
23+
super("radius");
6524
}
6625

6726
/**
@@ -108,7 +67,7 @@ public void loadSettings(@NotNull CommentedConfigurationSection config) {
10867
int level = Integer.parseInt(key);
10968
this.tiers.put(level, new Tier(Cost.of(this.pullSection(section, "cost")), newRadius));
11069
} catch (IllegalArgumentException ex) {
111-
FishingPlugin.get().getLogger().warning("Failed to load tier: " + key + " in upgrade: " + this.name() + ", The tier must be a number.");
70+
FishingPlugin.get().getLogger().warning("Failed to load tier: " + key + " in upgrade: " + this.name + ", The tier must be a number.");
11271
}
11372
});
11473
}

0 commit comments

Comments
 (0)