Skip to content

Commit

Permalink
Add dMaterial object. Allow FallingBlock with data values in dEntity.…
Browse files Browse the repository at this point in the history
… Allow some debug to be disabled.
  • Loading branch information
davidcernat committed Jun 30, 2013
1 parent 3812d5c commit 1f6dbc1
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/aufdemrand/denizen/objects/dColor.java
@@ -1,7 +1,6 @@
package net.aufdemrand.denizen.objects;

import java.lang.reflect.Field;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -30,6 +29,7 @@ public static dColor valueOf(String string) {

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

// Get a color using random RGB values
return new dColor(Utilities.getRandom().nextInt(256), Utilities.getRandom().nextInt(256), Utilities.getRandom().nextInt(256));
}

Expand Down Expand Up @@ -116,7 +116,7 @@ public dColor(Color color) {
// INSTANCE FIELDS/METHODS
/////////////////

// Bukkit itemstack associated
// Bukkit Color associated

private Color color;

Expand Down
35 changes: 30 additions & 5 deletions src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -3,22 +3,21 @@
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.containers.core.EntityScriptContainer;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.nbt.CustomNBT;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.FireworkEffect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_5_R3.CraftWorld;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Ocelot;
import org.bukkit.entity.Ocelot.Type;
import org.bukkit.entity.Player;
import org.bukkit.entity.Skeleton;
import org.bukkit.entity.Villager;
Expand Down Expand Up @@ -292,7 +291,33 @@ public void spawnAt(Location location) {

if (entity_type.name().matches("FALLING_BLOCK")) {

ent = location.getWorld().spawnFallingBlock(location, 12, (byte) 0);
Material material = null;

if (data != null) {

material = dMaterial.valueOf(data).getMaterial();

// If we did not get a block with "RANDOM", or we got
// air or portals, keep trying
while (data.equals("RANDOM") &&
(material.isBlock() == false ||
material == Material.AIR ||
material == Material.PORTAL ||
material == Material.ENDER_PORTAL)) {

material = dMaterial.valueOf(data).getMaterial();
}
}

// If material is null, not a block, a portal section or air,
// default to SAND
if (material == null || material.isBlock() == false) {

material = Material.SAND;
}

// This is currently the only way to spawn a falling block
ent = location.getWorld().spawnFallingBlock(location, material, (byte) 0);
entity = ent;
}

Expand Down Expand Up @@ -387,7 +412,7 @@ public void setSubtype (Class<? extends Entity> entityClass, String typeName, St

if (value.matches("RANDOM")) {

entityClass.getMethod(method, typeClass).invoke(entity, types[new Random().nextInt(types.length)]);
entityClass.getMethod(method, typeClass).invoke(entity, types[Utilities.getRandom().nextInt(types.length)]);
}
else {
for (Object type : types) {
Expand Down
144 changes: 144 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dMaterial.java
@@ -0,0 +1,144 @@
package net.aufdemrand.denizen.objects;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.aufdemrand.denizen.utilities.Utilities;
import org.bukkit.Material;

import net.aufdemrand.denizen.tags.Attribute;

public class dMaterial implements dObject {

final static Pattern materialPattern = Pattern.compile("(\\w+):?(\\w+)?");

//////////////////
// OBJECT FETCHER
////////////////

/**
* Gets a Material Object from a string form.
*
* @param string the string
* @return a Material, or null if incorrectly formatted
*
*/
public static dMaterial valueOf(String string) {

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

// Get a random material
return new dMaterial(Material.values()[Utilities.getRandom().nextInt(Material.values().length)]);
}

Matcher m = materialPattern.matcher(string);

if (m.matches()) {

if (aH.matchesInteger(m.group(1))) {

return new dMaterial(aH.getIntegerFrom(m.group(1)));
}
else {

for (Material material : Material.values()) {

if (material.name().equalsIgnoreCase(m.group(1))) {

return new dMaterial(material);
}
}
}
}

// No match
return null;
}

/**
* Determine whether a string is a valid material.
*
* @param string the string
* @return true if matched, otherwise false
*
*/
public static boolean matches(String arg) {

if (arg.toUpperCase().matches("RANDOM"))
return true;

Matcher m = materialPattern.matcher(arg);

if (m.matches())
return true;

return false;

}


///////////////
// Constructors
/////////////

public dMaterial(Material material) {
this.material = material;
}

public dMaterial(int id) {
this.material = Material.getMaterial(id);
}

/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////

// Bukkit Material associated

private Material material;

public Material getMaterial() {
return material;
}


@Override
public String getPrefix() {
// TODO Auto-generated method stub
return null;
}

@Override
public String debug() {
// TODO Auto-generated method stub
return null;
}

@Override
public boolean isUnique() {
return false;
}

@Override
public String getType() {
return "material";
}

@Override
public String identify() {
return null;
}

@Override
public dObject setPrefix(String prefix) {
// TODO Auto-generated method stub
return null;
}

@Override
public String getAttribute(Attribute attribute) {
// TODO Auto-generated method stub
return null;
}

}
Expand Up @@ -12,6 +12,7 @@
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;

Expand Down Expand Up @@ -55,7 +56,7 @@ else if (aH.matchesValueArg("type", arg, ArgumentType.String)) {

if (typeArg.matches("RANDOM")) {

type = FireworkEffect.Type.values()[new Random().nextInt(FireworkEffect.Type.values().length)];
type = FireworkEffect.Type.values()[Utilities.getRandom().nextInt(FireworkEffect.Type.values().length)];
}
else {

Expand Down
11 changes: 7 additions & 4 deletions src/main/java/net/aufdemrand/denizen/utilities/debugging/dB.java
Expand Up @@ -258,12 +258,9 @@ public static void echoError(String message, String arg) {
ConsoleSender.sendMessage(ChatColor.LIGHT_PURPLE + " " + ChatColor.RED + "ERROR! " + ChatColor.WHITE + String.format(message, arg));
}

/*
* These methods do NOT require DebugMode to be enabled
*/

@SuppressWarnings("restriction")
public static void log(String message, String arg) {
if (!debugMode) return;
ConsoleSender.sendMessage(ChatColor.YELLOW + "+> ["
+ (sun.reflect.Reflection.getCallerClass(2).getSimpleName().length() > 16 ?
sun.reflect.Reflection.getCallerClass(2).getSimpleName().substring(0, 12) + "..."
Expand All @@ -273,6 +270,7 @@ public static void log(String message, String arg) {

@SuppressWarnings("restriction")
public static void log(Messages message, String arg) {
if (!debugMode) return;
ConsoleSender.sendMessage(ChatColor.YELLOW + "+> ["
+ (sun.reflect.Reflection.getCallerClass(2).getSimpleName().length() > 16 ?
sun.reflect.Reflection.getCallerClass(2).getSimpleName().substring(0, 12) + "..."
Expand All @@ -282,11 +280,16 @@ public static void log(Messages message, String arg) {

@SuppressWarnings("restriction")
public static void log(String message) {
if (!debugMode) return;
ConsoleSender.sendMessage(ChatColor.YELLOW + "+> ["
+ (sun.reflect.Reflection.getCallerClass(2).getSimpleName().length() > 16 ?
sun.reflect.Reflection.getCallerClass(2).getSimpleName().substring(0, 12) + "..."
: sun.reflect.Reflection.getCallerClass(2).getSimpleName()) + "] " + ChatColor.WHITE + message);
}

/*
* These methods do NOT require DebugMode to be enabled
*/

public static void notify(Player player, String message, String arg) {
player.sendMessage(ChatColor.YELLOW + "+> " + ChatColor.WHITE + String.format(message, arg));
Expand Down

0 comments on commit 1f6dbc1

Please sign in to comment.