Skip to content

Commit

Permalink
api: API improvements and docs for v3
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Feb 5, 2024
1 parent 824eed7 commit a49dee9
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.william278.husktowns.claim.*;
import net.william278.husktowns.map.ClaimMap;
import net.william278.husktowns.town.Member;
import net.william278.husktowns.town.Privilege;
import net.william278.husktowns.town.Town;
import net.william278.husktowns.user.OnlineUser;
import net.william278.husktowns.user.Preferences;
Expand Down Expand Up @@ -262,6 +263,18 @@ public Optional<TownClaim> getClaimAt(@NotNull Chunk chunk, @NotNull World world
return plugin.getClaimAt(chunk, world);
}

/**
* Get whether a claim exists at a {@link Chunk} in a {@link World}
*
* @param chunk The {@link Chunk} to check
* @param world The {@link World} the {@link Chunk} is in
* @return Whether a claim exists at the chunk in the world
* @since 3.0
*/
public boolean isClaimAt(@NotNull Chunk chunk, @NotNull World world) {
return plugin.getClaimAt(chunk, world).isPresent();
}

/**
* Get a {@link TownClaim} at a {@link Position}, if it exists.
*
Expand All @@ -273,6 +286,17 @@ public Optional<TownClaim> getClaimAt(@NotNull Position position) {
return plugin.getClaimAt(position);
}

/**
* Get whether a claim exists at a {@link Position}
*
* @param position The {@link Position} to check
* @return Whether a claim exists at the position
* @since 3.0
*/
public boolean isClaimAt(@NotNull Position position) {
return plugin.getClaimAt(position).isPresent();
}

/**
* Create a claim for a town
*
Expand Down Expand Up @@ -601,7 +625,7 @@ public Component getClaimMapComponent(@NotNull Position position) {
}

/**
* Get whether an {@link Operation} is allowed
* Get whether an {@link Operation} should be allowed to occur.
*
* @param operation The {@link Operation} to check against
* @return Whether the {@link Operation} would be allowed
Expand All @@ -613,6 +637,46 @@ public boolean isOperationAllowed(@NotNull Operation operation) {
return !plugin.cancelOperation(operation);
}


/**
* Get whether an {@link Operation}, consisting of an {@link OnlineUser}, {@link OperationType} and
* {@link Position}, should be allowed to occur.
*
* @param user the user
* @param type the operation type
* @param position the position of the operation
* @return {@code true} if the operation is allowed, else {@code false}
*/
public boolean isOperationAllowed(@NotNull OnlineUser user, @NotNull OperationType type,
@NotNull Position position) {
return isOperationAllowed(Operation.of(user, type, position));
}

/**
* Get whether an {@link Operation}, consisting of a {@link Position} and {@link OperationType}, should be allowed
* to occur.
*
* @param position the position of the operation
* @param type the operation type
* @return {@code true} if the operation is allowed, else {@code false}
* @since 1.0
*/
public boolean isOperationAllowed(@NotNull Position position, @NotNull OperationType type) {
return isOperationAllowed(Operation.of(type, position));
}

/**
* Returns whether a {@link User} can exercise a {@link Privilege}.
*
* @param privilege the privilege
* @param user the user
* @return {@code true} if the user can exercise the privilege in the claim, else {@code false}
* @since 3.0
*/
public boolean isPrivilegeAllowed(@NotNull Privilege privilege, @NotNull User user) {
return getUserTown(user).map(m -> m.hasPrivilege(plugin, privilege)).orElse(false);
}

