Skip to content

Commit

Permalink
Fixed skip menu not working correctly for creation menu (#1494)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Dec 16, 2022
1 parent 0c994e0 commit de12d8c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 58 deletions.
Expand Up @@ -106,7 +106,7 @@ public void execute(SuperiorSkyblockPlugin plugin, CommandSender sender, String[
if (schematicName == null) {
Menus.MENU_ISLAND_CREATION.openMenu(superiorPlayer, superiorPlayer.getOpenedView(), islandName);
} else {
Menus.MENU_ISLAND_CREATION.simulateClick(superiorPlayer, islandName, schematicName, false);
Menus.MENU_ISLAND_CREATION.simulateClick(superiorPlayer, islandName, schematicName, false, superiorPlayer.getOpenedView());
}
}

Expand Down
@@ -1,22 +1,17 @@
package com.bgsoftware.superiorskyblock.core.menu.button.impl;

import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.menu.button.MenuTemplateButton;
import com.bgsoftware.superiorskyblock.api.schematic.Schematic;
import com.bgsoftware.superiorskyblock.api.world.GameSound;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.core.GameSoundImpl;
import com.bgsoftware.superiorskyblock.core.menu.Menus;
import com.bgsoftware.superiorskyblock.core.menu.TemplateItem;
import com.bgsoftware.superiorskyblock.core.menu.button.AbstractMenuTemplateButton;
import com.bgsoftware.superiorskyblock.core.menu.button.AbstractMenuViewButton;
import com.bgsoftware.superiorskyblock.core.menu.button.MenuTemplateButtonImpl;
import com.bgsoftware.superiorskyblock.core.menu.impl.MenuIslandCreation;
import com.bgsoftware.superiorskyblock.core.messages.Message;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -46,8 +41,9 @@ public ItemStack createViewItem() {

@Override
public void onButtonClick(InventoryClickEvent clickEvent) {
clickButton(plugin, (Player) clickEvent.getWhoClicked(), clickEvent.getClick().isRightClick(),
menuView.getIslandName(), menuView);
SuperiorPlayer clickedPlayer = plugin.getPlayers().getSuperiorPlayer(clickEvent.getWhoClicked());
Menus.MENU_ISLAND_CREATION.simulateClick(clickedPlayer, menuView.getIslandName(), getTemplate(),
clickEvent.getClick().isRightClick(), menuView);
}

@Override
Expand All @@ -57,39 +53,6 @@ public void onButtonClickLackPermission(InventoryClickEvent clickEvent) {
command.replace("%player%", clickEvent.getWhoClicked().getName())));
}

public void clickButton(SuperiorSkyblockPlugin plugin, Player whoClicked, boolean isRightClick,
String islandName, @Nullable MenuIslandCreation.View menuView) {
SuperiorPlayer clickedPlayer = plugin.getPlayers().getSuperiorPlayer(whoClicked);

// Checking for preview of islands.
if (isRightClick) {
Location previewLocation = plugin.getSettings().getPreviewIslands().get(getTemplate().schematic.getName());
if (previewLocation != null) {
plugin.getGrid().startIslandPreview(clickedPlayer, getTemplate().schematic.getName(), islandName);
return;
}
}

GameSoundImpl.playSound(whoClicked, getTemplate().accessSound);

getTemplate().accessCommands.forEach(command -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
command.replace("%player%", clickedPlayer.getName())));

Message.ISLAND_CREATE_PROCCESS_REQUEST.send(clickedPlayer);

if (menuView != null)
menuView.closeView();

World.Environment environment = plugin.getSettings().getWorlds().getDefaultWorld();
boolean offset = getTemplate().isOffset || (environment == World.Environment.NORMAL ?
plugin.getSettings().getWorlds().getNormal().isSchematicOffset() :
environment == World.Environment.NETHER ? plugin.getSettings().getWorlds().getNether().isSchematicOffset() :
plugin.getSettings().getWorlds().getEnd().isSchematicOffset());

plugin.getGrid().createIsland(clickedPlayer, getTemplate().schematic.getName(), getTemplate().bonusWorth,
getTemplate().bonusLevel, getTemplate().biome, islandName, offset);
}

public static class Builder extends AbstractMenuTemplateButton.AbstractBuilder<MenuIslandCreation.View> {

private final Schematic schematic;
Expand Down Expand Up @@ -181,6 +144,31 @@ public static class Template extends MenuTemplateButtonImpl<MenuIslandCreation.V
this.schematic = schematic;
}

@Nullable
public GameSound getAccessSound() {
return accessSound;
}

public List<String> getAccessCommands() {
return accessCommands;
}

public Biome getBiome() {
return biome;
}

public BigDecimal getBonusWorth() {
return bonusWorth;
}

public BigDecimal getBonusLevel() {
return bonusLevel;
}

public boolean isOffset() {
return isOffset;
}

