Skip to content

Commit

Permalink
Make functional map gui
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 29, 2024
1 parent bb0c832 commit 2f6449e
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 38 deletions.
16 changes: 15 additions & 1 deletion src/main/java/com/cjburkey/claimchunk/ClaimChunkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,17 @@ public class ClaimChunkConfig {
@Getter private boolean debugSpam;

/* GUI */
@Getter private String guiMenuBackButtonItem;
@Getter private String guiMainMenuCurrentChunkItem;
@Getter private String guiMainMenuChunkMapItem;
@Getter private String guiMainMenuPermFlagsItem;
@Getter private boolean guiMapMenuAllowClaimOtherChunks;
@Getter private String guiMapMenuUnclaimedItem;
@Getter private String guiMapMenuSelfClaimedItem;
@Getter private String guiMapMenuOtherClaimedItem;
@Getter private String guiMapMenuCenterUnclaimedItem;
@Getter private String guiMapMenuCenterSelfClaimedItem;
@Getter private String guiMapMenuCenterOtherClaimedItem;

/* Titles */

Expand Down Expand Up @@ -168,10 +175,17 @@ public void reload() {
anonymousMetrics = getBool("log", "anonymousMetrics");
debugSpam = getBool("log", "debugSpam");

guiMainMenuCurrentChunkItem = getString("gui", "mainMenuCurrentChunk");
guiMenuBackButtonItem = getString("gui", "menuBackButtonItem");
guiMainMenuCurrentChunkItem = getString("gui", "mainMenuCurrentChunkItem");
guiMainMenuChunkMapItem = getString("gui", "mainMenuChunkMapItem");
guiMainMenuPermFlagsItem = getString("gui", "mainMenuPermFlagsItem");
guiMapMenuAllowClaimOtherChunks = getBool("gui", "mapMenuAllowClaimOtherChunks");
guiMapMenuUnclaimedItem = getString("gui", "mapMenuUnclaimedItem");
guiMapMenuSelfClaimedItem = getString("gui", "mapMenuSelfClaimedItem");
guiMapMenuOtherClaimedItem = getString("gui", "mapMenuOtherClaimedItem");
guiMapMenuCenterUnclaimedItem = getString("gui", "mapMenuCenterUnclaimedItem");
guiMapMenuCenterSelfClaimedItem = getString("gui", "mapMenuCenterSelfClaimedItem");
guiMapMenuCenterOtherClaimedItem = getString("gui", "mapMenuCenterOtherClaimedItem");

useTitlesInsteadOfChat = getBool("titles", "useTitlesInsteadOfChat");
useActionBar = getBool("titles", "useActionBar");
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/cjburkey/claimchunk/gui/GuiMenuScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

import org.bukkit.Material;
import org.bukkit.entity.Player;
Expand All @@ -13,6 +14,8 @@
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

/**
* An abstract wrapper for {@link ICCGui} to make creating inventory menus easier.
Expand Down Expand Up @@ -147,4 +150,31 @@ public int getRows() {
}
return item;
}

/**
* Generate chunk position text based on the message handler.
*
* @param chunkPos The position of the chunk
* @return A localized string representing a position in the given world.
*/
protected @NotNull String guiChunkPosText(@NotNull ChunkPos chunkPos) {
return claimChunk
.getMessages()
.guiChunkPos
.replaceAll(Pattern.quote("%%WORLD%%"), chunkPos.world())
.replaceAll(Pattern.quote("%%X%%"), chunkPos.x() + "")
.replaceAll(Pattern.quote("%%Z%%"), chunkPos.z() + "");
}

protected @NotNull String guiChunkOwnerNameText(@NotNull String chunkName) {
return claimChunk
.getMessages()
.guiChunkOwner
.replaceAll(Pattern.quote("%%NAME%%"), chunkName);
}

protected @NotNull String chunkNameOrUnknown(@NotNull UUID chunkOwner) {
String chunkName = claimChunk.getPlayerHandler().getChunkName(chunkOwner);
return chunkName != null ? chunkName : claimChunk.getMessages().unknownChunkOwner;
}
}
29 changes: 5 additions & 24 deletions src/main/java/com/cjburkey/claimchunk/gui/screens/MainMenu.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.cjburkey.claimchunk.gui.screens;

import com.cjburkey.claimchunk.ClaimChunk;
import com.cjburkey.claimchunk.chunk.ChunkHandler;
import com.cjburkey.claimchunk.chunk.ChunkPos;
import com.cjburkey.claimchunk.gui.GuiMenuScreen;
import com.cjburkey.claimchunk.player.PlayerHandler;

import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
Expand All @@ -13,7 +11,6 @@
import java.util.Collections;
import java.util.Objects;
import java.util.UUID;
import java.util.regex.Pattern;

public class MainMenu extends GuiMenuScreen {

Expand All @@ -38,38 +35,22 @@ protected void onBuildGui() {
}

private void addCurrentChunkItem(@NotNull Player player) {
ChunkHandler chunkHandler = claimChunk.getChunkHandler();
PlayerHandler playerHandler = claimChunk.getPlayerHandler();

ChunkPos chunkPos = new ChunkPos(player.getLocation().getChunk());
UUID chunkOwner = chunkHandler.getOwner(chunkPos);
String chunkName = chunkOwner == null ? null : playerHandler.getChunkName(chunkOwner);
if (chunkName == null && chunkOwner != null)
chunkName = playerHandler.getUsername(chunkOwner);
UUID chunkOwner = claimChunk.getChunkHandler().getOwner(chunkPos);

ArrayList<String> lore = new ArrayList<>();

lore.add(
claimChunk
.getMessages()
.guiChunkPos
.replaceAll(Pattern.quote("%%WORLD%%"), chunkPos.world())
.replaceAll(Pattern.quote("%%X%%"), chunkPos.x() + "")
.replaceAll(Pattern.quote("%%Z%%"), chunkPos.z() + ""));
lore.add(guiChunkPosText(chunkPos));
if (chunkOwner != null) {
lore.add(
claimChunk
.getMessages()
.guiChunkOwner
.replaceAll(Pattern.quote("%%NAME%%"), chunkName));
lore.add(guiChunkOwnerNameText(chunkNameOrUnknown(chunkOwner)));
if (chunkOwner.equals(player.getUniqueId())) {
lore.add("");
lore.add(claimChunk.getMessages().guiMainMenuUnclaim);
lore.add(claimChunk.getMessages().guiClickToUnclaim);
}
} else {
lore.add(claimChunk.getMessages().guiNotClaimed);
lore.add("");
lore.add(claimChunk.getMessages().guiMainMenuClaim);
lore.add(claimChunk.getMessages().guiClickToClaim);
}

addInteractiveButton(
Expand Down
91 changes: 81 additions & 10 deletions src/main/java/com/cjburkey/claimchunk/gui/screens/MapMenu.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,119 @@
package com.cjburkey.claimchunk.gui.screens;

import com.cjburkey.claimchunk.ClaimChunk;
import com.cjburkey.claimchunk.ClaimChunkConfig;
import com.cjburkey.claimchunk.chunk.ChunkHandler;
import com.cjburkey.claimchunk.chunk.ChunkPos;
import com.cjburkey.claimchunk.gui.GuiMenuScreen;

import org.bukkit.Material;
import com.cjburkey.claimchunk.i18n.V2JsonMessages;

import java.util.ArrayList;
import java.util.Collections;
import java.util.UUID;

public class MapMenu extends GuiMenuScreen {

private static final int MAP_HEIGHT = 5;

public MapMenu(ClaimChunk claimChunk) {
super(claimChunk, MAP_HEIGHT, claimChunk.getMessages().guiMapMenuTitle);
// Offset to make it 6 tall so that we can have the top row be a menu bar.
super(claimChunk, MAP_HEIGHT + 1, claimChunk.getMessages().guiMapMenuTitle);
}

@Override
protected void onBuildGui() {
if (player == null) return;

int halfHeight = Math.floorDiv(MAP_HEIGHT, 2);

ChunkHandler chunkHandler = claimChunk.getChunkHandler();
V2JsonMessages messages = claimChunk.getMessages();
ClaimChunkConfig config = claimChunk.getConfigHandler();
ChunkPos centerChunk = new ChunkPos(player.getLocation().getChunk());
boolean enableClaimingFromMap = config.getGuiMapMenuAllowClaimOtherChunks();

int halfHeight = Math.floorDiv(MAP_HEIGHT, 2);
// Add the back button
addInteractiveButton(
0,
materialFromStr(config.getGuiMenuBackButtonItem()),
messages.guiMenuBackButtonName,
Collections.singletonList(messages.guiMenuBackButtonDesc),
(clickType, stack) ->
claimChunk.getGuiHandler().openGui(player, new MainMenu(claimChunk)));

// Add the map items
// Inventory width is 9, so go 4 eastward and westward
for (int offsetX = -4; offsetX <= 4; offsetX++) {
// Inventory height is `MAP_HEIGHT`, so go `halfHeight` northward and southward
for (int offsetZ = -halfHeight; offsetZ <= halfHeight; offsetZ++) {
int slot = (offsetX + 4) + 9 * (offsetZ + halfHeight);
// The `+1` pushes down by one row, for the top row menu bar idea I have.
int slot = (offsetX + 4) + 9 * (offsetZ + halfHeight + 1);
ChunkPos offsetChunk =
new ChunkPos(
centerChunk.world(),
centerChunk.x() + offsetX,
centerChunk.z() + offsetZ);
boolean isCenter = offsetChunk.equals(centerChunk);

ArrayList<String> lore = new ArrayList<>();
UUID chunkOwner = chunkHandler.getOwner(offsetChunk);
boolean isOwner = player.getUniqueId().equals(chunkOwner);

if (isCenter) lore.add(messages.guiMapMenuInsideThisChunk);

if (chunkOwner != null) {
lore.add(guiChunkOwnerNameText(chunkNameOrUnknown(chunkOwner)));
if (enableClaimingFromMap && isOwner) {
lore.add("");
lore.add(messages.guiClickToUnclaim);
}
} else {
lore.add(messages.guiNotClaimed);
if (enableClaimingFromMap) {
lore.add("");
lore.add(messages.guiClickToClaim);
}
}

// TODO: CHANGE MATERIAL
String mapItemMaterialStr;
if (isCenter) {
mapItemMaterialStr =
isOwner
? config.getGuiMapMenuCenterSelfClaimedItem()
: chunkOwner == null
? config.getGuiMapMenuCenterUnclaimedItem()
: config.getGuiMapMenuCenterOtherClaimedItem();
} else {
mapItemMaterialStr =
isOwner
? config.getGuiMapMenuSelfClaimedItem()
: chunkOwner == null
? config.getGuiMapMenuUnclaimedItem()
: config.getGuiMapMenuOtherClaimedItem();
}

addInteractiveButton(
slot,
Material.DIRT,
"&r&fChunk at " + offsetChunk,
new ArrayList<>(),
(clickType, stack) -> {});
materialFromStr(mapItemMaterialStr),
guiChunkPosText(offsetChunk),
lore,
(clickType, stack) -> {
if (clickType.isLeftClick()) {
claimChunk.getMainHandler().claimChunk(player, offsetChunk);
refresh();
} else if (clickType.isRightClick()) {
claimChunk
.getMainHandler()
.unclaimChunk(
false,
false,
player,
offsetChunk.world(),
offsetChunk.x(),
offsetChunk.z());
refresh();
}
});
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,20 @@ public final class V2JsonMessages {
// GUI localization
public String guiMainMenuTitle = "ClaimChunk Main GUI";
public String guiMainMenuCurrentChunkItemName = "&r&3&lCurrent Chunk";
public String guiMenuBackButtonName = "&r&3&lBack";
public String guiMenuBackButtonDesc = "&fReturn to the main menu";
public String guiChunkPos = "&fIn &e%%WORLD%%&f at (&e%%X%%&f, &e%%Z%%&f)";
public String guiChunkOwner = "&fOwner: %%NAME%%";
public String guiNotClaimed = "&fNot claimed";
public String guiMainMenuClaim = "&2Click to claim!";
public String guiMainMenuUnclaim = "&4Right click to unclaim";
public String guiClickToClaim = "&2Left click to claim!";
public String guiClickToUnclaim = "&4Right click to unclaim";
public String guiMainMenuMapItemName = "&r&3&lChunk Map";
public String guiMapDescription = "&fClick to view the in-game map";
public String guiMainMenuPermFlagsItemName = "&r&3&lPermission Flags";
public String guiMainMenuPermFlagsDescription =
"&fClick to manage other players' permissions in your chunk(s)";
public String guiMapMenuTitle = "ClaimChunk Map";
public String guiMapMenuInsideThisChunk = "&fYou are standing inside this chunk";

// AdminOverride localization
public String adminOverrideEnable = "&eYou now have protection bypass";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.cjburkey.claimchunk.data.newdata.IClaimChunkDataHandler;

import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -91,6 +92,7 @@ public void clearChunkName(UUID owner) {
setChunkName(owner, null);
}

@Nullable
public String getChunkName(UUID owner) {
String chunkName = dataHandler.getPlayerChunkName(owner);
if (chunkName != null) return chunkName;
Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ log:
anonymousMetrics: true

gui:
mainMenuCurrentChunk: 'minecraft:grass_block'
menuBackButtonItem: 'minecraft:barrier'
mainMenuCurrentChunkItem: 'minecraft:grass_block'
mainMenuChunkMapItem: 'minecraft:map'
mainMenuPermFlagsItem: 'minecraft:writable_book'
mapMenuUnclaimedItem: 'minecraft:white_wool'
mapMenuSelfClaimedItem: 'minecraft:green_wool'
mapMenuOtherClaimedItem: 'minecraft:blue_wool'
mapMenuCenterUnclaimedItem: 'minecraft:white_glazed_terracotta'
mapMenuCenterSelfClaimedItem: 'minecraft:green_glazed_terracotta'
mapMenuCenterOtherClaimedItem: 'minecraft:blue_glazed_terracotta'
mapMenuAllowClaimOtherChunks: true

titles:
Expand Down

0 comments on commit 2f6449e

Please sign in to comment.