Skip to content

Commit

Permalink
Add some GUI items
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 28, 2024
1 parent a755313 commit 8ebe4ab
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 83 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/cjburkey/claimchunk/ClaimChunkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class ClaimChunkConfig {
@Getter private int chunkOutlineSpawnPerSec;
@Getter private int chunkOutlineParticlesPerSpawn;
@Getter private int chunkOutlineHeightRadius;
@Getter private boolean chunkOutlineNewEffect;

/* Data */

Expand Down Expand Up @@ -74,6 +75,10 @@ public class ClaimChunkConfig {
@Getter private boolean anonymousMetrics;
@Getter private boolean debugSpam;

/* GUI */
@Getter private String guiCurrentChunkItem;
@Getter private String guiChunkMapItem;

/* Titles */

@Getter private boolean useTitlesInsteadOfChat;
Expand Down Expand Up @@ -134,6 +139,7 @@ public void reload() {
chunkOutlineSpawnPerSec = getInt("chunkOutline", "spawnsPerSecond");
chunkOutlineParticlesPerSpawn = getInt("chunkOutline", "particlesPerSpawn");
chunkOutlineHeightRadius = getInt("chunkOutline", "heightRadius");
chunkOutlineNewEffect = getBool("chunkOutline", "useNewEffect");

keepJsonBackups = getBool("data", "keepJsonBackups");
saveDataIntervalInMinutes = getInt("data", "saveDataIntervalInMinutes");
Expand All @@ -160,6 +166,9 @@ public void reload() {
anonymousMetrics = getBool("log", "anonymousMetrics");
debugSpam = getBool("log", "debugSpam");

guiCurrentChunkItem = getString("gui", "currentChunkItem");
guiChunkMapItem = getString("gui", "chunkMapItem");

useTitlesInsteadOfChat = getBool("titles", "useTitlesInsteadOfChat");
useActionBar = getBool("titles", "useActionBar");
titleFadeInTime = getInt("titles", "titleFadeInTime");
Expand Down
27 changes: 24 additions & 3 deletions src/main/java/com/cjburkey/claimchunk/chunk/ChunkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@ public boolean isClaimed(String world, int x, int z) {
return dataHandler.isChunkClaimed(new ChunkPos(world, x, z));
}

/**
* Check if the provided chunk is claimed.
*
* @param chunkPos The position of the chunk.
* @return Whether this chunk is currently claimed.
*/
public boolean isClaimed(ChunkPos chunkPos) {
return dataHandler.isChunkClaimed(chunkPos);
}

/**
* Check if the provided chunk is claimed.
*
Expand All @@ -393,9 +403,7 @@ public boolean isClaimed(Chunk chunk) {
* @return Whether this player owns this chunk.
*/
public boolean isOwner(World world, int x, int z, UUID ply) {
ChunkPos pos = new ChunkPos(world.getName(), x, z);
UUID owner = dataHandler.getChunkOwner(pos);
return owner != null && owner.equals(ply);
return isOwner(new ChunkPos(world.getName(), x, z), ply);
}

/**
Expand Down Expand Up @@ -434,6 +442,19 @@ public boolean isOwner(Chunk chunk, Player ply) {
return isOwner(chunk.getWorld(), chunk.getX(), chunk.getZ(), ply);
}

/**
* Check if the provided player is the owner of the provided chunk
*
* @param chunk The chunk position.
* @param ply The player.
* @return Whether this player owns this chunk.
* @since 0.0.26
*/
public boolean isOwner(ChunkPos chunk, UUID ply) {
UUID owner = dataHandler.getChunkOwner(chunk);
return owner != null && owner.equals(ply);
}

/**
* Get the UUID of the owner of the provided chunk.
*
Expand Down
23 changes: 9 additions & 14 deletions src/main/java/com/cjburkey/claimchunk/cmd/MainHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void outlineChunk(ChunkPos chunk, Player showTo, int timeToShow) {
}
}

public void claimChunk(Player p, Chunk loc) {
public void claimChunk(Player p, ChunkPos loc) {
final ChunkHandler chunkHandler = claimChunk.getChunkHandler();

claimChunk
Expand All @@ -133,22 +133,16 @@ public void claimChunk(Player p, Chunk loc) {
errorMsg -> errorMsg.ifPresent(msg -> Utils.toPlayer(p, msg)),
successMsg -> {
// Claim the chunk if nothing is wrong
ChunkPos pos =
ChunkPos out =
chunkHandler.claimChunk(
loc.getWorld(),
loc.getX(),
loc.getZ(),
p.getUniqueId());
loc.world(), loc.x(), loc.z(), p.getUniqueId());

// Error check, though it *shouldn't* occur
if (pos == null) {
if (out == null) {
Utils.err(
"Failed to claim chunk (%s, %s) in world %s for player %s."
+ " The data handler returned a null position?",
loc.getX(),
loc.getZ(),
loc.getWorld().getName(),
p.getName());
loc.x(), loc.x(), loc.z(), loc.world(), p.getName());
return;
}

Expand All @@ -160,7 +154,7 @@ public void claimChunk(Player p, Chunk loc) {
claimChunk
.getChunkOutlineHandler()
.showChunkFor(
pos,
loc,
p,
claimChunk
.getConfigHandler()
Expand Down Expand Up @@ -260,9 +254,10 @@ public boolean unclaimChunk(
return false;
}

public void unclaimChunk(boolean adminOverride, boolean raw, Player p) {
public void unclaimChunk(boolean adminOverride, boolean hideTitle, Player p) {
Chunk chunk = p.getLocation().getChunk();
unclaimChunk(adminOverride, raw, p, p.getWorld().getName(), chunk.getX(), chunk.getZ());
unclaimChunk(
adminOverride, hideTitle, p, p.getWorld().getName(), chunk.getX(), chunk.getZ());
}

private void accessChunk(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.cjburkey.claimchunk.Utils;
import com.cjburkey.claimchunk.chunk.AutoClaimHandler;
import com.cjburkey.claimchunk.chunk.ChunkHandler;
import com.cjburkey.claimchunk.chunk.ChunkPos;
import com.cjburkey.claimchunk.player.PlayerHandler;

import org.bukkit.Bukkit;
Expand Down Expand Up @@ -39,7 +40,7 @@ public void onPlayerMove(PlayerMoveEvent e) {
if (prev.getX() != to.getX() || prev.getZ() != to.getZ()) {
// If the claim is currently auto-claiming, try to claim this chunk
if (AutoClaimHandler.inList(e.getPlayer())) {
claimChunk.getMainHandler().claimChunk(e.getPlayer(), to);
claimChunk.getMainHandler().claimChunk(e.getPlayer(), new ChunkPos(to));
return;
}

Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/cjburkey/claimchunk/gui/CCGuiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.UUID;
Expand Down Expand Up @@ -48,16 +49,15 @@ public void onGuiClose(InventoryCloseEvent e) {
}
}

@SuppressWarnings("unused")
public void openGui(Player player, ICCGui gui) {
public void openGui(@NotNull Player player, @NotNull 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) {
public void closeGui(@NotNull Player player) {
if (openGuis.containsKey(player.getUniqueId())) {
openGuis.get(player.getUniqueId())
.gui()
Expand All @@ -66,7 +66,12 @@ public void closeGui(Player player) {
}
}

private static Inventory createAndShowGui(Player player, ICCGui gui) {
public void refreshGui(@NotNull Player player, @NotNull ICCGui gui) {
closeGui(player);
openGui(player, gui);
}

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()));
Expand Down
79 changes: 61 additions & 18 deletions src/main/java/com/cjburkey/claimchunk/gui/GuiMenuScreen.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.cjburkey.claimchunk.gui;

import com.cjburkey.claimchunk.ClaimChunk;
import com.cjburkey.claimchunk.Utils;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
* An abstract wrapper for {@link ICCGui} to make creating inventory menus easier.
*
Expand All @@ -19,22 +26,22 @@ public abstract class GuiMenuScreen implements ICCGui {
public interface GuiItemAction {

/** Called when the item is clicked. */
void onClick(
@NotNull Inventory inventory,
@NotNull Player player,
int slot,
@NotNull ClickType clickType,
@NotNull ItemStack stack);
void onClick(@NotNull ClickType clickType, @NotNull ItemStack stack);
}

/** Wrapper around an interactive item in the GUI menu. */
record GuiItemWrapper(@NotNull ItemStack stack, @NotNull GuiItemAction action) {}

protected final ClaimChunk claimChunk;
private final int rowCount;
private final GuiItemWrapper[] actions;
private final String name;

protected GuiMenuScreen(int rowCount, @NotNull String name) {
protected @Nullable Inventory inventory;
protected @Nullable Player player;

protected GuiMenuScreen(ClaimChunk claimChunk, int rowCount, @NotNull String name) {
this.claimChunk = claimChunk;
this.rowCount = Math.min(Math.max(rowCount, 1), 6);
this.actions = new GuiItemWrapper[this.rowCount * 9];
this.name = name;
Expand All @@ -43,25 +50,61 @@ protected GuiMenuScreen(int rowCount, @NotNull String name) {
/**
* Add an interactive button item to this inventory GUI.
*
* <p>Must be called in {@link this#onBuildGui()},
*
* @param slot The slot of the inventory in which to put the item.
* @param stack The stack that represents this action.
* @param itemType The material of the item.
* @param itemName The display name of the item stack.
* @param itemLore The lore attached to the item.
* @param action The on-click callback
*/
protected void addInteractiveButton(
int slot, @NotNull ItemStack stack, @NotNull GuiItemAction action) {
if (slot >= 0 && slot < this.actions.length) {
this.actions[slot] = new GuiItemWrapper(stack, action);
int slot,
@NotNull Material itemType,
@NotNull String itemName,
@NotNull List<String> itemLore,
@NotNull GuiItemAction action) {
if (inventory == null) return;

Utils.debug("Made GUI stack in slot: " + slot);
Utils.debug("Max: " + this.actions.length);
if (slot >= 0 && slot < this.actions.length && itemType != Material.AIR) {
ItemStack stack = makeStack(itemType, itemName, itemLore);
if (stack != null) {
inventory.setItem(slot, stack);
this.actions[slot] = new GuiItemWrapper(stack, action);
}
}
}

private @Nullable ItemStack makeStack(
@NotNull Material itemType, @NotNull String itemName, @NotNull List<String> itemLore) {
ItemStack stack = new ItemStack(itemType);
ItemMeta meta = stack.getItemMeta();
if (meta == null) return null;
meta.setDisplayName(Utils.color(itemName));
meta.setLore(itemLore.stream().map(Utils::color).toList());
stack.setItemMeta(meta);
return stack;
}

@Override
public void onOpen(@NotNull Inventory inventory, @NotNull Player player) {
for (int slot = 0; slot < actions.length; slot++) {
GuiItemWrapper action = actions[slot];
if (action != null) {
inventory.setItem(slot, action.stack());
}
}
this.inventory = inventory;
this.player = player;

onBuildGui();
}

/**
* Called when the GUI is opened so you don't have to override {@link this#onOpen(Inventory,
* Player)}
*/
protected abstract void onBuildGui();

/** Method to reopen this gui (to update item names, etc.) */
protected void refresh() {
claimChunk.getGuiHandler().refreshGui(player, this);
}

@Override
Expand All @@ -75,7 +118,7 @@ public void onClick(

GuiItemWrapper action = actions[slot];
if (action != null && stack != null) {
action.action().onClick(inventory, player, slot, clickType, stack);
action.action().onClick(clickType, stack);
}
}

Expand Down
Loading

0 comments on commit 8ebe4ab

Please sign in to comment.