Skip to content

Commit

Permalink
Merge branch 'actionbar' (new feature to toggle chat spam)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brianetta committed Jan 10, 2018
2 parents 2d974e9 + 3468f85 commit 5a7a6b4
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 26 deletions.
17 changes: 14 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,33 @@

<groupId>StickyLocks</groupId>
<artifactId>StickyLocks</artifactId>
<version>0.10</version>
<version>0.11</version>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<!--Spigot API-->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!--Bukkit API-->
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.12.1-R0.1-SNAPSHOT</version>
<version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-chat</artifactId>
<version>1.12-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<resources>
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/net/simplycrafted/StickyLocks/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -779,4 +779,21 @@ public void duplicate(Block target, Block source) {
stickylocks.getLogger().info("Failed to duplicate access list information");
}
}

public Boolean getNotification(Player player) {
PreparedStatement psql;
ResultSet results;
Boolean returnVal = false;
try {
psql = db_conn.prepareStatement("SELECT notify FROM player WHERE uuid LIKE ?");
psql.setString(1,player.getUniqueId().toString());
results = psql.executeQuery();
if (results.next()) returnVal = (results.getInt(1) ==1);
results.close();
psql.close();
} catch (SQLException e) {
stickylocks.getLogger().info("Failed to fetch notification setting for player");
}
return returnVal;
}
}
31 changes: 26 additions & 5 deletions src/main/java/net/simplycrafted/StickyLocks/StickyLocks.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.simplycrafted.StickyLocks;

import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.simplycrafted.StickyLocks.commands.StickyLocksCommand;
import net.simplycrafted.StickyLocks.listeners.StickyLocksClick;
import net.simplycrafted.StickyLocks.listeners.StickyLocksCreateDestroy;
Expand All @@ -15,6 +17,7 @@
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;

