2018-10-31 14:32:21 +01:00
|
|
|
package world.bentobox.warps;
|
2017-12-31 06:15:47 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2019-09-19 23:23:29 +02:00
|
|
|
import java.util.Random;
|
2017-12-31 06:15:47 +01:00
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import org.bukkit.Material;
|
2018-05-29 00:42:03 +02:00
|
|
|
import org.bukkit.World;
|
2017-12-31 06:15:47 +01:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
2018-08-01 18:47:57 +02:00
|
|
|
import world.bentobox.bentobox.api.panels.PanelItem;
|
|
|
|
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
|
|
|
|
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
|
|
|
|
import world.bentobox.bentobox.api.user.User;
|
2017-12-31 06:15:47 +01:00
|
|
|
|
|
|
|
public class WarpPanelManager {
|
|
|
|
|
2017-12-31 23:08:36 +01:00
|
|
|
private static final int PANEL_MAX_SIZE = 52;
|
2018-04-16 02:31:12 +02:00
|
|
|
private Warp addon;
|
2018-05-29 00:42:03 +02:00
|
|
|
// This is a cache of signs
|
2020-01-29 02:11:23 +01:00
|
|
|
private SignCacheManager signCacheManager;
|
2017-12-31 06:15:47 +01:00
|
|
|
|
2018-04-16 02:31:12 +02:00
|
|
|
public WarpPanelManager(Warp addon) {
|
|
|
|
this.addon = addon;
|
2020-01-29 02:11:23 +01:00
|
|
|
signCacheManager = new SignCacheManager(addon);
|
2017-12-31 06:15:47 +01:00
|
|
|
}
|
|
|
|
|
2018-05-29 00:42:03 +02:00
|
|
|
private PanelItem getPanelItem(World world, UUID warpOwner) {
|
2019-08-23 00:08:14 +02:00
|
|
|
PanelItemBuilder pib = new PanelItemBuilder()
|
2019-09-19 01:48:38 +02:00
|
|
|
.name(addon.getSettings().getNameFormat() + addon.getPlugin().getPlayers().getName(warpOwner))
|
2020-01-29 02:11:23 +01:00
|
|
|
.description(signCacheManager.getSign(world, warpOwner))
|
2019-10-31 22:49:26 +01:00
|
|
|
.clickHandler((panel, clicker, click, slot) -> hander(world, clicker, warpOwner));
|
2020-01-29 02:11:23 +01:00
|
|
|
Material icon = signCacheManager.getSignIcon(world, warpOwner);
|
2019-08-23 00:08:14 +02:00
|
|
|
if (icon.equals(Material.PLAYER_HEAD)) {
|
|
|
|
return pib.icon(addon.getPlayers().getName(warpOwner)).build();
|
|
|
|
} else {
|
|
|
|
return pib.icon(icon).build();
|
|
|
|
}
|
2017-12-31 06:15:47 +01:00
|
|
|
}
|
|
|
|
|
2019-10-31 22:49:26 +01:00
|
|
|
private boolean hander(World world, User clicker, UUID warpOwner) {
|
2019-10-31 22:12:44 +01:00
|
|
|
clicker.closeInventory();
|
2019-12-09 01:02:55 +01:00
|
|
|
String playerCommand = addon.getPlugin().getIWM().getAddon(world).map(gm -> gm.getPlayerCommand().map(c -> c.getLabel()).orElse("")).orElse("");
|
|
|
|
String command = addon.getSettings().getWarpCommand() + " " + addon.getPlayers().getName(warpOwner);
|
|
|
|
clicker.getPlayer().performCommand((playerCommand.isEmpty() ? "" : playerCommand + " ") + command);
|
|
|
|
//addon.getWarpSignsManager().warpPlayer(world, clicker, warpOwner);
|
2019-10-31 22:12:44 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-19 23:23:29 +02:00
|
|
|
private PanelItem getRandomButton(World world, User user, UUID warpOwner) {
|
2019-10-31 22:12:44 +01:00
|
|
|
///give @p minecraft:player_head{display:{Name:"{\"text\":\"Question Mark\"}"},SkullOwner:"MHF_Question"} 1
|
2019-09-19 23:23:29 +02:00
|
|
|
return new PanelItemBuilder()
|
|
|
|
.name(addon.getSettings().getNameFormat() + user.getTranslation("warps.random"))
|
2019-10-31 22:49:26 +01:00
|
|
|
.clickHandler((panel, clicker, click, slot) -> hander(world, clicker, warpOwner))
|
2019-09-19 23:23:29 +02:00
|
|
|
.icon(Material.END_CRYSTAL).build();
|
|
|
|
}
|
|
|
|
|
2018-01-01 20:49:02 +01:00
|
|
|
/**
|
|
|
|
* Show the warp panel for the user
|
2018-07-30 23:24:08 +02:00
|
|
|
* @param world - world
|
|
|
|
* @param user - user
|
|
|
|
* @param index - page to show - 0 is first
|
2018-01-01 20:49:02 +01:00
|
|
|
*/
|
2018-07-29 06:42:05 +02:00
|
|
|
public void showWarpPanel(World world, User user, int index) {
|
2018-05-29 00:42:03 +02:00
|
|
|
List<UUID> warps = new ArrayList<>(addon.getWarpSignsManager().getSortedWarps(world));
|
2019-09-19 23:23:29 +02:00
|
|
|
UUID randomWarp = null;
|
|
|
|
// Add random UUID
|
2019-10-31 22:12:44 +01:00
|
|
|
if (!warps.isEmpty() && addon.getSettings().isRandomAllowed()) {
|
2019-09-19 23:23:29 +02:00
|
|
|
randomWarp = warps.get(new Random().nextInt(warps.size()));
|
|
|
|
warps.add(0, randomWarp);
|
|
|
|
}
|
2017-12-31 06:15:47 +01:00
|
|
|
if (index < 0) {
|
|
|
|
index = 0;
|
|
|
|
} else if (index > (warps.size() / PANEL_MAX_SIZE)) {
|
|
|
|
index = warps.size() / PANEL_MAX_SIZE;
|
|
|
|
}
|
2018-01-01 20:49:02 +01:00
|
|
|
PanelBuilder panelBuilder = new PanelBuilder()
|
2018-03-12 01:38:40 +01:00
|
|
|
.user(user)
|
2019-10-31 22:12:44 +01:00
|
|
|
.name(user.getTranslation("warps.title") + " " + (index + 1));
|
2018-04-16 02:31:12 +02:00
|
|
|
|
2017-12-31 06:15:47 +01:00
|
|
|
int i = index * PANEL_MAX_SIZE;
|
|
|
|
for (; i < (index * PANEL_MAX_SIZE + PANEL_MAX_SIZE) && i < warps.size(); i++) {
|
2019-09-19 23:23:29 +02:00
|
|
|
if (i == 0 && randomWarp != null) {
|
|
|
|
panelBuilder.item(getRandomButton(world, user, randomWarp));
|
|
|
|
} else {
|
|
|
|
panelBuilder.item(getPanelItem(world, warps.get(i)));
|
|
|
|
}
|
2017-12-31 06:15:47 +01:00
|
|
|
}
|
|
|
|
final int panelNum = index;
|
|
|
|
// Add signs
|
|
|
|
if (i < warps.size()) {
|
|
|
|
// Next
|
2018-03-12 01:38:40 +01:00
|
|
|
panelBuilder.item(new PanelItemBuilder()
|
2019-09-19 23:23:29 +02:00
|
|
|
.name(user.getTranslation("warps.next"))
|
2019-04-30 08:23:54 +02:00
|
|
|
.icon(new ItemStack(Material.STONE))
|
2018-05-02 08:19:02 +02:00
|
|
|
.clickHandler((panel, clicker, click, slot) -> {
|
|
|
|
user.closeInventory();
|
2018-05-29 00:42:03 +02:00
|
|
|
showWarpPanel(world, user, panelNum+1);
|
2018-05-02 08:19:02 +02:00
|
|
|
return true;
|
2017-12-31 06:15:47 +01:00
|
|
|
}).build());
|
|
|
|
}
|
|
|
|
if (i > PANEL_MAX_SIZE) {
|
|
|
|
// Previous
|
2018-03-12 01:38:40 +01:00
|
|
|
panelBuilder.item(new PanelItemBuilder()
|
2019-09-19 23:23:29 +02:00
|
|
|
.name(user.getTranslation("warps.previous"))
|
2019-04-30 08:23:54 +02:00
|
|
|
.icon(new ItemStack(Material.COBBLESTONE))
|
2018-05-02 08:19:02 +02:00
|
|
|
.clickHandler((panel, clicker, click, slot) -> {
|
|
|
|
user.closeInventory();
|
2018-05-29 00:42:03 +02:00
|
|
|
showWarpPanel(world, user, panelNum-1);
|
2018-05-02 08:19:02 +02:00
|
|
|
return true;
|
2017-12-31 06:15:47 +01:00
|
|
|
}).build());
|
|
|
|
}
|
|
|
|
panelBuilder.build();
|
|
|
|
}
|
2017-12-31 23:08:36 +01:00
|
|
|
|
2018-04-18 20:21:09 +02:00
|
|
|
/**
|
|
|
|
* Removes sign text from the cache
|
2020-01-29 02:11:23 +01:00
|
|
|
* @param world - world
|
|
|
|
* @param key - uuid of owner
|
2018-04-18 20:21:09 +02:00
|
|
|
*/
|
2018-05-29 00:42:03 +02:00
|
|
|
public void removeWarp(World world, UUID key) {
|
2020-01-29 02:11:23 +01:00
|
|
|
signCacheManager.removeWarp(world, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void saveCache() {
|
|
|
|
signCacheManager.saveCache();
|
2018-04-18 20:21:09 +02:00
|
|
|
}
|
|
|
|
|
2020-01-29 22:25:46 +01:00
|
|
|
public SignCacheManager getSignCacheManager() {
|
|
|
|
return this.signCacheManager;
|
|
|
|
}
|
|
|
|
|
2017-12-31 06:15:47 +01:00
|
|
|
}
|