-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46fa18c
commit acb5a5b
Showing
8 changed files
with
228 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/com/cjburkey/claimchunk/gui/CCGuiHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.cjburkey.claimchunk.gui; | ||
|
||
import com.cjburkey.claimchunk.Utils; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.inventory.InventoryClickEvent; | ||
import org.bukkit.event.inventory.InventoryCloseEvent; | ||
import org.bukkit.inventory.Inventory; | ||
|
||
import java.util.HashMap; | ||
import java.util.UUID; | ||
|
||
/** | ||
* The handler behind ClaimChunk GUI screens. This class acts as the interface for opening GUIs as | ||
* well as the event listener for players clicking in/closing the screens. | ||
* | ||
* @since 0.0.26 | ||
*/ | ||
public class CCGuiHandler implements Listener { | ||
|
||
private final HashMap<UUID, CCOpenGui> openGuis = new HashMap<>(); | ||
|
||
@EventHandler | ||
public void onGuiClick(InventoryClickEvent e) { | ||
UUID uuid = e.getWhoClicked().getUniqueId(); | ||
CCOpenGui openGui = openGuis.get(uuid); | ||
if (openGui != null) { | ||
if (e.getInventory().equals(openGui.inventory())) { | ||
openGui.gui() | ||
.onClick( | ||
openGui.inventory(), | ||
(Player) e.getWhoClicked(), | ||
e.getSlot(), | ||
e.getClick(), | ||
e.getCurrentItem()); | ||
} | ||
e.setCancelled(true); | ||
} | ||
} | ||
|
||
@EventHandler | ||
public void onGuiClose(InventoryCloseEvent e) { | ||
if (openGuis.containsKey(e.getPlayer().getUniqueId())) { | ||
closeGui((Player) e.getPlayer()); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public void openGui(Player player, ICCGui gui) { | ||
if (openGuis.containsKey(player.getUniqueId())) { | ||
closeGui(player); | ||
} | ||
openGuis.put(player.getUniqueId(), new CCOpenGui(gui, createAndShowGui(player, gui))); | ||
gui.onOpen(openGuis.get(player.getUniqueId()).inventory(), player); | ||
} | ||
|
||
public void closeGui(Player player) { | ||
if (openGuis.containsKey(player.getUniqueId())) { | ||
openGuis.get(player.getUniqueId()) | ||
.gui() | ||
.onClose(openGuis.get(player.getUniqueId()).inventory(), player); | ||
openGuis.remove(player.getUniqueId()); | ||
} | ||
} | ||
|
||
private static Inventory createAndShowGui(Player player, ICCGui gui) { | ||
int rowCount = Math.min(Math.max(gui.getRows(), 1), 6); | ||
Inventory inventory = | ||
Bukkit.createInventory(player, rowCount * 9, Utils.color(gui.getName())); | ||
player.openInventory(inventory); | ||
return inventory; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.cjburkey.claimchunk.gui; | ||
|
||
import org.bukkit.inventory.Inventory; | ||
|
||
record CCOpenGui(ICCGui gui, Inventory inventory) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.cjburkey.claimchunk.gui; | ||
|
||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.inventory.ClickType; | ||
import org.bukkit.inventory.Inventory; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* Interface representing a ClaimChunk GUI screen. | ||
* | ||
* @since 0.0.26 | ||
*/ | ||
public interface ICCGui { | ||
|
||
/** | ||
* Called when this screen is shown to a player. | ||
* | ||
* @param inventory The inventory behind this GUI | ||
* @param player The player the GUI is shown to | ||
*/ | ||
void onOpen(@NotNull Inventory inventory, @NotNull Player player); | ||
|
||
/** | ||
* Called when this screen is being closed. | ||
* | ||
* @param inventory The inventory behind this GUI | ||
* @param player The player closing the GUI | ||
*/ | ||
void onClose(@NotNull Inventory inventory, @NotNull Player player); | ||
|
||
/** | ||
* Called when the player clicks on a given slot within the inventory. | ||
* | ||
* @param inventory The inventory behind this GUI | ||
* @param player The player clicking in the GUI being shown to them. | ||
* @param slot The index of the slot being clicked. | ||
* @param clickType Which type of click the player performed. | ||
* @param stack The stack on which the player clicked. | ||
*/ | ||
void onClick( | ||
@NotNull Inventory inventory, | ||
@NotNull Player player, | ||
int slot, | ||
@NotNull ClickType clickType, | ||
@Nullable ItemStack stack); | ||
|
||
/** | ||
* @return The name to be shown at the top of the GUI | ||
*/ | ||
@NotNull | ||
String getName(); | ||
|
||
/** | ||
* @return The number of rows this GUI should have | ||
*/ | ||
int getRows(); | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/cjburkey/claimchunk/gui/screens/MainMenu.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.cjburkey.claimchunk.gui.screens; | ||
|
||
import com.cjburkey.claimchunk.gui.ICCGui; | ||
|
||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.inventory.ClickType; | ||
import org.bukkit.inventory.Inventory; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class MainMenu implements ICCGui { | ||
|
||
@Override | ||
public void onOpen(@NotNull Inventory inventory, @NotNull Player player) {} | ||
|
||
@Override | ||
public void onClose(@NotNull Inventory inventory, @NotNull Player player) {} | ||
|
||
@Override | ||
public void onClick( | ||
@NotNull Inventory inventory, | ||
@NotNull Player player, | ||
int slot, | ||
@NotNull ClickType clickType, | ||
@NotNull ItemStack stack) {} | ||
|
||
@Override | ||
public @NotNull String getName() { | ||
return "ClaimChunk GUI"; | ||
} | ||
|
||
@Override | ||
public int getRows() { | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/GuiCmd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.cjburkey.claimchunk.smartcommand.sub.ply; | ||
|
||
import com.cjburkey.claimchunk.ClaimChunk; | ||
import com.cjburkey.claimchunk.gui.screens.MainMenu; | ||
import com.cjburkey.claimchunk.smartcommand.CCSubCommand; | ||
|
||
import de.goldmensch.commanddispatcher.Executor; | ||
|
||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* @since 0.0.23 | ||
*/ | ||
public class GuiCmd extends CCSubCommand { | ||
|
||
public GuiCmd(ClaimChunk claimChunk) { | ||
super(claimChunk, Executor.PLAYER, true, "player"); | ||
} | ||
|
||
@Override | ||
public @Nullable String getDescription() { | ||
return claimChunk.getMessages().cmdGui; | ||
} | ||
|
||
@Override | ||
public CCArg[] getPermittedArguments() { | ||
return new CCArg[0]; | ||
} | ||
|
||
@Override | ||
public int getRequiredArguments() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public boolean onCall(@NotNull String cmdUsed, @NotNull CommandSender executor, String[] args) { | ||
claimChunk.getGuiHandler().openGui((Player) executor, new MainMenu()); | ||
return true; | ||
} | ||
} |