Skip to content

Commit

Permalink
Implements visit/warp actions in top gui
Browse files Browse the repository at this point in the history
Add 2 new actions for island buttons in TOP GUI:
- Visit -> allows to visit island, but it requires Visit Addon
- Warp -> allows to warp to island, but it requires Warp Addon

Requested via Discord.
  • Loading branch information
BONNe committed Aug 21, 2022
1 parent 90ae98e commit dae3db6
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 11 deletions.
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
<!-- More visible way how to change dependency versions -->
<spigot.version>1.16.5-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.20.0</bentobox.version>
<!-- Warps addon version -->
<warps.version>1.12.0</warps.version>
<!-- Visit addon version -->
<visit.version>1.4.0</visit.version>
<!-- Panel Utils version -->
<panelutils.version>1.1.0</panelutils.version>
<!-- Revision variable removes warning about dynamic version -->
Expand Down Expand Up @@ -185,6 +189,18 @@
<version>${bentobox.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>world.bentobox</groupId>
<artifactId>warps</artifactId>
<version>${warps.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>world.bentobox</groupId>
<artifactId>visit</artifactId>
<version>${visit.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>lv.id.bonne</groupId>
<artifactId>panelutils</artifactId>
Expand Down
76 changes: 74 additions & 2 deletions src/main/java/world/bentobox/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
import world.bentobox.level.objects.TopTenData;
import world.bentobox.level.requests.LevelRequestHandler;
import world.bentobox.level.requests.TopTenRequestHandler;
import world.bentobox.visit.VisitAddon;
import world.bentobox.warps.Warp;


/**
* @author tastybento
Expand All @@ -63,6 +66,17 @@ public class Level extends Addon implements Listener {
private boolean roseStackersEnabled;
private final List<GameModeAddon> registeredGameModes = new ArrayList<>();

/**
* Local variable that stores if warpHook is present.
*/
private Warp warpHook;

/**
* Local variable that stores if visitHook is present.
*/
private VisitAddon visitHook;


@Override
public void onLoad() {
// Save the default config from config.yml
Expand Down Expand Up @@ -125,10 +139,10 @@ public void onEnable() {
advChestEnabled = advChest != null;
if (advChestEnabled) {
// Check version
if (compareVersions(advChest.getDescription().getVersion(), "14.2") > 0) {
if (compareVersions(advChest.getDescription().getVersion(), "23.0") > 0) {
log("Hooked into AdvancedChests.");
} else {
logError("Could not hook into AdvancedChests " + advChest.getDescription().getVersion() + " - requires version 14.3 or later");
logError("Could not hook into AdvancedChests " + advChest.getDescription().getVersion() + " - requires version 23.0 or later");
advChestEnabled = false;
}
}
Expand All @@ -139,6 +153,45 @@ public void onEnable() {
}
}

@Override
public void allLoaded()
{
super.allLoaded();

if (this.isEnabled())
{
this.hookExtensions();
}
}


/**
* This method tries to hook into addons and plugins
*/
private void hookExtensions()
{
// Try to find Visit addon and if it does not exist, display a warning
this.getAddonByName("Visit").ifPresentOrElse(addon ->
{
this.visitHook = (VisitAddon) addon;
this.log("Likes Addon hooked into Visit addon.");
}, () ->
{
this.visitHook = null;
});

// Try to find Warps addon and if it does not exist, display a warning
this.getAddonByName("Warps").ifPresentOrElse(addon ->
{
this.warpHook = (Warp) addon;
this.log("Likes Addon hooked into Warps addon.");
}, () ->
{
this.warpHook = null;
});
}


/**
* Compares versions
* @param version1
Expand Down Expand Up @@ -507,4 +560,23 @@ public boolean isRoseStackersEnabled() {
return roseStackersEnabled;
}

/**
* Method Level#getVisitHook returns the visitHook of this object.
*
* @return {@code Visit} of this object, {@code null} otherwise.
*/
public VisitAddon getVisitHook()
{
return this.visitHook;
}

/**
* Method Level#getWarpHook returns the warpHook of this object.
*
* @return {@code Warp} of this object, {@code null} otherwise.
*/
public Warp getWarpHook()
{
return this.warpHook;
}
}
63 changes: 54 additions & 9 deletions src/main/java/world/bentobox/level/panels/TopLevelPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.*;
import java.util.stream.Collectors;

import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.TemplatedPanel;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
Expand Down Expand Up @@ -185,22 +186,66 @@ private PanelItem createIslandIcon(ItemTemplateRecord template, IslandTopRecord
List<ItemTemplateRecord.ActionRecords> activeActions = new ArrayList<>(template.actions());

activeActions.removeIf(action ->
"VIEW".equalsIgnoreCase(action.actionType()) && island.getOwner() == null &&
island.getMemberSet(RanksManager.MEMBER_RANK).
contains(this.user.getUniqueId()));
{
switch (action.actionType().toUpperCase())
{
case "WARP" -> {
return island.getOwner() == null ||
this.addon.getWarpHook() == null ||
!this.addon.getWarpHook().getWarpSignsManager().hasWarp(this.world, island.getOwner());
}
case "VISIT" -> {
return island.getOwner() == null ||
this.addon.getVisitHook() == null ||
!this.addon.getVisitHook().getAddonManager().preprocessTeleportation(this.user, island);
}
case "VIEW" -> {
return island.getOwner() == null ||
!island.getMemberSet(RanksManager.MEMBER_RANK).contains(this.user.getUniqueId());
}
default -> {
return false;
}
}
});

// Add Click handler
builder.clickHandler((panel, user, clickType, i) ->
{
for (ItemTemplateRecord.ActionRecords action : activeActions)
{
if ((clickType == action.clickType() || action.clickType() == ClickType.UNKNOWN) &&
"VIEW".equalsIgnoreCase(action.actionType()))
if (clickType == action.clickType() || action.clickType() == ClickType.UNKNOWN)
{
this.user.closeInventory();
// Open Detailed GUI.

DetailsPanel.openPanel(this.addon, this.world, this.user);
switch (action.actionType().toUpperCase())
{
case "WARP" -> {
this.user.closeInventory();
this.addon.getWarpHook().getWarpSignsManager().warpPlayer(this.world, this.user, island.getOwner());
}
case "VISIT" -> {
// The command call implementation solves necessity to check for all visits options,
// like cool down, confirmation and preprocess in single go. Would it be better to write
// all logic here?

this.addon.getPlugin().getIWM().getAddon(this.world).
flatMap(GameModeAddon::getPlayerCommand).ifPresent(command ->
{
String mainCommand =
this.addon.getVisitHook().getSettings().getPlayerMainCommand();

if (!mainCommand.isBlank())
{
this.user.closeInventory();
this.user.performCommand(command.getTopLabel() + " " + mainCommand + " " + island.getOwner());
}
});
}
case "VIEW" -> {
this.user.closeInventory();
// Open Detailed GUI.
DetailsPanel.openPanel(this.addon, this.world, this.user);
}
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ level:
right-click-to-clear: "&e Right Click &7 to clear."
click-to-asc: "&e Click &7 to sort in increasing order."
click-to-desc: "&e Click &7 to sort in decreasing order."
click-to-warp: "&e Click &7 to warp."
click-to-visit: "&e Click &7 to visit."
right-click-to-visit: "&e Right Click &7 to visit."
conversations:
# Prefix for messages that are send from server.
prefix: "&l&6 [BentoBox]: &r"
Expand Down
70 changes: 70 additions & 0 deletions src/main/resources/panels/top_panel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ top_panel:
data:
type: TOP
index: 1
actions:
warp:
click-type: LEFT
tooltip: level.gui.tips.click-to-warp
visit:
click-type: RIGHT
tooltip: level.gui.tips.right-click-to-visit
fallback:
icon: LIME_STAINED_GLASS_PANE
title: level.gui.buttons.island.empty
Expand All @@ -28,6 +35,13 @@ top_panel:
data:
type: TOP
index: 2
actions:
warp:
click-type: LEFT
tooltip: level.gui.tips.click-to-warp
visit:
click-type: RIGHT
tooltip: level.gui.tips.right-click-to-visit
fallback:
icon: LIME_STAINED_GLASS_PANE
title: level.gui.buttons.island.empty
Expand All @@ -38,6 +52,13 @@ top_panel:
data:
type: TOP
index: 3
actions:
warp:
click-type: LEFT
tooltip: level.gui.tips.click-to-warp
visit:
click-type: RIGHT
tooltip: level.gui.tips.right-click-to-visit
fallback:
icon: LIME_STAINED_GLASS_PANE
title: level.gui.buttons.island.empty
Expand All @@ -49,6 +70,13 @@ top_panel:
data:
type: TOP
index: 4
actions:
warp:
click-type: LEFT
tooltip: level.gui.tips.click-to-warp
visit:
click-type: RIGHT
tooltip: level.gui.tips.right-click-to-visit
fallback:
icon: LIME_STAINED_GLASS_PANE
title: level.gui.buttons.island.empty
Expand All @@ -59,6 +87,13 @@ top_panel:
data:
type: TOP
index: 5
actions:
warp:
click-type: LEFT
tooltip: level.gui.tips.click-to-warp
visit:
click-type: RIGHT
tooltip: level.gui.tips.right-click-to-visit
fallback:
icon: LIME_STAINED_GLASS_PANE
title: level.gui.buttons.island.empty
Expand All @@ -69,6 +104,13 @@ top_panel:
data:
type: TOP
index: 6
actions:
warp:
click-type: LEFT
tooltip: level.gui.tips.click-to-warp
visit:
click-type: RIGHT
tooltip: level.gui.tips.right-click-to-visit
fallback:
icon: LIME_STAINED_GLASS_PANE
title: level.gui.buttons.island.empty
Expand All @@ -79,6 +121,13 @@ top_panel:
data:
type: TOP
index: 7
actions:
warp:
click-type: LEFT
tooltip: level.gui.tips.click-to-warp
visit:
click-type: RIGHT
tooltip: level.gui.tips.right-click-to-visit
fallback:
icon: LIME_STAINED_GLASS_PANE
title: level.gui.buttons.island.empty
Expand All @@ -89,6 +138,13 @@ top_panel:
data:
type: TOP
index: 8
actions:
warp:
click-type: LEFT
tooltip: level.gui.tips.click-to-warp
visit:
click-type: RIGHT
tooltip: level.gui.tips.right-click-to-visit
fallback:
icon: LIME_STAINED_GLASS_PANE
title: level.gui.buttons.island.empty
Expand All @@ -99,6 +155,13 @@ top_panel:
data:
type: TOP
index: 9
actions:
warp:
click-type: LEFT
tooltip: level.gui.tips.click-to-warp
visit:
click-type: RIGHT
tooltip: level.gui.tips.right-click-to-visit
fallback:
icon: LIME_STAINED_GLASS_PANE
title: level.gui.buttons.island.empty
Expand All @@ -109,6 +172,13 @@ top_panel:
data:
type: TOP
index: 10
actions:
warp:
click-type: LEFT
tooltip: level.gui.tips.click-to-warp
visit:
click-type: RIGHT
tooltip: level.gui.tips.right-click-to-visit
fallback:
icon: LIME_STAINED_GLASS_PANE
title: level.gui.buttons.island.empty
Expand Down

0 comments on commit dae3db6

Please sign in to comment.