Skip to content

Commit f278389

Browse files
author
Oribuin
committed
totem fixes
Took 23 minutes
1 parent fed0ddf commit f278389

File tree

9 files changed

+173
-24
lines changed

9 files changed

+173
-24
lines changed

src/main/java/dev/oribuin/fishing/api/event/def/TotemEvents.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@
1515
public interface TotemEvents {
1616

1717
/**
18-
* The functionality provided when a player activates a fishing totem ability
18+
* The functionality provided when a player activates the fishing totem
1919
*
2020
* @param event The event that was called when a player activates a totem
2121
*/
2222
default void onActivate(TotemActivateEvent event) {}
2323

24+
/**
25+
* The functionality provided when the fishing totem deactivates
26+
*
27+
* @param event The event that was called when the totem deactivates
28+
*/
29+
default void onDeactivate(TotemActivateEvent event) {}
30+
2431
}

src/main/java/dev/oribuin/fishing/api/event/impl/FishCatchEvent.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dev.oribuin.fishing.model.augment.Augment;
44
import dev.oribuin.fishing.model.augment.AugmentRegistry;
55
import dev.oribuin.fishing.model.fish.Fish;
6+
import dev.oribuin.fishing.model.fish.Tier;
67
import org.bukkit.Bukkit;
78
import org.bukkit.entity.FishHook;
89
import org.bukkit.entity.Player;
@@ -25,6 +26,10 @@ public class FishCatchEvent extends PlayerEvent implements Cancellable {
2526
private final ItemStack rod;
2627
private final FishHook hook;
2728
private Fish fish;
29+
30+
private final int baseEntropy;
31+
private final int baseFishExp;
32+
private final float baseNaturalExp;
2833
private int entropy;
2934
private int fishExp;
3035
private float naturalExp;
@@ -47,9 +52,15 @@ public FishCatchEvent(@NotNull Player who, @NotNull ItemStack rod, @NotNull Fish
4752
this.rod = rod;
4853
this.hook = hook;
4954
this.fish = fish;
50-
this.entropy = fish.tier().entropy();
51-
this.fishExp = fish.tier().fishExp();
52-
this.naturalExp = fish.tier().naturalExp();
55+
56+
// Set the base values for the fish
57+
Tier tier = this.fish.tier();
58+
this.baseEntropy = tier.entropy();
59+
this.entropy = tier.entropy();
60+
this.baseFishExp = tier.fishExp();
61+
this.fishExp = tier.fishExp();
62+
this.baseNaturalExp = tier.naturalExp();
63+
this.naturalExp = tier.naturalExp();
5364
}
5465

5566
/**
@@ -63,7 +74,7 @@ public Map<Augment, Integer> augments() {
6374

6475
/**
6576
* The fishing rod the player is using to catch the fish
66-
*
77+
*-
6778
* @return The fishing rod {@link ItemStack}
6879
*/
6980
public @NotNull ItemStack rod() {
@@ -96,6 +107,15 @@ public Map<Augment, Integer> augments() {
96107
public void fish(@Nullable Fish fish) {
97108
this.fish = fish;
98109
}
110+
111+
/**
112+
* The base amount of entropy the fish gives
113+
*
114+
* @return The base amount of entropy the fish gives
115+
*/
116+
public int baseEntropy() {
117+
return baseEntropy;
118+
}
99119

100120
/**
101121
* The amount of entropy the fish gives
@@ -114,6 +134,15 @@ public int entropy() {
114134
public void entropy(int entropy) {
115135
this.entropy = entropy;
116136
}
137+
138+
/**
139+
* The base amount of plugin experience the fish gives
140+
*
141+
* @return The base amount of experience the fish gives
142+
*/
143+
public int baseFishExp() {
144+
return baseFishExp;
145+
}
117146

118147
/**
119148
* The amount of plugin experience the fish gives
@@ -132,6 +161,15 @@ public int fishExp() {
132161
public void fishExp(int fishExp) {
133162
this.fishExp = fishExp;
134163
}
164+
165+
/**
166+
* The base minecraft experience the fish gives
167+
*
168+
* @return The base minecraft experience the fish gives
169+
*/
170+
public float baseNaturalExp() {
171+
return baseNaturalExp;
172+
}
135173

136174
/**
137175
* The base minecraft experience the fish gives
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package dev.oribuin.fishing.api.event.impl;
2+
3+
import dev.oribuin.fishing.model.totem.Totem;
4+
import org.bukkit.Bukkit;
5+
import org.bukkit.entity.Player;
6+
import org.bukkit.event.Cancellable;
7+
import org.bukkit.event.Event;
8+
import org.bukkit.event.HandlerList;
9+
import org.bukkit.event.player.PlayerEvent;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
/**
13+
* This event is called when a player activates a fishing totem
14+
*/
15+
public class TotemDeactivateEvent extends Event implements Cancellable {
16+
17+
private static final HandlerList HANDLERS = new HandlerList();
18+
private final Totem totem;
19+
private boolean cancelled;
20+
21+
/**
22+
* Define a new Totem Deactivate Event, This is called when a totem deactivates by running out of time.
23+
*
24+
* @param totem The {@link Totem} that is being deactivated
25+
*/
26+
public TotemDeactivateEvent(@NotNull Totem totem) {
27+
super(!Bukkit.isPrimaryThread());
28+
29+
this.totem = totem;
30+
}
31+
32+
/**
33+
* Get the totem that is being deactivated
34+
*
35+
* @return The totem that is being deactivated
36+
*/
37+
public @NotNull Totem totem() {
38+
return this.totem;
39+
}
40+
41+
/**
42+
* Get the handlers for this event class
43+
*
44+
* @return The handlers for this event class
45+
*/
46+
@Override
47+
public @NotNull HandlerList getHandlers() {
48+
return HANDLERS;
49+
}
50+
51+
/**
52+
* Get the handlers for this event class
53+
*
54+
* @return The handlers for this event class
55+
*/
56+
public static HandlerList getHandlerList() {
57+
return HANDLERS;
58+
}
59+
60+
/**
61+
* Check if the event is cancelled
62+
*
63+
* @return If the event is cancelled
64+
*/
65+
@Override
66+
public boolean isCancelled() {
67+
return this.cancelled;
68+
}
69+
70+
/**
71+
* Set the event to be cancelled
72+
*
73+
* @param b If the event should be cancelled
74+
*/
75+
@Override
76+
public void setCancelled(boolean b) {
77+
this.cancelled = b;
78+
}
79+
80+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void onFish(PlayerFishEvent event) {
3737
FishManager manager = this.plugin.getManager(FishManager.class);
3838

3939
// If caught no fish, do nothing
40-
List<Fish> caught = manager.tryCatch(event.getPlayer(), hand, event.getHook());
40+
List<Fish> caught = manager.tryCatch(event);
4141
if (caught.isEmpty()) return;
4242

4343
// Add the fish into the player inventory

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import dev.rosewood.rosegarden.manager.Manager;
1313
import org.bukkit.entity.FishHook;
1414
import org.bukkit.entity.Player;
15+
import org.bukkit.event.Event;
16+
import org.bukkit.event.player.PlayerFishEvent;
1517
import org.bukkit.inventory.ItemStack;
1618

1719
import java.util.ArrayList;
@@ -31,32 +33,41 @@ public void reload() {
3133
/**
3234
* Try to catch a fish from the tier based on the player's fishing rod and fishhook
3335
*
34-
* @param player The player to check
35-
* @param rod The fishing rod the player is using
36-
* @param hook The fishhook the player is using
36+
* @param event The initial player fish event to catch the fish
3737
*
3838
* @return The fish the player caught
3939
*/
40-
public List<Fish> tryCatch(Player player, ItemStack rod, FishHook hook) {
40+
public List<Fish> tryCatch(PlayerFishEvent event) {
41+
if (event.getHand() == null) return new ArrayList<>();
42+
4143
List<Fish> result = new ArrayList<>();
44+
ItemStack rod = event.getPlayer().getInventory().getItem(event.getHand());
4245
Map<Augment, Integer> augments = AugmentRegistry.from(rod);
46+
Totem nearest = this.rosePlugin.getManager(TotemManager.class).getClosestActive(event.getHook().getLocation());
4347

44-
InitialFishCatchEvent event = new InitialFishCatchEvent(player, rod, hook);
48+
FishEventHandler.callEvents(augments, event);
49+
50+
if (nearest != null) {
51+
FishEventHandler.callEvents(nearest.upgrades(), event);
52+
}
53+
54+
if (event.isCancelled()) return result; // Cancel the event if it is cancelled
55+
56+
InitialFishCatchEvent catchEvent = new InitialFishCatchEvent(event.getPlayer(), rod, event.getHook());
4557

4658
// Run the augments onInitialCatch method
47-
FishEventHandler.callEvents(augments, event);
59+
FishEventHandler.callEvents(augments, catchEvent);
4860

4961
// Run Totem Stuff
50-
Totem nearest = FishingPlugin.get().getManager(TotemManager.class).getClosestActive(hook.getLocation());
5162
if (nearest != null) {
52-
FishEventHandler.callEvents(nearest.upgrades(), event);
63+
FishEventHandler.callEvents(nearest.upgrades(), catchEvent);
5364
}
5465

5566
// Cancel the event if it is cancelled
56-
if (event.isCancelled()) return result;
67+
if (catchEvent.isCancelled()) return result;
5768

58-
for (int i = 0; i < event.getAmountToCatch(); i++) {
59-
result.add(this.generateFish(augments, player, rod, hook));
69+
for (int i = 0; i < catchEvent.getAmountToCatch(); i++) {
70+
result.add(this.generateFish(augments, event.getPlayer().getPlayer(), rod, event.getHook()));
6071
}
6172

6273
return result;

src/main/java/dev/oribuin/fishing/model/augment/impl/AugmentEnlightened.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
public class AugmentEnlightened extends Augment {
1616

17-
private String formula = "(%entropy% + %level%) * 0.03";
17+
private String formula = "(%xp% + %level%) * 0.03";
1818

1919
/**
2020
* Create a new type of augment with a name and description.
@@ -37,7 +37,7 @@ public AugmentEnlightened() {
3737
*/
3838
@Override
3939
public void onFishCatch(FishCatchEvent event, int level) {
40-
StringPlaceholders plc = StringPlaceholders.of("level", level, "xp", event.fishExp());
40+
StringPlaceholders plc = StringPlaceholders.of("level", level, "xp", event.baseFishExp());
4141
double xp = FishUtils.evaluate(plc.apply(this.formula));
4242
event.fishExp((int) xp);
4343
}

src/main/java/dev/oribuin/fishing/model/augment/impl/AugmentIntuition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public AugmentIntuition() {
4040
*/
4141
@Override
4242
public void onFishCatch(FishCatchEvent event, int level) {
43-
StringPlaceholders plc = StringPlaceholders.of("level", level, "entropy", event.entropy());
43+
StringPlaceholders plc = StringPlaceholders.of("level", level, "entropy", event.baseEntropy());
4444
double entropy = FishUtils.evaluate(plc.apply(this.formula));
4545
event.entropy((int) entropy);
4646
}

src/main/java/dev/oribuin/fishing/model/totem/Totem.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.destroystokyo.paper.ParticleBuilder;
44
import dev.oribuin.fishing.FishingPlugin;
55
import dev.oribuin.fishing.api.Propertied;
6+
import dev.oribuin.fishing.api.event.FishEventHandler;
7+
import dev.oribuin.fishing.api.event.impl.TotemActivateEvent;
8+
import dev.oribuin.fishing.api.event.impl.TotemDeactivateEvent;
69
import dev.oribuin.fishing.api.task.AsyncTicker;
710
import dev.oribuin.fishing.manager.TotemManager;
811
import dev.oribuin.fishing.model.item.ItemConstruct;
@@ -124,13 +127,18 @@ public void tickAsync() {
124127
this.rotation = 0;
125128
this.entity.setHeadRotations(Rotations.ZERO);
126129
this.update(); // Update the totem
130+
131+
// Call the totem activate event on upgrades
132+
FishEventHandler.callEvents(this.upgrades, new TotemDeactivateEvent(this));
127133
}
128134
}
129135

130136
/**
131137
* Activate the totem for the player to use
138+
*
139+
* @param player The activating player
132140
*/
133-
public void activate() {
141+
public void activate(Player player) {
134142
if (this.onCooldown()) {
135143
FishingPlugin.get().getLogger().warning("Failed to activate totem, The totem is on cooldown.");
136144
return;
@@ -140,6 +148,9 @@ public void activate() {
140148
this.setProperty(TOTEM_ACTIVE, true);
141149
this.setProperty(TOTEM_LAST_ACTIVE, System.currentTimeMillis());
142150
this.update();
151+
152+
// Call the totem activate event on upgrades
153+
FishEventHandler.callEvents(this.upgrades, new TotemActivateEvent(this, player));
143154
}
144155

145156
/**
@@ -335,7 +346,7 @@ public boolean withinRadius(Location location) {
335346
// Radius will be in a circle around the center
336347
if (location.getWorld() != this.center.getWorld()) return false;
337348

338-
return location.distance(this.center) <= UpgradeRegistry.RADIUS_UPGRADE.calculateRadius(this);
349+
return location.distance(this.center) <= UpgradeRegistry.RADIUS_UPGRADE.calculateRadius(this);
339350
}
340351

341352
/**

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public StringPlaceholders placeholders(Totem totem) {
4646
}
4747

4848
/**
49-
* Calculate the radius of the totem based on the level of the upgrade
49+
* Calculate the radius of the totem based on the level of the upgrade.
50+
* <p>
51+
* Radius is divided by 2 so it acts as a radius instead of a diameter.
5052
*
5153
* @param totem The totem to calculate the radius for
5254
*
@@ -55,7 +57,7 @@ public StringPlaceholders placeholders(Totem totem) {
5557
public int calculateRadius(Totem totem) {
5658
Integer level = totem.getProperty(this.key(), this.defaultLevel());
5759
StringPlaceholders plc = StringPlaceholders.of("level", level);
58-
return (int) FishUtils.evaluate(plc.apply(this.radiusFormula));
60+
return (int) FishUtils.evaluate(plc.apply(this.radiusFormula)) / 2;
5961
}
6062

6163
/**

0 commit comments

Comments
 (0)