/**
Expand All @@ -39,7 +42,8 @@ public class StickyLocks extends JavaPlugin {

private static Database db;

public HashMap<Player,Location> SelectedBlock;
public HashMap<Player,Location> selectedBlock;
public HashMap<Player,Boolean> playerNotification;

@Override
public void onEnable() {
Expand Down Expand Up @@ -68,8 +72,11 @@ public void onEnable() {
db=new Database();
db.createTables();

// Which block players mighht have selected
SelectedBlock = new HashMap<Player,Location>();
// Which block players might have selected
selectedBlock = new HashMap<>();

// Per-player notification settings
playerNotification = new HashMap<>();
}

@Override
Expand All @@ -83,7 +90,7 @@ public void onDisable() {
// Clear away the database class
db.shutdown();
// Clear the block selections
SelectedBlock.clear();
selectedBlock.clear();
}

public static StickyLocks getInstance() {
Expand All @@ -92,7 +99,21 @@ public static StickyLocks getInstance() {
}

public void sendMessage(CommandSender player, String message, boolean unlocked) {
player.sendMessage(String.format("%s[%s]%s %s", ChatColor.GRAY, getConfig().getString("chatprefix"), unlocked ? ChatColor.DARK_GREEN : ChatColor.DARK_RED, message));
}

public void sendMuteableMessage(CommandSender player, String message, boolean unlocked) {
sendMuteableMessage(player, message, unlocked, null);
}

public void sendMuteableMessage(CommandSender player, String message, boolean unlocked, String altMessage) {
// "unlocked" is a colour flag. If true, message is green. If not, message is red.
player.sendMessage(String.format("%s[%s]%s %s", ChatColor.GRAY,getConfig().getString("chatprefix"), unlocked ? ChatColor.DARK_GREEN : ChatColor.DARK_RED,message));
if(playerNotification.get(player)) {
// Chat message
player.sendMessage(String.format("%s[%s]%s %s", ChatColor.GRAY, getConfig().getString("chatprefix"), unlocked ? ChatColor.DARK_GREEN : ChatColor.DARK_RED, message));
} else if (altMessage != null & player instanceof Player) {
// Brief action bar pop-up in red or green
((Player) player).spigot().sendMessage(ChatMessageType.ACTION_BAR, new ComponentBuilder(altMessage).color(unlocked ? net.md_5.bungee.api.ChatColor.DARK_GREEN : net.md_5.bungee.api.ChatColor.DARK_RED).create());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import net.simplycrafted.StickyLocks.Database;
import net.simplycrafted.StickyLocks.StickyLocks;
import net.simplycrafted.StickyLocks.listeners.StickyLocksClick;
import net.simplycrafted.StickyLocks.util.Util;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerInteractEvent;

import java.net.URI;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -51,12 +52,12 @@ public boolean onCommand (CommandSender sender, Command command, String label, S

// If a block is selected, use that block's owner. If that block has no owner, or
// if no block is selected, use the player.
if (stickylocks.SelectedBlock.get(sender) == null) {
if (stickylocks.selectedBlock.get(sender) == null) {
playerID = ((Player) sender).getUniqueId();
} else {
playerID = db.getUUID(stickylocks.SelectedBlock.get(sender));
playerID = db.getUUID(stickylocks.selectedBlock.get(sender));
if (playerID == null) playerID = ((Player) sender).getUniqueId();
location = stickylocks.SelectedBlock.get(sender);
location = stickylocks.selectedBlock.get(sender);
}

// Player-specific commands
Expand Down Expand Up @@ -199,19 +200,23 @@ public boolean onCommand (CommandSender sender, Command command, String label, S
return false;
case "notify" :
db.toggleNotify((Player)sender);
stickylocks.playerNotification.put((Player) sender, !stickylocks.playerNotification.get((Player) sender));
stickylocks.sendMessage(sender,"Toggled lock notifications", true);
return true;
case "reload" :
if(sender.hasPermission("stickylocks.reload")) {
stickylocks.sendMessage(sender, "Reloading configuration", true);
stickylocks.reloadConfig();
db.createTables();
// Re-register the PlayerInteractEvent in case the tool has changed
PlayerInteractEvent.getHandlerList().unregister(stickylocks);
stickylocks.getServer().getPluginManager().registerEvents(new StickyLocksClick(),stickylocks);
} else {
stickylocks.sendMessage(sender,"No permission",false);
}
return true;
case "clearselection" :
stickylocks.SelectedBlock.remove(sender);
stickylocks.selectedBlock.remove(sender);
stickylocks.sendMessage(sender, "Selection cleared", true);
return true;
default:
Expand All @@ -224,6 +229,9 @@ public boolean onCommand (CommandSender sender, Command command, String label, S
stickylocks.sendMessage(sender, "Reloading configuration", true);
stickylocks.reloadConfig();
db.createTables();
// Re-register the PlayerInteractEvent in case the tool has changed
PlayerInteractEvent.getHandlerList().unregister(stickylocks);
stickylocks.getServer().getPluginManager().registerEvents(new StickyLocksClick(),stickylocks);
} else {
stickylocks.sendMessage(sender, "Only the reload command works from console", false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public void onPlayerInteract(PlayerInteractEvent event) {
Protection protection = db.getProtection(target);
if (protection.getType() != null) {
String selected = "";
if (stickylocks.SelectedBlock.get(player) == null || (stickylocks.SelectedBlock.get(player).getWorld().equals(target.getLocation().getWorld()) && !(stickylocks.SelectedBlock.get(player).distanceSquared(target.getLocation()) < 1))) {
if (stickylocks.selectedBlock.get(player) == null || (stickylocks.selectedBlock.get(player).getWorld().equals(target.getLocation().getWorld()) && !(stickylocks.selectedBlock.get(player).distanceSquared(target.getLocation()) < 1))) {
// Block is not one previously selected
stickylocks.SelectedBlock.put(player, target.getLocation());
stickylocks.selectedBlock.put(player, target.getLocation());
selected = " " + ChatColor.RED + "(selected)";
}
if (protection.isProtected())
Expand All @@ -80,9 +80,9 @@ public void onPlayerInteract(PlayerInteractEvent event) {
Protection protection = db.getProtection(target);
if (protection.isProtected()) {
if (protection.getOwner().equals(player.getUniqueId())) {
stickylocks.sendMessage(player, String.format("%s owned by you", protection.getType()), true);
stickylocks.sendMuteableMessage(player, String.format("%s owned by you", protection.getType()), true);
} else {
stickylocks.sendMessage(player, String.format("%s owned by %s", protection.getType(), protection.getOwnerName()), player.hasPermission("stickylocks.ghost"));
stickylocks.sendMuteableMessage(player, String.format("%s owned by %s", protection.getType(), protection.getOwnerName()), player.hasPermission("stickylocks.ghost"), String.format("LOCKED by %s", protection.getOwnerName()));
// Use of permission on previous line changes colour of message
if (!player.hasPermission("stickylocks.ghost") && db.accessDenied(player, target)) {
event.setCancelled(true);
Expand All @@ -94,7 +94,7 @@ public void onPlayerInteract(PlayerInteractEvent event) {
// Left-clicks are either with a stick, or not
if (player.getInventory().getItemInMainHand().getType() == tool) {
// Stick used - check if it's the selected block, and lock/unlock if appropriate
if (stickylocks.SelectedBlock.get(player) != null && (stickylocks.SelectedBlock.get(player).getWorld().equals(target.getLocation().getWorld()) && stickylocks.SelectedBlock.get(player).distanceSquared(target.getLocation()) < 1)) {
if (stickylocks.selectedBlock.get(player) != null && (stickylocks.selectedBlock.get(player).getWorld().equals(target.getLocation().getWorld()) && stickylocks.selectedBlock.get(player).distanceSquared(target.getLocation()) < 1)) {
// Just left-clicked the selected block with a stick!
if (player.hasPermission("stickylocks.lock")) {
Protection protection = db.getProtection(target);
Expand Down Expand Up @@ -135,9 +135,9 @@ public void onPlayerInteract(PlayerInteractEvent event) {
}
}
} else if (event.getAction() == Action.RIGHT_CLICK_AIR) {
if (player.getInventory().getItemInMainHand().getType() == tool && stickylocks.SelectedBlock.get(player) != null) {
if (player.getInventory().getItemInMainHand().getType() == tool && stickylocks.selectedBlock.get(player) != null) {
// Player right-clicked nothing - Deselect whatever might be selected.
stickylocks.SelectedBlock.remove(player);
stickylocks.selectedBlock.remove(player);
stickylocks.sendMessage(player, "Selection cleared", true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand Down Expand Up @@ -58,10 +56,10 @@ public void onBlockPlaceEvent(BlockPlaceEvent event) {
// It belongs to the player, and needs to have the lock information
// copied to the newly placed half chest
db.duplicate(target, Util.getOtherHalfOfChest(target));
stickylocks.sendMessage(player, "Chest lock has been expanded", true);
stickylocks.sendMuteableMessage(player, "Chest lock has been expanded", true);
} else {
// It is belongs to another player, so cancel this event. Denied!
stickylocks.sendMessage(player, "Chest placement blocked - access is denied to the existing chest", false);
stickylocks.sendMuteableMessage(player, "Chest placement blocked - access is denied to the existing chest", false, "Can't expand locked chest");
event.setCancelled(true);
return;
}
Expand All @@ -84,7 +82,7 @@ public void checkBlockPlaceEvent(BlockPlaceEvent event) {
}
} else {
if (Util.getOtherHalfOfChest(event.getBlockPlaced()) == null && db.isProtectable(event.getBlockPlaced().getType())) {
stickylocks.sendMessage(player, String.format("Right-click then left-click with %s to lock this object", stickylocks.getConfig().getString("tool")), true);
stickylocks.sendMuteableMessage(player, String.format("Right-click then left-click with %s to lock this object", stickylocks.getConfig().getString("tool")), true);
}
}
}
Expand All @@ -106,7 +104,7 @@ public void onHopperPlaceEvent(BlockPlaceEvent event) {
if (target.getState() instanceof InventoryHolder & db.getProtection(target).isProtected() & db.accessDenied(player,target)) {
// The block above the hopper has an inventory, and it's not ours - cancel!
event.setCancelled(true);
stickylocks.sendMessage(player,"Hopper placement blocked - access is denied to the inventory above",false);
stickylocks.sendMuteableMessage(player,"Hopper placement blocked - access is denied to the inventory above",false, "Hopper placement cancelled, block above locked");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.simplycrafted.StickyLocks.listeners;

import net.simplycrafted.StickyLocks.Database;
import net.simplycrafted.StickyLocks.StickyLocks;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
Expand Down Expand Up @@ -29,5 +30,6 @@ public class StickyLocksPlayerjoin implements Listener {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
db.addPlayer(event.getPlayer());
StickyLocks.getInstance().playerNotification.put(event.getPlayer(),db.getNotification(event.getPlayer()));
}
}
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ commands:
/<command> group <group> {add|remove} <player> ...
/<command> group <group> {rename|merge} <name>
/<command> reload
/<command> notify
permission: stickylocks.lock
aliases: sl
permissions:
Expand Down

0 comments on commit 5a7a6b4

Please sign in to comment.