Skip to content

Commit

Permalink
Bring over most changes from v0.0.26 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed Aug 18, 2024
1 parent c88de67 commit afad146
Show file tree
Hide file tree
Showing 29 changed files with 938 additions and 112 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/cjburkey/claimchunk/ClaimChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.cjburkey.claimchunk.data.newdata.*;
import com.cjburkey.claimchunk.data.sqlite.SqLiteDataHandler;
import com.cjburkey.claimchunk.event.*;
import com.cjburkey.claimchunk.gui.CCGuiHandler;
import com.cjburkey.claimchunk.i18n.V2JsonMessages;
import com.cjburkey.claimchunk.layer.PlaceholderInitLayer;
import com.cjburkey.claimchunk.layer.PrereqsInitLayer;
Expand Down Expand Up @@ -112,6 +113,7 @@ public final class ClaimChunk extends JavaPlugin implements IClaimChunkPlugin {
// The main handler (may not always be here, please don't rely on this)
@Getter private MainHandler mainHandler;
@Getter private ChunkOutlineHandler chunkOutlineHandler;
@Getter private CCGuiHandler guiHandler;

@Getter private CCInteractClasses interactClasses;
@Getter private CCPermFlags permFlags;
Expand Down Expand Up @@ -629,10 +631,13 @@ private void setupConfig() {
}

private void setupEvents() {
guiHandler = new CCGuiHandler();

// Register all the event handlers
getServer().getPluginManager().registerEvents(new PlayerConnectionHandler(this), this);
getServer().getPluginManager().registerEvents(new PlayerMovementHandler(this), this);
getServer().getPluginManager().registerEvents(new WorldProfileEventHandler(this), this);
getServer().getPluginManager().registerEvents(guiHandler, this);
}

private void setupNewCommands() {
Expand Down
33 changes: 33 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 chunkOutlineUseNewEffect;

/* Data */

Expand Down Expand Up @@ -75,6 +76,22 @@ public class ClaimChunkConfig {
@Getter private boolean showExtraInfoOnAnonymousMetrics;
@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;
@Getter private String guiPermSelectMenuItem;
@Getter private String guiPermModifyAllowItem;
@Getter private String guiPermModifyDenyItem;

/* Titles */

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

keepJsonBackups = getBool("data", "keepJsonBackups");
saveDataIntervalInMinutes = getInt("data", "saveDataIntervalInMinutes");
Expand Down Expand Up @@ -159,6 +177,21 @@ public void reload() {
showExtraInfoOnAnonymousMetrics = getBool("log", "showExtraInfoOnAnonymousMetrics");
debugSpam = getBool("log", "debugSpam");

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");
guiPermSelectMenuItem = getString("gui", "permSelectMenuItem");
guiPermModifyAllowItem = getString("gui", "permModifyAllowItem");
guiPermModifyDenyItem = getString("gui", "permModifyDenyItem");

useTitlesInsteadOfChat = getBool("titles", "useTitlesInsteadOfChat");
useActionBar = getBool("titles", "useActionBar");
titleFadeInTime = getInt("titles", "titleFadeInTime");
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/com/cjburkey/claimchunk/access/CCPermFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.function.BiFunction;

/**
Expand All @@ -27,6 +24,8 @@ public class CCPermFlags {

public final HashMap<String, CCFlags.BlockFlagData> blockControls = new HashMap<>();
public final HashMap<String, CCFlags.EntityFlagData> entityControls = new HashMap<>();
public final HashSet<String> pvpControls = new HashSet<>();
private final HashSet<String> allFlags = new HashSet<>();

/**
* Read the flags defined in the flag definitions file.
Expand Down Expand Up @@ -116,6 +115,7 @@ public void loadFromConfig(@NotNull YamlConfiguration config) {
Utils.err("Failed to load block flag data for flag \"%s\"", flagName);
continue;
}
allFlags.add(flagName);
blockControls.put(flagName, blockFlagData);
}
case "ENTITIES" -> {
Expand All @@ -136,8 +136,17 @@ public void loadFromConfig(@NotNull YamlConfiguration config) {
Utils.err("Failed to load entity flag data for flag \"%s\"", flagName);
continue;
}
allFlags.add(flagName);
entityControls.put(flagName, entityFlagData);
}
case "PLAYERS" -> {
if (pvpControls.contains(flagName)) {
Utils.err("Flag \"%s\" already has pvp protection", flagName);
continue;
}
allFlags.add(flagName);
pvpControls.add(flagName);
}
default ->
Utils.err(
"Invalid flag protection target \"%s\" for flag \"%s\"",
Expand All @@ -155,6 +164,10 @@ public void loadFromConfig(@NotNull YamlConfiguration config) {
}
}

public @NotNull Set<String> getAllFlags() {
return Collections.unmodifiableSet(allFlags);
}

private @Nullable YamlConfiguration readFlagFile(
@NotNull File flagsFile,
@NotNull JavaPlugin plugin,
Expand Down
10 changes: 10 additions & 0 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 pos The position of the chunk
* @return Whether this chunk is currently claimed.
*/
public boolean isClaimed(ChunkPos pos) {
return dataHandler.isChunkClaimed(pos);
}

/**
* Check if the provided chunk is claimed.
*
Expand Down
12 changes: 3 additions & 9 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 @@ -135,20 +135,14 @@ public void claimChunk(Player p, Chunk loc) {
// Claim the chunk if nothing is wrong
ChunkPos pos =
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) {
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.z(), loc.world(), p.getName());
return;
}

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
89 changes: 89 additions & 0 deletions src/main/java/com/cjburkey/claimchunk/gui/CCGuiHandler.java
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;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/cjburkey/claimchunk/gui/CCOpenGui.java
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) {}
Loading

0 comments on commit afad146

Please sign in to comment.