diff --git a/src/main/java/com/archivesmc/archblock/Plugin.java b/src/main/java/com/archivesmc/archblock/Plugin.java index da941e6..fac8708 100644 --- a/src/main/java/com/archivesmc/archblock/Plugin.java +++ b/src/main/java/com/archivesmc/archblock/Plugin.java @@ -4,10 +4,7 @@ import com.archivesmc.archblock.commands.*; import com.archivesmc.archblock.config.MainConfig; import com.archivesmc.archblock.events.*; -import com.archivesmc.archblock.events.protection.BlockBreakEvent; -import com.archivesmc.archblock.events.protection.BlockPlaceEvent; -import com.archivesmc.archblock.events.protection.PistonMoveEvent; -import com.archivesmc.archblock.events.protection.PlayerInteractEvent; +import com.archivesmc.archblock.events.protection.*; import com.archivesmc.archblock.importers.WatchBlockImporter; import com.archivesmc.archblock.integrations.WorldGuard; import com.mewin.WGCustomFlags.WGCustomFlagsPlugin; @@ -107,6 +104,7 @@ public void onEnable() { this.getServer().getPluginManager().registerEvents(new BlockBreakEvent(this), this); this.getServer().getPluginManager().registerEvents(new BlockPlaceEvent(this), this); this.getServer().getPluginManager().registerEvents(new PistonMoveEvent(this), this); + this.getServer().getPluginManager().registerEvents(new PlayerBucketEmpty(this), this); this.getServer().getPluginManager().registerEvents(new PlayerConnectEvent(this), this); this.getServer().getPluginManager().registerEvents(new PlayerInteractEvent(this), this); diff --git a/src/main/java/com/archivesmc/archblock/api/ArchBlock.java b/src/main/java/com/archivesmc/archblock/api/ArchBlock.java index 4e2cb99..e3b72fd 100644 --- a/src/main/java/com/archivesmc/archblock/api/ArchBlock.java +++ b/src/main/java/com/archivesmc/archblock/api/ArchBlock.java @@ -227,11 +227,8 @@ public boolean canEditBlock(Block block, Player player) { return true; } - if (owner.equals(player.getUniqueId())) { - return true; - } + return owner.equals(player.getUniqueId()) || this.hasFriendship(owner, player.getUniqueId()); - return this.hasFriendship(owner, player.getUniqueId()); } /** @@ -368,7 +365,7 @@ public void storePlayer(UUID uuid, String username) { username = username.toLowerCase(); com.archivesmc.archblock.storage.entities.Player p = new com.archivesmc.archblock.storage.entities.Player( - uuid.toString(), username.toLowerCase() + uuid.toString(), username ); this.storePlayer(p); diff --git a/src/main/java/com/archivesmc/archblock/events/protection/PlayerBucketEmpty.java b/src/main/java/com/archivesmc/archblock/events/protection/PlayerBucketEmpty.java new file mode 100644 index 0000000..f5cb082 --- /dev/null +++ b/src/main/java/com/archivesmc/archblock/events/protection/PlayerBucketEmpty.java @@ -0,0 +1,50 @@ +package com.archivesmc.archblock.events.protection; + +import com.archivesmc.archblock.Plugin; +import org.bukkit.ChatColor; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerBucketEmptyEvent; + +import java.util.UUID; + +public class PlayerBucketEmpty implements Listener { + private final Plugin plugin; + + public PlayerBucketEmpty(Plugin plugin) { + this.plugin = plugin; + } + + @EventHandler(priority = EventPriority.NORMAL) + public void onEvent(PlayerBucketEmptyEvent event) { + UUID owner = this.plugin.getApi().getOwnerUUID(event.getBlockClicked()); + UUID ourUuid = event.getPlayer().getUniqueId(); + + if (this.plugin.getWorldGuardIntegration().isInIgnoredRegion(event.getBlockClicked())) { + this.plugin.debug("Region has bypass-protection set to true"); + return; + } + + + if (owner != null && ! owner.equals(ourUuid)) { + if (! event.getPlayer().hasPermission("archblock.bypass") && ! this.plugin.getApi().hasFriendship(owner, ourUuid)) { + event.getPlayer().sendMessage( + String.format( + "%s[%sArchBlock%s]%s You may not empty buckets on blocks owned by %s%s%s.", + ChatColor.LIGHT_PURPLE, ChatColor.GOLD, ChatColor.LIGHT_PURPLE, + ChatColor.RED, ChatColor.AQUA, this.plugin.getApi().getUsernameForUuid(owner), + ChatColor.RED + ) + ); + + event.setCancelled(true); + return; + } + this.plugin.debug("Owner is different but player has a bypass or is friends with the owner"); + } + + this.plugin.debug("Everything's okay - empty ze bucket"); + this.plugin.getApi().removeOwner(event.getBlockClicked()); + } +} diff --git a/src/main/java/com/archivesmc/archblock/runnables/database/StorePlayerThread.java b/src/main/java/com/archivesmc/archblock/runnables/database/StorePlayerThread.java index dda4f52..6312887 100644 --- a/src/main/java/com/archivesmc/archblock/runnables/database/StorePlayerThread.java +++ b/src/main/java/com/archivesmc/archblock/runnables/database/StorePlayerThread.java @@ -23,38 +23,58 @@ public void run() { Object uuid, username; Query q; - q = s.createQuery("SELECT p.uuid FROM Player p WHERE username=:username"); - q.setString("username", this.player.getUsername()); - uuid = q.uniqueResult(); - q = s.createQuery("SELECT p.username FROM Player p WHERE uuid=:uuid"); q.setString("uuid", this.player.getUuid()); username = q.uniqueResult(); - if (this.player.getUuid().equalsIgnoreCase((String) uuid)) { - // If the UUID was found.. - if (! this.player.getUsername().equalsIgnoreCase((String) username)) { - // If the username wasn't found.. - q = s.createQuery("UPDATE Player p SET p.username=:username WHERE p.uuid=:uuid"); - q.setString("username", this.player.getUsername()); - q.setString("uuid", (String) uuid); - q.executeUpdate(); - } + if (username == null) { + // Their UUID isn't in the database, let's check for their username. + + this.plugin.debug("UUID isn't in the database."); + } else if (this.player.getUsername().equalsIgnoreCase((String) username)) { + // Their username and UUID matches. + + s.flush(); + s.close(); + return; } else { - // If the UUID wasn't found.. - if (this.player.getUsername().equalsIgnoreCase((String) username)) { - // If the username was found.. - q = s.createQuery("UPDATE Player p SET p.uuid=:uuid WHERE p.username=:username"); - q.setString("username", this.player.getUsername()); - q.setString("uuid", (String) uuid); - q.executeUpdate(); - } else { - // If the username wasn't found.. - s.save(this.player); - } + // Their UUID is there, but the username is wrong. Update it. + + q = s.createQuery("UPDATE Player p SET p.username=:username WHERE p.uuid=:uuid"); + q.setString("username", this.player.getUsername().toLowerCase()); + q.setString("uuid", this.player.getUuid()); + q.executeUpdate(); + + s.flush(); + s.close(); + return; } - s.flush(); - s.close(); + q = s.createQuery("SELECT p.uuid FROM Player p WHERE username=:username"); + q.setString("username", this.player.getUsername().toLowerCase()); + uuid = q.uniqueResult(); + + if (uuid == null) { + // Their username isn't in the database either. Store them. + + s.save(this.player); + s.flush(); + s.close(); + } else if (this.player.getUuid().equalsIgnoreCase((String) uuid)) { + // Their username and UUID matches. + + s.flush(); + s.close(); + } else { + // Their username is there, but the UUID is wrong. Update it. + + q = s.createQuery("UPDATE Player p SET p.uuid=:uuid WHERE p.username=:username"); + q.setString("username", this.player.getUsername().toLowerCase()); + q.setString("uuid", this.player.getUuid()); + q.executeUpdate(); + + s.flush(); + s.close(); + } } }