Skip to content

Commit

Permalink
Fix random durations and dMaterial object fetching.
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Aug 4, 2013
1 parent a22441b commit f580b02
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
Expand Up @@ -4,6 +4,9 @@
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.tags.TagManager;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.ParticleEffect;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.blocks.FakeBlock;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/Duration.java
Expand Up @@ -56,8 +56,8 @@ public static Duration valueOf(String string) {
if (low != null && high != null
&& low.getSecondsAsInt() < high.getSecondsAsInt()) {
int seconds = Utilities.getRandom()
.nextInt((high.getSecondsAsInt() - low.getSecondsAsInt() + 1)
+ low.getSecondsAsInt());
.nextInt((high.getSecondsAsInt() - low.getSecondsAsInt() + 1))
+ low.getSecondsAsInt();
// Send the result to the debugger since it's probably good to know what is being chosen.
dB.echoDebug("Getting random duration between " + low.identify()
+ " and " + high.identify() + "... " + seconds + "s");
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/aufdemrand/denizen/objects/dMaterial.java
Expand Up @@ -11,7 +11,7 @@

public class dMaterial implements dObject {

final static Pattern materialPattern = Pattern.compile("(\\w+):?(\\d+)?");
final static Pattern materialPattern = Pattern.compile("(?:m@)?(\\w+):?(\\d+)?", Pattern.CASE_INSENSITIVE);

//////////////////
// OBJECT FETCHER
Expand All @@ -26,8 +26,9 @@ public class dMaterial implements dObject {
*/
@ObjectFetcher("m")
public static dMaterial valueOf(String string) {

if (string.toUpperCase().matches("RANDOM")) {

if (string.toLowerCase().matches("random")
|| string.toLowerCase().matches("m@random")) {

// Get a random material
return new dMaterial(Material.values()[Utilities.getRandom().nextInt(Material.values().length)]);
Expand Down
59 changes: 58 additions & 1 deletion src/main/java/net/aufdemrand/denizen/utilities/Utilities.java
Expand Up @@ -16,6 +16,7 @@
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.tags.TagManager;

import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -162,7 +163,35 @@ public String getVersionNumber() {
}
return props.getProperty("version");
}



public static List<Block> getRandomSolidBlocks(Location location, int range, int count) {
List<Block> blocks = new ArrayList<Block>();

int x = 0;
int f = 0;

while (x < count) {

if (f > 1000) break;
f++;

Location loc = location.clone()
.add(Utilities.getRandom().nextInt(range * 2) - range,
Utilities.getRandom().nextInt(range * 2) - range,
Utilities.getRandom().nextInt(range * 2) - range);

if (loc.getBlock().getType().isSolid()) {
blocks.add(loc.getBlock());
x++;
}

}

dB.log(blocks.size() + " blocksize");

return blocks;
}


/**
Expand Down Expand Up @@ -192,6 +221,34 @@ public static Player getClosestPlayer (Location location, int range) {
}
return closestPlayer;
}


/**
* Finds the closest Players to a particular location.
*
* @param location The location to find the closest Player to.
* @param range The maximum range to look for the Player.
*
* @return The closest Player to the location, or null if no Player was found
* within the range specified.
*/

public static List<dPlayer> getClosestPlayers(Location location, int range) {

List<dPlayer> closestPlayers = new ArrayList<dPlayer>();
double closestDistance = Math.pow(range, 2);
List<Player> playerList = new ArrayList<Player>(Arrays.asList(Bukkit.getOnlinePlayers()));
Iterator<Player> it = playerList.iterator();
while (it.hasNext()) {
Player player = it.next();
Location loc = player.getLocation();
if (loc.getWorld().equals(location.getWorld())
&& loc.distanceSquared(location) < closestDistance) {
closestPlayers.add(dPlayer.mirrorBukkitPlayer(player));
}
}
return closestPlayers;
}


/**
Expand Down

0 comments on commit f580b02

Please sign in to comment.