64 Commits

Author SHA1 Message Date
430f340fd4 Updates to 1.21.4
Updates Spigot version
Updates API version
Fixes invulnerability ticks not working as intended
Removes missing material tag.
Fixes max health attribute.
2025-02-21 18:11:06 +01:00
f09dcbe7ff Updates README, and allows the un-setting of kill plane and obstacle blocks 2024-05-16 15:34:53 +02:00
901b052b25 Improves some things
Makes sure to save the arena after editing allowed or loss trigger damage causes
Makes sure to save and restore player saturation
Adds damage animation and damage sound when faking damage for allowed damage causes
Plays a sound when a player loses
2024-05-16 15:00:16 +02:00
2ec15c69c7 Restores player health upon arena exit 2024-05-16 13:32:58 +02:00
d101c7ed02 Implements #37 and improves material tab-completion 2024-05-16 13:14:33 +02:00
bfc0eb7334 Adds POM relocations 2024-05-03 15:19:15 +02:00
eceb7fc6d2 Makes sure levers are reset on an immediate quit 2024-04-30 16:32:15 +02:00
fb68c18fe6 Implements #42 2024-04-30 15:50:18 +02:00
98b5ea5abe Fixes #38 2024-04-28 21:52:18 +02:00
87788e60a3 Implements #35 2024-04-28 16:21:54 +02:00
3a68d30044 Adds the lever functionality to the README 2024-04-22 14:58:27 +02:00
fdd6d94102 Makes levers clicked in parkour arenas turn off
When a lever is clicked while inside a parkour arena, that lever is saved. Once the last player leaves, or the last player restarts, the saved levers are turned off.
2024-04-22 14:52:19 +02:00
a1f76bcfc2 Fixes #36 2024-04-22 12:41:55 +02:00
d124c0948b Improves output of arena group listing 2024-04-21 18:14:44 +02:00
8574dfebbb Fixes trigger for parkour winning 2024-04-21 05:40:05 +02:00
f2328ecfa3 Improves some wording 2024-04-06 21:45:19 +02:00
a632de5b1f Improves some GUI items, and removes some redundancy 2024-04-05 17:28:40 +02:00
aa39243232 Fix #34 2024-04-05 16:29:12 +02:00
0360ada849 Adds two decimals to best time placeholders 2024-04-04 23:54:48 +02:00
81e652b4ff Stores potion effects for players in arenas 2024-04-04 23:29:56 +02:00
d7e950c53e Improves display of time records 2024-04-04 15:50:38 +02:00
c8fbdec64f Fixes some minor issues
Fixes the tab-completion for the randomly inverted game-mode not being properly matched.
Prevents the usage text from displaying for some join errors
2024-04-04 04:28:44 +02:00
09f29e5f67 Merge pull request #30 from SunNetservers/dev
Arena saving bugfix
2024-04-04 03:07:30 +02:00
7213bd0c70 Fixes README formatting 2024-04-04 03:03:57 +02:00
a23d0c769f Bumps version 2024-04-04 03:01:28 +02:00
142cd5339b Fixes a bug in arena saving
Saving a single arena caused all other arena data of that type to be deleted.
2024-04-04 03:00:03 +02:00
b063bd1088 Bumps version for release 2023-12-29 01:56:05 +01:00
310802b42d Adds obstacle blocks for parkour
This change reverts the advanced hit-box detection for kill plane blocks, giving them a full block's hit-box again.
Instead, obstacle blocks have been added, which have an accurate hit-box, and can trigger a hit from any direction.
The horizontal kill plane hit box option has been removed as it's no longer useful.
2023-10-02 23:05:39 +02:00
fc6bd33e87 Adds ability to specify horizontal hit-box for end-rods and similar 2023-09-27 23:13:03 +02:00
ccf43060dc Adds exact hit-boxes for parkour kill planes 2023-09-27 19:14:18 +02:00
a498e9bad0 Fixes some liquid hitbox problems 2023-09-25 20:50:02 +02:00
d8bf77d317 Fixes visibility of some methods 2023-09-17 15:18:51 +02:00
162aff0c1f Fixes a bug in killBlockNames, and reduces writes 2023-09-17 08:03:03 +02:00
a7cfe36c72 Fixes parkour kill plane block names not being updated 2023-08-18 17:54:39 +02:00
407acf0ea2 Adds LIGHT to the block whitelist 2023-07-31 23:51:44 +02:00
b6bf22d1c7 Fixes incorrect alias for parkourRemove 2023-07-31 23:30:56 +02:00
67144fec06 Bumps version to SNAPSHOT again 2023-07-31 23:01:36 +02:00
e52732433a Fixes some incorrect command usage descriptions 2023-07-31 23:01:03 +02:00
776fc5a757 Fixes some markdown 2023-07-17 23:58:25 +02:00
1e7cdf02fc Makes possible player name placeholders more clear 2023-07-17 22:53:28 +02:00
f3d8be8be2 Adds missing info about the command reward player name placeholder 2023-07-17 18:26:27 +02:00
4bf00243e6 Removes SNAPSHOT from version 2023-07-17 13:06:06 +02:00
ea439bfa33 Adds documentation for the minigames reward command 2023-07-17 13:05:29 +02:00
003c9e8367 Finishes the reward tab completer 2023-07-11 16:36:19 +02:00
9dff407713 Mostly finishes rewards 2023-07-10 17:51:59 +02:00
d7b1695cd9 Adds Vault support for Economy and Permission rewards 2023-07-09 19:21:34 +02:00
2bc6c2c825 Fixes reward messages 2023-07-09 18:52:27 +02:00
f24959c8d1 Merge branch 'dev' into rewards
# Conflicts:
#	src/main/java/net/knarcraft/minigames/config/Message.java
2023-07-09 18:22:23 +02:00
8ea930a5f3 Makes all messages configurable 2023-07-09 18:20:31 +02:00
57183b64f5 Makes looking around a dropper arena possible 2023-06-13 18:42:20 +02:00
ba1a7fff68 Adds note about underscores in arena names 2023-06-12 23:37:41 +02:00
57b3b85fbc Adds a new item to allow arena re-tries
Prevents a NumberFormatException if the record number is invalid
Updates Spigot to 1.20
2023-06-12 23:17:48 +02:00
38839c0287 Re-implements the collision removal to fix a severe bug 2023-05-12 14:31:30 +02:00
de5124c8dd Hopefully prevents an exception when restoring a player's fly state 2023-05-12 13:16:32 +02:00
9c91e11780 Gives players an item to open the arena menu 2023-05-11 17:52:49 +02:00
4513fc2de7 Removes some un-used configuration options 2023-05-11 00:47:48 +02:00
7900eb691f Makes hiding players default to true 2023-05-10 16:44:30 +02:00
d6fb9ab0b9 Makes some GUI changes
Makes the current state of the player toggle visible.
Makes it possible to toggle player visibility outside arenas.
2023-05-10 16:33:01 +02:00
7848a0a028 Implements #27 among other things
It was found that the Spigot API's methods for cancelling player collisions won't work without changing the scoreboard. Because of that, the normal option has been disabled. The invisibility option has also been removed, as that's a bad idea if players can still push each-other.
The toggle player option which is implemented in this commit does disable player collision, so that's the only working way right now.

A potential ConcurrentModificationException has been fixed.
The parkourCheckpoint command has been removed, as the functionality is now available through the API.
2023-05-10 15:14:28 +02:00
00ac0582f4 Adds a partially usable arena menu openable through a command 2023-05-09 15:47:54 +02:00
0704e138ec Adds a command for returning to checkpoint by forcing a loss #28 2023-05-08 18:14:37 +02:00
05d647052b Adds unfinished code for storing arena rewards 2023-05-08 02:15:03 +02:00
fc1902e86a Denies playing hardcore in a no-checkpoint arena 2023-04-28 15:32:51 +02:00
e039840e89 Adds a hardcore game-mode ignoring checkpoints 2023-04-28 15:05:36 +02:00
108 changed files with 5946 additions and 1678 deletions

152
README.md
View File

@ -13,6 +13,11 @@ playing, simply use `/parkourjoin <name>`, where \<name> is the same as you spec
To modify the arena, use `/parkouredit <name> <property> <value>`. Use `/parkouredit checkpointAdd here` to add a To modify the arena, use `/parkouredit <name> <property> <value>`. Use `/parkouredit checkpointAdd here` to add a
checkpoint at your current location. checkpoint at your current location.
Special Parkour behavior: If a lever is toggled while in a parkour arena, that lever is saved, and automatically turned
off once the last arena player leaves, or the last arena player restarts. This allows parkour arenas where lever toggles
are required for progression. It is however possible to cheat by having one player go first, toggling required levers,
and then going through much faster.
## Permissions ## Permissions
The only permission normal players will need is `minigames.join` which is set to true by default. The only permission normal players will need is `minigames.join` which is set to true by default.
@ -38,9 +43,11 @@ The only permission normal players will need is `minigames.join` which is set to
## Commands ## Commands
| Command | Alias | Arguments | Description | | Command | Alias | Arguments | Description |
|----------------------------------------|----------|-----------------------------|-------------------------------------------------------------------------------------| |----------------------------------------|----------|------------------------------|-------------------------------------------------------------------------------------|
| /miniGamesReload | /mreload | | Reloads all data from disk. | | /miniGamesReload | /mreload | | Reloads all data from disk. |
| /miniGamesLeave | /mleave | | Leaves the current mini-game. | | /miniGamesLeave | /mleave | | Leaves the current mini-game. |
| /miniGamesMenu | /mmenu | | Shows a menu of actions if used while in an arena |
| [/miniGamesReward](#minigamesreward) | /mreward | [See this](#minigamesreward) | Adds or removes rewards for an arena |
| /dropperList | /dlist | | Lists available dropper arenas. | | /dropperList | /dlist | | Lists available dropper arenas. |
| [/dropperJoin](#dropperjoin) | /djoin | \<arena> \[mode] | Joins the selected arena. | | [/dropperJoin](#dropperjoin) | /djoin | \<arena> \[mode] | Joins the selected arena. |
| /dropperCreate | /dcreate | \<name> | Creates a new dropper arena with the given name. The spawn is set to your location. | | /dropperCreate | /dcreate | \<name> | Creates a new dropper arena with the given name. The spawn is set to your location. |
@ -50,7 +57,7 @@ The only permission normal players will need is `minigames.join` which is set to
| /dropperGroupList | /dglist | \[group] | Lists groups, or the stages of a group if a group is specified. | | /dropperGroupList | /dglist | \[group] | Lists groups, or the stages of a group if a group is specified. |
| [/dropperGroupSwap](#droppergroupswap) | /dgswap | \<arena1> \<arena2> | Swaps the two arenas in the group's ordered list. | | [/dropperGroupSwap](#droppergroupswap) | /dgswap | \<arena1> \<arena2> | Swaps the two arenas in the group's ordered list. |
| /parkourList | /plist | | Lists available parkour arenas. | | /parkourList | /plist | | Lists available parkour arenas. |
| /parkourJoin | /pjoin | \<arena> | Joins the selected arena. | | [/parkourJoin](#parkourjoin) | /pjoin | \<arena> \[mode] | Joins the selected arena. |
| /parkourCreate | /pcreate | \<name> | Creates a new parkour arena with the given name. The spawn is set to your location. | | /parkourCreate | /pcreate | \<name> | Creates a new parkour arena with the given name. The spawn is set to your location. |
| /parkourRemove | /premove | \<arena> | Removes the specified parkour arena. | | /parkourRemove | /premove | \<arena> | Removes the specified parkour arena. |
| [/parkourEdit](#parkouredit) | /pedit | \<arena> \<option> \[value] | Gets or sets a parkour arena option. | | [/parkourEdit](#parkouredit) | /pedit | \<arena> \<option> \[value] | Gets or sets a parkour arena option. |
@ -58,6 +65,55 @@ The only permission normal players will need is `minigames.join` which is set to
| /parkourGroupList | /pglist | \[group] | Lists groups, or the stages of a group if a group is specified. | | /parkourGroupList | /pglist | \[group] | Lists groups, or the stages of a group if a group is specified. |
| [/parkourGroupSwap](#droppergroupswap) | /pgswap | \<arena1> \<arena2> | Swaps the two arenas in the group's ordered list. | | [/parkourGroupSwap](#droppergroupswap) | /pgswap | \<arena1> \<arena2> | Swaps the two arenas in the group's ordered list. |
### Command explanation mini-games
#### /miniGamesReward
This command is used to set the rewards for an arena. Rewards can be set for six conditions; a reward for each time the
arena is cleared, a reward for the first time the arena is cleared, a reward for beating your own least deaths record, a
reward for beating your own least time record, a record for beating the global least deaths record and a record for
beating the global least time record. You can give an item, give money, give a permission or execute a console command
with the winning player as an argument.
Note, you can add as many rewards as you want for each reward condition, so you can add a permission and an amount of
currency on the player's first win for example.
`/mreward add dropper|parkour <name> <condition> <type> [value] [value] ...`
`/mreward clear dropper|parkour <name> <condition>`
| Argument | Type | Usage |
|-----------|-----------------------------------------------------------------------------------------------------------|-------------------------------------------------------------|
| action | add / clear | Whether you are adding a reward or clearing rewards. |
| type | dropper / parkour | The type of arena to change rewards for |
| name | _Arena name_ | The name of the arena to change rewards for |
| condition | WIN / FIRST_WIN / PERSONAL_DEATH_RECORD / PERSONAL_TIME_RECORD / GLOBAL_DEATH_RECORD / GLOBAL_TIME_RECORD | The condition to change rewards for. |
| type | COMMAND / ECONOMY / ITEM / PERMISSION | The type of reward to add |
| value | [See reward types](#reward-types) | Input for the reward type. Valid values depend on the type. |
##### Reward types
###### Economy
This reward requires an argument which is a number above zero, which is the amount of currency granted to players.
###### Permission
This reward requires an argument which is the permission string you want to grant the player.
###### Command
The reward requires the command as an argument. Type the full command with spaces and everything, but omit the `/` at
the beginning of the command. Use %player_name% or anything that matches the
RegEx `[<%(\\[{]player[_\\-]?(name)?[>%)\\]}]` (\<player>. \<player-name>, %player_name%, {player}, etc.) as the
placeholder for the rewarded player's name.
###### Item
If used with no arguments, the item in your main hand is used. You can specify a material in the first argument to give
one item of the specified type. You can specify a positive whole number as the second argument to specify the amount of
items to give.
### Command explanation dropper ### Command explanation dropper
#### /dropperJoin #### /dropperJoin
@ -86,13 +142,15 @@ This command allows editing the specified property for the specified dropper are
These are all the options that can be changed for an arena. These are all the options that can be changed for an arena.
| Option | Details | | Option | Details |
|--------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| name | The name of the arena. Used mainly to select the arena in commands. | | name | The name of the arena. Used mainly to select the arena in commands. |
| spawnLocation | The spawn location of any player joining the arena. Use `56.546,64.0,44.45` to specify coordinates, or `here`, `this` or any other string to select your current location. | | spawnLocation | The spawn location of any player joining the arena. Use `56.546,64.0,44.45` to specify coordinates, or `here`, `this` or any other string to select your current location. |
| exitLocation | The location players will be sent to when exiting the arena. If not set, the player will be sent to where they joined from. Valid values are the same as for spawnLocation. | | exitLocation | The location players will be sent to when exiting the arena. If not set, the player will be sent to where they joined from. Valid values are the same as for spawnLocation. |
| verticalVelocity | The vertical velocity set for players in the arena (basically their falling speed). It must be greater than 0, but max 75. `12.565` and other decimals are allowed. | | verticalVelocity | The vertical velocity set for players in the arena (basically their falling speed). It must be greater than 0, but max 75. `12.565` and other decimals are allowed. |
| horizontalVelocity | The horizontal velocity (technically fly speed) set for players in the arena. It must be between 0 and 1, and cannot be 0. Decimals are allowed. | | horizontalVelocity | The horizontal velocity (technically fly speed) set for players in the arena. It must be between 0 and 1, and cannot be 0. Decimals are allowed. |
| winBlockType | The type of block players must hit to win the arena. It can be any material as long as it's a block, and not a type of air. | | winBlockType | The type of block players must hit to win the arena. It can be any material as long as it's a block, and not a type of air. |
| allowedDamageCauses | A list of damage causes that can affect players in the arena. The damage is simulated, rather than real damage. Reaching 0 health triggers a loss. Note: Fall damage cannot be allowed. |
| lossTriggerDamageCauses | A list of damage causes that will trigger a loss for the arena. Useful for arenas where players need to dodge arrows or similar. Note: Fall damage already causes a loss. |
#### /dropperGroupSet #### /dropperGroupSet
@ -128,6 +186,17 @@ You could use `/droppergroupswap Sea Savanna` to change the order to:
### Command explanation parkour ### Command explanation parkour
#### /parkourJoin
This command is used for joining a dropper arena.
`/parkourjoin <arena> [mode]`
| Argument | Usage |
|----------|-----------------------------------------------------------------------------------------------------------|
| arena | The name of the arena to join. |
| mode | Additional challenge modes can be played after an arena has been cleared once. Available modes: hardcore. |
#### /parkourEdit #### /parkourEdit
This command allows editing the specified property for the specified parkour arena. This command allows editing the specified property for the specified parkour arena.
@ -143,22 +212,25 @@ This command allows editing the specified property for the specified parkour are
These are all the options that can be changed for an arena. These are all the options that can be changed for an arena.
| Option | Details | | Option | Details |
|-----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| name | The name of the arena. Used mainly to select the arena in commands. | | name | The name of the arena. Used mainly to select the arena in commands. Note that underscore (_) cannot be used if you want to utilize placeholders, as it's used to split placeholder arguments. |
| spawnLocation | The spawn location of any player joining the arena. Use `56.546,64.0,44.45` to specify coordinates, or `here`, `this` or any other string to select your current location. | | spawnLocation | The spawn location of any player joining the arena. Use `56.546,64.0,44.45` to specify coordinates, or `here`, `this` or any other string to select your current location. |
| exitLocation | The location players will be sent to when exiting the arena. If not set, the player will be sent to where they joined from. Valid values are the same as for spawnLocation. | | exitLocation | The location players will be sent to when exiting the arena. If not set, the player will be sent to where they joined from. Valid values are the same as for spawnLocation. |
| winBlockType | The type of block players must hit to win the arena. It can be any material as long as it's a block, and not a type of air. | | winBlockType | The type of block players must hit to win the arena. It can be any material as long as it's a block, and not a type of air. |
| winLocation | The location players must reach to win the arena (see spawnLocation for valid values). If set, this overrides, and is used instead of, the win block type. | | winLocation | The location players must reach to win the arena (see spawnLocation for valid values). If set, this overrides, and is used instead of, the win block type. |
| checkpointAdd | Adds a new checkpoint to the arena's checkpoints (see spawnLocation for valid values). | | checkpointAdd | Adds a new checkpoint to the arena's checkpoints (see spawnLocation for valid values). |
| checkpointClear | Clears all current checkpoints. Give any value to execute. If not given a value, current checkpoints are shown. | | checkpointClear | Clears all current checkpoints. Give any value to execute. If not given a value, current checkpoints are shown. |
| killPlaneBlocks | A comma-separated list of materials which will force a loss on hit. +WOOL and other [material tags](#notes-about-material-tags) are supported as well. | | killPlaneBlocks | A comma-separated list of materials which will force a loss when stepped on. +WOOL and other [material tags](#notes-about-material-tags) are supported as well. |
| obstacleBlocks | A comma-separated list of materials which will force a loss when touched from any direction. +WOOL and other [material tags](#notes-about-material-tags) are supported as well. |
| allowedDamageCauses | A list of damage causes that can affect players in the arena. The damage is simulated, rather than real damage. Reaching 0 health triggers a loss. |
| lossTriggerDamageCauses | A list of damage causes that will trigger a loss for the arena. Useful for arenas where players need to dodge arrows or similar. |
## Configuration options ## Configuration options
### Shared ### Shared
| Name | Type | Default | Description | | Name | Type | Default | Description |
|-----------------------------------|---------------------|-------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |---------------------|------------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| liquidHitBoxDepth | -1 < decimal < 0 | -0.8 | This decides how far inside a non-solid block the player must go before detection triggers (-1, 0). The closer to -1 it is, the more accurate it will seem to the player, but the likelihood of not detecting the hit increases. | | liquidHitBoxDepth | -1 < decimal < 0 | -0.8 | This decides how far inside a non-solid block the player must go before detection triggers (-1, 0). The closer to -1 it is, the more accurate it will seem to the player, but the likelihood of not detecting the hit increases. |
| solidHitBoxDistance | 0 < decimal < 1 | 0.2 | This decides the distance the player must be from a block below them before a hit triggers (0, 1). If too low, the likelihood of detecting the hit decreases, but it won't look like the player hit the block without being near. | | solidHitBoxDistance | 0 < decimal < 1 | 0.2 | This decides the distance the player must be from a block below them before a hit triggers (0, 1). If too low, the likelihood of detecting the hit decreases, but it won't look like the player hit the block without being near. |
@ -174,8 +246,6 @@ These are all the options that can be changed for an arena.
| mustDoGroupedInSequence | true/false | true | Whether grouped dropper arenas must be played in the correct sequence | | mustDoGroupedInSequence | true/false | true | Whether grouped dropper arenas must be played in the correct sequence |
| ignoreRecordsUntilGroupBeatenOnce | true/false | false | Whether records won't be registered unless the player has already beaten all arenas in a group. That means players are required to do a second play-through to register a record for a grouped arena. | | ignoreRecordsUntilGroupBeatenOnce | true/false | false | Whether records won't be registered unless the player has already beaten all arenas in a group. That means players are required to do a second play-through to register a record for a grouped arena. |
| mustDoNormalModeFirst | true/false | true | Whether a player must do the normal/default game-mode before playing any other game-modes | | mustDoNormalModeFirst | true/false | true | Whether a player must do the normal/default game-mode before playing any other game-modes |
| makePlayersInvisible | true/false | false | Whether players should be made invisible while playing in a dropper arena |
| disableHitCollision | true/false | true | Whether players should have their entity hit collision disabled while in an arena. This prevents players from pushing each-other if in the same arena. |
| liquidHitBoxDepth | -1 < decimal < 0 | -0.8 | This decides how far inside a non-solid block the player must go before detection triggers (-1, 0). The closer to -1 it is, the more accurate it will seem to the player, but the likelihood of not detecting the hit increases. | | liquidHitBoxDepth | -1 < decimal < 0 | -0.8 | This decides how far inside a non-solid block the player must go before detection triggers (-1, 0). The closer to -1 it is, the more accurate it will seem to the player, but the likelihood of not detecting the hit increases. |
| solidHitBoxDistance | 0 < decimal < 1 | 0.2 | This decides the distance the player must be from a block below them before a hit triggers (0, 1). If too low, the likelihood of detecting the hit decreases, but it won't look like the player hit the block without being near. | | solidHitBoxDistance | 0 < decimal < 1 | 0.2 | This decides the distance the player must be from a block below them before a hit triggers (0, 1). If too low, the likelihood of detecting the hit decreases, but it won't look like the player hit the block without being near. |
| blockWhitelist | list | [see this](#blockwhitelist-default) | A whitelist for which blocks won't trigger a loss when hit/passed through. The win block check happens before the loss check, so even blocks on the whitelist can be used as the win-block. "+" denotes a [material tag](#notes-about-material-tags). | | blockWhitelist | list | [see this](#blockwhitelist-default) | A whitelist for which blocks won't trigger a loss when hit/passed through. The win block check happens before the loss check, so even blocks on the whitelist can be used as the win-block. "+" denotes a [material tag](#notes-about-material-tags). |
@ -187,8 +257,8 @@ These are all the options that can be changed for an arena.
| enforceCheckpointOrder | true/false | false | Whether to enforce the order in which a player must reach checkpoints. Enabling this ensures that a player cannot trigger a previous checkpoint by accident. It also ensures players cannot skip a checkpoint, even if the arena layout makes it possible. | | enforceCheckpointOrder | true/false | false | Whether to enforce the order in which a player must reach checkpoints. Enabling this ensures that a player cannot trigger a previous checkpoint by accident. It also ensures players cannot skip a checkpoint, even if the arena layout makes it possible. |
| mustDoGroupedInSequence | true/false | true | Whether grouped dropper arenas must be played in the correct sequence | | mustDoGroupedInSequence | true/false | true | Whether grouped dropper arenas must be played in the correct sequence |
| ignoreRecordsUntilGroupBeatenOnce | true/false | false | Whether records won't be registered unless the player has already beaten all arenas in a group. That means players are required to do a second play-through to register a record for a grouped arena. | | ignoreRecordsUntilGroupBeatenOnce | true/false | false | Whether records won't be registered unless the player has already beaten all arenas in a group. That means players are required to do a second play-through to register a record for a grouped arena. |
| makePlayersInvisible | true/false | false | Whether players should be made invisible while playing in a dropper arena |
| killPlaneBlocks | list | [see this](#killplaneblocks-default) | The types of blocks compromising parkour arenas' kill planes. Add any materials you want to use for the "bottom" of your parkour arenas. +WOOL and other [material tags](#notes-about-material-tags) are supported. | | killPlaneBlocks | list | [see this](#killplaneblocks-default) | The types of blocks compromising parkour arenas' kill planes. Add any materials you want to use for the "bottom" of your parkour arenas. +WOOL and other [material tags](#notes-about-material-tags) are supported. |
| obstacleBlocks | list | [see this](#obstacleblocks-default) | The types of blocks treated as obstacles in every direction. +WOOL and other [material tags](#notes-about-material-tags) are supported. |
#### blockWhitelist default: #### blockWhitelist default:
@ -210,25 +280,61 @@ These are all the options that can be changed for an arena.
- LAVA - LAVA
- MAGMA_BLOCK - MAGMA_BLOCK
## Record placeholders #### obstacleBlocks default:
- END_ROD
- LIGHTNING_ROD
- CHAIN
## Placeholders
### Record Placeholders
Player records can be displayed on a leaderboard by using PlaceholderAPI. If you want to display a sign-based Player records can be displayed on a leaderboard by using PlaceholderAPI. If you want to display a sign-based
leaderboard, you can use the [Placeholder Signs](https://git.knarcraft.net/EpicKnarvik97/PlaceholderSigns) plugin. The leaderboard, you can use the [Placeholder Signs](https://git.knarcraft.net/EpicKnarvik97/PlaceholderSigns) plugin. The
format for the built-in placeholders is as follows: format for the built-in placeholders is as follows:
`%gameMode_record_recordType_gameModeType_identifierType_identifier_recordPlacing_infoType%` `%gameMode_record_recordType_gameModeType_identifierType_identifier_recordPosition_infoType%`
| Variable | Values | Description | | Variable | Values | Description |
|----------------|-----------------------------|------------------------------------------------------------------------------------------------------------------------------------| |----------------|----------------------------------------|------------------------------------------------------------------------------------------------------------------------------------|
| gameMode | dropper / parkour | A selection of which game-mode you are getting a record for | | gameMode | dropper / parkour | A selection of which game-mode you are getting a record for |
| record | | This must be as-is. It's a selector in case placeholders are added for more than records. | | record | | This must be as-is. It's a selector for the type of placeholder to get. |
| recordType | deaths / time | Selects the type of record to get (deaths or time). | | recordType | deaths / time | Selects the type of record to get (deaths or time). |
| gameModeType | default / inverted / random | Selects the game-mode to get the record for. | | gameModeType | default / inverted / random / hardcore | Selects the game-mode to get the record for. |
| identifierType | arena / group | The type of thing the following identifier points to (an arena or an arena group). | | identifierType | arena / group | The type of thing the following identifier points to (an arena or an arena group). |
| identifier | ? | An identifier (the name or UUID) for an arena or a group (whichever was chosen as identifierType). | | identifier | ? | An identifier (the name or UUID) for an arena or a group (whichever was chosen as identifierType). |
| recordPlacing | 1 / 2 / 3 / ... | The position of the record to get (1 = first place, 2 = second place, etc.). | | recordPosition | 1 / 2 / 3 / ... | The position of the record to get (1 = first place, 2 = second place, etc.). |
| infoType | player / value / combined | The type of info to get. Player gets the player name, Value gets the value of the achieved record. Combined gets "Player: Record". | | infoType | player / value / combined | The type of info to get. Player gets the player name, Value gets the value of the achieved record. Combined gets "Player: Record". |
### Player Placeholders
The number of currently playing players can be displayed using PlaceholderAPI. If you want to display a sign-based
leaderboard, you can use the [Placeholder Signs](https://git.knarcraft.net/EpicKnarvik97/PlaceholderSigns) plugin. The
format for the built-in placeholders is as follows:
`%gameMode_players_playing_gameModeType_identifierType_identifier_infoType_additionalSpecifier%`
| Variable | Values | Description |
|-----------------|----------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|
| gameMode | dropper / parkour | A selection of which game-mode you are getting a record for |
| players_playing | | This must be as-is. |
| gameModeType | default / inverted / random / hardcore / all | Selects the game-mode to get the players for. Note that "all" combines players for all game-modes. |
| identifierType | arena / group | The type of thing the following identifier points to (an arena or an arena group). |
| identifier | ? | An identifier (the name or UUID) for an arena or a group (whichever was chosen as identifierType). |
| playerInfoType | player / count | The type of info to get. Player gets the player name, and count gets the total number of players. |
| playerNumber | 1 / 2 / 3 / ... | The player number to get the name of. Players are numbered after their sorted names. This argument is not used when getting the player count. |
### Max Players Placeholder
`%gameMode_players_max_arenaName%`
| Variable | Values | Description |
|-----------------|-------------------|-------------------------------------------------------------|
| gameMode | dropper / parkour | A selection of which game-mode you are getting a record for |
| players_maximum | | This must be as-is. |
| arenaName | ? | An identifier (the name or UUID) for an arena. |
## Notes about material tags ## Notes about material tags
Where a list of material is allowed, this plugin supports using material tags that specify a set of blocks. This makes Where a list of material is allowed, this plugin supports using material tags that specify a set of blocks. This makes
@ -250,6 +356,20 @@ Example tags:
- +FENCE_GATES - +FENCE_GATES
- +FENCES - +FENCES
## Language customization
Most or all strings are customizable. If you place a strings.yml file in the plugin folder, it will take
priority over built-in languages. If you want to change strings, look at MiniGames/src/main/resources/strings.yml for
the proper keys. All strings have the format: ENUM: "Displayed string". The enum must be identical as it defines which
string you have changed. All strings belonging to a language are beneath the language code and indented with two spaces.
The easiest way to add a new language is to copy an existing language and paste it into your custom strings.yml and
change strings as necessary. If you don't include all strings, the remaining will use the built-in English translation.
Remember to change the language code to whichever you use for your custom language.
The interval messages are unique in that if several values are separated by comma (option1,option2,option3), a random
message will be chosen each time it's displayed.
## License ## License
MiniGames is licensed under the GNU Public License Version 3.0. This includes every source and resource file. See the MiniGames is licensed under the GNU Public License Version 3.0. This includes every source and resource file. See the

90
pom.xml
View File

@ -6,7 +6,7 @@
<groupId>net.knarcraft</groupId> <groupId>net.knarcraft</groupId>
<artifactId>MiniGames</artifactId> <artifactId>MiniGames</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.3-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>MiniGames</name> <name>MiniGames</name>
@ -31,7 +31,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version> <version>3.3.0</version>
<executions> <executions>
<execution> <execution>
<phase>package</phase> <phase>package</phase>
@ -40,6 +40,40 @@
</goals> </goals>
<configuration> <configuration>
<createDependencyReducedPom>false</createDependencyReducedPom> <createDependencyReducedPom>false</createDependencyReducedPom>
<relocations>
<relocation>
<pattern>net.knarcraft.knarlib</pattern>
<shadedPattern>net.knarcraft.minigames.lib.knarlib</shadedPattern>
</relocation>
<relocation>
<pattern>net.knarcraft.knargui</pattern>
<shadedPattern>net.knarcraft.minigames.lib.knargui</shadedPattern>
</relocation>
<relocation>
<pattern>org.jetbrains.annotations</pattern>
<shadedPattern>net.knarcraft.minigames.lib.annotations</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
<artifact>net.knarcraft:knarlib</artifact>
<includes>
<include>net/knarcraft/knarlib/**</include>
</includes>
</filter>
<filter>
<artifact>net.knarcraft:knargui</artifact>
<includes>
<include>net/knarcraft/knargui/**</include>
</includes>
</filter>
<filter>
<artifact>org.jetbrains:annotations</artifact>
<includes>
<include>org/jetbrains/annotations/**</include>
</includes>
</filter>
</filters>
</configuration> </configuration>
</execution> </execution>
</executions> </executions>
@ -62,26 +96,32 @@
<id>placeholderapi</id> <id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url> <url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository> </repository>
<repository>
<id>knarcraft-repo</id>
<url>https://git.knarcraft.net/api/packages/EpicKnarvik97/maven</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>opencollab-snapshot</id>
<url>https://repo.opencollab.dev/main/</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId> <artifactId>spigot-api</artifactId>
<version>1.19.4-R0.1-SNAPSHOT</version> <version>1.21.4-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.jetbrains</groupId> <groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId> <artifactId>annotations</artifactId>
<version>24.0.1</version> <version>24.0.1</version>
<scope>provided</scope> <scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>me.clip</groupId> <groupId>me.clip</groupId>
@ -89,5 +129,35 @@
<version>2.10.0</version> <version>2.10.0</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>net.knarcraft</groupId>
<artifactId>knargui</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.knarcraft</groupId>
<artifactId>knarlib</artifactId>
<version>1.2.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.geysermc.geyser</groupId>
<artifactId>api</artifactId>
<version>2.2.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.geysermc.floodgate</groupId>
<artifactId>api</artifactId>
<version>2.2.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,7 +1,12 @@
package net.knarcraft.minigames; package net.knarcraft.minigames;
import net.knarcraft.knargui.GUIListener;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.Translator;
import net.knarcraft.knarlib.property.ColorConversion;
import net.knarcraft.minigames.arena.ArenaPlayerRegistry; import net.knarcraft.minigames.arena.ArenaPlayerRegistry;
import net.knarcraft.minigames.arena.ArenaSession; import net.knarcraft.minigames.arena.ArenaSession;
import net.knarcraft.minigames.arena.PlayerVisibilityManager;
import net.knarcraft.minigames.arena.dropper.DropperArena; import net.knarcraft.minigames.arena.dropper.DropperArena;
import net.knarcraft.minigames.arena.dropper.DropperArenaData; import net.knarcraft.minigames.arena.dropper.DropperArenaData;
import net.knarcraft.minigames.arena.dropper.DropperArenaGameMode; import net.knarcraft.minigames.arena.dropper.DropperArenaGameMode;
@ -20,8 +25,16 @@ import net.knarcraft.minigames.arena.parkour.ParkourArenaRecordsRegistry;
import net.knarcraft.minigames.arena.parkour.ParkourPlayerEntryState; import net.knarcraft.minigames.arena.parkour.ParkourPlayerEntryState;
import net.knarcraft.minigames.arena.record.IntegerRecord; import net.knarcraft.minigames.arena.record.IntegerRecord;
import net.knarcraft.minigames.arena.record.LongRecord; import net.knarcraft.minigames.arena.record.LongRecord;
import net.knarcraft.minigames.arena.reward.CommandReward;
import net.knarcraft.minigames.arena.reward.EconomyReward;
import net.knarcraft.minigames.arena.reward.ItemReward;
import net.knarcraft.minigames.arena.reward.PermissionReward;
import net.knarcraft.minigames.arena.reward.RewardCondition;
import net.knarcraft.minigames.command.LeaveArenaCommand; import net.knarcraft.minigames.command.LeaveArenaCommand;
import net.knarcraft.minigames.command.MenuCommand;
import net.knarcraft.minigames.command.ReloadCommand; import net.knarcraft.minigames.command.ReloadCommand;
import net.knarcraft.minigames.command.SetArenaRewardCommand;
import net.knarcraft.minigames.command.SetArenaRewardTabCompleter;
import net.knarcraft.minigames.command.dropper.CreateDropperArenaCommand; import net.knarcraft.minigames.command.dropper.CreateDropperArenaCommand;
import net.knarcraft.minigames.command.dropper.DropperGroupListCommand; import net.knarcraft.minigames.command.dropper.DropperGroupListCommand;
import net.knarcraft.minigames.command.dropper.DropperGroupSetCommand; import net.knarcraft.minigames.command.dropper.DropperGroupSetCommand;
@ -45,22 +58,31 @@ import net.knarcraft.minigames.command.parkour.ParkourGroupSwapCommand;
import net.knarcraft.minigames.command.parkour.RemoveParkourArenaCommand; import net.knarcraft.minigames.command.parkour.RemoveParkourArenaCommand;
import net.knarcraft.minigames.command.parkour.RemoveParkourArenaTabCompleter; import net.knarcraft.minigames.command.parkour.RemoveParkourArenaTabCompleter;
import net.knarcraft.minigames.config.DropperConfiguration; import net.knarcraft.minigames.config.DropperConfiguration;
import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.config.ParkourConfiguration; import net.knarcraft.minigames.config.ParkourConfiguration;
import net.knarcraft.minigames.config.SharedConfiguration; import net.knarcraft.minigames.config.SharedConfiguration;
import net.knarcraft.minigames.container.SerializableMaterial; import net.knarcraft.minigames.container.SerializableMaterial;
import net.knarcraft.minigames.container.SerializableUUID; import net.knarcraft.minigames.container.SerializableUUID;
import net.knarcraft.minigames.listener.CommandListener; import net.knarcraft.minigames.listener.CommandListener;
import net.knarcraft.minigames.listener.DamageListener; import net.knarcraft.minigames.listener.DamageListener;
import net.knarcraft.minigames.listener.InteractListener;
import net.knarcraft.minigames.listener.MoveListener; import net.knarcraft.minigames.listener.MoveListener;
import net.knarcraft.minigames.listener.PlayerStateChangeListener; import net.knarcraft.minigames.listener.PlayerStateChangeListener;
import net.knarcraft.minigames.placeholder.DropperRecordExpansion; import net.knarcraft.minigames.manager.EconomyManager;
import net.knarcraft.minigames.placeholder.ParkourRecordExpansion; import net.knarcraft.minigames.manager.PermissionManager;
import net.knarcraft.minigames.placeholder.DropperExpansion;
import net.knarcraft.minigames.placeholder.ParkourExpansion;
import net.md_5.bungee.api.ChatColor;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicesManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -80,10 +102,13 @@ public final class MiniGames extends JavaPlugin {
private ParkourConfiguration parkourConfiguration; private ParkourConfiguration parkourConfiguration;
private DropperArenaHandler dropperArenaHandler; private DropperArenaHandler dropperArenaHandler;
private ArenaPlayerRegistry<DropperArena> dropperArenaPlayerRegistry; private ArenaPlayerRegistry<DropperArena> dropperArenaPlayerRegistry;
private DropperRecordExpansion dropperRecordExpansion; private DropperExpansion dropperExpansion;
private ParkourRecordExpansion parkourRecordExpansion; private ParkourExpansion parkourExpansion;
private ParkourArenaHandler parkourArenaHandler; private ParkourArenaHandler parkourArenaHandler;
private ArenaPlayerRegistry<ParkourArena> parkourArenaPlayerRegistry; private ArenaPlayerRegistry<ParkourArena> parkourArenaPlayerRegistry;
private PlayerVisibilityManager playerVisibilityManager;
private Translator translator;
private StringFormatter stringFormatter;
/** /**
* Gets an instance of this plugin * Gets an instance of this plugin
@ -159,6 +184,33 @@ public final class MiniGames extends JavaPlugin {
return this.parkourConfiguration; return this.parkourConfiguration;
} }
/**
* Gets the manager keeping track of player visibility
*
* @return <p>The player visibility manager</p>
*/
public PlayerVisibilityManager getPlayerVisibilityManager() {
return this.playerVisibilityManager;
}
/**
* Gets the translator to get messages from
*
* @return <p>The translator</p>
*/
public Translator getTranslator() {
return this.translator;
}
/**
* Gets the string formatter to get formatted messages from
*
* @return <p>The string formatter</p>
*/
public StringFormatter getStringFormatter() {
return this.stringFormatter;
}
/** /**
* Gets the current session of the given player * Gets the current session of the given player
* *
@ -194,13 +246,15 @@ public final class MiniGames extends JavaPlugin {
// Reload configuration // Reload configuration
this.reloadConfig(); this.reloadConfig();
translator.loadLanguages(this.getDataFolder(), "en",
getConfig().getString("language", "en"));
this.sharedConfiguration.load(this.getConfig()); this.sharedConfiguration.load(this.getConfig());
this.dropperConfiguration.load(this.getConfig()); this.dropperConfiguration.load(this.getConfig());
this.parkourConfiguration.load(this.getConfig()); this.parkourConfiguration.load(this.getConfig());
// Clear record caches // Clear record caches
this.dropperRecordExpansion.clearCaches(); this.dropperExpansion.clearCaches();
this.parkourRecordExpansion.clearCaches(); this.parkourExpansion.clearCaches();
} }
@Override @Override
@ -221,63 +275,29 @@ public final class MiniGames extends JavaPlugin {
ConfigurationSerialization.registerClass(ParkourArenaGameMode.class); ConfigurationSerialization.registerClass(ParkourArenaGameMode.class);
ConfigurationSerialization.registerClass(DropperPlayerEntryState.class); ConfigurationSerialization.registerClass(DropperPlayerEntryState.class);
ConfigurationSerialization.registerClass(ParkourPlayerEntryState.class); ConfigurationSerialization.registerClass(ParkourPlayerEntryState.class);
ConfigurationSerialization.registerClass(CommandReward.class);
ConfigurationSerialization.registerClass(EconomyReward.class);
ConfigurationSerialization.registerClass(ItemReward.class);
ConfigurationSerialization.registerClass(PermissionReward.class);
ConfigurationSerialization.registerClass(RewardCondition.class);
} }
@Override @Override
public void onEnable() { public void onEnable() {
// Plugin startup logic // Plugin startup logic
instance = this; instance = this;
this.saveDefaultConfig();
getConfig().options().copyDefaults(true);
saveConfig();
reloadConfig();
this.sharedConfiguration = new SharedConfiguration(this.getConfig());
this.dropperConfiguration = new DropperConfiguration(this.getConfig());
this.parkourConfiguration = new ParkourConfiguration(this.getConfig());
this.dropperArenaPlayerRegistry = new DropperArenaPlayerRegistry();
this.dropperArenaHandler = new DropperArenaHandler(this.dropperArenaPlayerRegistry);
this.dropperArenaHandler.load();
this.parkourArenaPlayerRegistry = new ParkourArenaPlayerRegistry();
this.parkourArenaHandler = new ParkourArenaHandler(this.parkourArenaPlayerRegistry);
this.parkourArenaHandler.load();
PluginManager pluginManager = getServer().getPluginManager(); // Load configuration
pluginManager.registerEvents(new DamageListener(), this); loadConfiguration();
pluginManager.registerEvents(new MoveListener(this.dropperConfiguration, this.parkourConfiguration), this);
pluginManager.registerEvents(new PlayerStateChangeListener(), this);
pluginManager.registerEvents(new CommandListener(), this);
registerCommand("miniGamesReload", new ReloadCommand(), null); // Register all listeners
registerCommand("miniGamesLeave", new LeaveArenaCommand(), null); registerListeners();
registerCommand("dropperCreate", new CreateDropperArenaCommand(), null); // Register all commands
registerCommand("dropperList", new ListDropperArenaCommand(), null); registerCommands();
registerCommand("dropperJoin", new JoinDropperArenaCommand(), new JoinDropperArenaTabCompleter());
registerCommand("dropperEdit", new EditDropperArenaCommand(this.dropperConfiguration), new EditDropperArenaTabCompleter());
registerCommand("dropperRemove", new RemoveDropperArenaCommand(), new RemoveDropperArenaTabCompleter());
registerCommand("dropperGroupSet", new DropperGroupSetCommand(), null);
registerCommand("dropperGroupSwap", new DropperGroupSwapCommand(), null);
registerCommand("dropperGroupList", new DropperGroupListCommand(), null);
registerCommand("parkourCreate", new CreateParkourArenaCommand(), null); // Integrate with other plugins
registerCommand("parkourList", new ListParkourArenaCommand(), null); doPluginIntegration();
registerCommand("parkourJoin", new JoinParkourArenaCommand(), new JoinParkourArenaTabCompleter());
registerCommand("parkourEdit", new EditParkourArenaCommand(), new EditParkourArenaTabCompleter());
registerCommand("parkourRemove", new RemoveParkourArenaCommand(), new RemoveParkourArenaTabCompleter());
registerCommand("parkourGroupSet", new ParkourGroupSetCommand(), null);
registerCommand("parkourGroupSwap", new ParkourGroupSwapCommand(), null);
registerCommand("parkourGroupList", new ParkourGroupListCommand(), null);
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
this.dropperRecordExpansion = new DropperRecordExpansion(this);
if (!this.dropperRecordExpansion.register()) {
log(Level.WARNING, "Unable to register PlaceholderAPI dropper expansion!");
}
this.parkourRecordExpansion = new ParkourRecordExpansion(this);
if (!this.parkourRecordExpansion.register()) {
log(Level.WARNING, "Unable to register PlaceholderAPI parkour expansion!");
}
}
} }
@Override @Override
@ -291,6 +311,63 @@ public final class MiniGames extends JavaPlugin {
} }
} }
/**
* Sets up integration with third-party plugins
*/
private void doPluginIntegration() {
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
this.dropperExpansion = new DropperExpansion(this);
if (!this.dropperExpansion.register()) {
log(Level.WARNING, "Unable to register PlaceholderAPI dropper expansion!");
}
this.parkourExpansion = new ParkourExpansion(this);
if (!this.parkourExpansion.register()) {
log(Level.WARNING, "Unable to register PlaceholderAPI parkour expansion!");
}
}
if (Bukkit.getPluginManager().getPlugin("Vault") != null) {
setupVault();
} else {
log(Level.WARNING, "Vault not found. Economy and Permission rewards are unavailable.");
}
}
/**
* Loads all configuration values used by this plugin
*/
private void loadConfiguration() {
this.saveDefaultConfig();
getConfig().options().copyDefaults(true);
saveConfig();
reloadConfig();
setupStringFormatter();
this.sharedConfiguration = new SharedConfiguration(this.getConfig());
this.dropperConfiguration = new DropperConfiguration(this.getConfig());
this.parkourConfiguration = new ParkourConfiguration(this.getConfig());
this.dropperArenaPlayerRegistry = new DropperArenaPlayerRegistry();
this.dropperArenaHandler = new DropperArenaHandler(this.dropperArenaPlayerRegistry);
this.dropperArenaHandler.load();
this.parkourArenaPlayerRegistry = new ParkourArenaPlayerRegistry();
this.parkourArenaHandler = new ParkourArenaHandler(this.parkourArenaPlayerRegistry);
this.parkourArenaHandler.load();
this.playerVisibilityManager = new PlayerVisibilityManager();
}
/**
* Registers all listeners used by this plugin
*/
private void registerListeners() {
PluginManager pluginManager = getServer().getPluginManager();
pluginManager.registerEvents(new DamageListener(), this);
pluginManager.registerEvents(new MoveListener(this.dropperConfiguration, this.parkourConfiguration), this);
pluginManager.registerEvents(new PlayerStateChangeListener(), this);
pluginManager.registerEvents(new CommandListener(), this);
pluginManager.registerEvents(new GUIListener(true), this);
pluginManager.registerEvents(new InteractListener(), this);
}
/** /**
* Registers a command * Registers a command
* *
@ -311,4 +388,82 @@ public final class MiniGames extends JavaPlugin {
} }
} }
/**
* Registers all commands used by this plugin
*/
private void registerCommands() {
registerCommand("miniGamesReload", new ReloadCommand(), null);
registerCommand("miniGamesLeave", new LeaveArenaCommand(), null);
registerCommand("miniGamesMenu", new MenuCommand(), null);
registerCommand("miniGamesReward", new SetArenaRewardCommand(), new SetArenaRewardTabCompleter());
registerDropperCommands();
registerParkourCommands();
}
/**
* Registers all commands related to droppers
*/
private void registerDropperCommands() {
registerCommand("dropperCreate", new CreateDropperArenaCommand(), null);
registerCommand("dropperList", new ListDropperArenaCommand(), null);
registerCommand("dropperJoin", new JoinDropperArenaCommand(), new JoinDropperArenaTabCompleter());
registerCommand("dropperEdit", new EditDropperArenaCommand(this.dropperConfiguration), new EditDropperArenaTabCompleter());
registerCommand("dropperRemove", new RemoveDropperArenaCommand(), new RemoveDropperArenaTabCompleter());
registerCommand("dropperGroupSet", new DropperGroupSetCommand(), null);
registerCommand("dropperGroupSwap", new DropperGroupSwapCommand(), null);
registerCommand("dropperGroupList", new DropperGroupListCommand(), null);
}
/**
* Registers all commands related to parkour
*/
private void registerParkourCommands() {
registerCommand("parkourCreate", new CreateParkourArenaCommand(), null);
registerCommand("parkourList", new ListParkourArenaCommand(), null);
registerCommand("parkourJoin", new JoinParkourArenaCommand(), new JoinParkourArenaTabCompleter());
registerCommand("parkourEdit", new EditParkourArenaCommand(), new EditParkourArenaTabCompleter());
registerCommand("parkourRemove", new RemoveParkourArenaCommand(), new RemoveParkourArenaTabCompleter());
registerCommand("parkourGroupSet", new ParkourGroupSetCommand(), null);
registerCommand("parkourGroupSwap", new ParkourGroupSwapCommand(), null);
registerCommand("parkourGroupList", new ParkourGroupListCommand(), null);
}
/**
* Sets up the translator and the string formatter
*/
private void setupStringFormatter() {
translator = new Translator();
translator.registerMessageCategory(MiniGameMessage.ERROR_PLAYER_ONLY);
translator.loadLanguages(this.getDataFolder(), "en",
getConfig().getString("language", "en"));
stringFormatter = new StringFormatter(this.getDescription().getName(), translator);
stringFormatter.setColorConversion(ColorConversion.RGB);
stringFormatter.setNamePrefix("#546EED[&r&l");
stringFormatter.setNameSuffix("#546EED]");
stringFormatter.setErrorColor(ChatColor.RED);
stringFormatter.setSuccessColor(ChatColor.GREEN);
}
/**
* Sets up Vault by getting plugins from their providers
*/
private void setupVault() {
ServicesManager servicesManager = this.getServer().getServicesManager();
RegisteredServiceProvider<Permission> permissionProvider = servicesManager.getRegistration(Permission.class);
RegisteredServiceProvider<Economy> economyProvider = servicesManager.getRegistration(Economy.class);
if (permissionProvider != null) {
PermissionManager.initialize(permissionProvider.getProvider());
} else {
log(Level.WARNING, "No Vault permission provider found. Permission rewards are unavailable.");
}
if (economyProvider != null) {
EconomyManager.initialize(economyProvider.getProvider());
} else {
log(Level.WARNING, "No Vault economy provider found. Economy rewards are unavailable.");
}
}
} }

View File

@ -19,7 +19,8 @@ import java.util.logging.Level;
*/ */
public abstract class AbstractArenaPlayerRegistry<K extends Arena> implements ArenaPlayerRegistry<K> { public abstract class AbstractArenaPlayerRegistry<K extends Arena> implements ArenaPlayerRegistry<K> {
private final Map<UUID, ArenaSession> arenaPlayers = new HashMap<>(); private final Map<Arena, Set<UUID>> arenaPlayers = new HashMap<>();
private final Map<UUID, ArenaSession> arenaSessions = new HashMap<>();
private final Map<UUID, PlayerEntryState> entryStates = new HashMap<>(); private final Map<UUID, PlayerEntryState> entryStates = new HashMap<>();
/** /**
@ -30,14 +31,25 @@ public abstract class AbstractArenaPlayerRegistry<K extends Arena> implements Ar
} }
@Override @Override
public @Nullable PlayerEntryState getEntryState(@NotNull UUID playerId) { @NotNull
public Set<UUID> getPlayingPlayers() {
return arenaSessions.keySet();
}
@Override
@Nullable
public PlayerEntryState getEntryState(@NotNull UUID playerId) {
return this.entryStates.get(playerId); return this.entryStates.get(playerId);
} }
@Override @Override
public void registerPlayer(@NotNull UUID playerId, @NotNull ArenaSession arenaSession) { public void registerPlayer(@NotNull UUID playerId, @NotNull ArenaSession arenaSession) {
this.arenaPlayers.put(playerId, arenaSession); this.arenaSessions.put(playerId, arenaSession);
this.entryStates.put(playerId, arenaSession.getEntryState()); this.entryStates.put(playerId, arenaSession.getEntryState());
this.arenaPlayers.putIfAbsent(arenaSession.getArena(), new HashSet<>());
this.arenaPlayers.get(arenaSession.getArena()).add(playerId);
this.saveEntryStates(); this.saveEntryStates();
} }
@ -55,23 +67,38 @@ public abstract class AbstractArenaPlayerRegistry<K extends Arena> implements Ar
this.saveEntryStates(); this.saveEntryStates();
} }
return this.arenaPlayers.remove(playerId) != null; if (this.arenaSessions.containsKey(playerId)) {
this.arenaPlayers.get(this.arenaSessions.get(playerId).getArena()).remove(playerId);
}
return this.arenaSessions.remove(playerId) != null;
} }
@Override @Override
public @Nullable ArenaSession getArenaSession(@NotNull UUID playerId) { public @Nullable ArenaSession getArenaSession(@NotNull UUID playerId) {
return this.arenaPlayers.getOrDefault(playerId, null); return this.arenaSessions.getOrDefault(playerId, null);
} }
@Override @Override
public void removeForArena(K arena, boolean immediately) { public void removeForArena(@NotNull K arena, boolean immediately) {
for (Map.Entry<UUID, ArenaSession> entry : this.arenaPlayers.entrySet()) { Set<UUID> removed = new HashSet<>();
for (Map.Entry<UUID, ArenaSession> entry : this.arenaSessions.entrySet()) {
if (entry.getValue().getArena() == arena) { if (entry.getValue().getArena() == arena) {
// Kick the player gracefully // Kick the player gracefully
entry.getValue().triggerQuit(immediately); entry.getValue().triggerQuit(immediately, false);
this.arenaPlayers.remove(entry.getKey()); removed.add(entry.getKey());
} }
} }
removed.forEach(this.arenaSessions::remove);
}
@Override
@NotNull
public Set<UUID> getPlayingPlayers(@NotNull K arena) {
if (arenaPlayers.containsKey(arena)) {
return arenaPlayers.get(arena);
} else {
return new HashSet<>();
}
} }
/** /**
@ -97,7 +124,7 @@ public abstract class AbstractArenaPlayerRegistry<K extends Arena> implements Ar
for (PlayerEntryState entryState : entryStates) { for (PlayerEntryState entryState : entryStates) {
this.entryStates.put(entryState.getPlayerId(), entryState); this.entryStates.put(entryState.getPlayerId(), entryState);
} }
if (this.entryStates.size() > 0) { if (!this.entryStates.isEmpty()) {
MiniGames.log(Level.WARNING, entryStates.size() + " un-exited sessions found. This happens if " + MiniGames.log(Level.WARNING, entryStates.size() + " un-exited sessions found. This happens if " +
"players leave in the middle of a game, or if the server crashes. MiniGames will do its best " + "players leave in the middle of a game, or if the server crashes. MiniGames will do its best " +
"to fix the players' states."); "to fix the players' states.");

View File

@ -1,9 +1,13 @@
package net.knarcraft.minigames.arena; package net.knarcraft.minigames.arena;
import net.knarcraft.minigames.config.Message; import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.container.PlaceholderContainer; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.reward.RewardCondition;
import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.property.RecordResult; import net.knarcraft.minigames.property.RecordResult;
import net.knarcraft.minigames.property.RecordType;
import net.knarcraft.minigames.util.PlayerTeleporter; import net.knarcraft.minigames.util.PlayerTeleporter;
import net.knarcraft.minigames.util.RewardHelper;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -14,7 +18,7 @@ public abstract class AbstractArenaSession implements ArenaSession {
private final @NotNull ArenaGameMode gameMode; private final @NotNull ArenaGameMode gameMode;
private final @NotNull Player player; private final @NotNull Player player;
protected int deaths; protected int deaths;
protected final long startTime; protected long startTime;
protected PlayerEntryState entryState; protected PlayerEntryState entryState;
/** /**
@ -33,22 +37,34 @@ public abstract class AbstractArenaSession implements ArenaSession {
} }
@Override @Override
public void triggerQuit(boolean immediately) { public void triggerQuit(boolean immediately, boolean removeSession) {
// Stop this session // Stop this session
if (removeSession) {
removeSession(); removeSession();
}
// Teleport the player out of the arena // Teleport the player out of the arena
teleportToExit(immediately); teleportToExit(immediately);
// Make the player visible to everyone
MiniGames.getInstance().getPlayerVisibilityManager().showPlayersFor(player);
player.sendMessage(Message.SUCCESS_ARENA_QUIT.getMessage()); MiniGames.getInstance().getStringFormatter().displaySuccessMessage(player, MiniGameMessage.SUCCESS_ARENA_QUIT);
}
@Override
public void reset() {
this.deaths = 0;
this.startTime = System.currentTimeMillis();
PlayerTeleporter.teleportPlayer(this.player, this.arena.getSpawnLocation(), false, false);
this.entryState.setArenaState();
} }
/** /**
* Announces a record set by this player * Announces a record set by this player
* *
* @param recordResult <p>The result of the record</p> * @param recordResult <p>The result of the record</p>
* @param type <p>The type of record set (time or deaths)</p> * @param recordType <p>The type of record set (time or deaths)</p>
*/ */
protected void announceRecord(@NotNull RecordResult recordResult, @NotNull String type) { protected void announceRecord(@NotNull RecordResult recordResult, @NotNull RecordType recordType) {
if (recordResult == RecordResult.NONE) { if (recordResult == RecordResult.NONE) {
return; return;
} }
@ -56,16 +72,21 @@ public abstract class AbstractArenaSession implements ArenaSession {
// Gets a string representation of the played game-mode // Gets a string representation of the played game-mode
String gameModeString = getGameModeString(); String gameModeString = getGameModeString();
Message recordInfoMessage = switch (recordResult) { MiniGameMessage recordInfoMiniGameMessage = switch (recordResult) {
case WORLD_RECORD -> Message.RECORD_ACHIEVED_GLOBAL; case WORLD_RECORD -> MiniGameMessage.RECORD_ACHIEVED_GLOBAL;
case PERSONAL_BEST -> Message.RECORD_ACHIEVED_PERSONAL; case PERSONAL_BEST -> MiniGameMessage.RECORD_ACHIEVED_PERSONAL;
default -> throw new IllegalStateException("Unexpected value: " + recordResult); default -> throw new IllegalStateException("Unexpected value: " + recordResult);
}; };
String recordInfo = recordInfoMessage.getPartialMessage("{recordType}", type); StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
String recordInfo = stringFormatter.replacePlaceholder(recordInfoMiniGameMessage, "{recordType}",
recordType.name().toLowerCase().replace("_", " "));
PlaceholderContainer placeholderContainer = new PlaceholderContainer().add("{gameMode}", gameModeString); stringFormatter.displaySuccessMessage(player, stringFormatter.replacePlaceholders(
placeholderContainer.add("{recordInfo}", recordInfo); MiniGameMessage.SUCCESS_RECORD_ACHIEVED, new String[]{"{gameMode}", "{recordInfo}"},
player.sendMessage(Message.SUCCESS_RECORD_ACHIEVED.getMessage(placeholderContainer)); new String[]{gameModeString, recordInfo}));
// Reward the player
rewardRecord(recordResult, recordType);
} }
/** /**
@ -74,8 +95,32 @@ public abstract class AbstractArenaSession implements ArenaSession {
protected void registerRecord() { protected void registerRecord() {
ArenaRecordsRegistry recordsRegistry = this.arena.getData().getRecordRegistries().get(this.gameMode); ArenaRecordsRegistry recordsRegistry = this.arena.getData().getRecordRegistries().get(this.gameMode);
long timeElapsed = System.currentTimeMillis() - this.startTime; long timeElapsed = System.currentTimeMillis() - this.startTime;
announceRecord(recordsRegistry.registerTimeRecord(this.player.getUniqueId(), timeElapsed), "time"); announceRecord(recordsRegistry.registerTimeRecord(this.player.getUniqueId(), timeElapsed), RecordType.TIME);
announceRecord(recordsRegistry.registerDeathRecord(this.player.getUniqueId(), this.deaths), "least deaths"); announceRecord(recordsRegistry.registerDeathRecord(this.player.getUniqueId(), this.deaths), RecordType.DEATHS);
}
/**
* Rewards the specified achieved record
*
* @param recordResult <p>The result of the record achieved</p>
* @param recordType <p>The type of record achieved</p>
*/
protected void rewardRecord(RecordResult recordResult, RecordType recordType) {
RewardCondition condition = null;
if (recordResult == RecordResult.WORLD_RECORD) {
if (recordType == RecordType.DEATHS) {
condition = RewardCondition.GLOBAL_DEATH_RECORD;
} else if (recordType == RecordType.TIME) {
condition = RewardCondition.GLOBAL_TIME_RECORD;
}
} else if (recordResult == RecordResult.PERSONAL_BEST) {
if (recordType == RecordType.DEATHS) {
condition = RewardCondition.PERSONAL_DEATH_RECORD;
} else if (recordType == RecordType.TIME) {
condition = RewardCondition.PERSONAL_TIME_RECORD;
}
}
RewardHelper.grantRewards(this.player, this.arena.getRewards(condition));
} }
/** /**

View File

@ -2,16 +2,24 @@ package net.knarcraft.minigames.arena;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.container.SerializableUUID; import net.knarcraft.minigames.container.SerializableUUID;
import net.knarcraft.minigames.property.PersistentDataKey;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
@ -21,7 +29,6 @@ import java.util.logging.Level;
public abstract class AbstractPlayerEntryState implements PlayerEntryState { public abstract class AbstractPlayerEntryState implements PlayerEntryState {
protected final UUID playerId; protected final UUID playerId;
private final boolean makePlayerInvisible;
private final Location entryLocation; private final Location entryLocation;
private final boolean originalIsFlying; private final boolean originalIsFlying;
private final GameMode originalGameMode; private final GameMode originalGameMode;
@ -29,16 +36,17 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
private final boolean originalInvulnerable; private final boolean originalInvulnerable;
private final boolean originalIsSwimming; private final boolean originalIsSwimming;
private final boolean originalCollideAble; private final boolean originalCollideAble;
private final double originalHealth;
private final float originalSaturation;
private final Collection<PotionEffect> originalPotionEffects;
/** /**
* Instantiates a new abstract player entry state * Instantiates a new abstract player entry state
* *
* @param player <p>The player whose state this should keep track of</p> * @param player <p>The player whose state this should keep track of</p>
* @param makePlayerInvisible <p>Whether players should be made invisible while in the arena</p>
*/ */
public AbstractPlayerEntryState(@NotNull Player player, boolean makePlayerInvisible) { public AbstractPlayerEntryState(@NotNull Player player) {
this.playerId = player.getUniqueId(); this.playerId = player.getUniqueId();
this.makePlayerInvisible = makePlayerInvisible;
this.entryLocation = player.getLocation().clone(); this.entryLocation = player.getLocation().clone();
this.originalIsFlying = player.isFlying(); this.originalIsFlying = player.isFlying();
this.originalGameMode = player.getGameMode(); this.originalGameMode = player.getGameMode();
@ -46,13 +54,20 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
this.originalInvulnerable = player.isInvulnerable(); this.originalInvulnerable = player.isInvulnerable();
this.originalIsSwimming = player.isSwimming(); this.originalIsSwimming = player.isSwimming();
this.originalCollideAble = player.isCollidable(); this.originalCollideAble = player.isCollidable();
this.originalHealth = player.getHealth();
this.originalSaturation = player.getSaturation();
// Store and clear potion effects
this.originalPotionEffects = getPlayer().getActivePotionEffects();
for (PotionEffect potionEffect : this.originalPotionEffects) {
player.removePotionEffect(potionEffect.getType());
}
} }
/** /**
* Instantiates a new abstract player entry state * Instantiates a new abstract player entry state
* *
* @param playerId <p>The id of the player whose state this should keep track of</p> * @param playerId <p>The id of the player whose state this should keep track of</p>
* @param makePlayerInvisible <p>Whether players should be made invisible while in the arena</p>
* @param entryLocation <p>The location the player entered from</p> * @param entryLocation <p>The location the player entered from</p>
* @param originalIsFlying <p>Whether the player was flying before entering the arena</p> * @param originalIsFlying <p>Whether the player was flying before entering the arena</p>
* @param originalGameMode <p>The game-mode of the player before entering the arena</p> * @param originalGameMode <p>The game-mode of the player before entering the arena</p>
@ -60,13 +75,16 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
* @param originalInvulnerable <p>Whether the player was invulnerable before entering the arena</p> * @param originalInvulnerable <p>Whether the player was invulnerable before entering the arena</p>
* @param originalIsSwimming <p>Whether the player was swimming before entering the arena</p> * @param originalIsSwimming <p>Whether the player was swimming before entering the arena</p>
* @param originalCollideAble <p>Whether the player was collide-able before entering the arena</p> * @param originalCollideAble <p>Whether the player was collide-able before entering the arena</p>
* @param originalPotionEffects <p>The potion effects applied to the player when joining</p>
* @param originalHealth <p>The health of the player when joining the arena</p>
* @param originalSaturation <p>The saturation of the player when joining the arena</p>
*/ */
public AbstractPlayerEntryState(@NotNull UUID playerId, boolean makePlayerInvisible, Location entryLocation, public AbstractPlayerEntryState(@NotNull UUID playerId, @NotNull Location entryLocation,
boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight, boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight,
boolean originalInvulnerable, boolean originalIsSwimming, boolean originalInvulnerable, boolean originalIsSwimming,
boolean originalCollideAble) { boolean originalCollideAble, @NotNull Collection<PotionEffect> originalPotionEffects,
double originalHealth, float originalSaturation) {
this.playerId = playerId; this.playerId = playerId;
this.makePlayerInvisible = makePlayerInvisible;
this.entryLocation = entryLocation; this.entryLocation = entryLocation;
this.originalIsFlying = originalIsFlying; this.originalIsFlying = originalIsFlying;
this.originalGameMode = originalGameMode; this.originalGameMode = originalGameMode;
@ -74,6 +92,9 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
this.originalInvulnerable = originalInvulnerable; this.originalInvulnerable = originalInvulnerable;
this.originalIsSwimming = originalIsSwimming; this.originalIsSwimming = originalIsSwimming;
this.originalCollideAble = originalCollideAble; this.originalCollideAble = originalCollideAble;
this.originalPotionEffects = originalPotionEffects;
this.originalHealth = originalHealth;
this.originalSaturation = originalSaturation;
} }
@Override @Override
@ -81,18 +102,6 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
return this.playerId; return this.playerId;
} }
@Override
public void setArenaState() {
Player player = getPlayer();
if (player == null) {
return;
}
if (this.makePlayerInvisible) {
player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY,
PotionEffect.INFINITE_DURATION, 3));
}
}
@Override @Override
public boolean restore() { public boolean restore() {
Player player = getPlayer(); Player player = getPlayer();
@ -105,15 +114,19 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
@Override @Override
public void restore(@NotNull Player player) { public void restore(@NotNull Player player) {
player.setFlying(this.originalIsFlying); player.setCollidable(this.originalCollideAble);
player.setGameMode(this.originalGameMode);
player.setAllowFlight(this.originalAllowFlight); player.setAllowFlight(this.originalAllowFlight);
player.setFlying(player.getAllowFlight() && this.originalIsFlying);
player.setGameMode(this.originalGameMode);
player.setInvulnerable(this.originalInvulnerable); player.setInvulnerable(this.originalInvulnerable);
player.setSwimming(this.originalIsSwimming); player.setSwimming(this.originalIsSwimming);
player.setCollidable(this.originalCollideAble); for (PotionEffect potionEffect : originalPotionEffects) {
if (this.makePlayerInvisible) { player.addPotionEffect(potionEffect);
player.removePotionEffect(PotionEffectType.INVISIBILITY);
} }
removeMenuItem(player);
player.setHealth(originalHealth);
player.setSaturation(originalSaturation);
player.setFallDistance(0);
} }
@Override @Override
@ -140,7 +153,6 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
public Map<String, Object> serialize() { public Map<String, Object> serialize() {
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
data.put("playerId", new SerializableUUID(this.playerId)); data.put("playerId", new SerializableUUID(this.playerId));
data.put("makePlayerInvisible", this.makePlayerInvisible);
data.put("entryLocation", this.entryLocation); data.put("entryLocation", this.entryLocation);
data.put("originalIsFlying", this.originalIsFlying); data.put("originalIsFlying", this.originalIsFlying);
data.put("originalGameMode", this.originalGameMode.name()); data.put("originalGameMode", this.originalGameMode.name());
@ -148,7 +160,48 @@ public abstract class AbstractPlayerEntryState implements PlayerEntryState {
data.put("originalInvulnerable", this.originalInvulnerable); data.put("originalInvulnerable", this.originalInvulnerable);
data.put("originalIsSwimming", this.originalIsSwimming); data.put("originalIsSwimming", this.originalIsSwimming);
data.put("originalCollideAble", this.originalCollideAble); data.put("originalCollideAble", this.originalCollideAble);
data.put("originalPotionEffects", this.originalPotionEffects);
data.put("originalHealth", this.originalHealth);
data.put("originalSaturation", this.originalSaturation);
return data; return data;
} }
/**
* Removes the menu item from the given player's inventory
*
* @param player <p>The player to remove the menu item from</p>
*/
private void removeMenuItem(Player player) {
Set<ItemStack> itemsToRemove = new HashSet<>();
player.getInventory().forEach((item) -> {
if (item == null) {
return;
}
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return;
}
Integer persistentData = meta.getPersistentDataContainer().get(new NamespacedKey(MiniGames.getInstance(),
PersistentDataKey.MENU_ITEM.getKeyName()), PersistentDataType.INTEGER);
if (persistentData != null && persistentData == PersistentDataKey.MENU_ITEM.getDataValue()) {
itemsToRemove.add(item);
}
});
for (ItemStack toRemove : itemsToRemove) {
player.getInventory().remove(toRemove);
}
}
/**
* Gets a boolean value from a serialization map
*
* @param data <p>The serialization data to look through</p>
* @param key <p>The key to get</p>
* @return <p>The boolean value of the key</p>
*/
protected static boolean getBoolean(Map<String, Object> data, String key) {
Boolean value = (Boolean) data.get(key);
return Objects.requireNonNullElse(value, false);
}
} }

View File

@ -1,15 +1,21 @@
package net.knarcraft.minigames.arena; package net.knarcraft.minigames.arena;
import net.knarcraft.minigames.arena.reward.Reward;
import net.knarcraft.minigames.arena.reward.RewardCondition;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.event.entity.EntityDamageEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
/** /**
* An interface describing an arena * An interface describing an arena
*/ */
@SuppressWarnings("unused")
public interface Arena { public interface Arena {
/** /**
@ -17,28 +23,40 @@ public interface Arena {
* *
* @return <p>The name of this arena</p> * @return <p>The name of this arena</p>
*/ */
@NotNull String getArenaName(); @NotNull
String getArenaName();
/**
* Sets the name of this arena
*
* @param arenaName <p>The new name</p>
* @return <p>True if successfully updated</p>
*/
boolean setName(@NotNull String arenaName);
/** /**
* Gets the data stored for this arena * Gets the data stored for this arena
* *
* @return <p>The stored data</p> * @return <p>The stored data</p>
*/ */
@NotNull ArenaData getData(); @NotNull
ArenaData getData();
/** /**
* Gets the id of this arena * Gets the id of this arena
* *
* @return <p>This arena's identifier</p> * @return <p>This arena's identifier</p>
*/ */
@NotNull UUID getArenaId(); @NotNull
UUID getArenaId();
/** /**
* Gets this arena's sanitized name * Gets this arena's sanitized name
* *
* @return <p>This arena's sanitized name</p> * @return <p>This arena's sanitized name</p>
*/ */
@NotNull String getArenaNameSanitized(); @NotNull
String getArenaNameSanitized();
/** /**
* Removes the data file belonging to this arena * Removes the data file belonging to this arena
@ -54,13 +72,31 @@ public interface Arena {
*/ */
boolean saveData(); boolean saveData();
/**
* Gets the type of block a player has to hit to win this arena
*
* @return <p>The kind of block players must hit</p>
*/
@NotNull
Material getWinBlockType();
/**
* Sets the material of the win block type
*
* <p>The win block type is the type of block a player must hit to win in this arena</p>
*
* @param material <p>The material to set for the win block type</p>
* @return <p>True if successfully updated</p>
*/
boolean setWinBlockType(@NotNull Material material);
/** /**
* Gets whether standing on the given block should cause a win * Gets whether standing on the given block should cause a win
* *
* @param block <p>The block to check</p> * @param block <p>The block to check</p>
* @return <p>True if standing on the block will cause a win</p> * @return <p>True if standing on the block will cause a win</p>
*/ */
boolean willCauseWin(Block block); boolean willCauseWin(@NotNull Block block);
/** /**
* Gets whether standing on the given block should cause a loss * Gets whether standing on the given block should cause a loss
@ -68,7 +104,7 @@ public interface Arena {
* @param block <p>The block to check</p> * @param block <p>The block to check</p>
* @return <p>True if standing on the block will cause a loss</p> * @return <p>True if standing on the block will cause a loss</p>
*/ */
boolean willCauseLoss(Block block); boolean willCauseLoss(@NotNull Block block);
/** /**
* Gets whether the win location is a solid block * Gets whether the win location is a solid block
@ -82,13 +118,105 @@ public interface Arena {
* *
* @return <p>This arena's spawn location</p> * @return <p>This arena's spawn location</p>
*/ */
@NotNull Location getSpawnLocation(); @NotNull
Location getSpawnLocation();
/**
* Sets the spawn location for this arena
*
* @param newLocation <p>The new spawn location</p>
* @return <p>True if successfully updated</p>
*/
boolean setSpawnLocation(@Nullable Location newLocation);
/** /**
* Gets this arena's exit location * Gets this arena's exit location
* *
* @return <p>This arena's exit location, or null if no such location is set.</p> * @return <p>This arena's exit location, or null if no such location is set.</p>
*/ */
@Nullable Location getExitLocation(); @Nullable
Location getExitLocation();
/**
* Sets the exit location for this arena
*
* @param newLocation <p>The new exit location</p>
* @return <p>True if successfully updated</p>
*/
boolean setExitLocation(@Nullable Location newLocation);
/**
* Adds a reward to this arena
*
* @param rewardCondition <p>The condition for the reward to be granted</p>
* @param reward <p>The reward to be granted</p>
*/
void addReward(@NotNull RewardCondition rewardCondition, @NotNull Reward reward);
/**
* Clears this arena's rewards for the given condition
*
* @param rewardCondition <p>The reward condition to clear all rewards for</p>
*/
void clearRewards(@NotNull RewardCondition rewardCondition);
/**
* Gets all rewards for the given reward condition
*
* @param rewardCondition <p>The condition to get the rewards for</p>
* @return <p>All rewards</p>
*/
@NotNull
Set<Reward> getRewards(RewardCondition rewardCondition);
/**
* Gets the maximum amount of players that can join this arena at once
*
* @return <p>The maximum amount of players</p>
*/
int getMaxPlayers();
/**
* Sets the maximum amount of players that can join this arena at once
*
* @param newValue <p>The new maximum amount of players</p>
*/
boolean setMaxPlayers(int newValue);
/**
* Gets the damage causes that won't be blocked for this arena
*
* <p>Receiving any damage of this type will be allowed, but if the damage is fatal, a loss will be triggered.</p>
*
* @return <p>The damage causes that won't be blocked</p>
*/
@NotNull
Set<EntityDamageEvent.DamageCause> getAllowedDamageCauses();
/**
* Sets the damage causes that will trigger a loss for this arena
*
* <p>Receiving any damage of this type will immediately cause a loss to the player.</p>
*
* @return <p>The damage causes that will trigger a loss</p>
*/
@NotNull
Set<EntityDamageEvent.DamageCause> getLossTriggerDamageCauses();
/**
* Sets the damage causes that are allowed for this arena
*
* @param causes <p>The allowed damage causes</p>
*/
@SuppressWarnings("SameReturnValue")
boolean setAllowedDamageCauses(@NotNull Set<EntityDamageEvent.DamageCause> causes);
/**
* Sets the damage causes that will trigger a loss for this arena
*
* @param causes <p>The causes that cause a loss</p>
*/
@SuppressWarnings("SameReturnValue")
boolean setLossTriggerDamageCauses(@NotNull Set<EntityDamageEvent.DamageCause> causes);
} }

View File

@ -12,13 +12,15 @@ public interface ArenaGameMode {
* *
* @return <p>The name of this game-mode</p> * @return <p>The name of this game-mode</p>
*/ */
@NotNull String name(); @NotNull
String name();
/** /**
* Gets a set of all available arena game-modes in the type definition of this game-mode * Gets a set of all available arena game-modes in the type definition of this game-mode
* *
* @return <p>All game-modes in this game-mode's class</p> * @return <p>All game-modes in this game-mode's class</p>
*/ */
@NotNull ArenaGameMode[] getValues(); @NotNull
ArenaGameMode[] getValues();
} }

View File

@ -151,8 +151,12 @@ public abstract class ArenaHandler<K extends Arena, S extends ArenaGroup<K, S>>
* @return <p>The arena with the given name, or null if not found</p> * @return <p>The arena with the given name, or null if not found</p>
*/ */
public @Nullable K getArena(@NotNull String arenaName) { public @Nullable K getArena(@NotNull String arenaName) {
try {
return this.arenas.get(UUID.fromString(arenaName));
} catch (IllegalArgumentException exception) {
return this.arenas.get(this.arenaNameLookup.get(StringSanitizer.sanitizeArenaName(arenaName))); return this.arenas.get(this.arenaNameLookup.get(StringSanitizer.sanitizeArenaName(arenaName)));
} }
}
/** /**
* Gets all known arenas * Gets all known arenas

View File

@ -3,6 +3,7 @@ package net.knarcraft.minigames.arena;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
/** /**
@ -12,13 +13,31 @@ import java.util.UUID;
*/ */
public interface ArenaPlayerRegistry<K extends Arena> { public interface ArenaPlayerRegistry<K extends Arena> {
/**
* Gets the ids of the players currently playing
*
* @return <p>The ids of the playing players</p>
*/
@NotNull
Set<UUID> getPlayingPlayers();
/**
* Gets all players currently playing in the given arena
*
* @param arena <p>The arena to check</p>
* @return <p>All players currently in the arena</p>
*/
@NotNull
Set<UUID> getPlayingPlayers(@NotNull K arena);
/** /**
* Gets the current entry state for the given player * Gets the current entry state for the given player
* *
* @param playerId <p>The id of the player to get an entry state for</p> * @param playerId <p>The id of the player to get an entry state for</p>
* @return <p>The entry state of the player, or null if not found</p> * @return <p>The entry state of the player, or null if not found</p>
*/ */
@Nullable PlayerEntryState getEntryState(@NotNull UUID playerId); @Nullable
PlayerEntryState getEntryState(@NotNull UUID playerId);
/** /**
* Registers that the given player has started playing the given dropper arena session * Registers that the given player has started playing the given dropper arena session
@ -42,7 +61,8 @@ public interface ArenaPlayerRegistry<K extends Arena> {
* @param playerId <p>The id of the player to get arena for</p> * @param playerId <p>The id of the player to get arena for</p>
* @return <p>The player's active arena session, or null if not currently playing</p> * @return <p>The player's active arena session, or null if not currently playing</p>
*/ */
@Nullable ArenaSession getArenaSession(@NotNull UUID playerId); @Nullable
ArenaSession getArenaSession(@NotNull UUID playerId);
/** /**
* Removes all active sessions for the given arena * Removes all active sessions for the given arena
@ -50,6 +70,6 @@ public interface ArenaPlayerRegistry<K extends Arena> {
* @param arena <p>The arena to remove sessions for</p> * @param arena <p>The arena to remove sessions for</p>
* @param immediately <p>Whether to immediately teleport the player</p> * @param immediately <p>Whether to immediately teleport the player</p>
*/ */
void removeForArena(K arena, boolean immediately); void removeForArena(@NotNull K arena, boolean immediately);
} }

View File

@ -1,5 +1,6 @@
package net.knarcraft.minigames.arena; package net.knarcraft.minigames.arena;
import net.knarcraft.minigames.gui.ArenaGUI;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
@ -12,7 +13,8 @@ public interface ArenaSession {
* *
* @return <p>The player's entry state</p> * @return <p>The player's entry state</p>
*/ */
@NotNull PlayerEntryState getEntryState(); @NotNull
PlayerEntryState getEntryState();
/** /**
* Triggers a win for the player playing in this session * Triggers a win for the player playing in this session
@ -28,14 +30,37 @@ public interface ArenaSession {
* Triggers a quit for the player playing in this session * Triggers a quit for the player playing in this session
* *
* @param immediately <p>Whether to to the teleportation immediately, not using any timers</p> * @param immediately <p>Whether to to the teleportation immediately, not using any timers</p>
* @param removeSession <p>Whether to also remove the session. Should usually be true.</p>
*/ */
void triggerQuit(boolean immediately); void triggerQuit(boolean immediately, boolean removeSession);
/** /**
* Gets the arena this session is being played in * Gets the arena this session is being played in
* *
* @return <p>The session's arena</p> * @return <p>The session's arena</p>
*/ */
@NotNull Arena getArena(); @NotNull
Arena getArena();
/**
* Gets the GUI with this arena's options
*
* @return <p>This arena's GUI</p>
*/
@NotNull
ArenaGUI getGUI();
/**
* Resets the session to allow a player to try again
*/
void reset();
/**
* Gets the game-mode the player is playing
*
* @return <p>The game-mode the player is playing</p>
*/
@NotNull
ArenaGameMode getGameMode();
} }

View File

@ -38,6 +38,21 @@ public enum EditablePropertyType {
/** /**
* The property is a comma-separated list of materials * The property is a comma-separated list of materials
*/ */
MATERIAL_LIST MATERIAL_LIST,
/**
* The property is any double value
*/
DOUBLE,
/**
* The property is an integer with no particular restrictions
*/
INTEGER,
/**
* The property is a comma-separated list of damage causes
*/
DAMAGE_CAUSE_LIST,
} }

View File

@ -0,0 +1,109 @@
package net.knarcraft.minigames.arena;
import net.knarcraft.minigames.MiniGames;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
/**
* A manager for keeping track of which players have set other players as hidden
*/
public class PlayerVisibilityManager {
private final Set<UUID> displayingEnabledFor = new HashSet<>();
/**
* Toggles whether players should be hidden for the player with the given id
*
* @param player <p>The the player to update</p>
*/
public void toggleHidePlayers(@Nullable ArenaPlayerRegistry<?> playerRegistry, @NotNull Player player) {
if (displayingEnabledFor.contains(player.getUniqueId())) {
displayingEnabledFor.remove(player.getUniqueId());
// Make all other players hidden
changeVisibilityFor(playerRegistry, player, true);
} else {
displayingEnabledFor.add(player.getUniqueId());
// Make all other players visible again
changeVisibilityFor(playerRegistry, player, false);
}
}
/**
* Gets whether the given player is currently hiding other players
*
* @param player <p>The player to check</p>
* @return <p>True if currently hiding other players</p>
*/
public boolean isHidingPlayers(Player player) {
return !this.displayingEnabledFor.contains(player.getUniqueId());
}
/**
* Updates which players are seen as hidden
*
* @param playerRegistry <p>The registry containing all playing players</p>
* @param player <p>The player that joined the arena</p>
*/
public void updateHiddenPlayers(@NotNull ArenaPlayerRegistry<?> playerRegistry, @NotNull Player player) {
boolean hideForPlayer = !displayingEnabledFor.contains(player.getUniqueId());
for (UUID playerId : playerRegistry.getPlayingPlayers()) {
Player otherPlayer = Bukkit.getPlayer(playerId);
if (otherPlayer == null) {
continue;
}
// Hide the arena player from the newly joined player
if (hideForPlayer) {
player.hidePlayer(MiniGames.getInstance(), otherPlayer);
}
// Hide the newly joined player from this player
if (!displayingEnabledFor.contains(playerId)) {
otherPlayer.hidePlayer(MiniGames.getInstance(), player);
}
}
}
/**
* Makes all players visible to the given player
*
* @param player <p>The player to update visibility for</p>
*/
public void showPlayersFor(@NotNull Player player) {
for (Player otherPlayer : Bukkit.getOnlinePlayers()) {
player.showPlayer(MiniGames.getInstance(), otherPlayer);
otherPlayer.showPlayer(MiniGames.getInstance(), player);
}
}
/**
* Changes whether the given player can see the other players in the arena
*
* @param playerRegistry <p>The player registry containing other players</p>
* @param player <p>The player to change the visibility for</p>
* @param hide <p>Whether to hide the players or show the players</p>
*/
private void changeVisibilityFor(@Nullable ArenaPlayerRegistry<?> playerRegistry, @NotNull Player player, boolean hide) {
if (playerRegistry == null) {
return;
}
for (UUID playerId : playerRegistry.getPlayingPlayers()) {
Player otherPlayer = Bukkit.getPlayer(playerId);
if (otherPlayer == null) {
continue;
}
if (hide) {
player.hidePlayer(MiniGames.getInstance(), otherPlayer);
} else {
player.showPlayer(MiniGames.getInstance(), otherPlayer);
}
}
}
}

View File

@ -0,0 +1,18 @@
package net.knarcraft.minigames.arena;
import org.jetbrains.annotations.NotNull;
/**
* A representation of each key used for storing arena data
*/
public interface StorageKey {
/**
* Gets the configuration key this enum represents
*
* @return <p>The string key representation.</p>
*/
@NotNull
String getKey();
}

View File

@ -4,19 +4,26 @@ import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.Arena; import net.knarcraft.minigames.arena.Arena;
import net.knarcraft.minigames.arena.ArenaGameMode; import net.knarcraft.minigames.arena.ArenaGameMode;
import net.knarcraft.minigames.arena.ArenaRecordsRegistry; import net.knarcraft.minigames.arena.ArenaRecordsRegistry;
import net.knarcraft.minigames.arena.reward.Reward;
import net.knarcraft.minigames.arena.reward.RewardCondition;
import net.knarcraft.minigames.config.DropperConfiguration; import net.knarcraft.minigames.config.DropperConfiguration;
import net.knarcraft.minigames.util.DropperArenaStorageHelper; import net.knarcraft.minigames.util.DropperArenaStorageHelper;
import net.knarcraft.minigames.util.InputValidationHelper;
import net.knarcraft.minigames.util.StringSanitizer; import net.knarcraft.minigames.util.StringSanitizer;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.event.entity.EntityDamageEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level;
import static net.knarcraft.minigames.util.InputValidationHelper.isInvalid; import static net.knarcraft.minigames.util.InputValidationHelper.isInvalid;
@ -58,6 +65,21 @@ public class DropperArena implements Arena {
*/ */
private float playerHorizontalVelocity; private float playerHorizontalVelocity;
/**
* The maximum amount of players able to join this arena at any time
*/
private int maxPlayers = -1;
/**
* Types of damage that won't be blocked in this arena
*/
private Set<EntityDamageEvent.DamageCause> allowedDamageCauses;
/**
* Types of damage that will trigger a loss in this arena
*/
private Set<EntityDamageEvent.DamageCause> lossTriggerDamageCauses;
/** /**
* The material of the block players have to hit to win this dropper arena * The material of the block players have to hit to win this dropper arena
*/ */
@ -70,6 +92,8 @@ public class DropperArena implements Arena {
private final DropperArenaHandler dropperArenaHandler; private final DropperArenaHandler dropperArenaHandler;
private Map<RewardCondition, Set<Reward>> rewards = new HashMap<>();
private static final DropperConfiguration dropperConfiguration = MiniGames.getInstance().getDropperConfiguration(); private static final DropperConfiguration dropperConfiguration = MiniGames.getInstance().getDropperConfiguration();
/** /**
@ -82,13 +106,20 @@ public class DropperArena implements Arena {
* @param playerVerticalVelocity <p>The velocity to use for players' vertical velocity</p> * @param playerVerticalVelocity <p>The velocity to use for players' vertical velocity</p>
* @param playerHorizontalVelocity <p>The velocity to use for players' horizontal velocity (-1 to 1)</p> * @param playerHorizontalVelocity <p>The velocity to use for players' horizontal velocity (-1 to 1)</p>
* @param winBlockType <p>The material of the block players have to hit to win this dropper arena</p> * @param winBlockType <p>The material of the block players have to hit to win this dropper arena</p>
* @param maxPlayers <p>The maximum amount of players able to join this arena at once</p>
* @param rewards <p>The rewards given by this arena</p>
* @param dropperArenaData <p>The arena data keeping track of which players have done what in this arena</p> * @param dropperArenaData <p>The arena data keeping track of which players have done what in this arena</p>
* @param arenaHandler <p>The arena handler used for saving any changes</p> * @param arenaHandler <p>The arena handler used for saving any changes</p>
* @param allowedDamageCauses <p>The damage causes to not cancel. If the player received fatal damage, a loss is triggered.</p>
* @param lossTriggerDamageCauses <p>The damage causes that will trigger a loss (for arrow traps and similar)</p>
*/ */
public DropperArena(@NotNull UUID arenaId, @NotNull String arenaName, @NotNull Location spawnLocation, public DropperArena(@NotNull UUID arenaId, @NotNull String arenaName, @NotNull Location spawnLocation,
@Nullable Location exitLocation, double playerVerticalVelocity, float playerHorizontalVelocity, @Nullable Location exitLocation, double playerVerticalVelocity, float playerHorizontalVelocity,
@NotNull Material winBlockType, @NotNull DropperArenaData dropperArenaData, @NotNull Material winBlockType, int maxPlayers,
@NotNull DropperArenaHandler arenaHandler) { @NotNull Map<RewardCondition, Set<Reward>> rewards, @NotNull DropperArenaData dropperArenaData,
@NotNull DropperArenaHandler arenaHandler,
@Nullable Set<String> allowedDamageCauses,
@Nullable Set<String> lossTriggerDamageCauses) {
this.arenaId = arenaId; this.arenaId = arenaId;
this.arenaName = arenaName; this.arenaName = arenaName;
this.spawnLocation = spawnLocation; this.spawnLocation = spawnLocation;
@ -98,6 +129,10 @@ public class DropperArena implements Arena {
this.winBlockType = winBlockType; this.winBlockType = winBlockType;
this.dropperArenaData = dropperArenaData; this.dropperArenaData = dropperArenaData;
this.dropperArenaHandler = arenaHandler; this.dropperArenaHandler = arenaHandler;
this.rewards = rewards;
this.maxPlayers = maxPlayers;
this.allowedDamageCauses = InputValidationHelper.parseDamageCauses(allowedDamageCauses);
this.lossTriggerDamageCauses = InputValidationHelper.parseDamageCauses(lossTriggerDamageCauses);
} }
/** /**
@ -128,6 +163,8 @@ public class DropperArena implements Arena {
this.dropperArenaData = new DropperArenaData(this.arenaId, recordRegistries, new HashMap<>()); this.dropperArenaData = new DropperArenaData(this.arenaId, recordRegistries, new HashMap<>());
this.winBlockType = Material.WATER; this.winBlockType = Material.WATER;
this.dropperArenaHandler = arenaHandler; this.dropperArenaHandler = arenaHandler;
this.allowedDamageCauses = new HashSet<>();
this.lossTriggerDamageCauses = new HashSet<>();
} }
@Override @Override
@ -155,6 +192,146 @@ public class DropperArena implements Arena {
return this.exitLocation != null ? this.exitLocation.clone() : null; return this.exitLocation != null ? this.exitLocation.clone() : null;
} }
@Override
public void addReward(@NotNull RewardCondition rewardCondition, @NotNull Reward reward) {
this.rewards.computeIfAbsent(rewardCondition, k -> new HashSet<>());
this.rewards.get(rewardCondition).add(reward);
this.saveArena();
}
@Override
public void clearRewards(@NotNull RewardCondition rewardCondition) {
this.rewards.remove(rewardCondition);
this.saveArena();
}
@Override
public @NotNull Set<Reward> getRewards(RewardCondition rewardCondition) {
if (this.rewards.containsKey(rewardCondition) && this.rewards.get(rewardCondition) != null) {
return this.rewards.get(rewardCondition);
} else {
return new HashSet<>();
}
}
@Override
public int getMaxPlayers() {
return this.maxPlayers;
}
@Override
public boolean setMaxPlayers(int newValue) {
if (newValue < -1) {
return false;
}
this.maxPlayers = newValue;
this.saveArena();
return true;
}
@Override
public @NotNull Set<EntityDamageEvent.DamageCause> getAllowedDamageCauses() {
return this.allowedDamageCauses;
}
@Override
public @NotNull Set<EntityDamageEvent.DamageCause> getLossTriggerDamageCauses() {
return this.lossTriggerDamageCauses;
}
@Override
public boolean setAllowedDamageCauses(@NotNull Set<EntityDamageEvent.DamageCause> causes) {
this.allowedDamageCauses = causes;
this.saveArena();
return true;
}
@Override
public boolean setLossTriggerDamageCauses(@NotNull Set<EntityDamageEvent.DamageCause> causes) {
this.lossTriggerDamageCauses = causes;
this.saveArena();
return true;
}
@Override
@NotNull
public Material getWinBlockType() {
return this.winBlockType;
}
@Override
@NotNull
public String getArenaNameSanitized() {
return StringSanitizer.sanitizeArenaName(this.getArenaName());
}
@Override
public boolean removeData() {
return DropperArenaStorageHelper.removeDropperArenaData(getArenaId());
}
@Override
public boolean saveData() {
try {
DropperArenaStorageHelper.saveDropperArenaData(getData());
return true;
} catch (IOException exception) {
return false;
}
}
@Override
public boolean willCauseWin(@NotNull Block block) {
return block.getType() == winBlockType;
}
@Override
public boolean willCauseLoss(@NotNull Block block) {
return !dropperConfiguration.getBlockWhitelist().contains(block.getType());
}
@Override
public boolean winLocationIsSolid() {
return winBlockType.isSolid();
}
@Override
public boolean setSpawnLocation(@Nullable Location newLocation) {
if (isInvalid(newLocation)) {
return false;
} else {
this.spawnLocation = newLocation;
this.saveArena();
return true;
}
}
@Override
public boolean setExitLocation(@Nullable Location newLocation) {
if (isInvalid(newLocation)) {
return false;
} else {
this.exitLocation = newLocation;
this.saveArena();
return true;
}
}
@Override
public boolean setName(@NotNull String arenaName) {
if (!arenaName.isBlank()) {
String oldName = this.getArenaNameSanitized();
this.arenaName = arenaName;
// Update the arena lookup map to make sure the new name can be used immediately
this.dropperArenaHandler.updateLookupName(oldName, this.getArenaNameSanitized());
this.saveArena();
return true;
} else {
return false;
}
}
/** /**
* Gets the vertical velocity for players in this arena * Gets the vertical velocity for players in this arena
* *
@ -178,119 +355,13 @@ public class DropperArena implements Arena {
return this.playerHorizontalVelocity; return this.playerHorizontalVelocity;
} }
/**
* Gets the type of block a player has to hit to win this arena
*
* @return <p>The kind of block players must hit</p>
*/
public @NotNull Material getWinBlockType() {
return this.winBlockType;
}
/**
* Gets this arena's sanitized name
*
* @return <p>This arena's sanitized name</p>
*/
public @NotNull String getArenaNameSanitized() {
return StringSanitizer.sanitizeArenaName(this.getArenaName());
}
@Override @Override
public boolean removeData() {
return DropperArenaStorageHelper.removeDropperArenaData(getArenaId());
}
@Override
public boolean saveData() {
try {
DropperArenaStorageHelper.saveDropperArenaData(getData());
return true;
} catch (IOException e) {
return false;
}
}
@Override
public boolean willCauseWin(Block block) {
return block.getType() == winBlockType;
}
@Override
public boolean willCauseLoss(Block block) {
return !dropperConfiguration.getBlockWhitelist().contains(block.getType());
}
@Override
public boolean winLocationIsSolid() {
return winBlockType.isSolid();
}
/**
* Sets the spawn location for this arena
*
* @param newLocation <p>The new spawn location</p>
* @return <p>True if successfully updated</p>
*/
public boolean setSpawnLocation(@NotNull Location newLocation) {
if (isInvalid(newLocation)) {
return false;
} else {
this.spawnLocation = newLocation;
dropperArenaHandler.saveArenas();
return true;
}
}
/**
* Sets the exit location for this arena
*
* @param newLocation <p>The new exit location</p>
* @return <p>True if successfully updated</p>
*/
public boolean setExitLocation(@NotNull Location newLocation) {
if (isInvalid(newLocation)) {
return false;
} else {
this.exitLocation = newLocation;
dropperArenaHandler.saveArenas();
return true;
}
}
/**
* Sets the name of this arena
*
* @param arenaName <p>The new name</p>
* @return <p>True if successfully updated</p>
*/
public boolean setName(@NotNull String arenaName) {
if (!arenaName.isBlank()) {
String oldName = this.getArenaNameSanitized();
this.arenaName = arenaName;
// Update the arena lookup map to make sure the new name can be used immediately
dropperArenaHandler.updateLookupName(oldName, this.getArenaNameSanitized());
dropperArenaHandler.saveArenas();
return true;
} else {
return false;
}
}
/**
* Sets the material of the win block type
*
* <p>The win block type is the type of block a player must hit to win in this arena</p>
*
* @param material <p>The material to set for the win block type</p>
* @return <p>True if successfully updated</p>
*/
public boolean setWinBlockType(@NotNull Material material) { public boolean setWinBlockType(@NotNull Material material) {
if (material.isAir() || !material.isBlock()) { if (material.isAir() || !material.isBlock()) {
return false; return false;
} else { } else {
this.winBlockType = material; this.winBlockType = material;
dropperArenaHandler.saveArenas(); this.saveArena();
return true; return true;
} }
} }
@ -308,7 +379,7 @@ public class DropperArena implements Arena {
return false; return false;
} else { } else {
this.playerHorizontalVelocity = horizontalVelocity; this.playerHorizontalVelocity = horizontalVelocity;
dropperArenaHandler.saveArenas(); this.saveArena();
return true; return true;
} }
} }
@ -324,11 +395,24 @@ public class DropperArena implements Arena {
return false; return false;
} else { } else {
this.playerVerticalVelocity = verticalVelocity; this.playerVerticalVelocity = verticalVelocity;
dropperArenaHandler.saveArenas(); this.saveArena();
return true; return true;
} }
} }
/**
* Saves this arena to disk
*/
public void saveArena() {
try {
DropperArenaStorageHelper.saveSingleDropperArena(this);
} catch (IOException exception) {
MiniGames.log(Level.SEVERE, "Unable to save arena! " +
"Data loss can occur!");
MiniGames.log(Level.SEVERE, exception.getMessage());
}
}
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (!(other instanceof DropperArena otherArena)) { if (!(other instanceof DropperArena otherArena)) {

View File

@ -1,6 +1,7 @@
package net.knarcraft.minigames.arena.dropper; package net.knarcraft.minigames.arena.dropper;
import net.knarcraft.minigames.arena.EditablePropertyType; import net.knarcraft.minigames.arena.EditablePropertyType;
import net.knarcraft.minigames.util.ArenaStorageHelper;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -45,6 +46,26 @@ public enum DropperArenaEditableProperty {
*/ */
WIN_BLOCK_TYPE("winBlockType", (arena) -> arena.getWinBlockType().toString(), WIN_BLOCK_TYPE("winBlockType", (arena) -> arena.getWinBlockType().toString(),
EditablePropertyType.BLOCK_TYPE), EditablePropertyType.BLOCK_TYPE),
/**
* The arena's max players
*/
MAX_PLAYERS("maxPlayers", (arena) -> String.valueOf(arena.getMaxPlayers()),
EditablePropertyType.INTEGER),
/**
* The arena's allowed damage causes
*/
ALLOWED_DAMAGE_CAUSES("allowedDamageCauses", (arena) -> String.valueOf(
ArenaStorageHelper.getDamageCauseNames(arena.getAllowedDamageCauses())),
EditablePropertyType.DAMAGE_CAUSE_LIST),
/**
* The arena's loss trigger damage causes
*/
LOSS_TRIGGER_DAMAGE_CAUSES("lossTriggerDamageCauses", (arena) -> String.valueOf(
ArenaStorageHelper.getDamageCauseNames(arena.getLossTriggerDamageCauses())),
EditablePropertyType.DAMAGE_CAUSE_LIST),
; ;
private final @NotNull String argumentString; private final @NotNull String argumentString;

View File

@ -34,11 +34,11 @@ public enum DropperArenaGameMode implements ConfigurationSerializable, ArenaGame
* @param gameMode <p>The game-mode string to match</p> * @param gameMode <p>The game-mode string to match</p>
* @return <p>The specified arena game-mode</p> * @return <p>The specified arena game-mode</p>
*/ */
public static @NotNull DropperArenaGameMode matchGamemode(@NotNull String gameMode) { public static @NotNull DropperArenaGameMode matchGameMode(@NotNull String gameMode) {
String sanitized = gameMode.trim().toLowerCase(); String sanitized = gameMode.trim().toLowerCase();
if (sanitized.matches("(invert(ed)?|inverse)")) { if (sanitized.matches("(invert(ed)?|inverse)")) {
return DropperArenaGameMode.INVERTED; return DropperArenaGameMode.INVERTED;
} else if (sanitized.matches("rand(om)?")) { } else if (sanitized.matches("rand(om)?_?(inverted)?")) {
return DropperArenaGameMode.RANDOM_INVERTED; return DropperArenaGameMode.RANDOM_INVERTED;
} else { } else {
return DropperArenaGameMode.DEFAULT; return DropperArenaGameMode.DEFAULT;

View File

@ -3,7 +3,7 @@ package net.knarcraft.minigames.arena.dropper;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaHandler; import net.knarcraft.minigames.arena.ArenaHandler;
import net.knarcraft.minigames.arena.ArenaPlayerRegistry; import net.knarcraft.minigames.arena.ArenaPlayerRegistry;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.util.DropperArenaStorageHelper; import net.knarcraft.minigames.util.DropperArenaStorageHelper;
import java.io.IOException; import java.io.IOException;
@ -32,9 +32,10 @@ public class DropperArenaHandler extends ArenaHandler<DropperArena, DropperArena
public void saveGroups() { public void saveGroups() {
try { try {
DropperArenaStorageHelper.saveDropperArenaGroups(new HashSet<>(this.arenaGroups.values())); DropperArenaStorageHelper.saveDropperArenaGroups(new HashSet<>(this.arenaGroups.values()));
} catch (IOException e) { } catch (IOException exception) {
MiniGames.log(Level.SEVERE, Message.ERROR_CANNOT_SAVE_ARENA_GROUPS.getMessage()); MiniGames.log(Level.SEVERE, MiniGames.getInstance().getTranslator().getTranslatedMessage(
MiniGames.log(Level.SEVERE, e.getMessage()); MiniGameMessage.ERROR_CANNOT_SAVE_ARENA_GROUPS));
MiniGames.log(Level.SEVERE, exception.getMessage());
} }
} }
@ -54,10 +55,10 @@ public class DropperArenaHandler extends ArenaHandler<DropperArena, DropperArena
public void saveArenas() { public void saveArenas() {
try { try {
DropperArenaStorageHelper.saveDropperArenas(this.arenas); DropperArenaStorageHelper.saveDropperArenas(this.arenas);
} catch (IOException e) { } catch (IOException exception) {
MiniGames.log(Level.SEVERE, "Unable to save current arenas! " + MiniGames.log(Level.SEVERE, "Unable to save current arenas! " +
"Data loss can occur!"); "Data loss can occur!");
MiniGames.log(Level.SEVERE, e.getMessage()); MiniGames.log(Level.SEVERE, exception.getMessage());
} }
} }

View File

@ -1,11 +1,18 @@
package net.knarcraft.minigames.arena.dropper; package net.knarcraft.minigames.arena.dropper;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.AbstractArenaSession; import net.knarcraft.minigames.arena.AbstractArenaSession;
import net.knarcraft.minigames.arena.PlayerEntryState; import net.knarcraft.minigames.arena.PlayerEntryState;
import net.knarcraft.minigames.config.DropperConfiguration; import net.knarcraft.minigames.arena.reward.RewardCondition;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.gui.ArenaGUI;
import net.knarcraft.minigames.gui.DropperGUI;
import net.knarcraft.minigames.gui.DropperGUIBedrock;
import net.knarcraft.minigames.util.GeyserHelper;
import net.knarcraft.minigames.util.PlayerTeleporter; import net.knarcraft.minigames.util.PlayerTeleporter;
import net.knarcraft.minigames.util.RewardHelper;
import org.bukkit.Sound;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -19,6 +26,7 @@ public class DropperArenaSession extends AbstractArenaSession {
private final @NotNull DropperArena arena; private final @NotNull DropperArena arena;
private final @NotNull Player player; private final @NotNull Player player;
private final @NotNull DropperArenaGameMode gameMode; private final @NotNull DropperArenaGameMode gameMode;
private boolean startedMoving = false;
/** /**
* Instantiates a new dropper arena session * Instantiates a new dropper arena session
@ -34,14 +42,26 @@ public class DropperArenaSession extends AbstractArenaSession {
this.player = player; this.player = player;
this.gameMode = gameMode; this.gameMode = gameMode;
DropperConfiguration configuration = MiniGames.getInstance().getDropperConfiguration(); this.entryState = new DropperPlayerEntryState(player, gameMode, dropperArena.getPlayerHorizontalVelocity());
boolean makeInvisible = configuration.makePlayersInvisible();
boolean disableCollision = configuration.disableHitCollision();
this.entryState = new DropperPlayerEntryState(player, gameMode, makeInvisible, disableCollision,
dropperArena.getPlayerHorizontalVelocity());
this.entryState.setArenaState(); this.entryState.setArenaState();
} }
/**
* Marks that this arena's player has started moving
*/
public void setStartedMoving() {
this.startedMoving = true;
}
/**
* Gets whether the player of this session has started moving in the arena
*
* @return <p>True if the player has started moving</p>
*/
public boolean getStartedMoving() {
return this.startedMoving;
}
/** /**
* Gets the player playing in this session * Gets the player playing in this session
* *
@ -78,11 +98,15 @@ public class DropperArenaSession extends AbstractArenaSession {
registerRecord(); registerRecord();
} }
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
// Mark the arena as cleared // Mark the arena as cleared
if (this.arena.getData().setCompleted(this.gameMode, this.player)) { if (this.arena.getData().setCompleted(this.gameMode, this.player)) {
this.player.sendMessage(Message.SUCCESS_ARENA_FIRST_CLEAR.getMessage()); stringFormatter.displaySuccessMessage(this.player, MiniGameMessage.SUCCESS_ARENA_FIRST_CLEAR);
RewardHelper.grantRewards(this.player, this.arena.getRewards(RewardCondition.FIRST_WIN));
} }
this.player.sendMessage(Message.SUCCESS_ARENA_WIN.getMessage()); stringFormatter.displaySuccessMessage(this.player, MiniGameMessage.SUCCESS_ARENA_WIN);
RewardHelper.grantRewards(this.player, this.arena.getRewards(RewardCondition.WIN));
// Teleport the player out of the arena // Teleport the player out of the arena
teleportToExit(false); teleportToExit(false);
@ -93,6 +117,7 @@ public class DropperArenaSession extends AbstractArenaSession {
this.deaths++; this.deaths++;
//Teleport the player back to the top //Teleport the player back to the top
PlayerTeleporter.teleportPlayer(this.player, this.arena.getSpawnLocation(), true, false); PlayerTeleporter.teleportPlayer(this.player, this.arena.getSpawnLocation(), true, false);
this.player.playSound(this.player, Sound.ENTITY_CHICKEN_EGG, 5f, 0.5f);
this.entryState.setArenaState(); this.entryState.setArenaState();
} }
@ -101,11 +126,26 @@ public class DropperArenaSession extends AbstractArenaSession {
return this.arena; return this.arena;
} }
@Override
public @NotNull ArenaGUI getGUI() {
if (GeyserHelper.isGeyserPlayer(this.player)) {
return new DropperGUIBedrock(this.player);
} else {
return new DropperGUI(this.player);
}
}
@Override
public void reset() {
this.startedMoving = false;
super.reset();
}
@Override @Override
protected void removeSession() { protected void removeSession() {
// Remove this session for game sessions to stop listeners from fiddling more with the player // Remove this session for game sessions to stop listeners from fiddling more with the player
boolean removedSession = MiniGames.getInstance().getDropperArenaPlayerRegistry().removePlayer( boolean removedSession = MiniGames.getInstance().getDropperArenaPlayerRegistry().removePlayer(
player.getUniqueId(), true); this.player.getUniqueId(), true);
if (!removedSession) { if (!removedSession) {
MiniGames.log(Level.SEVERE, "Unable to remove dropper arena session for " + player.getName() + ". " + MiniGames.log(Level.SEVERE, "Unable to remove dropper arena session for " + player.getName() + ". " +
"This will have unintended consequences."); "This will have unintended consequences.");

View File

@ -1,11 +1,12 @@
package net.knarcraft.minigames.arena.dropper; package net.knarcraft.minigames.arena.dropper;
import net.knarcraft.minigames.arena.StorageKey;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* A representation of each key used for storing arena data * A representation of each key used for storing arena data
*/ */
public enum DropperArenaStorageKey { public enum DropperArenaStorageKey implements StorageKey {
/** /**
* The key for an arena's id * The key for an arena's id
@ -43,9 +44,29 @@ public enum DropperArenaStorageKey {
WIN_BLOCK_TYPE("winBlockType"), WIN_BLOCK_TYPE("winBlockType"),
/** /**
* The hey for this arena's data * The key for this arena's data
*/ */
DATA("arenaData"), DATA("arenaData"),
/**
* The key for this arena's rewards
*/
REWARDS("rewards"),
/**
* The key for this arena's maximum players
*/
MAX_PLAYERS("maxPlayers"),
/**
* The key for this arena's allowed damage causes
*/
ALLOWED_DAMAGE_CAUSES("allowedDamageCauses"),
/**
* The key for this arena's loss trigger damage causes
*/
LOSS_TRIGGER_DAMAGE_CAUSES("lossTriggerDamageCauses"),
; ;
private final @NotNull String key; private final @NotNull String key;
@ -59,11 +80,7 @@ public enum DropperArenaStorageKey {
this.key = key; this.key = key;
} }
/** @Override
* Gets the configuration key this enum represents
*
* @return <p>The string key representation.</p>
*/
public @NotNull String getKey() { public @NotNull String getKey() {
return this.key; return this.key;
} }

View File

@ -5,8 +5,11 @@ import net.knarcraft.minigames.container.SerializableUUID;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -16,7 +19,6 @@ import java.util.UUID;
public class DropperPlayerEntryState extends AbstractPlayerEntryState { public class DropperPlayerEntryState extends AbstractPlayerEntryState {
private final float originalFlySpeed; private final float originalFlySpeed;
private final boolean disableHitCollision;
private final float horizontalVelocity; private final float horizontalVelocity;
private final DropperArenaGameMode arenaGameMode; private final DropperArenaGameMode arenaGameMode;
@ -26,11 +28,10 @@ public class DropperPlayerEntryState extends AbstractPlayerEntryState {
* @param player <p>The player whose state should be stored</p> * @param player <p>The player whose state should be stored</p>
*/ */
public DropperPlayerEntryState(@NotNull Player player, @NotNull DropperArenaGameMode arenaGameMode, public DropperPlayerEntryState(@NotNull Player player, @NotNull DropperArenaGameMode arenaGameMode,
boolean makePlayerInvisible, boolean disableHitCollision, float horizontalVelocity) { float horizontalVelocity) {
super(player, makePlayerInvisible); super(player);
this.originalFlySpeed = player.getFlySpeed(); this.originalFlySpeed = player.getFlySpeed();
this.arenaGameMode = arenaGameMode; this.arenaGameMode = arenaGameMode;
this.disableHitCollision = disableHitCollision;
this.horizontalVelocity = horizontalVelocity; this.horizontalVelocity = horizontalVelocity;
} }
@ -38,31 +39,36 @@ public class DropperPlayerEntryState extends AbstractPlayerEntryState {
* Instantiates a new parkour player entry state * Instantiates a new parkour player entry state
* *
* @param playerId <p>The id of the player whose state this should keep track of</p> * @param playerId <p>The id of the player whose state this should keep track of</p>
* @param makePlayerInvisible <p>Whether players should be made invisible while in the arena</p>
* @param entryLocation <p>The location the player entered from</p> * @param entryLocation <p>The location the player entered from</p>
* @param originalIsFlying <p>Whether the player was flying before entering the arena</p> * @param originalIsFlying <p>Whether the player was flying before entering the arena</p>
* @param originalGameMode <p>The game-mode of the player before entering the arena</p> * @param originalGameMode <p>The game-mode of the player before entering the arena</p>
* @param originalAllowFlight <p>Whether the player was allowed flight before entering the arena</p> * @param originalAllowFlight <p>Whether the player was allowed flight before entering the arena</p>
* @param originalInvulnerable <p>Whether the player was invulnerable before entering the arena</p> * @param originalInvulnerable <p>Whether the player was invulnerable before entering the arena</p>
* @param originalIsSwimming <p>Whether the player was swimming before entering the arena</p> * @param originalIsSwimming <p>Whether the player was swimming before entering the arena</p>
* @param originalFlySpeed <p>The fly-speed of the player before entering the arena</p>
* @param horizontalVelocity <p>The horizontal velocity of the player before entering the arena</p>
* @param originalCollideAble <p>Whether the player was collide-able before entering the arena</p> * @param originalCollideAble <p>Whether the player was collide-able before entering the arena</p>
* @param originalPotionEffects <p>The potion effects applied to the player when joining</p>
* @param originalHealth <p>The health of the player when joining the arena</p>
* @param originalSaturation <p>The saturation of the player when joining the arena</p>
*/ */
public DropperPlayerEntryState(@NotNull UUID playerId, boolean makePlayerInvisible, Location entryLocation, public DropperPlayerEntryState(@NotNull UUID playerId, @NotNull Location entryLocation,
boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight, boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight,
boolean originalInvulnerable, boolean originalIsSwimming, boolean originalInvulnerable, boolean originalIsSwimming,
boolean originalCollideAble, float originalFlySpeed, boolean disableHitCollision, float originalFlySpeed, float horizontalVelocity,
float horizontalVelocity, DropperArenaGameMode arenaGameMode) { @NotNull DropperArenaGameMode arenaGameMode, boolean originalCollideAble,
super(playerId, makePlayerInvisible, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight, @NotNull Collection<PotionEffect> originalPotionEffects, double originalHealth,
originalInvulnerable, originalIsSwimming, originalCollideAble); float originalSaturation) {
super(playerId, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight,
originalInvulnerable, originalIsSwimming, originalCollideAble, originalPotionEffects, originalHealth,
originalSaturation);
this.originalFlySpeed = originalFlySpeed; this.originalFlySpeed = originalFlySpeed;
this.disableHitCollision = disableHitCollision;
this.horizontalVelocity = horizontalVelocity; this.horizontalVelocity = horizontalVelocity;
this.arenaGameMode = arenaGameMode; this.arenaGameMode = arenaGameMode;
} }
@Override @Override
public void setArenaState() { public void setArenaState() {
super.setArenaState();
Player player = getPlayer(); Player player = getPlayer();
if (player == null) { if (player == null) {
return; return;
@ -71,9 +77,6 @@ public class DropperPlayerEntryState extends AbstractPlayerEntryState {
player.setFlying(true); player.setFlying(true);
player.setGameMode(GameMode.ADVENTURE); player.setGameMode(GameMode.ADVENTURE);
player.setSwimming(false); player.setSwimming(false);
if (this.disableHitCollision) {
player.setCollidable(false);
}
// If playing on the inverted game-mode, negate the horizontal velocity to swap the controls // If playing on the inverted game-mode, negate the horizontal velocity to swap the controls
if (this.arenaGameMode == DropperArenaGameMode.INVERTED) { if (this.arenaGameMode == DropperArenaGameMode.INVERTED) {
@ -104,7 +107,6 @@ public class DropperPlayerEntryState extends AbstractPlayerEntryState {
public Map<String, Object> serialize() { public Map<String, Object> serialize() {
Map<String, Object> data = super.serialize(); Map<String, Object> data = super.serialize();
data.put("originalFlySpeed", this.originalFlySpeed); data.put("originalFlySpeed", this.originalFlySpeed);
data.put("disableHitCollision", this.disableHitCollision);
data.put("horizontalVelocity", this.horizontalVelocity); data.put("horizontalVelocity", this.horizontalVelocity);
data.put("arenaGameMode", this.arenaGameMode); data.put("arenaGameMode", this.arenaGameMode);
return data; return data;
@ -115,25 +117,28 @@ public class DropperPlayerEntryState extends AbstractPlayerEntryState {
* *
* @return <p>The data to deserialize</p> * @return <p>The data to deserialize</p>
*/ */
@SuppressWarnings("unused") @SuppressWarnings({"unused", "unchecked"})
public static DropperPlayerEntryState deserialize(Map<String, Object> data) { public static DropperPlayerEntryState deserialize(Map<String, Object> data) {
UUID playerId = ((SerializableUUID) data.get("playerId")).getRawValue(); UUID playerId = ((SerializableUUID) data.get("playerId")).getRawValue();
boolean makePlayerInvisible = (boolean) data.get("makePlayerInvisible");
Location entryLocation = (Location) data.get("entryLocation"); Location entryLocation = (Location) data.get("entryLocation");
boolean originalIsFlying = (boolean) data.get("originalIsFlying"); boolean originalIsFlying = getBoolean(data, "originalIsFlying");
GameMode originalGameMode = GameMode.valueOf((String) data.get("originalGameMode")); GameMode originalGameMode = GameMode.valueOf((String) data.get("originalGameMode"));
boolean originalAllowFlight = (boolean) data.get("originalAllowFlight"); boolean originalAllowFlight = getBoolean(data, "originalAllowFlight");
boolean originalInvulnerable = (boolean) data.get("originalInvulnerable"); boolean originalInvulnerable = getBoolean(data, "originalInvulnerable");
boolean originalIsSwimming = (boolean) data.get("originalIsSwimming"); boolean originalIsSwimming = getBoolean(data, "originalIsSwimming");
boolean originalCollideAble = (boolean) data.get("originalCollideAble");
float originalFlySpeed = ((Number) data.get("originalFlySpeed")).floatValue(); float originalFlySpeed = ((Number) data.get("originalFlySpeed")).floatValue();
boolean disableHitCollision = (boolean) data.get("disableHitCollision");
float horizontalVelocity = ((Number) data.get("horizontalVelocity")).floatValue(); float horizontalVelocity = ((Number) data.get("horizontalVelocity")).floatValue();
DropperArenaGameMode arenaGameMode = (DropperArenaGameMode) data.get("arenaGameMode"); DropperArenaGameMode arenaGameMode = (DropperArenaGameMode) data.get("arenaGameMode");
boolean originalCollideAble = getBoolean(data, "originalCollideAble");
Collection<PotionEffect> originalPotionEffect =
(Collection<PotionEffect>) data.getOrDefault("originalPotionEffects", new ArrayList<>());
double originalHealth = ((Number) data.get("originalHealth")).doubleValue();
float originalSaturation = ((Number) data.get("originalSaturation")).floatValue();
return new DropperPlayerEntryState(playerId, makePlayerInvisible, entryLocation, originalIsFlying, return new DropperPlayerEntryState(playerId, entryLocation, originalIsFlying,
originalGameMode, originalAllowFlight, originalInvulnerable, originalIsSwimming, originalCollideAble, originalGameMode, originalAllowFlight, originalInvulnerable, originalIsSwimming,
originalFlySpeed, disableHitCollision, horizontalVelocity, arenaGameMode); originalFlySpeed, horizontalVelocity, arenaGameMode, originalCollideAble, originalPotionEffect,
originalHealth, originalSaturation);
} }
} }

View File

@ -1,15 +1,20 @@
package net.knarcraft.minigames.arena.parkour; package net.knarcraft.minigames.arena.parkour;
import net.knarcraft.knarlib.util.MaterialHelper;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.Arena; import net.knarcraft.minigames.arena.Arena;
import net.knarcraft.minigames.arena.ArenaGameMode; import net.knarcraft.minigames.arena.ArenaGameMode;
import net.knarcraft.minigames.arena.ArenaRecordsRegistry; import net.knarcraft.minigames.arena.ArenaRecordsRegistry;
import net.knarcraft.minigames.util.MaterialHelper; import net.knarcraft.minigames.arena.reward.Reward;
import net.knarcraft.minigames.arena.reward.RewardCondition;
import net.knarcraft.minigames.util.InputValidationHelper;
import net.knarcraft.minigames.util.ParkourArenaStorageHelper; import net.knarcraft.minigames.util.ParkourArenaStorageHelper;
import net.knarcraft.minigames.util.StringSanitizer; import net.knarcraft.minigames.util.StringSanitizer;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.entity.EntityDamageEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -21,6 +26,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level;
import static net.knarcraft.minigames.util.InputValidationHelper.isInvalid; import static net.knarcraft.minigames.util.InputValidationHelper.isInvalid;
@ -70,6 +76,31 @@ public class ParkourArena implements Arena {
*/ */
private @Nullable Set<Material> killPlaneBlocks; private @Nullable Set<Material> killPlaneBlocks;
/**
* The names of the block types serving as obstacles for this arena
*/
private @Nullable Set<String> obstacleBlockNames;
/**
* The block types serving as obstacles for this arena
*/
private @Nullable Set<Material> obstacleBlocks;
/**
* The maximum amount of players able to join this arena at any time
*/
private int maxPlayers;
/**
* Types of damage that won't be blocked in this arena
*/
private Set<EntityDamageEvent.DamageCause> allowedDamageCauses;
/**
* Types of damage that will trigger a loss in this arena
*/
private Set<EntityDamageEvent.DamageCause> lossTriggerDamageCauses;
/** /**
* The checkpoints for this arena. Entering a checkpoint overrides the player's spawn location. * The checkpoints for this arena. Entering a checkpoint overrides the player's spawn location.
*/ */
@ -82,6 +113,8 @@ public class ParkourArena implements Arena {
private final @NotNull ParkourArenaHandler parkourArenaHandler; private final @NotNull ParkourArenaHandler parkourArenaHandler;
private Map<RewardCondition, Set<Reward>> rewards = new HashMap<>();
/** /**
* Instantiates a new parkour arena * Instantiates a new parkour arena
* *
@ -91,13 +124,24 @@ public class ParkourArena implements Arena {
* @param exitLocation <p>The location the players are teleported to when exiting the arena, or null</p> * @param exitLocation <p>The location the players are teleported to when exiting the arena, or null</p>
* @param winBlockType <p>The material of the block players have to hit to win this parkour arena</p> * @param winBlockType <p>The material of the block players have to hit to win this parkour arena</p>
* @param winLocation <p>The location a player has to reach to win this arena</p> * @param winLocation <p>The location a player has to reach to win this arena</p>
* @param killPlaneBlockNames <p>The names of the types of blocks that trigger a loss when stepped on</p>
* @param obstacleBlockNames <p>The names of the types of blocks that trigger a loss when touched</p>
* @param checkpoints <p>The checkpoints set for this arena</p>
* @param maxPlayers <p>The maximum amount of players able to join this arena at once</p>
* @param rewards <p>The rewards given by this arena</p>
* @param parkourArenaData <p>The arena data keeping track of which players have done what in this arena</p> * @param parkourArenaData <p>The arena data keeping track of which players have done what in this arena</p>
* @param arenaHandler <p>The arena handler used for saving any changes</p> * @param arenaHandler <p>The arena handler used for saving any changes</p>
* @param allowedDamageCauses <p>The damage causes to not cancel. If the player received fatal damage, a loss is triggered.</p>
* @param lossTriggerDamageCauses <p>The damage causes that will trigger a loss (for arrow traps and similar)</p>
*/ */
public ParkourArena(@NotNull UUID arenaId, @NotNull String arenaName, @NotNull Location spawnLocation, public ParkourArena(@NotNull UUID arenaId, @NotNull String arenaName, @NotNull Location spawnLocation,
@Nullable Location exitLocation, @NotNull Material winBlockType, @Nullable Location winLocation, @Nullable Location exitLocation, @NotNull Material winBlockType, @Nullable Location winLocation,
@Nullable Set<String> killPlaneBlockNames, @NotNull List<Location> checkpoints, @Nullable Set<String> killPlaneBlockNames, @Nullable Set<String> obstacleBlockNames,
@NotNull ParkourArenaData parkourArenaData, @NotNull ParkourArenaHandler arenaHandler) { @NotNull List<Location> checkpoints, int maxPlayers,
@NotNull Map<RewardCondition, Set<Reward>> rewards,
@NotNull ParkourArenaData parkourArenaData, @NotNull ParkourArenaHandler arenaHandler,
@Nullable Set<String> allowedDamageCauses,
@Nullable Set<String> lossTriggerDamageCauses) {
this.arenaId = arenaId; this.arenaId = arenaId;
this.arenaName = arenaName; this.arenaName = arenaName;
this.spawnLocation = spawnLocation; this.spawnLocation = spawnLocation;
@ -106,10 +150,17 @@ public class ParkourArena implements Arena {
this.winLocation = winLocation; this.winLocation = winLocation;
this.killPlaneBlockNames = killPlaneBlockNames; this.killPlaneBlockNames = killPlaneBlockNames;
this.killPlaneBlocks = this.killPlaneBlockNames == null ? null : MaterialHelper.loadMaterialList( this.killPlaneBlocks = this.killPlaneBlockNames == null ? null : MaterialHelper.loadMaterialList(
new ArrayList<>(killPlaneBlockNames)); new ArrayList<>(killPlaneBlockNames), "+", MiniGames.getInstance().getLogger());
this.obstacleBlockNames = obstacleBlockNames;
this.obstacleBlocks = this.obstacleBlockNames == null ? null : MaterialHelper.loadMaterialList(
new ArrayList<>(obstacleBlockNames), "+", MiniGames.getInstance().getLogger());
this.checkpoints = checkpoints; this.checkpoints = checkpoints;
this.parkourArenaData = parkourArenaData; this.parkourArenaData = parkourArenaData;
this.parkourArenaHandler = arenaHandler; this.parkourArenaHandler = arenaHandler;
this.rewards = rewards;
this.maxPlayers = maxPlayers;
this.allowedDamageCauses = InputValidationHelper.parseDamageCauses(allowedDamageCauses);
this.lossTriggerDamageCauses = InputValidationHelper.parseDamageCauses(lossTriggerDamageCauses);
} }
/** /**
@ -138,8 +189,12 @@ public class ParkourArena implements Arena {
this.parkourArenaData = new ParkourArenaData(this.arenaId, recordRegistries, new HashMap<>()); this.parkourArenaData = new ParkourArenaData(this.arenaId, recordRegistries, new HashMap<>());
this.winBlockType = Material.EMERALD_BLOCK; this.winBlockType = Material.EMERALD_BLOCK;
this.killPlaneBlocks = null; this.killPlaneBlocks = null;
this.obstacleBlocks = null;
this.checkpoints = new ArrayList<>(); this.checkpoints = new ArrayList<>();
this.parkourArenaHandler = arenaHandler; this.parkourArenaHandler = arenaHandler;
this.maxPlayers = -1;
this.allowedDamageCauses = new HashSet<>();
this.lossTriggerDamageCauses = new HashSet<>();
} }
@Override @Override
@ -167,12 +222,71 @@ public class ParkourArena implements Arena {
return this.exitLocation; return this.exitLocation;
} }
/** @Override
* Gets the type of block a player has to hit to win this arena public void addReward(@NotNull RewardCondition rewardCondition, @NotNull Reward reward) {
* this.rewards.computeIfAbsent(rewardCondition, k -> new HashSet<>());
* @return <p>The kind of block players must hit</p> this.rewards.get(rewardCondition).add(reward);
*/ this.saveArena();
public @NotNull Material getWinBlockType() { }
@Override
public void clearRewards(@NotNull RewardCondition rewardCondition) {
this.rewards.remove(rewardCondition);
this.saveArena();
}
@Override
public @NotNull Set<Reward> getRewards(RewardCondition rewardCondition) {
if (this.rewards.containsKey(rewardCondition)) {
return this.rewards.get(rewardCondition);
} else {
return new HashSet<>();
}
}
@Override
public int getMaxPlayers() {
return this.maxPlayers;
}
@Override
public boolean setMaxPlayers(int newValue) {
if (newValue < -1) {
return false;
}
this.maxPlayers = newValue;
this.saveArena();
return true;
}
@Override
public @NotNull Set<EntityDamageEvent.DamageCause> getAllowedDamageCauses() {
return this.allowedDamageCauses;
}
@Override
public @NotNull Set<EntityDamageEvent.DamageCause> getLossTriggerDamageCauses() {
return this.lossTriggerDamageCauses;
}
@Override
public boolean setAllowedDamageCauses(@NotNull Set<EntityDamageEvent.DamageCause> causes) {
this.allowedDamageCauses = causes;
this.saveArena();
return true;
}
@Override
public boolean setLossTriggerDamageCauses(@NotNull Set<EntityDamageEvent.DamageCause> causes) {
this.lossTriggerDamageCauses = causes;
this.saveArena();
return true;
}
@Override
@NotNull
public Material getWinBlockType() {
return this.winBlockType; return this.winBlockType;
} }
@ -209,6 +323,28 @@ public class ParkourArena implements Arena {
return this.killPlaneBlockNames; return this.killPlaneBlockNames;
} }
/**
* Gets the block types used for this parkour arena's obstacle blocks
*
* @return <p>The types of blocks used as obstacles</p>
*/
public @NotNull Set<Material> getObstacleBlocks() {
if (this.obstacleBlocks != null) {
return new HashSet<>(this.obstacleBlocks);
} else {
return MiniGames.getInstance().getParkourConfiguration().getObstacleBlocks();
}
}
/**
* Gets the names of the blocks used as this arena's obstacle blocks
*
* @return <p>The names of the blocks used as this arena's obstacle blocks</p>
*/
public @Nullable Set<String> getObstacleBlockNames() {
return this.obstacleBlockNames;
}
/** /**
* Gets all checkpoint locations for this arena * Gets all checkpoint locations for this arena
* *
@ -223,11 +359,17 @@ public class ParkourArena implements Arena {
} }
/** /**
* Gets this arena's sanitized name * Gets whether this arena has no checkpoints
* *
* @return <p>This arena's sanitized name</p> * @return <p>True if this arena has no checkpoints</p>
*/ */
public @NotNull String getArenaNameSanitized() { public boolean hasNoCheckpoints() {
return this.checkpoints.isEmpty();
}
@Override
@NotNull
public String getArenaNameSanitized() {
return StringSanitizer.sanitizeArenaName(this.getArenaName()); return StringSanitizer.sanitizeArenaName(this.getArenaName());
} }
@ -241,20 +383,27 @@ public class ParkourArena implements Arena {
try { try {
ParkourArenaStorageHelper.saveParkourArenaData(getData()); ParkourArenaStorageHelper.saveParkourArenaData(getData());
return true; return true;
} catch (IOException e) { } catch (IOException exception) {
return false; return false;
} }
} }
@Override @Override
public boolean willCauseWin(Block block) { public boolean willCauseWin(@NotNull Block block) {
return (this.winLocation != null && this.winLocation.getBlock().equals(block)) || if (this.winLocation != null) {
(this.winLocation == null && this.winBlockType == block.getType()); return this.winLocation.getBlock().equals(block);
} else {
if (this.winBlockType.isSolid()) {
return this.winBlockType == block.getRelative(BlockFace.DOWN).getType();
} else {
return this.winBlockType == block.getType();
}
}
} }
@Override @Override
public boolean willCauseLoss(Block block) { public boolean willCauseLoss(@NotNull Block block) {
return this.getKillPlaneBlocks().contains(block.getType()); return this.getKillPlaneBlocks().contains(block.getType()) || this.getObstacleBlocks().contains(block.getType());
} }
@Override @Override
@ -263,71 +412,49 @@ public class ParkourArena implements Arena {
this.winBlockType.isSolid(); this.winBlockType.isSolid();
} }
/** @Override
* Sets the spawn location for this arena public boolean setSpawnLocation(@Nullable Location newLocation) {
*
* @param newLocation <p>The new spawn location</p>
* @return <p>True if successfully updated</p>
*/
public boolean setSpawnLocation(@NotNull Location newLocation) {
if (isInvalid(newLocation)) { if (isInvalid(newLocation)) {
return false; return false;
} else { } else {
this.spawnLocation = newLocation; this.spawnLocation = newLocation;
this.parkourArenaHandler.saveArenas(); this.saveArena();
return true; return true;
} }
} }
/** @Override
* Sets the exit location for this arena public boolean setExitLocation(@Nullable Location newLocation) {
*
* @param newLocation <p>The new exit location</p>
* @return <p>True if successfully updated</p>
*/
public boolean setExitLocation(@NotNull Location newLocation) {
if (isInvalid(newLocation)) { if (isInvalid(newLocation)) {
return false; return false;
} else { } else {
this.exitLocation = newLocation; this.exitLocation = newLocation;
this.parkourArenaHandler.saveArenas(); this.saveArena();
return true; return true;
} }
} }
/** @Override
* Sets the name of this arena
*
* @param arenaName <p>The new name</p>
* @return <p>True if successfully updated</p>
*/
public boolean setName(@NotNull String arenaName) { public boolean setName(@NotNull String arenaName) {
if (!arenaName.isBlank()) { if (!arenaName.isBlank()) {
String oldName = this.getArenaNameSanitized(); String oldName = this.getArenaNameSanitized();
this.arenaName = arenaName; this.arenaName = arenaName;
// Update the arena lookup map to make sure the new name can be used immediately // Update the arena lookup map to make sure the new name can be used immediately
this.parkourArenaHandler.updateLookupName(oldName, this.getArenaNameSanitized()); this.parkourArenaHandler.updateLookupName(oldName, this.getArenaNameSanitized());
this.parkourArenaHandler.saveArenas(); this.saveArena();
return true; return true;
} else { } else {
return false; return false;
} }
} }
/** @Override
* Sets the material of the win block type
*
* <p>The win block type is the type of block a player must hit to win in this arena</p>
*
* @param material <p>The material to set for the win block type</p>
* @return <p>True if successfully updated</p>
*/
public boolean setWinBlockType(@NotNull Material material) { public boolean setWinBlockType(@NotNull Material material) {
if (material.isAir() || !material.isBlock()) { if (material.isAir() || !material.isBlock()) {
return false; return false;
} else { } else {
this.winBlockType = material; this.winBlockType = material;
this.parkourArenaHandler.saveArenas(); this.saveArena();
return true; return true;
} }
} }
@ -338,13 +465,18 @@ public class ParkourArena implements Arena {
* @param newLocation <p>The location players have to reach</p> * @param newLocation <p>The location players have to reach</p>
* @return <p>True if successfully changed</p> * @return <p>True if successfully changed</p>
*/ */
public boolean setWinLocation(@NotNull Location newLocation) { public boolean setWinLocation(@Nullable Location newLocation) {
if (isInvalid(newLocation)) { if (newLocation == null) {
return false; // Un-set the win location, and fall back to the win block type
} else { this.winLocation = null;
this.winLocation = newLocation.clone(); this.saveArena();
this.parkourArenaHandler.saveArenas();
return true; return true;
} else if (!isInvalid(newLocation)) {
this.winLocation = newLocation.clone();
this.saveArena();
return true;
} else {
return false;
} }
} }
@ -353,17 +485,42 @@ public class ParkourArena implements Arena {
* *
* @param killPlaneBlockNames <p>The names of the blocks that will cause players to lose</p> * @param killPlaneBlockNames <p>The names of the blocks that will cause players to lose</p>
*/ */
public boolean setKillPlaneBlocks(@NotNull Set<String> killPlaneBlockNames) { public boolean setKillPlaneBlocks(@Nullable Set<String> killPlaneBlockNames) {
if (killPlaneBlockNames.isEmpty()) { if (killPlaneBlockNames == null || killPlaneBlockNames.isEmpty()) {
this.killPlaneBlockNames = null;
this.killPlaneBlocks = null; this.killPlaneBlocks = null;
} else { } else {
Set<Material> parsed = MaterialHelper.loadMaterialList(new ArrayList<>(killPlaneBlockNames)); Set<Material> parsed = MaterialHelper.loadMaterialList(new ArrayList<>(killPlaneBlockNames), "+",
MiniGames.getInstance().getLogger());
if (parsed.isEmpty()) { if (parsed.isEmpty()) {
return false; return false;
} }
this.killPlaneBlockNames = killPlaneBlockNames;
this.killPlaneBlocks = parsed; this.killPlaneBlocks = parsed;
} }
this.parkourArenaHandler.saveArenas(); this.saveArena();
return true;
}
/**
* Sets the type of blocks used as obstacle blocks
*
* @param obstacleBlockNames <p>The names of the obstacle blocks</p>
*/
public boolean setObstacleBlocks(@Nullable Set<String> obstacleBlockNames) {
if (obstacleBlockNames == null || obstacleBlockNames.isEmpty()) {
this.obstacleBlockNames = null;
this.obstacleBlocks = null;
} else {
Set<Material> parsed = MaterialHelper.loadMaterialList(new ArrayList<>(obstacleBlockNames), "+",
MiniGames.getInstance().getLogger());
if (parsed.isEmpty()) {
return false;
}
this.obstacleBlockNames = obstacleBlockNames;
this.obstacleBlocks = parsed;
}
this.saveArena();
return true; return true;
} }
@ -373,13 +530,13 @@ public class ParkourArena implements Arena {
* @param checkpoint <p>The checkpoint to add</p> * @param checkpoint <p>The checkpoint to add</p>
* @return <p>True if successfully added</p> * @return <p>True if successfully added</p>
*/ */
public boolean addCheckpoint(@NotNull Location checkpoint) { public boolean addCheckpoint(@Nullable Location checkpoint) {
if (isInvalid(checkpoint)) { if (isInvalid(checkpoint)) {
return false; return false;
} }
this.checkpoints.add(checkpoint.clone()); this.checkpoints.add(checkpoint.clone());
this.parkourArenaHandler.saveArenas(); this.saveArena();
return true; return true;
} }
@ -394,10 +551,23 @@ public class ParkourArena implements Arena {
} }
this.checkpoints.clear(); this.checkpoints.clear();
this.parkourArenaHandler.saveArenas(); this.saveArena();
return true; return true;
} }
/**
* Saves this arena to disk
*/
public void saveArena() {
try {
ParkourArenaStorageHelper.saveSingleParkourArena(this);
} catch (IOException exception) {
MiniGames.log(Level.SEVERE, "Unable to save arena! " +
"Data loss can occur!");
MiniGames.log(Level.SEVERE, exception.getMessage());
}
}
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (!(other instanceof ParkourArena otherArena)) { if (!(other instanceof ParkourArena otherArena)) {

View File

@ -1,6 +1,7 @@
package net.knarcraft.minigames.arena.parkour; package net.knarcraft.minigames.arena.parkour;
import net.knarcraft.minigames.arena.EditablePropertyType; import net.knarcraft.minigames.arena.EditablePropertyType;
import net.knarcraft.minigames.util.ArenaStorageHelper;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -62,6 +63,32 @@ public enum ParkourArenaEditableProperty {
*/ */
KILL_PLANE_BLOCKS("killPlaneBlocks", (arena) -> String.valueOf(arena.getKillPlaneBlockNames()), KILL_PLANE_BLOCKS("killPlaneBlocks", (arena) -> String.valueOf(arena.getKillPlaneBlockNames()),
EditablePropertyType.MATERIAL_LIST), EditablePropertyType.MATERIAL_LIST),
/**
* The blocks used as this arena's obstacle blocks
*/
OBSTACLE_BLOCKS("obstacleBlocks", (arena) -> String.valueOf(arena.getObstacleBlockNames()),
EditablePropertyType.MATERIAL_LIST),
/**
* The arena's max players
*/
MAX_PLAYERS("maxPlayers", (arena) -> String.valueOf(arena.getMaxPlayers()),
EditablePropertyType.INTEGER),
/**
* The arena's allowed damage causes
*/
ALLOWED_DAMAGE_CAUSES("allowedDamageCauses", (arena) -> String.valueOf(
ArenaStorageHelper.getDamageCauseNames(arena.getAllowedDamageCauses())),
EditablePropertyType.DAMAGE_CAUSE_LIST),
/**
* The arena's loss trigger damage causes
*/
LOSS_TRIGGER_DAMAGE_CAUSES("lossTriggerDamageCauses", (arena) -> String.valueOf(
ArenaStorageHelper.getDamageCauseNames(arena.getLossTriggerDamageCauses())),
EditablePropertyType.DAMAGE_CAUSE_LIST),
; ;
private final @NotNull String argumentString; private final @NotNull String argumentString;

View File

@ -16,6 +16,11 @@ public enum ParkourArenaGameMode implements ConfigurationSerializable, ArenaGame
* The default game-mode. Failing once throws the player out. * The default game-mode. Failing once throws the player out.
*/ */
DEFAULT, DEFAULT,
/**
* A hardcore game mode where no checkpoints are allowed
*/
HARDCORE,
; ;
/** /**
@ -24,7 +29,7 @@ public enum ParkourArenaGameMode implements ConfigurationSerializable, ArenaGame
* @param gameMode <p>The game-mode string to match</p> * @param gameMode <p>The game-mode string to match</p>
* @return <p>The specified arena game-mode</p> * @return <p>The specified arena game-mode</p>
*/ */
public static @NotNull ParkourArenaGameMode matchGamemode(@NotNull String gameMode) { public static @NotNull ParkourArenaGameMode matchGameMode(@NotNull String gameMode) {
try { try {
return ParkourArenaGameMode.valueOf(gameMode.toUpperCase()); return ParkourArenaGameMode.valueOf(gameMode.toUpperCase());
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {

View File

@ -3,7 +3,7 @@ package net.knarcraft.minigames.arena.parkour;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaHandler; import net.knarcraft.minigames.arena.ArenaHandler;
import net.knarcraft.minigames.arena.ArenaPlayerRegistry; import net.knarcraft.minigames.arena.ArenaPlayerRegistry;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.util.ParkourArenaStorageHelper; import net.knarcraft.minigames.util.ParkourArenaStorageHelper;
import java.io.IOException; import java.io.IOException;
@ -32,9 +32,10 @@ public class ParkourArenaHandler extends ArenaHandler<ParkourArena, ParkourArena
public void saveGroups() { public void saveGroups() {
try { try {
ParkourArenaStorageHelper.saveParkourArenaGroups(new HashSet<>(this.arenaGroups.values())); ParkourArenaStorageHelper.saveParkourArenaGroups(new HashSet<>(this.arenaGroups.values()));
} catch (IOException e) { } catch (IOException exception) {
MiniGames.log(Level.SEVERE, Message.ERROR_CANNOT_SAVE_ARENA_GROUPS.getMessage()); MiniGames.log(Level.SEVERE, MiniGames.getInstance().getTranslator().getTranslatedMessage(
MiniGames.log(Level.SEVERE, e.getMessage()); MiniGameMessage.ERROR_CANNOT_SAVE_ARENA_GROUPS));
MiniGames.log(Level.SEVERE, exception.getMessage());
} }
} }
@ -54,10 +55,10 @@ public class ParkourArenaHandler extends ArenaHandler<ParkourArena, ParkourArena
public void saveArenas() { public void saveArenas() {
try { try {
ParkourArenaStorageHelper.saveParkourArenas(this.arenas); ParkourArenaStorageHelper.saveParkourArenas(this.arenas);
} catch (IOException e) { } catch (IOException exception) {
MiniGames.log(Level.SEVERE, "Unable to save current arenas! " + MiniGames.log(Level.SEVERE, "Unable to save current arenas! " +
"Data loss can occur!"); "Data loss can occur!");
MiniGames.log(Level.SEVERE, e.getMessage()); MiniGames.log(Level.SEVERE, exception.getMessage());
} }
} }

View File

@ -1,16 +1,31 @@
package net.knarcraft.minigames.arena.parkour; package net.knarcraft.minigames.arena.parkour;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.AbstractArenaSession; import net.knarcraft.minigames.arena.AbstractArenaSession;
import net.knarcraft.minigames.arena.Arena;
import net.knarcraft.minigames.arena.PlayerEntryState; import net.knarcraft.minigames.arena.PlayerEntryState;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.arena.reward.RewardCondition;
import net.knarcraft.minigames.config.ParkourConfiguration; import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.gui.ArenaGUI;
import net.knarcraft.minigames.gui.ParkourGUI;
import net.knarcraft.minigames.gui.ParkourGUIBedrock;
import net.knarcraft.minigames.util.GeyserHelper;
import net.knarcraft.minigames.util.PlayerTeleporter; import net.knarcraft.minigames.util.PlayerTeleporter;
import net.knarcraft.minigames.util.RewardHelper;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Powerable;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
/** /**
@ -18,10 +33,11 @@ import java.util.logging.Level;
*/ */
public class ParkourArenaSession extends AbstractArenaSession { public class ParkourArenaSession extends AbstractArenaSession {
private static final @NotNull Map<Arena, Set<Block>> changedLevers = new HashMap<>();
private final @NotNull ParkourArena arena; private final @NotNull ParkourArena arena;
private final @NotNull Player player; private final @NotNull Player player;
private final @NotNull ParkourArenaGameMode gameMode; private final @NotNull ParkourArenaGameMode gameMode;
private Location reachedCheckpoint = null; private @Nullable Location reachedCheckpoint = null;
/** /**
* Instantiates a new parkour arena session * Instantiates a new parkour arena session
@ -37,12 +53,19 @@ public class ParkourArenaSession extends AbstractArenaSession {
this.player = player; this.player = player;
this.gameMode = gameMode; this.gameMode = gameMode;
ParkourConfiguration configuration = MiniGames.getInstance().getParkourConfiguration(); this.entryState = new ParkourPlayerEntryState(player);
boolean makeInvisible = configuration.makePlayersInvisible();
this.entryState = new ParkourPlayerEntryState(player, makeInvisible);
this.entryState.setArenaState(); this.entryState.setArenaState();
} }
/**
* Gets the game-mode the player is playing in this session
*
* @return <p>The game-mode for this session</p>
*/
public @NotNull ParkourArenaGameMode getGameMode() {
return this.gameMode;
}
/** /**
* Registers the checkpoint this session's player has reached * Registers the checkpoint this session's player has reached
* *
@ -52,6 +75,16 @@ public class ParkourArenaSession extends AbstractArenaSession {
this.reachedCheckpoint = location; this.reachedCheckpoint = location;
} }
/**
* Registers a lever change
*
* @param block <p>The block of the lever</p>
*/
public void registerChangedLever(@NotNull Block block) {
changedLevers.putIfAbsent(this.arena, new HashSet<>());
changedLevers.get(this.arena).add(block);
}
/** /**
* Gets the checkpoint currently registered as the player's spawn location * Gets the checkpoint currently registered as the player's spawn location
* *
@ -79,14 +112,22 @@ public class ParkourArenaSession extends AbstractArenaSession {
registerRecord(); registerRecord();
} }
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
// Mark the arena as cleared // Mark the arena as cleared
if (this.arena.getData().setCompleted(this.gameMode, this.player)) { if (this.arena.getData().setCompleted(this.gameMode, this.player)) {
this.player.sendMessage(Message.SUCCESS_ARENA_FIRST_CLEAR.getMessage()); stringFormatter.displaySuccessMessage(this.player, MiniGameMessage.SUCCESS_ARENA_FIRST_CLEAR);
RewardHelper.grantRewards(this.player, this.arena.getRewards(RewardCondition.FIRST_WIN));
} }
this.player.sendMessage(Message.SUCCESS_ARENA_WIN.getMessage()); stringFormatter.displaySuccessMessage(this.player, MiniGameMessage.SUCCESS_ARENA_WIN);
RewardHelper.grantRewards(this.player, this.arena.getRewards(RewardCondition.WIN));
// Teleport the player out of the arena // Teleport the player out of the arena
teleportToExit(false); teleportToExit(false);
if (MiniGames.getInstance().getParkourArenaPlayerRegistry().getPlayingPlayers(this.arena).isEmpty()) {
resetLevers();
}
} }
@Override @Override
@ -95,6 +136,7 @@ public class ParkourArenaSession extends AbstractArenaSession {
//Teleport the player back to the top //Teleport the player back to the top
Location spawnLocation = this.reachedCheckpoint != null ? this.reachedCheckpoint : this.arena.getSpawnLocation(); Location spawnLocation = this.reachedCheckpoint != null ? this.reachedCheckpoint : this.arena.getSpawnLocation();
PlayerTeleporter.teleportPlayer(this.player, spawnLocation, true, false); PlayerTeleporter.teleportPlayer(this.player, spawnLocation, true, false);
this.player.playSound(this.player, Sound.ENTITY_CHICKEN_EGG, 5f, 0.5f);
this.entryState.setArenaState(); this.entryState.setArenaState();
} }
@ -103,14 +145,32 @@ public class ParkourArenaSession extends AbstractArenaSession {
return this.arena; return this.arena;
} }
@Override
public @NotNull ArenaGUI getGUI() {
if (GeyserHelper.isGeyserPlayer(this.player)) {
return new ParkourGUIBedrock(this.player);
} else {
return new ParkourGUI(this.player);
}
}
@Override
public void reset() {
this.reachedCheckpoint = null;
if (MiniGames.getInstance().getParkourArenaPlayerRegistry().getPlayingPlayers(this.arena).size() == 1) {
resetLevers();
}
super.reset();
}
@Override @Override
protected void removeSession() { protected void removeSession() {
// Remove this session for game sessions to stop listeners from fiddling more with the player // Remove this session for game sessions to stop listeners from fiddling more with the player
boolean removedSession = MiniGames.getInstance().getParkourArenaPlayerRegistry().removePlayer( boolean removedSession = MiniGames.getInstance().getParkourArenaPlayerRegistry().removePlayer(
player.getUniqueId(), true); player.getUniqueId(), true);
if (!removedSession) { if (!removedSession) {
MiniGames.log(Level.SEVERE, "Unable to remove parkour arena session for " + player.getName() + ". " + MiniGames.log(Level.SEVERE, "Unable to remove parkour arena session for " + this.player.getName() +
"This will have unintended consequences."); ". This will have unintended consequences.");
} }
} }
@ -118,7 +178,38 @@ public class ParkourArenaSession extends AbstractArenaSession {
protected String getGameModeString() { protected String getGameModeString() {
return switch (this.gameMode) { return switch (this.gameMode) {
case DEFAULT -> "default"; case DEFAULT -> "default";
case HARDCORE -> "hardcore";
}; };
} }
@Override
public void triggerQuit(boolean immediately, boolean removeSession) {
super.triggerQuit(immediately, removeSession);
// Note: As immediately is only used when stopping the server, levers should be reset regardless of current players
if (MiniGames.getInstance().getParkourArenaPlayerRegistry().getPlayingPlayers(this.arena).isEmpty() || immediately) {
resetLevers();
}
}
/**
* Resets all levers if the arena is empty
*/
private void resetLevers() {
// Make a copy in case new player join while the levers are resetting
Set<Block> changed = changedLevers.remove(this.arena);
if (changed == null || changed.isEmpty()) {
return;
}
// Reset all levers toggled by players in the arena
for (Block block : changed) {
BlockData blockData = block.getBlockData();
if (blockData instanceof Powerable powerable) {
powerable.setPowered(false);
block.setBlockData(blockData);
}
}
}
} }

View File

@ -1,11 +1,12 @@
package net.knarcraft.minigames.arena.parkour; package net.knarcraft.minigames.arena.parkour;
import net.knarcraft.minigames.arena.StorageKey;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* A representation of each key used for storing arena data * A representation of each key used for storing arena data
*/ */
public enum ParkourArenaStorageKey { public enum ParkourArenaStorageKey implements StorageKey {
/** /**
* The key for an arena's id * The key for an arena's id
@ -42,6 +43,11 @@ public enum ParkourArenaStorageKey {
*/ */
KILL_PLANE_BLOCKS("killPlaneBlocks"), KILL_PLANE_BLOCKS("killPlaneBlocks"),
/**
* The key for this arena's obstacle blocks (overrides the config)
*/
OBSTACLE_BLOCKS("obstacleBlocks"),
/** /**
* The key for this arena's checkpoint locations * The key for this arena's checkpoint locations
*/ */
@ -51,6 +57,26 @@ public enum ParkourArenaStorageKey {
* The hey for this arena's data * The hey for this arena's data
*/ */
DATA("arenaData"), DATA("arenaData"),
/**
* The key for this arena's rewards
*/
REWARDS("rewards"),
/**
* The key for this arena's maximum players
*/
MAX_PLAYERS("maxPlayers"),
/**
* The key for this arena's allowed damage causes
*/
ALLOWED_DAMAGE_CAUSES("allowedDamageCauses"),
/**
* The key for this arena's loss trigger damage causes
*/
LOSS_TRIGGER_DAMAGE_CAUSES("lossTriggerDamageCauses"),
; ;
private final @NotNull String key; private final @NotNull String key;
@ -64,11 +90,7 @@ public enum ParkourArenaStorageKey {
this.key = key; this.key = key;
} }
/** @Override
* Gets the configuration key this enum represents
*
* @return <p>The string key representation.</p>
*/
public @NotNull String getKey() { public @NotNull String getKey() {
return this.key; return this.key;
} }

View File

@ -5,8 +5,11 @@ import net.knarcraft.minigames.container.SerializableUUID;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -20,15 +23,14 @@ public class ParkourPlayerEntryState extends AbstractPlayerEntryState {
* *
* @param player <p>The player whose state should be stored</p> * @param player <p>The player whose state should be stored</p>
*/ */
public ParkourPlayerEntryState(@NotNull Player player, boolean makePlayerInvisible) { public ParkourPlayerEntryState(@NotNull Player player) {
super(player, makePlayerInvisible); super(player);
} }
/** /**
* Instantiates a new parkour player entry state * Instantiates a new parkour player entry state
* *
* @param playerId <p>The id of the player whose state this should keep track of</p> * @param playerId <p>The id of the player whose state this should keep track of</p>
* @param makePlayerInvisible <p>Whether players should be made invisible while in the arena</p>
* @param entryLocation <p>The location the player entered from</p> * @param entryLocation <p>The location the player entered from</p>
* @param originalIsFlying <p>Whether the player was flying before entering the arena</p> * @param originalIsFlying <p>Whether the player was flying before entering the arena</p>
* @param originalGameMode <p>The game-mode of the player before entering the arena</p> * @param originalGameMode <p>The game-mode of the player before entering the arena</p>
@ -36,18 +38,22 @@ public class ParkourPlayerEntryState extends AbstractPlayerEntryState {
* @param originalInvulnerable <p>Whether the player was invulnerable before entering the arena</p> * @param originalInvulnerable <p>Whether the player was invulnerable before entering the arena</p>
* @param originalIsSwimming <p>Whether the player was swimming before entering the arena</p> * @param originalIsSwimming <p>Whether the player was swimming before entering the arena</p>
* @param originalCollideAble <p>Whether the player was collide-able before entering the arena</p> * @param originalCollideAble <p>Whether the player was collide-able before entering the arena</p>
* @param originalPotionEffects <p>The potion effects applied to the player when joining</p>
* @param originalHealth <p>The health of the player when joining the arena</p>
* @param originalSaturation <p>The saturation of the player when joining the arena</p>
*/ */
public ParkourPlayerEntryState(@NotNull UUID playerId, boolean makePlayerInvisible, Location entryLocation, public ParkourPlayerEntryState(@NotNull UUID playerId, Location entryLocation,
boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight, boolean originalIsFlying, GameMode originalGameMode, boolean originalAllowFlight,
boolean originalInvulnerable, boolean originalIsSwimming, boolean originalInvulnerable, boolean originalIsSwimming,
boolean originalCollideAble) { boolean originalCollideAble, Collection<PotionEffect> originalPotionEffects,
super(playerId, makePlayerInvisible, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight, double originalHealth, float originalSaturation) {
originalInvulnerable, originalIsSwimming, originalCollideAble); super(playerId, entryLocation, originalIsFlying, originalGameMode, originalAllowFlight,
originalInvulnerable, originalIsSwimming, originalCollideAble, originalPotionEffects, originalHealth,
originalSaturation);
} }
@Override @Override
public void setArenaState() { public void setArenaState() {
super.setArenaState();
Player player = getPlayer(); Player player = getPlayer();
if (player == null) { if (player == null) {
return; return;
@ -56,7 +62,6 @@ public class ParkourPlayerEntryState extends AbstractPlayerEntryState {
player.setFlying(false); player.setFlying(false);
player.setGameMode(GameMode.ADVENTURE); player.setGameMode(GameMode.ADVENTURE);
player.setSwimming(false); player.setSwimming(false);
player.setCollidable(false);
} }
/** /**
@ -64,20 +69,24 @@ public class ParkourPlayerEntryState extends AbstractPlayerEntryState {
* *
* @return <p>The data to deserialize</p> * @return <p>The data to deserialize</p>
*/ */
@SuppressWarnings("unused") @SuppressWarnings({"unused", "unchecked"})
public static ParkourPlayerEntryState deserialize(Map<String, Object> data) { public static ParkourPlayerEntryState deserialize(Map<String, Object> data) {
UUID playerId = ((SerializableUUID) data.get("playerId")).getRawValue(); UUID playerId = ((SerializableUUID) data.get("playerId")).getRawValue();
boolean makePlayerInvisible = (boolean) data.get("makePlayerInvisible");
Location entryLocation = (Location) data.get("entryLocation"); Location entryLocation = (Location) data.get("entryLocation");
boolean originalIsFlying = (boolean) data.get("originalIsFlying"); boolean originalIsFlying = getBoolean(data, "originalIsFlying");
GameMode originalGameMode = GameMode.valueOf((String) data.get("originalGameMode")); GameMode originalGameMode = GameMode.valueOf((String) data.get("originalGameMode"));
boolean originalAllowFlight = (boolean) data.get("originalAllowFlight"); boolean originalAllowFlight = getBoolean(data, "originalAllowFlight");
boolean originalInvulnerable = (boolean) data.get("originalInvulnerable"); boolean originalInvulnerable = getBoolean(data, "originalInvulnerable");
boolean originalIsSwimming = (boolean) data.get("originalIsSwimming"); boolean originalIsSwimming = getBoolean(data, "originalIsSwimming");
boolean originalCollideAble = (boolean) data.get("originalCollideAble"); boolean originalCollideAble = getBoolean(data, "originalCollideAble");
Collection<PotionEffect> originalPotionEffect =
(Collection<PotionEffect>) data.getOrDefault("originalPotionEffects", new ArrayList<>());
double originalHealth = ((Number) data.get("originalHealth")).doubleValue();
float originalSaturation = ((Number) data.get("originalSaturation")).floatValue();
return new ParkourPlayerEntryState(playerId, makePlayerInvisible, entryLocation, originalIsFlying, return new ParkourPlayerEntryState(playerId, entryLocation, originalIsFlying, originalGameMode,
originalGameMode, originalAllowFlight, originalInvulnerable, originalIsSwimming, originalCollideAble); originalAllowFlight, originalInvulnerable, originalIsSwimming, originalCollideAble,
originalPotionEffect, originalHealth, originalSaturation);
} }
} }

View File

@ -44,6 +44,13 @@ public abstract class ArenaRecord<K extends Comparable<K>> implements Comparable
return record; return record;
} }
/**
* Gets this as a string that should be printed on a sign
*
* @return <p>This as a string</p>
*/
public abstract String getAsString();
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
return other instanceof ArenaRecord<?> && userId.equals(((ArenaRecord<?>) other).userId); return other instanceof ArenaRecord<?> && userId.equals(((ArenaRecord<?>) other).userId);

View File

@ -19,6 +19,11 @@ public class IntegerRecord extends SummableArenaRecord<Integer> {
super(userId, record); super(userId, record);
} }
@Override
public String getAsString() {
return String.valueOf(this.getRecord());
}
@Override @Override
public SummableArenaRecord<Integer> sum(Integer value) { public SummableArenaRecord<Integer> sum(Integer value) {
return new IntegerRecord(this.getUserId(), this.getRecord() + value); return new IntegerRecord(this.getUserId(), this.getRecord() + value);

View File

@ -7,7 +7,7 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
/** /**
* A record storing a Long * A record storing a Long time
*/ */
public class LongRecord extends SummableArenaRecord<Long> { public class LongRecord extends SummableArenaRecord<Long> {
@ -29,6 +29,22 @@ public class LongRecord extends SummableArenaRecord<Long> {
return new LongRecord(this.getUserId(), this.getRecord() + value); return new LongRecord(this.getUserId(), this.getRecord() + value);
} }
@Override
public String getAsString() {
double seconds = getRecord() / 1000.0;
double minutes = 0;
if (seconds > 60) {
minutes = seconds / 60.0;
seconds = seconds % 60;
}
if (minutes > 0) {
return String.format("%.0fm%.2fs", minutes, seconds);
} else {
return String.format("%.2fs", seconds);
}
}
/** /**
* Deserializes the saved arena record * Deserializes the saved arena record
* *
@ -37,7 +53,8 @@ public class LongRecord extends SummableArenaRecord<Long> {
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
public static LongRecord deserialize(@NotNull Map<String, Object> data) { public static LongRecord deserialize(@NotNull Map<String, Object> data) {
return new LongRecord(((SerializableUUID) data.get("userId")).getRawValue(), ((Number) data.get("record")).longValue()); return new LongRecord(((SerializableUUID) data.get("userId")).getRawValue(),
((Number) data.get("record")).longValue());
} }
} }

View File

@ -0,0 +1,69 @@
package net.knarcraft.minigames.arena.reward;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.config.MiniGameMessage;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
/**
* A reward that executes a specified command when it's granted
*/
public class CommandReward implements Reward {
private final @NotNull String command;
/**
* Instantiates a new command reward
*
* @param command <p>The command to execute when granting this reward</p>
*/
public CommandReward(@NotNull String command) {
this.command = command;
}
@Override
public boolean grant(@NotNull Player player) {
return Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), replaceNamePlaceholder(player, command));
}
@Override
public @NotNull String getGrantMessage() {
return MiniGames.getInstance().getStringFormatter().replacePlaceholder(
MiniGameMessage.SUCCESS_COMMAND_REWARDED, "{command}", command);
}
@NotNull
@Override
public Map<String, Object> serialize() {
Map<String, Object> data = new HashMap<>();
data.put("command", command);
return data;
}
/**
* Replaces the name placeholder in the given input with the given player's name
*
* @param player <p>The player whose name should be used</p>
* @param input <p>The input containing a name placeholder</p>
* @return <p>The input with the placeholder replaced</p>
*/
private String replaceNamePlaceholder(@NotNull Player player, @NotNull String input) {
return input.replaceAll("[<%(\\[{]player[_\\-]?(name)?[>%)\\]}]", player.getName());
}
/**
* Deserializes the command reward defined in the given data
*
* @param data <p>The data to deserialize from</p>
* @return <p>The deserialized data</p>
*/
@SuppressWarnings("unused")
public static CommandReward deserialize(Map<String, Object> data) {
return new CommandReward((String) data.get("command"));
}
}

View File

@ -0,0 +1,65 @@
package net.knarcraft.minigames.arena.reward;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.manager.EconomyManager;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
/**
* A reward that gives an amount of currency when it's granted
*/
public class EconomyReward implements Reward {
private final double amount;
/**
* Instantiates a new economy reward
*
* @param amount <p>The amount of currency granted</p>
*/
public EconomyReward(double amount) {
this.amount = amount;
}
@Override
public boolean grant(@NotNull Player player) {
if (!EconomyManager.isInitialized()) {
MiniGames.log(Level.SEVERE, "An economy reward has been set, but no Vault-compatible economy" +
" plugin has been initialized.");
return false;
}
EconomyManager.deposit(player, amount);
return true;
}
@Override
public @NotNull String getGrantMessage() {
return MiniGames.getInstance().getStringFormatter().replacePlaceholder(MiniGameMessage.SUCCESS_ECONOMY_REWARDED,
"{currency}", EconomyManager.format(amount));
}
@NotNull
@Override
public Map<String, Object> serialize() {
Map<String, Object> data = new HashMap<>();
data.put("amount", amount);
return data;
}
/**
* Deserializes the economy reward defined in the given data
*
* @param data <p>The data to deserialize from</p>
* @return <p>The deserialized data</p>
*/
@SuppressWarnings("unused")
public static EconomyReward deserialize(Map<String, Object> data) {
return new EconomyReward((Double) data.get("amount"));
}
}

View File

@ -0,0 +1,110 @@
package net.knarcraft.minigames.arena.reward;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.config.MiniGameMessage;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
/**
* A reward that gives an item stack when granted
*/
public class ItemReward implements Reward {
private final ItemStack item;
/**
* Instantiates a new item reward
*
* @param item <p>The item rewarded</p>
*/
public ItemReward(@NotNull ItemStack item) {
if (item.getAmount() > item.getMaxStackSize()) {
throw new IllegalArgumentException("Item stack exceeds the maximum stack size");
}
this.item = item;
}
@Override
public boolean grant(@NotNull Player player) {
Inventory inventory = player.getInventory();
if (canFitItem(inventory)) {
inventory.addItem(item);
return true;
} else {
return false;
}
}
@Override
public @NotNull String getGrantMessage() {
NamespacedKey key = item.getType().getKeyOrNull();
String name = key == null ? "Unnamed item" : key.getKey().replace("_", " ");
return MiniGames.getInstance().getStringFormatter().replacePlaceholders(MiniGameMessage.SUCCESS_ITEM_REWARDED,
new String[]{"{amount}", "{item}"}, new String[]{String.valueOf(item.getAmount()), name});
}
@NotNull
@Override
public Map<String, Object> serialize() {
Map<String, Object> data = new HashMap<>();
data.put("item", item);
return data;
}
/**
* Deserializes the item reward defined in the given data
*
* @param data <p>The data to deserialize from</p>
* @return <p>The deserialized data</p>
*/
@SuppressWarnings("unused")
public static ItemReward deserialize(Map<String, Object> data) {
return new ItemReward((ItemStack) data.get("item"));
}
/**
* Checks whether the given inventory is able to fit this item reward
*
* @param inventory <p>The inventory to check</p>
* @return <p>True if the inventory can fit the item</p>
*/
private boolean canFitItem(Inventory inventory) {
// If a slot is available, there is no problem
if (inventory.firstEmpty() != -1) {
return true;
}
// If the inventory doesn't contain the correct type of item, stacking is impossible
if (!inventory.contains(item.getType())) {
return false;
}
// Check if the item stack can fit in the inventory if stacked with existing items
int availableSlots = 0;
for (ItemStack itemStack : inventory.getStorageContents()) {
ItemMeta itemMeta = itemStack.getItemMeta();
ItemMeta targetMeta = item.getItemMeta();
// Skip items of a different type, or with metadata that would prevent stacking
if (itemStack.getType() != item.getType() ||
(itemMeta != null && targetMeta != null && !itemMeta.equals(targetMeta))) {
continue;
}
availableSlots += itemStack.getMaxStackSize() - itemStack.getAmount();
if (availableSlots < item.getAmount()) {
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,85 @@
package net.knarcraft.minigames.arena.reward;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.manager.PermissionManager;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
/**
* A reward that grants a specified permission when granted
*/
public class PermissionReward implements Reward {
private final @NotNull String permission;
private final @Nullable World world;
/**
* Instantiates a new permission reward
*
* @param world <p>The world to grant the permission for, or null</p>
* @param permission <p>The permission to grant</p>
*/
public PermissionReward(@Nullable World world, @NotNull String permission) {
this.world = world;
this.permission = permission;
}
@Override
public boolean grant(@NotNull Player player) {
if (!PermissionManager.isInitialized()) {
MiniGames.log(Level.SEVERE, "A permission reward has been set, but no Vault-compatible permission" +
" plugin has been initialized.");
return false;
}
if (PermissionManager.hasPermission(player, this.permission, this.world != null ? this.world.getName() : null)) {
return false;
} else {
PermissionManager.addPermission(player, this.permission, this.world);
return true;
}
}
@Override
public @NotNull String getGrantMessage() {
if (world == null) {
return MiniGames.getInstance().getStringFormatter().replacePlaceholder(
MiniGameMessage.SUCCESS_PERMISSION_REWARDED, "{permission}", permission);
} else {
return MiniGames.getInstance().getStringFormatter().replacePlaceholders(
MiniGameMessage.SUCCESS_PERMISSION_REWARDED_WORLD, new String[]{"{permission}", "{world}"},
new String[]{permission, world.getName()});
}
}
@NotNull
@Override
public Map<String, Object> serialize() {
Map<String, Object> data = new HashMap<>();
if (world != null) {
data.put("world", world);
}
data.put("permission", permission);
return data;
}
/**
* Deserializes the permission reward defined in the given data
*
* @param data <p>The data to deserialize from</p>
* @return <p>The deserialized data</p>
*/
@SuppressWarnings("unused")
public static PermissionReward deserialize(Map<String, Object> data) {
World world = (World) data.getOrDefault("world", null);
String permission = (String) data.get("permission");
return new PermissionReward(world, permission);
}
}

View File

@ -0,0 +1,28 @@
package net.knarcraft.minigames.arena.reward;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
/**
* A reward a player can be awarded
*/
public interface Reward extends ConfigurationSerializable {
/**
* Grants this reward to the given player
*
* @param player <p>The player this reward should be granted to</p>
* @return <p>True if the item was granted. False if not possible.</p>
*/
boolean grant(@NotNull Player player);
/**
* Gets the message to display to a user when granting this reward
*
* @return <p>The message to display when this reward is granted</p>
*/
@NotNull
String getGrantMessage();
}

View File

@ -0,0 +1,83 @@
package net.knarcraft.minigames.arena.reward;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* The condition for granting a reward
*/
public enum RewardCondition implements ConfigurationSerializable {
/**
* The reward is granted each time the player wins/clears the arena
*/
WIN,
/**
* The reward is granted the first time the player wins/clears the arena
*/
FIRST_WIN,
/**
* The reward is granted if the player beats their personal least deaths record
*/
PERSONAL_DEATH_RECORD,
/**
* The reward is granted if the player beats their personal least time record
*/
PERSONAL_TIME_RECORD,
/**
* The reward is granted if the player beats the global least deaths record
*/
GLOBAL_DEATH_RECORD,
/**
* The reward is granted if the player beats the global least time record
*/
GLOBAL_TIME_RECORD,
;
/**
* Gets a reward condition from the given string
*
* @param condition <p>The string specifying a reward condition</p>
* @return <p>The matching reward condition, or null if not found</p>
*/
public static @Nullable RewardCondition getFromString(@NotNull String condition) {
for (RewardCondition rewardCondition : RewardCondition.values()) {
if (rewardCondition.name().equalsIgnoreCase(condition.replace("-", "_"))) {
return rewardCondition;
}
}
return null;
}
@NotNull
@Override
public Map<String, Object> serialize() {
Map<String, Object> data = new HashMap<>();
data.put("condition", this.name());
return data;
}
/**
* Deserializes a reward condition from the given data
*
* @param data <p>The data to deserialize</p>
* @return <p>The deserialized reward condition</p>
*/
@SuppressWarnings({"unused"})
public static @NotNull RewardCondition deserialize(@NotNull Map<String, Object> data) {
RewardCondition rewardCondition = getFromString(String.valueOf(data.get("condition")));
return Objects.requireNonNullElse(rewardCondition, RewardCondition.FIRST_WIN);
}
}

View File

@ -0,0 +1,47 @@
package net.knarcraft.minigames.arena.reward;
import org.jetbrains.annotations.NotNull;
/**
* The type of a specific reward
*/
public enum RewardType {
/**
* A command reward
*/
COMMAND,
/**
* An economy reward
*/
ECONOMY,
/**
* An item reward
*/
ITEM,
/**
* A permission reward
*/
PERMISSION,
;
/**
* Gets a reward type from the given string
*
* @param condition <p>The string specifying a reward type</p>
* @return <p>The matching reward type, or null if not found</p>
*/
public static RewardType getFromString(@NotNull String condition) {
for (RewardType rewardType : RewardType.values()) {
if (rewardType.name().equalsIgnoreCase(condition.replace("-", "_"))) {
return rewardType;
}
}
return null;
}
}

View File

@ -0,0 +1,134 @@
package net.knarcraft.minigames.command;
import net.knarcraft.minigames.config.DropperConfiguration;
import net.knarcraft.minigames.util.InputValidationHelper;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.CommandExecutor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* An abstract arena edit command, implementing input validation
*/
public abstract class EditArenaCommand implements CommandExecutor {
private final DropperConfiguration configuration;
/**
* Instantiates a new edit arena command
*
* @param configuration <p>The configuration to use</p>
*/
public EditArenaCommand(DropperConfiguration configuration) {
this.configuration = configuration;
}
/**
* Parses the specified max players
*
* @param maxPlayers <p>The max players string to parse</p>
* @return <p>The parsed value, or -1 if not parse-able</p>
*/
protected int parseMaxPlayers(@NotNull String maxPlayers) {
try {
return Integer.parseInt(maxPlayers);
} catch (NumberFormatException exception) {
return -1;
}
}
/**
* Sanitizes the player's specified vertical velocity
*
* @param velocityString <p>The string to parse into a velocity</p>
* @return <p>The parsed velocity, defaulting to 0.5 if not parse-able</p>
*/
protected double sanitizeVerticalVelocity(@NotNull String velocityString) {
// Vertical velocity should not be negative, as it would make the player go upwards. There is technically not a
// max speed limit, but setting it too high makes the arena unplayable
double velocity;
try {
velocity = Double.parseDouble(velocityString);
} catch (NumberFormatException exception) {
velocity = configuration.getVerticalVelocity();
}
// Require at least speed of 0.001, and at most 75 blocks/s
return Math.min(Math.max(velocity, 0.001), 75);
}
/**
* Sanitizes the user's specified horizontal velocity
*
* @param velocityString <p>The string to parse into a velocity</p>
* @return <p>The parsed velocity, defaulting to 1 if not parse-able</p>
*/
protected float sanitizeHorizontalVelocity(@NotNull String velocityString) {
// Horizontal velocity is valid between -1 and 1, where negative values swaps directions
float velocity;
try {
velocity = Float.parseFloat(velocityString);
} catch (NumberFormatException exception) {
velocity = configuration.getHorizontalVelocity();
}
// If outside bonds, choose the most extreme value
return Math.min(Math.max(0.1f, velocity), 1);
}
/**
* Parses the given location string
*
* @param player <p>The player changing a location</p>
* @param locationString <p>The location string to parse</p>
* @return <p>The parsed location, or the player's location if not parse-able</p>
*/
protected @NotNull Location parseLocation(Player player, String locationString) {
if ((locationString.trim() + ",").matches("([0-9]+.?[0-9]*,){3}")) {
String[] parts = locationString.split(",");
Location newLocation = player.getLocation().clone();
newLocation.setX(Double.parseDouble(parts[0].trim()));
newLocation.setY(Double.parseDouble(parts[1].trim()));
newLocation.setZ(Double.parseDouble(parts[2].trim()));
return newLocation;
} else {
return player.getLocation().clone();
}
}
/**
* Parses the given material name
*
* @param materialName <p>The material name to parse</p>
* @return <p>The parsed material, or AIR if not valid</p>
*/
protected @NotNull Material parseMaterial(String materialName) {
Material material = Material.matchMaterial(materialName);
if (material == null) {
material = Material.AIR;
}
return material;
}
/**
* Splits the given string on comma, and returns a set of its parts
*
* @param input <p>The input string to get as a set</p>
* @return <p>The resulting string set</p>
*/
@Nullable
protected Set<String> asSet(@NotNull String input) {
if (InputValidationHelper.isEmptyValue(input)) {
return null;
} else {
return new HashSet<>(List.of(input.split(",")));
}
}
}

View File

@ -0,0 +1,103 @@
package net.knarcraft.minigames.command;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.Arena;
import net.knarcraft.minigames.arena.ArenaGroup;
import net.knarcraft.minigames.arena.ArenaHandler;
import net.knarcraft.minigames.config.MiniGameMessage;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingContains;
/**
* A command for listing the arenas in a group
*
* @param <K> <p>The type of arena handler to get arenas from</p>
* @param <L> <p>The type of arena to list</p>
* @param <M> <p>The type of arena group to list</p>
*/
public abstract class GroupListCommand<
K extends ArenaHandler<L, M>,
L extends Arena,
M extends ArenaGroup<L, M>
> implements TabExecutor {
/**
* Displays all currently existing dropper arena groups
*
* @param arenaHandler <p>The arena handler to get groups from</p>
* @param sender <p>The command sender to display the groups to</p>
*/
protected void displayExistingGroups(@NotNull K arenaHandler, @NotNull CommandSender sender) {
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
StringBuilder builder = new StringBuilder(stringFormatter.getUnFormattedMessage(
MiniGameMessage.SUCCESS_GROUPS)).append("\n");
arenaHandler.getAllGroups().stream().sorted().forEachOrdered((group) ->
builder.append("- ").append(group.getGroupName()).append("\n"));
stringFormatter.displaySuccessMessage(sender, builder.toString());
}
/**
* Displays the ordered stages in a specified group to the specified command sender
*
* @param arenaHandler <p>The arena handler to get groups from</p>
* @param sender <p>The command sender to display the stages to</p>
* @param groupName <p>The name of the group to display stages for</p>
* @return <p>True if the stages were successfully displayed</p>
*/
protected boolean displayOrderedArenaNames(@NotNull K arenaHandler, @NotNull CommandSender sender,
@NotNull String groupName) {
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
M arenaGroup = arenaHandler.getGroup(groupName);
if (arenaGroup == null) {
stringFormatter.displayErrorMessage(sender, MiniGameMessage.ERROR_GROUP_NOT_FOUND);
return false;
}
// Send a list of all stages (arenas in the group)
StringBuilder builder = new StringBuilder(stringFormatter.replacePlaceholder(
MiniGameMessage.SUCCESS_GROUP_STAGES, "{group}", groupName));
int counter = 1;
for (UUID arenaId : arenaGroup.getArenas()) {
L arena = arenaHandler.getArena(arenaId);
if (arena != null) {
builder.append("\n").append(counter++).append(". ").append(arena.getArenaName());
}
}
stringFormatter.displaySuccessMessage(sender, builder.toString());
return true;
}
/**
* Gets all available groups
*
* @return <p>All available groups</p>
*/
protected abstract Set<M> getGroups();
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) {
if (arguments.length == 1) {
List<String> groupNames = new ArrayList<>();
for (M group : getGroups()) {
groupNames.add(group.getGroupName());
}
return filterMatchingContains(groupNames, arguments[0]);
} else {
return new ArrayList<>();
}
}
}

View File

@ -11,7 +11,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Supplier; import java.util.function.Supplier;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains; import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingContains;
/** /**
* An abstract class for an arena joining tab-completer * An abstract class for an arena joining tab-completer

View File

@ -2,7 +2,7 @@ package net.knarcraft.minigames.command;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaSession; import net.knarcraft.minigames.arena.ArenaSession;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor; import org.bukkit.command.TabExecutor;
@ -22,17 +22,19 @@ public class LeaveArenaCommand implements TabExecutor {
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] strings) { @NotNull String[] strings) {
if (!(commandSender instanceof Player player)) { if (!(commandSender instanceof Player player)) {
commandSender.sendMessage(Message.ERROR_PLAYER_ONLY.getMessage()); MiniGames.getInstance().getStringFormatter().displayErrorMessage(commandSender,
MiniGameMessage.ERROR_PLAYER_ONLY);
return false; return false;
} }
ArenaSession existingSession = MiniGames.getInstance().getSession(player.getUniqueId()); ArenaSession existingSession = MiniGames.getInstance().getSession(player.getUniqueId());
if (existingSession == null) { if (existingSession == null) {
commandSender.sendMessage(Message.ERROR_NOT_IN_ARENA.getMessage()); MiniGames.getInstance().getStringFormatter().displayErrorMessage(commandSender,
MiniGameMessage.ERROR_NOT_IN_ARENA);
return false; return false;
} }
existingSession.triggerQuit(false); existingSession.triggerQuit(false, true);
return true; return true;
} }

View File

@ -0,0 +1,41 @@
package net.knarcraft.minigames.command;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.util.GUIHelper;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* The command for opening up an arena's menu
*/
public class MenuCommand implements TabExecutor {
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) {
if (!(commandSender instanceof Player player)) {
MiniGames.getInstance().getStringFormatter().displayErrorMessage(commandSender,
MiniGameMessage.ERROR_PLAYER_ONLY);
return false;
}
GUIHelper.openGUI(player);
return true;
}
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] strings) {
return new ArrayList<>();
}
}

View File

@ -1,7 +1,7 @@
package net.knarcraft.minigames.command; package net.knarcraft.minigames.command;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor; import org.bukkit.command.TabExecutor;
@ -20,7 +20,8 @@ public class ReloadCommand implements TabExecutor {
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) { @NotNull String[] arguments) {
MiniGames.getInstance().reload(); MiniGames.getInstance().reload();
commandSender.sendMessage(Message.SUCCESS_PLUGIN_RELOADED.getMessage()); MiniGames.getInstance().getStringFormatter().displaySuccessMessage(commandSender,
MiniGameMessage.SUCCESS_PLUGIN_RELOADED);
return true; return true;
} }

View File

@ -0,0 +1,91 @@
package net.knarcraft.minigames.command;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.Arena;
import net.knarcraft.minigames.arena.reward.Reward;
import net.knarcraft.minigames.arena.reward.RewardCondition;
import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.util.InputValidationHelper;
import net.knarcraft.minigames.util.RewardHelper;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
/**
* The command used for setting arena rewards
*/
public class SetArenaRewardCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) {
if (!(commandSender instanceof Player player)) {
MiniGames.getInstance().getStringFormatter().displayErrorMessage(commandSender,
MiniGameMessage.ERROR_PLAYER_ONLY);
return false;
}
if (arguments.length < 4) {
return false;
}
/*
/MiniGamesReward add dropper <name> <condition> <type> [data]
/MiniGamesReward add parkour <name> <condition> <type> [data]
/MiniGamesReward clear dropper <name> <condition>
/MiniGamesReward clear parkour <name> <condition>
*/
Arena arena = null;
if (arguments[1].equalsIgnoreCase("dropper")) {
arena = MiniGames.getInstance().getDropperArenaHandler().getArena(arguments[2]);
} else if (arguments[1].equalsIgnoreCase("parkour")) {
arena = MiniGames.getInstance().getParkourArenaHandler().getArena(arguments[2]);
}
if (arena == null) {
MiniGames.getInstance().getStringFormatter().displayErrorMessage(commandSender,
MiniGameMessage.ERROR_ARENA_NOT_FOUND);
return false;
}
RewardCondition condition = RewardCondition.getFromString(arguments[3]);
if (condition == null) {
MiniGames.getInstance().getStringFormatter().displayErrorMessage(player,
MiniGameMessage.ERROR_REWARD_CONDITION_INVALID);
return false;
}
if (InputValidationHelper.isEmptyValue(arguments[0])) {
arena.clearRewards(condition);
MiniGames.getInstance().getStringFormatter().displaySuccessMessage(player,
MiniGameMessage.SUCCESS_REWARDS_CLEARED);
return true;
}
if (!arguments[0].equalsIgnoreCase("add") || arguments.length < 5) {
return false;
}
String firstArgument = arguments.length > 5 ? arguments[5] : null;
String secondArgument = arguments.length > 6 ? arguments[6] : null;
Reward reward = RewardHelper.parseRewardInput(player, arguments[4], firstArgument, secondArgument,
Arrays.copyOfRange(arguments, 5, arguments.length));
if (reward != null) {
arena.addReward(condition, reward);
MiniGames.getInstance().getStringFormatter().displaySuccessMessage(player,
MiniGameMessage.SUCCESS_REWARD_ADDED);
return true;
} else {
return false;
}
}
}

View File

@ -0,0 +1,168 @@
package net.knarcraft.minigames.command;
import net.knarcraft.knarlib.util.MaterialHelper;
import net.knarcraft.knarlib.util.TabCompletionHelper;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.Arena;
import net.knarcraft.minigames.arena.reward.RewardCondition;
import net.knarcraft.minigames.arena.reward.RewardType;
import net.knarcraft.minigames.util.InputValidationHelper;
import net.knarcraft.minigames.util.TabCompleteHelper;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingContains;
import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingStartsWith;
/**
* The tab completer for the reward setting command
*/
public class SetArenaRewardTabCompleter implements TabCompleter {
private static final List<String> materials = getMaterials();
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) {
if (arguments.length == 1) {
// The first argument is either clear or add
return TabCompletionHelper.filterMatchingStartsWith(Arrays.asList("add", "clear"), arguments[0]);
}
if (arguments.length >= 2) {
// If the first argument is invalid, stop further tab completion
if (!arguments[0].equalsIgnoreCase("add") && !InputValidationHelper.isEmptyValue(arguments[0])) {
return new ArrayList<>();
}
}
if (arguments.length == 2) {
// The second argument is the type of arena to change rewards for
return TabCompletionHelper.filterMatchingStartsWith(Arrays.asList("dropper", "parkour"), arguments[1]);
} else if (arguments.length == 3) {
// The third argument is the name of the arena to change rewards for
if (arguments[1].equalsIgnoreCase("dropper")) {
return filterMatchingContains(TabCompleteHelper.getDropperArenas(), arguments[2]);
} else if (arguments[1].equalsIgnoreCase("parkour")) {
return filterMatchingContains(TabCompleteHelper.getParkourArenas(), arguments[2]);
}
}
if (arguments.length >= 4) {
// Make sure a valid dropper or arena name has been given
Arena arena = null;
if (arguments[1].equalsIgnoreCase("dropper")) {
arena = MiniGames.getInstance().getDropperArenaHandler().getArena(arguments[2]);
} else if (arguments[1].equalsIgnoreCase("parkour")) {
arena = MiniGames.getInstance().getParkourArenaHandler().getArena(arguments[2]);
}
if (arena == null) {
return new ArrayList<>();
}
}
if (arguments.length == 4) {
// The fourth argument is the condition to change the reward for
return filterMatchingContains(getRewardConditions(), arguments[3]);
}
if (arguments.length >= 5) {
// If the condition is invalid, or it's the clear action, stop tab-completion
if (RewardCondition.getFromString(arguments[3]) == null || InputValidationHelper.isEmptyValue(arguments[0])) {
return new ArrayList<>();
}
}
if (arguments.length == 5) {
// The fifth argument is the type of reward to grant
return filterMatchingContains(getRewardTypes(), arguments[4]);
}
if (arguments.length >= 6) {
// Make sure a valid reward type has been given
RewardType rewardType = RewardType.getFromString(arguments[4]);
if (rewardType == null) {
return new ArrayList<>();
}
if (arguments.length == 6) {
return switch (rewardType) {
case ITEM -> filterMatchingContains(materials, arguments[5]);
case PERMISSION -> TabCompleteHelper.tabCompletePermission(arguments[5]);
case ECONOMY -> filterMatchingStartsWith(Arrays.asList("1", "5", "10", "25", "50"), arguments[5]);
case COMMAND -> filterMatchingStartsWith(getCommands(), arguments[5]);
};
}
if (rewardType == RewardType.ITEM && arguments.length == 7) {
// If a valid item material has been given, give potential amounts
if (MaterialHelper.loadMaterialString(arguments[5], MiniGames.getInstance().getLogger()) == null) {
return new ArrayList<>();
}
return Arrays.asList("1", "5", "10", "16", "32", "48", "64");
}
}
return new ArrayList<>();
}
/**
* Gets example command rewards
*
* @return <p>Example command rewards</p>
*/
private static List<String> getCommands() {
List<String> commands = new ArrayList<>();
commands.add("f powerboost player add %player% 1");
commands.add("minecraft:xp give (player_name) 1000");
return commands;
}
/**
* Gets all materials grant-able as item rewards
*
* @return <p>All grant-able materials</p>
*/
private static List<String> getMaterials() {
List<String> materials = new ArrayList<>();
Set<Material> invalid = EnumSet.of(Material.WATER, Material.LAVA, Material.POWDER_SNOW);
for (Material material : Material.values()) {
if (material.isAir() || invalid.contains(material) || (material.isBlock() &&
(material.getHardness() == -1 || material.getHardness() == Double.MAX_VALUE))) {
continue;
}
materials.add(material.name());
}
return materials;
}
/**
* Gets a list of all reward types
*
* @return <p>All reward types</p>
*/
private List<String> getRewardTypes() {
List<String> types = new ArrayList<>();
for (RewardType rewardType : RewardType.values()) {
types.add(rewardType.name());
}
return types;
}
/**
* Gets a list of all reward conditions
*
* @return <p>All reward conditions</p>
*/
private List<String> getRewardConditions() {
List<String> conditions = new ArrayList<>();
for (RewardCondition rewardCondition : RewardCondition.values()) {
conditions.add(rewardCondition.name());
}
return conditions;
}
}

View File

@ -3,7 +3,7 @@ package net.knarcraft.minigames.command.dropper;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.dropper.DropperArena; import net.knarcraft.minigames.arena.dropper.DropperArena;
import net.knarcraft.minigames.arena.dropper.DropperArenaHandler; import net.knarcraft.minigames.arena.dropper.DropperArenaHandler;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.util.StringSanitizer; import net.knarcraft.minigames.util.StringSanitizer;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -20,7 +20,8 @@ public class CreateDropperArenaCommand implements CommandExecutor {
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) { @NotNull String[] arguments) {
if (!(commandSender instanceof Player player)) { if (!(commandSender instanceof Player player)) {
commandSender.sendMessage(Message.ERROR_PLAYER_ONLY.getMessage()); MiniGames.getInstance().getStringFormatter().displayErrorMessage(commandSender,
MiniGameMessage.ERROR_PLAYER_ONLY);
return false; return false;
} }
@ -38,16 +39,17 @@ public class CreateDropperArenaCommand implements CommandExecutor {
} }
DropperArenaHandler arenaHandler = MiniGames.getInstance().getDropperArenaHandler(); DropperArenaHandler arenaHandler = MiniGames.getInstance().getDropperArenaHandler();
DropperArena existingArena = arenaHandler.getArena(arenaName); DropperArena existingArena = arenaHandler.getArena(arenaName);
if (existingArena != null) { if (existingArena != null) {
commandSender.sendMessage(Message.ERROR_ARENA_NAME_COLLISION.getMessage()); MiniGames.getInstance().getStringFormatter().displayErrorMessage(commandSender,
MiniGameMessage.ERROR_ARENA_NAME_COLLISION);
return false; return false;
} }
DropperArena arena = new DropperArena(arenaName, player.getLocation(), arenaHandler); DropperArena arena = new DropperArena(arenaName, player.getLocation(), arenaHandler);
arenaHandler.addArena(arena); arenaHandler.addArena(arena);
commandSender.sendMessage(Message.SUCCESS_ARENA_CREATED.getMessage()); MiniGames.getInstance().getStringFormatter().displaySuccessMessage(commandSender,
MiniGameMessage.SUCCESS_ARENA_CREATED);
return true; return true;
} }

View File

@ -4,23 +4,17 @@ import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.dropper.DropperArena; import net.knarcraft.minigames.arena.dropper.DropperArena;
import net.knarcraft.minigames.arena.dropper.DropperArenaGroup; import net.knarcraft.minigames.arena.dropper.DropperArenaGroup;
import net.knarcraft.minigames.arena.dropper.DropperArenaHandler; import net.knarcraft.minigames.arena.dropper.DropperArenaHandler;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.command.GroupListCommand;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.Set;
import java.util.List;
import java.util.UUID;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains;
/** /**
* The command for listing groups and the stages within * The command for listing groups and the stages within
*/ */
public class DropperGroupListCommand implements TabExecutor { public class DropperGroupListCommand extends GroupListCommand<DropperArenaHandler, DropperArena, DropperArenaGroup> {
@Override @Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@ -36,61 +30,9 @@ public class DropperGroupListCommand implements TabExecutor {
} }
} }
/**
* Displays all currently existing dropper arena groups
*
* @param arenaHandler <p>The arena handler to get groups from</p>
* @param sender <p>The command sender to display the groups to</p>
*/
private void displayExistingGroups(@NotNull DropperArenaHandler arenaHandler, @NotNull CommandSender sender) {
StringBuilder builder = new StringBuilder("Dropper arena groups:").append("\n");
arenaHandler.getAllGroups().stream().sorted().forEachOrdered((group) ->
builder.append(group.getGroupName()).append("\n"));
sender.sendMessage(builder.toString());
}
/**
* Displays the ordered stages in a specified group to the specified command sender
*
* @param arenaHandler <p>The arena handler to get groups from</p>
* @param sender <p>The command sender to display the stages to</p>
* @param groupName <p>The name of the group to display stages for</p>
* @return <p>True if the stages were successfully displayed</p>
*/
private boolean displayOrderedArenaNames(@NotNull DropperArenaHandler arenaHandler, @NotNull CommandSender sender,
@NotNull String groupName) {
DropperArenaGroup arenaGroup = arenaHandler.getGroup(groupName);
if (arenaGroup == null) {
sender.sendMessage(Message.ERROR_GROUP_NOT_FOUND.getMessage());
return false;
}
// Send a list of all stages (arenas in the group)
StringBuilder builder = new StringBuilder(Message.SUCCESS_GROUP_STAGES.getMessage("{group}", groupName));
int counter = 1;
for (UUID arenaId : arenaGroup.getArenas()) {
DropperArena arena = arenaHandler.getArena(arenaId);
if (arena != null) {
builder.append(counter++).append(". ").append(arena.getArenaName()).append("\n");
}
}
sender.sendMessage(builder.toString());
return true;
}
@Nullable
@Override @Override
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, protected Set<DropperArenaGroup> getGroups() {
@NotNull String[] arguments) { return MiniGames.getInstance().getDropperArenaHandler().getAllGroups();
if (arguments.length == 1) {
List<String> groupNames = new ArrayList<>();
for (DropperArenaGroup group : MiniGames.getInstance().getDropperArenaHandler().getAllGroups()) {
groupNames.add(group.getGroupName());
}
return filterMatchingContains(groupNames, arguments[0]);
} else {
return new ArrayList<>();
}
} }
} }

View File

@ -4,7 +4,8 @@ import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.dropper.DropperArena; import net.knarcraft.minigames.arena.dropper.DropperArena;
import net.knarcraft.minigames.arena.dropper.DropperArenaGroup; import net.knarcraft.minigames.arena.dropper.DropperArenaGroup;
import net.knarcraft.minigames.arena.dropper.DropperArenaHandler; import net.knarcraft.minigames.arena.dropper.DropperArenaHandler;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.util.InputValidationHelper;
import net.knarcraft.minigames.util.StringSanitizer; import net.knarcraft.minigames.util.StringSanitizer;
import net.knarcraft.minigames.util.TabCompleteHelper; import net.knarcraft.minigames.util.TabCompleteHelper;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -16,7 +17,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains; import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingContains;
/** /**
* The command for setting the group of an arena * The command for setting the group of an arena
@ -34,7 +35,8 @@ public class DropperGroupSetCommand implements TabExecutor {
DropperArena specifiedArena = arenaHandler.getArena(arguments[0]); DropperArena specifiedArena = arenaHandler.getArena(arguments[0]);
if (specifiedArena == null) { if (specifiedArena == null) {
commandSender.sendMessage(Message.ERROR_ARENA_NOT_FOUND.getMessage()); MiniGames.getInstance().getStringFormatter().displayErrorMessage(commandSender,
MiniGameMessage.ERROR_ARENA_NOT_FOUND);
return false; return false;
} }
@ -45,7 +47,7 @@ public class DropperGroupSetCommand implements TabExecutor {
} }
DropperArenaGroup arenaGroup; DropperArenaGroup arenaGroup;
if (groupName.equalsIgnoreCase("null") || groupName.equalsIgnoreCase("none")) { if (InputValidationHelper.isEmptyValue(groupName)) {
arenaGroup = null; arenaGroup = null;
} else { } else {
arenaGroup = arenaHandler.getGroup(groupName); arenaGroup = arenaHandler.getGroup(groupName);
@ -56,7 +58,8 @@ public class DropperGroupSetCommand implements TabExecutor {
arenaHandler.setGroup(specifiedArena.getArenaId(), arenaGroup); arenaHandler.setGroup(specifiedArena.getArenaId(), arenaGroup);
commandSender.sendMessage(Message.SUCCESS_ARENA_GROUP_UPDATED.getMessage()); MiniGames.getInstance().getStringFormatter().displaySuccessMessage(commandSender,
MiniGameMessage.SUCCESS_ARENA_GROUP_UPDATED);
return true; return true;
} }

View File

@ -1,10 +1,11 @@
package net.knarcraft.minigames.command.dropper; package net.knarcraft.minigames.command.dropper;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.dropper.DropperArena; import net.knarcraft.minigames.arena.dropper.DropperArena;
import net.knarcraft.minigames.arena.dropper.DropperArenaGroup; import net.knarcraft.minigames.arena.dropper.DropperArenaGroup;
import net.knarcraft.minigames.arena.dropper.DropperArenaHandler; import net.knarcraft.minigames.arena.dropper.DropperArenaHandler;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor; import org.bukkit.command.TabExecutor;
@ -15,7 +16,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains; import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingContains;
/** /**
* The command for swapping the order of two arenas in a group * The command for swapping the order of two arenas in a group
@ -29,30 +30,31 @@ public class DropperGroupSwapCommand implements TabExecutor {
return false; return false;
} }
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
DropperArenaHandler arenaHandler = MiniGames.getInstance().getDropperArenaHandler(); DropperArenaHandler arenaHandler = MiniGames.getInstance().getDropperArenaHandler();
DropperArena arena1 = arenaHandler.getArena(arguments[0]); DropperArena arena1 = arenaHandler.getArena(arguments[0]);
if (arena1 == null) { if (arena1 == null) {
commandSender.sendMessage(Message.ERROR_ARENA_1_NOT_FOUND.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_ARENA_1_NOT_FOUND);
return false; return false;
} }
DropperArena arena2 = arenaHandler.getArena(arguments[1]); DropperArena arena2 = arenaHandler.getArena(arguments[1]);
if (arena2 == null) { if (arena2 == null) {
commandSender.sendMessage(Message.ERROR_ARENA_2_NOT_FOUND.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_ARENA_2_NOT_FOUND);
return false; return false;
} }
DropperArenaGroup arena1Group = arenaHandler.getGroup(arena1.getArenaId()); DropperArenaGroup arena1Group = arenaHandler.getGroup(arena1.getArenaId());
DropperArenaGroup arena2Group = arenaHandler.getGroup(arena2.getArenaId()); DropperArenaGroup arena2Group = arenaHandler.getGroup(arena2.getArenaId());
if (arena1Group == null || !arena1Group.equals(arena2Group)) { if (arena1Group == null || !arena1Group.equals(arena2Group)) {
commandSender.sendMessage(Message.ERROR_SWAP_DIFFERENT_GROUPS.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_SWAP_DIFFERENT_GROUPS);
return false; return false;
} }
arena1Group.swapArenas(arena1Group.getArenas().indexOf(arena1.getArenaId()), arena1Group.swapArenas(arena1Group.getArenas().indexOf(arena1.getArenaId()),
arena1Group.getArenas().indexOf(arena2.getArenaId())); arena1Group.getArenas().indexOf(arena2.getArenaId()));
commandSender.sendMessage(Message.SUCCESS_ARENAS_SWAPPED.getMessage()); stringFormatter.displaySuccessMessage(commandSender, MiniGameMessage.SUCCESS_ARENAS_SWAPPED);
return true; return true;
} }

View File

@ -1,15 +1,14 @@
package net.knarcraft.minigames.command.dropper; package net.knarcraft.minigames.command.dropper;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.dropper.DropperArena; import net.knarcraft.minigames.arena.dropper.DropperArena;
import net.knarcraft.minigames.arena.dropper.DropperArenaEditableProperty; import net.knarcraft.minigames.arena.dropper.DropperArenaEditableProperty;
import net.knarcraft.minigames.command.EditArenaCommand;
import net.knarcraft.minigames.config.DropperConfiguration; import net.knarcraft.minigames.config.DropperConfiguration;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.container.PlaceholderContainer; import net.knarcraft.minigames.util.InputValidationHelper;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -17,9 +16,7 @@ import org.jetbrains.annotations.NotNull;
/** /**
* The command for editing an existing dropper arena * The command for editing an existing dropper arena
*/ */
public class EditDropperArenaCommand implements CommandExecutor { public class EditDropperArenaCommand extends EditArenaCommand {
private final DropperConfiguration configuration;
/** /**
* Instantiates a new edit arena command * Instantiates a new edit arena command
@ -27,14 +24,15 @@ public class EditDropperArenaCommand implements CommandExecutor {
* @param configuration <p>The configuration to use</p> * @param configuration <p>The configuration to use</p>
*/ */
public EditDropperArenaCommand(DropperConfiguration configuration) { public EditDropperArenaCommand(DropperConfiguration configuration) {
this.configuration = configuration; super(configuration);
} }
@Override @Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) { @NotNull String[] arguments) {
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
if (!(commandSender instanceof Player player)) { if (!(commandSender instanceof Player player)) {
commandSender.sendMessage(Message.ERROR_PLAYER_ONLY.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_PLAYER_ONLY);
return false; return false;
} }
@ -44,29 +42,31 @@ public class EditDropperArenaCommand implements CommandExecutor {
DropperArena specifiedArena = MiniGames.getInstance().getDropperArenaHandler().getArena(arguments[0]); DropperArena specifiedArena = MiniGames.getInstance().getDropperArenaHandler().getArena(arguments[0]);
if (specifiedArena == null) { if (specifiedArena == null) {
commandSender.sendMessage(Message.ERROR_ARENA_NOT_FOUND.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_ARENA_NOT_FOUND);
return false; return false;
} }
DropperArenaEditableProperty editableProperty = DropperArenaEditableProperty.getFromArgumentString(arguments[1]); DropperArenaEditableProperty editableProperty = DropperArenaEditableProperty.getFromArgumentString(arguments[1]);
if (editableProperty == null) { if (editableProperty == null) {
commandSender.sendMessage(Message.ERROR_UNKNOWN_PROPERTY.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_UNKNOWN_PROPERTY);
return false; return false;
} }
if (arguments.length < 3) { if (arguments.length < 3) {
// Print the current value of the property // Print the current value of the property
String value = editableProperty.getCurrentValueAsString(specifiedArena); String value = editableProperty.getCurrentValueAsString(specifiedArena);
commandSender.sendMessage(Message.SUCCESS_CURRENT_VALUE.getMessage(new PlaceholderContainer().add( stringFormatter.displaySuccessMessage(commandSender, stringFormatter.replacePlaceholders(
"{property}", editableProperty.getArgumentString()).add("{value}", value))); MiniGameMessage.SUCCESS_CURRENT_VALUE, new String[]{"{property}", "{value}"},
new String[]{editableProperty.getArgumentString(), value}));
return true; return true;
} else { } else {
boolean successful = changeValue(specifiedArena, editableProperty, arguments[2], player); boolean successful = changeValue(specifiedArena, editableProperty, arguments[2], player);
if (successful) { if (successful) {
player.sendMessage(Message.SUCCESS_PROPERTY_CHANGED.getMessage("{property}", stringFormatter.displaySuccessMessage(player, stringFormatter.replacePlaceholder(
MiniGameMessage.SUCCESS_PROPERTY_CHANGED, "{property}",
editableProperty.getArgumentString())); editableProperty.getArgumentString()));
} else { } else {
player.sendMessage(Message.ERROR_PROPERTY_INPUT_INVALID.getMessage()); stringFormatter.displayErrorMessage(player, MiniGameMessage.ERROR_PROPERTY_INPUT_INVALID);
} }
return successful; return successful;
} }
@ -90,80 +90,12 @@ public class EditDropperArenaCommand implements CommandExecutor {
case SPAWN_LOCATION -> arena.setSpawnLocation(parseLocation(player, value)); case SPAWN_LOCATION -> arena.setSpawnLocation(parseLocation(player, value));
case NAME -> arena.setName(value); case NAME -> arena.setName(value);
case EXIT_LOCATION -> arena.setExitLocation(parseLocation(player, value)); case EXIT_LOCATION -> arena.setExitLocation(parseLocation(player, value));
case MAX_PLAYERS -> arena.setMaxPlayers(parseMaxPlayers(value));
case ALLOWED_DAMAGE_CAUSES ->
arena.setAllowedDamageCauses(InputValidationHelper.parseDamageCauses(asSet(value)));
case LOSS_TRIGGER_DAMAGE_CAUSES ->
arena.setLossTriggerDamageCauses(InputValidationHelper.parseDamageCauses(asSet(value)));
}; };
} }
/**
* Sanitizes the player's specified vertical velocity
*
* @param velocityString <p>The string to parse into a velocity</p>
* @return <p>The parsed velocity, defaulting to 0.5 if not parse-able</p>
*/
private double sanitizeVerticalVelocity(@NotNull String velocityString) {
// Vertical velocity should not be negative, as it would make the player go upwards. There is technically not a
// max speed limit, but setting it too high makes the arena unplayable
double velocity;
try {
velocity = Double.parseDouble(velocityString);
} catch (NumberFormatException exception) {
velocity = configuration.getVerticalVelocity();
}
// Require at least speed of 0.001, and at most 75 blocks/s
return Math.min(Math.max(velocity, 0.001), 75);
}
/**
* Sanitizes the user's specified horizontal velocity
*
* @param velocityString <p>The string to parse into a velocity</p>
* @return <p>The parsed velocity, defaulting to 1 if not parse-able</p>
*/
private float sanitizeHorizontalVelocity(@NotNull String velocityString) {
// Horizontal velocity is valid between -1 and 1, where negative values swaps directions
float velocity;
try {
velocity = Float.parseFloat(velocityString);
} catch (NumberFormatException exception) {
velocity = configuration.getHorizontalVelocity();
}
// If outside bonds, choose the most extreme value
return Math.min(Math.max(0.1f, velocity), 1);
}
/**
* Parses the given location string
*
* @param player <p>The player changing a location</p>
* @param locationString <p>The location string to parse</p>
* @return <p>The parsed location, or the player's location if not parse-able</p>
*/
private @NotNull Location parseLocation(Player player, String locationString) {
if ((locationString.trim() + ",").matches("([0-9]+.?[0-9]*,){3}")) {
String[] parts = locationString.split(",");
Location newLocation = player.getLocation().clone();
newLocation.setX(Double.parseDouble(parts[0].trim()));
newLocation.setY(Double.parseDouble(parts[1].trim()));
newLocation.setZ(Double.parseDouble(parts[2].trim()));
return newLocation;
} else {
return player.getLocation().clone();
}
}
/**
* Parses the given material name
*
* @param materialName <p>The material name to parse</p>
* @return <p>The parsed material, or AIR if not valid</p>
*/
private @NotNull Material parseMaterial(String materialName) {
Material material = Material.matchMaterial(materialName);
if (material == null) {
material = Material.AIR;
}
return material;
}
} }

View File

@ -1,5 +1,6 @@
package net.knarcraft.minigames.command.dropper; package net.knarcraft.minigames.command.dropper;
import net.knarcraft.minigames.arena.EditablePropertyType;
import net.knarcraft.minigames.arena.dropper.DropperArenaEditableProperty; import net.knarcraft.minigames.arena.dropper.DropperArenaEditableProperty;
import net.knarcraft.minigames.util.TabCompleteHelper; import net.knarcraft.minigames.util.TabCompleteHelper;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -11,7 +12,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains; import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingContains;
/** /**
* The tab-completer for the edit arena command * The tab-completer for the edit arena command
@ -30,8 +31,14 @@ public class EditDropperArenaTabCompleter implements TabCompleter {
if (property == null) { if (property == null) {
return new ArrayList<>(); return new ArrayList<>();
} }
return filterMatchingContains(TabCompleteHelper.getTabCompleteSuggestions(property.getPropertyType()), EditablePropertyType propertyType = property.getPropertyType();
arguments[2]);
if (propertyType == EditablePropertyType.MATERIAL_LIST ||
propertyType == EditablePropertyType.DAMAGE_CAUSE_LIST) {
return TabCompleteHelper.getListCompleteSuggestions(propertyType, arguments[2]);
} else {
return filterMatchingContains(TabCompleteHelper.getTabCompleteSuggestions(propertyType), arguments[2]);
}
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -1,5 +1,6 @@
package net.knarcraft.minigames.command.dropper; package net.knarcraft.minigames.command.dropper;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaPlayerRegistry; import net.knarcraft.minigames.arena.ArenaPlayerRegistry;
import net.knarcraft.minigames.arena.dropper.DropperArena; import net.knarcraft.minigames.arena.dropper.DropperArena;
@ -7,7 +8,9 @@ import net.knarcraft.minigames.arena.dropper.DropperArenaGameMode;
import net.knarcraft.minigames.arena.dropper.DropperArenaGroup; import net.knarcraft.minigames.arena.dropper.DropperArenaGroup;
import net.knarcraft.minigames.arena.dropper.DropperArenaSession; import net.knarcraft.minigames.arena.dropper.DropperArenaSession;
import net.knarcraft.minigames.config.DropperConfiguration; import net.knarcraft.minigames.config.DropperConfiguration;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.util.GUIHelper;
import net.knarcraft.minigames.util.GeyserHelper;
import net.knarcraft.minigames.util.PlayerTeleporter; import net.knarcraft.minigames.util.PlayerTeleporter;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -23,8 +26,9 @@ public class JoinDropperArenaCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) { @NotNull String[] arguments) {
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
if (!(commandSender instanceof Player player)) { if (!(commandSender instanceof Player player)) {
commandSender.sendMessage(Message.ERROR_PLAYER_ONLY.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_PLAYER_ONLY);
return false; return false;
} }
@ -32,26 +36,39 @@ public class JoinDropperArenaCommand implements CommandExecutor {
return false; return false;
} }
if (GeyserHelper.isGeyserPlayer(player)) {
stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_GEYSER_DROPPER);
return true;
}
// Disallow joining if the player is already in a mini-game arena // Disallow joining if the player is already in a mini-game arena
if (MiniGames.getInstance().getSession(player.getUniqueId()) != null) { if (MiniGames.getInstance().getSession(player.getUniqueId()) != null) {
commandSender.sendMessage(Message.ERROR_ALREADY_PLAYING.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_ALREADY_PLAYING);
return false; return true;
} }
// Make sure the arena exists // Make sure the arena exists
DropperArena specifiedArena = MiniGames.getInstance().getDropperArenaHandler().getArena(arguments[0]); DropperArena specifiedArena = MiniGames.getInstance().getDropperArenaHandler().getArena(arguments[0]);
if (specifiedArena == null) { if (specifiedArena == null) {
commandSender.sendMessage(Message.ERROR_ARENA_NOT_FOUND.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_ARENA_NOT_FOUND);
return false; return false;
} }
// Deny vehicles as allowing this is tricky, and will cause problems in some cases // Deny vehicles as allowing this is tricky, and will cause problems in some cases
if (player.isInsideVehicle() || !player.getPassengers().isEmpty()) { if (player.isInsideVehicle() || !player.getPassengers().isEmpty()) {
commandSender.sendMessage(Message.ERROR_JOIN_IN_VEHICLE_OR_PASSENGER.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_JOIN_IN_VEHICLE_OR_PASSENGER);
return false; return true;
} }
return joinArena(specifiedArena, player, arguments); // Deny joining full arenas
int playingNow = MiniGames.getInstance().getDropperArenaPlayerRegistry().getPlayingPlayers(specifiedArena).size();
if (specifiedArena.getMaxPlayers() > 0 && playingNow >= specifiedArena.getMaxPlayers()) {
stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_JOIN_ARENA_FULL);
return true;
}
joinArena(specifiedArena, player, arguments);
return true;
} }
/** /**
@ -60,13 +77,12 @@ public class JoinDropperArenaCommand implements CommandExecutor {
* @param specifiedArena <p>The arena the player wants to join</p> * @param specifiedArena <p>The arena the player wants to join</p>
* @param player <p>The player joining the arena</p> * @param player <p>The player joining the arena</p>
* @param arguments <p>The arguments given</p> * @param arguments <p>The arguments given</p>
* @return <p>Whether the arena was joined successfully</p>
*/ */
private boolean joinArena(DropperArena specifiedArena, Player player, String[] arguments) { private void joinArena(DropperArena specifiedArena, Player player, String[] arguments) {
// Find the specified game-mode // Find the specified game-mode
DropperArenaGameMode gameMode; DropperArenaGameMode gameMode;
if (arguments.length > 1) { if (arguments.length > 1) {
gameMode = DropperArenaGameMode.matchGamemode(arguments[1]); gameMode = DropperArenaGameMode.matchGameMode(arguments[1]);
} else { } else {
gameMode = DropperArenaGameMode.DEFAULT; gameMode = DropperArenaGameMode.DEFAULT;
} }
@ -74,15 +90,17 @@ public class JoinDropperArenaCommand implements CommandExecutor {
// Make sure the player has beaten the necessary levels // Make sure the player has beaten the necessary levels
DropperArenaGroup arenaGroup = MiniGames.getInstance().getDropperArenaHandler().getGroup(specifiedArena.getArenaId()); DropperArenaGroup arenaGroup = MiniGames.getInstance().getDropperArenaHandler().getGroup(specifiedArena.getArenaId());
if (arenaGroup != null && !doGroupChecks(specifiedArena, arenaGroup, gameMode, player)) { if (arenaGroup != null && !doGroupChecks(specifiedArena, arenaGroup, gameMode, player)) {
return false; return;
} }
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
// Make sure the player has beaten the arena once in normal mode before playing another mode // Make sure the player has beaten the arena once in normal mode before playing another mode
if (MiniGames.getInstance().getDropperConfiguration().mustDoNormalModeFirst() && if (MiniGames.getInstance().getDropperConfiguration().mustDoNormalModeFirst() &&
gameMode != DropperArenaGameMode.DEFAULT && gameMode != DropperArenaGameMode.DEFAULT &&
specifiedArena.getData().hasNotCompleted(DropperArenaGameMode.DEFAULT, player)) { specifiedArena.getData().hasNotCompleted(DropperArenaGameMode.DEFAULT, player)) {
player.sendMessage(Message.ERROR_NORMAL_MODE_REQUIRED.getMessage()); stringFormatter.displayErrorMessage(player, MiniGameMessage.ERROR_NORMAL_MODE_REQUIRED);
return false; return;
} }
// Register the player's session // Register the player's session
@ -90,17 +108,20 @@ public class JoinDropperArenaCommand implements CommandExecutor {
ArenaPlayerRegistry<DropperArena> playerRegistry = MiniGames.getInstance().getDropperArenaPlayerRegistry(); ArenaPlayerRegistry<DropperArena> playerRegistry = MiniGames.getInstance().getDropperArenaPlayerRegistry();
playerRegistry.registerPlayer(player.getUniqueId(), newSession); playerRegistry.registerPlayer(player.getUniqueId(), newSession);
// Update visibility and hit-box for the player
MiniGames.getInstance().getPlayerVisibilityManager().updateHiddenPlayers(playerRegistry, player);
// Try to teleport the player to the arena // Try to teleport the player to the arena
boolean teleported = PlayerTeleporter.teleportPlayer(player, specifiedArena.getSpawnLocation(), false, false); boolean teleported = PlayerTeleporter.teleportPlayer(player, specifiedArena.getSpawnLocation(), false, false);
if (!teleported) { if (!teleported) {
player.sendMessage(Message.ERROR_ARENA_TELEPORT_FAILED.getMessage()); stringFormatter.displayErrorMessage(player, MiniGameMessage.ERROR_ARENA_TELEPORT_FAILED);
newSession.triggerQuit(false); newSession.triggerQuit(false, true);
return false;
} else { } else {
// Make sure to update the state again in the air to remove a potential swimming state // Update the player's state to follow the arena's rules
newSession.getEntryState().setArenaState(); newSession.getEntryState().setArenaState();
player.sendMessage(Message.SUCCESS_ARENA_JOINED.getMessage());
return true; player.getInventory().addItem(GUIHelper.getGUIOpenItem(player));
stringFormatter.displaySuccessMessage(player, MiniGameMessage.SUCCESS_ARENA_JOINED);
} }
} }
@ -115,18 +136,19 @@ public class JoinDropperArenaCommand implements CommandExecutor {
*/ */
private boolean doGroupChecks(@NotNull DropperArena dropperArena, @NotNull DropperArenaGroup arenaGroup, private boolean doGroupChecks(@NotNull DropperArena dropperArena, @NotNull DropperArenaGroup arenaGroup,
@NotNull DropperArenaGameMode arenaGameMode, @NotNull Player player) { @NotNull DropperArenaGameMode arenaGameMode, @NotNull Player player) {
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
DropperConfiguration configuration = MiniGames.getInstance().getDropperConfiguration(); DropperConfiguration configuration = MiniGames.getInstance().getDropperConfiguration();
// Require that players beat all arenas in the group in the normal game-mode before trying challenge modes // Require that players beat all arenas in the group in the normal game-mode before trying challenge modes
if (configuration.mustDoNormalModeFirst() && arenaGameMode != DropperArenaGameMode.DEFAULT && if (configuration.mustDoNormalModeFirst() && arenaGameMode != DropperArenaGameMode.DEFAULT &&
!arenaGroup.hasBeatenAll(DropperArenaGameMode.DEFAULT, player)) { !arenaGroup.hasBeatenAll(DropperArenaGameMode.DEFAULT, player)) {
player.sendMessage(Message.ERROR_GROUP_NORMAL_MODE_REQUIRED.getMessage()); stringFormatter.displayErrorMessage(player, MiniGameMessage.ERROR_GROUP_NORMAL_MODE_REQUIRED);
return false; return false;
} }
// Require that the player has beaten the previous arena on the same game-mode before trying this one // Require that the player has beaten the previous arena on the same game-mode before trying this one
if (configuration.mustDoGroupedInSequence() && if (configuration.mustDoGroupedInSequence() &&
arenaGroup.cannotPlay(arenaGameMode, player, dropperArena.getArenaId())) { arenaGroup.cannotPlay(arenaGameMode, player, dropperArena.getArenaId())) {
player.sendMessage(Message.ERROR_PREVIOUS_ARENA_REQUIRED.getMessage()); stringFormatter.displayErrorMessage(player, MiniGameMessage.ERROR_PREVIOUS_ARENA_REQUIRED);
return false; return false;
} }

View File

@ -1,6 +1,7 @@
package net.knarcraft.minigames.command.dropper; package net.knarcraft.minigames.command.dropper;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.util.TabCompleteHelper; import net.knarcraft.minigames.util.TabCompleteHelper;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -19,11 +20,12 @@ public class ListDropperArenaCommand implements TabExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] arguments) { @NotNull String[] arguments) {
StringBuilder builder = new StringBuilder(Message.SUCCESS_DROPPER_ARENAS_LIST.getMessage()); StringBuilder builder = new StringBuilder(MiniGames.getInstance().getTranslator().getTranslatedMessage(
MiniGameMessage.SUCCESS_DROPPER_ARENAS_LIST));
for (String arenaName : TabCompleteHelper.getDropperArenas()) { for (String arenaName : TabCompleteHelper.getDropperArenas()) {
builder.append("\n").append(arenaName); builder.append("\n").append(arenaName);
} }
sender.sendMessage(builder.toString()); MiniGames.getInstance().getStringFormatter().displaySuccessMessage(sender, builder.toString());
return true; return true;
} }

View File

@ -1,8 +1,9 @@
package net.knarcraft.minigames.command.dropper; package net.knarcraft.minigames.command.dropper;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.dropper.DropperArena; import net.knarcraft.minigames.arena.dropper.DropperArena;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -21,16 +22,18 @@ public class RemoveDropperArenaCommand implements CommandExecutor {
return false; return false;
} }
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
// Get the specified arena // Get the specified arena
DropperArena targetArena = MiniGames.getInstance().getDropperArenaHandler().getArena(arguments[0]); DropperArena targetArena = MiniGames.getInstance().getDropperArenaHandler().getArena(arguments[0]);
if (targetArena == null) { if (targetArena == null) {
commandSender.sendMessage(Message.ERROR_ARENA_NOT_FOUND.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_ARENA_NOT_FOUND);
return false; return false;
} }
// Remove the arena // Remove the arena
MiniGames.getInstance().getDropperArenaHandler().removeArena(targetArena); MiniGames.getInstance().getDropperArenaHandler().removeArena(targetArena);
commandSender.sendMessage(Message.SUCCESS_ARENA_REMOVED.getMessage()); stringFormatter.displaySuccessMessage(commandSender, MiniGameMessage.SUCCESS_ARENA_REMOVED);
return true; return true;
} }

View File

@ -10,7 +10,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains; import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingContains;
/** /**
* The tab-completer for the remove arena command * The tab-completer for the remove arena command

View File

@ -1,9 +1,10 @@
package net.knarcraft.minigames.command.parkour; package net.knarcraft.minigames.command.parkour;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.parkour.ParkourArena; import net.knarcraft.minigames.arena.parkour.ParkourArena;
import net.knarcraft.minigames.arena.parkour.ParkourArenaHandler; import net.knarcraft.minigames.arena.parkour.ParkourArenaHandler;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.util.StringSanitizer; import net.knarcraft.minigames.util.StringSanitizer;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -19,8 +20,9 @@ public class CreateParkourArenaCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) { @NotNull String[] arguments) {
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
if (!(commandSender instanceof Player player)) { if (!(commandSender instanceof Player player)) {
commandSender.sendMessage(Message.ERROR_PLAYER_ONLY.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_PLAYER_ONLY);
return false; return false;
} }
@ -38,16 +40,15 @@ public class CreateParkourArenaCommand implements CommandExecutor {
} }
ParkourArenaHandler arenaHandler = MiniGames.getInstance().getParkourArenaHandler(); ParkourArenaHandler arenaHandler = MiniGames.getInstance().getParkourArenaHandler();
ParkourArena existingArena = arenaHandler.getArena(arenaName); ParkourArena existingArena = arenaHandler.getArena(arenaName);
if (existingArena != null) { if (existingArena != null) {
commandSender.sendMessage(Message.ERROR_ARENA_NAME_COLLISION.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_ARENA_NAME_COLLISION);
return false; return false;
} }
ParkourArena arena = new ParkourArena(arenaName, player.getLocation(), arenaHandler); ParkourArena arena = new ParkourArena(arenaName, player.getLocation(), arenaHandler);
arenaHandler.addArena(arena); arenaHandler.addArena(arena);
commandSender.sendMessage(Message.SUCCESS_ARENA_CREATED.getMessage()); stringFormatter.displaySuccessMessage(commandSender, MiniGameMessage.SUCCESS_ARENA_CREATED);
return true; return true;
} }

View File

@ -1,37 +1,35 @@
package net.knarcraft.minigames.command.parkour; package net.knarcraft.minigames.command.parkour;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.parkour.ParkourArena; import net.knarcraft.minigames.arena.parkour.ParkourArena;
import net.knarcraft.minigames.arena.parkour.ParkourArenaEditableProperty; import net.knarcraft.minigames.arena.parkour.ParkourArenaEditableProperty;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.command.EditArenaCommand;
import net.knarcraft.minigames.container.PlaceholderContainer; import net.knarcraft.minigames.config.MiniGameMessage;
import org.bukkit.Location; import net.knarcraft.minigames.util.InputValidationHelper;
import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.List;
/** /**
* The command for editing an existing dropper arena * The command for editing an existing dropper arena
*/ */
public class EditParkourArenaCommand implements CommandExecutor { public class EditParkourArenaCommand extends EditArenaCommand {
/** /**
* Instantiates a new edit arena command * Instantiates a new edit arena command
*/ */
public EditParkourArenaCommand() { public EditParkourArenaCommand() {
super(null);
} }
@Override @Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) { @NotNull String[] arguments) {
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
if (!(commandSender instanceof Player player)) { if (!(commandSender instanceof Player player)) {
commandSender.sendMessage(Message.ERROR_PLAYER_ONLY.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_PLAYER_ONLY);
return false; return false;
} }
@ -41,29 +39,36 @@ public class EditParkourArenaCommand implements CommandExecutor {
ParkourArena specifiedArena = MiniGames.getInstance().getParkourArenaHandler().getArena(arguments[0]); ParkourArena specifiedArena = MiniGames.getInstance().getParkourArenaHandler().getArena(arguments[0]);
if (specifiedArena == null) { if (specifiedArena == null) {
commandSender.sendMessage(Message.ERROR_ARENA_NOT_FOUND.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_ARENA_NOT_FOUND);
return false; return false;
} }
ParkourArenaEditableProperty editableProperty = ParkourArenaEditableProperty.getFromArgumentString(arguments[1]); ParkourArenaEditableProperty editableProperty = ParkourArenaEditableProperty.getFromArgumentString(arguments[1]);
if (editableProperty == null) { if (editableProperty == null) {
commandSender.sendMessage(Message.ERROR_UNKNOWN_PROPERTY.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_UNKNOWN_PROPERTY);
return false; return false;
} }
if (arguments.length < 3) { if (arguments.length < 3) {
// Print the current value of the property // Print the current value of the property
String value = editableProperty.getCurrentValueAsString(specifiedArena); String value = editableProperty.getCurrentValueAsString(specifiedArena);
commandSender.sendMessage(Message.SUCCESS_CURRENT_VALUE.getMessage(new PlaceholderContainer().add( stringFormatter.displaySuccessMessage(commandSender, stringFormatter.replacePlaceholders(
"{property}", editableProperty.getArgumentString()).add("{value}", value))); MiniGameMessage.SUCCESS_CURRENT_VALUE, new String[]{"{property}", "{value}"},
new String[]{editableProperty.getArgumentString(), value}));
return true; return true;
} else { } else {
boolean successful = changeValue(specifiedArena, editableProperty, arguments[2], player); boolean successful;
try {
successful = changeValue(specifiedArena, editableProperty, arguments[2], player);
} catch (NumberFormatException exception) {
successful = false;
}
if (successful) { if (successful) {
player.sendMessage(Message.SUCCESS_PROPERTY_CHANGED.getMessage("{property}", stringFormatter.displaySuccessMessage(player, stringFormatter.replacePlaceholder(
MiniGameMessage.SUCCESS_PROPERTY_CHANGED, "{property}",
editableProperty.getArgumentString())); editableProperty.getArgumentString()));
} else { } else {
player.sendMessage(Message.ERROR_PROPERTY_INPUT_INVALID.getMessage()); stringFormatter.displayErrorMessage(player, MiniGameMessage.ERROR_PROPERTY_INPUT_INVALID);
} }
return successful; return successful;
} }
@ -77,9 +82,10 @@ public class EditParkourArenaCommand implements CommandExecutor {
* @param value <p>The new value of the property</p> * @param value <p>The new value of the property</p>
* @param player <p>The player trying to change the value</p> * @param player <p>The player trying to change the value</p>
* @return <p>True if the value was successfully changed</p> * @return <p>True if the value was successfully changed</p>
* @throws NumberFormatException <p>If unable to parse a given numeric value</p>
*/ */
private boolean changeValue(@NotNull ParkourArena arena, @NotNull ParkourArenaEditableProperty property, private boolean changeValue(@NotNull ParkourArena arena, @NotNull ParkourArenaEditableProperty property,
@NotNull String value, @NotNull Player player) { @NotNull String value, @NotNull Player player) throws NumberFormatException {
return switch (property) { return switch (property) {
case WIN_BLOCK_TYPE -> arena.setWinBlockType(parseMaterial(value)); case WIN_BLOCK_TYPE -> arena.setWinBlockType(parseMaterial(value));
case SPAWN_LOCATION -> arena.setSpawnLocation(parseLocation(player, value)); case SPAWN_LOCATION -> arena.setSpawnLocation(parseLocation(player, value));
@ -88,42 +94,14 @@ public class EditParkourArenaCommand implements CommandExecutor {
case WIN_LOCATION -> arena.setWinLocation(parseLocation(player, value)); case WIN_LOCATION -> arena.setWinLocation(parseLocation(player, value));
case CHECKPOINT_ADD -> arena.addCheckpoint(parseLocation(player, value)); case CHECKPOINT_ADD -> arena.addCheckpoint(parseLocation(player, value));
case CHECKPOINT_CLEAR -> arena.clearCheckpoints(); case CHECKPOINT_CLEAR -> arena.clearCheckpoints();
case KILL_PLANE_BLOCKS -> arena.setKillPlaneBlocks(new HashSet<>(List.of(value.split(",")))); case KILL_PLANE_BLOCKS -> arena.setKillPlaneBlocks(asSet(value));
case OBSTACLE_BLOCKS -> arena.setObstacleBlocks(asSet(value));
case MAX_PLAYERS -> arena.setMaxPlayers(parseMaxPlayers(value));
case ALLOWED_DAMAGE_CAUSES ->
arena.setAllowedDamageCauses(InputValidationHelper.parseDamageCauses(asSet(value)));
case LOSS_TRIGGER_DAMAGE_CAUSES ->
arena.setLossTriggerDamageCauses(InputValidationHelper.parseDamageCauses(asSet(value)));
}; };
} }
/**
* Parses the given location string
*
* @param player <p>The player changing a location</p>
* @param locationString <p>The location string to parse</p>
* @return <p>The parsed location, or the player's location if not parse-able</p>
*/
private @NotNull Location parseLocation(Player player, String locationString) {
if ((locationString.trim() + ",").matches("([0-9]+.?[0-9]*,){3}")) {
String[] parts = locationString.split(",");
Location newLocation = player.getLocation().clone();
newLocation.setX(Double.parseDouble(parts[0].trim()));
newLocation.setY(Double.parseDouble(parts[1].trim()));
newLocation.setZ(Double.parseDouble(parts[2].trim()));
return newLocation;
} else {
return player.getLocation().clone();
}
}
/**
* Parses the given material name
*
* @param materialName <p>The material name to parse</p>
* @return <p>The parsed material, or AIR if not valid</p>
*/
private @NotNull Material parseMaterial(String materialName) {
Material material = Material.matchMaterial(materialName);
if (material == null) {
material = Material.AIR;
}
return material;
}
} }

View File

@ -1,5 +1,6 @@
package net.knarcraft.minigames.command.parkour; package net.knarcraft.minigames.command.parkour;
import net.knarcraft.minigames.arena.EditablePropertyType;
import net.knarcraft.minigames.arena.parkour.ParkourArenaEditableProperty; import net.knarcraft.minigames.arena.parkour.ParkourArenaEditableProperty;
import net.knarcraft.minigames.util.TabCompleteHelper; import net.knarcraft.minigames.util.TabCompleteHelper;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -11,7 +12,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains; import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingContains;
/** /**
* The tab-completer for the edit arena command * The tab-completer for the edit arena command
@ -30,8 +31,14 @@ public class EditParkourArenaTabCompleter implements TabCompleter {
if (property == null) { if (property == null) {
return new ArrayList<>(); return new ArrayList<>();
} }
return filterMatchingContains(TabCompleteHelper.getTabCompleteSuggestions(property.getPropertyType()), EditablePropertyType propertyType = property.getPropertyType();
arguments[2]);
if (propertyType == EditablePropertyType.MATERIAL_LIST ||
propertyType == EditablePropertyType.DAMAGE_CAUSE_LIST) {
return TabCompleteHelper.getListCompleteSuggestions(propertyType, arguments[2]);
} else {
return filterMatchingContains(TabCompleteHelper.getTabCompleteSuggestions(propertyType), arguments[2]);
}
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -1,13 +1,15 @@
package net.knarcraft.minigames.command.parkour; package net.knarcraft.minigames.command.parkour;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaPlayerRegistry; import net.knarcraft.minigames.arena.ArenaPlayerRegistry;
import net.knarcraft.minigames.arena.parkour.ParkourArena; import net.knarcraft.minigames.arena.parkour.ParkourArena;
import net.knarcraft.minigames.arena.parkour.ParkourArenaGameMode; import net.knarcraft.minigames.arena.parkour.ParkourArenaGameMode;
import net.knarcraft.minigames.arena.parkour.ParkourArenaGroup; import net.knarcraft.minigames.arena.parkour.ParkourArenaGroup;
import net.knarcraft.minigames.arena.parkour.ParkourArenaSession; import net.knarcraft.minigames.arena.parkour.ParkourArenaSession;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.config.ParkourConfiguration; import net.knarcraft.minigames.config.ParkourConfiguration;
import net.knarcraft.minigames.util.GUIHelper;
import net.knarcraft.minigames.util.PlayerTeleporter; import net.knarcraft.minigames.util.PlayerTeleporter;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -23,8 +25,9 @@ public class JoinParkourArenaCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) { @NotNull String[] arguments) {
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
if (!(commandSender instanceof Player player)) { if (!(commandSender instanceof Player player)) {
commandSender.sendMessage(Message.ERROR_PLAYER_ONLY.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_PLAYER_ONLY);
return false; return false;
} }
@ -34,24 +37,32 @@ public class JoinParkourArenaCommand implements CommandExecutor {
// Disallow joining if the player is already in a mini-game arena // Disallow joining if the player is already in a mini-game arena
if (MiniGames.getInstance().getSession(player.getUniqueId()) != null) { if (MiniGames.getInstance().getSession(player.getUniqueId()) != null) {
commandSender.sendMessage(Message.ERROR_ALREADY_PLAYING.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_ALREADY_PLAYING);
return false; return true;
} }
// Make sure the arena exists // Make sure the arena exists
ParkourArena specifiedArena = MiniGames.getInstance().getParkourArenaHandler().getArena(arguments[0]); ParkourArena specifiedArena = MiniGames.getInstance().getParkourArenaHandler().getArena(arguments[0]);
if (specifiedArena == null) { if (specifiedArena == null) {
commandSender.sendMessage(Message.ERROR_ARENA_NOT_FOUND.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_ARENA_NOT_FOUND);
return false; return false;
} }
// Deny vehicles as allowing this is tricky, and will cause problems in some cases // Deny vehicles as allowing this is tricky, and will cause problems in some cases
if (player.isInsideVehicle() || !player.getPassengers().isEmpty()) { if (player.isInsideVehicle() || !player.getPassengers().isEmpty()) {
commandSender.sendMessage(Message.ERROR_JOIN_IN_VEHICLE_OR_PASSENGER.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_JOIN_IN_VEHICLE_OR_PASSENGER);
return false; return true;
} }
return joinArena(specifiedArena, player, arguments); // Deny joining full arenas
int playingNow = MiniGames.getInstance().getParkourArenaPlayerRegistry().getPlayingPlayers(specifiedArena).size();
if (specifiedArena.getMaxPlayers() > 0 && playingNow >= specifiedArena.getMaxPlayers()) {
stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_JOIN_ARENA_FULL);
return true;
}
joinArena(specifiedArena, player, arguments);
return true;
} }
/** /**
@ -60,21 +71,27 @@ public class JoinParkourArenaCommand implements CommandExecutor {
* @param specifiedArena <p>The arena the player wants to join</p> * @param specifiedArena <p>The arena the player wants to join</p>
* @param player <p>The player joining the arena</p> * @param player <p>The player joining the arena</p>
* @param arguments <p>The arguments given</p> * @param arguments <p>The arguments given</p>
* @return <p>Whether the arena was joined successfully</p>
*/ */
private boolean joinArena(ParkourArena specifiedArena, Player player, String[] arguments) { private void joinArena(ParkourArena specifiedArena, Player player, String[] arguments) {
// Find the specified game-mode // Find the specified game-mode
ParkourArenaGameMode gameMode; ParkourArenaGameMode gameMode;
if (arguments.length > 1) { if (arguments.length > 1) {
gameMode = ParkourArenaGameMode.matchGamemode(arguments[1]); gameMode = ParkourArenaGameMode.matchGameMode(arguments[1]);
} else { } else {
gameMode = ParkourArenaGameMode.DEFAULT; gameMode = ParkourArenaGameMode.DEFAULT;
} }
// Don't allow joining the hardcore game-mode if there are no checkpoints to skip
if (specifiedArena.hasNoCheckpoints() && gameMode == ParkourArenaGameMode.HARDCORE) {
MiniGames.getInstance().getStringFormatter().displayErrorMessage(player,
MiniGameMessage.ERROR_HARDCORE_NO_CHECKPOINTS);
return;
}
// Make sure the player has beaten the necessary levels // Make sure the player has beaten the necessary levels
ParkourArenaGroup arenaGroup = MiniGames.getInstance().getParkourArenaHandler().getGroup(specifiedArena.getArenaId()); ParkourArenaGroup arenaGroup = MiniGames.getInstance().getParkourArenaHandler().getGroup(specifiedArena.getArenaId());
if (arenaGroup != null && !doGroupChecks(specifiedArena, arenaGroup, gameMode, player)) { if (arenaGroup != null && !doGroupChecks(specifiedArena, arenaGroup, gameMode, player)) {
return false; return;
} }
// Register the player's session // Register the player's session
@ -82,17 +99,22 @@ public class JoinParkourArenaCommand implements CommandExecutor {
ArenaPlayerRegistry<ParkourArena> playerRegistry = MiniGames.getInstance().getParkourArenaPlayerRegistry(); ArenaPlayerRegistry<ParkourArena> playerRegistry = MiniGames.getInstance().getParkourArenaPlayerRegistry();
playerRegistry.registerPlayer(player.getUniqueId(), newSession); playerRegistry.registerPlayer(player.getUniqueId(), newSession);
// Update visibility and hit-box for the player
MiniGames.getInstance().getPlayerVisibilityManager().updateHiddenPlayers(playerRegistry, player);
// Try to teleport the player to the arena // Try to teleport the player to the arena
boolean teleported = PlayerTeleporter.teleportPlayer(player, specifiedArena.getSpawnLocation(), false, false); boolean teleported = PlayerTeleporter.teleportPlayer(player, specifiedArena.getSpawnLocation(), false, false);
if (!teleported) { if (!teleported) {
player.sendMessage(Message.ERROR_ARENA_TELEPORT_FAILED.getMessage()); MiniGames.getInstance().getStringFormatter().displayErrorMessage(player,
newSession.triggerQuit(false); MiniGameMessage.ERROR_ARENA_TELEPORT_FAILED);
return false; newSession.triggerQuit(false, true);
} else { } else {
// Make sure to update the state again in the air to remove a potential swimming state // Update the player's state to follow the arena's rules
newSession.getEntryState().setArenaState(); newSession.getEntryState().setArenaState();
player.sendMessage(Message.SUCCESS_ARENA_JOINED.getMessage());
return true; player.getInventory().addItem(GUIHelper.getGUIOpenItem(player));
MiniGames.getInstance().getStringFormatter().displaySuccessMessage(player,
MiniGameMessage.SUCCESS_ARENA_JOINED);
} }
} }
@ -112,7 +134,8 @@ public class JoinParkourArenaCommand implements CommandExecutor {
// Require that the player has beaten the previous arena on the same game-mode before trying this one // Require that the player has beaten the previous arena on the same game-mode before trying this one
if (configuration.mustDoGroupedInSequence() && if (configuration.mustDoGroupedInSequence() &&
arenaGroup.cannotPlay(arenaGameMode, player, parkourArena.getArenaId())) { arenaGroup.cannotPlay(arenaGameMode, player, parkourArena.getArenaId())) {
player.sendMessage(Message.ERROR_PREVIOUS_ARENA_REQUIRED.getMessage()); MiniGames.getInstance().getStringFormatter().displayErrorMessage(player,
MiniGameMessage.ERROR_PREVIOUS_ARENA_REQUIRED);
return false; return false;
} }

View File

@ -1,6 +1,7 @@
package net.knarcraft.minigames.command.parkour; package net.knarcraft.minigames.command.parkour;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.util.TabCompleteHelper; import net.knarcraft.minigames.util.TabCompleteHelper;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -19,11 +20,12 @@ public class ListParkourArenaCommand implements TabExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] arguments) { @NotNull String[] arguments) {
StringBuilder builder = new StringBuilder(Message.SUCCESS_PARKOUR_ARENAS_LIST.getMessage()); StringBuilder builder = new StringBuilder(MiniGames.getInstance().getTranslator().getTranslatedMessage(
MiniGameMessage.SUCCESS_PARKOUR_ARENAS_LIST));
for (String arenaName : TabCompleteHelper.getParkourArenas()) { for (String arenaName : TabCompleteHelper.getParkourArenas()) {
builder.append("\n").append(arenaName); builder.append("\n").append(arenaName);
} }
sender.sendMessage(builder.toString()); MiniGames.getInstance().getStringFormatter().displaySuccessMessage(sender, builder.toString());
return true; return true;
} }

View File

@ -4,24 +4,17 @@ import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.parkour.ParkourArena; import net.knarcraft.minigames.arena.parkour.ParkourArena;
import net.knarcraft.minigames.arena.parkour.ParkourArenaGroup; import net.knarcraft.minigames.arena.parkour.ParkourArenaGroup;
import net.knarcraft.minigames.arena.parkour.ParkourArenaHandler; import net.knarcraft.minigames.arena.parkour.ParkourArenaHandler;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.command.GroupListCommand;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains;
/** /**
* The command for listing groups and the stages within * The command for listing groups and the stages within
*/ */
public class ParkourGroupListCommand implements TabExecutor { public class ParkourGroupListCommand extends GroupListCommand<ParkourArenaHandler, ParkourArena, ParkourArenaGroup> {
@Override @Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@ -37,62 +30,9 @@ public class ParkourGroupListCommand implements TabExecutor {
} }
} }
/**
* Displays all currently existing parkour arena groups
*
* @param arenaHandler <p>The arena handler to get groups from</p>
* @param sender <p>The command sender to display the groups to</p>
*/
private void displayExistingGroups(@NotNull ParkourArenaHandler arenaHandler, @NotNull CommandSender sender) {
StringBuilder builder = new StringBuilder("Parkour arena groups:").append("\n");
arenaHandler.getAllGroups().stream().sorted().forEachOrdered((group) ->
builder.append(group.getGroupName()).append("\n"));
sender.sendMessage(builder.toString());
}
/**
* Displays the ordered stages in a specified group to the specified command sender
*
* @param arenaHandler <p>The arena handler to get groups from</p>
* @param sender <p>The command sender to display the stages to</p>
* @param groupName <p>The name of the group to display stages for</p>
* @return <p>True if the stages were successfully displayed</p>
*/
private boolean displayOrderedArenaNames(@NotNull ParkourArenaHandler arenaHandler, @NotNull CommandSender sender,
@NotNull String groupName) {
ParkourArenaGroup arenaGroup = arenaHandler.getGroup(groupName);
if (arenaGroup == null) {
sender.sendMessage(Message.ERROR_GROUP_NOT_FOUND.getMessage());
return false;
}
// Send a list of all stages (arenas in the group)
StringBuilder builder = new StringBuilder(Message.SUCCESS_GROUP_STAGES.getMessage("{group}", groupName));
int counter = 1;
for (UUID arenaId : arenaGroup.getArenas()) {
ParkourArena arena = arenaHandler.getArena(arenaId);
if (arena != null) {
builder.append("\n").append(counter++).append(". ").append(arena.getArenaName());
}
}
sender.sendMessage(builder.toString());
return true;
}
@Nullable
@Override @Override
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, protected Set<ParkourArenaGroup> getGroups() {
@NotNull String[] arguments) { return MiniGames.getInstance().getParkourArenaHandler().getAllGroups();
if (arguments.length == 1) {
List<String> groupNames = new ArrayList<>();
Set<ParkourArenaGroup> arenaGroups = MiniGames.getInstance().getParkourArenaHandler().getAllGroups();
for (ParkourArenaGroup group : arenaGroups) {
groupNames.add(group.getGroupName());
}
return filterMatchingContains(groupNames, arguments[0]);
} else {
return new ArrayList<>();
}
} }
} }

View File

@ -1,10 +1,12 @@
package net.knarcraft.minigames.command.parkour; package net.knarcraft.minigames.command.parkour;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.parkour.ParkourArena; import net.knarcraft.minigames.arena.parkour.ParkourArena;
import net.knarcraft.minigames.arena.parkour.ParkourArenaGroup; import net.knarcraft.minigames.arena.parkour.ParkourArenaGroup;
import net.knarcraft.minigames.arena.parkour.ParkourArenaHandler; import net.knarcraft.minigames.arena.parkour.ParkourArenaHandler;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.util.InputValidationHelper;
import net.knarcraft.minigames.util.StringSanitizer; import net.knarcraft.minigames.util.StringSanitizer;
import net.knarcraft.minigames.util.TabCompleteHelper; import net.knarcraft.minigames.util.TabCompleteHelper;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -16,7 +18,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains; import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingContains;
/** /**
* The command for setting the group of an arena * The command for setting the group of an arena
@ -30,11 +32,12 @@ public class ParkourGroupSetCommand implements TabExecutor {
return false; return false;
} }
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
ParkourArenaHandler arenaHandler = MiniGames.getInstance().getParkourArenaHandler(); ParkourArenaHandler arenaHandler = MiniGames.getInstance().getParkourArenaHandler();
ParkourArena specifiedArena = arenaHandler.getArena(arguments[0]); ParkourArena specifiedArena = arenaHandler.getArena(arguments[0]);
if (specifiedArena == null) { if (specifiedArena == null) {
commandSender.sendMessage(Message.ERROR_ARENA_NOT_FOUND.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_ARENA_NOT_FOUND);
return false; return false;
} }
@ -45,7 +48,7 @@ public class ParkourGroupSetCommand implements TabExecutor {
} }
ParkourArenaGroup arenaGroup; ParkourArenaGroup arenaGroup;
if (groupName.equalsIgnoreCase("null") || groupName.equalsIgnoreCase("none")) { if (InputValidationHelper.isEmptyValue(groupName)) {
arenaGroup = null; arenaGroup = null;
} else { } else {
arenaGroup = arenaHandler.getGroup(groupName); arenaGroup = arenaHandler.getGroup(groupName);
@ -56,7 +59,7 @@ public class ParkourGroupSetCommand implements TabExecutor {
arenaHandler.setGroup(specifiedArena.getArenaId(), arenaGroup); arenaHandler.setGroup(specifiedArena.getArenaId(), arenaGroup);
commandSender.sendMessage(Message.SUCCESS_ARENA_GROUP_UPDATED.getMessage()); stringFormatter.displaySuccessMessage(commandSender, MiniGameMessage.SUCCESS_ARENA_GROUP_UPDATED);
return true; return true;
} }

View File

@ -1,10 +1,11 @@
package net.knarcraft.minigames.command.parkour; package net.knarcraft.minigames.command.parkour;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.parkour.ParkourArena; import net.knarcraft.minigames.arena.parkour.ParkourArena;
import net.knarcraft.minigames.arena.parkour.ParkourArenaGroup; import net.knarcraft.minigames.arena.parkour.ParkourArenaGroup;
import net.knarcraft.minigames.arena.parkour.ParkourArenaHandler; import net.knarcraft.minigames.arena.parkour.ParkourArenaHandler;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor; import org.bukkit.command.TabExecutor;
@ -15,7 +16,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains; import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingContains;
/** /**
* The command for swapping the order of two arenas in a group * The command for swapping the order of two arenas in a group
@ -30,29 +31,30 @@ public class ParkourGroupSwapCommand implements TabExecutor {
} }
ParkourArenaHandler arenaHandler = MiniGames.getInstance().getParkourArenaHandler(); ParkourArenaHandler arenaHandler = MiniGames.getInstance().getParkourArenaHandler();
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
ParkourArena arena1 = arenaHandler.getArena(arguments[0]); ParkourArena arena1 = arenaHandler.getArena(arguments[0]);
if (arena1 == null) { if (arena1 == null) {
commandSender.sendMessage(Message.ERROR_ARENA_1_NOT_FOUND.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_ARENA_1_NOT_FOUND);
return false; return false;
} }
ParkourArena arena2 = arenaHandler.getArena(arguments[1]); ParkourArena arena2 = arenaHandler.getArena(arguments[1]);
if (arena2 == null) { if (arena2 == null) {
commandSender.sendMessage(Message.ERROR_ARENA_2_NOT_FOUND.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_ARENA_2_NOT_FOUND);
return false; return false;
} }
ParkourArenaGroup arena1Group = arenaHandler.getGroup(arena1.getArenaId()); ParkourArenaGroup arena1Group = arenaHandler.getGroup(arena1.getArenaId());
ParkourArenaGroup arena2Group = arenaHandler.getGroup(arena2.getArenaId()); ParkourArenaGroup arena2Group = arenaHandler.getGroup(arena2.getArenaId());
if (arena1Group == null || !arena1Group.equals(arena2Group)) { if (arena1Group == null || !arena1Group.equals(arena2Group)) {
commandSender.sendMessage(Message.ERROR_SWAP_DIFFERENT_GROUPS.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_SWAP_DIFFERENT_GROUPS);
return false; return false;
} }
arena1Group.swapArenas(arena1Group.getArenas().indexOf(arena1.getArenaId()), arena1Group.swapArenas(arena1Group.getArenas().indexOf(arena1.getArenaId()),
arena1Group.getArenas().indexOf(arena2.getArenaId())); arena1Group.getArenas().indexOf(arena2.getArenaId()));
commandSender.sendMessage(Message.SUCCESS_ARENAS_SWAPPED.getMessage()); stringFormatter.displaySuccessMessage(commandSender, MiniGameMessage.SUCCESS_ARENAS_SWAPPED);
return true; return true;
} }

View File

@ -1,8 +1,9 @@
package net.knarcraft.minigames.command.parkour; package net.knarcraft.minigames.command.parkour;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.parkour.ParkourArena; import net.knarcraft.minigames.arena.parkour.ParkourArena;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -21,16 +22,18 @@ public class RemoveParkourArenaCommand implements CommandExecutor {
return false; return false;
} }
StringFormatter stringFormatter = MiniGames.getInstance().getStringFormatter();
// Get the specified arena // Get the specified arena
ParkourArena targetArena = MiniGames.getInstance().getParkourArenaHandler().getArena(arguments[0]); ParkourArena targetArena = MiniGames.getInstance().getParkourArenaHandler().getArena(arguments[0]);
if (targetArena == null) { if (targetArena == null) {
commandSender.sendMessage(Message.ERROR_ARENA_NOT_FOUND.getMessage()); stringFormatter.displayErrorMessage(commandSender, MiniGameMessage.ERROR_ARENA_NOT_FOUND);
return false; return false;
} }
// Remove the arena // Remove the arena
MiniGames.getInstance().getParkourArenaHandler().removeArena(targetArena); MiniGames.getInstance().getParkourArenaHandler().removeArena(targetArena);
commandSender.sendMessage(Message.SUCCESS_ARENA_REMOVED.getMessage()); stringFormatter.displaySuccessMessage(commandSender, MiniGameMessage.SUCCESS_ARENA_REMOVED);
return true; return true;
} }

View File

@ -10,7 +10,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains; import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingContains;
/** /**
* The tab-completer for the remove arena command * The tab-completer for the remove arena command

View File

@ -19,8 +19,6 @@ public class DropperConfiguration extends MiniGameConfiguration {
private boolean mustDoGroupedInSequence; private boolean mustDoGroupedInSequence;
private boolean ignoreRecordsUntilGroupBeatenOnce; private boolean ignoreRecordsUntilGroupBeatenOnce;
private boolean mustDoNormalModeFirst; private boolean mustDoNormalModeFirst;
private boolean makePlayersInvisible;
private boolean disableHitCollision;
private boolean blockSneaking; private boolean blockSneaking;
private boolean blockSprinting; private boolean blockSprinting;
private Set<Material> blockWhitelist; private Set<Material> blockWhitelist;
@ -97,24 +95,6 @@ public class DropperConfiguration extends MiniGameConfiguration {
return this.ignoreRecordsUntilGroupBeatenOnce; return this.ignoreRecordsUntilGroupBeatenOnce;
} }
/**
* Gets whether players should be made invisible while in an arena
*
* @return <p>Whether players should be made invisible</p>
*/
public boolean makePlayersInvisible() {
return this.makePlayersInvisible;
}
/**
* Gets whether entity hit-collision of players in an arena should be disabled
*
* @return <p>Whether to disable hit collision</p>
*/
public boolean disableHitCollision() {
return this.disableHitCollision;
}
/** /**
* Gets whether players trying to sneak while in a dropper arena to increase their downwards speed should be blocked * Gets whether players trying to sneak while in a dropper arena to increase their downwards speed should be blocked
* *
@ -141,8 +121,6 @@ public class DropperConfiguration extends MiniGameConfiguration {
this.mustDoGroupedInSequence = configuration.getBoolean(rootNode + "mustDoGroupedInSequence", true); this.mustDoGroupedInSequence = configuration.getBoolean(rootNode + "mustDoGroupedInSequence", true);
this.ignoreRecordsUntilGroupBeatenOnce = configuration.getBoolean(rootNode + "ignoreRecordsUntilGroupBeatenOnce", false); this.ignoreRecordsUntilGroupBeatenOnce = configuration.getBoolean(rootNode + "ignoreRecordsUntilGroupBeatenOnce", false);
this.mustDoNormalModeFirst = configuration.getBoolean(rootNode + "mustDoNormalModeFirst", true); this.mustDoNormalModeFirst = configuration.getBoolean(rootNode + "mustDoNormalModeFirst", true);
this.makePlayersInvisible = configuration.getBoolean(rootNode + "makePlayersInvisible", false);
this.disableHitCollision = configuration.getBoolean(rootNode + "disableHitCollision", true);
this.blockSprinting = configuration.getBoolean(rootNode + "blockSprinting", true); this.blockSprinting = configuration.getBoolean(rootNode + "blockSprinting", true);
this.blockSneaking = configuration.getBoolean(rootNode + "blockSneaking", true); this.blockSneaking = configuration.getBoolean(rootNode + "blockSneaking", true);
this.blockWhitelist = loadMaterialList(rootNode + "blockWhitelist"); this.blockWhitelist = loadMaterialList(rootNode + "blockWhitelist");
@ -176,8 +154,6 @@ public class DropperConfiguration extends MiniGameConfiguration {
"\n" + "Must do groups in sequence: " + mustDoGroupedInSequence + "\n" + "Must do groups in sequence: " + mustDoGroupedInSequence +
"\n" + "Ignore records until group beaten once: " + ignoreRecordsUntilGroupBeatenOnce + "\n" + "Ignore records until group beaten once: " + ignoreRecordsUntilGroupBeatenOnce +
"\n" + "Must do normal mode first: " + mustDoNormalModeFirst + "\n" + "Must do normal mode first: " + mustDoNormalModeFirst +
"\n" + "Make players invisible: " + makePlayersInvisible +
"\n" + "Disable hit collision: " + disableHitCollision +
"\n" + "Block whitelist: "); "\n" + "Block whitelist: ");
for (Material material : blockWhitelist) { for (Material material : blockWhitelist) {
builder.append("\n - ").append(material.name()); builder.append("\n - ").append(material.name());

View File

@ -1,300 +0,0 @@
package net.knarcraft.minigames.config;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.container.PlaceholderContainer;
import net.knarcraft.minigames.util.ColorHelper;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
/**
* A message which ca be displayed to the user
*/
public enum Message {
/* ************** *
* Error messages *
* ************** */
/**
* The message displayed if saving arena groups fails
*/
ERROR_CANNOT_SAVE_ARENA_GROUPS("&cUnable to save current arena groups! Data loss can occur!"),
/**
* The message displayed if an un-parse-able message is given by a user
*/
ERROR_MATERIAL_NOT_PARSE_ABLE("&cUnable to parse material: {material}"),
/**
* The message displayed if a player tries to be teleported to/from an arena with a passenger
*/
ERROR_TELEPORT_WITH_PASSENGER("&cYou cannot be teleported with a passenger!"),
/**
* The message displayed if a player tries to be teleported to/from an arena with a vehicle
*/
ERROR_TELEPORT_IN_VEHICLE("&cYou cannot be teleported while in a vehicle"),
/**
* The message displayed if an arena cannot be loaded
*/
ERROR_ARENA_NOT_LOADED("&cCould not load the arena at configuration section {section}. Please check " +
"the {file} storage file for issues."),
/**
* The message displayed if an arena's data cannot be loaded
*/
ERROR_ARENA_DATA_NOT_LOADED("&cUnable to load arena data for dropper arena: {arena}"),
/**
* The message displayed if the user specifies an unrecognized arena
*/
ERROR_ARENA_NOT_FOUND("&cUnable to find the specified arena."),
/**
* The message displayed if the user specifies an unrecognized group
*/
ERROR_GROUP_NOT_FOUND("&cUnable to find the specified group!"),
/**
* The message displayed if the console tries to execute a player-only command
*/
ERROR_PLAYER_ONLY("&cThis command must be used by a player"),
/**
* The message displayed if the name of an arena is duplicated
*/
ERROR_ARENA_NAME_COLLISION("&cThere already exists an arena with that name!"),
/**
* The message displayed if the player is required to win on the default difficulty first
*/
ERROR_NORMAL_MODE_REQUIRED("&cYou must complete this arena in normal mode first!"),
/**
* The message displayed if the player is required to win on the default difficulty for all arenas in the group first
*/
ERROR_GROUP_NORMAL_MODE_REQUIRED("&cYou have not yet beaten the default game-mode for all arenas in this group!"),
/**
* The message displayed if the player is required to beat the previous arena in the group
*/
ERROR_PREVIOUS_ARENA_REQUIRED("&cYou have not yet beaten the previous arena!"),
/**
* The message displayed if player teleportation failed for some reason
*/
ERROR_ARENA_TELEPORT_FAILED("&cUnable to teleport you to the arena."),
/**
* The message displayed if the player tries to quit the arena while not in an arena
*/
ERROR_NOT_IN_ARENA("&cYou are not in a mini-games arena!"),
/**
* The message displayed if the player tries to join an arena while already playing
*
* <p>This should in theory be impossible, as players cannot use any commands except /miniGamesLeave while playing
* in an arena.</p>
*/
ERROR_ALREADY_PLAYING("&cYou are already playing a mini-game!"),
/**
* The message displayed if a player tries to join an arena with a passenger or riding a vehicle
*/
ERROR_JOIN_IN_VEHICLE_OR_PASSENGER("&cYou cannot join an arena while inside a vehicle or carrying a passenger."),
/**
* The message displayed if the player tries to change an unrecognized arena property
*/
ERROR_UNKNOWN_PROPERTY("&cUnknown property specified."),
/**
* The message displayed if the given input to /dEdit or /pEdit's value is invalid
*/
ERROR_PROPERTY_INPUT_INVALID("&cUnable to change the property. Make sure your input is valid!"),
/**
* The message displayed if the first arena specified in /dgSwap or /pgSwap is invalid
*/
ERROR_ARENA_1_NOT_FOUND("&cUnable to find the first specified arena."),
/**
* The message displayed if the second arena specified in /dgSwap or /pgSwap is invalid
*/
ERROR_ARENA_2_NOT_FOUND("&cUnable to find the second specified dropper arena."),
/**
* The message displayed if the two groups specified for /dgSwap or /pgSwap are in different arenas
*/
ERROR_SWAP_DIFFERENT_GROUPS("&cYou cannot swap arenas in different groups!"),
/**
* The message displayed if a player tries to use any command other than /mLeave while in an arena
*/
ERROR_ILLEGAL_COMMAND("&cYou cannot use that command while in an arena!"),
/* **************** *
* Success messages *
* **************** */
/**
* The message displayed if an arena's group has been changed
*/
SUCCESS_ARENA_GROUP_UPDATED("&aThe arena's group has been updated"),
/**
* The message displayed if the MiniGames plugin is reloaded
*/
SUCCESS_PLUGIN_RELOADED("&aPlugin reloaded!"),
/**
* The message displayed if a new arena has been created
*/
SUCCESS_ARENA_CREATED("&aThe arena was successfully created!"),
/**
* The message displayed if a player clears/wins an arena for the first time
*/
SUCCESS_ARENA_FIRST_CLEAR("&aYou cleared the arena!"),
/**
* The message displayed when a player wins an arena
*/
SUCCESS_ARENA_WIN("&aYou won!"),
/**
* The message displayed when a player quits an arena
*/
SUCCESS_ARENA_QUIT("&aYou quit the arena!"),
/**
* The message used to display the current value of an arena property
*/
SUCCESS_CURRENT_VALUE("&aCurrent value of {property} is: {value}"),
/**
* The message used to announce that an arena property has been changed
*/
SUCCESS_PROPERTY_CHANGED("&aProperty {property} successfully changed"),
/**
* The message displayed when two arenas' order in a group have been swapped
*/
SUCCESS_ARENAS_SWAPPED("&aThe arenas have been swapped!"),
/**
* The message displayed when an arena has been removed
*/
SUCCESS_ARENA_REMOVED("&aThe specified arena has been successfully removed"),
/**
* The header displayed before listing all dropper arenas
*/
SUCCESS_DROPPER_ARENAS_LIST("&aDropper arenas:&r"),
/**
* The header displayed before listing all parkour arenas
*/
SUCCESS_PARKOUR_ARENAS_LIST("&aParkour arenas:&r"),
/**
* The message displayed when a player reaches a new checkpoint in a parkour arena
*/
SUCCESS_CHECKPOINT_REACHED("&aCheckpoint reached!"),
/**
* The header displayed before listing all arenas (stages) in a group
*/
SUCCESS_GROUP_STAGES("&a{group}'s stages:&r"),
/**
* The message displayed when a new record has been achieved
*/
SUCCESS_RECORD_ACHIEVED("&aYou just set a {recordInfo} on the {gameMode} game-mode!"),
/**
* The partial message used to describe that the player achieved a world record
*/
RECORD_ACHIEVED_GLOBAL("new {recordType} record"),
/**
* The partial message used to describe that the player achieved a personal best record
*/
RECORD_ACHIEVED_PERSONAL("personal {recordType} record"),
/**
* The message displayed when a player joins an arena
*/
SUCCESS_ARENA_JOINED("&aYou joined the arena."),
;
private final @NotNull String defaultMessage;
/**
* Instantiates a new message
*
* @param defaultMessage <p>The default value of the message</p>
*/
Message(@NotNull String defaultMessage) {
this.defaultMessage = defaultMessage;
}
/**
* Gets the message this enum represents
*
* @return <p>The formatted message</p>
*/
public @NotNull String getMessage() {
return formatMessage(this.defaultMessage);
}
/**
* Gets the message this enum represents
*
* @param placeholder <p>The placeholder to replace</p>
* @param replacement <p>The replacement to use</p>
* @return <p>The formatted message</p>
*/
public @NotNull String getMessage(@NotNull String placeholder, @NotNull String replacement) {
return formatMessage(this.defaultMessage.replace(placeholder, replacement));
}
/**
* Gets the message this enum represents, intended for display within another message
*
* @param placeholder <p>The placeholder to replace</p>
* @param replacement <p>The replacement to use</p>
* @return <p>The formatted message</p>
*/
public @NotNull String getPartialMessage(@NotNull String placeholder, @NotNull String replacement) {
return ColorHelper.translateAllColorCodes(this.defaultMessage.replace(placeholder, replacement));
}
/**
* Gets the message this enum represents
*
* @param placeholders <p>The placeholder -> replacement map specifying necessary replacements</p>
* @return <p>The formatted message</p>
*/
public @NotNull String getMessage(@NotNull PlaceholderContainer placeholders) {
String replaced = this.defaultMessage;
for (Map.Entry<String, String> entry : placeholders.getPlaceholders().entrySet()) {
replaced = replaced.replace(entry.getKey(), entry.getValue());
}
return formatMessage(replaced);
}
/**
* Gets the formatted version of the given message
*
* @param message <p>The message to format</p>
* @return <p>The formatted message</p>
*/
private @NotNull String formatMessage(@NotNull String message) {
String prefix = MiniGames.getInstance().getDescription().getPrefix();
return ColorHelper.translateAllColorCodes("#546EED[&r&l" + prefix + "#546EED]&r " + message);
}
}

View File

@ -1,6 +1,7 @@
package net.knarcraft.minigames.config; package net.knarcraft.minigames.config;
import net.knarcraft.minigames.util.MaterialHelper; import net.knarcraft.knarlib.util.MaterialHelper;
import net.knarcraft.minigames.MiniGames;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -46,7 +47,7 @@ public abstract class MiniGameConfiguration {
*/ */
public @NotNull Set<Material> loadMaterialList(@NotNull String path) { public @NotNull Set<Material> loadMaterialList(@NotNull String path) {
List<?> blockWhitelist = configuration.getList(path, new ArrayList<>()); List<?> blockWhitelist = configuration.getList(path, new ArrayList<>());
return MaterialHelper.loadMaterialList(blockWhitelist); return MaterialHelper.loadMaterialList(blockWhitelist, "+", MiniGames.getInstance().getLogger());
} }
} }

View File

@ -0,0 +1,318 @@
package net.knarcraft.minigames.config;
import net.knarcraft.knarlib.formatting.TranslatableMessage;
import org.jetbrains.annotations.NotNull;
/**
* A message which ca be displayed to the user
*/
public enum MiniGameMessage implements TranslatableMessage {
/* ************** *
* Error messages *
* ************** */
/**
* The message displayed if saving arena groups fails
*/
ERROR_CANNOT_SAVE_ARENA_GROUPS,
/**
* The message displayed if an un-parse-able message is given by a user
*/
ERROR_MATERIAL_NOT_PARSE_ABLE,
/**
* The message displayed if a player tries to be teleported to/from an arena with a passenger
*/
ERROR_TELEPORT_WITH_PASSENGER,
/**
* The message displayed if a player tries to be teleported to/from an arena with a vehicle
*/
ERROR_TELEPORT_IN_VEHICLE,
/**
* The message displayed if an arena cannot be loaded
*/
ERROR_ARENA_NOT_LOADED,
/**
* The message displayed if an arena's data cannot be loaded
*/
ERROR_ARENA_DATA_NOT_LOADED,
/**
* The message displayed if the user specifies an unrecognized arena
*/
ERROR_ARENA_NOT_FOUND,
/**
* The message displayed if the user specifies an unrecognized group
*/
ERROR_GROUP_NOT_FOUND,
/**
* The message displayed if the console tries to execute a player-only command
*/
ERROR_PLAYER_ONLY,
/**
* The message displayed if the name of an arena is duplicated
*/
ERROR_ARENA_NAME_COLLISION,
/**
* The message displayed if the player is required to win on the default difficulty first
*/
ERROR_NORMAL_MODE_REQUIRED,
/**
* The message displayed if the player is required to win on the default difficulty for all arenas in the group first
*/
ERROR_GROUP_NORMAL_MODE_REQUIRED,
/**
* The message displayed if the player is required to beat the previous arena in the group
*/
ERROR_PREVIOUS_ARENA_REQUIRED,
/**
* The message displayed if player teleportation failed for some reason
*/
ERROR_ARENA_TELEPORT_FAILED,
/**
* The message displayed if the player tries to quit the arena while not in an arena
*/
ERROR_NOT_IN_ARENA,
/**
* The message displayed if the player tries to join an arena while already playing
*
* <p>This should in theory be impossible, as players cannot use any commands except /miniGamesLeave while playing
* in an arena.</p>
*/
ERROR_ALREADY_PLAYING,
/**
* The message displayed if a player tries to join an arena with a passenger or riding a vehicle
*/
ERROR_JOIN_IN_VEHICLE_OR_PASSENGER,
/**
* The message displayed if the player tries to change an unrecognized arena property
*/
ERROR_UNKNOWN_PROPERTY,
/**
* The message displayed if the given input to /dEdit or /pEdit's value is invalid
*/
ERROR_PROPERTY_INPUT_INVALID,
/**
* The message displayed if the first arena specified in /dgSwap or /pgSwap is invalid
*/
ERROR_ARENA_1_NOT_FOUND,
/**
* The message displayed if the second arena specified in /dgSwap or /pgSwap is invalid
*/
ERROR_ARENA_2_NOT_FOUND,
/**
* The message displayed if the two groups specified for /dgSwap or /pgSwap are in different arenas
*/
ERROR_SWAP_DIFFERENT_GROUPS,
/**
* The message displayed if a player tries to use any command other than /mLeave while in an arena
*/
ERROR_ILLEGAL_COMMAND,
/**
* The message displayed if the player is trying to join a parkour arena on hardcore which has no checkpoints
*/
ERROR_HARDCORE_NO_CHECKPOINTS,
/**
* The message displayed if a user specifies an invalid material
*/
ERROR_INVALID_MATERIAL,
/**
* The message displayed if a user specifies an invalid world
*/
ERROR_INVALID_WORLD,
/**
* The message displayed if a user specifies an invalid number
*/
ERROR_INVALID_NUMBER,
/**
* The message displayed if a user specifies an invalid command (for a command reward)
*/
ERROR_INVALID_COMMAND_STRING,
/**
* The message displayed if a user specified an invalid reward type
*/
ERROR_REWARD_TYPE_INVALID,
/**
* The message displayed if a user specified an invalid reward condition
*/
ERROR_REWARD_CONDITION_INVALID,
/**
* The message displayed when a geyser player tries to join a dropper arena
*/
ERROR_GEYSER_DROPPER,
/**
* The message displayed when a player attempts to join a full arena
*/
ERROR_JOIN_ARENA_FULL,
/* **************** *
* Success messages *
* **************** */
/**
* The message displayed if an arena's group has been changed
*/
SUCCESS_ARENA_GROUP_UPDATED,
/**
* The message displayed if the MiniGames plugin is reloaded
*/
SUCCESS_PLUGIN_RELOADED,
/**
* The message displayed if a new arena has been created
*/
SUCCESS_ARENA_CREATED,
/**
* The message displayed if a player clears/wins an arena for the first time
*/
SUCCESS_ARENA_FIRST_CLEAR,
/**
* The message displayed when a player wins an arena
*/
SUCCESS_ARENA_WIN,
/**
* The message displayed when a player quits an arena
*/
SUCCESS_ARENA_QUIT,
/**
* The message used to display the current value of an arena property
*/
SUCCESS_CURRENT_VALUE,
/**
* The message used to announce that an arena property has been changed
*/
SUCCESS_PROPERTY_CHANGED,
/**
* The message displayed when two arenas' order in a group have been swapped
*/
SUCCESS_ARENAS_SWAPPED,
/**
* The message displayed when an arena has been removed
*/
SUCCESS_ARENA_REMOVED,
/**
* The header displayed before listing all dropper arenas
*/
SUCCESS_DROPPER_ARENAS_LIST,
/**
* The header displayed before listing all parkour arenas
*/
SUCCESS_PARKOUR_ARENAS_LIST,
/**
* The message displayed when a player reaches a new checkpoint in a parkour arena
*/
SUCCESS_CHECKPOINT_REACHED,
/**
* The header displayed before listing all arenas (stages) in a group
*/
SUCCESS_GROUP_STAGES,
/**
* The message displayed when a new record has been achieved
*/
SUCCESS_RECORD_ACHIEVED,
/**
* The partial message used to describe that the player achieved a world record
*/
RECORD_ACHIEVED_GLOBAL,
/**
* The partial message used to describe that the player achieved a personal best record
*/
RECORD_ACHIEVED_PERSONAL,
/**
* The message displayed when a player joins an arena
*/
SUCCESS_ARENA_JOINED,
/**
* The message displayed when a player is rewarded with an item
*/
SUCCESS_ITEM_REWARDED,
/**
* The message displayed when a player is rewarded with a permission
*/
SUCCESS_PERMISSION_REWARDED,
/**
* The message displayed when a player is rewarded with a permission, for a specific world
*/
SUCCESS_PERMISSION_REWARDED_WORLD,
/**
* The message displayed when a player is rewarded by a command being run
*/
SUCCESS_COMMAND_REWARDED,
/**
* The message displayed when a player is rewarded with an amount of currency
*/
SUCCESS_ECONOMY_REWARDED,
/**
* The message displayed when an arena reward has been successfully added
*/
SUCCESS_REWARD_ADDED,
/**
* The message displayed when arena rewards have been cleared
*/
SUCCESS_REWARDS_CLEARED,
/**
* The message displayed when listing arena groups
*/
SUCCESS_GROUPS,
;
@Override
public @NotNull TranslatableMessage[] getAllMessages() {
return MiniGameMessage.values();
}
}

View File

@ -16,8 +16,8 @@ public class ParkourConfiguration extends MiniGameConfiguration {
private boolean enforceCheckpointOrder; private boolean enforceCheckpointOrder;
private boolean mustDoGroupedInSequence; private boolean mustDoGroupedInSequence;
private boolean ignoreRecordsUntilGroupBeatenOnce; private boolean ignoreRecordsUntilGroupBeatenOnce;
private boolean makePlayersInvisible;
private Set<Material> killPlaneBlocks; private Set<Material> killPlaneBlocks;
private Set<Material> obstacleBlocks;
/** /**
* Instantiates a new dropper configuration * Instantiates a new dropper configuration
@ -55,15 +55,6 @@ public class ParkourConfiguration extends MiniGameConfiguration {
return this.ignoreRecordsUntilGroupBeatenOnce; return this.ignoreRecordsUntilGroupBeatenOnce;
} }
/**
* Gets whether players should be made invisible while in an arena
*
* @return <p>Whether players should be made invisible</p>
*/
public boolean makePlayersInvisible() {
return this.makePlayersInvisible;
}
/** /**
* Gets all types of blocks constituting parkour arenas' kill planes * Gets all types of blocks constituting parkour arenas' kill planes
* *
@ -73,13 +64,22 @@ public class ParkourConfiguration extends MiniGameConfiguration {
return new HashSet<>(this.killPlaneBlocks); return new HashSet<>(this.killPlaneBlocks);
} }
/**
* Gets all types of blocks constituting parkour arena's obstacle blocks
*
* @return <p>The types of blocks constituting parkour arena's obstacle blocks</p>
*/
public Set<Material> getObstacleBlocks() {
return new HashSet<>(this.obstacleBlocks);
}
@Override @Override
protected void load() { protected void load() {
this.enforceCheckpointOrder = configuration.getBoolean(rootNode + "enforceCheckpointOrder", false); this.enforceCheckpointOrder = configuration.getBoolean(rootNode + "enforceCheckpointOrder", false);
this.mustDoGroupedInSequence = configuration.getBoolean(rootNode + "mustDoGroupedInSequence", true); this.mustDoGroupedInSequence = configuration.getBoolean(rootNode + "mustDoGroupedInSequence", true);
this.ignoreRecordsUntilGroupBeatenOnce = configuration.getBoolean(rootNode + "ignoreRecordsUntilGroupBeatenOnce", false); this.ignoreRecordsUntilGroupBeatenOnce = configuration.getBoolean(rootNode + "ignoreRecordsUntilGroupBeatenOnce", false);
this.makePlayersInvisible = configuration.getBoolean(rootNode + "makePlayersInvisible", false);
this.killPlaneBlocks = loadMaterialList(rootNode + "killPlaneBlocks"); this.killPlaneBlocks = loadMaterialList(rootNode + "killPlaneBlocks");
this.obstacleBlocks = loadMaterialList(rootNode + "obstacleBlocks");
} }
@Override @Override
@ -89,11 +89,14 @@ public class ParkourConfiguration extends MiniGameConfiguration {
"Current configuration:" + "Current configuration:" +
"\n" + "Must do groups in sequence: " + mustDoGroupedInSequence + "\n" + "Must do groups in sequence: " + mustDoGroupedInSequence +
"\n" + "Ignore records until group beaten once: " + ignoreRecordsUntilGroupBeatenOnce + "\n" + "Ignore records until group beaten once: " + ignoreRecordsUntilGroupBeatenOnce +
"\n" + "Make players invisible: " + makePlayersInvisible +
"\n" + "Kill plane blocks: "); "\n" + "Kill plane blocks: ");
for (Material material : killPlaneBlocks) { for (Material material : killPlaneBlocks) {
builder.append("\n - ").append(material.name()); builder.append("\n - ").append(material.name());
} }
builder.append("\n" + "Obstacle blocks: ");
for (Material material : obstacleBlocks) {
builder.append("\n - ").append(material.name());
}
return builder.toString(); return builder.toString();
} }

View File

@ -1,41 +0,0 @@
package net.knarcraft.minigames.container;
import java.util.HashMap;
import java.util.Map;
/**
* A container for keeping track of several placeholder to value mappings
*/
public class PlaceholderContainer {
private final Map<String, String> placeholders;
/**
* Instantiates a new placeholder container
*/
public PlaceholderContainer() {
this.placeholders = new HashMap<>();
}
/**
* Gets all placeholders
*
* @return <p>All placeholders</p>
*/
public Map<String, String> getPlaceholders() {
return new HashMap<>(this.placeholders);
}
/**
* Adds a new placeholder
*
* @param placeholder <p>The placeholder to register</p>
* @param value <p>The value of the placeholder</p>
* @return <p>This object</p>
*/
public PlaceholderContainer add(String placeholder, String value) {
this.placeholders.put(placeholder, value);
return this;
}
}

View File

@ -0,0 +1,266 @@
package net.knarcraft.minigames.gui;
import net.knarcraft.knargui.AbstractGUI;
import net.knarcraft.knargui.GUIAction;
import net.knarcraft.knargui.item.GUIItemFactory;
import net.knarcraft.knargui.item.PlayerHeadGUIItemFactory;
import net.knarcraft.knargui.item.SimpleGUIItemFactory;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaPlayerRegistry;
import net.knarcraft.minigames.arena.ArenaSession;
import net.knarcraft.minigames.arena.PlayerVisibilityManager;
import net.knarcraft.minigames.arena.parkour.ParkourArenaSession;
import net.knarcraft.minigames.util.GeyserHelper;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* A generic GUI for all arenas
*/
public abstract class ArenaGUI extends AbstractGUI {
/**
* Instantiates a new arena gui
*
* @param inventorySize <p>The size of the GUI's inventory</p>
* @param inventoryName <p>The name of the inventory</p>
*/
public ArenaGUI(int inventorySize, @NotNull String inventoryName) {
super(inventorySize, inventoryName, null);
}
/**
* Gets an item describing a retry arena action (for Java edition)
*
* @return <p>An arena restart item</p>
*/
@NotNull
protected ItemStack getRestartItemJava() {
PlayerHeadGUIItemFactory restartItemFactory = new PlayerHeadGUIItemFactory();
restartItemFactory.useSkin("e23b225ed0443c4eec7cf30a034490485904e6eb3d53ef2ab9e39c73bdf22c30");
return setRestartItemData(restartItemFactory);
}
/**
* Gets an item describing a retry arena action (for Bedrock edition)
*
* @return <p>An arena restart item</p>
*/
@NotNull
protected ItemStack getRestartItemBedrock() {
return setRestartItemData(new SimpleGUIItemFactory(Material.MAGENTA_GLAZED_TERRACOTTA));
}
/**
* Sets the lore and name for a retry item, and returns the finished item
*
* @param guiItemFactory <p>The factory to apply the data to</p>
* @return <p>The finished item, with the data applied</p>
*/
@NotNull
protected ItemStack setRestartItemData(@NotNull GUIItemFactory guiItemFactory) {
List<String> loreLines = getLoreLines();
loreLines.add(ChatColor.GRAY + "Use this item to retry the arena");
loreLines.add(ChatColor.GRAY + "(deaths and time is reset to 0)");
guiItemFactory.setName(ChatColor.BLUE + "Retry arena");
guiItemFactory.setLore(loreLines);
return guiItemFactory.build();
}
/**
* Gets an item describing player visibility toggling
*
* @return <p>A player toggle item</p>
*/
@NotNull
protected ItemStack getTogglePlayersItemDisabled() {
GUIItemFactory togglePlayersItemFactory = new SimpleGUIItemFactory(Material.PLAYER_HEAD);
List<String> loreLines = getLoreLines();
loreLines.add(ChatColor.GRAY + "Use this item to disable the visibility");
loreLines.add(ChatColor.GRAY + "of other players");
togglePlayersItemFactory.setName(ChatColor.BLUE + "Disable Players");
togglePlayersItemFactory.setLore(loreLines);
return togglePlayersItemFactory.build();
}
/**
* Gets an item describing player visibility toggling
*
* @param player <p>The player to get the item for</p>
* @return <p>A player toggle item</p>
*/
@NotNull
protected ItemStack getTogglePlayersItemEnabled(@NotNull Player player) {
if (GeyserHelper.isGeyserPlayer(player)) {
return getTogglePlayersItemEnabledBedrock();
} else {
return getTogglePlayersItemEnabledJava();
}
}
/**
* Gets an item describing player visibility toggling
*
* @return <p>A player toggle item</p>
*/
@NotNull
protected ItemStack getTogglePlayersItemEnabledJava() {
PlayerHeadGUIItemFactory togglePlayersItemFactory = new PlayerHeadGUIItemFactory();
togglePlayersItemFactory.useSkin("c10591e6909e6a281b371836e462d67a2c78fa0952e910f32b41a26c48c1757c");
return setTogglePlayersItemData(togglePlayersItemFactory);
}
/**
* Gets an item describing player visibility toggling
*
* @return <p>A player toggle item</p>
*/
@NotNull
protected ItemStack getTogglePlayersItemEnabledBedrock() {
return setTogglePlayersItemData(new SimpleGUIItemFactory(Material.SKELETON_SKULL));
}
/**
* Sets the lore and name for a toggle players item, and returns the finished item
*
* @param itemFactory <p>The factory to apply the data to</p>
* @return <p>The finished item, with the data applied</p>
*/
@NotNull
protected ItemStack setTogglePlayersItemData(@NotNull GUIItemFactory itemFactory) {
List<String> loreLines = getLoreLines();
loreLines.add(ChatColor.GRAY + "Use this item to enable the visibility");
loreLines.add(ChatColor.GRAY + "of other players");
itemFactory.setName(ChatColor.BLUE + "Enable Players");
itemFactory.setLore(loreLines);
return itemFactory.build();
}
/**
* Gets an item describing a give up action
*
* @return <p>A give up item</p>
*/
@NotNull
protected ItemStack getGiveUpItem() {
GUIItemFactory giveUpItemFactory = new SimpleGUIItemFactory(Material.RESPAWN_ANCHOR);
List<String> loreLines = getLoreLines();
loreLines.add(ChatColor.GRAY + "Use this item to give up and");
loreLines.add(ChatColor.GRAY + "go to your current checkpoint");
giveUpItemFactory.setName(ChatColor.RED + "Give up");
giveUpItemFactory.setLore(loreLines);
return giveUpItemFactory.build();
}
/**
* Gets an item describing a leave arena action
*
* @return <p>A leave item</p>
*/
@NotNull
protected ItemStack getLeaveItem() {
GUIItemFactory leaveItemFactory = new SimpleGUIItemFactory(Material.BARRIER);
List<String> loreLines = getLoreLines();
loreLines.add(ChatColor.GRAY + "Use this item to leave the arena");
leaveItemFactory.setName(ChatColor.DARK_RED + "Leave");
leaveItemFactory.setLore(loreLines);
return leaveItemFactory.build();
}
/**
* Gets an arraylist with one blank line lore-lines can be added to
*
* @return <p>An arraylist with one blank line</p>
*/
@NotNull
protected List<String> getLoreLines() {
List<String> loreLines = new ArrayList<>();
loreLines.add("");
return loreLines;
}
/**
* Sets a click action for both right-click and left-click
*
* @param inventorySlot <p>The inventory slot the action should be added to</p>
* @param action <p>The action to register</p>
*/
protected void setAnyClickAction(int inventorySlot, @NotNull GUIAction action) {
setClickAction(inventorySlot, ClickType.LEFT, action);
setClickAction(inventorySlot, ClickType.RIGHT, action);
}
/**
* Gets the action to run when triggering the leave item
*
* @return <p>The leave action</p>
*/
@NotNull
public static GUIAction getLeaveAction() {
return (player) -> {
ArenaSession session = MiniGames.getInstance().getSession(player.getUniqueId());
if (session != null) {
session.triggerQuit(false, true);
}
};
}
/**
* Gets the action to run when triggering the restart action
*
* @return <p>The action for triggering a session restart</p>
*/
@NotNull
public static GUIAction getRestartAction() {
return (player -> {
ArenaSession session = MiniGames.getInstance().getSession(player.getUniqueId());
if (session != null) {
session.reset();
}
});
}
/**
* Gets the action to run when triggering the toggle players action
*
* @param playerRegistry <p>The registry containing relevant players</p>
* @param inventorySlot <p>The inventory slot to replace when toggling</p>
* @return <p>The action for triggering player visibility</p>
*/
@NotNull
public GUIAction getTogglePlayersAction(@Nullable ArenaPlayerRegistry<?> playerRegistry, int inventorySlot) {
return (player) -> {
PlayerVisibilityManager visibilityManager = MiniGames.getInstance().getPlayerVisibilityManager();
visibilityManager.toggleHidePlayers(playerRegistry, player);
if (MiniGames.getInstance().getPlayerVisibilityManager().isHidingPlayers(player)) {
setItem(inventorySlot, getTogglePlayersItemEnabled(player));
} else {
setItem(inventorySlot, getTogglePlayersItemDisabled());
}
};
}
/**
* Gets the action to run when triggering the give up item
*
* @return <p>The give up action</p>
*/
@NotNull
public static GUIAction getGiveUpAction() {
return (player) -> {
ArenaSession session = MiniGames.getInstance().getSession(player.getUniqueId());
if (session instanceof ParkourArenaSession) {
session.triggerLoss();
}
};
}
}

View File

@ -0,0 +1,31 @@
package net.knarcraft.minigames.gui;
import net.knarcraft.minigames.MiniGames;
import org.bukkit.entity.Player;
/**
* A GUI used in the dropper arena
*/
public class DropperGUI extends ArenaGUI {
/**
* Instantiates a new dropper gui
*
* @param player <p>The player the GUI is created for</p>
*/
public DropperGUI(Player player) {
super(9, "Dropper");
if (MiniGames.getInstance().getPlayerVisibilityManager().isHidingPlayers(player)) {
setItem(0, getTogglePlayersItemEnabled(player));
} else {
setItem(0, getTogglePlayersItemDisabled());
}
setItem(2, getLeaveItem());
setItem(4, getRestartItemJava());
setAnyClickAction(0, getTogglePlayersAction(MiniGames.getInstance().getDropperArenaPlayerRegistry(), 0));
setAnyClickAction(2, getLeaveAction());
setAnyClickAction(4, getRestartAction());
}
}

View File

@ -0,0 +1,31 @@
package net.knarcraft.minigames.gui;
import net.knarcraft.minigames.MiniGames;
import org.bukkit.entity.Player;
/**
* A GUI used in the dropper arena, altered for Bedrock players
*/
public class DropperGUIBedrock extends ArenaGUI {
/**
* Instantiates a new dropper gui
*
* @param player <p>The player the GUI is created for</p>
*/
public DropperGUIBedrock(Player player) {
super(27, "Dropper");
if (MiniGames.getInstance().getPlayerVisibilityManager().isHidingPlayers(player)) {
setItem(10, getTogglePlayersItemEnabledBedrock());
} else {
setItem(10, getTogglePlayersItemDisabled());
}
setItem(12, getLeaveItem());
setItem(14, getRestartItemBedrock());
setAnyClickAction(10, getTogglePlayersAction(MiniGames.getInstance().getDropperArenaPlayerRegistry(), 10));
setAnyClickAction(12, getLeaveAction());
setAnyClickAction(14, getRestartAction());
}
}

View File

@ -0,0 +1,27 @@
package net.knarcraft.minigames.gui;
import net.knarcraft.minigames.MiniGames;
import org.bukkit.entity.Player;
/**
* A GUI used outside arenas
*/
public class MiniGamesGUI extends ArenaGUI {
/**
* Instantiates a new mini games gui
*
* @param player <p>The player the GUI is created for</p>
*/
public MiniGamesGUI(Player player) {
super(9, "MiniGames");
if (MiniGames.getInstance().getPlayerVisibilityManager().isHidingPlayers(player)) {
setItem(0, getTogglePlayersItemEnabled(player));
} else {
setItem(0, getTogglePlayersItemDisabled());
}
setAnyClickAction(0, getTogglePlayersAction(null, 0));
}
}

View File

@ -0,0 +1,33 @@
package net.knarcraft.minigames.gui;
import net.knarcraft.minigames.MiniGames;
import org.bukkit.entity.Player;
/**
* A GUI used in the parkour arena
*/
public class ParkourGUI extends ArenaGUI {
/**
* Instantiates a new parkour gui
*
* @param player <p>The player the GUI is created for</p>
*/
public ParkourGUI(Player player) {
super(9, "Parkour");
if (MiniGames.getInstance().getPlayerVisibilityManager().isHidingPlayers(player)) {
setItem(1, getTogglePlayersItemEnabled(player));
} else {
setItem(1, getTogglePlayersItemDisabled());
}
setItem(3, getGiveUpItem());
setItem(5, getLeaveItem());
setItem(7, getRestartItemJava());
setAnyClickAction(1, getTogglePlayersAction(MiniGames.getInstance().getParkourArenaPlayerRegistry(), 1));
setAnyClickAction(3, getGiveUpAction());
setAnyClickAction(5, getLeaveAction());
setAnyClickAction(7, getRestartAction());
}
}

View File

@ -0,0 +1,33 @@
package net.knarcraft.minigames.gui;
import net.knarcraft.minigames.MiniGames;
import org.bukkit.entity.Player;
/**
* A GUI used in the parkour arena, altered for Bedrock players
*/
public class ParkourGUIBedrock extends ArenaGUI {
/**
* Instantiates a new parkour gui
*
* @param player <p>The player the GUI is created for</p>
*/
public ParkourGUIBedrock(Player player) {
super(27, "Parkour");
if (MiniGames.getInstance().getPlayerVisibilityManager().isHidingPlayers(player)) {
setItem(10, getTogglePlayersItemEnabled(player));
} else {
setItem(10, getTogglePlayersItemDisabled());
}
setItem(12, getGiveUpItem());
setItem(14, getLeaveItem());
setItem(16, getRestartItemBedrock());
setAnyClickAction(10, getTogglePlayersAction(MiniGames.getInstance().getParkourArenaPlayerRegistry(), 10));
setAnyClickAction(12, getGiveUpAction());
setAnyClickAction(14, getLeaveAction());
setAnyClickAction(16, getRestartAction());
}
}

View File

@ -2,7 +2,7 @@ package net.knarcraft.minigames.listener;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaSession; import net.knarcraft.minigames.arena.ArenaSession;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -29,6 +29,8 @@ public class CommandListener implements Listener {
allowedCommands.add("/mLeave"); allowedCommands.add("/mLeave");
allowedCommands.add("/dLeave"); allowedCommands.add("/dLeave");
allowedCommands.add("/pLeave"); allowedCommands.add("/pLeave");
allowedCommands.add("/miniGamesMenu");
allowedCommands.add("/mMenu");
String message = event.getMessage(); String message = event.getMessage();
if (!message.startsWith("/")) { if (!message.startsWith("/")) {
@ -41,7 +43,7 @@ public class CommandListener implements Listener {
} }
} }
player.sendMessage(Message.ERROR_ILLEGAL_COMMAND.getMessage()); MiniGames.getInstance().getStringFormatter().displayErrorMessage(player, MiniGameMessage.ERROR_ILLEGAL_COMMAND);
event.setCancelled(true); event.setCancelled(true);
} }

View File

@ -1,8 +1,12 @@
package net.knarcraft.minigames.listener; package net.knarcraft.minigames.listener;
import net.knarcraft.knarlib.lib.annotations.NotNull;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaSession; import net.knarcraft.minigames.arena.ArenaSession;
import net.knarcraft.minigames.arena.dropper.DropperArenaSession; import net.knarcraft.minigames.arena.dropper.DropperArenaSession;
import org.bukkit.Sound;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -16,7 +20,7 @@ import org.bukkit.event.entity.EntityDamageEvent;
public class DamageListener implements Listener { public class DamageListener implements Listener {
@EventHandler @EventHandler
public void onPlayerDamage(EntityDamageEvent event) { public void onPlayerDamage(@NotNull EntityDamageEvent event) {
// Only player damage matters // Only player damage matters
if (event.getEntityType() != EntityType.PLAYER) { if (event.getEntityType() != EntityType.PLAYER) {
return; return;
@ -32,14 +36,52 @@ public class DamageListener implements Listener {
event.setCancelled(true); event.setCancelled(true);
// Only trigger a loss when a player suffers fall damage in a dropper arena // Only trigger a loss when a player suffers fall damage in a dropper arena (This cannot be cancelled!)
if (arenaSession instanceof DropperArenaSession && event.getCause() == EntityDamageEvent.DamageCause.FALL) { if (arenaSession instanceof DropperArenaSession && event.getCause() == EntityDamageEvent.DamageCause.FALL) {
arenaSession.triggerLoss(); arenaSession.triggerLoss();
return;
}
// If set as allowed damage, do nothing, except if the damage is fatal
if (arenaSession.getArena().getAllowedDamageCauses().contains(event.getCause())) {
applyFakeDamage(player, event.getDamage(), arenaSession);
return;
}
// If set as trigger loss, trigger a loss
if (arenaSession.getArena().getLossTriggerDamageCauses().contains(event.getCause())) {
arenaSession.triggerLoss();
}
}
/**
* Fakes the damaging of a player
*
* @param player <p>The player to damage</p>
* @param damage <p>The raw damage to apply</p>
* @param arenaSession <p>The arena session to trigger a loss for, if the player reaches 0 damage</p>
*/
private void applyFakeDamage(@NotNull Player player, double damage, @NotNull ArenaSession arenaSession) {
if (player.getNoDamageTicks() > 0) {
return;
}
double newHealth = player.getHealth() - damage;
player.sendHurtAnimation(180);
if (newHealth <= 0) {
AttributeInstance health = player.getAttribute(Attribute.MAX_HEALTH);
if (health != null) {
player.setHealth(health.getValue());
}
arenaSession.triggerLoss();
} else {
player.setHealth(newHealth);
player.setNoDamageTicks(10);
player.playSound(player, Sound.ENTITY_PLAYER_HURT, 1, 1);
} }
} }
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onPlayerCombustion(EntityCombustEvent event) { public void onPlayerCombustion(@NotNull EntityCombustEvent event) {
if (event.getEntityType() != EntityType.PLAYER) { if (event.getEntityType() != EntityType.PLAYER) {
return; return;
} }

View File

@ -0,0 +1,82 @@
package net.knarcraft.minigames.listener;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaPlayerRegistry;
import net.knarcraft.minigames.arena.parkour.ParkourArena;
import net.knarcraft.minigames.arena.parkour.ParkourArenaSession;
import net.knarcraft.minigames.gui.ArenaGUI;
import net.knarcraft.minigames.gui.ParkourGUI;
import net.knarcraft.minigames.property.PersistentDataKey;
import net.knarcraft.minigames.util.GUIHelper;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.data.type.Switch;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
/**
* A listener that listens for player interactions
*/
public class InteractListener implements Listener {
@EventHandler
public void menuInteractListener(@NotNull PlayerInteractEvent event) {
handleMenuClick(event);
Block clicked = event.getClickedBlock();
if (event.useInteractedBlock() != Event.Result.DENY && event.getAction() == Action.RIGHT_CLICK_BLOCK &&
clicked != null && clicked.getBlockData() instanceof Switch) {
ArenaPlayerRegistry<ParkourArena> playerRegistry = MiniGames.getInstance().getParkourArenaPlayerRegistry();
ParkourArenaSession arenaSession = (ParkourArenaSession) playerRegistry.getArenaSession(event.getPlayer().getUniqueId());
if (arenaSession != null) {
arenaSession.registerChangedLever(clicked);
}
}
}
/**
* Handles clicking of the menu item
*
* @param event <p>The triggered player interact event</p>
*/
private void handleMenuClick(@NotNull PlayerInteractEvent event) {
ItemStack item = event.getItem();
if (item == null || !item.hasItemMeta()) {
return;
}
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return;
}
Integer persistentData = meta.getPersistentDataContainer().get(new NamespacedKey(MiniGames.getInstance(),
PersistentDataKey.MENU_ITEM.getKeyName()), PersistentDataType.INTEGER);
if (persistentData != null && persistentData == PersistentDataKey.MENU_ITEM.getDataValue()) {
event.setUseItemInHand(Event.Result.DENY);
event.setUseInteractedBlock(Event.Result.DENY);
GUIHelper.openGUI(event.getPlayer());
return;
}
persistentData = meta.getPersistentDataContainer().get(new NamespacedKey(MiniGames.getInstance(),
PersistentDataKey.LEAVE_ITEM.getKeyName()), PersistentDataType.INTEGER);
if (persistentData != null) {
event.setUseItemInHand(Event.Result.DENY);
event.setUseInteractedBlock(Event.Result.DENY);
if (persistentData == PersistentDataKey.LEAVE_ITEM.getDataValue()) {
ArenaGUI.getLeaveAction().run(event.getPlayer());
} else if (persistentData == PersistentDataKey.GIVE_UP_ITEM.getDataValue()) {
ParkourGUI.getGiveUpAction().run(event.getPlayer());
}
}
}
}

View File

@ -3,20 +3,25 @@ package net.knarcraft.minigames.listener;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.Arena; import net.knarcraft.minigames.arena.Arena;
import net.knarcraft.minigames.arena.ArenaSession; import net.knarcraft.minigames.arena.ArenaSession;
import net.knarcraft.minigames.arena.dropper.DropperArena;
import net.knarcraft.minigames.arena.dropper.DropperArenaGameMode; import net.knarcraft.minigames.arena.dropper.DropperArenaGameMode;
import net.knarcraft.minigames.arena.dropper.DropperArenaSession; import net.knarcraft.minigames.arena.dropper.DropperArenaSession;
import net.knarcraft.minigames.arena.parkour.ParkourArena; import net.knarcraft.minigames.arena.parkour.ParkourArena;
import net.knarcraft.minigames.arena.parkour.ParkourArenaGameMode;
import net.knarcraft.minigames.arena.parkour.ParkourArenaSession; import net.knarcraft.minigames.arena.parkour.ParkourArenaSession;
import net.knarcraft.minigames.config.DropperConfiguration; import net.knarcraft.minigames.config.DropperConfiguration;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.config.ParkourConfiguration; import net.knarcraft.minigames.config.ParkourConfiguration;
import net.knarcraft.minigames.config.SharedConfiguration; import net.knarcraft.minigames.config.SharedConfiguration;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -24,12 +29,15 @@ import java.util.Calendar;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.logging.Level;
/** /**
* A listener for players moving inside a dropper arena * A listener for players moving inside a dropper arena
*/ */
public class MoveListener implements Listener { public class MoveListener implements Listener {
private static final BoundingBox fullBlockBox = new BoundingBox(0, 0, 0, 1, 1, 1);
private final DropperConfiguration dropperConfiguration; private final DropperConfiguration dropperConfiguration;
private final ParkourConfiguration parkourConfiguration; private final ParkourConfiguration parkourConfiguration;
@ -47,12 +55,15 @@ public class MoveListener implements Listener {
@EventHandler @EventHandler
public void onPlayerMove(PlayerMoveEvent event) { public void onPlayerMove(PlayerMoveEvent event) {
// Ignore if no actual movement is happening // Ignore if no actual movement is happening
if (event.getFrom().equals(event.getTo()) || event.getTo() == null) { if (event.getTo() == null) {
return; return;
} }
ArenaSession session = MiniGames.getInstance().getSession(event.getPlayer().getUniqueId()); ArenaSession session = MiniGames.getInstance().getSession(event.getPlayer().getUniqueId());
if (session instanceof DropperArenaSession dropperSession) { if (session instanceof DropperArenaSession dropperSession) {
if (event.getFrom().equals(event.getTo())) {
return;
}
doDropperArenaChecks(event, dropperSession); doDropperArenaChecks(event, dropperSession);
} else if (session instanceof ParkourArenaSession parkourSession) { } else if (session instanceof ParkourArenaSession parkourSession) {
doParkourArenaChecks(event, parkourSession); doParkourArenaChecks(event, parkourSession);
@ -66,8 +77,7 @@ public class MoveListener implements Listener {
* @param arenaSession <p>The dropper session of the player triggering the event</p> * @param arenaSession <p>The dropper session of the player triggering the event</p>
*/ */
private void doParkourArenaChecks(@NotNull PlayerMoveEvent event, ParkourArenaSession arenaSession) { private void doParkourArenaChecks(@NotNull PlayerMoveEvent event, ParkourArenaSession arenaSession) {
// Ignore movement which won't cause the player's block to change if (event.getTo() == null) {
if (event.getTo() == null || event.getFrom().getBlock() == event.getTo().getBlock()) {
return; return;
} }
@ -76,14 +86,31 @@ public class MoveListener implements Listener {
return; return;
} }
// Skip checkpoint registration if playing on hardcore
if (arenaSession.getGameMode() == ParkourArenaGameMode.HARDCORE) {
return;
}
// Check if the player reached one of the checkpoints for the arena // Check if the player reached one of the checkpoints for the arena
updateCheckpoint(arenaSession, event.getTo().getBlock(), event.getPlayer());
}
/**
* Updates the checkpoint of a player if reached
*
* @param arenaSession <p>The session of the player</p>
* @param targetBlock <p>The block the player is moving to</p>
* @param player <p>The player moving</p>
*/
private void updateCheckpoint(@NotNull ParkourArenaSession arenaSession, @NotNull Block targetBlock,
@NotNull Player player) {
ParkourArena arena = arenaSession.getArena(); ParkourArena arena = arenaSession.getArena();
List<Location> checkpoints = arena.getCheckpoints(); List<Location> checkpoints = arena.getCheckpoints();
for (Location checkpoint : checkpoints) { for (Location checkpoint : checkpoints) {
Location previousCheckpoint = arenaSession.getRegisteredCheckpoint(); Location previousCheckpoint = arenaSession.getRegisteredCheckpoint();
// Skip if checkpoint has not been reached // Skip if checkpoint has not been reached
if (!checkpoint.getBlock().equals(event.getTo().getBlock())) { if (!checkpoint.getBlock().equals(targetBlock)) {
continue; continue;
} }
@ -103,7 +130,8 @@ public class MoveListener implements Listener {
// Register the checkpoint // Register the checkpoint
arenaSession.registerCheckpoint(checkpoint.clone()); arenaSession.registerCheckpoint(checkpoint.clone());
event.getPlayer().sendMessage(Message.SUCCESS_CHECKPOINT_REACHED.getMessage()); MiniGames.getInstance().getStringFormatter().displaySuccessMessage(player,
MiniGameMessage.SUCCESS_CHECKPOINT_REACHED);
return; return;
} }
} }
@ -115,14 +143,26 @@ public class MoveListener implements Listener {
* @param arenaSession <p>The dropper session of the player triggering the event</p> * @param arenaSession <p>The dropper session of the player triggering the event</p>
*/ */
private void doDropperArenaChecks(@NotNull PlayerMoveEvent event, @NotNull DropperArenaSession arenaSession) { private void doDropperArenaChecks(@NotNull PlayerMoveEvent event, @NotNull DropperArenaSession arenaSession) {
if (event.getTo() == null) { // If the player has yet to move in the arena, allow them to look around
boolean startedMoving = arenaSession.getStartedMoving();
if (event.getTo() == null ||
(!startedMoving && isSameLocation(event.getFrom(), event.getTo()))) {
return; return;
} }
// Marks the player as started moving if necessary, so they can no longer hang in the air
if (!startedMoving) {
arenaSession.setStartedMoving();
}
// Prevent the player from flying upwards while in flight mode // Prevent the player from flying upwards while in flight mode
if (event.getFrom().getY() < event.getTo().getY() || if (event.getFrom().getY() < event.getTo().getY() ||
(dropperConfiguration.blockSneaking() && event.getPlayer().isSneaking()) || (dropperConfiguration.blockSneaking() && event.getPlayer().isSneaking()) ||
(dropperConfiguration.blockSprinting() && event.getPlayer().isSprinting())) { (dropperConfiguration.blockSprinting() && event.getPlayer().isSprinting())) {
event.setCancelled(true); event.setCancelled(true);
// Force movement downwards once the player lets go
Bukkit.getScheduler().scheduleSyncDelayedTask(MiniGames.getInstance(),
() -> updatePlayerVelocity(arenaSession), 1);
return; return;
} }
@ -137,6 +177,18 @@ public class MoveListener implements Listener {
updatePlayerVelocity(arenaSession); updatePlayerVelocity(arenaSession);
} }
/**
* Checks if two locations are the same, excluding rotation
*
* @param location1 <p>The first location to check</p>
* @param location2 <p>The second location to check</p>
* @return <p>True if the locations are the same, excluding rotation</p>
*/
private boolean isSameLocation(Location location1, Location location2) {
return location1.getX() == location2.getX() && location1.getY() == location2.getY() &&
location1.getZ() == location2.getZ();
}
/** /**
* Check if the player in the session is triggering a block with a special significance * Check if the player in the session is triggering a block with a special significance
* *
@ -146,30 +198,199 @@ public class MoveListener implements Listener {
* @param toLocation <p>The location the player's session is about to hit</p> * @param toLocation <p>The location the player's session is about to hit</p>
* @return <p>True if a special block has been hit</p> * @return <p>True if a special block has been hit</p>
*/ */
private boolean checkForSpecialBlock(ArenaSession arenaSession, Location toLocation) { private boolean checkForSpecialBlock(@NotNull ArenaSession arenaSession, @NotNull Location toLocation) {
SharedConfiguration sharedConfiguration = MiniGames.getInstance().getSharedConfiguration(); SharedConfiguration sharedConfiguration = MiniGames.getInstance().getSharedConfiguration();
double liquidDepth = sharedConfiguration.getLiquidHitBoxDepth();
double solidDepth = sharedConfiguration.getSolidHitBoxDistance(); double solidDepth = sharedConfiguration.getSolidHitBoxDistance();
double liquidDepth = sharedConfiguration.getLiquidHitBoxDepth();
Arena arena = arenaSession.getArena(); Arena arena = arenaSession.getArena();
if (arena instanceof DropperArena) {
return checkDropperWin(arenaSession, toLocation, solidDepth, liquidDepth) ||
checkDropperDeathBlocks((DropperArenaSession) arenaSession, toLocation, solidDepth, liquidDepth);
} else if (arena instanceof ParkourArena) {
return checkParkourWin(arenaSession, toLocation.getBlock()) ||
checkParkourDeathBlock((ParkourArenaSession) arenaSession, toLocation);
} else {
MiniGames.log(Level.SEVERE, "Unknown arena type encountered!");
}
return false;
}
/**
* Checks if the player has triggered a parkour win block
*
* @param arenaSession <p>The player's current arena session</p>
* @param targetBlock <p>The block the player moved to</p>
* @return <p>True if the player triggered a parkour win block</p>
*/
private boolean checkParkourWin(@NotNull ArenaSession arenaSession, @NotNull Block targetBlock) {
boolean winBlock = arenaSession.getArena().willCauseWin(targetBlock);
if (winBlock) {
arenaSession.triggerWin();
}
return winBlock;
}
/**
* Checks if the player has triggered a dropper win block
*
* @param arenaSession <p>The player's arena session</p>
* @param toLocation <p>The location the player moved to</p>
* @param solidDepth <p>The distance away from solid blocks that will trigger a hit</p>
* @param liquidDepth <p>The depth players need to be inside a liquid to trigger a hit</p>
* @return <p>True if the player triggered a dropper win block</p>
*/
private boolean checkDropperWin(@NotNull ArenaSession arenaSession, @NotNull Location toLocation, double solidDepth,
double liquidDepth) {
Arena arena = arenaSession.getArena();
Set<Block> potentialWinTriggerBlocks;
// For water, only trigger when the player enters the water, but trigger earlier for everything else // For water, only trigger when the player enters the water, but trigger earlier for everything else
double depth = arena.winLocationIsSolid() ? solidDepth : liquidDepth; if (arena.winLocationIsSolid()) {
for (Block block : getBlocksBeneathLocation(toLocation, depth)) { potentialWinTriggerBlocks = getBlocksBeneathLocation(toLocation, solidDepth);
} else {
potentialWinTriggerBlocks = getBlocksBeneathLocation(toLocation, liquidDepth);
}
for (Block block : potentialWinTriggerBlocks) {
if (arena.willCauseWin(block)) { if (arena.willCauseWin(block)) {
arenaSession.triggerWin(); arenaSession.triggerWin();
return true; return true;
} }
} }
return false;
}
/**
* Checks if a player is moving onto a block part of the dropper arena death plane
*
* @param arenaSession <p>The player's arena session</p>
* @param toLocation <p>The location the player is moving to</p>
* @param solidDepth <p>The distance away from solid blocks that will trigger a hit</p>
* @param liquidDepth <p>The depth players need to be inside a liquid to trigger a hit</p>
* @return <p>True if the player triggered a dropper death block</p>
*/
private boolean checkDropperDeathBlocks(@NotNull DropperArenaSession arenaSession,
@NotNull Location toLocation, double solidDepth, double liquidDepth) {
Arena arena = arenaSession.getArena();
// Check if the player is about to hit a non-air and non-liquid block // Check if the player is about to hit a non-air and non-liquid block
for (Block block : getBlocksBeneathLocation(toLocation, solidDepth)) { for (Block block : getBlocksBeneathLocation(toLocation, solidDepth)) {
if (!block.getType().isAir() && arena.willCauseLoss(block)) { if (!block.getType().isAir() && !block.isLiquid() && arena.willCauseLoss(block)) {
arenaSession.triggerLoss(); arenaSession.triggerLoss();
return true; return true;
} }
} }
// Check if the player has entered a liquid that causes a loss
for (Block block : getBlocksBeneathLocation(toLocation, liquidDepth)) {
if (block.isLiquid() && arena.willCauseLoss(block)) {
arenaSession.triggerLoss();
return true;
}
}
return false;
}
/**
* Checks if a player is moving onto a block part of the parkour death plane
*
* @param arenaSession <p>The player's arena session</p>
* @param toLocation <p>The location the player is moving to</p>
* @return <p>True if the player hit a death block</p>
*/
private boolean checkParkourDeathBlock(@NotNull ParkourArenaSession arenaSession,
@NotNull Location toLocation) {
// A simple check, only for kill blocks
if (isOnKillBlock(arenaSession, toLocation)) {
return true;
}
// As the check for obstacle blocks is extensive, it's skipped if possible
Set<Material> obstacleBlocks = arenaSession.getArena().getObstacleBlocks();
if (obstacleBlocks.isEmpty()) {
return false;
}
// Create a hit-box approximate to the player's real hit-box
double playerHeight = 1.8;
Player player = Bukkit.getPlayer(arenaSession.getEntryState().getPlayerId());
if (player != null && player.isSneaking()) {
playerHeight = 1.5;
}
BoundingBox playerBox = new BoundingBox(-0.05, -0.05, -0.05,
0.6 + 0.05, playerHeight + 0.05, 0.6 + 0.05).shift(
toLocation).shift(-0.3, -0.05, -0.3);
BoundingBox playerPassableBox = new BoundingBox(0.2, 0.5, 0.2,
0.4, playerHeight - 0.5, 0.4).shift(
toLocation).shift(-0.3, 0, -0.3);
Set<Block> possiblyHitBlocks = new HashSet<>();
possiblyHitBlocks.addAll(getBlocksBeneathLocation(toLocation, 0, 0.01));
possiblyHitBlocks.addAll(getBlocksBeneathLocation(toLocation, 1, 0.01));
possiblyHitBlocks.addAll(getBlocksBeneathLocation(toLocation, -1, 0.01));
possiblyHitBlocks.addAll(getBlocksBeneathLocation(toLocation, -2, 0.01));
for (Block block : possiblyHitBlocks) {
if (!obstacleBlocks.contains(block.getType())) {
continue;
}
// For liquids, or anything without a proper collision shape, trigger collision if the player is partly
// inside when treated as a full block
if (block.isLiquid() || block.getCollisionShape().getBoundingBoxes().isEmpty()) {
if (playerPassableBox.overlaps(fullBlockBox.clone().shift(block.getLocation()))) {
arenaSession.triggerLoss();
return true;
}
// Make the hit-box stricter for liquids beside the player to make lava labyrinths feasible
if (block.isLiquid() && player != null && block.getY() > player.getLocation().getBlockY() &&
playerBox.overlaps(fullBlockBox.clone().shift(block.getLocation()))) {
arenaSession.triggerLoss();
return true;
}
}
// Check whether the player's actual hit-box is intersecting with a block
for (BoundingBox boundingBox : block.getCollisionShape().getBoundingBoxes()) {
// A collision shape's bounding box is relative to 0,0 and therefore must be adjusted to the block's
// location. Then overlap is checked by the player's collision box and the shifted bounding box.
if (playerBox.overlaps(boundingBox.clone().shift(block.getLocation()))) {
arenaSession.triggerLoss();
return true;
}
}
}
return false;
}
/**
* As simple check for whether a player is moving on top of a kill block
*
* @param arenaSession <p>The arena session the player is in</p>
* @param toLocation <p>The location the player is moving to</p>
* @return <p>True if the player is on a kill block, and a loss has been triggered</p>
*/
private boolean isOnKillBlock(ParkourArenaSession arenaSession, Location toLocation) {
// If the player is standing on a non-full block, event.getTo will give the correct block, but if not, the
// block below has to be checked instead.
Set<Block> blocksBelow = getBlocksBeneathLocation(toLocation, 0);
Set<Material> killPlaneBlocks = arenaSession.getArena().getKillPlaneBlocks();
for (Block block : blocksBelow) {
if (block.getType().isAir()) {
block = block.getLocation().clone().subtract(0, 0.2, 0).getBlock();
// Only trigger hit detection for passable blocks if the player is in the block
if (block.isPassable()) {
continue;
}
}
if (killPlaneBlocks.contains(block.getType())) {
arenaSession.triggerLoss();
return true;
}
}
return false; return false;
} }
@ -180,12 +401,31 @@ public class MoveListener implements Listener {
* @return <p>The blocks beneath the player</p> * @return <p>The blocks beneath the player</p>
*/ */
private Set<Block> getBlocksBeneathLocation(Location location, double depth) { private Set<Block> getBlocksBeneathLocation(Location location, double depth) {
return getBlocksBeneathLocation(location, depth, 0);
}
/**
* Gets the blocks at the given location that will be affected by the player's hit-box
*
* @param location <p>The location to check</p>
* @param extraRange <p>Extra range of the square used for finding blocks</p>
* @return <p>The blocks beneath the player</p>
*/
private Set<Block> getBlocksBeneathLocation(Location location, double depth, double extraRange) {
Set<Block> blocksBeneath = new HashSet<>(); Set<Block> blocksBeneath = new HashSet<>();
double halfPlayerWidth = 0.3; double halfPlayerWidth = 0.3 + extraRange;
blocksBeneath.add(location.clone().subtract(halfPlayerWidth, depth, halfPlayerWidth).getBlock()); blocksBeneath.add(location.clone().subtract(halfPlayerWidth, depth, halfPlayerWidth).getBlock());
blocksBeneath.add(location.clone().subtract(-halfPlayerWidth, depth, halfPlayerWidth).getBlock()); blocksBeneath.add(location.clone().subtract(-halfPlayerWidth, depth, halfPlayerWidth).getBlock());
blocksBeneath.add(location.clone().subtract(halfPlayerWidth, depth, -halfPlayerWidth).getBlock()); blocksBeneath.add(location.clone().subtract(halfPlayerWidth, depth, -halfPlayerWidth).getBlock());
blocksBeneath.add(location.clone().subtract(-halfPlayerWidth, depth, -halfPlayerWidth).getBlock()); blocksBeneath.add(location.clone().subtract(-halfPlayerWidth, depth, -halfPlayerWidth).getBlock());
// Once a certain size is reached, if the player is in the centre of a block, 9 must be accounted for
if (halfPlayerWidth > 0.5) {
blocksBeneath.add(location.getBlock());
blocksBeneath.add(location.clone().subtract(halfPlayerWidth, depth, 0).getBlock());
blocksBeneath.add(location.clone().subtract(-halfPlayerWidth, depth, 0).getBlock());
blocksBeneath.add(location.clone().subtract(0, depth, -halfPlayerWidth).getBlock());
blocksBeneath.add(location.clone().subtract(0, depth, halfPlayerWidth).getBlock());
}
return blocksBeneath; return blocksBeneath;
} }

View File

@ -0,0 +1,55 @@
package net.knarcraft.minigames.manager;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.OfflinePlayer;
/**
* A manager that performs all Economy tasks
*/
public final class EconomyManager {
private static Economy economy;
private EconomyManager() {
}
/**
* Initializes the economy manager
*
* @param economy <p>The economy object to use for everything economy-related</p>
*/
public static void initialize(Economy economy) {
EconomyManager.economy = economy;
}
/**
* Checks whether the economy manager has been initialized
*
* @return <p>True if the economy manager has been initialized</p>
*/
public static boolean isInitialized() {
return EconomyManager.economy != null;
}
/**
* Formats the given amount of currency according to the economy plugin's format
*
* @param amount <p>The amount of currency to format</p>
* @return <p>The formatted string</p>
*/
public static String format(double amount) {
return economy.format(amount);
}
/**
* Deposits a given sum into the given player's account
*
* @param player <p>The player to deposit money to</p>
* @param sum <p>The amount of money to deposit</p>
*/
public static void deposit(OfflinePlayer player, double sum) {
economy.depositPlayer(player, sum);
}
}

View File

@ -0,0 +1,64 @@
package net.knarcraft.minigames.manager;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* A manager that performs all Permission tasks
*/
public final class PermissionManager {
private static Permission permission;
private PermissionManager() {
}
/**
* Initializes the permission manager
*
* @param permission <p>The permission object to use for everything permission-related</p>
*/
public static void initialize(Permission permission) {
PermissionManager.permission = permission;
}
/**
* Checks whether the permission manager has been initialized
*
* @return <p>True if the permission manager has been initialized</p>
*/
public static boolean isInitialized() {
return PermissionManager.permission != null;
}
/**
* Grants a permanent permission to a player
*
* @param player <p>The player to grant the permission to</p>
* @param permissionNode <p>The permission node to grant to the player</p>
*/
public static void addPermission(@NotNull Player player, @NotNull String permissionNode, @Nullable World world) {
if (world != null) {
permission.playerAdd(world.getName(), player, permissionNode);
} else {
permission.playerAdd(player, permissionNode);
}
}
/**
* Checks whether the given player has the given permission
*
* @param player <p>The player to check</p>
* @param permissionNode <p>The permission node to check for</p>
* @param world <p>The world to check for the permission</p>
* @return <p>True if the player has the permission</p>
*/
public static boolean hasPermission(@NotNull Player player, @NotNull String permissionNode, @Nullable String world) {
return permission.playerHas(world, player, permissionNode);
}
}

View File

@ -0,0 +1,75 @@
package net.knarcraft.minigames.placeholder;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.dropper.DropperArena;
import net.knarcraft.minigames.arena.dropper.DropperArenaGameMode;
import net.knarcraft.minigames.arena.dropper.DropperArenaHandler;
import net.knarcraft.minigames.placeholder.parsing.PlayerPlaceholderParser;
import net.knarcraft.minigames.placeholder.parsing.RecordPlaceholderParser;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* A placeholderAPI expansion for Dropper-related placeholders
*/
public class DropperExpansion extends PlaceholderExpansion {
private final @NotNull RecordPlaceholderParser recordPlaceholderParser;
private final @NotNull PlayerPlaceholderParser<DropperArena> playerPlaceholderParser;
/**
* Instantiates a new dropper expansion
*
* @param plugin <p>A reference to the mini-games plugin</p>
*/
public DropperExpansion(@NotNull MiniGames plugin) {
DropperArenaHandler arenaHandler = plugin.getDropperArenaHandler();
this.recordPlaceholderParser = new RecordPlaceholderParser(arenaHandler, DropperArenaGameMode::matchGameMode);
this.playerPlaceholderParser = new PlayerPlaceholderParser<>(arenaHandler, DropperArenaGameMode::matchGameMode,
plugin.getDropperArenaPlayerRegistry());
}
/**
* Clears record caches
*/
public void clearCaches() {
this.recordPlaceholderParser.clearCaches();
}
@Override
public String getIdentifier() {
return "dropper";
}
@Override
public String getAuthor() {
return "EpicKnarvik97";
}
@Override
public String getVersion() {
return "1.0.0";
}
@Override
public boolean persist() {
return true;
}
@Override
@Nullable
public String onRequest(OfflinePlayer player, String parameters) {
String[] parts = parameters.split("_");
// Record is used as the prefix for all record placeholders in case more placeholder types are added
if (parts[0].equalsIgnoreCase("record") && parts.length >= 7) {
return recordPlaceholderParser.onRequest(parts);
} else if (parts[0].equalsIgnoreCase("players")) {
return this.playerPlaceholderParser.onRequest(parts);
} else {
return parameters;
}
}
}

View File

@ -1,32 +0,0 @@
package net.knarcraft.minigames.placeholder;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaGameMode;
import net.knarcraft.minigames.arena.dropper.DropperArenaGameMode;
import org.jetbrains.annotations.NotNull;
/**
* A placeholder expansion for dropper record placeholders
*/
public class DropperRecordExpansion extends RecordExpansion {
/**
* Initializes a new record expansion
*
* @param plugin <p>A reference to the dropper plugin</p>
*/
public DropperRecordExpansion(MiniGames plugin) {
super(plugin.getDropperArenaHandler());
}
@Override
public String getIdentifier() {
return "dropper";
}
@Override
protected @NotNull ArenaGameMode parseGameMode(@NotNull String gameMode) {
return DropperArenaGameMode.matchGamemode(gameMode);
}
}

View File

@ -0,0 +1,75 @@
package net.knarcraft.minigames.placeholder;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.parkour.ParkourArena;
import net.knarcraft.minigames.arena.parkour.ParkourArenaGameMode;
import net.knarcraft.minigames.arena.parkour.ParkourArenaHandler;
import net.knarcraft.minigames.placeholder.parsing.PlayerPlaceholderParser;
import net.knarcraft.minigames.placeholder.parsing.RecordPlaceholderParser;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* A placeholderAPI expansion for Parkour-related placeholders
*/
public class ParkourExpansion extends PlaceholderExpansion {
private final @NotNull RecordPlaceholderParser recordPlaceholderParser;
private final @NotNull PlayerPlaceholderParser<ParkourArena> playerPlaceholderParser;
/**
* Instantiates a new dropper expansion
*
* @param plugin <p>A reference to the mini-games plugin</p>
*/
public ParkourExpansion(@NotNull MiniGames plugin) {
ParkourArenaHandler arenaHandler = plugin.getParkourArenaHandler();
this.recordPlaceholderParser = new RecordPlaceholderParser(arenaHandler, ParkourArenaGameMode::matchGameMode);
this.playerPlaceholderParser = new PlayerPlaceholderParser<>(arenaHandler, ParkourArenaGameMode::matchGameMode,
plugin.getParkourArenaPlayerRegistry());
}
/**
* Clears record caches
*/
public void clearCaches() {
this.recordPlaceholderParser.clearCaches();
}
@Override
public String getIdentifier() {
return "parkour";
}
@Override
public String getAuthor() {
return "EpicKnarvik97";
}
@Override
public String getVersion() {
return "1.0.0";
}
@Override
public boolean persist() {
return true;
}
@Override
@Nullable
public String onRequest(OfflinePlayer player, String parameters) {
String[] parts = parameters.split("_");
// Record is used as the prefix for all record placeholders in case more placeholder types are added
if (parts[0].equalsIgnoreCase("record") && parts.length >= 7) {
return recordPlaceholderParser.onRequest(parts);
} else if (parts[0].equalsIgnoreCase("players")) {
return this.playerPlaceholderParser.onRequest(parts);
} else {
return parameters;
}
}
}

View File

@ -1,32 +0,0 @@
package net.knarcraft.minigames.placeholder;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaGameMode;
import net.knarcraft.minigames.arena.parkour.ParkourArenaGameMode;
import org.jetbrains.annotations.NotNull;
/**
* A placeholder expansion for parkour record placeholders
*/
public class ParkourRecordExpansion extends RecordExpansion {
/**
* Initializes a new record expansion
*
* @param plugin <p>A reference to the dropper plugin</p>
*/
public ParkourRecordExpansion(MiniGames plugin) {
super(plugin.getParkourArenaHandler());
}
@Override
public String getIdentifier() {
return "parkour";
}
@Override
protected @NotNull ArenaGameMode parseGameMode(@NotNull String gameMode) {
return ParkourArenaGameMode.matchGamemode(gameMode);
}
}

View File

@ -0,0 +1,37 @@
package net.knarcraft.minigames.placeholder.parsing;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* The types of player information that can be retrieved in placeholders
*/
public enum PlayerInfoType {
/**
* The number of current players
*/
COUNT,
/**
* Information about a single player
*/
PLAYER,
;
/**
* Gets the info type specified in the given string
*
* @param type <p>The string specifying the info type</p>
* @return <p>The info type, or null if not found</p>
*/
public static @Nullable PlayerInfoType getFromString(@NotNull String type) {
for (PlayerInfoType infoType : PlayerInfoType.values()) {
if (infoType.name().equalsIgnoreCase(type)) {
return infoType;
}
}
return null;
}
}

View File

@ -0,0 +1,303 @@
package net.knarcraft.minigames.placeholder.parsing;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.Arena;
import net.knarcraft.minigames.arena.ArenaGameMode;
import net.knarcraft.minigames.arena.ArenaGroup;
import net.knarcraft.minigames.arena.ArenaHandler;
import net.knarcraft.minigames.arena.ArenaPlayerRegistry;
import net.knarcraft.minigames.arena.ArenaSession;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.logging.Level;
/**
* A parser for player-related placeholders
*/
public class PlayerPlaceholderParser<K extends Arena> {
private final @NotNull ArenaHandler<K, ?> arenaHandler;
private final @NotNull Function<String, ArenaGameMode> gameModeParser;
private final ArenaPlayerRegistry<K> playerRegistry;
/**
* Initializes a new player placeholder parser
*
* @param arenaHandler <p>The arena handler to get arenas from</p>
* @param gameModeParser <p>The function to use for parsing the specified game-mode</p>
* @param playerRegistry <p>The player registry to get player info from</p>
*/
public PlayerPlaceholderParser(@NotNull ArenaHandler<K, ?> arenaHandler,
@NotNull Function<String, ArenaGameMode> gameModeParser,
ArenaPlayerRegistry<K> playerRegistry) {
this.arenaHandler = arenaHandler;
this.gameModeParser = gameModeParser;
this.playerRegistry = playerRegistry;
}
/**
* The method to run when parsing a record placeholder request
*
* @param parts <p>The split parameters, without irrelevant info</p>
* @return <p>The resulting string</p>
*/
@Nullable
public String onRequest(@NotNull String[] parts) {
if (parts.length < 2) {
return null;
}
String selector = parts[1];
if (parts.length >= 6 && selector.equalsIgnoreCase("playing")) {
return getPlayingPlayersInfo(parts);
} else if (parts.length >= 3 && selector.equalsIgnoreCase("maximum")) {
return getMaxPlayersInfo(parts);
} else {
return null;
}
}
/**
* Gets placeholder info about max players
*
* @param parts <p>The split parameters, without irrelevant info</p>
* @return <p>The resulting string</p>
*/
@Nullable
private String getMaxPlayersInfo(@NotNull String[] parts) {
String info = null;
K arena = arenaHandler.getArena(parts[2]);
if (arena != null) {
info = String.valueOf(arena.getMaxPlayers());
}
return info;
}
/**
* Gets placeholder info about playing players
*
* @param parts <p>The split parameters, without irrelevant info</p>
* @return <p>The resulting string</p>
*/
@Nullable
private String getPlayingPlayersInfo(@NotNull String[] parts) {
String gameModeName = parts[2];
ArenaGameMode gameMode = gameModeParser.apply(gameModeName);
if (gameModeName.equalsIgnoreCase("combined") || gameModeName.equalsIgnoreCase("all")) {
gameMode = null;
}
SelectionType selectionType = SelectionType.getFromString(parts[3]);
String identifier = parts[4];
// The type of info to get. Either count (number of players) or player_position (a named player).
PlayerInfoType infoType = PlayerInfoType.getFromString(parts[5]);
if (infoType == null) {
return null;
}
String info = null;
if (selectionType == SelectionType.GROUP) {
ArenaGroup<?, ?> group = arenaHandler.getGroup(identifier);
if (group != null) {
info = getGroupInfo(group, gameMode, infoType, parts);
}
} else if (selectionType == SelectionType.ARENA) {
info = getArenaInfo(identifier, gameMode, infoType, parts);
}
return info;
}
/**
* Gets information about an arena group's players
*
* @param group <p>The group to get info about</p>
* @param gameMode <p>The game-mode to get information for</p>
* @param infoType <p>The type of information to get</p>
* @param parts <p>The placeholder arguments specified by a user</p>
* @return <p>The specified group info, or null if the placeholder is invalid</p>
*/
@Nullable
private String getGroupInfo(@NotNull ArenaGroup<?, ?> group, @Nullable ArenaGameMode gameMode,
@NotNull PlayerInfoType infoType, @NotNull String[] parts) {
List<UUID> arenaIds = group.getArenas();
List<K> arenas = new ArrayList<>();
for (UUID arenaId : arenaIds) {
K arena = arenaHandler.getArena(arenaId);
if (arena != null) {
arenas.add(arena);
}
}
if (infoType == PlayerInfoType.COUNT) {
int playerCount = 0;
for (K arena : arenas) {
playerCount += getArenaPlayers(arena, gameMode).size();
}
return String.valueOf(playerCount);
} else if (infoType == PlayerInfoType.PLAYER) {
if (parts.length < 7) {
return null;
}
Integer playerNumber = getPositionNumber(parts[6]);
List<String> arenaPlayerNames = new ArrayList<>();
for (K arena : arenas) {
arenaPlayerNames.addAll(getArenaPlayersSorted(arena, gameMode));
}
arenaPlayerNames.sort(Comparator.naturalOrder());
if (playerNumber != null) {
if (playerNumber >= arenaPlayerNames.size()) {
return "";
} else {
return arenaPlayerNames.get(playerNumber);
}
}
}
return null;
}
/**
* Gets information about an arena's players
*
* @param arenaName <p>The name of the arena</p>
* @param gameMode <p>The game-mode to get information for</p>
* @param infoType <p>The type of information to get</p>
* @param parts <p>The placeholder arguments specified by a user</p>
* @return <p>The specified arena info, or null if the placeholder is invalid</p>
*/
@Nullable
private String getArenaInfo(@NotNull String arenaName, @Nullable ArenaGameMode gameMode,
@NotNull PlayerInfoType infoType, @NotNull String[] parts) {
if (infoType == PlayerInfoType.COUNT) {
Set<UUID> arenaPlayers = getArenaPlayers(arenaName, gameMode);
if (arenaPlayers != null) {
return String.valueOf(arenaPlayers.size());
}
} else if (infoType == PlayerInfoType.PLAYER) {
if (parts.length < 7) {
return null;
}
Integer playerNumber = getPositionNumber(parts[6]);
List<String> players = getArenaPlayersSorted(arenaName, gameMode);
if (playerNumber != null && players != null) {
if (playerNumber >= players.size()) {
return "";
} else {
return players.get(playerNumber);
}
}
}
return null;
}
/**
* Gets the position number from the given string
*
* @param positionNumber <p>The position number to parse</p>
* @return <p>The position number, or null if not valid</p>
*/
@Nullable
private Integer getPositionNumber(@NotNull String positionNumber) {
try {
return Integer.parseInt(positionNumber) - 1;
} catch (NumberFormatException exception) {
MiniGames.log(Level.WARNING, "Invalid placeholder given. " + positionNumber +
" supplied instead of player number.");
return null;
}
}
/**
* Gets names of all players in an arena in sorted order
*
* @param arenaName <p>The name of the arena to get players from</p>
* @param arenaGameMode <p>The game-mode to get players for</p>
* @return <p>Player names in sorted order, or null if the arena name is invalid</p>
*/
@Nullable
private List<String> getArenaPlayersSorted(@NotNull String arenaName, @Nullable ArenaGameMode arenaGameMode) {
K arena = arenaHandler.getArena(arenaName);
if (arena == null) {
return null;
}
return getArenaPlayersSorted(arena, arenaGameMode);
}
/**
* Gets names of all players in an arena in sorted order
*
* @param arena <p>The arena to get players from</p>
* @param arenaGameMode <p>The game-mode to get players for</p>
* @return <p>Player names in sorted order, or null if the arena name is invalid</p>
*/
@NotNull
private List<String> getArenaPlayersSorted(@NotNull K arena, @Nullable ArenaGameMode arenaGameMode) {
Set<UUID> players = getArenaPlayers(arena, arenaGameMode);
List<String> playerNames = new ArrayList<>(players.size());
for (UUID playerId : players) {
Player player = Bukkit.getPlayer(playerId);
if (player != null) {
playerNames.add(player.getName());
}
}
playerNames.sort(Comparator.naturalOrder());
return playerNames;
}
/**
* Gets all players from the given arena
*
* @param arenaName <p>The name of the arena to get players from</p>
* @param arenaGameMode <p>The game-mode to get players for</p>
* @return <p>The players in the given arena playing the given game-mode</p>
*/
@Nullable
private Set<UUID> getArenaPlayers(@NotNull String arenaName, @Nullable ArenaGameMode arenaGameMode) {
K arena = arenaHandler.getArena(arenaName);
if (arena == null) {
return null;
}
return getArenaPlayers(arena, arenaGameMode);
}
/**
* Gets all players from the given arena
*
* @param arena <p>The arena to get players from</p>
* @param arenaGameMode <p>The game-mode to get players for</p>
* @return <p>The players in the given arena playing the given game-mode</p>
*/
@NotNull
private Set<UUID> getArenaPlayers(@NotNull K arena, @Nullable ArenaGameMode arenaGameMode) {
// If getting count for any game-mode, skip filtering
Set<UUID> players = playerRegistry.getPlayingPlayers(arena);
if (arenaGameMode == null) {
return players;
}
Set<UUID> output = new HashSet<>();
for (UUID playerId : players) {
ArenaSession arenaSession = playerRegistry.getArenaSession(playerId);
if (arenaSession == null || arenaSession.getGameMode() != arenaGameMode) {
continue;
}
output.add(playerId);
}
return output;
}
}

View File

@ -1,18 +1,16 @@
package net.knarcraft.minigames.placeholder; package net.knarcraft.minigames.placeholder.parsing;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.Arena; import net.knarcraft.minigames.arena.Arena;
import net.knarcraft.minigames.arena.ArenaGameMode; import net.knarcraft.minigames.arena.ArenaGameMode;
import net.knarcraft.minigames.arena.ArenaGroup; import net.knarcraft.minigames.arena.ArenaGroup;
import net.knarcraft.minigames.arena.ArenaHandler; import net.knarcraft.minigames.arena.ArenaHandler;
import net.knarcraft.minigames.arena.ArenaRecordsRegistry; import net.knarcraft.minigames.arena.ArenaRecordsRegistry;
import net.knarcraft.minigames.arena.record.ArenaRecord; import net.knarcraft.minigames.arena.record.ArenaRecord;
import net.knarcraft.minigames.placeholder.parsing.InfoType; import net.knarcraft.minigames.placeholder.GroupRecordCache;
import net.knarcraft.minigames.placeholder.parsing.SelectionType;
import net.knarcraft.minigames.property.RecordType; import net.knarcraft.minigames.property.RecordType;
import net.knarcraft.minigames.util.GroupRecordHelper; import net.knarcraft.minigames.util.GroupRecordHelper;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -22,60 +20,61 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.logging.Level;
/** /**
* A placeholder expansion for parkour record placeholders * A parser for record-related placeholders
*/ */
public abstract class RecordExpansion extends PlaceholderExpansion { public class RecordPlaceholderParser {
private final ArenaHandler<?, ?> arenaHandler; private final @NotNull ArenaHandler<?, ?> arenaHandler;
private final Map<UUID, Set<GroupRecordCache<Integer>>> groupRecordDeathsCache; private final @NotNull Map<UUID, Set<GroupRecordCache<Integer>>> groupRecordDeathsCache;
private final Map<UUID, Set<GroupRecordCache<Long>>> groupRecordTimeCache; private final @NotNull Map<UUID, Set<GroupRecordCache<Long>>> groupRecordTimeCache;
private final @NotNull Function<String, ArenaGameMode> gameModeParser;
/** /**
* Initializes a new record expansion * Initializes a new record placeholder parser
*/ */
public RecordExpansion(ArenaHandler<?, ?> arenaHandler) { public RecordPlaceholderParser(@NotNull ArenaHandler<?, ?> arenaHandler,
@NotNull Function<String, ArenaGameMode> gameModeParser) {
this.groupRecordDeathsCache = new HashMap<>(); this.groupRecordDeathsCache = new HashMap<>();
this.groupRecordTimeCache = new HashMap<>(); this.groupRecordTimeCache = new HashMap<>();
this.arenaHandler = arenaHandler; this.arenaHandler = arenaHandler;
this.gameModeParser = gameModeParser;
} }
@Override /**
public String getAuthor() { * The method to run when parsing a record placeholder request
return "EpicKnarvik97"; *
* @param parts <p>All parts of the placeholder</p>
* @return <p>The resulting string</p>
*/
@Nullable
public String onRequest(@NotNull String[] parts) {
if (parts.length < 7) {
return null;
} }
@Override
public String getVersion() {
return "1.0.0";
}
@Override
public boolean persist() {
return true;
}
@Override
public String onRequest(OfflinePlayer player, String parameters) {
String[] parts = parameters.split("_");
// Record is used as the prefix for all record placeholders in case more placeholder types are added
if (parts.length < 7 || !parts[0].equals("record")) {
return parameters;
}
RecordType recordType = RecordType.getFromString(parts[1]); RecordType recordType = RecordType.getFromString(parts[1]);
ArenaGameMode gameMode = parseGameMode(parts[2]); ArenaGameMode gameMode = gameModeParser.apply(parts[2]);
SelectionType selectionType = SelectionType.getFromString(parts[3]); SelectionType selectionType = SelectionType.getFromString(parts[3]);
String identifier = parts[4]; String identifier = parts[4];
int recordNumber = Integer.parseInt(parts[5]) - 1; int recordNumber;
try {
recordNumber = Integer.parseInt(parts[5]) - 1;
} catch (NumberFormatException exception) {
MiniGames.log(Level.WARNING, "Invalid placeholder given. " + parts[5] +
" supplied instead of record position.");
return null;
}
InfoType infoType = InfoType.getFromString(parts[6]); InfoType infoType = InfoType.getFromString(parts[6]);
if (recordType == null || infoType == null) { if (recordType == null || infoType == null) {
return parameters; return null;
} }
String info = null; String info = null;
@ -85,17 +84,9 @@ public abstract class RecordExpansion extends PlaceholderExpansion {
info = getArenaRecord(arenaHandler, identifier, gameMode, recordType, recordNumber, infoType); info = getArenaRecord(arenaHandler, identifier, gameMode, recordType, recordNumber, infoType);
} }
return Objects.requireNonNullElse(info, parameters); return info;
} }
/**
* Parses the game-mode specified in the given string
*
* @param gameMode <p>The game-mode to parse</p>
* @return <p>The parsed game-mode</p>
*/
protected abstract @NotNull ArenaGameMode parseGameMode(@NotNull String gameMode);
/** /**
* Clears all record caches * Clears all record caches
*/ */
@ -111,11 +102,12 @@ public abstract class RecordExpansion extends PlaceholderExpansion {
* @param identifier <p>The identifier (name/uuid) selecting the group</p> * @param identifier <p>The identifier (name/uuid) selecting the group</p>
* @param gameMode <p>The game-mode to get a record for</p> * @param gameMode <p>The game-mode to get a record for</p>
* @param recordType <p>The type of record to get</p> * @param recordType <p>The type of record to get</p>
* @param recordNumber <p>The placing of the record to get (1st place, 2nd place, etc.)</p> * @param recordNumber <p>The position of the record to get (1st place, 2nd place, etc.)</p>
* @param infoType <p>The type of info (player, value, combined) to get</p> * @param infoType <p>The type of info (player, value, combined) to get</p>
* @return <p>The selected information about the record, or null if not found</p> * @return <p>The selected information about the record, or null if not found</p>
*/ */
private @Nullable String getGroupRecord(@NotNull ArenaHandler<?, ?> arenaHandler, @NotNull String identifier, @Nullable
private String getGroupRecord(@NotNull ArenaHandler<?, ?> arenaHandler, @NotNull String identifier,
@NotNull ArenaGameMode gameMode, @NotNull RecordType recordType, @NotNull ArenaGameMode gameMode, @NotNull RecordType recordType,
int recordNumber, @NotNull InfoType infoType) { int recordNumber, @NotNull InfoType infoType) {
// Allow specifying the group UUID or the arena name // Allow specifying the group UUID or the arena name
@ -149,11 +141,12 @@ public abstract class RecordExpansion extends PlaceholderExpansion {
* *
* @param group <p>The group to get the record from</p> * @param group <p>The group to get the record from</p>
* @param gameMode <p>The game-mode to get the record from</p> * @param gameMode <p>The game-mode to get the record from</p>
* @param recordNumber <p>The placing of the record to get (1st place, 2nd place, etc.)</p> * @param recordNumber <p>The position of the record to get (1st place, 2nd place, etc.)</p>
* @param arenaHandler <p>The handler to get arenas from</p> * @param arenaHandler <p>The handler to get arenas from</p>
* @return <p>The record, or null if not found</p> * @return <p>The record, or null if not found</p>
*/ */
private @Nullable ArenaRecord<?> getGroupTimeRecord(@NotNull ArenaGroup<?, ?> group, @Nullable
private ArenaRecord<?> getGroupTimeRecord(@NotNull ArenaGroup<?, ?> group,
@NotNull ArenaGameMode gameMode, int recordNumber, @NotNull ArenaGameMode gameMode, int recordNumber,
@NotNull ArenaHandler<?, ?> arenaHandler) { @NotNull ArenaHandler<?, ?> arenaHandler) {
return getCachedGroupRecord(group, gameMode, RecordType.TIME, recordNumber, groupRecordTimeCache, return getCachedGroupRecord(group, gameMode, RecordType.TIME, recordNumber, groupRecordTimeCache,
@ -165,11 +158,12 @@ public abstract class RecordExpansion extends PlaceholderExpansion {
* *
* @param group <p>The group to get the record from</p> * @param group <p>The group to get the record from</p>
* @param gameMode <p>The game-mode to get the record from</p> * @param gameMode <p>The game-mode to get the record from</p>
* @param recordNumber <p>The placing of the record to get (1st place, 2nd place, etc.)</p> * @param recordNumber <p>The position of the record to get (1st place, 2nd place, etc.)</p>
* @param arenaHandler <p>The handler to get arenas from</p> * @param arenaHandler <p>The handler to get arenas from</p>
* @return <p>The record, or null if not found</p> * @return <p>The record, or null if not found</p>
*/ */
private @Nullable ArenaRecord<?> getGroupDeathRecord(@NotNull ArenaGroup<?, ?> group, @Nullable
private ArenaRecord<?> getGroupDeathRecord(@NotNull ArenaGroup<?, ?> group,
@NotNull ArenaGameMode gameMode, int recordNumber, @NotNull ArenaGameMode gameMode, int recordNumber,
@NotNull ArenaHandler<?, ?> arenaHandler) { @NotNull ArenaHandler<?, ?> arenaHandler) {
return getCachedGroupRecord(group, gameMode, RecordType.DEATHS, recordNumber, groupRecordDeathsCache, return getCachedGroupRecord(group, gameMode, RecordType.DEATHS, recordNumber, groupRecordDeathsCache,
@ -182,13 +176,14 @@ public abstract class RecordExpansion extends PlaceholderExpansion {
* @param group <p>The group to get the record for</p> * @param group <p>The group to get the record for</p>
* @param gameMode <p>The game-mode to get the record for</p> * @param gameMode <p>The game-mode to get the record for</p>
* @param recordType <p>The type of record to get</p> * @param recordType <p>The type of record to get</p>
* @param recordNumber <p>The placing of the record to get (1st place, 2nd place, etc.)</p> * @param recordNumber <p>The position of the record to get (1st place, 2nd place, etc.)</p>
* @param caches <p>The caches to use for looking for and saving the record</p> * @param caches <p>The caches to use for looking for and saving the record</p>
* @param recordProvider <p>The provider of records if the cache cannot provide the record</p> * @param recordProvider <p>The provider of records if the cache cannot provide the record</p>
* @param <K> <p>The type of the provided records</p> * @param <K> <p>The type of the provided records</p>
* @return <p>The specified record, or null if not found</p> * @return <p>The specified record, or null if not found</p>
*/ */
private <K extends Comparable<K>> @Nullable ArenaRecord<?> getCachedGroupRecord(@NotNull ArenaGroup<?, ?> group, @Nullable
private <K extends Comparable<K>> ArenaRecord<?> getCachedGroupRecord(@NotNull ArenaGroup<?, ?> group,
@NotNull ArenaGameMode gameMode, @NotNull ArenaGameMode gameMode,
@NotNull RecordType recordType, @NotNull RecordType recordType,
int recordNumber, int recordNumber,
@ -231,11 +226,12 @@ public abstract class RecordExpansion extends PlaceholderExpansion {
* @param identifier <p>The identifier (name/uuid) selecting the arena</p> * @param identifier <p>The identifier (name/uuid) selecting the arena</p>
* @param gameMode <p>The game-mode to get a record for</p> * @param gameMode <p>The game-mode to get a record for</p>
* @param recordType <p>The type of record to get</p> * @param recordType <p>The type of record to get</p>
* @param recordNumber <p>The placing of the record to get (1st place, 2nd place, etc.)</p> * @param recordNumber <p>The position of the record to get (1st place, 2nd place, etc.)</p>
* @param infoType <p>The type of info (player, value, combined) to get</p> * @param infoType <p>The type of info (player, value, combined) to get</p>
* @return <p>The selected information about the record, or null if not found</p> * @return <p>The selected information about the record, or null if not found</p>
*/ */
private @Nullable String getArenaRecord(@NotNull ArenaHandler<?, ?> arenaHandler, @NotNull String identifier, @Nullable
private String getArenaRecord(@NotNull ArenaHandler<?, ?> arenaHandler, @NotNull String identifier,
@NotNull ArenaGameMode gameMode, @NotNull RecordType recordType, @NotNull ArenaGameMode gameMode, @NotNull RecordType recordType,
int recordNumber, @NotNull InfoType infoType) { int recordNumber, @NotNull InfoType infoType) {
// Allow specifying the arena UUID or the arena name // Allow specifying the arena UUID or the arena name
@ -266,10 +262,11 @@ public abstract class RecordExpansion extends PlaceholderExpansion {
* *
* @param recordsRegistry <p>The records registry to get the record from</p> * @param recordsRegistry <p>The records registry to get the record from</p>
* @param recordType <p>The type of record to get</p> * @param recordType <p>The type of record to get</p>
* @param recordNumber <p>The placing of the record to get (1st place, 2nd place, etc.)</p> * @param recordNumber <p>The position of the record to get (1st place, 2nd place, etc.)</p>
* @return <p>The record, or null if not found</p> * @return <p>The record, or null if not found</p>
*/ */
private @Nullable ArenaRecord<?> getRecord(@NotNull ArenaRecordsRegistry recordsRegistry, @Nullable
private ArenaRecord<?> getRecord(@NotNull ArenaRecordsRegistry recordsRegistry,
@NotNull RecordType recordType, int recordNumber) { @NotNull RecordType recordType, int recordNumber) {
return switch (recordType) { return switch (recordType) {
case TIME -> getRecord(new HashSet<>(recordsRegistry.getShortestTimeMilliSecondsRecords()), recordNumber); case TIME -> getRecord(new HashSet<>(recordsRegistry.getShortestTimeMilliSecondsRecords()), recordNumber);
@ -285,7 +282,8 @@ public abstract class RecordExpansion extends PlaceholderExpansion {
* @param <K> <p>The type of record in the record list</p> * @param <K> <p>The type of record in the record list</p>
* @return <p>The record, or null if index is out of bounds</p> * @return <p>The record, or null if index is out of bounds</p>
*/ */
private <K extends Comparable<K>> @Nullable ArenaRecord<K> getRecord(Set<ArenaRecord<K>> records, int index) { @Nullable
private <K extends Comparable<K>> ArenaRecord<K> getRecord(@NotNull Set<ArenaRecord<K>> records, int index) {
List<ArenaRecord<K>> sorted = getSortedRecords(records); List<ArenaRecord<K>> sorted = getSortedRecords(records);
if (index < sorted.size() && index >= 0) { if (index < sorted.size() && index >= 0) {
return sorted.get(index); return sorted.get(index);
@ -301,11 +299,12 @@ public abstract class RecordExpansion extends PlaceholderExpansion {
* @param arenaRecord <p>The record to get the data from</p> * @param arenaRecord <p>The record to get the data from</p>
* @return <p>The requested data as a string, or null</p> * @return <p>The requested data as a string, or null</p>
*/ */
@Nullable
private String getRecordData(@NotNull InfoType infoType, @NotNull ArenaRecord<?> arenaRecord) { private String getRecordData(@NotNull InfoType infoType, @NotNull ArenaRecord<?> arenaRecord) {
return switch (infoType) { return switch (infoType) {
case PLAYER -> getPlayerName(arenaRecord.getUserId()); case PLAYER -> getPlayerName(arenaRecord.getUserId());
case VALUE -> arenaRecord.getRecord().toString(); case VALUE -> arenaRecord.getAsString();
case COMBINED -> getPlayerName(arenaRecord.getUserId()) + ": " + arenaRecord.getRecord().toString(); case COMBINED -> getPlayerName(arenaRecord.getUserId()) + ": " + arenaRecord.getAsString();
}; };
} }
@ -316,8 +315,8 @@ public abstract class RecordExpansion extends PlaceholderExpansion {
* @param <K> <p>The type of the records</p> * @param <K> <p>The type of the records</p>
* @return <p>The sorted records</p> * @return <p>The sorted records</p>
*/ */
private <K extends Comparable<K>> @NotNull List<ArenaRecord<K>> getSortedRecords( @NotNull
@NotNull Set<ArenaRecord<K>> recordSet) { private <K extends Comparable<K>> List<ArenaRecord<K>> getSortedRecords(@NotNull Set<ArenaRecord<K>> recordSet) {
List<ArenaRecord<K>> records = new ArrayList<>(recordSet); List<ArenaRecord<K>> records = new ArrayList<>(recordSet);
Collections.sort(records); Collections.sort(records);
return records; return records;
@ -329,6 +328,7 @@ public abstract class RecordExpansion extends PlaceholderExpansion {
* @param playerId <p>The id of the player to get the name for</p> * @param playerId <p>The id of the player to get the name for</p>
* @return <p>The name of the player, or a string representation of the UUID if not found</p> * @return <p>The name of the player, or a string representation of the UUID if not found</p>
*/ */
@Nullable
private String getPlayerName(@NotNull UUID playerId) { private String getPlayerName(@NotNull UUID playerId) {
return Bukkit.getOfflinePlayer(playerId).getName(); return Bukkit.getOfflinePlayer(playerId).getName();
} }

View File

@ -0,0 +1,45 @@
package net.knarcraft.minigames.property;
/**
* An enum for all persistent data keys used by this plugin
*/
public enum PersistentDataKey {
MENU_ITEM("MiniGamesMenu", 1799804),
LEAVE_ITEM("MiniGamesAction", 1799871),
GIVE_UP_ITEM("MiniGamesAction", 1799872),
;
private final String keyName;
private final int dataValue;
/**
* Instantiates a new persistent data key
*
* @param keyName <p>The name of this key</p>
* @param dataValue <p>The integer data value of this key</p>
*/
PersistentDataKey(String keyName, int dataValue) {
this.keyName = keyName;
this.dataValue = dataValue;
}
/**
* Gets the name of this persistent data key
*
* @return <p>The name of this key</p>
*/
public String getKeyName() {
return this.keyName;
}
/**
* Gets the integer data value of this persistent data key
*
* @return <p>The integer data value</p>
*/
public int getDataValue() {
return this.dataValue;
}
}

View File

@ -2,8 +2,12 @@ package net.knarcraft.minigames.util;
import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.PlayerEntryState; import net.knarcraft.minigames.arena.PlayerEntryState;
import net.knarcraft.minigames.arena.StorageKey;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.entity.EntityDamageEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -35,7 +39,7 @@ public final class ArenaStorageHelper {
try { try {
configuration.save(new File(MiniGames.getInstance().getDataFolder(), key + "EntryStates.yml")); configuration.save(new File(MiniGames.getInstance().getDataFolder(), key + "EntryStates.yml"));
} catch (IOException e) { } catch (IOException exception) {
MiniGames.log(Level.SEVERE, "Unable to save entry states to disk"); MiniGames.log(Level.SEVERE, "Unable to save entry states to disk");
} }
} }
@ -78,4 +82,40 @@ public final class ArenaStorageHelper {
return arenaDataFile; return arenaDataFile;
} }
/**
* Loads a set of strings from the given configuration section
*
* @param configurationSection <p>The configuration section to load from</p>
* @param storageKey <p>The key to the info to load</p>
* @return <p>The loaded items, or null if not set</p>
*/
@Nullable
@SuppressWarnings("unchecked")
public static Set<String> loadStrings(@NotNull ConfigurationSection configurationSection,
@NotNull StorageKey storageKey) {
List<?> rawList = configurationSection.getList(storageKey.getKey());
Set<String> output;
if (rawList == null) {
output = null;
} else {
output = new HashSet<>((List<String>) rawList);
}
return output;
}
/**
* Gets the names of the given damage causes
*
* @param causes <p>The damage causes to get names of</p>
* @return <p>The names of the damage causes</p>
*/
@NotNull
public static List<String> getDamageCauseNames(@NotNull Set<EntityDamageEvent.DamageCause> causes) {
List<String> output = new ArrayList<>(causes.size());
for (EntityDamageEvent.DamageCause cause : causes) {
output.add(cause.name());
}
return output;
}
} }

View File

@ -1,29 +0,0 @@
package net.knarcraft.minigames.util;
import net.md_5.bungee.api.ChatColor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* A helper class for converting colors
*/
public class ColorHelper {
/**
* Translates all found color codes to formatting in a string
*
* @param message <p>The string to search for color codes</p>
* @return <p>The message with color codes translated</p>
*/
public static String translateAllColorCodes(String message) {
message = ChatColor.translateAlternateColorCodes('&', message);
Pattern pattern = Pattern.compile("&?(#[a-fA-F0-9]{6})");
Matcher matcher = pattern.matcher(message);
while (matcher.find()) {
message = message.replace(matcher.group(), "" + ChatColor.of(matcher.group(1)));
}
return message;
}
}

View File

@ -9,8 +9,9 @@ import net.knarcraft.minigames.arena.dropper.DropperArenaGameMode;
import net.knarcraft.minigames.arena.dropper.DropperArenaGroup; import net.knarcraft.minigames.arena.dropper.DropperArenaGroup;
import net.knarcraft.minigames.arena.dropper.DropperArenaRecordsRegistry; import net.knarcraft.minigames.arena.dropper.DropperArenaRecordsRegistry;
import net.knarcraft.minigames.arena.dropper.DropperArenaStorageKey; import net.knarcraft.minigames.arena.dropper.DropperArenaStorageKey;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.arena.reward.Reward;
import net.knarcraft.minigames.container.PlaceholderContainer; import net.knarcraft.minigames.arena.reward.RewardCondition;
import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.container.SerializableMaterial; import net.knarcraft.minigames.container.SerializableMaterial;
import net.knarcraft.minigames.container.SerializableUUID; import net.knarcraft.minigames.container.SerializableUUID;
import org.bukkit.Location; import org.bukkit.Location;
@ -19,6 +20,7 @@ import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.yaml.snakeyaml.error.YAMLException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -30,6 +32,7 @@ import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
import static net.knarcraft.minigames.util.ArenaStorageHelper.getArenaDataFile; import static net.knarcraft.minigames.util.ArenaStorageHelper.getArenaDataFile;
import static net.knarcraft.minigames.util.ArenaStorageHelper.loadStrings;
/** /**
* A helper class for saving and loading arenas * A helper class for saving and loading arenas
@ -96,6 +99,35 @@ public final class DropperArenaStorageHelper {
YamlConfiguration configuration = new YamlConfiguration(); YamlConfiguration configuration = new YamlConfiguration();
ConfigurationSection arenaSection = configuration.createSection(dropperArenasConfigurationSection); ConfigurationSection arenaSection = configuration.createSection(dropperArenasConfigurationSection);
for (DropperArena arena : arenas.values()) { for (DropperArena arena : arenas.values()) {
saveDropperArena(arenaSection, arena);
}
configuration.save(dropperArenaFile);
}
/**
* Saves a single arena
*
* @param arena <p>The arena to save</p>
* @throws IOException <p>If unable to write to the file</p>
*/
public static void saveSingleDropperArena(DropperArena arena) throws IOException {
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(dropperArenaFile);
ConfigurationSection arenaSection = configuration.getConfigurationSection(dropperArenasConfigurationSection);
if (arenaSection == null) {
arenaSection = configuration.createSection(dropperArenasConfigurationSection);
}
saveDropperArena(arenaSection, arena);
configuration.save(dropperArenaFile);
}
/**
* Updates the given configuration section with the arena's data, and stores arena data for the arena
*
* @param arenaSection <p>The configuration section to update</p>
* @param arena <p>The arena to save</p>
* @throws IOException <p>If unable to save the arena data</p>
*/
private static void saveDropperArena(ConfigurationSection arenaSection, DropperArena arena) throws IOException {
//Note: While the arena name is used as the key, as the key has to be sanitized, the un-sanitized arena name //Note: While the arena name is used as the key, as the key has to be sanitized, the un-sanitized arena name
// must be stored as well // must be stored as well
@NotNull ConfigurationSection configSection = arenaSection.createSection(arena.getArenaId().toString()); @NotNull ConfigurationSection configSection = arenaSection.createSection(arena.getArenaId().toString());
@ -106,10 +138,14 @@ public final class DropperArenaStorageHelper {
configSection.set(DropperArenaStorageKey.PLAYER_VERTICAL_VELOCITY.getKey(), arena.getPlayerVerticalVelocity()); configSection.set(DropperArenaStorageKey.PLAYER_VERTICAL_VELOCITY.getKey(), arena.getPlayerVerticalVelocity());
configSection.set(DropperArenaStorageKey.PLAYER_HORIZONTAL_VELOCITY.getKey(), arena.getPlayerHorizontalVelocity()); configSection.set(DropperArenaStorageKey.PLAYER_HORIZONTAL_VELOCITY.getKey(), arena.getPlayerHorizontalVelocity());
configSection.set(DropperArenaStorageKey.WIN_BLOCK_TYPE.getKey(), new SerializableMaterial(arena.getWinBlockType())); configSection.set(DropperArenaStorageKey.WIN_BLOCK_TYPE.getKey(), new SerializableMaterial(arena.getWinBlockType()));
configSection.set(DropperArenaStorageKey.MAX_PLAYERS.getKey(), arena.getMaxPlayers());
configSection.set(DropperArenaStorageKey.ALLOWED_DAMAGE_CAUSES.getKey(),
ArenaStorageHelper.getDamageCauseNames(arena.getAllowedDamageCauses()));
configSection.set(DropperArenaStorageKey.LOSS_TRIGGER_DAMAGE_CAUSES.getKey(),
ArenaStorageHelper.getDamageCauseNames(arena.getLossTriggerDamageCauses()));
RewardStorageHelper.saveRewards(arena, configSection, DropperArenaStorageKey.REWARDS.getKey());
saveDropperArenaData(arena.getData()); saveDropperArenaData(arena.getData());
} }
configuration.save(dropperArenaFile);
}
/** /**
* Loads all arenas * Loads all arenas
@ -157,28 +193,37 @@ public final class DropperArenaStorageHelper {
double verticalVelocity = configurationSection.getDouble(DropperArenaStorageKey.PLAYER_VERTICAL_VELOCITY.getKey()); double verticalVelocity = configurationSection.getDouble(DropperArenaStorageKey.PLAYER_VERTICAL_VELOCITY.getKey());
float horizontalVelocity = sanitizeHorizontalVelocity((float) configurationSection.getDouble( float horizontalVelocity = sanitizeHorizontalVelocity((float) configurationSection.getDouble(
DropperArenaStorageKey.PLAYER_HORIZONTAL_VELOCITY.getKey())); DropperArenaStorageKey.PLAYER_HORIZONTAL_VELOCITY.getKey()));
int maxPlayers = configurationSection.getInt(DropperArenaStorageKey.MAX_PLAYERS.getKey(), -1);
SerializableMaterial winBlockType = (SerializableMaterial) configurationSection.get( SerializableMaterial winBlockType = (SerializableMaterial) configurationSection.get(
DropperArenaStorageKey.WIN_BLOCK_TYPE.getKey()); DropperArenaStorageKey.WIN_BLOCK_TYPE.getKey());
if (arenaName == null || spawnLocation == null) { if (arenaName == null || spawnLocation == null) {
MiniGames.log(Level.SEVERE, Message.ERROR_ARENA_NOT_LOADED.getMessage(new PlaceholderContainer().add( MiniGames.log(Level.SEVERE, MiniGames.getInstance().getStringFormatter().replacePlaceholders(
"{section}", configurationSection.getName()).add("{file}", "dropper_arenas"))); MiniGameMessage.ERROR_ARENA_NOT_LOADED, new String[]{"{section}", "{file}"},
new String[]{configurationSection.getName(), "dropper_arenas"}));
return null; return null;
} }
if (winBlockType == null) { if (winBlockType == null) {
winBlockType = new SerializableMaterial(Material.WATER); winBlockType = new SerializableMaterial(Material.WATER);
} }
Map<RewardCondition, Set<Reward>> rewards = RewardStorageHelper.loadRewards(configurationSection,
DropperArenaStorageKey.REWARDS.getKey());
// Generate new, empty arena data if not available // Generate new, empty arena data if not available
DropperArenaData arenaData = loadDropperArenaData(arenaId); DropperArenaData arenaData = loadDropperArenaData(arenaId);
if (arenaData == null) { if (arenaData == null) {
MiniGames.log(Level.SEVERE, Message.ERROR_ARENA_DATA_NOT_LOADED.getMessage("{arena}", MiniGames.log(Level.SEVERE, MiniGames.getInstance().getStringFormatter().replacePlaceholder(
arenaId.toString())); MiniGameMessage.ERROR_ARENA_DATA_NOT_LOADED, "{arena}", arenaId.toString()));
arenaData = getEmptyDropperData(arenaId); arenaData = getEmptyDropperData(arenaId);
} }
Set<String> allowedDamageCauseNames = loadStrings(configurationSection, DropperArenaStorageKey.ALLOWED_DAMAGE_CAUSES);
Set<String> lossTriggerDamageCauseNames = loadStrings(configurationSection, DropperArenaStorageKey.LOSS_TRIGGER_DAMAGE_CAUSES);
return new DropperArena(arenaId, arenaName, spawnLocation, exitLocation, verticalVelocity, horizontalVelocity, return new DropperArena(arenaId, arenaName, spawnLocation, exitLocation, verticalVelocity, horizontalVelocity,
winBlockType.getRawValue(), arenaData, MiniGames.getInstance().getDropperArenaHandler()); winBlockType.getRawValue(), maxPlayers, rewards, arenaData,
MiniGames.getInstance().getDropperArenaHandler(), allowedDamageCauseNames, lossTriggerDamageCauseNames);
} }
/** /**
@ -216,9 +261,14 @@ public final class DropperArenaStorageHelper {
* @return <p>The loaded arena data</p> * @return <p>The loaded arena data</p>
*/ */
private static @Nullable DropperArenaData loadDropperArenaData(@NotNull UUID arenaId) { private static @Nullable DropperArenaData loadDropperArenaData(@NotNull UUID arenaId) {
try {
File arenaDataFile = getDropperArenaDataFile(arenaId); File arenaDataFile = getDropperArenaDataFile(arenaId);
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(arenaDataFile); YamlConfiguration configuration = YamlConfiguration.loadConfiguration(arenaDataFile);
return (DropperArenaData) configuration.get(DropperArenaStorageKey.DATA.getKey()); return (DropperArenaData) configuration.get(DropperArenaStorageKey.DATA.getKey());
} catch (YAMLException exception) {
MiniGames.log(Level.SEVERE, "Unable to load arena data from arena " + arenaId);
return null;
}
} }
/** /**

View File

@ -0,0 +1,95 @@
package net.knarcraft.minigames.util;
import net.knarcraft.knargui.item.GUIItemFactory;
import net.knarcraft.knargui.item.PlayerHeadGUIItemFactory;
import net.knarcraft.knargui.item.SimpleGUIItemFactory;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.arena.ArenaSession;
import net.knarcraft.minigames.gui.MiniGamesGUI;
import net.knarcraft.minigames.property.PersistentDataKey;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
/**
* A helper class for the in-arena GUI
*/
public final class GUIHelper {
private GUIHelper() {
}
/**
* Gets the GUI open item to give to the specified player
*
* @param player <p>The player to give a GUI opening item</p>
* @return <p>The item to give</p>
*/
public static ItemStack getGUIOpenItem(@NotNull Player player) {
if (GeyserHelper.isGeyserPlayer(player)) {
return getGUIOpenItemBedrock();
} else {
return getGUIOpenItemJava();
}
}
/**
* Gets the item used for opening the mini-games menu
*
* @return <p>The item used for opening the GUI</p>
*/
public static ItemStack getGUIOpenItemJava() {
PlayerHeadGUIItemFactory factory = new PlayerHeadGUIItemFactory();
factory.useSkin("3fdab40434ed5d01f58c45ca0c9fada4662e1772ff43e2974979440a5cfe15c9");
factory.setName(ChatColor.AQUA + "§ MiniGames Menu §");
ItemStack item = factory.build();
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.getPersistentDataContainer().set(new NamespacedKey(MiniGames.getInstance(),
PersistentDataKey.MENU_ITEM.getKeyName()),
PersistentDataType.INTEGER, PersistentDataKey.MENU_ITEM.getDataValue());
}
item.setItemMeta(meta);
return item;
}
/**
* Gets the item used for opening the mini-games menu
*
* @return <p>The item used for opening the GUI</p>
*/
public static ItemStack getGUIOpenItemBedrock() {
GUIItemFactory factory = new SimpleGUIItemFactory(Material.BEACON);
factory.setName(ChatColor.AQUA + "MiniGames Menu");
ItemStack item = factory.build();
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.getPersistentDataContainer().set(new NamespacedKey(MiniGames.getInstance(),
PersistentDataKey.MENU_ITEM.getKeyName()),
PersistentDataType.INTEGER, PersistentDataKey.MENU_ITEM.getDataValue());
}
item.setItemMeta(meta);
return item;
}
/**
* Opens the correct GUI for the given player
*
* @param player <p>The player to show a GUI for</p>
*/
public static void openGUI(Player player) {
ArenaSession existingSession = MiniGames.getInstance().getSession(player.getUniqueId());
if (existingSession == null) {
new MiniGamesGUI(player).openFor(player);
} else {
existingSession.getGUI().openFor(player);
}
}
}

View File

@ -0,0 +1,53 @@
package net.knarcraft.minigames.util;
import org.bukkit.entity.Player;
import org.geysermc.floodgate.api.FloodgateApi;
import org.geysermc.geyser.api.GeyserApi;
import org.jetbrains.annotations.NotNull;
/**
* A helper class for dealing with geyser/floodgate players
*/
public final class GeyserHelper {
private static boolean hasGeyser = true;
private static boolean hasFloodgate = true;
private GeyserHelper() {
}
/**
* Checks whether the given player is connected through Geyser
*
* @param player <p>The player to check</p>
* @return <p>True if the player is connected through Geyser</p>
*/
public static boolean isGeyserPlayer(@NotNull Player player) {
// Prevent unnecessary checking for non-geyser and floodgate servers
if (!hasGeyser && !hasFloodgate) {
return false;
}
// Use Geyser API to get connection status
if (hasGeyser) {
try {
return GeyserApi.api().connectionByUuid(player.getUniqueId()) != null;
} catch (NoClassDefFoundError error1) {
hasGeyser = false;
}
}
// Use Floodgate API to get connection status
if (hasFloodgate) {
try {
return FloodgateApi.getInstance().isFloodgatePlayer(player.getUniqueId());
} catch (NoClassDefFoundError error2) {
hasFloodgate = false;
}
}
return false;
}
}

View File

@ -1,7 +1,15 @@
package net.knarcraft.minigames.util; package net.knarcraft.minigames.util;
import net.knarcraft.minigames.MiniGames;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.event.entity.EntityDamageEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
/** /**
* A helper class for validating whether given input is valid * A helper class for validating whether given input is valid
@ -18,9 +26,48 @@ public final class InputValidationHelper {
* @param location <p>The location to validate</p> * @param location <p>The location to validate</p>
* @return <p>False if the location is valid</p> * @return <p>False if the location is valid</p>
*/ */
public static boolean isInvalid(Location location) { public static boolean isInvalid(@Nullable Location location) {
if (location == null) {
return true;
}
World world = location.getWorld(); World world = location.getWorld();
return world == null || !world.getWorldBorder().isInside(location); return world == null || !world.getWorldBorder().isInside(location);
} }
/**
* Checks whether the given value can be considered "empty"
*
* @param value <p>The value to check</p>
* @return <p>True if the value can be considered as empty</p>
*/
public static boolean isEmptyValue(@NotNull String value) {
return value.equalsIgnoreCase("null") || value.equalsIgnoreCase("clear") ||
value.equalsIgnoreCase("none");
}
/**
* Parses a set of damage causes from a set of damage cause names
*
* @param input <p>The damage cause names to parse</p>
* @return <p>The resulting damage causes</p>
*/
@NotNull
public static Set<EntityDamageEvent.DamageCause> parseDamageCauses(@Nullable Set<String> input) {
Set<EntityDamageEvent.DamageCause> output = new HashSet<>();
if (input == null) {
return output;
}
for (String causeName : input) {
try {
output.add(EntityDamageEvent.DamageCause.valueOf(causeName));
} catch (IllegalArgumentException | NullPointerException exception) {
MiniGames.log(Level.WARNING, "The damage cause " + causeName +
" is invalid, and will be ignored.");
}
}
return output;
}
} }

View File

@ -1,77 +0,0 @@
package net.knarcraft.minigames.util;
import net.knarcraft.minigames.MiniGames;
import net.knarcraft.minigames.config.Message;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Tag;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* A helper class for dealing with and parsing materials
*/
public final class MaterialHelper {
private MaterialHelper() {
}
/**
* Loads the materials specified in the block whitelist
*/
public static @NotNull Set<Material> loadMaterialList(@NotNull List<?> materials) {
Set<Material> parsedMaterials = new HashSet<>();
for (Object value : materials) {
if (!(value instanceof String string)) {
continue;
}
// Try to parse a material tag first
if (parseMaterialTag(parsedMaterials, string)) {
continue;
}
// Try to parse a material name
Material matched = Material.matchMaterial(string);
if (matched != null) {
parsedMaterials.add(matched);
} else {
MiniGames.log(Level.WARNING, Message.ERROR_MATERIAL_NOT_PARSE_ABLE.getMessage("{material}", string));
}
}
return parsedMaterials;
}
/**
* Tries to parse the material tag in the specified material name
*
* @param targetSet <p>The set all parsed materials should be added to</p>
* @param materialName <p>The material name that might be a material tag</p>
* @return <p>True if a tag was found</p>
*/
private static boolean parseMaterialTag(@NotNull Set<Material> targetSet, @NotNull String materialName) {
Pattern pattern = Pattern.compile("^\\+([a-zA-Z_]+)");
Matcher matcher = pattern.matcher(materialName);
if (matcher.find()) {
// The material is a material tag
Tag<Material> tag = Bukkit.getTag(Tag.REGISTRY_BLOCKS, NamespacedKey.minecraft(
matcher.group(1).toLowerCase()), Material.class);
if (tag != null) {
targetSet.addAll(tag.getValues());
} else {
MiniGames.log(Level.WARNING, "Unable to parse: " + materialName);
}
return true;
}
return false;
}
}

View File

@ -9,8 +9,9 @@ import net.knarcraft.minigames.arena.parkour.ParkourArenaGameMode;
import net.knarcraft.minigames.arena.parkour.ParkourArenaGroup; import net.knarcraft.minigames.arena.parkour.ParkourArenaGroup;
import net.knarcraft.minigames.arena.parkour.ParkourArenaRecordsRegistry; import net.knarcraft.minigames.arena.parkour.ParkourArenaRecordsRegistry;
import net.knarcraft.minigames.arena.parkour.ParkourArenaStorageKey; import net.knarcraft.minigames.arena.parkour.ParkourArenaStorageKey;
import net.knarcraft.minigames.config.Message; import net.knarcraft.minigames.arena.reward.Reward;
import net.knarcraft.minigames.container.PlaceholderContainer; import net.knarcraft.minigames.arena.reward.RewardCondition;
import net.knarcraft.minigames.config.MiniGameMessage;
import net.knarcraft.minigames.container.SerializableMaterial; import net.knarcraft.minigames.container.SerializableMaterial;
import net.knarcraft.minigames.container.SerializableUUID; import net.knarcraft.minigames.container.SerializableUUID;
import org.bukkit.Location; import org.bukkit.Location;
@ -32,6 +33,7 @@ import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
import static net.knarcraft.minigames.util.ArenaStorageHelper.getArenaDataFile; import static net.knarcraft.minigames.util.ArenaStorageHelper.getArenaDataFile;
import static net.knarcraft.minigames.util.ArenaStorageHelper.loadStrings;
/** /**
* A helper class for saving and loading parkour arenas * A helper class for saving and loading parkour arenas
@ -98,6 +100,35 @@ public final class ParkourArenaStorageHelper {
YamlConfiguration configuration = new YamlConfiguration(); YamlConfiguration configuration = new YamlConfiguration();
ConfigurationSection arenaSection = configuration.createSection(parkourArenasConfigurationSection); ConfigurationSection arenaSection = configuration.createSection(parkourArenasConfigurationSection);
for (ParkourArena arena : arenas.values()) { for (ParkourArena arena : arenas.values()) {
saveParkourArena(arenaSection, arena);
}
configuration.save(parkourArenaFile);
}
/**
* Saves a single arena
*
* @param arena <p>The arena to save</p>
* @throws IOException <p>If unable to write to the file</p>
*/
public static void saveSingleParkourArena(ParkourArena arena) throws IOException {
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(parkourArenaFile);
ConfigurationSection arenaSection = configuration.getConfigurationSection(parkourArenasConfigurationSection);
if (arenaSection == null) {
arenaSection = configuration.createSection(parkourArenasConfigurationSection);
}
saveParkourArena(arenaSection, arena);
configuration.save(parkourArenaFile);
}
/**
* Updates the given configuration section with the arena's data, and stores arena data for the arena
*
* @param arenaSection <p>The configuration section to update</p>
* @param arena <p>The arena to save</p>
* @throws IOException <p>If unable to save the arena data</p>
*/
private static void saveParkourArena(ConfigurationSection arenaSection, ParkourArena arena) throws IOException {
//Note: While the arena name is used as the key, as the key has to be sanitized, the un-sanitized arena name //Note: While the arena name is used as the key, as the key has to be sanitized, the un-sanitized arena name
// must be stored as well // must be stored as well
@NotNull ConfigurationSection configSection = arenaSection.createSection(arena.getArenaId().toString()); @NotNull ConfigurationSection configSection = arenaSection.createSection(arena.getArenaId().toString());
@ -107,11 +138,46 @@ public final class ParkourArenaStorageHelper {
configSection.set(ParkourArenaStorageKey.EXIT_LOCATION.getKey(), arena.getExitLocation()); configSection.set(ParkourArenaStorageKey.EXIT_LOCATION.getKey(), arena.getExitLocation());
configSection.set(ParkourArenaStorageKey.WIN_BLOCK_TYPE.getKey(), new SerializableMaterial(arena.getWinBlockType())); configSection.set(ParkourArenaStorageKey.WIN_BLOCK_TYPE.getKey(), new SerializableMaterial(arena.getWinBlockType()));
configSection.set(ParkourArenaStorageKey.WIN_LOCATION.getKey(), arena.getWinLocation()); configSection.set(ParkourArenaStorageKey.WIN_LOCATION.getKey(), arena.getWinLocation());
configSection.set(ParkourArenaStorageKey.KILL_PLANE_BLOCKS.getKey(), arena.getKillPlaneBlockNames()); configSection.set(ParkourArenaStorageKey.KILL_PLANE_BLOCKS.getKey(), getKillPlaneBlocks(arena));
configSection.set(ParkourArenaStorageKey.OBSTACLE_BLOCKS.getKey(), getObstacleBlocks(arena));
configSection.set(ParkourArenaStorageKey.CHECKPOINTS.getKey(), arena.getCheckpoints()); configSection.set(ParkourArenaStorageKey.CHECKPOINTS.getKey(), arena.getCheckpoints());
configSection.set(ParkourArenaStorageKey.MAX_PLAYERS.getKey(), arena.getMaxPlayers());
configSection.set(ParkourArenaStorageKey.ALLOWED_DAMAGE_CAUSES.getKey(),
ArenaStorageHelper.getDamageCauseNames(arena.getAllowedDamageCauses()));
configSection.set(ParkourArenaStorageKey.LOSS_TRIGGER_DAMAGE_CAUSES.getKey(),
ArenaStorageHelper.getDamageCauseNames(arena.getLossTriggerDamageCauses()));
RewardStorageHelper.saveRewards(arena, configSection, ParkourArenaStorageKey.REWARDS.getKey());
saveParkourArenaData(arena.getData()); saveParkourArenaData(arena.getData());
} }
configuration.save(parkourArenaFile);
/**
* Gets a list of the kill plane blocks for the given arena
*
* @param arena <p>The arena to get kill plane blocks for</p>
* @return <p>The kill plane blocks</p>
*/
@NotNull
private static List<String> getKillPlaneBlocks(@NotNull ParkourArena arena) {
if (arena.getKillPlaneBlockNames() == null) {
return new ArrayList<>();
} else {
return new ArrayList<>(arena.getKillPlaneBlockNames());
}
}
/**
* Gets a list of the obstacle blocks for the given arena
*
* @param arena <p>The arena to get obstacle blocks for</p>
* @return <p>The obstacle blocks</p>
*/
@NotNull
private static List<String> getObstacleBlocks(@NotNull ParkourArena arena) {
if (arena.getObstacleBlockNames() == null) {
return new ArrayList<>();
} else {
return new ArrayList<>(arena.getObstacleBlockNames());
}
} }
/** /**
@ -119,7 +185,8 @@ public final class ParkourArenaStorageHelper {
* *
* @return <p>The loaded arenas, or null if the arenas configuration section is missing.</p> * @return <p>The loaded arenas, or null if the arenas configuration section is missing.</p>
*/ */
public static @NotNull Map<UUID, ParkourArena> loadParkourArenas() { @NotNull
public static Map<UUID, ParkourArena> loadParkourArenas() {
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(parkourArenaFile); YamlConfiguration configuration = YamlConfiguration.loadConfiguration(parkourArenaFile);
ConfigurationSection arenaSection = configuration.getConfigurationSection(parkourArenasConfigurationSection); ConfigurationSection arenaSection = configuration.getConfigurationSection(parkourArenasConfigurationSection);
//If no such section exists, it must be the case that there is no data to load //If no such section exists, it must be the case that there is no data to load
@ -152,22 +219,28 @@ public final class ParkourArenaStorageHelper {
* @return <p>The loaded arena, or null if invalid</p> * @return <p>The loaded arena, or null if invalid</p>
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static @Nullable ParkourArena loadParkourArena(@NotNull ConfigurationSection configurationSection) { @Nullable
private static ParkourArena loadParkourArena(@NotNull ConfigurationSection configurationSection) {
UUID arenaId = ((SerializableUUID) configurationSection.get(ParkourArenaStorageKey.ID.getKey(), UUID arenaId = ((SerializableUUID) configurationSection.get(ParkourArenaStorageKey.ID.getKey(),
new SerializableUUID(UUID.randomUUID()))).getRawValue(); new SerializableUUID(UUID.randomUUID()))).getRawValue();
String arenaName = configurationSection.getString(ParkourArenaStorageKey.NAME.getKey()); String arenaName = configurationSection.getString(ParkourArenaStorageKey.NAME.getKey());
Location spawnLocation = (Location) configurationSection.get(ParkourArenaStorageKey.SPAWN_LOCATION.getKey()); Location spawnLocation = (Location) configurationSection.get(ParkourArenaStorageKey.SPAWN_LOCATION.getKey());
Location exitLocation = (Location) configurationSection.get(ParkourArenaStorageKey.EXIT_LOCATION.getKey()); Location exitLocation = (Location) configurationSection.get(ParkourArenaStorageKey.EXIT_LOCATION.getKey());
Location winLocation = (Location) configurationSection.get(ParkourArenaStorageKey.WIN_LOCATION.getKey()); Location winLocation = (Location) configurationSection.get(ParkourArenaStorageKey.WIN_LOCATION.getKey());
SerializableMaterial winBlockType = (SerializableMaterial) configurationSection.get( int maxPlayers = configurationSection.getInt(ParkourArenaStorageKey.MAX_PLAYERS.getKey(), -1);
ParkourArenaStorageKey.WIN_BLOCK_TYPE.getKey()); SerializableMaterial winBlockType = (SerializableMaterial) configurationSection.get(ParkourArenaStorageKey.WIN_BLOCK_TYPE.getKey());
List<?> killPlaneBlockNames = configurationSection.getList(ParkourArenaStorageKey.KILL_PLANE_BLOCKS.getKey()); Set<String> killPlaneBlockNames = loadStrings(configurationSection, ParkourArenaStorageKey.KILL_PLANE_BLOCKS);
Set<String> obstacleBlockNames = loadStrings(configurationSection, ParkourArenaStorageKey.OBSTACLE_BLOCKS);
List<Location> checkpoints = (List<Location>) configurationSection.get(ParkourArenaStorageKey.CHECKPOINTS.getKey()); List<Location> checkpoints = (List<Location>) configurationSection.get(ParkourArenaStorageKey.CHECKPOINTS.getKey());
Map<RewardCondition, Set<Reward>> rewards = RewardStorageHelper.loadRewards(configurationSection,
ParkourArenaStorageKey.REWARDS.getKey());
// The arena name and spawn location must be present // The arena name and spawn location must be present
if (arenaName == null || spawnLocation == null) { if (arenaName == null || spawnLocation == null) {
MiniGames.log(Level.SEVERE, Message.ERROR_ARENA_NOT_LOADED.getMessage(new PlaceholderContainer().add( MiniGames.log(Level.SEVERE, MiniGames.getInstance().getStringFormatter().replacePlaceholders(
"{section}", configurationSection.getName()).add("{file}", "parkour_arena"))); MiniGameMessage.ERROR_ARENA_NOT_LOADED, new String[]{"{section}", "{file}"},
new String[]{configurationSection.getName(), "parkour_arena"}));
return null; return null;
} }
@ -179,8 +252,8 @@ public final class ParkourArenaStorageHelper {
// Generate new, empty arena data if not available // Generate new, empty arena data if not available
ParkourArenaData arenaData = loadParkourArenaData(arenaId); ParkourArenaData arenaData = loadParkourArenaData(arenaId);
if (arenaData == null) { if (arenaData == null) {
MiniGames.log(Level.SEVERE, Message.ERROR_ARENA_DATA_NOT_LOADED.getMessage("{arena}", MiniGames.log(Level.SEVERE, MiniGames.getInstance().getStringFormatter().replacePlaceholder(
arenaId.toString())); MiniGameMessage.ERROR_ARENA_DATA_NOT_LOADED, "{arena}", arenaId.toString()));
arenaData = getEmptyParkourData(arenaId); arenaData = getEmptyParkourData(arenaId);
} }
@ -188,8 +261,12 @@ public final class ParkourArenaStorageHelper {
checkpoints = new ArrayList<>(); checkpoints = new ArrayList<>();
} }
Set<String> allowedDamageCauseNames = loadStrings(configurationSection, ParkourArenaStorageKey.ALLOWED_DAMAGE_CAUSES);
Set<String> lossTriggerDamageCauseNames = loadStrings(configurationSection, ParkourArenaStorageKey.LOSS_TRIGGER_DAMAGE_CAUSES);
return new ParkourArena(arenaId, arenaName, spawnLocation, exitLocation, winBlockType.getRawValue(), winLocation, return new ParkourArena(arenaId, arenaName, spawnLocation, exitLocation, winBlockType.getRawValue(), winLocation,
(Set<String>) killPlaneBlockNames, checkpoints, arenaData, MiniGames.getInstance().getParkourArenaHandler()); killPlaneBlockNames, obstacleBlockNames, checkpoints, maxPlayers, rewards, arenaData,
MiniGames.getInstance().getParkourArenaHandler(), allowedDamageCauseNames, lossTriggerDamageCauseNames);
} }
/** /**
@ -198,7 +275,8 @@ public final class ParkourArenaStorageHelper {
* @param arenaId <p>The id to get parkour data for</p> * @param arenaId <p>The id to get parkour data for</p>
* @return <p>Empty parkour data</p> * @return <p>Empty parkour data</p>
*/ */
private static @NotNull ParkourArenaData getEmptyParkourData(@NotNull UUID arenaId) { @NotNull
private static ParkourArenaData getEmptyParkourData(@NotNull UUID arenaId) {
Map<ArenaGameMode, ArenaRecordsRegistry> recordRegistries = new HashMap<>(); Map<ArenaGameMode, ArenaRecordsRegistry> recordRegistries = new HashMap<>();
Map<ArenaGameMode, Set<UUID>> playersCompleted = new HashMap<>(); Map<ArenaGameMode, Set<UUID>> playersCompleted = new HashMap<>();
for (ArenaGameMode arenaGameMode : ParkourArenaGameMode.values()) { for (ArenaGameMode arenaGameMode : ParkourArenaGameMode.values()) {
@ -226,7 +304,8 @@ public final class ParkourArenaStorageHelper {
* @param arenaId <p>The id of the arena to get data for</p> * @param arenaId <p>The id of the arena to get data for</p>
* @return <p>The loaded arena data</p> * @return <p>The loaded arena data</p>
*/ */
private static @Nullable ParkourArenaData loadParkourArenaData(@NotNull UUID arenaId) { @Nullable
private static ParkourArenaData loadParkourArenaData(@NotNull UUID arenaId) {
File arenaDataFile = getParkourArenaDataFile(arenaId); File arenaDataFile = getParkourArenaDataFile(arenaId);
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(arenaDataFile); YamlConfiguration configuration = YamlConfiguration.loadConfiguration(arenaDataFile);
return (ParkourArenaData) configuration.get(ParkourArenaStorageKey.DATA.getKey()); return (ParkourArenaData) configuration.get(ParkourArenaStorageKey.DATA.getKey());
@ -248,7 +327,8 @@ public final class ParkourArenaStorageHelper {
* @param arenaId <p>The id of the arena to get a data file for</p> * @param arenaId <p>The id of the arena to get a data file for</p>
* @return <p>The file the arena's data is/should be stored in</p> * @return <p>The file the arena's data is/should be stored in</p>
*/ */
private static @NotNull File getParkourArenaDataFile(@NotNull UUID arenaId) { @NotNull
private static File getParkourArenaDataFile(@NotNull UUID arenaId) {
return getArenaDataFile(parkourArenaDataFolder, arenaId); return getArenaDataFile(parkourArenaDataFolder, arenaId);
} }

Some files were not shown because too many files have changed in this diff Show More