Skip to content

Commit 58b5dd5

Browse files
authored
Merge pull request #3 from Simplexity-Development/safety-check-refactor
Safety check refactor
2 parents 794c27c + fe3557e commit 58b5dd5

20 files changed

Lines changed: 473 additions & 421 deletions

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ provides a straightforward interface for home management commands.
2323
| `homes.commands.delhome` | op | Allows player to delete home |
2424
| `homes.commands.home` | op | Allows player to teleport to home |
2525
| `homes.commands.list` | op | Allows you to list your own homes |
26+
| `homes.bed` | op | Allows you to teleport to your bed |
2627
| `homes.count` | op | Base for permission on number of homes you can have |
2728
| `homes.count.<num>` | op | Base for permission on number of homes you can have |
2829
| `homes.count.bypass` | op | Allows for setting infinite homes regardless of how many you have set as the max |
@@ -32,12 +33,15 @@ provides a straightforward interface for home management commands.
3233
## Importing from other plugins
3334

3435
**THIS COMMAND IS IRREVERSIBLE AND WILL DELETE ANY HOMES YOU HAVE ALREADY SET IN SIMPLEHOMES**
35-
it is **HIGHLY** recommended you do not run this command while anyone is online, to reduce the risk of any save corruption.
36+
it is **HIGHLY** recommended you do not run this command while anyone is online, to reduce the risk of any save
37+
corruption.
3638
**USE AT YOUR OWN RISK**
3739

3840
To import from another plugin, you need to go to console, as the command is console-only. Command syntax is as follows:
3941
`importhomes <plugin> [player]`
40-
The player argument is optional, if you want to import all homes that are saved in the plugin specified, leave the player argument out.
42+
The player argument is optional, if you want to import all homes that are saved in the plugin specified, leave the
43+
player argument out.
4144

4245
Currently supported plugins to import from:
46+
4347
- Essentials

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>simplexity</groupId>
88
<artifactId>SimpleHomes</artifactId>
9-
<version>1.2.0</version>
9+
<version>1.2.1</version>
1010
<packaging>jar</packaging>
1111

1212
<name>SimpleHomes</name>

src/main/java/simplexity/simplehomes/SafetyCheck.java

Lines changed: 27 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,44 @@
22

33
import org.bukkit.Location;
44
import org.bukkit.Material;
5-
import org.bukkit.block.data.BlockData;
6-
import org.bukkit.block.data.type.Campfire;
7-
import org.bukkit.block.data.type.Fire;
85

96
import java.util.List;
107

