Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
Add world whitelist + refactor
Browse files Browse the repository at this point in the history
+ make broadcastBlacklist and blocksToBroadcast  private
+ remove logger var
+ remove dumb finals from private methods
  • Loading branch information
bendem committed May 22, 2014
1 parent 6d84852 commit 0cc6513
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 46 deletions.
50 changes: 27 additions & 23 deletions src/main/java/be/bendem/orebroadcast/BlockBreakListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,56 +22,61 @@ public class BlockBreakListener implements Listener {

@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
// Reject creative users and users without ob.broadcast permission
if(event.getPlayer().getGameMode() != GameMode.SURVIVAL
|| !event.getPlayer().hasPermission("ob.broadcast")) {
Player player = event.getPlayer();
// Reject creative users, users without ob.broadcast permission
// and user in a non whitelisted world
if(player.getGameMode() != GameMode.SURVIVAL
|| !player.hasPermission("ob.broadcast")
|| !plugin.isWorldWhitelisted(player.getWorld().getName())) {
return;
}

Block block = event.getBlock();
// Don't broadcast the blocks which has already been broadcasted
// or which have been placed by a player
if(plugin.broadcastBlacklist.contains(block)) {
plugin.broadcastBlacklist.remove(block);
plugin.logger.finer("Block in blackList : " + plugin.broadcastBlacklist.size());
if(plugin.isBlackListed(block)) {
plugin.unBlackList(block);
return;
}

// Measuring event time
long timer = System.currentTimeMillis();

String blockName = (block.getType() == Material.GLOWING_REDSTONE_ORE ? "redstone" :
block.getType().name().toLowerCase().replace("_ore", ""));
if(plugin.isWhitelisted(block.getType())) {
String blockName;
if(block.getType() == Material.GLOWING_REDSTONE_ORE) {
blockName = "redstone";
} else {
blockName = block.getType().name().toLowerCase().replace("_ore", "");
}

if(plugin.blocksToBroadcast.contains(block.getType().name())) {
int veinSize = getVeinSize(block);
String color = plugin.getConfig().getString("colors." + blockName, "white").toUpperCase();
String formattedMessage = format(
plugin.getConfig().getString("message", "{player} just found {count} block{plural} of {ore}"),
event.getPlayer(),
Integer.toString(veinSize),
player,
String.valueOf(veinSize),
blockName,
color,
veinSize > 1
);
broadcast(event.getPlayer(), formattedMessage);
broadcast(player, formattedMessage);
}

plugin.logger.finer("Block in blackList : " + plugin.broadcastBlacklist.size());
plugin.logger.finer("Event duration : " + (System.currentTimeMillis() - timer) + "ms");
plugin.getLogger().finer("Event duration : " + (System.currentTimeMillis() - timer) + "ms");
}

private final int getVeinSize(Block block) {
private int getVeinSize(Block block) {
Set<Block> vein = new HashSet<>();
vein.add(block);
vein = getVein(block, vein);
plugin.broadcastBlacklist.addAll(vein);
plugin.broadcastBlacklist.remove(block);
plugin.blackList(vein);
plugin.unBlackList(block);

return vein.size();
}

private final Set<Block> getVein(Block block, Set<Block> vein) {
private Set<Block> getVein(Block block, Set<Block> vein) {
int i, j, k;
for (i = -1; i < 2; ++i) {
for (j = -1; j < 2; ++j) {
Expand Down Expand Up @@ -113,24 +118,23 @@ private void broadcast(Player player, String message) {
}
}

private final String format(String msg, Player player, String count, String ore, String color, boolean plural) {
private String format(String msg, Player player, String count, String ore, String color, boolean plural) {
return colorize(msg
// DEPRECATED Use player_name instead
.replace("{player}", player.getDisplayName())
.replace("{player_name}", player.getDisplayName())
.replace("{real_player_name}", player.getName())
.replace("{world}", player.getWorld().getName())
.replace("{count}", count)
.replace("{ore}", translateOre(ore, color))
.replace("{ore_color}", "&" + ChatColor.valueOf(color).getChar())
.replace("{plural}", plural ? plugin.getConfig().getString("plural", "s") : ""));
}

private final String translateOre(String ore, String color) {
private String translateOre(String ore, String color) {
return "&" + ChatColor.valueOf(color).getChar()
+ plugin.getConfig().getString("ore-translations." + ore, ore);
}

private final String colorize(String msg) {
private String colorize(String msg) {
return ChatColor.translateAlternateColorCodes('&', msg);
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/be/bendem/orebroadcast/BlockPlaceListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public class BlockPlaceListener implements Listener {
@EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
Block block = event.getBlock();
if(plugin.blocksToBroadcast.contains(block.getType().name())
&& !plugin.broadcastBlacklist.contains(block)) {
plugin.broadcastBlacklist.add(block);
if(plugin.isWhitelisted(block.getType())
&& plugin.isWorldWhitelisted(block.getWorld().getName())
&& !plugin.isBlackListed(block)) {
plugin.blackList(block);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/be/bendem/orebroadcast/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
&& args[0].equalsIgnoreCase("reload")
&& sender.hasPermission("ob.reload")) {
plugin.reloadConfig();
plugin.loadBlocksToBroadcastList();
plugin.loadConfig();
sender.sendMessage("Config reloaded...");
return true;
}
Expand Down
69 changes: 51 additions & 18 deletions src/main/java/be/bendem/orebroadcast/OreBroadcast.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package be.bendem.orebroadcast;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

/**
* OreBroadcast for Bukkit
Expand All @@ -17,41 +17,74 @@
*/
public class OreBroadcast extends JavaPlugin {

public PluginDescriptionFile pdfFile;
public Logger logger;
// As it's currently stored, blocks which have already been broadcasted
// will be again after a server restart / reload.
public Set<Block> broadcastBlacklist = new HashSet<>();
public List<String> blocksToBroadcast;
private final Set<Block> broadcastBlacklist = new HashSet<>();
private final Set<Material> blocksToBroadcast = new HashSet<>();
private final Set<String> worldWhitelist = new HashSet<>();
private boolean worldWhitelistActive = false;

@Override
public void onEnable() {
logger = getLogger();
pdfFile = getDescription();
PluginDescriptionFile description = getDescription();

saveDefaultConfig();
loadBlocksToBroadcastList();
loadConfig();
getServer().getPluginManager().registerEvents(new BlockBreakListener(this), this);
getServer().getPluginManager().registerEvents(new BlockPlaceListener(this), this);
getCommand("ob").setExecutor(new CommandHandler(this));
logger.fine(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!");
getLogger().fine(description.getName() + " version " + description.getVersion() + " is enabled!");
}

@Override
public void onDisable() {
logger.fine(pdfFile.getName() + " want you to have a nice day ;-)");
getLogger().fine(getDescription().getName() + " want you to have a nice day ;-)");
}

public void loadBlocksToBroadcastList() {
// Create the list of blocks to broadcast from the file
blocksToBroadcast = new ArrayList<>(getConfig().getStringList("ores"));
for (int i = 0; i < blocksToBroadcast.size(); ++i) {
blocksToBroadcast.set(i, blocksToBroadcast.get(i).toUpperCase() + "_ORE");
public void blackList(Block block) {
broadcastBlacklist.add(block);
}

public void blackList(Collection<Block> blocks) {
broadcastBlacklist.addAll(blocks);
}

public void unBlackList(Block block) {
broadcastBlacklist.remove(block);
}

public boolean isBlackListed(Block block) {
return broadcastBlacklist.contains(block);
}

public boolean isWhitelisted(Material material) {
return blocksToBroadcast.contains(material);
}

public boolean isWorldWhitelisted(String world) {
return !worldWhitelistActive || worldWhitelist.contains(world);
}

public void loadConfig() {
// Create the list of materials to broadcast from the file
List<String> configList = getConfig().getStringList("ores");
blocksToBroadcast.clear();

for (String item : configList) {
Material material = Material.getMaterial(item.toUpperCase() + "_ORE");
blocksToBroadcast.add(material);
// Handle glowing redstone ore (id 74) and redstone ore (id 73)
if(blocksToBroadcast.get(i).equals("REDSTONE_ORE")) {
blocksToBroadcast.add("GLOWING_REDSTONE");
if (material.equals(Material.REDSTONE_ORE)) {
blocksToBroadcast.add(Material.GLOWING_REDSTONE_ORE);
}
}

// Load worlds
worldWhitelist.clear();
worldWhitelistActive = getConfig().getBoolean("active-per-worlds", true);
if(worldWhitelistActive) {
worldWhitelist.addAll(getConfig().getStringList("active-worlds"));
}
}

}
12 changes: 11 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ colors:
diamond: aqua
emerald: green

# The message to be broadcasted (you can use the tags {player_name}, {real_player_name}, {count}, {plural}, {ore} and {ore_color})
# The message to be broadcasted (you can use the tags {player_name}, {real_player_name}, {count}, {plural},
# {ore}, {ore_color} and {world})
# You can use colors by using &colorcode
# Note that the {ore} will be replaced by {ore_color}{ore}
# Note also that "player_name" is name of the player has it is set by different plugins, "real_player_name" is the pseudo of the player
Expand All @@ -42,6 +43,15 @@ ore-translations:
diamond: diamond
emerald: emerald

# Set this to true to only activate OreBroadcast in listed worlds
active-per-worlds: false

# List of worlds where OreBroadcast is active
# This has no effects if active-per-world is set to false!
#active-worlds:
# - world
# - world_nether

# Colors and color codes available :
# Use the colors for the ore list and the color codes for the message
# black : 0 | dark_blue : 1
Expand Down

0 comments on commit 0cc6513

Please sign in to comment.