Skip to content

Commit

Permalink
Added Message Util, fixed possiable bug sky worlds
Browse files Browse the repository at this point in the history
Added Message Util class so it's easier to manage all colors and
messages in one location.
Fixed possiable bug sky worlds where getHighestNonAirBlock could loop
forever.
  • Loading branch information
zwollner committed Feb 24, 2013
1 parent 60c2396 commit 06851bc
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 134 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.zmanww</groupId>
<artifactId>SnowControl</artifactId>
<version>1.0-beta9.5</version>
<version>1.0-beta9.6</version>
<packaging>jar</packaging>

<name>SnowControl</name>
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/zmanww/bukkit/SnowControl/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
if (sender instanceof Player) {// If run by player check permissions
if (sender.hasPermission("snowcontrol.reload")) {
Config.getInstance().reload();
sender.sendMessage("SnowControl config has been reloaded");
sender.sendMessage(MessageUtil.RELOAD_MESSAGE);
} else {
sender.sendMessage(NO_PERMISSION_MSG);
}
} else {
Config.getInstance().reload();
sender.sendMessage("SnowControl config has been reloaded");
sender.sendMessage(MessageUtil.RELOAD_MESSAGE);
}
return true;
} else if (args[0].equalsIgnoreCase("addReplace")) {
if (sender instanceof Player) {
if (sender.hasPermission("snowcontrol.addReplace")) {
SnowControl.pendingCommand.put((Player) sender, SnowControl.COMMAND_REPLACE);
sender.sendMessage("Please hit the block to add to the CanReplace List");
sender.sendMessage(MessageUtil.getToAddToMsg(Config.CONFIG_CAN_REPLACE));
} else {
sender.sendMessage(NO_PERMISSION_MSG);
}
Expand All @@ -52,7 +52,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
if (sender instanceof Player) {
if (sender.hasPermission("snowcontrol.addAccum")) {
SnowControl.pendingCommand.put((Player) sender, SnowControl.COMMAND_ACCUMULATE);
sender.sendMessage("Please hit the block to add to the CanAccumulateOn List");
sender.sendMessage(MessageUtil.getToAddToMsg(Config.CONFIG_CAN_ACCUMULATE_ON));
} else {
sender.sendMessage(NO_PERMISSION_MSG);
}
Expand All @@ -62,7 +62,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
if (sender instanceof Player) {
if (sender.hasPermission("snowcontrol.addFall")) {
SnowControl.pendingCommand.put((Player) sender, SnowControl.COMMAND_FALLTHROUGH);
sender.sendMessage("Please hit the block to add to the CanFallThrough List");
sender.sendMessage(MessageUtil.getToAddToMsg(Config.CONFIG_CAN_FALL_THROUGH));
} else {
sender.sendMessage(NO_PERMISSION_MSG);
}
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/com/zmanww/bukkit/SnowControl/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public class Config {
public List<Material> canAccumulateOn = new ArrayList<Material>();
public List<String> enabledWorlds = new ArrayList<String>();

private static final String CONFIG_SNOWFALL = "SnowFall.";
public static final String CONFIG_CAN_FALL_THROUGH = "CanFallThrough";
public static final String CONFIG_CAN_REPLACE = "CanReplace";
public static final String CONFIG_CAN_ACCUMULATE_ON = "CanAccumulateOn";

private Config() {
if (!new File(plugin.getDataFolder(), "config.yml").exists()) {
plugin.saveDefaultConfig();
Expand Down Expand Up @@ -49,22 +54,22 @@ public void reload() {
}

private void loadKeys() {
canFallThrough = stringToMaterial(plugin.getConfig().getStringList("SnowFall.CanFallThrough"));
canFallThrough = stringToMaterial(plugin.getConfig().getStringList(CONFIG_SNOWFALL + CONFIG_CAN_FALL_THROUGH));
if (!canFallThrough.contains(Material.AIR)) {
canFallThrough.add(Material.AIR);
}

canReplace = stringToMaterial(plugin.getConfig().getStringList("SnowFall.CanReplace"));
canReplace = stringToMaterial(plugin.getConfig().getStringList(CONFIG_SNOWFALL + CONFIG_CAN_REPLACE));
if (!canReplace.contains(Material.AIR)) {
canReplace.add(Material.AIR);
}

canAccumulateOn = stringToMaterial(plugin.getConfig().getStringList("SnowFall.CanAccumulateOn"));
canAccumulateOn = stringToMaterial(plugin.getConfig().getStringList(CONFIG_SNOWFALL + CONFIG_CAN_ACCUMULATE_ON));
if (!canAccumulateOn.contains(Material.SNOW_BLOCK)) {
canAccumulateOn.add(Material.SNOW_BLOCK);
}

if (plugin.getConfig().isSet(("SnowFall.EnabledWorlds"))) {
if (plugin.getConfig().isSet("SnowFall.EnabledWorlds")) {
enabledWorlds = plugin.getConfig().getStringList("SnowFall.EnabledWorlds");
} else {
for (World world : plugin.getServer().getWorlds()) {
Expand All @@ -82,7 +87,7 @@ public void addReplaceable(Material mat) {
if (!canReplace.contains(mat)) {
canReplace.add(mat);

plugin.getConfig().set("SnowFall.CanReplace", MaterialToString(canReplace));
plugin.getConfig().set(CONFIG_SNOWFALL + CONFIG_CAN_REPLACE, MaterialToString(canReplace));
plugin.saveConfig();
}
}
Expand All @@ -91,7 +96,7 @@ public void addAccumulate(Material mat) {
if (!canAccumulateOn.contains(mat)) {
canAccumulateOn.add(mat);

plugin.getConfig().set("SnowFall.CanAccumulateOn", MaterialToString(canAccumulateOn));
plugin.getConfig().set(CONFIG_SNOWFALL + CONFIG_CAN_ACCUMULATE_ON, MaterialToString(canAccumulateOn));
plugin.saveConfig();
}
}
Expand All @@ -100,7 +105,7 @@ public void addFallThrough(Material mat) {
if (!canFallThrough.contains(mat)) {
canFallThrough.add(mat);

plugin.getConfig().set("SnowFall.CanFallThrough", MaterialToString(canFallThrough));
plugin.getConfig().set(CONFIG_SNOWFALL + CONFIG_CAN_FALL_THROUGH, MaterialToString(canFallThrough));
plugin.saveConfig();
}
}
Expand Down Expand Up @@ -170,7 +175,7 @@ public byte getMaxAccumulation() {

public byte getMaxAccumulation(Material mat) {
int retVal = plugin.getConfig().getInt("SnowFall.MaxAccumulationDefault", 7);
if (mat != null) {
if (mat != null && plugin.getConfig().isSet("SnowFall.MaxAccumulationOverride." + mat.toString())) {
retVal = plugin.getConfig().getInt("SnowFall.MaxAccumulationOverride." + mat.toString(), retVal);
}
if (retVal < 0) {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/zmanww/bukkit/SnowControl/MessageUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.zmanww.bukkit.SnowControl;

import org.bukkit.ChatColor;

public class MessageUtil {
public static final String RELOAD_MESSAGE = ChatColor.GREEN + "SnowControl config has been reloaded";

public static String getAddedToList(String blkName, String listName) {
ChatColor msgColor = ChatColor.GREEN;
blkName = ChatColor.RED + blkName;
listName = ChatColor.RED + listName;
return msgColor + "Added " + blkName + msgColor + " to " + listName + msgColor + " list.";
}

public static String getToAddToMsg(String listName) {
ChatColor msgColor = ChatColor.GREEN;
listName = ChatColor.RED + listName;
return msgColor + "Please hit the block to add to the " + listName + msgColor + " list.";
}

}
37 changes: 26 additions & 11 deletions src/main/java/com/zmanww/bukkit/SnowControl/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public PlayerListener(SnowControl instance) {

@EventHandler()
public void blockBreak(BlockBreakEvent event) {
final Block block = event.getBlock();
Block block = event.getBlock();
if (block.getType() == Material.ICE) {
if (event.getPlayer().getItemInHand().getType() == Material.STONE_PICKAXE
|| event.getPlayer().getItemInHand().getType() == Material.WOOD_PICKAXE
Expand All @@ -37,37 +37,52 @@ public void blockBreak(BlockBreakEvent event) {
}
}
final Location loc = event.getBlock().getLocation();
if (block.getRelative(BlockFace.UP).getType() == Material.SNOW) {
byte under = -1;
if (block.getRelative(BlockFace.UP).getType() == Material.SNOW
|| block.getRelative(BlockFace.UP).getType() == Material.SNOW_BLOCK) {
/*
* If block above broken is snow, then remove the next replaceable block under it so that the falling snow
* lands properly.
*/
SnowManager.removeReplaceableUnder(block);
final byte above = block.getRelative(BlockFace.UP).getData();// get level of snow about to fall
final byte under = SnowManager.getSnowLevelUnder(block);// get level of snow where it will land
final byte newUnder = (byte) (above + under + 1 > 7 ? 7 : above + under + 1);
loc.getWorld().spawnFallingBlock(loc, Material.SNOW, newUnder);

under = SnowManager.getSnowLevelUnder(block);// get level of snow where it will land
}
while (block.getRelative(BlockFace.UP).getType() == Material.SNOW
|| block.getRelative(BlockFace.UP).getType() == Material.SNOW_BLOCK) {
block = block.getRelative(BlockFace.UP);

if (block.getType() == Material.SNOW_BLOCK) {
block.setType(Material.AIR);
loc.getWorld().spawnFallingBlock(loc, Material.SNOW_BLOCK, (byte) 0);
} else {
final byte above = block.getData();// get level of snow about to fall
final byte newUnder = (byte) (above + under + 1 > 7 ? 7 : above + under + 1);
loc.getWorld().spawnFallingBlock(loc, Material.SNOW, newUnder);
}

}
}

@EventHandler()
public void onBlockDamage(BlockDamageEvent event) {
if (SnowControl.pendingCommand.containsKey(event.getPlayer())) {
if (SnowControl.pendingCommand.get(event.getPlayer()).equals(SnowControl.COMMAND_ACCUMULATE)) {
event.getPlayer().sendMessage(
"Adding " + event.getBlock().getType().name() + " to CanAccumulateOn list.");
Config.getInstance().addAccumulate(event.getBlock().getType());
event.getPlayer().sendMessage(
MessageUtil.getAddedToList(event.getBlock().getType().name(), Config.CONFIG_CAN_ACCUMULATE_ON));
SnowControl.pendingCommand.remove(event.getPlayer());
event.setCancelled(true);
} else if (SnowControl.pendingCommand.get(event.getPlayer()).equals(SnowControl.COMMAND_FALLTHROUGH)) {
SnowControl.pendingCommand.remove(event.getPlayer());
event.getPlayer().sendMessage(
"Adding " + event.getBlock().getType().name() + " to CanFallThrough list.");
MessageUtil.getAddedToList(event.getBlock().getType().name(), Config.CONFIG_CAN_FALL_THROUGH));
Config.getInstance().addFallThrough(event.getBlock().getType());
SnowControl.pendingCommand.remove(event.getPlayer());
event.setCancelled(true);
} else if (SnowControl.pendingCommand.get(event.getPlayer()).equals(SnowControl.COMMAND_REPLACE)) {
event.getPlayer().sendMessage("Adding " + event.getBlock().getType().name() + " to CanReplace list.");
Config.getInstance().addReplaceable(event.getBlock().getType());
event.getPlayer().sendMessage(
MessageUtil.getAddedToList(event.getBlock().getType().name(), Config.CONFIG_CAN_REPLACE));
SnowControl.pendingCommand.remove(event.getPlayer());
event.setCancelled(true);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/zmanww/bukkit/SnowControl/SnowManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ public static Block getHighestNonAirBlock(Block block) {
World world = block.getWorld();
Block currentBlock = world.getBlockAt(block.getX(), world.getMaxHeight() - 1, block.getZ());
while (currentBlock.getType() == Material.AIR) {
if (currentBlock.getY() < 2){
//For sky levels or other areas where there are no Non-Air blocks
return currentBlock;
}
currentBlock = currentBlock.getRelative(BlockFace.DOWN);
}
return currentBlock;
Expand Down

0 comments on commit 06851bc

Please sign in to comment.