Skip to content

Commit fed0ddf

Browse files
author
Oribuin
committed
more upgrade stuff + gui
Took 1 hour 11 minutes
1 parent fbd9269 commit fed0ddf

File tree

17 files changed

+334
-96
lines changed

17 files changed

+334
-96
lines changed

src/main/java/dev/oribuin/fishing/FishingPlugin.java

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

3+
import dev.oribuin.fishing.gui.MenuRegistry;
34
import dev.oribuin.fishing.model.augment.AugmentRegistry;
45
import dev.oribuin.fishing.config.Setting;
56
import dev.oribuin.fishing.listener.FishListener;
@@ -54,6 +55,7 @@ public void reload() {
5455
UpgradeRegistry.reload();
5556
ItemRegistry.init();
5657
SkillRegistry.init();
58+
MenuRegistry.reload();
5759

5860
super.reload();
5961
}

src/main/java/dev/oribuin/fishing/gui/MenuRegistry.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import dev.oribuin.fishing.gui.codex.impl.AugmentCodexMenu;
55
import dev.oribuin.fishing.gui.codex.impl.FishCodexMenu;
66
import dev.oribuin.fishing.gui.totem.TotemMainMenu;
7+
import dev.oribuin.fishing.gui.totem.TotemUpgradeMenu;
78
import org.jetbrains.annotations.NotNull;
89

910
import java.util.HashMap;
@@ -12,16 +13,18 @@
1213

1314
public class MenuRegistry {
1415

15-
private static final Map<Class<?> , PluginMenu<?>> menus = new HashMap<>();
16-
16+
private static final Map<Class<?>, PluginMenu<?>> menus = new HashMap<>();
17+
1718
static {
1819
// Codex menus
1920
register(AugmentCodexMenu::new);
2021
register(FishCodexMenu::new);
21-
22+
2223
// Totem Menus
2324
register(TotemMainMenu::new);
25+
register(TotemUpgradeMenu::new);
2426
}
27+
2528
/**
2629
* Register a new menu to the registry with the specified name
2730
*
@@ -40,7 +43,7 @@ public static <T extends PluginMenu<?>> T register(@NotNull Supplier<T> menu) {
4043

4144
/**
4245
* Get a registered menu by the specified class
43-
*
46+
*
4447
* @param menu The menu class
4548
*
4649
* @return The menu
@@ -49,6 +52,13 @@ public static <T extends PluginMenu<?>> T register(@NotNull Supplier<T> menu) {
4952
public static <T extends PluginMenu<?>> T get(Class<T> menu) {
5053
return (T) menus.get(menu);
5154
}
52-
53-
55+
56+
/**
57+
* Reload all the menus in the registry
58+
*/
59+
public static void reload() {
60+
menus.values().forEach(PluginMenu::reload);
61+
}
62+
63+
5464
}

src/main/java/dev/oribuin/fishing/gui/totem/TotemMainMenu.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package dev.oribuin.fishing.gui.totem;
22

33
import dev.oribuin.fishing.api.gui.PluginMenu;
4+
import dev.oribuin.fishing.gui.MenuRegistry;
45
import dev.oribuin.fishing.model.totem.Totem;
56
import dev.oribuin.fishing.storage.util.KeyRegistry;
7+
import dev.rosewood.rosegarden.utils.StringPlaceholders;
68
import dev.triumphteam.gui.guis.Gui;
79
import org.bukkit.entity.Player;
810
import org.bukkit.event.inventory.InventoryCloseEvent;
@@ -23,7 +25,7 @@ public void open(Totem totem, Player player) {
2325
Gui gui = this.createRegular();
2426
this.placeExtras(totem.placeholders());
2527
this.updateTask(() -> this.placeDynamics(totem, player));
26-
28+
2729
gui.open(player);
2830
}
2931

