Skip to content

Commit

Permalink
Add nearby player tag. Format :player,minSearch,maxSearch,min#,max#. …
Browse files Browse the repository at this point in the history
…Chainable, invertible. Closes #34

Fix KeyParserRange error: bounds should be inclusive.
  • Loading branch information
Crudedragos committed Jun 3, 2013
1 parent d8d95e2 commit 54f32ab
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public enum Key {
KeyParserOpaqueBlock.class), solidSide("solidSide", KeyParserSolidSide.class), difficulty("difficulty",
KeyParserDifficulty.class), torchLight("torchLight", KeyParserTorchLight.class), ground("ground",
KeyParserGround.class), top("top", KeyParserTop.class), fill("fill", KeyParserFill.class), modspawn(
"modSpawn", KeyParserModSpawn.class), origin("origin", KeyParserOrigin.class),
"modSpawn", KeyParserModSpawn.class), origin("origin", KeyParserOrigin.class), players("players",
KeyParserPlayers.class),

/* Sub Tags */
blockRangeX("blockRangeX", null), blockRangeY("blockRangeY", null), blockRangeZ("blockRangeZ", null),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package jas.common.spawner.creature.handler.parsing.keys;

import jas.common.JASLog;
import jas.common.spawner.creature.handler.parsing.ParsingHelper;
import jas.common.spawner.creature.handler.parsing.TypeValuePair;
import jas.common.spawner.creature.handler.parsing.settings.OptionalSettings.Operand;

import java.util.ArrayList;
import java.util.HashMap;

import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;

public class KeyParserPlayers extends KeyParserBase {

public KeyParserPlayers(Key key) {
super(key, true, KeyType.CHAINABLE);
}

@Override
public boolean parseChainable(String parseable, ArrayList<TypeValuePair> parsedChainable,
ArrayList<Operand> operandvalue) {
String[] pieces = parseable.split(",");
Operand operand = getOperand(pieces);

if (pieces.length == 5) {
int minSearchRange = ParsingHelper.parseFilteredInteger(pieces[1], 32, "1st " + key.key);
int maxSearchRange = ParsingHelper.parseFilteredInteger(pieces[2], 32, "1st " + key.key);
int min = ParsingHelper.parseFilteredInteger(pieces[3], 16, "2st " + key.key);
int max = ParsingHelper.parseFilteredInteger(pieces[4], -1, "3nd " + key.key);
TypeValuePair typeValue = new TypeValuePair(key, new Object[] { isInverted(pieces[0]), minSearchRange,
maxSearchRange, min, max });
parsedChainable.add(typeValue);
operandvalue.add(operand);
return true;
} else {
JASLog.severe("Error Parsing %s Parameter. Invalid Argument Length.", key.key);
return false;
}
}

@Override
public boolean parseValue(String parseable, HashMap<String, Object> valueCache) {
throw new UnsupportedOperationException();
}

@Override
public boolean isValidLocation(World world, EntityLiving entity, int xCoord, int yCoord, int zCoord,
TypeValuePair typeValuePair, HashMap<String, Object> valueCache) {
Object[] values = (Object[]) typeValuePair.getValue();
boolean isInverted = (Boolean) values[0];
int minSearch = (Integer) values[1];
int maxSearch = (Integer) values[2];

int current = countNearbyPlayers(world, xCoord, yCoord, zCoord, minSearch, maxSearch);
int minRange = (Integer) values[3];
int maxRange = (Integer) values[4];

boolean isValid;
if (minRange <= maxRange) {
isValid = (current <= maxRange && current >= minRange);
} else {
isValid = !(current < minRange && current > maxRange);
}
return isInverted ? isValid : !isValid;
}

private int countNearbyPlayers(World world, int xCoord, int yCoord, int zCoord, int minRange, int maxRange) {
int count = 0;
for (int i = 0; i < world.playerEntities.size(); ++i) {
EntityPlayer player = (EntityPlayer) world.playerEntities.get(i);
if (player.isEntityAlive()) {
int distance = (int) Math.sqrt(player.getDistanceSq(xCoord, yCoord, zCoord));
if (maxRange >= minRange && distance >= minRange && distance <= maxRange) {
count++;
continue;
} else if (maxRange < minRange && !(distance < minRange && distance > maxRange)) {
count++;
continue;
}
}
}
return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public boolean parseChainable(String parseable, ArrayList<TypeValuePair> parsedC
TypeValuePair typeValue = new TypeValuePair(key, new Object[] { isInverted(pieces[0]), min, max });
parsedChainable.add(typeValue);
operandvalue.add(operand);
return true;
} else {
JASLog.severe("Error Parsing %s Parameter. Invalid Argument Length.", key.key);
return false;
}
return false;
}

@Override
Expand All @@ -55,7 +55,7 @@ public boolean isValidLocation(World world, EntityLiving entity, int xCoord, int
if (minRange <= maxRange) {
isValid = (current <= maxRange && current >= minRange);
} else {
isValid = !(current <= minRange && current >= maxRange);
isValid = !(current < minRange && current > maxRange);
}
return isInverted ? isValid : !isValid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class OptionalSettingsCreatureTypeSpawn extends OptionalSettingsBase {
public OptionalSettingsCreatureTypeSpawn(String parseableString) {
super(parseableString.replace("}", ""), EnumSet.of(Key.spawn, Key.light, Key.block, Key.blockRange,
Key.blockFoot, Key.spawnRange, Key.sky, Key.minSpawnHeight, Key.maxSpawnHeight, Key.liquid, Key.opaque,
Key.normal, Key.solidSide, Key.difficulty, Key.torchLight, Key.ground, Key.top, Key.fill, Key.origin));
Key.normal, Key.solidSide, Key.difficulty, Key.torchLight, Key.ground, Key.top, Key.fill, Key.origin,
Key.players));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public OptionalSettingsDespawning(String parseableString) {
super(parseableString.replace("}", ""), EnumSet.of(Key.despawn, Key.light, Key.block, Key.blockFoot,
Key.blockRange, Key.spawnRange, Key.spawnRate, Key.sky, Key.despawnAge, Key.maxSpawnRange,
Key.minSpawnHeight, Key.maxSpawnHeight, Key.liquid, Key.opaque, Key.normal, Key.solidSide,
Key.difficulty, Key.torchLight, Key.ground, Key.top, Key.fill, Key.origin));
Key.difficulty, Key.torchLight, Key.ground, Key.top, Key.fill, Key.origin, Key.players));
}

public int getRate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public OptionalSettingsSpawning(String parseableString) {
super(parseableString.replace("}", ""), EnumSet.of(Key.spawn, Key.light, Key.block, Key.blockRange,
Key.blockFoot, Key.spawnRange, Key.sky, Key.entityCap, Key.minSpawnHeight, Key.maxSpawnHeight,
Key.liquid, Key.opaque, Key.normal, Key.solidSide, Key.difficulty, Key.torchLight, Key.ground, Key.top,
Key.fill, Key.modspawn, Key.origin));
Key.fill, Key.modspawn, Key.origin, Key.players));
}

public Integer getEntityCap() {
Expand Down

0 comments on commit 54f32ab

Please sign in to comment.