Skip to content

Commit

Permalink
Add a 'max blocks' config setting
Browse files Browse the repository at this point in the history
To prevent tag-powered server crashes.
  • Loading branch information
mcmonkey4eva committed Nov 14, 2014
1 parent 4106719 commit 752f782
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/main/java/net/aufdemrand/denizen/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ public static boolean worldScriptChatEventAsynchronous() {
public static Duration worldScriptTimeEventFrequency() {
return Duration.valueOf(DenizenAPI.getCurrentInstance().getConfig()
.getString("Scripts.World.Events.On time changes.Frequency of check", "250t"));
}

public static int blockTagsMaxBlocks() {
return DenizenAPI.getCurrentInstance().getConfig()
.getInt("Tags.Block tags.Max blocks", 1000000);
}
}
30 changes: 29 additions & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dCuboid.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.objects.notable.Notable;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.objects.notable.Note;
Expand Down Expand Up @@ -289,6 +290,8 @@ public dList getOutline() {
// | +---|-+
// |/ |/
// 1-----+
int max = Settings.blockTagsMaxBlocks();
int index = 0;

dList list = new dList();

Expand Down Expand Up @@ -320,6 +323,9 @@ public dList getOutline() {
loc_2.getBlockX(),
y,
loc_1.getBlockZ()).identify());
index++;
if (index > max)
return list;
}

for (int x = loc_1.getBlockX(); x <= loc_1.getBlockX() + x_distance; x++) {
Expand All @@ -342,6 +348,9 @@ public dList getOutline() {
x,
loc_2.getBlockY(),
loc_1.getBlockZ()).identify());
index++;
if (index > max)
return list;
}

for (int z = loc_1.getBlockZ(); z <= loc_1.getBlockZ() + z_distance; z++) {
Expand All @@ -364,6 +373,9 @@ public dList getOutline() {
loc_2.getBlockX(),
loc_1.getBlockY(),
z).identify());
index++;
if (index > max)
return list;
}
}

Expand All @@ -388,8 +400,10 @@ private boolean matchesMaterialList(Location loc, List<dMaterial> materials) {
}

public dList getBlocks(List<dMaterial> materials) {
int max = Settings.blockTagsMaxBlocks();
dLocation loc;
dList list = new dList();
int index = 0;

for (LocationPair pair : pairs) {

Expand Down Expand Up @@ -418,6 +432,9 @@ public dList getBlocks(List<dMaterial> materials) {
list.add(loc.identify());
}
}
index++;
if (index > max)
return list;
}
}
}
Expand All @@ -428,8 +445,10 @@ public dList getBlocks(List<dMaterial> materials) {
}

public List<dLocation> getBlockLocations() {
int max = Settings.blockTagsMaxBlocks();
dLocation loc;
List<dLocation> list = new ArrayList<dLocation>();
int index = 0;

for (LocationPair pair : pairs) {

Expand All @@ -449,8 +468,12 @@ public List<dLocation> getBlockLocations() {
if (loc.getBlock().getType().name().equalsIgnoreCase(((dMaterial) material)
.getMaterial().name()))
list.add(loc);
} else
}
else
list.add(loc);
index++;
if (index > max)
return list;
}
}
}
Expand All @@ -474,8 +497,10 @@ public dList getSpawnableBlocks() {
*/

public dList getSpawnableBlocks(List<dMaterial> mats) {
int max = Settings.blockTagsMaxBlocks();
dLocation loc;
dList list = new dList();
int index = 0;

for (LocationPair pair : pairs) {

Expand All @@ -500,6 +525,9 @@ && matchesMaterialList(loc.clone().add(0, -1, 0), mats)) {
loc.add(0.5, 0, 0.5);
list.add(loc.identify());
}
index++;
if (index > max)
return list;
}
}
}
Expand Down
28 changes: 21 additions & 7 deletions src/main/java/net/aufdemrand/denizen/objects/dLocation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.aufdemrand.denizen.objects;

import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.objects.notable.Notable;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.objects.notable.Note;
Expand Down Expand Up @@ -717,27 +718,34 @@ else if (yaw < 315)
materials = dList.valueOf(attribute.getContext(1)).filter(dMaterial.class);
// Avoid NPE from invalid materials
if (materials == null) return null;
int max = Settings.blockTagsMaxBlocks();
int index = 0;