@@ -34,22 +36,24 @@ public void open(Totem totem, Player player) {
3436
* @param player The player to place the items for
3537
*/
3638
private void placeDynamics(Totem totem, Player player) {
37-
this.placeItem("totem-stats", totem.placeholders());
39+
StringPlaceholders placeholders = totem.placeholders();
40+
this.placeItem("totem-stats", placeholders);
41+
this.placeItem("totem-upgrade", placeholders, x-> MenuRegistry.get(TotemUpgradeMenu.class).open(totem, player));
3842

3943
// The totem is active, display the active totem item
4044
boolean active = totem.getProperty(KeyRegistry.TOTEM_ACTIVE, false);
4145
if (active) {
42-
this.placeItem("totem-active", totem.placeholders());
46+
this.placeItem("totem-active", placeholders);
4347
}
44-
48+
4549
// The totem is not active and is on cooldown, display the cooldown item
4650
if (!active && totem.onCooldown()) {
47-
this.placeItem("totem-cooldown", totem.placeholders());
51+
this.placeItem("totem-cooldown", placeholders);
4852
}
4953

5054
// The totem is not active and is not on cooldown, display the button to activate the totem
5155
if (!active && !totem.onCooldown()) {
52-
this.placeItem("totem-activate", totem.placeholders(), x -> {
56+
this.placeItem("totem-activate", placeholders, x -> {
5357
totem.activate(); // Activate the totem
5458
player.sendMessage("§aYou have activated the totem!");
5559
player.closeInventory(InventoryCloseEvent.Reason.PLUGIN); // Close the player's inventory
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package dev.oribuin.fishing.gui.totem;
2+
3+
import dev.oribuin.fishing.api.gui.PluginMenu;
4+
import dev.oribuin.fishing.model.item.ItemConstruct;
5+
import dev.oribuin.fishing.model.totem.Totem;
6+
import dev.triumphteam.gui.guis.Gui;
7+
import dev.triumphteam.gui.guis.GuiItem;
8+
import dev.triumphteam.gui.guis.PaginatedGui;
9+
import org.bukkit.entity.Player;
10+
import org.bukkit.event.inventory.InventoryCloseEvent;
11+
import org.bukkit.inventory.ItemStack;
12+
13+
public class TotemUpgradeMenu extends PluginMenu<PaginatedGui> {
14+
15+
public TotemUpgradeMenu() {
16+
super("totem/upgrades");
17+
}
18+
19+
/**
20+
* Open the GUI for the specified player
21+
*
22+
* @param totem The totem to open the GUI for
23+
* @param player The player to open the GUI for
24+
*/
25+
public void open(Totem totem, Player player) {
26+
PaginatedGui gui = this.createPaginated();
27+
this.placeExtras(totem.placeholders());
28+
this.placeItem("forward", x -> gui.next());
29+
this.placeItem("back", x -> gui.previous());
30+
this.placeUpgrades(gui, totem, player);
31+
32+
gui.open(player);
33+
}
34+
35+
/**
36+
* Place the dynamic items in the GUI for the totem
37+
*
38+
* @param totem The totem to place the items for
39+
* @param player The player to place the items for
40+
*/
41+
private void placeUpgrades(PaginatedGui gui, Totem totem, Player player) {
42+
gui.clearPageItems();
43+
totem.upgrades().forEach((upgrade, level) -> {
44+
ItemConstruct displayItem = upgrade.icon();
45+
if (displayItem == null) return;
46+
47+
ItemStack item = displayItem.build(upgrade.placeholders(totem));
48+
// todo: make less ugly
49+
gui.addItem(new GuiItem(item, x -> {
50+
if (upgrade.levelup(player, totem)) {
51+
this.placeUpgrades(gui, totem, player);
52+
}
53+
}));
54+
});
55+
56+
gui.update();
57+
}
58+
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package dev.oribuin.fishing.manager;
22

3+
import dev.oribuin.fishing.FishingPlugin;
34
import dev.oribuin.fishing.api.event.FishEventHandler;
45
import dev.oribuin.fishing.api.event.impl.FishGenerateEvent;
56
import dev.oribuin.fishing.api.event.impl.InitialFishCatchEvent;
67
import dev.oribuin.fishing.model.augment.Augment;
78
import dev.oribuin.fishing.model.augment.AugmentRegistry;
89
import dev.oribuin.fishing.model.fish.Fish;
10+
import dev.oribuin.fishing.model.totem.Totem;
911
import dev.rosewood.rosegarden.RosePlugin;
1012
import dev.rosewood.rosegarden.manager.Manager;
1113
import org.bukkit.entity.FishHook;
@@ -43,7 +45,13 @@ public List<Fish> tryCatch(Player player, ItemStack rod, FishHook hook) {
4345

4446
// Run the augments onInitialCatch method
4547
FishEventHandler.callEvents(augments, event);
46-
48+
49+
// Run Totem Stuff
50+
Totem nearest = FishingPlugin.get().getManager(TotemManager.class).getClosestActive(hook.getLocation());
51+
if (nearest != null) {
52+
FishEventHandler.callEvents(nearest.upgrades(), event);
53+
}
54+
4755
// Cancel the event if it is cancelled
4856
if (event.isCancelled()) return result;
4957

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ public boolean isRegistered(Totem totem) {
113113
* @return The closest active totem.
114114
*/
115115
public Totem getClosestActive(Location location) {
116+
if (this.totems.isEmpty()) return null;
117+
116118
return this.totems.values().stream().filter(x -> x.getProperty(KeyRegistry.TOTEM_ACTIVE)).min((t1, t2) -> {
117119
double distance1 = t1.center().distance(location);
118120
double distance2 = t2.center().distance(location);

src/main/java/dev/oribuin/fishing/model/item/ItemRegistry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void init() {
3535
public static void registerCustomItem(String key, ItemConstruct item) {
3636
CUSTOM_ITEMS.put(key.toUpperCase(), item);
3737
}
38-
38+
3939
/**
4040
* Get an {@link ItemConstruct} from the key provided
4141
*
@@ -55,6 +55,7 @@ public static ItemConstruct from(String key) {
5555
return null;
5656
}
5757
}
58+
5859

5960
/**
6061
* Initialize a {@link CommentedConfigurationSection} from a configuration file to establish the settings

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public Totem(@Nullable Location center, @Nullable Player owner) {
5858
this.applyProperty(UUID, TOTEM_OWNER, owner == null ? null : owner.getUniqueId());
5959
this.applyProperty(STRING, TOTEM_OWNER_NAME, owner == null ? "Unknown" : owner.getName());
6060

61-
6261
// Load the upgrades
6362
this.upgrades = UpgradeRegistry.from(this);
6463

@@ -99,6 +98,7 @@ public void tickAsync() {
9998
// Spawn additional particles around the totem bounds while active
10099
if (active) {
101100
ParticleBuilder dust = this.dust(Color.LIME);
101+
this.bounds = this.bounds(); // regularly update the bounds of the totem
102102
this.bounds.forEach(x -> dust.clone().location(x.clone().add(0, 1.5, 0)).spawn());
103103
}
104104

@@ -136,6 +136,7 @@ public void activate() {
136136
return;
137137
}
138138

139+
this.bounds = this.bounds(); // Update the bounds of the totem
139140
this.setProperty(TOTEM_ACTIVE, true);
140141
this.setProperty(TOTEM_LAST_ACTIVE, System.currentTimeMillis());
141142
this.update();
@@ -243,7 +244,13 @@ public StringPlaceholders placeholders() {
243244
// Add the upgrade placeholders
244245
this.upgrades.forEach((upgrade, level) -> {
245246
builder.add("upgrade_" + upgrade.name(), level);
246-
builder.addAll(upgrade.placeholders(this));
247+
248+
// Add all the placeholders for the upgrade
249+
upgrade.placeholders(this)
250+
.getPlaceholders()
251+
.forEach((key, value) ->
252+
builder.add(String.format("upgrade_%s_%s", upgrade.name(), key), value)
253+
);
247254
});
248255

249256
return builder.build();
@@ -328,7 +335,7 @@ public boolean withinRadius(Location location) {
328335
// Radius will be in a circle around the center
329336
if (location.getWorld() != this.center.getWorld()) return false;
330337

331-
return location.distance(this.center) <= UpgradeRegistry.RADIUS_UPGRADE.calculateRadius(this);
338+
return location.distance(this.center) <= UpgradeRegistry.RADIUS_UPGRADE.calculateRadius(this);
332339
}
333340

334341
/**
@@ -362,9 +369,9 @@ public static ItemConstruct defaultItem() {
362369
"",
363370
"&#4f73d6Information",
364371
" &#4f73d6- &7Owner: &f%owner%",
365-
" &#4f73d6- &7Radius: &f%radius% blocks",
366-
" &#4f73d6- &7Duration: &f%duration%",
367-
" &#4f73d6- &7Cooldown: &f%cooldown%",
372+
" &#4f73d6- &7Radius: &f%upgrade_radius_value% blocks",
373+
" &#4f73d6- &7Duration: &f%upgrade_duration_value%",
374+
" &#4f73d6- &7Cooldown: &f%upgrade_cooldown_value%",
368375
""
369376
)
370377
.glowing(true)
@@ -393,6 +400,24 @@ public void entity(ArmorStand entity) {
393400
}
394401
}
395402

403+
/**
404+
* Get the upgrades of the totem
405+
*
406+
* @return The upgrades of the totem
407+
*/
408+
public Map<TotemUpgrade, Integer> upgrades() {
409+
return upgrades;
410+
}
411+
412+
/**
413+
* Set the upgrades of the totem
414+
*
415+
* @param upgrades The upgrades of the totem
416+
*/
417+
public void upgrades(Map<TotemUpgrade, Integer> upgrades) {
418+
this.upgrades = upgrades;
419+
}
420+
396421
/**
397422
* The delay between each tick, Set to Duration#ZERO for no delay
398423
*

0 commit comments

Comments
 (0)