/**
* Get a {@link Town} by its ID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Claim {

@Expose
Expand All @@ -44,10 +47,6 @@ private Claim(@NotNull Chunk chunk, @NotNull Type type, @Nullable Map<UUID, Bool
this.plotMembers = type == Type.PLOT ? plotMembers : null;
}

@SuppressWarnings("unused")
private Claim() {
}

@NotNull
public static Claim at(@NotNull Chunk chunk) {
return new Claim(chunk, Type.CLAIM, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import java.util.Optional;

public record TownClaim(@NotNull Town town, @NotNull Claim claim) {

public static Optional<TownClaim> from(@NotNull Map.Entry<Integer, Claim> entry, @NotNull HuskTowns plugin) {
return plugin.findTown(entry.getKey()).map(town -> new TownClaim(town, entry.getValue()));
}

@NotNull
public static TownClaim admin(@NotNull Chunk chunk, @NotNull HuskTowns plugin) {
return new TownClaim(plugin.getAdminTown(), Claim.at(chunk));
}
Expand Down
15 changes: 5 additions & 10 deletions common/src/main/java/net/william278/husktowns/town/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

package net.william278.husktowns.town;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import net.william278.husktowns.HuskTowns;
import org.jetbrains.annotations.NotNull;

Expand All @@ -27,18 +30,14 @@
/**
* Represents a role in a town
*/
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Role {

private int weight;
private String name;
private List<Privilege> privileges;

private Role(int weight, @NotNull String name, @NotNull List<Privilege> privileges) {
this.weight = weight;
this.name = name;
this.privileges = privileges;
}

/**
* Create a role from a weight, name and list of privileges
*
Expand All @@ -51,10 +50,6 @@ public static Role of(int weight, @NotNull String name, @NotNull List<Privilege>
return new Role(weight, name, privileges);
}

@SuppressWarnings("unused")
private Role() {
}

/**
* Get the weight of the role, determining its position in the role hierarchy.
* <p>
Expand Down
6 changes: 3 additions & 3 deletions docs/API-v1.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
> ⚠️ **API v1 is no longer supported by HuskTowns v3**. Please refer to the new HuskTowns [[API]] v3 for the current API specification.
The HuskTowns API v1 provides methods to get data from HuskTowns directly. API v1 has been deprecated and superseded by the HuskTownsAPI v2 (See the new [[API]] documentation for more information).
The HuskTowns API v1 provides methods to get data from HuskTowns directly. API v1 has been deprecated and superseded by the HuskTownsAPI v3 (See the new [[API]] documentation for more information).

The API accesses cached data and can be used to check for things such as players being able to build on certain chunks, etc. This page contains how to use the API and provides example usages for developers.

Expand All @@ -12,7 +12,7 @@ The API accesses cached data and can be used to check for things such as players
4. Getting an instance
2. API Examples

## 1 API Introduction
## 1. API Introduction
### 1.1 Setup with Maven
<details>
<summary>Maven setup information</summary>
Expand Down Expand Up @@ -74,7 +74,7 @@ softdepend: # Or, use 'depend' here
### 1.4. Getting an instance of the API
Once you have added the API dependency, you can get an instance of it using `HuskTownsAPI.getInstance()`. This is the entrypoint for utilising the various methods, which you can look at on the [Javadoc](https://javadoc.jitpack.io/com/github/WiIIiam278/HuskTowns/husktowns/1.8.2/javadoc/).

## 2 API Examples
## 2. API Examples
### 2.1 Check if a location or block is in the wilderness

#### Method
Expand Down
139 changes: 138 additions & 1 deletion docs/Claims-API.md
Original file line number Diff line number Diff line change
@@ -1 +1,138 @@
Claims API. WIP
HuskTowns provides API for getting, creating, changing the type of, & deleting [[claims]] and admin claims.

This page assumes you have read the general [[API]] introduction and that you have both imported HuskTowns into your project and added it as a dependency.

## Table of contents


## 1. Getting if a location is claimed
* On the Bukkit platform, get a `Position` object using `#getPosition(org.bukkit.Location location)`
* Use `#isClaimAt(Position position)` to check if the location has been claimed
* Or, use `#getClaimAt(Position position)` to get the `Optional<TownClaim>` at the location
* With an `Optional<TownClaim>`, you can use `Optional#isPresent()` to check if a claim exists at the location
* With a `TownClaim` object, you can get the associated `Town` object (see [[Towns API]]) using `#town()`, and the `Claim` itself using `#claim()`
* The `Claim` object has a range of properties describing the claim.

<details>
<summary>Example &mdash; Getting if a location is claimed</summary>

```java
void showTownWhoHasClaimedAt(org.bukkit.Location location) {
Position position = huskTowns.getPosition(location);
Optional<TownClaim> claim = huskTowns.getClaimAt(position);
if (claim.isPresent()) {
System.out.println("This location is claimed by " + claim.get().town().getName());
}
}
```
</details>

### 1.1 Getting the ClaimWorld for a World
* Claims exist within a `ClaimWorld` in HuskTowns. `World`s without `ClaimWorld`s are not protected by HuskTowns.
* On the Bukkit platform, get a `World` object from a Bukkit World using `#getWorld(org.bukkit.World)` (or call `#getWorld()` on a `Position` object)
* You can then get the `ClaimWorld` for a world using `#getClaimWorld(World world)` which will return an `Optional<ClaimWorld>`

<details>
<summary>Example &mdash; Getting the claim world for a world</summary>

```java
void showClaimWorld(org.bukkit.World world) {
Optional<ClaimWorld> claimWorld = huskTowns.getClaimWorld(world);
if (claimWorld.isPresent()) {
System.out.println("This world is protected by HuskTowns, and contains " + claimWorld.get().getClaimCount() + " claims!");
}
}
```
</details>

## 2. Checking what a user can do at a location
* On the Bukkit platform, get an `OnlineUser` object using `#getOnlineUser(@NotNull org.bukkit.Player player)`
* Use `#getPosition()` to get the `Position` of an `OnlineUser` to check if there's a claim where they stand (see #1)
* Check if a user can perform `OperationTypes` using `#isOperationAllowed(OnlineUser user, OperationType type, Position position)`
* Use the `#isOperationAllowed` method that accepts and build an `Operation` via `Operation.builder()` for more complex operation checks!

<details>
<summary>Example &mdash; Checking what a user can do at a location</summary>

```java
void checkUserAccessAt(org.bukkit.Player player, org.bukkit.Location location) {
OnlineUser user = huskTowns.getOnlineUser(player);
Position position = huskTowns.getPosition(location);
if (huskTowns.isOperationAllowed(user, OperationType.BREAK_BLOCKS, position)) {
System.out.println("User can build here!");
} else {
System.out.println("User can't build here!");
}
}
```
</details>

## 3. Creating a claim
* You can create a claim using `#createClaimAt(OnlineUser actor, Town town, Chunk chunk, World world)`
* You may also create an admin claim using `#createAdminClaimAt(OnlineUser actor, Chunk chunk, World world)`
* This will create a claim at that position. You can then use `#getClaimAt(Position position)` to get the `TownClaim` object for the claim you just created (see #1)
* You can also create a claim at the chunk at a position using `#createClaimAt(OnlineUser actor, Town town, Position position)`

<details>
<summary>Example &mdash; Creating a claim</summary>

```java
void createClaimAt(org.bukkit.Player player, org.bukkit.Chunk chunk, org.bukkit.World world) {
OnlineUser user = huskTowns.getOnlineUser(player);
Town town = huskTowns.getTown("townName").get();
huskTowns.createClaimAt(user, town, chunk, world);
}
```
</details>

### 3.1 Editing a claim
* You can edit a claim using `#editClaimAt(Chunk chunk, World world, Consumer<TownClaim> editor)`
* This will allow you to edit the claim at the given chunk and world using the `Consumer<TownClaim>` to modify the `TownClaim` object
* For example, you can do `townClaim.claim().setType(Claim.Type type)` to change the type of the claim

<details>
<summary>Example &mdash; Editing a claim</summary>

```java
void editClaimAt(org.bukkit.Chunk chunk, org.bukkit.World world) {
huskTowns.editClaimAt(chunk, world, townClaim -> {
townClaim.claim().setType(Claim.Type.FARM);
});
}
```
</details>

### 3.2 Deleting a claim
* You can delete a claim using `#deleteClaimAt(OnlineUser actor, Position position)`
* A method that accepts a `Chunk` and a `World` is also available

<details>
<summary>Example &mdash; Deleting a claim</summary>

```java
void deleteClaimAt(org.bukkit.Player player, org.bukkit.Location location) {
OnlineUser user = huskTowns.getOnlineUser(player);
Position position = huskTowns.getPosition(location);
huskTowns.deleteClaimAt(user, position);
}
```
</details>

### 4. Highlighting a claim
* You can "highlight" a claim for an `OnlineUser` (displaying the outline particle effect) using `#highlightClaim(OnlineUser actor, TownClaim claim)`
* You may additionally specify the duration, and use `#highlightClaimAt` to attempt to highlight a claim at a specified `Position`

<details>
<summary>Example &mdash; Highlighting a claim</summary>

```java
void highlightClaimAt(org.bukkit.Player player, org.bukkit.Location location) {
OnlineUser user = huskTowns.getOnlineUser(player);
Position position = huskTowns.getPosition(location);
Optional<TownClaim> claim = huskTowns.getClaimAt(position);
if (claim.isPresent()) {
huskTowns.highlightClaim(user, claim.get());
}
}
```
</details>
Loading

0 comments on commit a49dee9

Please sign in to comment.