public Schematic getSchematic() {
return schematic;
}
Expand Down
Expand Up @@ -7,6 +7,7 @@
import com.bgsoftware.superiorskyblock.api.menu.view.ViewArgs;
import com.bgsoftware.superiorskyblock.api.schematic.Schematic;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.core.GameSoundImpl;
import com.bgsoftware.superiorskyblock.core.io.MenuParserImpl;
import com.bgsoftware.superiorskyblock.core.logging.Log;
import com.bgsoftware.superiorskyblock.core.menu.AbstractMenu;
Expand All @@ -18,9 +19,14 @@
import com.bgsoftware.superiorskyblock.core.menu.layout.AbstractMenuLayout;
import com.bgsoftware.superiorskyblock.core.menu.view.AbstractMenuView;
import com.bgsoftware.superiorskyblock.core.menu.view.MenuViewWrapper;
import com.bgsoftware.superiorskyblock.core.messages.Message;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import javax.annotation.Nullable;
import java.io.File;
Expand All @@ -42,28 +48,60 @@ protected View createViewInternal(SuperiorPlayer superiorPlayer, Args args,
return new View(superiorPlayer, previousMenuView, this, args);
}

public void simulateClick(SuperiorPlayer superiorPlayer, String islandName, String schematic, boolean isPreviewMode) {
IslandCreationButton button = getButtonForSchematic(schematic);
if (button != null)
button.clickButton(plugin, superiorPlayer.asPlayer(), isPreviewMode, islandName, null);
public void simulateClick(SuperiorPlayer clickedPlayer, String islandName, String schematic,
boolean isPreviewMode, @Nullable MenuView<?, ?> menuView) {
menuLayout.getButtons().stream()
.filter(button -> IslandCreationButton.class.equals(button.getViewButtonType()) &&
((IslandCreationButton.Template) button).getSchematic().getName().equals(schematic))
.map(button -> (IslandCreationButton.Template) button)
.findFirst().ifPresent(template -> simulateClick(clickedPlayer, islandName, template, isPreviewMode, menuView));
}

private IslandCreationButton getButtonForSchematic(String schematicName) {
return (IslandCreationButton) menuLayout.getButtons().stream()
.filter(button -> button instanceof IslandCreationButton &&
((IslandCreationButton) button).getTemplate().getSchematic().getName().equals(schematicName))
.findFirst().orElse(null);
public void simulateClick(SuperiorPlayer clickedPlayer, String islandName,
IslandCreationButton.Template template, boolean isPreviewMode,
@Nullable MenuView<?, ?> menuView) {
String schematic = template.getSchematic().getName();

// Checking for preview of islands.
if (isPreviewMode) {
Location previewLocation = plugin.getSettings().getPreviewIslands().get(schematic);
if (previewLocation != null) {
plugin.getGrid().startIslandPreview(clickedPlayer, schematic, islandName);
return;
}
}

Player whoClicked = clickedPlayer.asPlayer();

GameSoundImpl.playSound(whoClicked, template.getAccessSound());

template.getAccessCommands().forEach(command -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
command.replace("%player%", clickedPlayer.getName())));

Message.ISLAND_CREATE_PROCCESS_REQUEST.send(clickedPlayer);

if (menuView != null)
menuView.closeView();

World.Environment environment = plugin.getSettings().getWorlds().getDefaultWorld();
boolean offset = template.isOffset() || (environment == World.Environment.NORMAL ?
plugin.getSettings().getWorlds().getNormal().isSchematicOffset() :
environment == World.Environment.NETHER ? plugin.getSettings().getWorlds().getNether().isSchematicOffset() :
plugin.getSettings().getWorlds().getEnd().isSchematicOffset());

plugin.getGrid().createIsland(clickedPlayer, schematic, template.getBonusWorth(),
template.getBonusLevel(), template.getBiome(), islandName, offset);
}

public void openMenu(SuperiorPlayer superiorPlayer, @Nullable MenuView<?, ?> previousMenu, String islandName) {
if (isSkipOneItem()) {
List<String> schematicButtons = menuLayout.getButtons().stream()
.filter(button -> button instanceof IslandCreationButton)
.map(button -> ((IslandCreationButton) button).getTemplate().getSchematic().getName())
.filter(button -> IslandCreationButton.class.equals(button.getViewButtonType()))
.map(button -> ((IslandCreationButton.Template) button).getSchematic().getName())
.collect(Collectors.toList());

if (schematicButtons.size() == 1) {
simulateClick(superiorPlayer, islandName, schematicButtons.get(0), false);
simulateClick(superiorPlayer, islandName, schematicButtons.get(0), false, superiorPlayer.getOpenedView());
return;
}
}
Expand Down
@@ -1,11 +1,11 @@
package com.bgsoftware.superiorskyblock.island.preview;

import com.bgsoftware.superiorskyblock.core.menu.Menus;
import com.bgsoftware.superiorskyblock.core.messages.Message;
import com.bgsoftware.superiorskyblock.player.chat.PlayerChat;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.IslandPreview;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.core.menu.Menus;
import com.bgsoftware.superiorskyblock.core.messages.Message;
import com.bgsoftware.superiorskyblock.player.chat.PlayerChat;
import com.google.common.base.Preconditions;
import org.bukkit.Location;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -63,7 +63,7 @@ public String getIslandName() {

@Override
public void handleConfirm() {
Menus.MENU_ISLAND_CREATION.simulateClick(superiorPlayer, islandName, schematic, false);
Menus.MENU_ISLAND_CREATION.simulateClick(superiorPlayer, islandName, schematic, false, superiorPlayer.getOpenedView());
Player player = superiorPlayer.asPlayer();
assert player != null;
PlayerChat.remove(player);
Expand Down

0 comments on commit de12d8c

Please sign in to comment.