118
public class SafetyCheck {
129

13-
/**
14-
* Checks if the block above this location is water
15-
* @param location the location to check
16-
* @return boolean
17-
*/
18-
public static boolean underWater(Location location) {
19-
Location locationAbove = location.clone().add(0, 1, 0);
20-
return locationAbove.getBlock().getType() == Material.WATER;
21-
}
22-
23-
/**
24-
* Checks if this block or the one above it is lava
25-
* @param location the location to check
26-
* @return boolean
27-
*/
28-
public static boolean insideLava(Location location) {
29-
Location locationAbove = location.clone().add(0, 1, 0);
30-
if (location.getBlock().getType() == Material.LAVA) {
31-
return true;
10+
public static int checkSafetyFlags(Location location, List<Material> blacklistedMaterials) {
11+
int flags = 0;
12+
Location blockAbove = location.clone().add(0, 1, 0);
13+
Location blockBelow = location.clone().add(0, -1, 0);
14+
// Fall check, is the player in the air? i.e. is the block below them empty/air?
15+
if (isEmpty(blockBelow) || blockBelow.getBlock().isPassable()) {
16+
flags |= SafetyFlags.FALLING.bitFlag;
3217
}
33-
return locationAbove.getBlock().getType() == Material.LAVA;
34-
}
35-
36-
/**
37-
* Checks if the block below the provided location is air or is otherwise empty
38-
* @param location the location to check
39-
* @return boolean
40-
*/
41-
public static boolean willFall(Location location) {
42-
Location locationBelow = location.clone().add(0, -1, 0);
43-
if (locationBelow.getBlock().isEmpty()) return true;
44-
if (locationBelow.getBlock().getType().isEmpty()) return true;
45-
return locationBelow.getBlock().getType().equals(Material.AIR);
46-
}
47-
48-
/**
49-
* Checks if the block located at this position is a fire, or campfire
50-
* @param location the location to check
51-
* @return boolean
52-
*/
53-
public static boolean insideFire(Location location) {
54-
BlockData blockData = location.getBlock().getBlockData();
55-
if (blockData instanceof Fire) {
56-
return true;
18+
// Is there lava?
19+
if (isMaterial(location, Material.LAVA) || isMaterial(blockAbove, Material.LAVA)) {
20+
flags |= SafetyFlags.LAVA.bitFlag;
21+
}
22+
// Is the home encased in blocks?
23+
if (blockAbove.getBlock().isSolid()) {
24+
flags |= SafetyFlags.SUFFOCATION.bitFlag;
25+
}
26+
// Is the home under water?
27+
if (isMaterial(blockAbove, Material.WATER)) {
28+
flags |= SafetyFlags.UNDERWATER.bitFlag;
5729
}
58-
if (blockData instanceof Campfire campfire) {
59-
return campfire.isLit();
30+
// Is the home on a blacklisted block?
31+
if (blacklistedMaterials.contains(location.getBlock().getType())) {
32+
flags |= SafetyFlags.DAMAGE_RISK.bitFlag;
6033
}
61-
return false;
34+
return flags;
6235
}
6336

64-
/**
65-
* Checks if the block located at this position is solid
66-
* @param location the location to check
67-
* @return boolean
68-
*/
69-
public static boolean insideSolidBlocks(Location location) {
70-
Location locationAbove = location.clone().add(0, 1, 0);
71-
if (locationAbove.getBlock().isSolid()) return true;
72-
return false;
37+
private static boolean isMaterial(Location location, Material material) {
38+
return location.getBlock().getType() == material;
7339
}
7440

75-
/**
76-
* Checks if the block located at this position, or the block above, are blacklisted
77-
* @param location the location to check
78-
* @param materialList the list of blacklisted materials
79-
* @return boolean
80-
*/
81-
82-
public static boolean insideBlacklistedBlocks(Location location, List<Material> materialList) {
83-
Location locationAbove = location.clone().add(0, 1, 0);
84-
if (materialList.contains(locationAbove.getBlock().getType())) return true;
85-
return materialList.contains(location.getBlock().getType());
41+
private static boolean isEmpty(Location location) {
42+
return location.getBlock().getType().isEmpty();
8643
}
8744
}
45+

src/main/java/simplexity/simplehomes/SafetyFlags.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public enum SafetyFlags {
1616
this.bitFlag = bitFlag;
1717
}
1818

19-
public boolean checkFlag(int bitFlags) {
19+
public boolean matches(int bitFlags) {
2020
int result = bitFlag & bitFlags;
2121
return result == bitFlag;
2222
}

src/main/java/simplexity/simplehomes/SimpleHomes.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import net.kyori.adventure.text.minimessage.MiniMessage;
44
import org.bukkit.plugin.java.JavaPlugin;
55
import simplexity.simplehomes.commands.DeleteHome;
6-
import simplexity.simplehomes.commands.Home;
6+
import simplexity.simplehomes.commands.HomeCommand;
77
import simplexity.simplehomes.commands.HomeList;
88
import simplexity.simplehomes.commands.HomesReload;
99
import simplexity.simplehomes.commands.ImportHomes;
1010
import simplexity.simplehomes.commands.SetHome;
1111
import simplexity.simplehomes.configs.ConfigHandler;
1212
import simplexity.simplehomes.configs.LocaleHandler;
13+
import simplexity.simplehomes.listeners.PlayerLeaveListener;
1314
import simplexity.simplehomes.listeners.PlayerMoveListener;
1415
import simplexity.simplehomes.saving.SQLHandler;
1516

@@ -30,6 +31,7 @@ public void onEnable() {
3031
LocaleHandler.getInstance().loadLocale();
3132
SQLHandler.getInstance().init();
3233
this.getServer().getPluginManager().registerEvents(new PlayerMoveListener(), this);
34+
this.getServer().getPluginManager().registerEvents(new PlayerLeaveListener(), this);
3335
registerCommands();
3436
}
3537

@@ -44,7 +46,7 @@ public static MiniMessage getMiniMessage() {
4446
private void registerCommands() {
4547
Objects.requireNonNull(this.getCommand("sethome")).setExecutor(new SetHome());
4648
Objects.requireNonNull(this.getCommand("delhome")).setExecutor(new DeleteHome());
47-
Objects.requireNonNull(this.getCommand("home")).setExecutor(new Home());
49+
Objects.requireNonNull(this.getCommand("home")).setExecutor(new HomeCommand());
4850
Objects.requireNonNull(this.getCommand("homelist")).setExecutor(new HomeList());
4951
Objects.requireNonNull(this.getCommand("homesreload")).setExecutor(new HomesReload());
5052
Objects.requireNonNull(this.getCommand("importhomes")).setExecutor(new ImportHomes());

src/main/java/simplexity/simplehomes/Util.java

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package simplexity.simplehomes.commands;
2+
3+
import org.bukkit.entity.Player;
4+
import org.bukkit.permissions.PermissionAttachmentInfo;
5+
import simplexity.simplehomes.Home;
6+
import simplexity.simplehomes.SimpleHomes;
7+
import simplexity.simplehomes.configs.ConfigHandler;
8+
import simplexity.simplehomes.saving.SQLHandler;
9+
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.List;
13+
import java.util.UUID;
14+
15+
public class CommandUtils {
16+
17+
private static final List<String> OVERRIDE_ARGS = List.of("-override", "-o");
18+
private static final List<String> PLAYER_ARGS = List.of("-player", "-p");
19+
public static final String COUNT_BYPASS = "homes.count.bypass";
20+
public static final String COUNT_BASE = "homes.count.";
21+
public static final String BED_PERMISSION = "homes.bed";
22+
23+
public static Home getHomeFromList(List<Home> homes, String homeName) {
24+
for (Home home : homes) {
25+
if (home.name().equalsIgnoreCase(homeName)) {
26+
return home;
27+
}
28+
}
29+
return null;
30+
}
31+
32+
public static boolean hasMoreHomesThanAllowed(Player player){
33+
UUID playerUUID = player.getUniqueId();
34+
List<Home> playerHomeList = SQLHandler.getInstance().getHomes(playerUUID);
35+
return playerHomeList.size() > maxHomesPermission(player);
36+
}
37+
38+
public static int maxHomesPermission(Player player) {
39+
int maxHomes = 0;
40+
for (PermissionAttachmentInfo pai : player.getEffectivePermissions()) {
41+
String permission = pai.getPermission();
42+
if (!pai.getValue()) continue; //if the permission is set false, skip it
43+
if (!pai.getValue() || !permission.startsWith(COUNT_BASE))
44+
continue; // if the permission is set false, if it isn't ours, skip it
45+
permission = permission.replace(COUNT_BASE, ""); //
46+
try {
47+
int homeCount = Integer.parseInt(permission);
48+
if (maxHomes < homeCount) maxHomes = homeCount;
49+
} catch (NumberFormatException e) {
50+
SimpleHomes.getInstance().getLogger().warning("Found homes permission with invalid number format: " + permission);
51+
}
52+
}
53+
return maxHomes;
54+
}
55+
56+
// check for override arguments anywhere in the args
57+
public static boolean shouldOverride(String[] args) {
58+
return Arrays.stream(args).anyMatch(arg -> OVERRIDE_ARGS.stream().anyMatch(arg::equalsIgnoreCase));
59+
}
60+
61+
// This is going to hopefully be more useful the more flags are added
62+
public static List<String> getSanitizedArgsList(String[] args){
63+
List<String> argsList = new ArrayList<>(List.of(args));
64+
argsList.removeIf(OVERRIDE_ARGS::contains);
65+
return argsList;
66+
}
67+
68+
// Check if they should be locked out lol
69+
public static boolean isLockedOut(Player player) {
70+
if (player.hasPermission(CommandUtils.COUNT_BYPASS)) return false;
71+
if (!ConfigHandler.getInstance().isLockoutEnabled()) return false;
72+
if (!ConfigHandler.getInstance().isDisableDeleteHome()) return false;
73+
return hasMoreHomesThanAllowed(player);
74+
}
75+
76+
}

src/main/java/simplexity/simplehomes/commands/DeleteHome.java

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package simplexity.simplehomes.commands;
22

33
import net.kyori.adventure.text.Component;
4-
import net.kyori.adventure.text.minimessage.MiniMessage;
54
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
65
import org.bukkit.command.Command;
76
import org.bukkit.command.CommandSender;
@@ -10,17 +9,14 @@
109
import org.jetbrains.annotations.NotNull;
1110
import org.jetbrains.annotations.Nullable;
1211
import simplexity.simplehomes.Home;
13-
import simplexity.simplehomes.SimpleHomes;
14-
import simplexity.simplehomes.Util;
15-
import simplexity.simplehomes.configs.ConfigHandler;
1612
import simplexity.simplehomes.configs.LocaleHandler;
1713
import simplexity.simplehomes.saving.SQLHandler;
1814

1915
import java.util.ArrayList;
2016
import java.util.List;
2117

18+
2219
public class DeleteHome implements TabExecutor {
23-
MiniMessage miniMessage = SimpleHomes.getMiniMessage();
2420

2521
@Override
2622
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
@@ -32,35 +28,36 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
3228
sender.sendRichMessage(LocaleHandler.getInstance().getProvideHomeName());
3329
return false;
3430
}
35-
List<Home> playerHomes = SQLHandler.getInstance().getHomes(player.getUniqueId());
36-
if (ConfigHandler.getInstance().isLockoutEnabled() && ConfigHandler.getInstance().isDisableDeleteHome()) {
37-
int maxHomeCount = Util.maxHomesPermission(player);
38-
if (maxHomeCount < playerHomes.size() && !player.hasPermission("homes.count.bypass")) {
39-
player.sendMessage(miniMessage.deserialize(LocaleHandler.getInstance().getCannotUseCommand(),
40-
Placeholder.parsed("value", String.valueOf(maxHomeCount)),
41-
Placeholder.parsed("command", "/deletehome")));
42-
return false;
43-
}
31+
List<Home> playerHomesList = SQLHandler.getInstance().getHomes(player.getUniqueId());
32+
if (CommandUtils.isLockedOut(player)) {
33+
player.sendRichMessage(LocaleHandler.getInstance().getCannotUseCommand(),
34+
Placeholder.parsed("value", String.valueOf(CommandUtils.maxHomesPermission(player))),
35+
Placeholder.parsed("command", "/delhome"));
36+
return false;
4437
}
4538
String homeName = args[0].toLowerCase();
46-
if (Util.homeExists(playerHomes, homeName)) {
47-
Home home = SQLHandler.getInstance().getHome(player.getUniqueId(), homeName);
48-
if (home == null) {
49-
SimpleHomes.getInstance().getLogger().severe("HOME WAS NULL, RETURNING");
50-
return false;
51-
}
52-
Component messageToSend = LocaleHandler.getInstance().locationResolver(home, LocaleHandler.getInstance().getHomeDeleted());
53-
if (messageToSend == null) {
54-
SimpleHomes.getInstance().getLogger().warning("Unable to load 'messages.home-deleted' from locale.yml, please check your locale");
55-
messageToSend = miniMessage.deserialize("<yellow>Home was deleted></yellow>");
56-
}
57-
SQLHandler.getInstance().deleteHome(player.getUniqueId(), homeName);
58-
player.sendMessage(messageToSend);
59-
} else {
60-
player.sendMessage(miniMessage.deserialize(LocaleHandler.getInstance().getHomeNotFound(),
61-
Placeholder.unparsed("name", homeName)));
39+
Home homeRequested = CommandUtils.getHomeFromList(playerHomesList, homeName);
40+
if (!shouldDelete(homeRequested, player, homeName)) return false;
41+
//Need to parse the message before deleting the home otherwise there's no home to send into the method. At least that's what happened originally.
42+
//Probably had to do with how I was originally putting it in but whatever, I'm doing it here now.
43+
Component parsedHomeDeleteMessage = LocaleHandler.getInstance().locationResolver(homeRequested,
44+
LocaleHandler.getInstance().getHomeDeleted());
45+
SQLHandler.getInstance().deleteHome(player.getUniqueId(), homeName);
46+
player.sendMessage(parsedHomeDeleteMessage);
47+
return true;
48+
49+
}
50+
51+
52+
53+
//Handle logic to get the home requested in the arguments
54+
private boolean shouldDelete(Home homeRequested, Player player, String homeName) {
55+
if (homeRequested == null) {
56+
player.sendRichMessage(LocaleHandler.getInstance().getHomeNotFound(),
57+
Placeholder.unparsed("name", homeName));
58+
return false;
6259
}
63-
return false;
60+
return true;
6461
}
6562

6663
@Override

0 commit comments

Comments
 (0)