Skip to content

Commit a49dee9

Browse files
committed
api: API improvements and docs for v3
1 parent 824eed7 commit a49dee9

File tree

7 files changed

+270
-26
lines changed

7 files changed

+270
-26
lines changed

common/src/main/java/net/william278/husktowns/api/HuskTownsAPI.java

+65-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import net.william278.husktowns.claim.*;
3333
import net.william278.husktowns.map.ClaimMap;
3434
import net.william278.husktowns.town.Member;
35+
import net.william278.husktowns.town.Privilege;
3536
import net.william278.husktowns.town.Town;
3637
import net.william278.husktowns.user.OnlineUser;
3738
import net.william278.husktowns.user.Preferences;
@@ -262,6 +263,18 @@ public Optional<TownClaim> getClaimAt(@NotNull Chunk chunk, @NotNull World world
262263
return plugin.getClaimAt(chunk, world);
263264
}
264265

266+
/**
267+
* Get whether a claim exists at a {@link Chunk} in a {@link World}
268+
*
269+
* @param chunk The {@link Chunk} to check
270+
* @param world The {@link World} the {@link Chunk} is in
271+
* @return Whether a claim exists at the chunk in the world
272+
* @since 3.0
273+
*/
274+
public boolean isClaimAt(@NotNull Chunk chunk, @NotNull World world) {
275+
return plugin.getClaimAt(chunk, world).isPresent();
276+
}
277+
265278
/**
266279
* Get a {@link TownClaim} at a {@link Position}, if it exists.
267280
*
@@ -273,6 +286,17 @@ public Optional<TownClaim> getClaimAt(@NotNull Position position) {
273286
return plugin.getClaimAt(position);
274287
}
275288

289+
/**
290+
* Get whether a claim exists at a {@link Position}
291+
*
292+
* @param position The {@link Position} to check
293+
* @return Whether a claim exists at the position
294+
* @since 3.0
295+
*/
296+
public boolean isClaimAt(@NotNull Position position) {
297+
return plugin.getClaimAt(position).isPresent();
298+
}
299+
276300
/**
277301
* Create a claim for a town
278302
*
@@ -601,7 +625,7 @@ public Component getClaimMapComponent(@NotNull Position position) {
601625
}
602626

603627
/**
604-
* Get whether an {@link Operation} is allowed
628+
* Get whether an {@link Operation} should be allowed to occur.
605629
*
606630
* @param operation The {@link Operation} to check against
607631
* @return Whether the {@link Operation} would be allowed
@@ -613,6 +637,46 @@ public boolean isOperationAllowed(@NotNull Operation operation) {
613637
return !plugin.cancelOperation(operation);
614638
}
615639

640+
641+
/**
642+
* Get whether an {@link Operation}, consisting of an {@link OnlineUser}, {@link OperationType} and
643+
* {@link Position}, should be allowed to occur.
644+
*
645+
* @param user the user
646+
* @param type the operation type
647+
* @param position the position of the operation
648+
* @return {@code true} if the operation is allowed, else {@code false}
649+
*/
650+
public boolean isOperationAllowed(@NotNull OnlineUser user, @NotNull OperationType type,
651+
@NotNull Position position) {
652+
return isOperationAllowed(Operation.of(user, type, position));
653+
}
654+
655+
/**
656+
* Get whether an {@link Operation}, consisting of a {@link Position} and {@link OperationType}, should be allowed
657+
* to occur.
658+
*
659+
* @param position the position of the operation
660+
* @param type the operation type
661+
* @return {@code true} if the operation is allowed, else {@code false}
662+
* @since 1.0
663+
*/
664+
public boolean isOperationAllowed(@NotNull Position position, @NotNull OperationType type) {
665+
return isOperationAllowed(Operation.of(type, position));
666+
}
667+
668+
/**
669+
* Returns whether a {@link User} can exercise a {@link Privilege}.
670+
*
671+
* @param privilege the privilege
672+
* @param user the user
673+
* @return {@code true} if the user can exercise the privilege in the claim, else {@code false}
674+
* @since 3.0
675+
*/
676+
public boolean isPrivilegeAllowed(@NotNull Privilege privilege, @NotNull User user) {
677+
return getUserTown(user).map(m -> m.hasPrivilege(plugin, privilege)).orElse(false);
678+
}
679+
616680
/**
617681
* Get a {@link Town} by its ID.
618682
*

common/src/main/java/net/william278/husktowns/claim/Claim.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121

2222
import com.google.gson.annotations.Expose;
2323
import com.google.gson.annotations.SerializedName;
24+
import lombok.AccessLevel;
25+
import lombok.NoArgsConstructor;
2426
import org.jetbrains.annotations.NotNull;
2527
import org.jetbrains.annotations.Nullable;
2628

2729
import java.util.*;
2830

31+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
2932
public class Claim {
3033

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

47-
@SuppressWarnings("unused")
48-
private Claim() {
49-
}
50-
5150
@NotNull
5251
public static Claim at(@NotNull Chunk chunk) {
5352
return new Claim(chunk, Type.CLAIM, null);

common/src/main/java/net/william278/husktowns/claim/TownClaim.java

+2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
import java.util.Optional;
2828

2929
public record TownClaim(@NotNull Town town, @NotNull Claim claim) {
30+
3031
public static Optional<TownClaim> from(@NotNull Map.Entry<Integer, Claim> entry, @NotNull HuskTowns plugin) {
3132
return plugin.findTown(entry.getKey()).map(town -> new TownClaim(town, entry.getValue()));
3233
}
3334

35+
@NotNull
3436
public static TownClaim admin(@NotNull Chunk chunk, @NotNull HuskTowns plugin) {
3537
return new TownClaim(plugin.getAdminTown(), Claim.at(chunk));
3638
}

common/src/main/java/net/william278/husktowns/town/Role.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
package net.william278.husktowns.town;
2121

22+
import lombok.AccessLevel;
23+
import lombok.AllArgsConstructor;
24+
import lombok.NoArgsConstructor;
2225
import net.william278.husktowns.HuskTowns;
2326
import org.jetbrains.annotations.NotNull;
2427

@@ -27,18 +30,14 @@
2730
/**
2831
* Represents a role in a town
2932
*/
33+
@AllArgsConstructor(access = AccessLevel.PRIVATE)
34+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
3035
public class Role {
3136

3237
private int weight;
3338
private String name;
3439
private List<Privilege> privileges;
3540

36-
private Role(int weight, @NotNull String name, @NotNull List<Privilege> privileges) {
37-
this.weight = weight;
38-
this.name = name;
39-
this.privileges = privileges;
40-
}
41-
4241
/**
4342
* Create a role from a weight, name and list of privileges
4443
*
@@ -51,10 +50,6 @@ public static Role of(int weight, @NotNull String name, @NotNull List<Privilege>
5150
return new Role(weight, name, privileges);
5251
}
5352

54-
@SuppressWarnings("unused")
55-
private Role() {
56-
}
57-
5853
/**
5954
* Get the weight of the role, determining its position in the role hierarchy.
6055
* <p>

docs/API-v1.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
> ⚠️ **API v1 is no longer supported by HuskTowns v3**. Please refer to the new HuskTowns [[API]] v3 for the current API specification.
22
3-
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).
3+
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).
44

55
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.
66

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

15-
## 1 API Introduction
15+
## 1. API Introduction
1616
### 1.1 Setup with Maven
1717
<details>
1818
<summary>Maven setup information</summary>
@@ -74,7 +74,7 @@ softdepend: # Or, use 'depend' here
7474
### 1.4. Getting an instance of the API
7575
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/).
7676

77-
## 2 API Examples
77+
## 2. API Examples
7878
### 2.1 Check if a location or block is in the wilderness
7979

8080
#### Method

docs/Claims-API.md

+138-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,138 @@
1-
Claims API. WIP
1+
HuskTowns provides API for getting, creating, changing the type of, & deleting [[claims]] and admin claims.
2+
3+
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.
4+
5+
## Table of contents
6+
7+
8+
## 1. Getting if a location is claimed
9+
* On the Bukkit platform, get a `Position` object using `#getPosition(org.bukkit.Location location)`
10+
* Use `#isClaimAt(Position position)` to check if the location has been claimed
11+
* Or, use `#getClaimAt(Position position)` to get the `Optional<TownClaim>` at the location
12+
* With an `Optional<TownClaim>`, you can use `Optional#isPresent()` to check if a claim exists at the location
13+
* With a `TownClaim` object, you can get the associated `Town` object (see [[Towns API]]) using `#town()`, and the `Claim` itself using `#claim()`
14+
* The `Claim` object has a range of properties describing the claim.
15+
16+
<details>
17+
<summary>Example &mdash; Getting if a location is claimed</summary>
18+
19+
```java
20+
void showTownWhoHasClaimedAt(org.bukkit.Location location) {
21+
Position position = huskTowns.getPosition(location);
22+
Optional<TownClaim> claim = huskTowns.getClaimAt(position);
23+
if (claim.isPresent()) {
24+
System.out.println("This location is claimed by " + claim.get().town().getName());
25+
}
26+
}
27+
```
28+
</details>
29+
30+
### 1.1 Getting the ClaimWorld for a World
31+
* Claims exist within a `ClaimWorld` in HuskTowns. `World`s without `ClaimWorld`s are not protected by HuskTowns.
32+
* On the Bukkit platform, get a `World` object from a Bukkit World using `#getWorld(org.bukkit.World)` (or call `#getWorld()` on a `Position` object)
33+
* You can then get the `ClaimWorld` for a world using `#getClaimWorld(World world)` which will return an `Optional<ClaimWorld>`
34+
35+
<details>
36+
<summary>Example &mdash; Getting the claim world for a world</summary>
37+
38+
```java
39+
void showClaimWorld(org.bukkit.World world) {
40+
Optional<ClaimWorld> claimWorld = huskTowns.getClaimWorld(world);
41+
if (claimWorld.isPresent()) {
42+
System.out.println("This world is protected by HuskTowns, and contains " + claimWorld.get().getClaimCount() + " claims!");
43+
}
44+
}
45+
```
46+
</details>
47+
48+
## 2. Checking what a user can do at a location
49+
* On the Bukkit platform, get an `OnlineUser` object using `#getOnlineUser(@NotNull org.bukkit.Player player)`
50+
* Use `#getPosition()` to get the `Position` of an `OnlineUser` to check if there's a claim where they stand (see #1)
51+
* Check if a user can perform `OperationTypes` using `#isOperationAllowed(OnlineUser user, OperationType type, Position position)`
52+
* Use the `#isOperationAllowed` method that accepts and build an `Operation` via `Operation.builder()` for more complex operation checks!
53+
54+
<details>
55+
<summary>Example &mdash; Checking what a user can do at a location</summary>
56+
57+
```java
58+
void checkUserAccessAt(org.bukkit.Player player, org.bukkit.Location location) {
59+
OnlineUser user = huskTowns.getOnlineUser(player);
60+
Position position = huskTowns.getPosition(location);
61+
if (huskTowns.isOperationAllowed(user, OperationType.BREAK_BLOCKS, position)) {
62+
System.out.println("User can build here!");
63+
} else {
64+
System.out.println("User can't build here!");
65+
}
66+
}
67+
```
68+
</details>
69+
70+
## 3. Creating a claim
71+
* You can create a claim using `#createClaimAt(OnlineUser actor, Town town, Chunk chunk, World world)`
72+
* You may also create an admin claim using `#createAdminClaimAt(OnlineUser actor, Chunk chunk, World world)`
73+
* 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)
74+
* You can also create a claim at the chunk at a position using `#createClaimAt(OnlineUser actor, Town town, Position position)`
75+
76+
<details>
77+
<summary>Example &mdash; Creating a claim</summary>
78+
79+
```java
80+
void createClaimAt(org.bukkit.Player player, org.bukkit.Chunk chunk, org.bukkit.World world) {
81+
OnlineUser user = huskTowns.getOnlineUser(player);
82+
Town town = huskTowns.getTown("townName").get();
83+
huskTowns.createClaimAt(user, town, chunk, world);
84+
}
85+
```
86+
</details>
87+
88+
### 3.1 Editing a claim
89+
* You can edit a claim using `#editClaimAt(Chunk chunk, World world, Consumer<TownClaim> editor)`
90+
* This will allow you to edit the claim at the given chunk and world using the `Consumer<TownClaim>` to modify the `TownClaim` object
91+
* For example, you can do `townClaim.claim().setType(Claim.Type type)` to change the type of the claim
92+
93+
<details>
94+
<summary>Example &mdash; Editing a claim</summary>
95+
96+
```java
97+
void editClaimAt(org.bukkit.Chunk chunk, org.bukkit.World world) {
98+
huskTowns.editClaimAt(chunk, world, townClaim -> {
99+
townClaim.claim().setType(Claim.Type.FARM);
100+
});
101+
}
102+
```
103+
</details>
104+
105+
### 3.2 Deleting a claim
106+
* You can delete a claim using `#deleteClaimAt(OnlineUser actor, Position position)`
107+
* A method that accepts a `Chunk` and a `World` is also available
108+
109+
<details>
110+
<summary>Example &mdash; Deleting a claim</summary>
111+
112+
```java
113+
void deleteClaimAt(org.bukkit.Player player, org.bukkit.Location location) {
114+
OnlineUser user = huskTowns.getOnlineUser(player);
115+
Position position = huskTowns.getPosition(location);
116+
huskTowns.deleteClaimAt(user, position);
117+
}
118+
```
119+
</details>
120+
121+
### 4. Highlighting a claim
122+
* You can "highlight" a claim for an `OnlineUser` (displaying the outline particle effect) using `#highlightClaim(OnlineUser actor, TownClaim claim)`
123+
* You may additionally specify the duration, and use `#highlightClaimAt` to attempt to highlight a claim at a specified `Position`
124+
125+
<details>
126+
<summary>Example &mdash; Highlighting a claim</summary>
127+
128+
```java
129+
void highlightClaimAt(org.bukkit.Player player, org.bukkit.Location location) {
130+
OnlineUser user = huskTowns.getOnlineUser(player);
131+
Position position = huskTowns.getPosition(location);
132+
Optional<TownClaim> claim = huskTowns.getClaimAt(position);
133+
if (claim.isPresent()) {
134+
huskTowns.highlightClaim(user, claim.get());
135+
}
136+
}
137+
```
138+
</details>

0 commit comments

Comments
 (0)