-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring over most changes from v0.0.26 branch
- Loading branch information
1 parent
c88de67
commit afad146
Showing
29 changed files
with
938 additions
and
112 deletions.
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
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
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
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
89 changes: 89 additions & 0 deletions
89
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,89 @@ | ||
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 org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
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()) | ||
&& e.getSlot() >= 0 | ||
&& e.getSlot() < e.getInventory().getSize()) { | ||
openGui.gui() | ||
.onClick( | ||
openGui.inventory(), 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()); | ||
} | ||
} | ||
|
||
public void openOrRefreshGui(@NotNull ICCGui gui) { | ||
Player player = gui.getPlayer(); | ||
CCOpenGui openGui = closeGui(player, false); | ||
|
||
final Inventory inventory; | ||
if (openGui != null && openGui.gui() == gui) { | ||
inventory = openGui.inventory(); | ||
inventory.clear(); | ||
} else { | ||
inventory = createAndShowGui(player, gui); | ||
} | ||
|
||
openGuis.put(player.getUniqueId(), new CCOpenGui(gui, inventory)); | ||
gui.onOpen(openGuis.get(player.getUniqueId()).inventory()); | ||
} | ||
|
||
public void closeGui(@NotNull Player player) { | ||
closeGui(player, true); | ||
} | ||
|
||
private @Nullable CCOpenGui closeGui(@NotNull Player player, boolean removeFromMap) { | ||
CCOpenGui openGui = openGuis.get(player.getUniqueId()); | ||
if (openGui != null) { | ||
openGui.gui().onClose(openGuis.get(player.getUniqueId()).inventory()); | ||
if (removeFromMap) { | ||
openGuis.remove(player.getUniqueId()); | ||
} | ||
} | ||
return openGui; | ||
} | ||
|
||
private static Inventory createAndShowGui(@NotNull Player player, @NotNull 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) {} |
Oops, something went wrong.