// dB.log(materials + " " + radius + " ");
attribute.fulfill(2);
Location loc = getBlock().getLocation().add(0.5f, 0.5f, 0.5f);

fullloop:
for (double x = -(radius); x <= radius; x++)
for (double y = -(radius); y <= radius; y++)
for (double z = -(radius); z <= radius; z++)
for (double z = -(radius); z <= radius; z++) {
index++;
if (index > max)
break fullloop;
if (index > max)
if (Utilities.checkLocation(loc, getBlock().getLocation().add(x, y, z), radius))
if (!materials.isEmpty()) {
for (dMaterial material : materials) {
if (material.hasData() && material.getData() != 0) {
if (material.matchesMaterialData(getBlock()
.getLocation().add(x,y,z).getBlock().getType().getNewData(getBlock()
.getLocation().add(x,y,z).getBlock().getData())))
.getLocation().add(x, y, z).getBlock().getType().getNewData(getBlock()
.getLocation().add(x, y, z).getBlock().getData())))
found.add(new dLocation(getBlock().getLocation().add(x + 0.5, y, z + 0.5)));
}
else if (material.getMaterial() == getBlock().getLocation().add(x,y,z).getBlock().getType())
} else if (material.getMaterial() == getBlock().getLocation().add(x, y, z).getBlock().getType())
found.add(new dLocation(getBlock().getLocation().add(x + 0.5, y, z + 0.5)));
}
} else found.add(new dLocation(getBlock().getLocation().add(x + 0.5, y, z + 0.5)));
}
} else found.add(new dLocation(getBlock().getLocation().add(x + 0.5, y, z + 0.5)));
}

Collections.sort(found, new Comparator<dLocation>() {
@Override
Expand Down Expand Up @@ -765,13 +773,19 @@ else if (attribute.startsWith("surface_blocks")
materials = dList.valueOf(attribute.getContext(1)).filter(dMaterial.class);
// Avoid NPE from invalid materials
if (materials == null) return null;
int max = Settings.blockTagsMaxBlocks();
int index = 0;

attribute.fulfill(2);
Location loc = getBlock().getLocation().add(0.5f, 0.5f, 0.5f);

fullloop:
for (double x = -(radius); x <= radius; x++)
for (double y = -(radius); y <= radius; y++)
for (double z = -(radius); z <= radius; z++) {
index++;
if (index > max)
break fullloop;
if (Utilities.checkLocation(loc, getBlock().getLocation().add(x, y, z), radius)) {
Location l = getBlock().getLocation().clone().add(x,y,z);
if (!materials.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,12 @@ public void switchBlock(ScriptEntry scriptEntry, Location interactLocation, Swit

try {
if (interactLocation.getBlock().getType() == Material.IRON_DOOR_BLOCK) {
interactLocation.getBlock().setData((byte) (interactLocation.getBlock().getData() ^ 4));
Location block = null;
if (interactLocation.clone().add(0, 1, 0).getBlock().getType() == Material.IRON_DOOR_BLOCK)
block = interactLocation.clone().add(0, 1, 0);
else if (interactLocation.clone().add(0, -1, 0).getBlock().getType() == Material.IRON_DOOR_BLOCK)
Location block;
if (interactLocation.clone().add(0, -1, 0).getBlock().getType() == Material.IRON_DOOR_BLOCK)
block = interactLocation.clone().add(0, -1, 0);
if (block != null)
block.getBlock().setData((byte) (block.getBlock().getData() ^ 4));
else
block = interactLocation;
block.getBlock().setData((byte) (block.getBlock().getData() ^ 4));
}
else {

Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ Commands:
# Otherwise, a bad script or plugin could potentially damage things
# ALSO: Always read scripts you download for anything suspicious looking, EG the log command, the file_delete mechanism, ...

Tags:
# Settings for tags like cuboid.get_blocks, location.find.blocks, ...
Block tags:
# How many blocks can be read, max, before stopping the tag in place
max blocks: 1000000


# The version of this configuration file, used to check if your
# configuration file is outdated or too new
Config:
Expand Down

0 comments on commit 752f782

Please sign in to comment.