|
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 — 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 — 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 — 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 — 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 — 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 — 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 — 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